forked from KolibriOS/kolibrios
- Rebuilt TinyPy
- Non-working trash is cleaned. - Updated from latest git version. - Fixed modules pygame math and others. - Removed old modules added new ones. - All samples work except "net" git-svn-id: svn://kolibrios.org@8535 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
13
programs/develop/tinypy/std_modules/bitwise/bitwise.c
Normal file
13
programs/develop/tinypy/std_modules/bitwise/bitwise.c
Normal file
@@ -0,0 +1,13 @@
|
||||
/* Copyright (C) 2019-2021 Logaev Maxim (turbocat2001), GPLv3 */
|
||||
|
||||
#include "tinypy.h"
|
||||
|
||||
#define GET_NUM_ARG() TP_TYPE(TP_NUMBER).number.val
|
||||
#define GET_STR_ARG() TP_TYPE(TP_STRING).string.val
|
||||
|
||||
static tp_obj _add(TP){
|
||||
unsigned num1 = (unsigned)GET_NUM_ARG();
|
||||
unsigned num2 = (unsigned)GET_NUM_ARG();
|
||||
return tp_number(num1 | num2);
|
||||
}
|
||||
|
17
programs/develop/tinypy/std_modules/bitwise/init.c
Executable file
17
programs/develop/tinypy/std_modules/bitwise/init.c
Executable file
@@ -0,0 +1,17 @@
|
||||
#include "tinypy.h"
|
||||
#include "bitwise.c"
|
||||
|
||||
#define EXPORT(MOD_NAME, F_NAME, F_POINT) tp_set(tp, MOD_NAME , tp_string(F_NAME), tp_fnc(tp, F_POINT))
|
||||
|
||||
void bitwise_init(TP)
|
||||
{
|
||||
tp_obj bit_mod = tp_dict(tp);
|
||||
|
||||
EXPORT(bit_mod, "add" , _add);
|
||||
|
||||
tp_set(tp, bit_mod, tp_string("__doc__"), tp_string("Bitwise operations for large numbers"));
|
||||
tp_set(tp, bit_mod, tp_string("__name__"), tp_string("bitwise"));
|
||||
tp_set(tp, bit_mod, tp_string("__file__"), tp_string(__FILE__));
|
||||
|
||||
tp_set(tp, tp->modules, tp_string("bitwise"), bit_mod);
|
||||
}
|
255
programs/develop/tinypy/std_modules/ksys/fs.c
Executable file
255
programs/develop/tinypy/std_modules/ksys/fs.c
Executable file
@@ -0,0 +1,255 @@
|
||||
#include <string.h>
|
||||
#include "tinypy.h"
|
||||
|
||||
#define call70(par, st) __asm__ __volatile__("int $0x40":"=a"(st):"a"(70), "b"(par))
|
||||
#define call70_rw(par, st, cnt) __asm__ __volatile__ ("int $0x40":"=a"(st), "=b"(cnt):"a"(70), "b"(par))
|
||||
|
||||
#define GET_STR_ARG() TP_TYPE(TP_STRING).string.val
|
||||
|
||||
typedef unsigned int uint32_t;
|
||||
typedef unsigned char uint8_t;
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef struct {
|
||||
uint32_t subfnc;
|
||||
uint32_t res1;
|
||||
uint32_t res2;
|
||||
uint32_t res3;
|
||||
uint8_t *data;
|
||||
uint8_t res4;
|
||||
char *fn;
|
||||
}info_params_t;
|
||||
#pragma pack(pop)
|
||||
|
||||
#pragma pack(push,1)
|
||||
typedef struct {
|
||||
uint32_t subfnc;
|
||||
uint32_t pos;
|
||||
uint32_t res1;
|
||||
uint32_t cnt;
|
||||
char* data;
|
||||
uint8_t res2;
|
||||
char* fn;
|
||||
}rw_params_t;
|
||||
#pragma pack(pop)
|
||||
|
||||
static tp_obj kolibri_close(TP)
|
||||
{
|
||||
tp_obj self = TP_TYPE(TP_DICT);
|
||||
uint32_t size = tp_get(tp, self, tp_string("size")).number.val;
|
||||
char *mode = (char *)tp_get(tp, self, tp_string("mode")).string.val;
|
||||
tp_set(tp, self, tp_string("closed"), tp_True);
|
||||
return tp_None;
|
||||
}
|
||||
|
||||
static tp_obj kolibri_read(TP)
|
||||
{
|
||||
uint32_t status, num;
|
||||
tp_obj self = TP_TYPE(TP_DICT);
|
||||
uint32_t pos = tp_get(tp, self, tp_string("pos")).number.val;
|
||||
uint32_t size = tp_get(tp, self, tp_string("size")).number.val;
|
||||
uint32_t cnt;
|
||||
char *buf = (char *)malloc(size - pos);
|
||||
rw_params_t params = {0, pos, 0, size - pos, buf, 0,
|
||||
(char *)tp_get(tp, self, tp_string("name")).string.val};
|
||||
char *mode = (char *)tp_get(tp, self, tp_string("mode")).string.val;
|
||||
|
||||
if (*mode != 'r')
|
||||
tp_raise_f(tp_None, "IOError: file not open for reading", tp_None);
|
||||
|
||||
if (!buf)
|
||||
return tp_None;
|
||||
call70_rw((¶ms), status, cnt);
|
||||
buf[cnt] = '\0';
|
||||
return tp_string(buf);
|
||||
}
|
||||
|
||||
#if 0
|
||||
static tp_obj kolibri_readline(TP)
|
||||
{
|
||||
return tp_string("Line");
|
||||
}
|
||||
|
||||
static tp_obj kolibri_readlines(TP)
|
||||
{
|
||||
tp_obj result = tp_list(tp);
|
||||
int i;
|
||||
|
||||
for(i=0; i < 5; i++)
|
||||
tp_add(result, tp_string("Line"));
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Write object to file.
|
||||
*
|
||||
* tp TinyPy virtual machine structure.
|
||||
*
|
||||
* returns tp_True
|
||||
*/
|
||||
static tp_obj kolibri_write(TP)
|
||||
{
|
||||
tp_obj self = TP_TYPE(TP_DICT);
|
||||
tp_obj obj = TP_OBJ(); /* What to write. */
|
||||
char *mode = (char *)tp_get(tp, self, tp_string("mode")).string.val;
|
||||
uint32_t pos = (uint32_t)tp_get(tp, self, tp_string("pos")).number.val;
|
||||
uint32_t size = (uint32_t)tp_get(tp, self, tp_string("size")).number.val;
|
||||
|
||||
if (*mode != 'w' && *mode != 'a')
|
||||
{
|
||||
tp_raise_f(tp_None, "IOError: file not open for writing", tp_None);
|
||||
}
|
||||
else
|
||||
{
|
||||
char *data = (char *)TP_CSTR(obj);
|
||||
rw_params_t params = {3, pos, 0, strlen(data), data, 0,
|
||||
(char *)tp_get(tp, self, tp_string("name")).string.val};
|
||||
uint32_t status;
|
||||
uint32_t cnt;
|
||||
call70_rw((¶ms), status, cnt);
|
||||
if (status)
|
||||
{
|
||||
tp_raise_f(tp_None, "IOError: writing failed with status %d", status);
|
||||
}
|
||||
pos += cnt;
|
||||
tp_set(tp, self, tp_string("pos"), tp_number(pos));
|
||||
if (pos > size)
|
||||
{
|
||||
/* If writing has come beyond the file, increase its size. */
|
||||
tp_set(tp, self, tp_string("size"), tp_number(pos));
|
||||
}
|
||||
return tp_True;
|
||||
}
|
||||
}
|
||||
|
||||
/* Write line list into file.
|
||||
*
|
||||
* tp TinyPy virtual machine structure.
|
||||
*
|
||||
* returns tp_None.
|
||||
*/
|
||||
static tp_obj kolibri_writelines(TP)
|
||||
{
|
||||
tp_obj result = tp_None;
|
||||
tp_obj self = TP_TYPE(TP_DICT);
|
||||
tp_obj list = TP_TYPE(TP_LIST); /* What to write. */
|
||||
char *mode = (char *)tp_get(tp, self, tp_string("mode")).string.val;
|
||||
long pos = (long)tp_get(tp, self, tp_string("pos")).number.val;
|
||||
uint32_t size = (uint32_t)tp_get(tp, self, tp_string("size")).number.val;
|
||||
|
||||
if (*mode != 'w' && *mode != 'a')
|
||||
{
|
||||
tp_raise_f(tp_None, "IOError: file not open for writing", tp_None);
|
||||
}
|
||||
else
|
||||
{
|
||||
int i;
|
||||
char *fn = (char *)tp_get(tp, self, tp_string("name")).string.val;
|
||||
rw_params_t params = {3, 0, 0, 0, NULL, 0, fn};
|
||||
|
||||
for (i = 0; i < list.list.val->len; i++)
|
||||
{
|
||||
char *data = (char *)TP_CSTR(list.list.val->items[i]);
|
||||
uint32_t status;
|
||||
uint32_t cnt;
|
||||
|
||||
params.data = data;
|
||||
params.cnt = strlen(data);
|
||||
params.pos = pos;
|
||||
|
||||
call70_rw((¶ms), status, cnt);
|
||||
if (status)
|
||||
tp_raise_f(tp_None, "IOError: writing failed with status %d", status);
|
||||
pos += cnt;
|
||||
}
|
||||
tp_set(tp, self, tp_string("pos"), tp_number(pos));
|
||||
if (pos > size)
|
||||
{
|
||||
/* If writing has come beyond the file, increase its size. */
|
||||
tp_set(tp, self, tp_string("size"), tp_number(pos));
|
||||
}
|
||||
return tp_True;
|
||||
}
|
||||
}
|
||||
|
||||
/* Get file size.
|
||||
*
|
||||
* fn ASCIIZ absolute file path.
|
||||
*/
|
||||
long long int kolibri_filesize(const char *fn)
|
||||
{
|
||||
uint8_t data[40];
|
||||
uint32_t status;
|
||||
long long int result;
|
||||
info_params_t params = {5, 0, 0, 0, data, 0, (char *)fn};
|
||||
|
||||
call70((¶ms), status);
|
||||
/* File size is at offset 32. */
|
||||
if (status == 0)
|
||||
result = *(long long*)(&data[32]);
|
||||
else
|
||||
result = -status;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/* Open file.
|
||||
*
|
||||
* tp TinyPy virtual machine structure.
|
||||
*
|
||||
* returns file object.
|
||||
*/
|
||||
tp_obj kolibri_open(TP) {
|
||||
tp_obj obj;
|
||||
char *fn = (char *)(TP_TYPE(TP_STRING).string.val);
|
||||
tp_obj mode_obj = TP_OBJ();
|
||||
long long int size;
|
||||
long long int pos = 0;
|
||||
uint32_t status;
|
||||
info_params_t params = {2, 0, 0, 0, NULL, 0, fn};
|
||||
|
||||
if (mode_obj.type == TP_NONE)
|
||||
mode_obj = tp_string("r");
|
||||
else if (mode_obj.type != TP_STRING)
|
||||
tp_raise_f(tp_None, "ValueError: bad file access mode %s", TP_CSTR(mode_obj));
|
||||
switch(mode_obj.string.val[0])
|
||||
{
|
||||
case 'w':
|
||||
call70((¶ms), status);
|
||||
if (status)
|
||||
tp_raise_f(tp_None, "IOError: cannot open file for writing", tp_None);
|
||||
size = 0;
|
||||
break;
|
||||
case 'a':
|
||||
pos = size;
|
||||
break;
|
||||
case 'r':
|
||||
break;
|
||||
default:
|
||||
tp_raise_f(tp_None, "ValueError: mode string must begin with 'r', 'w', or 'a'", tp_None);
|
||||
}
|
||||
if ((size = kolibri_filesize(fn)) < 0)
|
||||
tp_raise_f(tp_None, "IOError: filesize returned %d", tp_number(size));
|
||||
obj = tp_dict(tp);
|
||||
tp_set(tp, obj, tp_string("name"), tp_string(fn));
|
||||
tp_set(tp, obj, tp_string("size"), tp_number(size));
|
||||
tp_set(tp, obj, tp_string("pos"), tp_number(pos));
|
||||
tp_set(tp, obj, tp_string("mode"), mode_obj);
|
||||
#if 0
|
||||
tp_set(tp, obj, tp_string("__doc__"),
|
||||
tp_string("File object.\nAttributes:\n name: file name\n"
|
||||
" closed: boolean indicating whether file was closed\n"
|
||||
"Methods:\n read: read the entire file into string\n"
|
||||
" readlines: get list of lines\n"
|
||||
" close: close the file\n"
|
||||
));
|
||||
#endif
|
||||
tp_set(tp, obj, tp_string("closed"), tp_False);
|
||||
tp_set(tp, obj, tp_string("close"), tp_method(tp, obj, kolibri_close));
|
||||
tp_set(tp, obj, tp_string("read"), tp_method(tp, obj, kolibri_read));
|
||||
tp_set(tp, obj, tp_string("write"), tp_method(tp, obj, kolibri_write));
|
||||
tp_set(tp, obj, tp_string("writelines"), tp_method(tp, obj, kolibri_writelines));
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
32
programs/develop/tinypy/std_modules/ksys/init.c
Executable file
32
programs/develop/tinypy/std_modules/ksys/init.c
Executable file
@@ -0,0 +1,32 @@
|
||||
#include "tinypy.h"
|
||||
#include "syscalls.c"
|
||||
#include "fs.c"
|
||||
|
||||
#define EXPORT(MOD_NAME, F_NAME, F_POINT) tp_set(tp, MOD_NAME , tp_string(F_NAME), tp_fnc(tp, F_POINT))
|
||||
|
||||
extern tp_obj tp_dict(TP);
|
||||
extern tp_obj tp_fnc(TP,tp_obj v(TP));
|
||||
|
||||
|
||||
void ksys_init(TP)
|
||||
{
|
||||
tp_obj ksys_mod = tp_dict(tp);
|
||||
// syscalls
|
||||
EXPORT(ksys_mod, "debug_print" , _debug_print);
|
||||
EXPORT(ksys_mod, "start_draw" , _start_draw);
|
||||
EXPORT(ksys_mod, "end_draw" , _end_draw);
|
||||
EXPORT(ksys_mod, "create_window", _create_window);
|
||||
EXPORT(ksys_mod, "create_button", _create_button);
|
||||
EXPORT(ksys_mod, "draw_text" , _draw_text);
|
||||
EXPORT(ksys_mod, "get_event" , _get_event);
|
||||
EXPORT(ksys_mod, "get_button" ,_get_button);
|
||||
EXPORT(ksys_mod, "get_sys_colors",_get_sys_colors);
|
||||
// filesystem
|
||||
EXPORT(ksys_mod, "open", kolibri_open);
|
||||
|
||||
tp_set(tp, ksys_mod, tp_string("__doc__"), tp_string("KolibriOS system specific functions."));
|
||||
tp_set(tp, ksys_mod, tp_string("__name__"), tp_string("kolibri"));
|
||||
tp_set(tp, ksys_mod, tp_string("__file__"), tp_string(__FILE__));
|
||||
|
||||
tp_set(tp, tp->modules, tp_string("ksys"), ksys_mod);
|
||||
}
|
296
programs/develop/tinypy/std_modules/ksys/net.c
Normal file
296
programs/develop/tinypy/std_modules/ksys/net.c
Normal file
@@ -0,0 +1,296 @@
|
||||
#include <sys/socket.h>
|
||||
// #include <menuet/net.h>
|
||||
|
||||
#include "tp.h"
|
||||
|
||||
extern tp_obj tp_dict(TP);
|
||||
extern tp_obj tp_method(TP,tp_obj self,tp_obj v(TP));
|
||||
extern tp_obj tp_fnc(TP,tp_obj v(TP));
|
||||
extern tp_obj tp_get(TP,tp_obj self, tp_obj k);
|
||||
tp_obj tp_has(TP,tp_obj self, tp_obj k);
|
||||
// #define _cdecl __attribute__((cdecl))
|
||||
extern int (* _cdecl con_printf)(const char* format,...);
|
||||
|
||||
#define PRECISION 0.000001
|
||||
|
||||
#define GET_SOCKET_DESCRIPTOR(_obj, _sock) do{ \
|
||||
if (fabs(tp_has(tp, _obj, tp_string("socket")).number.val) < PRECISION)\
|
||||
tp_raise(tp_None, "Socket not open", tp_None); \
|
||||
_sock = (__u32)(tp_get(tp, _obj, tp_string("socket")).number.val + PRECISION);\
|
||||
} while(0)
|
||||
|
||||
/* Socket close method.
|
||||
*
|
||||
* Example:
|
||||
* s.close() # s must be a socket object created by socket.
|
||||
*
|
||||
* Raises exception if socket was not opened. Otherwise returns True.
|
||||
*/
|
||||
static tp_obj kolibri_close_socket(TP)
|
||||
{
|
||||
// tp_obj self = TP_TYPE(TP_DICT);
|
||||
// __u32 socktype;
|
||||
// __u32 s;
|
||||
|
||||
// GET_SOCKET_DESCRIPTOR(self, s);
|
||||
|
||||
// socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
|
||||
// GET_SOCKET_DESCRIPTOR(self, s);
|
||||
// if (socktype == SOCK_STREAM)
|
||||
// __menuet__close_TCP_socket(s);
|
||||
// else if (socktype == SOCK_DGRAM)
|
||||
// __menuet__close_UDP_socket(s);
|
||||
return tp_True;
|
||||
}
|
||||
|
||||
/* Socket send method.
|
||||
*
|
||||
* Example:
|
||||
* data="<html><head><title>Preved!!!</title></head><body>Example.</body></html>"
|
||||
* s.send(data)
|
||||
* or:
|
||||
* s.send(data, 20) # Send just 20 bytes
|
||||
*/
|
||||
static tp_obj kolibri_send(TP)
|
||||
{
|
||||
// tp_obj self = TP_TYPE(TP_DICT);
|
||||
// tp_obj data_obj = TP_TYPE(TP_STRING);
|
||||
// __u32 datalen = TP_DEFAULT(tp_False).number.val;
|
||||
// __u32 socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
|
||||
// __u32 s;
|
||||
// int result;
|
||||
|
||||
// GET_SOCKET_DESCRIPTOR(self, s);
|
||||
|
||||
// if (datalen < 0 || datalen > data_obj.string.len)
|
||||
// datalen = data_obj.string.len;
|
||||
// if (socktype == SOCK_STREAM)
|
||||
// result = __menuet__write_TCP_socket(s, datalen, (void *)data_obj.string.val);
|
||||
// else if (socktype == SOCK_DGRAM)
|
||||
// result = __menuet__write_UDP_socket(s, datalen, (void *)data_obj.string.val);
|
||||
// return tp_number(!(result != 0));
|
||||
return tp_number(0);
|
||||
}
|
||||
|
||||
#define __u32 unsigned int
|
||||
#define __u8 unsigned char
|
||||
|
||||
/* Socket recv method.
|
||||
*
|
||||
* data="<html><head><title>Preved!!!</title></head><body>Example.</body></html>"
|
||||
* s.recv(data)
|
||||
* or:
|
||||
* s.recv(data, 20) # Send just 20 bytes
|
||||
*/
|
||||
static tp_obj kolibri_recv(TP)
|
||||
{
|
||||
tp_obj self = TP_TYPE(TP_DICT);
|
||||
__u32 datalen = TP_DEFAULT(tp_False).number.val;
|
||||
__u32 s;
|
||||
__u8 c;
|
||||
__u8 *buf, *p;
|
||||
__u32 buf_size;
|
||||
__u32 bytes_read = 0;
|
||||
int i;
|
||||
|
||||
// GET_SOCKET_DESCRIPTOR(self, s);
|
||||
|
||||
// if (datalen)
|
||||
// buf_size = datalen;
|
||||
// else
|
||||
// buf_size = 2048;
|
||||
// if (!(buf = malloc(datalen)))
|
||||
// tp_raise(tp_None, "Cannot allocate buffer for received data", tp_None);
|
||||
// p = buf;
|
||||
// while (__menuet__read_socket(s, &c) && bytes_read < buf_size)
|
||||
// {
|
||||
// *p++ = c;
|
||||
// bytes_read++;
|
||||
// if (bytes_read >= buf_size && !datalen)
|
||||
// {
|
||||
// buf_size += 1024;
|
||||
// buf = realloc(buf, buf_size);
|
||||
// }
|
||||
// }
|
||||
return tp_string_n(buf, bytes_read);
|
||||
}
|
||||
|
||||
static void inet_pton(TP, const char *buf, int len, __u32 *addr)
|
||||
{
|
||||
char *p = (char *)buf;
|
||||
int i = 0;
|
||||
__u32 val = 0;
|
||||
*addr = 0;
|
||||
while (*p && p < buf + len && i < 4)
|
||||
{
|
||||
if (*p == '.' || !*p)
|
||||
{
|
||||
if (val > 255)
|
||||
tp_raise(tp_None, "ValueError: number > 255 in IP address", tp_None);
|
||||
*addr += (val << ((i++) << 3));
|
||||
val = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (*p < '0' || *p > '9')
|
||||
tp_raise(tp_None, "ValueError: bad char in IP address, digit expected", tp_None);
|
||||
val = val * 10 + *p - '0';
|
||||
}
|
||||
p++;
|
||||
}
|
||||
if (!*p)
|
||||
{
|
||||
if (i == 3)
|
||||
*addr += (val << ((i++) << 3));
|
||||
else
|
||||
tp_raise(tp_None, "ValueError: bad IP address", tp_None);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* Converter from string presentation to binary address. */
|
||||
static tp_obj kolibri_inet_pton(TP)
|
||||
{
|
||||
tp_obj obj;
|
||||
__u32 addr;
|
||||
obj = TP_TYPE(TP_STRING);
|
||||
// inet_pton(tp, (char *)obj.string.val, (int)obj.string.len, &addr);
|
||||
return tp_number(addr);
|
||||
}
|
||||
|
||||
/* Socket bind method.
|
||||
*
|
||||
* In KolibriOS it just sets local address and port.
|
||||
*
|
||||
* Example:
|
||||
* s.bind('10.10.1.2', 6000) #Connects to 10.10.1.2:6000
|
||||
*/
|
||||
tp_obj kolibri_bind(TP)
|
||||
{
|
||||
// tp_obj self = TP_TYPE(TP_DICT);
|
||||
// tp_obj local_addr_obj = TP_OBJ();
|
||||
// __u32 local_port = (__u32)TP_TYPE(TP_NUMBER).number.val;
|
||||
// __u32 local_addr;
|
||||
|
||||
// if (local_addr_obj.type == TP_NUMBER)
|
||||
// local_addr = local_addr_obj.number.val;
|
||||
// else if (local_addr_obj.type == TP_STRING)
|
||||
// inet_pton(tp, (const char *)local_addr_obj.string.val, local_addr_obj.string.len, &local_addr);
|
||||
|
||||
// tp_set(tp, self, tp_string("local_addr"), tp_number(local_addr));
|
||||
// tp_set(tp, self, tp_string("local_port"), tp_number(local_port));
|
||||
return tp_None;
|
||||
}
|
||||
|
||||
/* Socket connect method.
|
||||
*
|
||||
* Example:
|
||||
* s.connect('10.10.1.1', 7000) #Connects to 10.10.1.1:7000
|
||||
*/
|
||||
tp_obj kolibri_connect(TP)
|
||||
{
|
||||
// tp_obj self = TP_TYPE(TP_DICT);
|
||||
// tp_obj remote_addr_obj = TP_OBJ();
|
||||
// __u32 remote_addr;
|
||||
// __u32 remote_port = (__u32)TP_TYPE(TP_NUMBER).number.val;
|
||||
// __u32 local_port = tp_get(tp, self, tp_string("local_port")).number.val;
|
||||
// __u32 socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
|
||||
// int s = -1; /* Socket descriptor */
|
||||
|
||||
|
||||
// if (remote_addr_obj.type == TP_NUMBER)
|
||||
// remote_addr = remote_addr_obj.number.val;
|
||||
// else if (remote_addr_obj.type == TP_STRING)
|
||||
// inet_pton(tp, (const char *)remote_addr_obj.string.val, remote_addr_obj.string.len, &remote_addr);
|
||||
|
||||
// if (socktype == SOCK_STREAM)
|
||||
// s = __menuet__open_TCP_socket(local_port, remote_port, remote_addr, 1);
|
||||
// else if (socktype == SOCK_DGRAM)
|
||||
// s = __menuet__open_UDP_socket(local_port, remote_port, remote_addr);
|
||||
// if (s >= 0)
|
||||
// {
|
||||
// tp_set(tp, self, tp_string("socket"), tp_number(s));
|
||||
// return tp_True;
|
||||
// }
|
||||
// else
|
||||
return tp_False;
|
||||
}
|
||||
|
||||
/* Socket listen method.
|
||||
*
|
||||
* Example:
|
||||
* s.listen('10.10.1.1', 5000)
|
||||
*/
|
||||
tp_obj kolibri_listen(TP)
|
||||
{
|
||||
// tp_obj self = TP_TYPE(TP_DICT);
|
||||
// tp_obj remote_addr_obj = TP_OBJ();
|
||||
// __u32 remote_addr;
|
||||
// __u32 remote_port = (__u32)TP_TYPE(TP_NUMBER).number.val;
|
||||
// __u32 local_port = tp_get(tp, self, tp_string("local_port")).number.val;
|
||||
// __u32 socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
|
||||
// int s = -1; /* Socket descriptor */
|
||||
|
||||
// if (socktype != SOCK_STREAM)
|
||||
// tp_raise(tp_None, "IOError: attempt to listen on non-TCP socket", tp_None);
|
||||
|
||||
// if (remote_addr_obj.type == TP_NUMBER)
|
||||
// remote_addr = remote_addr_obj.number.val;
|
||||
// else if (remote_addr_obj.type == TP_STRING)
|
||||
// inet_pton(tp, (const char *)remote_addr_obj.string.val, remote_addr_obj.string.len, &remote_addr);
|
||||
|
||||
// if ((s = __menuet__open_TCP_socket(local_port, remote_port, remote_addr, 0)) >= 0)
|
||||
// {
|
||||
// tp_set(tp, self, tp_string("socket"), tp_number(s));
|
||||
// return tp_True;
|
||||
// }
|
||||
// else
|
||||
return tp_False;
|
||||
}
|
||||
|
||||
|
||||
/* Exported function.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* s = socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
*
|
||||
* Returns socket object.
|
||||
*/
|
||||
tp_obj kolibri_socket(TP)
|
||||
{
|
||||
tp_obj s;
|
||||
tp_obj sockfamily = TP_TYPE(TP_NUMBER);
|
||||
tp_obj socktype = TP_TYPE(TP_NUMBER);
|
||||
|
||||
// if (fabs(sockfamily.number.val - AF_INET) > PRECISION ||
|
||||
// (fabs(socktype.number.val - SOCK_STREAM) > PRECISION &&
|
||||
// fabs(socktype.number.val - SOCK_DGRAM) > PRECISION))
|
||||
// return tp_None;
|
||||
s = tp_dict(tp);
|
||||
// tp_set(tp, s, tp_string("family"), sockfamily);
|
||||
// tp_set(tp, s, tp_string("type"), socktype);
|
||||
// tp_set(tp, s, tp_string("bind"), tp_method(tp, s, kolibri_bind));
|
||||
// tp_set(tp, s, tp_string("connect"), tp_method(tp, s, kolibri_connect));
|
||||
// tp_set(tp, s, tp_string("send"), tp_method(tp, s, kolibri_send));
|
||||
// tp_set(tp, s, tp_string("recv"), tp_method(tp, s, kolibri_recv));
|
||||
// tp_set(tp, s, tp_string("close"), tp_method(tp, s, kolibri_close_socket));
|
||||
// if (fabs(socktype.number.val - SOCK_STREAM) < PRECISION)
|
||||
// tp_set(tp, s, tp_string("listen"), tp_method(tp, s, kolibri_listen));
|
||||
return s;
|
||||
}
|
||||
|
||||
tp_obj kolibri_socket_module(TP)
|
||||
{
|
||||
tp_obj socket_mod = tp_dict(tp);
|
||||
|
||||
// tp_set(tp, socket_mod, tp_string("AF_INET"), tp_number(AF_INET));
|
||||
// tp_set(tp, socket_mod, tp_string("SOCK_STREAM"), tp_number(SOCK_STREAM));
|
||||
// tp_set(tp, socket_mod, tp_string("SOCK_DGRAM"), tp_number(SOCK_DGRAM));
|
||||
tp_set(tp, socket_mod, tp_string("AF_INET"), tp_number(0));
|
||||
tp_set(tp, socket_mod, tp_string("SOCK_STREAM"), tp_number(0));
|
||||
tp_set(tp, socket_mod, tp_string("SOCK_DGRAM"), tp_number(0));
|
||||
tp_set(tp, socket_mod, tp_string("inet_pton"), tp_fnc(tp, kolibri_inet_pton));
|
||||
tp_set(tp, socket_mod, tp_string("socket"), tp_fnc(tp, kolibri_socket));
|
||||
return socket_mod;
|
||||
}
|
92
programs/develop/tinypy/std_modules/ksys/syscalls.c
Normal file
92
programs/develop/tinypy/std_modules/ksys/syscalls.c
Normal file
@@ -0,0 +1,92 @@
|
||||
/* Copyright (C) 2019-2021 Logaev Maxim (turbocat2001), GPLv3 */
|
||||
|
||||
#include "tinypy.h"
|
||||
#include <kos32sys.h>
|
||||
|
||||
#define GET_NUM_ARG() TP_TYPE(TP_NUMBER).number.val
|
||||
#define GET_STR_ARG() TP_TYPE(TP_STRING).string.val
|
||||
|
||||
void debug_write_byte(const char ch){
|
||||
__asm__ __volatile__(
|
||||
"int $0x40"
|
||||
::"a"(63), "b"(1), "c"(ch)
|
||||
);
|
||||
}
|
||||
|
||||
static tp_obj _debug_print(TP){
|
||||
tp_obj str = TP_TYPE(TP_STRING);
|
||||
for(int i=0; i < str.string.len; i++)
|
||||
{
|
||||
debug_write_byte(str.string.val[i]);
|
||||
}
|
||||
return tp_None;
|
||||
}
|
||||
|
||||
static tp_obj _start_draw(TP){
|
||||
begin_draw();
|
||||
return tp_None;
|
||||
}
|
||||
|
||||
|
||||
static tp_obj _end_draw(TP){
|
||||
end_draw();
|
||||
return tp_None;
|
||||
}
|
||||
|
||||
static tp_obj _create_window(TP){
|
||||
int x = GET_NUM_ARG();
|
||||
int y = GET_NUM_ARG();
|
||||
int w = GET_NUM_ARG();
|
||||
int h = GET_NUM_ARG();
|
||||
const char *title= GET_STR_ARG();
|
||||
unsigned int color = GET_NUM_ARG();
|
||||
unsigned int style = GET_NUM_ARG();
|
||||
sys_create_window(x,y,w,h, title, color,style);
|
||||
return tp_None;
|
||||
}
|
||||
|
||||
static tp_obj _create_button(TP){
|
||||
unsigned int x = GET_NUM_ARG();
|
||||
unsigned int y = GET_NUM_ARG();
|
||||
unsigned int h = GET_NUM_ARG();
|
||||
unsigned int w = GET_NUM_ARG();
|
||||
unsigned int id = GET_NUM_ARG();
|
||||
unsigned int color = GET_NUM_ARG();
|
||||
define_button((x << 16) + w, (y << 16) + h, id, color);
|
||||
return tp_None;
|
||||
}
|
||||
|
||||
static tp_obj _draw_text(TP){
|
||||
const char *str= GET_STR_ARG();
|
||||
int x = GET_NUM_ARG();
|
||||
int y = GET_NUM_ARG();
|
||||
int len = GET_NUM_ARG();
|
||||
unsigned color = (unsigned)GET_NUM_ARG();
|
||||
draw_text_sys(str, x, y, len, color);
|
||||
return tp_None;
|
||||
}
|
||||
|
||||
static tp_obj _get_event(TP){
|
||||
return tp_number(get_os_event());
|
||||
}
|
||||
|
||||
static tp_obj _get_button(TP){
|
||||
return tp_number(get_os_button());
|
||||
}
|
||||
|
||||
static tp_obj _get_sys_colors(TP){
|
||||
tp_obj color_obj = tp_dict(tp);
|
||||
struct kolibri_system_colors colors;
|
||||
get_system_colors(&colors);
|
||||
tp_set(tp, color_obj, tp_string("frame_area"), tp_number(colors.frame_area));
|
||||
tp_set(tp, color_obj, tp_string("grab_bar"), tp_number(colors.grab_bar));
|
||||
tp_set(tp, color_obj, tp_string("grab_bar_button)"), tp_number(colors.grab_bar_button));
|
||||
tp_set(tp, color_obj, tp_string( "grab_button_text)"), tp_number(colors.grab_button_text));
|
||||
tp_set(tp, color_obj, tp_string("grab_text"), tp_number(colors.grab_text));
|
||||
tp_set(tp, color_obj, tp_string("work_area"), tp_number(colors.work_area));
|
||||
tp_set(tp, color_obj, tp_string("work_button"), tp_number(colors.work_button));
|
||||
tp_set(tp, color_obj, tp_string("work_button_text"), tp_number(colors.work_button_text));
|
||||
tp_set(tp, color_obj, tp_string("work_graph"), tp_number(colors.work_graph));
|
||||
tp_set(tp, color_obj, tp_string("work_text"), tp_number(colors.work_text));
|
||||
return color_obj;
|
||||
}
|
65
programs/develop/tinypy/std_modules/math/init.c
Normal file
65
programs/develop/tinypy/std_modules/math/init.c
Normal file
@@ -0,0 +1,65 @@
|
||||
#include "math.c"
|
||||
|
||||
/*
|
||||
* init math module, namely, set its dictionary
|
||||
*/
|
||||
void math_init(TP)
|
||||
{
|
||||
/*
|
||||
* new a module dict for math
|
||||
*/
|
||||
tp_obj math_mod = tp_dict(tp);
|
||||
|
||||
/*
|
||||
* initialize pi and e
|
||||
*/
|
||||
math_pi = tp_number(M_PI);
|
||||
math_e = tp_number(M_E);
|
||||
|
||||
/*
|
||||
* bind math functions to math module
|
||||
*/
|
||||
tp_set(tp, math_mod, tp_string("pi"), math_pi);
|
||||
tp_set(tp, math_mod, tp_string("e"), math_e);
|
||||
tp_set(tp, math_mod, tp_string("acos"), tp_fnc(tp, math_acos));
|
||||
tp_set(tp, math_mod, tp_string("asin"), tp_fnc(tp, math_asin));
|
||||
tp_set(tp, math_mod, tp_string("atan"), tp_fnc(tp, math_atan));
|
||||
tp_set(tp, math_mod, tp_string("atan2"), tp_fnc(tp, math_atan2));
|
||||
tp_set(tp, math_mod, tp_string("ceil"), tp_fnc(tp, math_ceil));
|
||||
tp_set(tp, math_mod, tp_string("cos"), tp_fnc(tp, math_cos));
|
||||
tp_set(tp, math_mod, tp_string("cosh"), tp_fnc(tp, math_cosh));
|
||||
tp_set(tp, math_mod, tp_string("degrees"), tp_fnc(tp, math_degrees));
|
||||
tp_set(tp, math_mod, tp_string("exp"), tp_fnc(tp, math_exp));
|
||||
tp_set(tp, math_mod, tp_string("fabs"), tp_fnc(tp, math_fabs));
|
||||
tp_set(tp, math_mod, tp_string("floor"), tp_fnc(tp, math_floor));
|
||||
tp_set(tp, math_mod, tp_string("fmod"), tp_fnc(tp, math_fmod));
|
||||
tp_set(tp, math_mod, tp_string("frexp"), tp_fnc(tp, math_frexp));
|
||||
tp_set(tp, math_mod, tp_string("hypot"), tp_fnc(tp, math_hypot));
|
||||
tp_set(tp, math_mod, tp_string("ldexp"), tp_fnc(tp, math_ldexp));
|
||||
tp_set(tp, math_mod, tp_string("log"), tp_fnc(tp, math_log));
|
||||
tp_set(tp, math_mod, tp_string("log10"), tp_fnc(tp, math_log10));
|
||||
tp_set(tp, math_mod, tp_string("modf"), tp_fnc(tp, math_modf));
|
||||
tp_set(tp, math_mod, tp_string("pow"), tp_fnc(tp, math_pow));
|
||||
tp_set(tp, math_mod, tp_string("radians"), tp_fnc(tp, math_radians));
|
||||
tp_set(tp, math_mod, tp_string("sin"), tp_fnc(tp, math_sin));
|
||||
tp_set(tp, math_mod, tp_string("sinh"), tp_fnc(tp, math_sinh));
|
||||
tp_set(tp, math_mod, tp_string("sqrt"), tp_fnc(tp, math_sqrt));
|
||||
tp_set(tp, math_mod, tp_string("tan"), tp_fnc(tp, math_tan));
|
||||
tp_set(tp, math_mod, tp_string("tanh"), tp_fnc(tp, math_tanh));
|
||||
|
||||
/*
|
||||
* bind special attributes to math module
|
||||
*/
|
||||
tp_set(tp, math_mod, tp_string("__doc__"),
|
||||
tp_string(
|
||||
"This module is always available. It provides access to the\n"
|
||||
"mathematical functions defined by the C standard."));
|
||||
tp_set(tp, math_mod, tp_string("__name__"), tp_string("math"));
|
||||
tp_set(tp, math_mod, tp_string("__file__"), tp_string(__FILE__));
|
||||
|
||||
/*
|
||||
* bind to tiny modules[]
|
||||
*/
|
||||
tp_set(tp, tp->modules, tp_string("math"), math_mod);
|
||||
}
|
||||
|
366
programs/develop/tinypy/std_modules/math/math.c
Normal file
366
programs/develop/tinypy/std_modules/math/math.c
Normal file
@@ -0,0 +1,366 @@
|
||||
#include <math.h>
|
||||
#include "tinypy.h"
|
||||
|
||||
#ifndef M_E
|
||||
#define M_E 2.7182818284590452354
|
||||
#endif
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
/*
|
||||
* template for tinypy math functions
|
||||
* with one parameter.
|
||||
*
|
||||
* @cfunc is the coresponding function name in C
|
||||
* math library.
|
||||
*/
|
||||
#define TP_MATH_FUNC1(cfunc) \
|
||||
static tp_obj math_##cfunc(TP) { \
|
||||
double x = TP_NUM(); \
|
||||
double r = 0.0; \
|
||||
\
|
||||
errno = 0; \
|
||||
r = cfunc(x); \
|
||||
if (errno == EDOM || errno == ERANGE) { \
|
||||
tp_raise(tp_None, tp_printf(tp, "%s(x): x=%f " \
|
||||
"out of range", __func__, x)); \
|
||||
} \
|
||||
\
|
||||
return (tp_number(r)); \
|
||||
}
|
||||
|
||||
/*
|
||||
* template for tinypy math functions
|
||||
* with two parameters.
|
||||
*
|
||||
* @cfunc is the coresponding function name in C
|
||||
* math library.
|
||||
*/
|
||||
#define TP_MATH_FUNC2(cfunc) \
|
||||
static tp_obj math_##cfunc(TP) { \
|
||||
double x = TP_NUM(); \
|
||||
double y = TP_NUM(); \
|
||||
double r = 0.0; \
|
||||
\
|
||||
errno = 0; \
|
||||
r = cfunc(x, y); \
|
||||
if (errno == EDOM || errno == ERANGE) { \
|
||||
tp_raise(tp_None, tp_printf(tp, "%s(x, y): x=%f,y=%f " \
|
||||
"out of range", __func__, x, y)); \
|
||||
} \
|
||||
\
|
||||
return (tp_number(r)); \
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* PI definition: 3.1415926535897931
|
||||
*/
|
||||
static tp_obj math_pi;
|
||||
|
||||
/*
|
||||
* E definition: 2.7182818284590451
|
||||
*/
|
||||
static tp_obj math_e;
|
||||
|
||||
/*
|
||||
* acos(x)
|
||||
*
|
||||
* return arc cosine of x, return value is measured in radians.
|
||||
* if x falls out -1 to 1, raise out-of-range exception.
|
||||
*/
|
||||
TP_MATH_FUNC1(acos)
|
||||
|
||||
/*
|
||||
* asin(x)
|
||||
*
|
||||
* return arc sine of x, measured in radians, actually [-PI/2, PI/2]
|
||||
* if x falls out of -1 to 1, raise out-of-range exception
|
||||
*/
|
||||
TP_MATH_FUNC1(asin)
|
||||
|
||||
/*
|
||||
* atan(x)
|
||||
*
|
||||
* return arc tangent of x, measured in radians,
|
||||
*/
|
||||
TP_MATH_FUNC1(atan)
|
||||
|
||||
/*
|
||||
* atan2(x, y)
|
||||
*
|
||||
* return arc tangent of x/y, measured in radians.
|
||||
* unlike atan(x/y), both the signs of x and y
|
||||
* are considered to determine the quaderant of
|
||||
* the result.
|
||||
*/
|
||||
TP_MATH_FUNC2(atan2)
|
||||
|
||||
/*
|
||||
* ceil(x)
|
||||
*
|
||||
* return the ceiling of x, i.e, the smallest
|
||||
* integer >= x.
|
||||
*/
|
||||
TP_MATH_FUNC1(ceil)
|
||||
|
||||
/*
|
||||
* cos(x)
|
||||
*
|
||||
* return cosine of x. x is measured in radians.
|
||||
*/
|
||||
TP_MATH_FUNC1(cos)
|
||||
|
||||
/*
|
||||
* cosh(x)
|
||||
*
|
||||
* return hyperbolic cosine of x.
|
||||
*/
|
||||
TP_MATH_FUNC1(cosh)
|
||||
|
||||
/*
|
||||
* degrees(x)
|
||||
*
|
||||
* converts angle x from radians to degrees.
|
||||
* NOTE: this function is introduced by python,
|
||||
* so we cannot wrap it directly in TP_MATH_FUNC1(),
|
||||
* here the solution is defining a new
|
||||
* C function - degrees().
|
||||
*/
|
||||
static const double degToRad =
|
||||
3.141592653589793238462643383 / 180.0;
|
||||
static double degrees(double x)
|
||||
{
|
||||
return (x / degToRad);
|
||||
}
|
||||
|
||||
TP_MATH_FUNC1(degrees)
|
||||
|
||||
/*
|
||||
* exp(x)
|
||||
*
|
||||
* return the value e raised to power of x.
|
||||
* e is the base of natural logarithms.
|
||||
*/
|
||||
TP_MATH_FUNC1(exp)
|
||||
|
||||
/*
|
||||
* fabs(x)
|
||||
*
|
||||
* return the absolute value of x.
|
||||
*/
|
||||
TP_MATH_FUNC1(fabs)
|
||||
|
||||
/*
|
||||
* floor(x)
|
||||
*
|
||||
* return the floor of x, i.e, the largest integer <= x
|
||||
*/
|
||||
TP_MATH_FUNC1(floor)
|
||||
|
||||
/*
|
||||
* fmod(x, y)
|
||||
*
|
||||
* return the remainder of dividing x by y. that is,
|
||||
* return x - n * y, where n is the quotient of x/y.
|
||||
* NOTE: this function relies on the underlying platform.
|
||||
*/
|
||||
TP_MATH_FUNC2(fmod)
|
||||
|
||||
/*
|
||||
* frexp(x)
|
||||
*
|
||||
* return a pair (r, y), which satisfies:
|
||||
* x = r * (2 ** y), and r is normalized fraction
|
||||
* which is laid between 1/2 <= abs(r) < 1.
|
||||
* if x = 0, the (r, y) = (0, 0).
|
||||
*/
|
||||
static tp_obj math_frexp(TP) {
|
||||
double x = TP_NUM();
|
||||
int y = 0;
|
||||
double r = 0.0;
|
||||
tp_obj rList = tp_list(tp);
|
||||
|
||||
errno = 0;
|
||||
r = frexp(x, &y);
|
||||
if (errno == EDOM || errno == ERANGE) {
|
||||
tp_raise(tp_None, tp_printf(tp, "%s(x): x=%f, "
|
||||
"out of range", __func__, x));
|
||||
}
|
||||
|
||||
_tp_list_append(tp, rList.list.val, tp_number(r));
|
||||
_tp_list_append(tp, rList.list.val, tp_number((tp_num)y));
|
||||
return (rList);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* hypot(x, y)
|
||||
*
|
||||
* return Euclidean distance, namely,
|
||||
* sqrt(x*x + y*y)
|
||||
*/
|
||||
TP_MATH_FUNC2(hypot)
|
||||
|
||||
|
||||
/*
|
||||
* ldexp(x, y)
|
||||
*
|
||||
* return the result of multiplying x by 2
|
||||
* raised to y.
|
||||
*/
|
||||
TP_MATH_FUNC2(ldexp)
|
||||
|
||||
/*
|
||||
* log(x, [base])
|
||||
*
|
||||
* return logarithm of x to given base. If base is
|
||||
* not given, return the natural logarithm of x.
|
||||
* Note: common logarithm(log10) is used to compute
|
||||
* the denominator and numerator. based on fomula:
|
||||
* log(x, base) = log10(x) / log10(base).
|
||||
*/
|
||||
static tp_obj math_log(TP) {
|
||||
double x = TP_NUM();
|
||||
tp_obj b = TP_DEFAULT(tp_None);
|
||||
double y = 0.0;
|
||||
double den = 0.0; /* denominator */
|
||||
double num = 0.0; /* numinator */
|
||||
double r = 0.0; /* result */
|
||||
|
||||
if (b.type == TP_NONE)
|
||||
y = M_E;
|
||||
else if (b.type == TP_NUMBER)
|
||||
y = (double)b.number.val;
|
||||
else
|
||||
tp_raise(tp_None, tp_printf(tp, "%s(x, [base]): base invalid", __func__));
|
||||
|
||||
errno = 0;
|
||||
num = log10(x);
|
||||
if (errno == EDOM || errno == ERANGE)
|
||||
goto excep;
|
||||
|
||||
errno = 0;
|
||||
den = log10(y);
|
||||
if (errno == EDOM || errno == ERANGE)
|
||||
goto excep;
|
||||
|
||||
r = num / den;
|
||||
|
||||
return (tp_number(r));
|
||||
|
||||
excep:
|
||||
tp_raise(tp_None, tp_printf(tp, "%s(x, y): x=%f,y=%f "
|
||||
"out of range", __func__, x, y));
|
||||
}
|
||||
|
||||
/*
|
||||
* log10(x)
|
||||
*
|
||||
* return 10-based logarithm of x.
|
||||
*/
|
||||
TP_MATH_FUNC1(log10)
|
||||
|
||||
/*
|
||||
* modf(x)
|
||||
*
|
||||
* return a pair (r, y). r is the integral part of
|
||||
* x and y is the fractional part of x, both holds
|
||||
* the same sign as x.
|
||||
*/
|
||||
static tp_obj math_modf(TP) {
|
||||
double x = TP_NUM();
|
||||
double y = 0.0;
|
||||
double r = 0.0;
|
||||
tp_obj rList = tp_list(tp);
|
||||
|
||||
errno = 0;
|
||||
r = modf(x, &y);
|
||||
if (errno == EDOM || errno == ERANGE) {
|
||||
tp_raise(tp_None, tp_printf(tp, "%s(x): x=%f, "
|
||||
"out of range", __func__, x));
|
||||
}
|
||||
|
||||
_tp_list_append(tp, rList.list.val, tp_number(r));
|
||||
_tp_list_append(tp, rList.list.val, tp_number(y));
|
||||
return (rList);
|
||||
}
|
||||
|
||||
/*
|
||||
* pow(x, y)
|
||||
*
|
||||
* return value of x raised to y. equivalence of x ** y.
|
||||
* NOTE: conventionally, tp_pow() is the implementation
|
||||
* of builtin function pow(); whilst, math_pow() is an
|
||||
* alternative in math module.
|
||||
*/
|
||||
static tp_obj math_pow(TP) {
|
||||
double x = TP_NUM();
|
||||
double y = TP_NUM();
|
||||
double r = 0.0;
|
||||
|
||||
errno = 0;
|
||||
r = pow(x, y);
|
||||
if (errno == EDOM || errno == ERANGE) {
|
||||
tp_raise(tp_None, tp_printf(tp, "%s(x, y): x=%f,y=%f "
|
||||
"out of range", __func__, x, y));
|
||||
}
|
||||
|
||||
return (tp_number(r));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* radians(x)
|
||||
*
|
||||
* converts angle x from degrees to radians.
|
||||
* NOTE: this function is introduced by python,
|
||||
* adopt same solution as degrees(x).
|
||||
*/
|
||||
static double radians(double x)
|
||||
{
|
||||
return (x * degToRad);
|
||||
}
|
||||
|
||||
TP_MATH_FUNC1(radians)
|
||||
|
||||
/*
|
||||
* sin(x)
|
||||
*
|
||||
* return sine of x, x is measured in radians.
|
||||
*/
|
||||
TP_MATH_FUNC1(sin)
|
||||
|
||||
/*
|
||||
* sinh(x)
|
||||
*
|
||||
* return hyperbolic sine of x.
|
||||
* mathematically, sinh(x) = (exp(x) - exp(-x)) / 2.
|
||||
*/
|
||||
TP_MATH_FUNC1(sinh)
|
||||
|
||||
/*
|
||||
* sqrt(x)
|
||||
*
|
||||
* return square root of x.
|
||||
* if x is negtive, raise out-of-range exception.
|
||||
*/
|
||||
TP_MATH_FUNC1(sqrt)
|
||||
|
||||
/*
|
||||
* tan(x)
|
||||
*
|
||||
* return tangent of x, x is measured in radians.
|
||||
*/
|
||||
TP_MATH_FUNC1(tan)
|
||||
|
||||
/*
|
||||
* tanh(x)
|
||||
*
|
||||
* return hyperbolic tangent of x.
|
||||
* mathematically, tanh(x) = sinh(x) / cosh(x).
|
||||
*/
|
||||
TP_MATH_FUNC1(tanh)
|
176
programs/develop/tinypy/std_modules/math/tests.py
Normal file
176
programs/develop/tinypy/std_modules/math/tests.py
Normal file
@@ -0,0 +1,176 @@
|
||||
### This file is grabbed from Python's Lib/test/test_math.py
|
||||
###---------------------------------------------------------
|
||||
# Python test set -- math module
|
||||
# XXXX Should not do tests around zero only
|
||||
|
||||
eps = 0.00001
|
||||
#print('math module, testing with eps ', eps)
|
||||
import math
|
||||
|
||||
def testit(name, value, expected):
|
||||
if abs(value-expected) > eps:
|
||||
msg = name + " returned " + str(value) + " expected " + str(expected)
|
||||
raise msg
|
||||
|
||||
#print 'constants'
|
||||
testit('pi', math.pi, 3.1415926)
|
||||
testit('e', math.e, 2.7182818)
|
||||
|
||||
#print 'acos'
|
||||
testit('acos(-1)', math.acos(-1), math.pi)
|
||||
testit('acos(0)', math.acos(0), math.pi/2)
|
||||
testit('acos(1)', math.acos(1), 0)
|
||||
|
||||
#print 'asin'
|
||||
testit('asin(-1)', math.asin(-1), -math.pi/2)
|
||||
testit('asin(0)', math.asin(0), 0)
|
||||
testit('asin(1)', math.asin(1), math.pi/2)
|
||||
|
||||
#print 'atan'
|
||||
testit('atan(-1)', math.atan(-1), -math.pi/4)
|
||||
testit('atan(0)', math.atan(0), 0)
|
||||
testit('atan(1)', math.atan(1), math.pi/4)
|
||||
|
||||
#print 'atan2'
|
||||
testit('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
|
||||
testit('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
|
||||
testit('atan2(0, 1)', math.atan2(0, 1), 0)
|
||||
testit('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
|
||||
testit('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
|
||||
|
||||
#print 'ceil'
|
||||
testit('ceil(0.5)', math.ceil(0.5), 1)
|
||||
testit('ceil(1.0)', math.ceil(1.0), 1)
|
||||
testit('ceil(1.5)', math.ceil(1.5), 2)
|
||||
testit('ceil(-0.5)', math.ceil(-0.5), 0)
|
||||
testit('ceil(-1.0)', math.ceil(-1.0), -1)
|
||||
testit('ceil(-1.5)', math.ceil(-1.5), -1)
|
||||
|
||||
#print 'cos'
|
||||
testit('cos(-pi/2)', math.cos(-math.pi/2), 0)
|
||||
testit('cos(0)', math.cos(0), 1)
|
||||
testit('cos(pi/2)', math.cos(math.pi/2), 0)
|
||||
testit('cos(pi)', math.cos(math.pi), -1)
|
||||
|
||||
#print 'cosh'
|
||||
testit('cosh(0)', math.cosh(0), 1)
|
||||
testit('cosh(2)-2*(cosh(1)**2)', math.cosh(2)-2*(math.cosh(1)**2), -1) # Thanks to Lambert
|
||||
|
||||
#print 'degrees'
|
||||
testit('degrees(pi)', math.degrees(math.pi), 180.0)
|
||||
testit('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
|
||||
testit('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
|
||||
|
||||
#print 'exp'
|
||||
testit('exp(-1)', math.exp(-1), 1/math.e)
|
||||
testit('exp(0)', math.exp(0), 1)
|
||||
testit('exp(1)', math.exp(1), math.e)
|
||||
|
||||
#print 'fabs'
|
||||
testit('fabs(-1)', math.fabs(-1), 1)
|
||||
testit('fabs(0)', math.fabs(0), 0)
|
||||
testit('fabs(1)', math.fabs(1), 1)
|
||||
|
||||
#print 'floor'
|
||||
testit('floor(0.5)', math.floor(0.5), 0)
|
||||
testit('floor(1.0)', math.floor(1.0), 1)
|
||||
testit('floor(1.5)', math.floor(1.5), 1)
|
||||
testit('floor(-0.5)', math.floor(-0.5), -1)
|
||||
testit('floor(-1.0)', math.floor(-1.0), -1)
|
||||
testit('floor(-1.5)', math.floor(-1.5), -2)
|
||||
|
||||
#print 'fmod'
|
||||
testit('fmod(10,1)', math.fmod(10,1), 0)
|
||||
testit('fmod(10,0.5)', math.fmod(10,0.5), 0)
|
||||
testit('fmod(10,1.5)', math.fmod(10,1.5), 1)
|
||||
testit('fmod(-10,1)', math.fmod(-10,1), 0)
|
||||
testit('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
|
||||
testit('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
|
||||
|
||||
#print 'frexp'
|
||||
def testfrexp(name, value, expected):
|
||||
mant = value[0]
|
||||
exp = value[1]
|
||||
emant = expected[0]
|
||||
eexp = expected[1]
|
||||
if abs(mant-emant) > eps or exp != eexp:
|
||||
raise '%s returned (%f, %f), expected (%f, %f)'%\
|
||||
(name, mant, exp, emant,eexp)
|
||||
|
||||
testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
|
||||
testfrexp('frexp(0)', math.frexp(0), (0, 0))
|
||||
testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
|
||||
testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
|
||||
|
||||
#print 'hypot'
|
||||
testit('hypot(0,0)', math.hypot(0,0), 0)
|
||||
testit('hypot(3,4)', math.hypot(3,4), 5)
|
||||
|
||||
#print 'ldexp'
|
||||
testit('ldexp(0,1)', math.ldexp(0,1), 0)
|
||||
testit('ldexp(1,1)', math.ldexp(1,1), 2)
|
||||
testit('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
|
||||
testit('ldexp(-1,1)', math.ldexp(-1,1), -2)
|
||||
|
||||
#print 'log'
|
||||
testit('log(1/e)', math.log(1/math.e), -1)
|
||||
testit('log(1)', math.log(1), 0)
|
||||
testit('log(e)', math.log(math.e), 1)
|
||||
testit('log(32,2)', math.log(32,2), 5)
|
||||
testit('log(10**40, 10)', math.log(10**40, 10), 40)
|
||||
testit('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
|
||||
|
||||
#print 'log10'
|
||||
testit('log10(0.1)', math.log10(0.1), -1)
|
||||
testit('log10(1)', math.log10(1), 0)
|
||||
testit('log10(10)', math.log10(10), 1)
|
||||
|
||||
#print 'modf'
|
||||
def testmodf(name, value, expected):
|
||||
v1 = value[0]
|
||||
v2 = value[1]
|
||||
e1 = expected[0]
|
||||
e2 = expected[1]
|
||||
if abs(v1-e1) > eps or abs(v2-e2):
|
||||
raise '%s returned (%f, %f), expected (%f, %f)'%\
|
||||
(name, v1,v2, e1,e2)
|
||||
|
||||
testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
|
||||
testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
|
||||
|
||||
#print 'pow'
|
||||
testit('pow(0,1)', math.pow(0,1), 0)
|
||||
testit('pow(1,0)', math.pow(1,0), 1)
|
||||
testit('pow(2,1)', math.pow(2,1), 2)
|
||||
testit('pow(2,-1)', math.pow(2,-1), 0.5)
|
||||
|
||||
#print 'radians'
|
||||
testit('radians(180)', math.radians(180), math.pi)
|
||||
testit('radians(90)', math.radians(90), math.pi/2)
|
||||
testit('radians(-45)', math.radians(-45), -math.pi/4)
|
||||
|
||||
#print 'sin'
|
||||
testit('sin(0)', math.sin(0), 0)
|
||||
testit('sin(pi/2)', math.sin(math.pi/2), 1)
|
||||
testit('sin(-pi/2)', math.sin(-math.pi/2), -1)
|
||||
|
||||
#print 'sinh'
|
||||
testit('sinh(0)', math.sinh(0), 0)
|
||||
testit('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
|
||||
testit('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
|
||||
|
||||
#print 'sqrt'
|
||||
testit('sqrt(0)', math.sqrt(0), 0)
|
||||
testit('sqrt(1)', math.sqrt(1), 1)
|
||||
testit('sqrt(4)', math.sqrt(4), 2)
|
||||
|
||||
#print 'tan'
|
||||
testit('tan(0)', math.tan(0), 0)
|
||||
testit('tan(pi/4)', math.tan(math.pi/4), 1)
|
||||
testit('tan(-pi/4)', math.tan(-math.pi/4), -1)
|
||||
|
||||
#print 'tanh'
|
||||
testit('tanh(0)', math.tanh(0), 0)
|
||||
testit('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
|
||||
|
||||
#print("OK: math module test pass")
|
15
programs/develop/tinypy/std_modules/modules.c
Normal file
15
programs/develop/tinypy/std_modules/modules.c
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "math/init.c"
|
||||
#include "random/init.c"
|
||||
#include "re/init.c"
|
||||
#include "ksys/init.c"
|
||||
#include "pygame/init.c"
|
||||
#include "bitwise/init.c"
|
||||
|
||||
void init_std_modules(TP){
|
||||
math_init(tp);
|
||||
random_init(tp);
|
||||
re_init(tp);
|
||||
ksys_init(tp);
|
||||
pygame_init(tp);
|
||||
bitwise_init(tp);
|
||||
}
|
182
programs/develop/tinypy/std_modules/pygame/init.c
Normal file
182
programs/develop/tinypy/std_modules/pygame/init.c
Normal file
@@ -0,0 +1,182 @@
|
||||
#include "SDL.h"
|
||||
|
||||
/* utility functions */
|
||||
Uint32 pygame_list_to_color(TP,tp_obj clr,SDL_Surface *s) {
|
||||
int r,g,b;
|
||||
r = tp_get(tp,clr,tp_number(0)).number.val;
|
||||
g = tp_get(tp,clr,tp_number(1)).number.val;
|
||||
b = tp_get(tp,clr,tp_number(2)).number.val;
|
||||
return SDL_MapRGB(s->format,r,g,b);
|
||||
}
|
||||
|
||||
/* surface */
|
||||
|
||||
#define PYGAME_TYPE_SURF 0x1001
|
||||
|
||||
void pygame_surf_free(TP,tp_obj d) {
|
||||
if (d.data.magic != PYGAME_TYPE_SURF) { tp_raise(,tp_printf(tp, "%s","not a surface")); }
|
||||
SDL_FreeSurface((SDL_Surface*)d.data.val);
|
||||
}
|
||||
|
||||
SDL_Surface *pygame_obj_to_surf(TP,tp_obj self) {
|
||||
tp_obj d = tp_get(tp,self,tp_string("__surf"));
|
||||
if (d.data.magic != PYGAME_TYPE_SURF) { tp_raise(0,tp_printf(tp, "%s","not a surface")); }
|
||||
return (SDL_Surface*)d.data.val;
|
||||
}
|
||||
|
||||
|
||||
tp_obj pygame_surface_set_at(TP) {
|
||||
tp_obj self = TP_OBJ();
|
||||
tp_obj pos = TP_TYPE(TP_LIST);
|
||||
tp_obj clr = TP_TYPE(TP_LIST);
|
||||
SDL_Rect r;
|
||||
r.x = tp_get(tp,pos,tp_number(0)).number.val;
|
||||
r.y = tp_get(tp,pos,tp_number(1)).number.val;
|
||||
r.w = 1; r.h = 1;
|
||||
SDL_Surface *s =pygame_obj_to_surf(tp,self);
|
||||
Uint32 c = pygame_list_to_color(tp,clr,s);
|
||||
SDL_FillRect(s, &r, c);
|
||||
return tp_None;
|
||||
}
|
||||
|
||||
|
||||
tp_obj pygame_surf_to_obj(TP,SDL_Surface *s) {
|
||||
tp_obj self = tp_dict(tp);
|
||||
|
||||
tp_obj d = tp_data(tp,PYGAME_TYPE_SURF,s);
|
||||
d.data.info->free = pygame_surf_free;
|
||||
|
||||
tp_set(tp,self,tp_string("__surf"),d);
|
||||
tp_set(tp,self,tp_string("set_at"),tp_method(tp,self,pygame_surface_set_at));
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* display module */
|
||||
|
||||
tp_obj pygame_display_set_mode(TP) {
|
||||
tp_obj sz = TP_TYPE(TP_LIST);
|
||||
int w = tp_get(tp,sz,tp_number(0)).number.val;
|
||||
int h = tp_get(tp,sz,tp_number(1)).number.val;
|
||||
SDL_Surface *s = SDL_SetVideoMode(w, h, 0, 0);
|
||||
return pygame_surf_to_obj(tp,s);
|
||||
}
|
||||
|
||||
tp_obj pygame_display_flip(TP) {
|
||||
SDL_Flip(SDL_GetVideoSurface());
|
||||
return tp_None;
|
||||
}
|
||||
|
||||
SDL_Rect pygame_list_to_rect(TP,tp_obj o) {
|
||||
SDL_Rect r;
|
||||
r.x = tp_get(tp,o,tp_number(0)).number.val;
|
||||
r.y = tp_get(tp,o,tp_number(1)).number.val;
|
||||
r.w = tp_get(tp,o,tp_number(2)).number.val;
|
||||
r.h = tp_get(tp,o,tp_number(3)).number.val;
|
||||
return r;
|
||||
}
|
||||
|
||||
tp_obj pygame_display_update(TP) {
|
||||
SDL_Rect r = pygame_list_to_rect(tp,TP_TYPE(TP_LIST));
|
||||
SDL_UpdateRects(SDL_GetVideoSurface(), 1, &r);
|
||||
return tp_None;
|
||||
}
|
||||
|
||||
/* event module */
|
||||
tp_obj pygame_event_get(TP) {
|
||||
SDL_Event e;
|
||||
tp_obj r = tp_list(tp);
|
||||
while (SDL_PollEvent(&e)) {
|
||||
tp_obj d = tp_dict(tp);
|
||||
tp_set(tp,d,tp_string("type"),tp_number(e.type));
|
||||
switch (e.type) {
|
||||
case SDL_KEYDOWN:
|
||||
case SDL_KEYUP:
|
||||
tp_set(tp,d,tp_string("key"),tp_number(e.key.keysym.sym));
|
||||
tp_set(tp,d,tp_string("mod"),tp_number(e.key.keysym.mod));
|
||||
break;
|
||||
case SDL_MOUSEMOTION:
|
||||
tp_set(tp,d,tp_string("pos"),tp_list_n(tp,2,(tp_obj[]){tp_number(e.motion.x),tp_number(e.motion.y)}));
|
||||
tp_set(tp,d,tp_string("rel"),tp_list_n(tp,2,(tp_obj[]){tp_number(e.motion.xrel),tp_number(e.motion.yrel)}));
|
||||
tp_set(tp,d,tp_string("state"),tp_number(e.motion.state));
|
||||
break;
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
tp_set(tp,d,tp_string("pos"),tp_list_n(tp,2,(tp_obj[]){tp_number(e.button.x),tp_number(e.button.y)}));
|
||||
tp_set(tp,d,tp_string("button"),tp_number(e.button.button));
|
||||
break;
|
||||
}
|
||||
tp_set(tp,r,tp_None,d);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
/* mouse */
|
||||
tp_obj pygame_mouse_get_pos(TP) {
|
||||
int x,y;
|
||||
SDL_GetMouseState(&x,&y);
|
||||
tp_obj r = tp_list_n(tp,2,(tp_obj[]){tp_number(x),tp_number(y)});
|
||||
return r;
|
||||
}
|
||||
|
||||
/* time */
|
||||
tp_obj pygame_time_get_ticks(TP) {
|
||||
return tp_number(SDL_GetTicks());
|
||||
}
|
||||
|
||||
|
||||
/* pygame */
|
||||
#define PYGAME_LOCALS(a,b) tp_set(tp,m,tp_string(a),tp_number(b));
|
||||
|
||||
tp_obj _pygame_init(TP) {
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
return tp_None;
|
||||
}
|
||||
|
||||
|
||||
void pygame_init(TP) {
|
||||
tp_obj g,m;
|
||||
g = tp_dict(tp);
|
||||
tp_set(tp,tp->modules,tp_string("pygame"),g);
|
||||
tp_set(tp,g,tp_string("init"),tp_fnc(tp,_pygame_init));
|
||||
|
||||
/* display */
|
||||
m = tp_dict(tp); tp_set(tp,g,tp_string("display"),m);
|
||||
tp_set(tp,m,tp_string("set_mode"),tp_fnc(tp,pygame_display_set_mode));
|
||||
tp_set(tp,m,tp_string("flip"),tp_fnc(tp,pygame_display_flip));
|
||||
tp_set(tp,m,tp_string("update"),tp_fnc(tp,pygame_display_update));
|
||||
|
||||
/* event */
|
||||
m = tp_dict(tp); tp_set(tp,g,tp_string("event"),m);
|
||||
tp_set(tp,m,tp_string("get"),tp_fnc(tp,pygame_event_get));
|
||||
|
||||
/* locals */
|
||||
m = tp_dict(tp); tp_set(tp,g,tp_string("locals"),m);
|
||||
PYGAME_LOCALS("QUIT",SDL_QUIT);
|
||||
PYGAME_LOCALS("KEYDOWN",SDL_KEYDOWN);
|
||||
PYGAME_LOCALS("KEYUP",SDL_KEYUP);
|
||||
PYGAME_LOCALS("MOUSEBUTTONDOWN",SDL_MOUSEBUTTONDOWN);
|
||||
PYGAME_LOCALS("MOUSEBUTTONUP",SDL_MOUSEBUTTONUP);
|
||||
PYGAME_LOCALS("MOUSEMOTION",SDL_MOUSEMOTION);
|
||||
PYGAME_LOCALS("K_UP",SDLK_UP);
|
||||
PYGAME_LOCALS("K_DOWN",SDLK_DOWN);
|
||||
PYGAME_LOCALS("K_LEFT",SDLK_LEFT);
|
||||
PYGAME_LOCALS("K_RIGHT",SDLK_RIGHT);
|
||||
PYGAME_LOCALS("K_ESCAPE",SDLK_ESCAPE);
|
||||
PYGAME_LOCALS("K_SPACE",SDLK_SPACE);
|
||||
PYGAME_LOCALS("K_RETURN",SDLK_RETURN);
|
||||
|
||||
/* mouse */
|
||||
m = tp_dict(tp); tp_set(tp,g,tp_string("mouse"),m);
|
||||
tp_set(tp,m,tp_string("get_pos"),tp_fnc(tp,pygame_mouse_get_pos));
|
||||
|
||||
/* time */
|
||||
m = tp_dict(tp); tp_set(tp,g,tp_string("time"),m);
|
||||
tp_set(tp,m,tp_string("get_ticks"),tp_fnc(tp,pygame_time_get_ticks));
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**/
|
52
programs/develop/tinypy/std_modules/random/init.c
Normal file
52
programs/develop/tinypy/std_modules/random/init.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "random.c"
|
||||
|
||||
/*
|
||||
* random_mod_init()
|
||||
*
|
||||
* random module initialization function
|
||||
*/
|
||||
void random_init(TP)
|
||||
{
|
||||
/*
|
||||
* module dict for random
|
||||
*/
|
||||
tp_obj random_mod = tp_dict(tp);
|
||||
|
||||
/*
|
||||
* bind functions to random module
|
||||
*/
|
||||
tp_set(tp, random_mod, tp_string("seed"), tp_fnc(tp, random_seed));
|
||||
tp_set(tp, random_mod, tp_string("getstate"), tp_fnc(tp, random_getstate));
|
||||
tp_set(tp, random_mod, tp_string("setstate"), tp_fnc(tp, random_setstate));
|
||||
tp_set(tp, random_mod, tp_string("jumpahead"), tp_fnc(tp, random_jumpahead));
|
||||
tp_set(tp, random_mod, tp_string("random"), tp_fnc(tp, random_random));
|
||||
|
||||
/*
|
||||
* bind usual distribution random variable generator
|
||||
*/
|
||||
tp_set(tp, random_mod, tp_string("uniform"), tp_fnc(tp, random_uniform));
|
||||
tp_set(tp, random_mod, tp_string("normalvariate"), tp_fnc(tp, random_normalvariate));
|
||||
tp_set(tp, random_mod, tp_string("lognormvariate"), tp_fnc(tp, random_lognormvariate));
|
||||
tp_set(tp, random_mod, tp_string("expovariate"), tp_fnc(tp, random_expovariate));
|
||||
tp_set(tp, random_mod, tp_string("vonmisesvariate"), tp_fnc(tp, random_vonmisesvariate));
|
||||
tp_set(tp, random_mod, tp_string("gammavariate"), tp_fnc(tp, random_gammavariate));
|
||||
tp_set(tp, random_mod, tp_string("betavariate"), tp_fnc(tp, random_betavariate));
|
||||
tp_set(tp, random_mod, tp_string("paretovariate"), tp_fnc(tp, random_paretovariate));
|
||||
tp_set(tp, random_mod, tp_string("weibullvariate"), tp_fnc(tp, random_weibullvariate));
|
||||
tp_set(tp, random_mod, tp_string("randrange"), tp_fnc(tp, random_randrange));
|
||||
tp_set(tp, random_mod, tp_string("randint"), tp_fnc(tp, random_randint));
|
||||
tp_set(tp, random_mod, tp_string("choice"), tp_fnc(tp, random_choice));
|
||||
tp_set(tp, random_mod, tp_string("shuffle"), tp_fnc(tp, random_shuffle));
|
||||
|
||||
/*
|
||||
* bind special attributes to random module
|
||||
*/
|
||||
tp_set(tp, random_mod, tp_string("__doc__"), tp_string("Random variable generators."));
|
||||
tp_set(tp, random_mod, tp_string("__name__"), tp_string("random"));
|
||||
tp_set(tp, random_mod, tp_string("__file__"), tp_string(__FILE__));
|
||||
|
||||
/*
|
||||
* bind random module to tinypy modules[]
|
||||
*/
|
||||
tp_set(tp, tp->modules, tp_string("random"), random_mod);
|
||||
}
|
1108
programs/develop/tinypy/std_modules/random/random.c
Normal file
1108
programs/develop/tinypy/std_modules/random/random.c
Normal file
File diff suppressed because it is too large
Load Diff
176
programs/develop/tinypy/std_modules/random/tests.py
Normal file
176
programs/develop/tinypy/std_modules/random/tests.py
Normal file
@@ -0,0 +1,176 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import random
|
||||
#from math import log, exp, sqrt, pi
|
||||
|
||||
def test_seed_state():
|
||||
"""test seed() and getstate()/setstate()
|
||||
"""
|
||||
# random ought to be able to deal with seeds in any form, of follows.
|
||||
# following code shouldn't cause an exception.
|
||||
random.seed()
|
||||
random.seed(0)
|
||||
random.seed(-1)
|
||||
random.seed(0.1)
|
||||
random.seed(-0.1)
|
||||
random.seed("a")
|
||||
random.seed("abc")
|
||||
random.seed("abcd")
|
||||
random.seed("fasdfasdfasdfadgaldhgldahlgahdlghadlgladh")
|
||||
random.seed("lxhlh90yowhldshlgah;")
|
||||
|
||||
# state1 and state2 should be different for different seeds
|
||||
random.seed(1)
|
||||
state1 = random.getstate()
|
||||
random.seed(2)
|
||||
state2 = random.getstate()
|
||||
rep = 0
|
||||
for ind in range(len(state1)):
|
||||
elem1 = state1[ind]
|
||||
elem2 = state2[ind]
|
||||
if (elem1 == elem2): rep += 1
|
||||
if (rep > len(state1) / 2):
|
||||
print("rep = ", rep, "len(state1) = ", len(state1))
|
||||
raise "state1 and state2 should be different"
|
||||
|
||||
# for the same seeds, state1 and state2 should be the same
|
||||
random.seed(100)
|
||||
state1 = random.getstate()
|
||||
random.seed(100)
|
||||
state2 = random.getstate()
|
||||
rep = 0
|
||||
for ind in range(len(state1)):
|
||||
elem1 = state1[ind]
|
||||
elem2 = state2[ind]
|
||||
if (elem1 == elem2): rep += 1
|
||||
if (rep != len(state1)):
|
||||
raise "state1 and state2 should be the same"
|
||||
|
||||
def test_jumpahead():
|
||||
"""jumpahead will change the pseudo-number generator's internal state
|
||||
"""
|
||||
random.seed()
|
||||
state1 = random.getstate()
|
||||
random.jumpahead(20)
|
||||
state2 = random.getstate()
|
||||
rep = 0
|
||||
for ind in range(len(state1)):
|
||||
elem1 = state1[ind]
|
||||
elem2 = state2[ind]
|
||||
if (elem1 == elem2): rep += 1
|
||||
if (rep > len(state1) / 2):
|
||||
raise "state1 and state2 can't be the same"
|
||||
|
||||
def test_setstate():
|
||||
"""
|
||||
"""
|
||||
random.seed()
|
||||
oldState = random.getstate()
|
||||
oldRandSeq = [random.random() for i in range(10)]
|
||||
random.setstate(oldState)
|
||||
newRandSeq = [random.random() for i in range(10)]
|
||||
rep = 0
|
||||
for ind in range(len(oldRandSeq)):
|
||||
elem1 = oldRandSeq[ind]
|
||||
elem2 = newRandSeq[ind]
|
||||
if (elem1 == elem2): rep += 1
|
||||
if (rep != len(oldRandSeq)):
|
||||
raise "oldRandSeq and newRandSeq should be the same"
|
||||
|
||||
def test_random():
|
||||
"""generate a random number list
|
||||
"""
|
||||
x = [random.random() for i in range(100)]
|
||||
|
||||
def test_distribution():
|
||||
"""these lines are borrowed from python, they shouldn't
|
||||
cause any exception.
|
||||
"""
|
||||
g = random
|
||||
g.uniform(1,10)
|
||||
g.paretovariate(1.0)
|
||||
g.expovariate(1.0)
|
||||
g.weibullvariate(1.0, 1.0)
|
||||
g.normalvariate(0.0, 1.0)
|
||||
g.lognormvariate(0.0, 1.0)
|
||||
g.vonmisesvariate(0.0, 1.0)
|
||||
g.gammavariate(0.01, 1.0)
|
||||
g.gammavariate(1.0, 1.0)
|
||||
g.gammavariate(200.0, 1.0)
|
||||
g.betavariate(3.0, 3.0)
|
||||
|
||||
def test_randrange():
|
||||
"""these input to randrange() shouldn't cause any exception.
|
||||
"""
|
||||
random.randrange(100000)
|
||||
random.randrange(-100000)
|
||||
random.randrange(0)
|
||||
random.randrange(-10.2)
|
||||
|
||||
random.randrange(-10, 10)
|
||||
random.randrange(2, 1000)
|
||||
random.randrange(0, 1)
|
||||
random.randrange(-1, 0)
|
||||
|
||||
random.randrange(10, 2000, 2)
|
||||
random.randrange(-2000, 100, 5)
|
||||
random.randrange(-1000.3, 1000.7, 2)
|
||||
|
||||
def test_randint():
|
||||
"""for any valid pair (a, b), randint(a, b) should lay between [a, b]
|
||||
"""
|
||||
for i in range(1000):
|
||||
r = random.randint(-10000, 10000)
|
||||
if (-10000 <= r <= 10000): continue
|
||||
else: raise "error: random.randint()"
|
||||
|
||||
def test_choice():
|
||||
"""random.choice() should be able to deal with string, list.
|
||||
"""
|
||||
S = "abcdefg123*@#$%)("
|
||||
L = [1, 2, 3, -1, 0.2, -0.1, -10000, "cyc"]
|
||||
|
||||
if random.choice(S) not in S:
|
||||
raise "error: random.choice(S)"
|
||||
|
||||
if random.choice(L) not in L:
|
||||
raise "error: random.choice(L)"
|
||||
|
||||
def test_shuffle():
|
||||
"""test random.shuffle() on list. since string is not writable in-place,
|
||||
random.shuffle() can not be applied on string.
|
||||
Note: to copy items from a list to a new list, must use syntax like:
|
||||
newList = oldList[:]
|
||||
if use syntax like: newList = oldList, newList is just an alias of oldList.
|
||||
"""
|
||||
oldL = [1, 2, 3, -1, 0.2, -0.1, -10000, "cyc"]
|
||||
newL = oldL[:]
|
||||
|
||||
random.shuffle(newL)
|
||||
|
||||
rep = 0
|
||||
for ind in range(len(oldL)):
|
||||
elem1 = oldL[ind]
|
||||
elem2 = newL[ind]
|
||||
if (elem1 == elem2): rep += 1
|
||||
if (rep > len(oldL) / 2):
|
||||
raise "oldL and newL shouldn't be the same"
|
||||
|
||||
def test_53_bits_per_float():
|
||||
pass
|
||||
|
||||
def main():
|
||||
test_seed_state()
|
||||
test_jumpahead()
|
||||
test_setstate()
|
||||
test_random()
|
||||
test_distribution()
|
||||
test_randrange()
|
||||
test_randint()
|
||||
test_choice()
|
||||
test_shuffle()
|
||||
test_53_bits_per_float()
|
||||
print("#OK")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
710
programs/develop/tinypy/std_modules/re/init.c
Normal file
710
programs/develop/tinypy/std_modules/re/init.c
Normal file
@@ -0,0 +1,710 @@
|
||||
/*
|
||||
* regular expression module
|
||||
*
|
||||
* Important Note: do not support group name index
|
||||
*
|
||||
* $Id$
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include "regexpr.c"
|
||||
|
||||
/* tinypy API to be use in this unit */
|
||||
extern tp_obj tp_data(TP,int magic,void *v);
|
||||
extern tp_obj tp_object_new(TP);
|
||||
extern tp_obj tp_object(TP);
|
||||
extern tp_obj tp_method(TP,tp_obj self,tp_obj v(TP));
|
||||
extern tp_obj tp_string_copy(TP, const char *s, int n);
|
||||
extern tp_obj tp_list(TP);
|
||||
extern tp_obj tp_copy(TP);
|
||||
|
||||
/* last error message */
|
||||
static const char * LastError = NULL;
|
||||
|
||||
/* lower level regex object */
|
||||
typedef struct {
|
||||
struct re_pattern_buffer re_patbuf; /* The compiled expression */
|
||||
struct re_registers re_regs; /* The registers from the last match */
|
||||
char re_fastmap[256]; /* Storage for fastmap */
|
||||
unsigned char *re_translate; /* String object for translate table */
|
||||
unsigned char *re_lastok; /* String object last matched/searched */
|
||||
|
||||
/* supplementary */
|
||||
int re_errno; /* error num */
|
||||
int re_syntax; /* syntax */
|
||||
} regexobject;
|
||||
|
||||
/* local declarations */
|
||||
static regexobject* getre(TP, tp_obj rmobj);
|
||||
static tp_obj match_obj_group(TP);
|
||||
static tp_obj match_obj_groups(TP);
|
||||
static tp_obj match_obj_start(TP);
|
||||
static tp_obj match_obj_end(TP);
|
||||
static tp_obj match_obj_span(TP);
|
||||
|
||||
/*
|
||||
* helper function: return lower level regex object
|
||||
* rmobj - regex or match object
|
||||
*/
|
||||
static regexobject * getre(TP, tp_obj rmobj)
|
||||
{
|
||||
tp_obj reobj_data = tp_get(tp, rmobj, tp_string("__data__"));
|
||||
regexobject *re = NULL;
|
||||
|
||||
/* validate magic */
|
||||
if (reobj_data.data.magic != sizeof(regexobject)) {
|
||||
LastError = "broken regex object";
|
||||
return (NULL);
|
||||
}
|
||||
re = (regexobject*)reobj_data.data.val;
|
||||
assert(re);
|
||||
|
||||
return (re);
|
||||
}
|
||||
|
||||
/*
|
||||
* derive match object from regex object
|
||||
*/
|
||||
static tp_obj match_object(TP, tp_obj reobj)
|
||||
{
|
||||
tp_obj mo = tp_object(tp); /* match object */
|
||||
tp_obj redata; /* regex object data */
|
||||
tp_obj madata; /* match object data */
|
||||
regexobject *re = NULL; /* lower level regex object */
|
||||
|
||||
redata = tp_get(tp, reobj, tp_string("__data__"));
|
||||
re = (regexobject *)redata.data.val;
|
||||
assert(re);
|
||||
madata = tp_data(tp, (int)sizeof(regexobject), re);
|
||||
|
||||
tp_set(tp, mo, tp_string("group"), tp_method(tp, mo, match_obj_group));
|
||||
tp_set(tp, mo, tp_string("groups"), tp_method(tp, mo, match_obj_groups));
|
||||
tp_set(tp, mo, tp_string("start"), tp_method(tp, mo, match_obj_start));
|
||||
tp_set(tp, mo, tp_string("end"), tp_method(tp, mo, match_obj_end));
|
||||
tp_set(tp, mo, tp_string("span"), tp_method(tp, mo, match_obj_span));
|
||||
tp_set(tp, mo, tp_string("__data__"), madata);
|
||||
|
||||
return (mo);
|
||||
}
|
||||
|
||||
/*
|
||||
* FUNC: regexobj.search(str[,pos=0])
|
||||
* self - regex object
|
||||
* str - string to be searched
|
||||
* pos - optional starting offset
|
||||
*
|
||||
* RETURN:
|
||||
* match object - when matched
|
||||
* None - not matched
|
||||
*/
|
||||
static tp_obj regex_obj_search(TP)
|
||||
{
|
||||
tp_obj self = TP_OBJ(); /* regex object */
|
||||
tp_obj str = TP_STR();
|
||||
tp_obj pos = TP_DEFAULT(tp_number(0));
|
||||
tp_obj maobj; /* match object */
|
||||
regexobject *re = NULL;
|
||||
int r = -2; /* -2 indicate exception */
|
||||
int range;
|
||||
|
||||
if (pos.number.val < 0 || pos.number.val > str.string.len) {
|
||||
LastError = "search offset out of range";
|
||||
goto exception;
|
||||
}
|
||||
range = str.string.len - pos.number.val;
|
||||
|
||||
re = getre(tp, self);
|
||||
re->re_lastok = NULL;
|
||||
r = re_search(&re->re_patbuf, (unsigned char *)str.string.val,
|
||||
str.string.len, pos.number.val, range, &re->re_regs);
|
||||
|
||||
/* cannot match pattern */
|
||||
if (r == -1)
|
||||
goto notfind;
|
||||
|
||||
/* error occurred */
|
||||
if (r == -2)
|
||||
goto exception;
|
||||
|
||||
/* matched */
|
||||
re->re_lastok = (unsigned char *)str.string.val;
|
||||
|
||||
/* match obj */
|
||||
maobj = match_object(tp, self);
|
||||
|
||||
return (maobj);
|
||||
|
||||
notfind:
|
||||
re->re_lastok = NULL;
|
||||
return (tp_None);
|
||||
exception:
|
||||
re->re_lastok = NULL;
|
||||
tp_raise(tp_None, tp_string("regex search error"));
|
||||
}
|
||||
|
||||
/*
|
||||
* FUNC: regexobj.match(str[,pos=0])
|
||||
* self - regex object
|
||||
* str - string to be matched
|
||||
* pos - optional starting position
|
||||
*
|
||||
* RETURN:
|
||||
* match object - when matched
|
||||
* None - not matched
|
||||
*/
|
||||
static tp_obj regex_obj_match(TP)
|
||||
{
|
||||
tp_obj self = TP_OBJ(); /* regex object */
|
||||
tp_obj str = TP_STR();
|
||||
tp_obj pos = TP_DEFAULT(tp_number(0));
|
||||
tp_obj maobj; /* match object */
|
||||
regexobject *re = NULL;
|
||||
int r = -2; /* -2 indicate exception */
|
||||
|
||||
re = getre(tp, self);
|
||||
re->re_lastok = NULL;
|
||||
r = re_match(&re->re_patbuf, (unsigned char *)str.string.val,
|
||||
str.string.len, pos.number.val, &re->re_regs);
|
||||
|
||||
/* cannot match pattern */
|
||||
if (r == -1)
|
||||
goto nomatch;
|
||||
|
||||
/* error occurred */
|
||||
if (r == -2)
|
||||
goto exception;
|
||||
|
||||
/* matched */
|
||||
re->re_lastok = (unsigned char *)str.string.val;
|
||||
|
||||
/* match obj */
|
||||
maobj = match_object(tp, self);
|
||||
|
||||
return (maobj);
|
||||
|
||||
nomatch:
|
||||
re->re_lastok = NULL;
|
||||
return (tp_None);
|
||||
exception:
|
||||
re->re_lastok = NULL;
|
||||
tp_raise(tp_None, tp_string("regex match error"));
|
||||
}
|
||||
|
||||
/*
|
||||
* regex object split()
|
||||
* self - regex object
|
||||
* restr - regex string
|
||||
* maxsplit - max split field, default 0, mean no limit
|
||||
*/
|
||||
static tp_obj regex_obj_split(TP)
|
||||
{
|
||||
tp_obj self = TP_OBJ(); /* regex object */
|
||||
tp_obj restr = TP_OBJ(); /* string */
|
||||
tp_obj maxsplit = TP_DEFAULT(tp_number(0));
|
||||
tp_obj maobj; /* match object */
|
||||
regexobject *re = NULL; /* lower level regex object */
|
||||
tp_obj result = tp_list(tp);
|
||||
tp_obj grpstr; /* group string */
|
||||
int slen; /* string length */
|
||||
int srchloc; /* search location */
|
||||
|
||||
/* maxsplit == 0 means no limit */
|
||||
if ((int)maxsplit.number.val == 0)
|
||||
maxsplit.number.val = RE_NREGS;
|
||||
assert(maxsplit.number.val > 0);
|
||||
|
||||
srchloc = 0;
|
||||
slen = strlen((char *)restr.string.val);
|
||||
|
||||
do {
|
||||
/* generate a temp match object */
|
||||
tp_params_v(tp, 3, self, restr, tp_number(srchloc));
|
||||
maobj = regex_obj_search(tp);
|
||||
if (!tp_bool(tp, maobj))
|
||||
break;
|
||||
|
||||
re = getre(tp, maobj);
|
||||
if (re->re_lastok == NULL) {
|
||||
tp_raise(tp_None, tp_string("no match for split()"));
|
||||
}
|
||||
|
||||
/* extract fields */
|
||||
if ((int)maxsplit.number.val > 0) {
|
||||
int start = re->re_regs.start[0];
|
||||
int end = re->re_regs.end[0];
|
||||
/*printf("%s:start(%d),end(%d)\n", __func__, start, end);*/
|
||||
if (start < 0 || end < 0)
|
||||
break;
|
||||
|
||||
grpstr = tp_string_copy(tp,
|
||||
(const char *)re->re_lastok + srchloc, start - srchloc);
|
||||
|
||||
if (tp_bool(tp, grpstr)) {
|
||||
tp_set(tp, result, tp_None, grpstr);
|
||||
maxsplit.number.val--;
|
||||
}
|
||||
|
||||
srchloc = end;
|
||||
}
|
||||
} while (srchloc < slen && (int)maxsplit.number.val > 0);
|
||||
|
||||
/* collect remaining string, if necessary */
|
||||
if (srchloc < slen) {
|
||||
grpstr = tp_string_copy(tp,
|
||||
(const char *)restr.string.val + srchloc, slen - srchloc);
|
||||
if (tp_bool(tp, grpstr))
|
||||
tp_set(tp, result, tp_None, grpstr);
|
||||
}
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*
|
||||
* regex object findall()
|
||||
* self - regex object
|
||||
* restr - regex string
|
||||
* pos - starting position, default 0
|
||||
*/
|
||||
static tp_obj regex_obj_findall(TP)
|
||||
{
|
||||
tp_obj self = TP_OBJ(); /* regex object */
|
||||
tp_obj restr = TP_OBJ(); /* string */
|
||||
tp_obj pos = TP_DEFAULT(tp_number(0));
|
||||
tp_obj maobj; /* match object */
|
||||
regexobject *re = NULL; /* lower level regex object */
|
||||
tp_obj result = tp_list(tp);
|
||||
tp_obj grpstr; /* group string */
|
||||
int slen; /* string length */
|
||||
int srchloc; /* search location */
|
||||
|
||||
srchloc = (int)pos.number.val;
|
||||
slen = strlen((char *)restr.string.val);
|
||||
if (srchloc < 0 || srchloc >= slen)
|
||||
tp_raise(tp_None, tp_string("starting position out of range"));
|
||||
|
||||
do {
|
||||
/* generate a temp match object */
|
||||
tp_params_v(tp, 3, self, restr, tp_number(srchloc));
|
||||
maobj = regex_obj_search(tp);
|
||||
if (!tp_bool(tp, maobj))
|
||||
break;
|
||||
|
||||
re = getre(tp, maobj);
|
||||
if (re->re_lastok == NULL) {
|
||||
tp_raise(tp_None, tp_string("no match for findall()"));
|
||||
}
|
||||
|
||||
/* extract fields */
|
||||
if (srchloc < slen) {
|
||||
int start = re->re_regs.start[0];
|
||||
int end = re->re_regs.end[0];
|
||||
/*printf("%s:start(%d),end(%d)\n", __func__, start, end);*/
|
||||
if (start < 0 || end < 0)
|
||||
break;
|
||||
|
||||
grpstr = tp_string_copy(tp,
|
||||
(const char *)re->re_lastok + start, end - start);
|
||||
|
||||
if (tp_bool(tp, grpstr)) {
|
||||
tp_set(tp, result, tp_None, grpstr);
|
||||
}
|
||||
|
||||
srchloc = end;
|
||||
}
|
||||
} while (srchloc < slen);
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*
|
||||
* FUNC: matchobj.group([group1, ...])
|
||||
* self - match object
|
||||
* args - optional group indices, default 0
|
||||
*
|
||||
* return specified group.
|
||||
*/
|
||||
static tp_obj match_obj_group(TP)
|
||||
{
|
||||
tp_obj self = TP_OBJ(); /* match object */
|
||||
tp_obj grpidx; /* a group index */
|
||||
regexobject *re = NULL;
|
||||
int indices[RE_NREGS];
|
||||
int start;
|
||||
int end;
|
||||
int i;
|
||||
int single = 0; /* single group index? */
|
||||
tp_obj result;
|
||||
|
||||
/* get lower level regex object representation */
|
||||
re = getre(tp, self);
|
||||
if (re->re_lastok == NULL)
|
||||
tp_raise(tp_None,
|
||||
tp_string("group() only valid after successful match/search"));
|
||||
|
||||
for (i = 0; i < RE_NREGS; i++)
|
||||
indices[i] = -1;
|
||||
|
||||
/*
|
||||
* if no group index provided, supply default group index 0; else
|
||||
* fill in indices[] with provided group index list.
|
||||
*/
|
||||
if (tp->params.list.val->len == 0) {
|
||||
indices[0] = 0;
|
||||
single = 1;
|
||||
} else if (tp->params.list.val->len == 1) {
|
||||
indices[0] = (int)TP_NUM();
|
||||
single = 1;
|
||||
} else {
|
||||
i = 0;
|
||||
TP_LOOP(grpidx)
|
||||
if (grpidx.number.val < 0 || grpidx.number.val > RE_NREGS)
|
||||
tp_raise(tp_None, tp_string("group() grpidx out of range"));
|
||||
indices[i++] = (int)grpidx.number.val;
|
||||
TP_END
|
||||
}
|
||||
|
||||
/* generate result string list */
|
||||
result = tp_list(tp);
|
||||
for (i = 0; i < RE_NREGS && indices[i] >= 0; i++) {
|
||||
tp_obj grpstr;
|
||||
start = re->re_regs.start[indices[i]];
|
||||
end = re->re_regs.end[indices[i]];
|
||||
if (start < 0 || end < 0) {
|
||||
grpstr = tp_None;
|
||||
} else {
|
||||
grpstr = tp_string_copy(tp, (const char *)re->re_lastok + start,
|
||||
end - start);
|
||||
}
|
||||
tp_set(tp, result, tp_None, grpstr);
|
||||
}
|
||||
return (single ? tp_get(tp, result, tp_number(0)) : result);
|
||||
}
|
||||
|
||||
/*
|
||||
* FUNC: matchobj.groups()
|
||||
* self - match object.
|
||||
* return all groups.
|
||||
* Note: CPython allow a 'default' argument, but we disallow it.
|
||||
*/
|
||||
static tp_obj match_obj_groups(TP)
|
||||
{
|
||||
tp_obj self = TP_OBJ(); /* match object */
|
||||
regexobject *re = NULL;
|
||||
int start;
|
||||
int end;
|
||||
int i;
|
||||
tp_obj result = tp_list(tp);
|
||||
|
||||
re = getre(tp, self);
|
||||
if (re->re_lastok == NULL) {
|
||||
tp_raise(tp_None,
|
||||
tp_string("groups() only valid after successful match/search"));
|
||||
}
|
||||
|
||||
for (i = 1; i < RE_NREGS; i++) {
|
||||
start = re->re_regs.start[i];
|
||||
end = re->re_regs.end[i];
|
||||
if (start < 0 || end < 0)
|
||||
break;
|
||||
|
||||
tp_obj grpstr = tp_string_copy(tp,
|
||||
(const char *)re->re_lastok + start, end - start);
|
||||
|
||||
if (tp_bool(tp, grpstr))
|
||||
tp_set(tp, result, tp_None, grpstr);
|
||||
}
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*
|
||||
* FUNC: matchobj.start([group])
|
||||
* self - match object
|
||||
* group - group index
|
||||
* return starting position of matched 'group' substring.
|
||||
*/
|
||||
static tp_obj match_obj_start(TP)
|
||||
{
|
||||
tp_obj self = TP_OBJ(); /* match object */
|
||||
tp_obj group = TP_DEFAULT(tp_number(0)); /* group */
|
||||
regexobject *re = NULL;
|
||||
int start;
|
||||
|
||||
re = getre(tp, self);
|
||||
if (re->re_lastok == NULL) {
|
||||
tp_raise(tp_None,
|
||||
tp_string("start() only valid after successful match/search"));
|
||||
}
|
||||
|
||||
if (group.number.val < 0 || group.number.val > RE_NREGS)
|
||||
tp_raise(tp_None, tp_string("IndexError: group index out of range"));
|
||||
|
||||
start = re->re_regs.start[(int)group.number.val];
|
||||
|
||||
return (tp_number(start));
|
||||
}
|
||||
|
||||
/*
|
||||
* FUNC: matchobj.end([group])
|
||||
* self - match object
|
||||
* group - group index
|
||||
* return ending position of matched 'group' substring.
|
||||
*/
|
||||
static tp_obj match_obj_end(TP)
|
||||
{
|
||||
tp_obj self = TP_OBJ(); /* match object */
|
||||
tp_obj group = TP_DEFAULT(tp_number(0)); /* group */
|
||||
regexobject *re = NULL;
|
||||
int end;
|
||||
|
||||
re = getre(tp, self);
|
||||
if (re->re_lastok == NULL) {
|
||||
tp_raise(tp_None,
|
||||
tp_string("end() only valid after successful match/search"));
|
||||
}
|
||||
|
||||
if (group.number.val < 0 || group.number.val > RE_NREGS)
|
||||
tp_raise(tp_None, tp_string("IndexError: group index out of range"));
|
||||
|
||||
end = re->re_regs.end[(int)group.number.val];
|
||||
|
||||
return (tp_number(end));
|
||||
}
|
||||
|
||||
/*
|
||||
* FUNC: matchobj.span([group])
|
||||
* self - match object
|
||||
* group - group index
|
||||
* return [start,end] position pair of matched 'group' substring.
|
||||
*/
|
||||
static tp_obj match_obj_span(TP)
|
||||
{
|
||||
tp_obj self = TP_OBJ(); /* match object */
|
||||
tp_obj group = TP_DEFAULT(tp_number(0)); /* group */
|
||||
regexobject *re = NULL;
|
||||
int start;
|
||||
int end;
|
||||
tp_obj result;
|
||||
|
||||
re = getre(tp, self);
|
||||
if (re->re_lastok == NULL) {
|
||||
tp_raise(tp_None,
|
||||
tp_string("span() only valid after successful match/search"));
|
||||
}
|
||||
|
||||
if (group.number.val < 0 || group.number.val > RE_NREGS)
|
||||
tp_raise(tp_None, tp_string("IndexError: group index out of range"));
|
||||
|
||||
start = re->re_regs.start[(int)group.number.val];
|
||||
end = re->re_regs.end[(int)group.number.val];
|
||||
|
||||
result = tp_list(tp);
|
||||
tp_set(tp, result, tp_None, tp_number(start));
|
||||
tp_set(tp, result, tp_None, tp_number(end));
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*
|
||||
* compile out a re object
|
||||
* repat - regex pattern
|
||||
* resyn - regex syntax
|
||||
*/
|
||||
static tp_obj regex_compile(TP)
|
||||
{
|
||||
char *error = NULL;
|
||||
char const *pat = NULL;
|
||||
int size = 0;
|
||||
tp_obj reobj_data;
|
||||
tp_obj repat = TP_TYPE(TP_STRING); /* pattern */
|
||||
tp_obj resyn = TP_DEFAULT(tp_number(RE_SYNTAX_EMACS)); /* syntax */
|
||||
tp_obj reobj; /* regex object */
|
||||
regexobject *re;
|
||||
|
||||
/*
|
||||
* create regex object, its parent is builtin 'object'
|
||||
*/
|
||||
reobj = tp_object(tp);
|
||||
|
||||
re = (regexobject *)malloc(sizeof(regexobject));
|
||||
if (!re) {
|
||||
error = "malloc lower level regex object failed";
|
||||
goto finally;
|
||||
}
|
||||
|
||||
re->re_patbuf.buffer = NULL;
|
||||
re->re_patbuf.allocated = 0;
|
||||
re->re_patbuf.fastmap = (unsigned char *)re->re_fastmap;
|
||||
re->re_patbuf.translate = NULL;
|
||||
re->re_translate = NULL;
|
||||
re->re_lastok = NULL;
|
||||
|
||||
re->re_errno = 0;
|
||||
re->re_syntax = (int)resyn.number.val;
|
||||
|
||||
pat = repat.string.val;
|
||||
size = repat.string.len;
|
||||
error = re_compile_pattern((unsigned char *)pat, size, &re->re_patbuf);
|
||||
if (error != NULL) {
|
||||
LastError = error;
|
||||
goto finally;
|
||||
}
|
||||
|
||||
/* regexobject's size as magic */
|
||||
reobj_data = tp_data(tp, (int)sizeof(regexobject), re);
|
||||
|
||||
/*
|
||||
* bind to regex object
|
||||
*/
|
||||
tp_set(tp, reobj, tp_string("search"),
|
||||
tp_method(tp, reobj, regex_obj_search));
|
||||
tp_set(tp, reobj, tp_string("match"),
|
||||
tp_method(tp, reobj, regex_obj_match));
|
||||
tp_set(tp, reobj, tp_string("split"),
|
||||
tp_method(tp, reobj, regex_obj_split));
|
||||
tp_set(tp, reobj, tp_string("findall"),
|
||||
tp_method(tp, reobj, regex_obj_findall));
|
||||
tp_set(tp, reobj, tp_string("__data__"), reobj_data);
|
||||
|
||||
tp_set(tp, reobj, tp_string("__name__"),
|
||||
tp_string("regular expression object"));
|
||||
tp_set(tp, reobj, tp_string("__doc__"), tp_string(
|
||||
"regular expression object, support methods:\n"
|
||||
"search(str[,pos=0])-search 'str' from 'pos'\n"
|
||||
"match(str[,pos=0]) -match 'str' from 'pos'\n"
|
||||
));
|
||||
|
||||
return (reobj);
|
||||
|
||||
finally:
|
||||
tp_raise(tp_None, tp_string(error));
|
||||
}
|
||||
|
||||
/*
|
||||
* module level search()
|
||||
*/
|
||||
static tp_obj regex_search(TP)
|
||||
{
|
||||
tp_obj repat = TP_OBJ(); /* pattern */
|
||||
tp_obj restr = TP_OBJ(); /* string */
|
||||
tp_obj resyn = TP_DEFAULT(tp_number(RE_SYNTAX_EMACS));
|
||||
tp_obj reobj; /* regex object */
|
||||
tp_obj maobj; /* match object */
|
||||
|
||||
/* compile out regex object */
|
||||
tp_params_v(tp, 2, repat, resyn);
|
||||
reobj = regex_compile(tp);
|
||||
|
||||
/* call r.search() */
|
||||
tp_params_v(tp, 3, reobj, restr, tp_number(0));
|
||||
maobj = regex_obj_search(tp);
|
||||
|
||||
return (maobj);
|
||||
}
|
||||
|
||||
/*
|
||||
* module level match()
|
||||
*/
|
||||
static tp_obj regex_match(TP)
|
||||
{
|
||||
tp_obj repat = TP_OBJ(); /* pattern */
|
||||
tp_obj restr = TP_OBJ(); /* string */
|
||||
tp_obj resyn = TP_DEFAULT(tp_number(RE_SYNTAX_EMACS));
|
||||
tp_obj reobj; /* regex object */
|
||||
tp_obj maobj; /* match object */
|
||||
|
||||
/* compile out regex object */
|
||||
tp_params_v(tp, 2, repat, resyn);
|
||||
reobj = regex_compile(tp);
|
||||
|
||||
/* call r.search() */
|
||||
tp_params_v(tp, 3, reobj, restr, tp_number(0));
|
||||
maobj = regex_obj_match(tp);
|
||||
|
||||
return (maobj);
|
||||
}
|
||||
|
||||
/*
|
||||
* module level split()
|
||||
* repat - regex pattern
|
||||
* restr - regex string
|
||||
* maxsplit - max split field, default 0, mean no limit
|
||||
*/
|
||||
static tp_obj regex_split(TP)
|
||||
{
|
||||
tp_obj repat = TP_OBJ(); /* pattern */
|
||||
tp_obj restr = TP_OBJ(); /* string */
|
||||
tp_obj maxsplit = TP_DEFAULT(tp_number(0));
|
||||
tp_obj reobj; /* regex object */
|
||||
|
||||
/* generate a temp regex object */
|
||||
tp_params_v(tp, 2, repat, tp_number(RE_SYNTAX_EMACS));
|
||||
reobj = regex_compile(tp);
|
||||
|
||||
tp_params_v(tp, 3, reobj, restr, maxsplit);
|
||||
return regex_obj_split(tp);
|
||||
}
|
||||
|
||||
/*
|
||||
* module level findall()
|
||||
* repat - regex pattern
|
||||
* restr - regex string
|
||||
* resyn - regex syntax, optional, default RE_SYNTAX_EMAC
|
||||
*/
|
||||
static tp_obj regex_findall(TP)
|
||||
{
|
||||
tp_obj repat = TP_OBJ(); /* pattern */
|
||||
tp_obj restr = TP_OBJ(); /* string */
|
||||
tp_obj resyn = TP_DEFAULT(tp_number(RE_SYNTAX_EMACS));
|
||||
tp_obj reobj; /* regex object */
|
||||
|
||||
/* generate a temp regex object */
|
||||
tp_params_v(tp, 2, repat, resyn);
|
||||
reobj = regex_compile(tp);
|
||||
|
||||
tp_params_v(tp, 2, reobj, restr);
|
||||
return regex_obj_findall(tp);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* re mod can only support 'set_syntax', 'get_syntax', and 'compile' functions,
|
||||
* 'compile' function will return a 'reobj', and this 'reobj' will support
|
||||
* methods 'search', 'match', 'group', 'groupall', el al.
|
||||
*/
|
||||
void re_init(TP)
|
||||
{
|
||||
/*
|
||||
* module dict for re
|
||||
*/
|
||||
tp_obj re_mod = tp_dict(tp);
|
||||
|
||||
/*
|
||||
* bind to re module
|
||||
*/
|
||||
tp_set(tp, re_mod, tp_string("compile"), tp_fnc(tp, regex_compile));
|
||||
tp_set(tp, re_mod, tp_string("search"), tp_fnc(tp, regex_search));
|
||||
tp_set(tp, re_mod, tp_string("match"), tp_fnc(tp, regex_match));
|
||||
tp_set(tp, re_mod, tp_string("split"), tp_fnc(tp, regex_split));
|
||||
tp_set(tp, re_mod, tp_string("findall"), tp_fnc(tp, regex_findall));
|
||||
tp_set(tp, re_mod, tp_string("AWK_SYNTAX"), tp_number(RE_SYNTAX_AWK));
|
||||
tp_set(tp, re_mod, tp_string("EGREP_SYNTAX"), tp_number(RE_SYNTAX_EGREP));
|
||||
tp_set(tp, re_mod, tp_string("GREP_SYNTAX"), tp_number(RE_SYNTAX_GREP));
|
||||
tp_set(tp, re_mod, tp_string("EMACS_SYNTAX"), tp_number(RE_SYNTAX_EMACS));
|
||||
|
||||
/*
|
||||
* bind special attibutes to re module
|
||||
*/
|
||||
tp_set(tp, re_mod, tp_string("__name__"),
|
||||
tp_string("regular expression module"));
|
||||
tp_set(tp, re_mod, tp_string("__file__"), tp_string(__FILE__));
|
||||
tp_set(tp, re_mod, tp_string("__doc__"),
|
||||
tp_string("simple regular express implementation"));
|
||||
|
||||
/*
|
||||
* bind regex module to tinypy modules[]
|
||||
*/
|
||||
tp_set(tp, tp->modules, tp_string("re"), re_mod);
|
||||
}
|
||||
|
2125
programs/develop/tinypy/std_modules/re/regexpr.c
Normal file
2125
programs/develop/tinypy/std_modules/re/regexpr.c
Normal file
File diff suppressed because it is too large
Load Diff
160
programs/develop/tinypy/std_modules/re/regexpr.h
Normal file
160
programs/develop/tinypy/std_modules/re/regexpr.h
Normal file
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* -*- mode: c-mode; c-file-style: python -*-
|
||||
*/
|
||||
|
||||
#ifndef Py_REGEXPR_H
|
||||
#define Py_REGEXPR_H
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* regexpr.h
|
||||
*
|
||||
* Author: Tatu Ylonen <ylo@ngs.fi>
|
||||
*
|
||||
* Copyright (c) 1991 Tatu Ylonen, Espoo, Finland
|
||||
*
|
||||
* Permission to use, copy, modify, distribute, and sell this software
|
||||
* and its documentation for any purpose is hereby granted without fee,
|
||||
* provided that the above copyright notice appear in all copies. This
|
||||
* software is provided "as is" without express or implied warranty.
|
||||
*
|
||||
* Created: Thu Sep 26 17:15:36 1991 ylo
|
||||
* Last modified: Mon Nov 4 15:49:46 1991 ylo
|
||||
*/
|
||||
|
||||
/* $Id$ */
|
||||
|
||||
#ifndef REGEXPR_H
|
||||
#define REGEXPR_H
|
||||
|
||||
#define RE_NREGS 100 /* number of registers available */
|
||||
|
||||
typedef struct re_pattern_buffer
|
||||
{
|
||||
unsigned char *buffer; /* compiled pattern */
|
||||
int allocated; /* allocated size of compiled pattern */
|
||||
int used; /* actual length of compiled pattern */
|
||||
unsigned char *fastmap; /* fastmap[ch] is true if ch can start pattern */
|
||||
unsigned char *translate; /* translation to apply during compilation/matching */
|
||||
unsigned char fastmap_accurate; /* true if fastmap is valid */
|
||||
unsigned char can_be_null; /* true if can match empty string */
|
||||
unsigned char uses_registers; /* registers are used and need to be initialized */
|
||||
int num_registers; /* number of registers used */
|
||||
unsigned char anchor; /* anchor: 0=none 1=begline 2=begbuf */
|
||||
} *regexp_t;
|
||||
|
||||
typedef struct re_registers
|
||||
{
|
||||
int start[RE_NREGS]; /* start offset of region */
|
||||
int end[RE_NREGS]; /* end offset of region */
|
||||
} *regexp_registers_t;
|
||||
|
||||
/* bit definitions for syntax */
|
||||
#define RE_NO_BK_PARENS 1 /* no quoting for parentheses */
|
||||
#define RE_NO_BK_VBAR 2 /* no quoting for vertical bar */
|
||||
#define RE_BK_PLUS_QM 4 /* quoting needed for + and ? */
|
||||
#define RE_TIGHT_VBAR 8 /* | binds tighter than ^ and $ */
|
||||
#define RE_NEWLINE_OR 16 /* treat newline as or */
|
||||
#define RE_CONTEXT_INDEP_OPS 32 /* ^$?*+ are special in all contexts */
|
||||
#define RE_ANSI_HEX 64 /* ansi sequences (\n etc) and \xhh */
|
||||
#define RE_NO_GNU_EXTENSIONS 128 /* no gnu extensions */
|
||||
|
||||
#define TP_RE_NOERR 0
|
||||
#define TP_RE_UNKNOWN_OPCODE (-1)
|
||||
#define TP_RE_JUMP_OUT_BOUNDS 1
|
||||
#define TP_RE_QUOTE_ERR 2
|
||||
|
||||
/* definitions for some common regexp styles */
|
||||
#define RE_SYNTAX_AWK (RE_NO_BK_PARENS|RE_NO_BK_VBAR|RE_CONTEXT_INDEP_OPS)
|
||||
#define RE_SYNTAX_EGREP (RE_SYNTAX_AWK|RE_NEWLINE_OR)
|
||||
#define RE_SYNTAX_GREP (RE_BK_PLUS_QM|RE_NEWLINE_OR)
|
||||
#define RE_SYNTAX_EMACS 0
|
||||
|
||||
#define Sword 1
|
||||
#define Swhitespace 2
|
||||
#define Sdigit 4
|
||||
#define Soctaldigit 8
|
||||
#define Shexdigit 16
|
||||
|
||||
/* Rename all exported symbols to avoid conflicts with similarly named
|
||||
symbols in some systems' standard C libraries... */
|
||||
|
||||
#define re_syntax _Py_re_syntax
|
||||
#define re_syntax_table _Py_re_syntax_table
|
||||
#define re_compile_initialize _Py_re_compile_initialize
|
||||
#define re_set_syntax _Py_re_set_syntax
|
||||
#define re_compile_pattern _Py_re_compile_pattern
|
||||
#define re_match _Py_re_match
|
||||
#define re_search _Py_re_search
|
||||
#define re_compile_fastmap _Py_re_compile_fastmap
|
||||
#define re_comp _Py_re_comp
|
||||
#define re_exec _Py_re_exec
|
||||
|
||||
#ifdef HAVE_PROTOTYPES
|
||||
|
||||
extern int re_syntax;
|
||||
/* This is the actual syntax mask. It was added so that Python could do
|
||||
* syntax-dependent munging of patterns before compilation. */
|
||||
|
||||
extern unsigned char re_syntax_table[256];
|
||||
|
||||
void re_compile_initialize(void);
|
||||
|
||||
int re_set_syntax(int syntax);
|
||||
/* This sets the syntax to use and returns the previous syntax. The
|
||||
* syntax is specified by a bit mask of the above defined bits. */
|
||||
|
||||
char *re_compile_pattern(unsigned char *regex, int regex_size, regexp_t compiled);
|
||||
/* This compiles the regexp (given in regex and length in regex_size).
|
||||
* This returns NULL if the regexp compiled successfully, and an error
|
||||
* message if an error was encountered. The buffer field must be
|
||||
* initialized to a memory area allocated by malloc (or to NULL) before
|
||||
* use, and the allocated field must be set to its length (or 0 if
|
||||
* buffer is NULL). Also, the translate field must be set to point to a
|
||||
* valid translation table, or NULL if it is not used. */
|
||||
|
||||
int re_match(regexp_t compiled, unsigned char *string, int size, int pos,
|
||||
regexp_registers_t old_regs);
|
||||
/* This tries to match the regexp against the string. This returns the
|
||||
* length of the matched portion, or -1 if the pattern could not be
|
||||
* matched and -2 if an error (such as failure stack overflow) is
|
||||
* encountered. */
|
||||
|
||||
int re_search(regexp_t compiled, unsigned char *string, int size, int startpos,
|
||||
int range, regexp_registers_t regs);
|
||||
/* This searches for a substring matching the regexp. This returns the
|
||||
* first index at which a match is found. range specifies at how many
|
||||
* positions to try matching; positive values indicate searching
|
||||
* forwards, and negative values indicate searching backwards. mstop
|
||||
* specifies the offset beyond which a match must not go. This returns
|
||||
* -1 if no match is found, and -2 if an error (such as failure stack
|
||||
* overflow) is encountered. */
|
||||
|
||||
void re_compile_fastmap(regexp_t compiled);
|
||||
/* This computes the fastmap for the regexp. For this to have any effect,
|
||||
* the calling program must have initialized the fastmap field to point
|
||||
* to an array of 256 characters. */
|
||||
|
||||
#else /* HAVE_PROTOTYPES */
|
||||
|
||||
extern int re_syntax;
|
||||
extern unsigned char re_syntax_table[256];
|
||||
void re_compile_initialize();
|
||||
int re_set_syntax();
|
||||
char *re_compile_pattern();
|
||||
int re_match();
|
||||
int re_search();
|
||||
void re_compile_fastmap();
|
||||
|
||||
#endif /* HAVE_PROTOTYPES */
|
||||
|
||||
#endif /* REGEXPR_H */
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* !Py_REGEXPR_H */
|
648
programs/develop/tinypy/std_modules/re/tests.py
Normal file
648
programs/develop/tinypy/std_modules/re/tests.py
Normal file
@@ -0,0 +1,648 @@
|
||||
"""
|
||||
test case for re module
|
||||
"""
|
||||
|
||||
import re
|
||||
import testsuite
|
||||
SUCCEED, FAIL, SYNTAX_ERROR = range(3)
|
||||
|
||||
def RAISE():
|
||||
raise("testing failed")
|
||||
|
||||
def main():
|
||||
#print("begin re tests")
|
||||
|
||||
assert(re.__name__ != None)
|
||||
assert(re.__doc__ != None)
|
||||
assert(re.__file__ != None)
|
||||
|
||||
test_re_obj_search()
|
||||
test_re_obj_match()
|
||||
test_re_mod_search()
|
||||
test_re_mod_match()
|
||||
test_re_obj_split()
|
||||
test_re_mod_split()
|
||||
test_re_obj_findall()
|
||||
test_re_mod_findall()
|
||||
test_mat_obj_groups()
|
||||
test_mat_obj_start()
|
||||
test_mat_obj_end()
|
||||
test_mat_obj_span()
|
||||
|
||||
print("#OK: re tests passed")
|
||||
|
||||
def test_re_obj_search(verbose = None):
|
||||
"""
|
||||
some tests borrowed from cpython
|
||||
testing re.compile(), reobj.search(), and matobj.group()
|
||||
"""
|
||||
regex_tests = testsuite.search_regex_tests
|
||||
for t in regex_tests:
|
||||
pattern=s=outcome=repl=expected=None
|
||||
if len(t)==5:
|
||||
pattern, s, outcome, repl, expected = t
|
||||
elif len(t)==3:
|
||||
pattern, s, outcome = t
|
||||
else:
|
||||
raise ('Test tuples should have 3 or 5 fields',t)
|
||||
|
||||
try:
|
||||
obj=re.compile(pattern)
|
||||
except:
|
||||
if outcome==SYNTAX_ERROR: continue # Expected a syntax error
|
||||
else:
|
||||
# Regex syntax errors aren't yet reported, so for
|
||||
# the official test suite they'll be quietly ignored.
|
||||
pass
|
||||
try:
|
||||
matobj=obj.search(s)
|
||||
except:
|
||||
print('=== Unexpected exception:', obj, matobj, pattern, s)
|
||||
RAISE()
|
||||
|
||||
if outcome==SYNTAX_ERROR:
|
||||
# This should have been a syntax error; forget it.
|
||||
pass
|
||||
elif outcome==FAIL:
|
||||
if matobj==None: pass # No match, as expected
|
||||
else: print('=== Succeeded incorrectly', obj, matobj, pattern, s)
|
||||
elif outcome==SUCCEED:
|
||||
if matobj!=None:
|
||||
# Matched, as expected, so now we compute the
|
||||
# result string and compare it to our expected result.
|
||||
found=matobj.group(0)
|
||||
repl = repl.replace("found", str(found))
|
||||
for i in range(1,11):
|
||||
if "g"+str(i) in repl:
|
||||
gi = str(matobj.group(i))
|
||||
repl = repl.replace("g"+str(i), gi)
|
||||
if len(t) == 5:
|
||||
repl = repl.replace('+', '')
|
||||
repl = repl.replace('\"', '')
|
||||
if repl!=expected:
|
||||
print( '=== grouping error', t,
|
||||
str(repl)+' should be '+str(expected))
|
||||
RAISE()
|
||||
else:
|
||||
print ('=== Failed incorrectly', t)
|
||||
|
||||
def test_re_obj_match(verbose = None):
|
||||
"""
|
||||
some tests borrowed from cpython
|
||||
testing re.compile(), reobj.match() and matobj.group()
|
||||
"""
|
||||
regex_tests = testsuite.match_regex_tests
|
||||
for t in regex_tests:
|
||||
pattern=s=outcome=repl=expected=None
|
||||
if len(t)==5:
|
||||
pattern, s, outcome, repl, expected = t
|
||||
elif len(t)==3:
|
||||
pattern, s, outcome = t
|
||||
else:
|
||||
raise ('Test tuples should have 3 or 5 fields',t)
|
||||
|
||||
try:
|
||||
obj=re.compile(pattern)
|
||||
except:
|
||||
if outcome==SYNTAX_ERROR: continue # Expected a syntax error
|
||||
else:
|
||||
# Regex syntax errors aren't yet reported, so for
|
||||
# the official test suite they'll be quietly ignored.
|
||||
pass
|
||||
try:
|
||||
matobj=obj.match(s)
|
||||
except:
|
||||
print('=== Unexpected exception:', obj, matobj, pattern, s)
|
||||
|
||||
if outcome==SYNTAX_ERROR:
|
||||
# This should have been a syntax error; forget it.
|
||||
pass
|
||||
elif outcome==FAIL:
|
||||
if matobj==None: pass # No match, as expected
|
||||
else: print('=== Succeeded incorrectly', obj, matobj, pattern, s)
|
||||
elif outcome==SUCCEED:
|
||||
if matobj!=None:
|
||||
# Matched, as expected, so now we compute the
|
||||
# result string and compare it to our expected result.
|
||||
found=matobj.group(0)
|
||||
repl = repl.replace("found", str(found))
|
||||
for i in range(1,11):
|
||||
if "g"+str(i) in repl:
|
||||
gi = str(matobj.group(i))
|
||||
repl = repl.replace("g"+str(i), gi)
|
||||
if len(t) == 5:
|
||||
repl = repl.replace('+', '')
|
||||
repl = repl.replace('\"', '')
|
||||
if repl!=expected:
|
||||
print( '=== grouping error', t,
|
||||
str(repl)+' should be '+str(expected))
|
||||
RAISE()
|
||||
else:
|
||||
print ('=== Failed incorrectly', obj, matobj, pattern, s)
|
||||
|
||||
def test_re_mod_search(verbose = None):
|
||||
"""
|
||||
some tests borrowed from cpython
|
||||
testing re.search(), and matobj.group()
|
||||
"""
|
||||
regex_tests = testsuite.search_regex_tests
|
||||
for t in regex_tests:
|
||||
pattern=s=outcome=repl=expected=None
|
||||
if len(t)==5:
|
||||
pattern, s, outcome, repl, expected = t
|
||||
elif len(t)==3:
|
||||
pattern, s, outcome = t
|
||||
else:
|
||||
raise ('Test tuples should have 3 or 5 fields',t)
|
||||
|
||||
try:
|
||||
matobj=re.search(pattern, s)
|
||||
except:
|
||||
if outcome==SYNTAX_ERROR:
|
||||
# This should have been a syntax error; forget it.
|
||||
pass
|
||||
else:
|
||||
print('=== Unexpected exception:', matobj, pattern, s)
|
||||
|
||||
if outcome==FAIL:
|
||||
if matobj==None: pass # No match, as expected
|
||||
else: print('=== Succeeded incorrectly', obj, matobj, pattern, s)
|
||||
elif outcome==SUCCEED:
|
||||
if matobj!=None:
|
||||
# Matched, as expected, so now we compute the
|
||||
# result string and compare it to our expected result.
|
||||
found=matobj.group(0)
|
||||
repl = repl.replace("found", str(found))
|
||||
for i in range(1,11):
|
||||
if "g"+str(i) in repl:
|
||||
gi = str(matobj.group(i))
|
||||
repl = repl.replace("g"+str(i), gi)
|
||||
if len(t) == 5:
|
||||
repl = repl.replace('+', '')
|
||||
repl = repl.replace('\"', '')
|
||||
if repl!=expected:
|
||||
print( '=== grouping error', t,
|
||||
str(repl)+' should be '+str(expected))
|
||||
RAISE()
|
||||
else:
|
||||
print ('=== Failed incorrectly', t)
|
||||
|
||||
def test_re_mod_match(verbose = None):
|
||||
"""
|
||||
some tests borrowed from cpython
|
||||
testing re.match(), and matobj.group()
|
||||
"""
|
||||
regex_tests = testsuite.match_regex_tests
|
||||
for t in regex_tests:
|
||||
pattern=s=outcome=repl=expected=None
|
||||
if len(t)==5:
|
||||
pattern, s, outcome, repl, expected = t
|
||||
elif len(t)==3:
|
||||
pattern, s, outcome = t
|
||||
else:
|
||||
raise ('Test tuples should have 3 or 5 fields',t)
|
||||
|
||||
try:
|
||||
matobj=re.match(pattern, s)
|
||||
except:
|
||||
if outcome==SYNTAX_ERROR:
|
||||
# This should have been a syntax error; forget it.
|
||||
pass
|
||||
else:
|
||||
print('=== Unexpected exception:', matobj, pattern, s)
|
||||
|
||||
if outcome==FAIL:
|
||||
if matobj==None: pass # No match, as expected
|
||||
else: print('=== Succeeded incorrectly', matobj, pattern, s)
|
||||
elif outcome==SUCCEED:
|
||||
if matobj!=None:
|
||||
# Matched, as expected, so now we compute the
|
||||
# result string and compare it to our expected result.
|
||||
found=matobj.group(0)
|
||||
repl = repl.replace("found", str(found))
|
||||
for i in range(1,11):
|
||||
if "g"+str(i) in repl:
|
||||
gi = str(matobj.group(i))
|
||||
repl = repl.replace("g"+str(i), gi)
|
||||
if len(t) == 5:
|
||||
repl = repl.replace('+', '')
|
||||
repl = repl.replace('\"', '')
|
||||
if repl!=expected:
|
||||
print( '=== grouping error', t,
|
||||
str(repl)+' should be '+str(expected))
|
||||
RAISE()
|
||||
else:
|
||||
print ('=== Failed incorrectly', t)
|
||||
|
||||
def test_re_obj_split(verbose = None):
|
||||
"""
|
||||
test re.compile(), and reobj.split()
|
||||
"""
|
||||
regex_tests = testsuite.split_regex_tests
|
||||
for t in regex_tests:
|
||||
pattern, s, outcome, maxsplit, fields = t
|
||||
try:
|
||||
reobj = re.compile(pattern)
|
||||
except:
|
||||
if outcome==SYNTAX_ERROR:
|
||||
# This should have been a syntax error; forget it.
|
||||
pass
|
||||
else:
|
||||
print('=== Unexpected exception:', pattern, s,
|
||||
outcome, maxsplit, fields)
|
||||
try:
|
||||
fldlst=reobj.split(s, maxsplit)
|
||||
except:
|
||||
if outcome == SYNTAX_ERROR:
|
||||
continue
|
||||
else:
|
||||
print('=== Unexpected exception:', pattern, s,
|
||||
outcome, maxsplit, fields)
|
||||
|
||||
if outcome==FAIL:
|
||||
pass # No match, as expected
|
||||
elif outcome==SUCCEED:
|
||||
if fldlst:
|
||||
# Matched, as expected, so now we compute the
|
||||
# result string and compare it to our expected result.
|
||||
if verbose:
|
||||
fldstr = fieldstr = ""
|
||||
for item in fldlst:
|
||||
fldstr = fldstr + str(item) + " | "
|
||||
for item in fields:
|
||||
fieldstr = fieldstr + str(item) + " | "
|
||||
print(fldstr, "~~~", fieldstr)
|
||||
if len(fields) != len(fldlst):
|
||||
print('=== Not coherent 1')
|
||||
RAISE()
|
||||
|
||||
for i in range(len(fields)):
|
||||
if fields[i] != fldlst[i]:
|
||||
if verbose:
|
||||
print('=== Not coherent 2', pattern, s,
|
||||
outcome, maxsplit, fields, i,
|
||||
fields[i],'(',len(fields[i]),')', ' | ',
|
||||
fldlst[i],'(',len(fldlst[i]),')')
|
||||
else:
|
||||
print('=== Not coherent 2')
|
||||
RAISE()
|
||||
else:
|
||||
print ('=== Failed incorrectly', pattern, s,
|
||||
outcome, maxsplit, fields)
|
||||
|
||||
def test_re_mod_split(verbose = None):
|
||||
"""
|
||||
test re.split()
|
||||
"""
|
||||
regex_tests = testsuite.split_regex_tests
|
||||
for t in regex_tests:
|
||||
pattern, s, outcome, maxsplit, fields = t
|
||||
try:
|
||||
fldlst=re.split(pattern, s, maxsplit)
|
||||
except:
|
||||
if outcome==SYNTAX_ERROR:
|
||||
# This should have been a syntax error; forget it.
|
||||
continue
|
||||
else:
|
||||
print('=== Unexpected exception:', pattern, s,
|
||||
outcome, maxsplit, fields)
|
||||
|
||||
if outcome==FAIL:
|
||||
pass # No match, as expected
|
||||
elif outcome==SUCCEED:
|
||||
if fldlst:
|
||||
# Matched, as expected, so now we compute the
|
||||
# result string and compare it to our expected result.
|
||||
if verbose:
|
||||
fldstr = fieldstr = ""
|
||||
for item in fldlst:
|
||||
fldstr = fldstr + str(item) + " | "
|
||||
for item in fields:
|
||||
fieldstr = fieldstr + str(item) + " | "
|
||||
print(fldstr, "~~~", fieldstr)
|
||||
|
||||
if len(fields) != len(fldlst):
|
||||
print('=== Not coherent 1')
|
||||
RAISE()
|
||||
|
||||
for i in range(len(fields)):
|
||||
if fields[i] != fldlst[i]:
|
||||
if verbose:
|
||||
print('=== Not coherent 2', pattern, s,
|
||||
outcome, maxsplit, fields, i,
|
||||
fields[i],'(',len(fields[i]),')', ' | ',
|
||||
fldlst[i],'(',len(fldlst[i]),')')
|
||||
else:
|
||||
print('=== Not coherent 2')
|
||||
RAISE()
|
||||
else:
|
||||
print ('=== Failed incorrectly', pattern, s,
|
||||
outcome, maxsplit, fields)
|
||||
|
||||
def test_re_obj_findall(verbose = None):
|
||||
"""
|
||||
test re.compile(), and reobj.findall()
|
||||
"""
|
||||
regex_tests = testsuite.findall_regex_tests
|
||||
for t in regex_tests:
|
||||
pattern, s, outcome, pos, fields = t
|
||||
try:
|
||||
reobj = re.compile(pattern)
|
||||
except:
|
||||
if outcome==SYNTAX_ERROR:
|
||||
# This should have been a syntax error; forget it.
|
||||
pass
|
||||
else:
|
||||
print('=== Unexpected exception:', pattern, s,
|
||||
outcome, pos, fields)
|
||||
try:
|
||||
fldlst=reobj.findall(s, pos)
|
||||
except:
|
||||
if outcome == SYNTAX_ERROR:
|
||||
continue
|
||||
else:
|
||||
print('=== Unexpected exception:', pattern, s,
|
||||
outcome, pos, fields)
|
||||
|
||||
if outcome==FAIL:
|
||||
pass # No match, as expected
|
||||
elif outcome==SUCCEED:
|
||||
if fldlst:
|
||||
# Matched, as expected, so now we compute the
|
||||
# result string and compare it to our expected result.
|
||||
if verbose:
|
||||
fldstr = fieldstr = ""
|
||||
for item in fldlst:
|
||||
fldstr = fldstr + str(item) + " | "
|
||||
for item in fields:
|
||||
fieldstr = fieldstr + str(item) + " | "
|
||||
print(fldstr, "~~~", fieldstr)
|
||||
|
||||
if len(fields) != len(fldlst):
|
||||
print('=== Not coherent 1')
|
||||
RAISE()
|
||||
|
||||
for i in range(len(fields)):
|
||||
if fields[i] != fldlst[i]:
|
||||
if verbose:
|
||||
print('=== Not coherent 2', pattern, s,
|
||||
outcome, maxsplit, fields, i,
|
||||
fields[i],'(',len(fields[i]),')', ' | ',
|
||||
fldlst[i],'(',len(fldlst[i]),')')
|
||||
else:
|
||||
print('=== Not coherent 2')
|
||||
RAISE()
|
||||
else:
|
||||
print ('=== Failed incorrectly', pattern, s,
|
||||
outcome, pos, fields)
|
||||
|
||||
def test_re_mod_findall(verbose = None):
|
||||
"""
|
||||
test re.findall()
|
||||
"""
|
||||
regex_tests = testsuite.mod_findall_regex_tests
|
||||
for t in regex_tests:
|
||||
pattern, s, outcome, pos, fields = t # pos is not used
|
||||
try:
|
||||
fldlst=re.findall(pattern, s)
|
||||
except:
|
||||
if outcome==SYNTAX_ERROR:
|
||||
# This should have been a syntax error; forget it.
|
||||
continue
|
||||
else:
|
||||
print('=== Unexpected exception:', pattern, s,
|
||||
outcome, pos, fields)
|
||||
|
||||
if outcome==FAIL:
|
||||
pass # No match, as expected
|
||||
elif outcome==SUCCEED:
|
||||
if fldlst:
|
||||
# Matched, as expected, so now we compute the
|
||||
# result string and compare it to our expected result.
|
||||
if verbose:
|
||||
fldstr = fieldstr = ""
|
||||
for item in fldlst:
|
||||
fldstr = fldstr + str(item) + " | "
|
||||
for item in fields:
|
||||
fieldstr = fieldstr + str(item) + " | "
|
||||
print(fldstr, "~~~", fieldstr)
|
||||
|
||||
if len(fields) != len(fldlst):
|
||||
print('=== Not coherent 1')
|
||||
RAISE()
|
||||
|
||||
for i in range(len(fields)):
|
||||
if fields[i] != fldlst[i]:
|
||||
if verbose:
|
||||
print('=== Not coherent 2', pattern, s,
|
||||
outcome, maxsplit, fields, i,
|
||||
fields[i],'(',len(fields[i]),')', ' | ',
|
||||
fldlst[i],'(',len(fldlst[i]),')')
|
||||
else:
|
||||
print('=== Not coherent 2')
|
||||
RAISE()
|
||||
else:
|
||||
print ('=== Failed incorrectly', pattern, s,
|
||||
outcome, pos, fields)
|
||||
|
||||
def test_mat_obj_groups(verbose = None):
|
||||
"""
|
||||
test re.search(), and matobj.groups()
|
||||
'verbose' is for debugging, when 'verbose' is true, print extra info
|
||||
"""
|
||||
regex_tests = testsuite.matobj_groups_regex_tests
|
||||
for t in regex_tests:
|
||||
pattern, s, outcome, fields, grpidx, start, end = t
|
||||
try:
|
||||
matobj=re.search(pattern, s)
|
||||
except:
|
||||
if outcome==SYNTAX_ERROR:
|
||||
# This should have been a syntax error; forget it.
|
||||
continue
|
||||
else:
|
||||
print('=== Unexpected exception 1:', pattern, s,
|
||||
outcome,fields)
|
||||
|
||||
try:
|
||||
if outcome==SUCCEED: assert(matobj != None)
|
||||
fldlst = matobj.groups()
|
||||
except:
|
||||
if outcome==SYNTAX_ERROR:
|
||||
# This should have been a syntax error; forget it.
|
||||
continue
|
||||
else:
|
||||
print('=== Unexpected exception 2:', pattern, s,
|
||||
outcome,fields)
|
||||
|
||||
if outcome==FAIL:
|
||||
pass # No match, as expected
|
||||
elif outcome==SUCCEED:
|
||||
if fldlst and fields:
|
||||
# Matched, as expected, so now we compute the
|
||||
# result string and compare it to our expected result.
|
||||
if verbose:
|
||||
fldstr = fieldstr = ""
|
||||
for item in fldlst:
|
||||
fldstr = fldstr + str(item) + " | "
|
||||
for item in fields:
|
||||
fieldstr = fieldstr + str(item) + " | "
|
||||
print(fldstr, "~~~", fieldstr)
|
||||
|
||||
if len(fields) != len(fldlst):
|
||||
print('=== Not coherent 2')
|
||||
RAISE()
|
||||
|
||||
for i in range(len(fields)):
|
||||
if fields[i] != fldlst[i]:
|
||||
if verbose:
|
||||
print('=== Not coherent', pattern, s,
|
||||
outcome,fields, i,
|
||||
fields[i],'(',len(fields[i]),')', ' | ',
|
||||
fldlst[i],'(',len(fldlst[i]),')')
|
||||
else:
|
||||
print('=== Not coherent')
|
||||
RAISE()
|
||||
elif not len(fldlst) and not len(fields):
|
||||
# output is empty, as expected
|
||||
if verbose:
|
||||
print("output is empty, as expected")
|
||||
continue
|
||||
else:
|
||||
if verbose:
|
||||
for item in fldlst:
|
||||
print(item,)
|
||||
print()
|
||||
for item in fields:
|
||||
print(item,)
|
||||
print()
|
||||
print ('=== Failed incorrectly', pattern, s,
|
||||
outcome,fields,fldlst)
|
||||
|
||||
def test_mat_obj_start(verbose = None):
|
||||
"""
|
||||
test re.search(), and matobj.start()
|
||||
'verbose' is for debugging, when 'verbose' is true, print extra info
|
||||
"""
|
||||
regex_tests = testsuite.matobj_groups_regex_tests
|
||||
for t in regex_tests:
|
||||
pattern, s, outcome, fields, grpidx, start, end = t
|
||||
try:
|
||||
matobj=re.search(pattern, s)
|
||||
except:
|
||||
if outcome==SYNTAX_ERROR:
|
||||
# This should have been a syntax error; forget it.
|
||||
continue
|
||||
else:
|
||||
print('=== Unexpected exception 1:', pattern, s,
|
||||
outcome,fields)
|
||||
|
||||
try:
|
||||
if outcome==SUCCEED: assert(matobj != None)
|
||||
fldlst = matobj.groups()
|
||||
except:
|
||||
if outcome==SYNTAX_ERROR:
|
||||
# This should have been a syntax error; forget it.
|
||||
continue
|
||||
else:
|
||||
print('=== Unexpected exception 2:', pattern, s,
|
||||
outcome,fields)
|
||||
|
||||
if outcome==FAIL:
|
||||
pass # No match, as expected
|
||||
elif outcome==SUCCEED:
|
||||
if grpidx > 0:
|
||||
if matobj.start(grpidx) == start:
|
||||
pass
|
||||
else:
|
||||
if verbose:
|
||||
print ('=== Failed incorrectly', pattern, s,
|
||||
outcome,fields,fldlst)
|
||||
raise("testing failed")
|
||||
|
||||
|
||||
def test_mat_obj_end(verbose = None):
|
||||
"""
|
||||
test re.search(), and matobj.end()
|
||||
'verbose' is for debugging, when 'verbose' is true, print extra info
|
||||
"""
|
||||
regex_tests = testsuite.matobj_groups_regex_tests
|
||||
for t in regex_tests:
|
||||
pattern, s, outcome, fields, grpidx, start, end = t
|
||||
try:
|
||||
matobj=re.search(pattern, s)
|
||||
except:
|
||||
if outcome==SYNTAX_ERROR:
|
||||
# This should have been a syntax error; forget it.
|
||||
continue
|
||||
else:
|
||||
print('=== Unexpected exception 1:', pattern, s,
|
||||
outcome,fields)
|
||||
|
||||
try:
|
||||
if outcome==SUCCEED: assert(matobj != None)
|
||||
fldlst = matobj.groups()
|
||||
except:
|
||||
if outcome==SYNTAX_ERROR:
|
||||
# This should have been a syntax error; forget it.
|
||||
continue
|
||||
else:
|
||||
print('=== Unexpected exception 2:', pattern, s,
|
||||
outcome,fields)
|
||||
|
||||
if outcome==FAIL:
|
||||
pass # No match, as expected
|
||||
elif outcome==SUCCEED:
|
||||
if grpidx > 0:
|
||||
if matobj.end(grpidx) == end:
|
||||
pass
|
||||
else:
|
||||
if verbose:
|
||||
print ('=== Failed incorrectly', pattern, s,
|
||||
outcome,fields,fldlst, matobj.end(grpidx), end)
|
||||
raise("testing failed")
|
||||
|
||||
def test_mat_obj_span(verbose = None):
|
||||
"""
|
||||
test re.search(), and matobj.span()
|
||||
'verbose' is for debugging, when 'verbose' is true, print extra info
|
||||
"""
|
||||
regex_tests = testsuite.matobj_groups_regex_tests
|
||||
for t in regex_tests:
|
||||
pattern, s, outcome, fields, grpidx, start, end = t
|
||||
try:
|
||||
matobj=re.search(pattern, s)
|
||||
except:
|
||||
if outcome==SYNTAX_ERROR:
|
||||
# This should have been a syntax error; forget it.
|
||||
continue
|
||||
else:
|
||||
print('=== Unexpected exception 1:', pattern, s,
|
||||
outcome,fields)
|
||||
|
||||
try:
|
||||
if outcome==SUCCEED: assert(matobj != None)
|
||||
fldlst = matobj.groups()
|
||||
except:
|
||||
if outcome==SYNTAX_ERROR:
|
||||
# This should have been a syntax error; forget it.
|
||||
continue
|
||||
else:
|
||||
print('=== Unexpected exception 2:', pattern, s,
|
||||
outcome,fields)
|
||||
|
||||
if outcome==FAIL:
|
||||
pass # No match, as expected
|
||||
elif outcome==SUCCEED:
|
||||
if (grpidx > 0):
|
||||
spstart, spend = matobj.span(grpidx)
|
||||
if spstart == start and spend == end:
|
||||
pass
|
||||
else:
|
||||
if verbose:
|
||||
print ('=== Failed incorrectly', pattern, s,
|
||||
outcome,fields,fldlst)
|
||||
raise("testing failed")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
367
programs/develop/tinypy/std_modules/re/testsuite.py
Normal file
367
programs/develop/tinypy/std_modules/re/testsuite.py
Normal file
@@ -0,0 +1,367 @@
|
||||
# Test suite (for verifying correctness)
|
||||
#
|
||||
# The test suite is a list of 5- or 3-tuples. The 5 parts of a
|
||||
# complete tuple are:
|
||||
# element 0: a string containing the pattern
|
||||
# 1: the string to match against the pattern
|
||||
# 2: the expected result (0 - SUCCEED, 1 - FAIL, 2 - SYNTAX_ERROR)
|
||||
# 3: a string that will be eval()'ed to produce a test string.
|
||||
# This is an arbitrary Python expression; the available
|
||||
# variables are "found" (the whole match), and "g1", "g2", ...
|
||||
# up to "g10" contain the contents of each group, or the
|
||||
# string 'None' if the group wasn't given a value.
|
||||
# 4: The expected result of evaluating the expression.
|
||||
# If the two don't match, an error is reported.
|
||||
#
|
||||
# If the regex isn't expected to work, the latter two elements can be omitted.
|
||||
|
||||
# test suite for search
|
||||
search_regex_tests=[
|
||||
['abc', 'abc', 0, 'found', 'abc'],
|
||||
['abc', 'xbc', 1],
|
||||
['abc', 'axc', 1],
|
||||
['abc', 'abx', 1],
|
||||
['abc', 'xabcy', 0, 'found', 'abc'],
|
||||
['abc', 'ababc', 0, 'found', 'abc'],
|
||||
['ab*c', 'abc', 0, 'found', 'abc'],
|
||||
['ab*bc', 'abc', 0, 'found', 'abc'],
|
||||
['ab*bc', 'abbc', 0, 'found', 'abbc'],
|
||||
['ab*bc', 'abbbbc', 0, 'found', 'abbbbc'],
|
||||
['ab+bc', 'abbc', 0, 'found', 'abbc'],
|
||||
['ab+bc', 'abc', 1],
|
||||
['ab+bc', 'abq', 1],
|
||||
['ab+bc', 'abbbbc', 0, 'found', 'abbbbc'],
|
||||
['ab?bc', 'abbc', 0, 'found', 'abbc'],
|
||||
['ab?bc', 'abc', 0, 'found', 'abc'],
|
||||
['ab?bc', 'abbbbc', 1],
|
||||
['ab?c', 'abc', 0, 'found', 'abc'],
|
||||
['^abc$', 'abc', 0, 'found', 'abc'],
|
||||
['^abc$', 'abcc', 1],
|
||||
['^abc', 'abcc', 0, 'found', 'abc'],
|
||||
['^abc$', 'aabc', 1],
|
||||
['abc$', 'aabc', 0, 'found', 'abc'],
|
||||
['^', 'abc', 0, 'found+"-"', '-'],
|
||||
['$', 'abc', 0, 'found+"-"', '-'],
|
||||
['a.c', 'abc', 0, 'found', 'abc'],
|
||||
['a.c', 'axc', 0, 'found', 'axc'],
|
||||
['a.*c', 'axyzc', 0, 'found', 'axyzc'],
|
||||
['a.*c', 'axyzd', 1],
|
||||
['a[bc]d', 'abc', 1],
|
||||
['a[bc]d', 'abd', 0, 'found', 'abd'],
|
||||
['a[b-d]e', 'abd', 1],
|
||||
['a[b-d]e', 'ace', 0, 'found', 'ace'],
|
||||
['a[b-d]', 'aac', 0, 'found', 'ac'],
|
||||
['a[-b]', 'a-', 0, 'found', 'a-'],
|
||||
['a[b-]', 'a-', 0, 'found', 'a-'],
|
||||
['a[]b', '-', 2],
|
||||
['a[', '-', 2],
|
||||
['a\\', '-', 2],
|
||||
['abc\\)', '-', 2],
|
||||
['\\(abc', '-', 2],
|
||||
['a]', 'a]', 0, 'found', 'a]'],
|
||||
['a[]]b', 'a]b', 0, 'found', 'a]b'],
|
||||
['a[^bc]d', 'aed', 0, 'found', 'aed'],
|
||||
['a[^bc]d', 'abd', 1],
|
||||
['a[^-b]c', 'adc', 0, 'found', 'adc'],
|
||||
['a[^-b]c', 'a-c', 1],
|
||||
['a[^]b]c', 'a]c', 1],
|
||||
['a[^]b]c', 'adc', 0, 'found', 'adc'],
|
||||
['\\ba\\b', 'a-', 0, '"-"', '-'],
|
||||
['\\ba\\b', '-a', 0, '"-"', '-'],
|
||||
['\\ba\\b', '-a-', 0, '"-"', '-'],
|
||||
['\\by\\b', 'xy', 1],
|
||||
['\\by\\b', 'yz', 1],
|
||||
['\\by\\b', 'xyz', 1],
|
||||
['ab\\|cd', 'abc', 0, 'found', 'ab'],
|
||||
['ab\\|cd', 'abcd', 0, 'found', 'ab'],
|
||||
['\\(\\)ef', 'def', 0, 'found+"-"+g1', 'ef-'],
|
||||
['$b', 'b', 1],
|
||||
['a(b', 'a(b', 0, 'found+"-"+g1', 'a(b-None'],
|
||||
['a(*b', 'ab', 0, 'found', 'ab'],
|
||||
['a(*b', 'a((b', 0, 'found', 'a((b'],
|
||||
['a\\\\b', 'a\\b', 0, 'found', 'a\\b'],
|
||||
['\\(\\(a\\)\\)', 'abc', 0, 'found+"-"+g1+"-"+g2', 'a-a-a'],
|
||||
['\\(a\\)b\\(c\\)', 'abc', 0, 'found+"-"+g1+"-"+g2', 'abc-a-c'],
|
||||
['a+b+c', 'aabbabc', 0, 'found', 'abc'],
|
||||
['\\(a+\\|b\\)*', 'ab', 0, 'found+"-"+g1', 'ab-b'],
|
||||
['\\(a+\\|b\\)+', 'ab', 0, 'found+"-"+g1', 'ab-b'],
|
||||
['\\(a+\\|b\\)?', 'ab', 0, 'found+"-"+g1', 'a-a'],
|
||||
['\\)\\(', '-', 2],
|
||||
['[^ab]*', 'cde', 0, 'found', 'cde'],
|
||||
['abc', '', 1],
|
||||
['a*', '', 0, 'found', ''],
|
||||
['a\\|b\\|c\\|d\\|e', 'e', 0, 'found', 'e'],
|
||||
['\\(a\\|b\\|c\\|d\\|e\\)f', 'ef', 0, 'found+"-"+g1', 'ef-e'],
|
||||
['abcd*efg', 'abcdefg', 0, 'found', 'abcdefg'],
|
||||
['ab*', 'xabyabbbz', 0, 'found', 'ab'],
|
||||
['ab*', 'xayabbbz', 0, 'found', 'a'],
|
||||
['\\(ab\\|cd\\)e', 'abcde', 0, 'found+"-"+g1', 'cde-cd'],
|
||||
['[abhgefdc]ij', 'hij', 0, 'found', 'hij'],
|
||||
['^\\(ab\\|cd\\)e', 'abcde', 1, 'xg1y', 'xy'],
|
||||
['\\(abc\\|\\)ef', 'abcdef', 0, 'found+"-"+g1', 'ef-'],
|
||||
['\\(a\\|b\\)c*d', 'abcd', 0, 'found+"-"+g1', 'bcd-b'],
|
||||
['\\(ab\\|ab*\\)bc', 'abc', 0, 'found+"-"+g1', 'abc-a'],
|
||||
['a\\([bc]*\\)c*', 'abc', 0, 'found+"-"+g1', 'abc-bc'],
|
||||
['a\\([bc]*\\)\\(c*d\\)', 'abcd', 0, 'found+"-"+g1+"-"+g2', 'abcd-bc-d'],
|
||||
['a\\([bc]+\\)\\(c*d\\)', 'abcd', 0, 'found+"-"+g1+"-"+g2', 'abcd-bc-d'],
|
||||
['a\\([bc]*\\)\\(c+d\\)', 'abcd', 0, 'found+"-"+g1+"-"+g2', 'abcd-b-cd'],
|
||||
['a[bcd]*dcdcde', 'adcdcde', 0, 'found', 'adcdcde'],
|
||||
['a[bcd]+dcdcde', 'adcdcde', 1],
|
||||
['\\(ab\\|a\\)b*c', 'abc', 0, 'found+"-"+g1', 'abc-ab'],
|
||||
['\\(\\(a\\)\\(b\\)c\\)\\(d\\)', 'abcd', 0, 'g1+"-"+g2+"-"+g3+"-"+g4', 'abc-a-b-d'],
|
||||
['[a-zA-Z_][a-zA-Z0-9_]*', 'alpha', 0, 'found', 'alpha'],
|
||||
['^a\\(bc+\\|b[eh]\\)g\\|.h$', 'abh', 0, 'found+"-"+g1', 'bh-None'],
|
||||
['\\(bc+d$\\|ef*g.\\|h?i\\(j\\|k\\)\\)', 'effgz', 0, 'found+"-"+g1+"-"+g2', 'effgz-effgz-None'],
|
||||
['\\(bc+d$\\|ef*g.\\|h?i\\(j\\|k\\)\\)', 'ij', 0, 'found+"-"+g1+"-"+g2', 'ij-ij-j'],
|
||||
['\\(bc+d$\\|ef*g.\\|h?i\\(j\\|k\\)\\)', 'effg', 1],
|
||||
['\\(bc+d$\\|ef*g.\\|h?i\\(j\\|k\\)\\)', 'bcdd', 1],
|
||||
['\\(bc+d$\\|ef*g.\\|h?i\\(j\\|k\\)\\)', 'reffgz', 0, 'found+"-"+g1+"-"+g2', 'effgz-effgz-None'],
|
||||
['\\(\\(\\(\\(\\(\\(\\(\\(\\(a\\)\\)\\)\\)\\)\\)\\)\\)\\)', 'a', 0, 'found', 'a'],
|
||||
['multiple words of text', 'uh-uh', 1],
|
||||
['multiple words', 'multiple words, yeah', 0, 'found', 'multiple words'],
|
||||
['\\(.*\\)c\\(.*\\)', 'abcde', 0, 'found+"-"+g1+"-"+g2', 'abcde-ab-de'],
|
||||
['(\\(.*\\), \\(.*\\))', '(a, b)', 0, 'g2+"-"+g1', 'b-a'],
|
||||
['[k]', 'ab', 1],
|
||||
['a[-]?c', 'ac', 0, 'found', 'ac'],
|
||||
['\\(abc\\)\\1', 'abcabc', 0, 'g1', 'abc'],
|
||||
['\\([a-c]*\\)\\1', 'abcabc', 0, 'g1', 'abc'],
|
||||
['^\\(.+\\)?B', 'AB', 0, 'g1', 'A'],
|
||||
['\\(a+\\).\\1$', 'aaaaa', 0, 'found+"-"+g1', 'aaaaa-aa'],
|
||||
['^\\(a+\\).\\1$', 'aaaa', 1],
|
||||
['\\(abc\\)\\1', 'abcabc', 0, 'found+"-"+g1', 'abcabc-abc'],
|
||||
['\\([a-c]+\\)\\1', 'abcabc', 0, 'found+"-"+g1', 'abcabc-abc'],
|
||||
['\\(a\\)\\1', 'aa', 0, 'found+"-"+g1', 'aa-a'],
|
||||
['\\(a+\\)\\1', 'aa', 0, 'found+"-"+g1', 'aa-a'],
|
||||
['\\(a+\\)+\\1', 'aa', 0, 'found+"-"+g1', 'aa-a'],
|
||||
['\\(a\\).+\\1', 'aba', 0, 'found+"-"+g1', 'aba-a'],
|
||||
['\\(a\\)ba*\\1', 'aba', 0, 'found+"-"+g1', 'aba-a'],
|
||||
['\\(aa\\|a\\)a\\1$', 'aaa', 0, 'found+"-"+g1', 'aaa-a'],
|
||||
['\\(a\\|aa\\)a\\1$', 'aaa', 0, 'found+"-"+g1', 'aaa-a'],
|
||||
['\\(a+\\)a\\1$', 'aaa', 0, 'found+"-"+g1', 'aaa-a'],
|
||||
['\\([abc]*\\)\\1', 'abcabc', 0, 'found+"-"+g1', 'abcabc-abc'],
|
||||
['\\(a\\)\\(b\\)c\\|ab', 'ab', 0, 'found+"-"+g1+"-"+g2', 'ab-None-None'],
|
||||
['\\(a\\)+x', 'aaax', 0, 'found+"-"+g1', 'aaax-a'],
|
||||
['\\([ac]\\)+x', 'aacx', 0, 'found+"-"+g1', 'aacx-c'],
|
||||
['\\([^/]*/\\)*sub1/', 'd:msgs/tdir/sub1/trial/away.cpp', 0, 'found+"-"+g1', 'd:msgs/tdir/sub1/-tdir/'],
|
||||
['\\([^.]*\\)\\.\\([^:]*\\):[T ]+\\(.*\\)', 'track1.title:TBlah blah blah', 0, 'found+"-"+g1+"-"+g2+"-"+g3', 'track1.title:TBlah blah blah-track1-title-Blah blah blah'],
|
||||
['\\([^N]*N\\)+', 'abNNxyzN', 0, 'found+"-"+g1', 'abNNxyzN-xyzN'],
|
||||
['\\([^N]*N\\)+', 'abNNxyz', 0, 'found+"-"+g1', 'abNN-N'],
|
||||
['\\([abc]*\\)x', 'abcx', 0, 'found+"-"+g1', 'abcx-abc'],
|
||||
['\\([abc]*\\)x', 'abc', 1],
|
||||
['\\([xyz]*\\)x', 'abcx', 0, 'found+"-"+g1', 'x-'],
|
||||
['\\(a\\)+b\\|aac', 'aac', 0, 'found+"-"+g1', 'aac-None'],
|
||||
['\\<a', 'a', 0, 'found', 'a'],
|
||||
['\\<a', '!', 1],
|
||||
['a\\<b', 'ab', 1],
|
||||
['a\\>', 'ab', 1],
|
||||
['a\\>', 'a!', 0, 'found', 'a'],
|
||||
['a\\>', 'a', 0, 'found', 'a'],
|
||||
]
|
||||
|
||||
|
||||
# test suite for match
|
||||
match_regex_tests=[
|
||||
['abc', 'abc', 0, 'found', 'abc'],
|
||||
['abc', 'xbc', 1],
|
||||
['abc', 'axc', 1],
|
||||
['abc', 'abx', 1],
|
||||
['abc', 'xabcy', 1],
|
||||
['abc', 'ababc', 1],
|
||||
['ab*c', 'abc', 0, 'found', 'abc'],
|
||||
['ab*bc', 'abc', 0, 'found', 'abc'],
|
||||
['ab*bc', 'abbc', 0, 'found', 'abbc'],
|
||||
['ab*bc', 'abbbbc', 0, 'found', 'abbbbc'],
|
||||
['ab+bc', 'abbc', 0, 'found', 'abbc'],
|
||||
['ab+bc', 'abc', 1],
|
||||
['ab+bc', 'abq', 1],
|
||||
['ab+bc', 'abbbbc', 0, 'found', 'abbbbc'],
|
||||
['ab?bc', 'abbc', 0, 'found', 'abbc'],
|
||||
['ab?bc', 'abc', 0, 'found', 'abc'],
|
||||
['ab?bc', 'abbbbc', 1],
|
||||
['ab?c', 'abc', 0, 'found', 'abc'],
|
||||
['^abc$', 'abc', 0, 'found', 'abc'],
|
||||
['^abc$', 'abcc', 1],
|
||||
['^abc', 'abcc', 0, 'found', 'abc'],
|
||||
['^abc$', 'aabc', 1],
|
||||
['abc$', 'aabc', 1],
|
||||
['^', 'abc', 0, 'found+"-"', '-'],
|
||||
['$', 'abc', 1],
|
||||
['a.c', 'abc', 0, 'found', 'abc'],
|
||||
['a.c', 'axc', 0, 'found', 'axc'],
|
||||
['a.*c', 'axyzc', 0, 'found', 'axyzc'],
|
||||
['a.*c', 'axyzd', 1],
|
||||
['a[bc]d', 'abc', 1],
|
||||
['a[bc]d', 'abd', 0, 'found', 'abd'],
|
||||
['a[b-d]e', 'abd', 1],
|
||||
['a[b-d]e', 'ace', 0, 'found', 'ace'],
|
||||
['a[b-d]', 'aac', 1],
|
||||
['a[-b]', 'a-', 0, 'found', 'a-'],
|
||||
['a[b-]', 'a-', 0, 'found', 'a-'],
|
||||
['a[]b', '-', 2],
|
||||
['a[', '-', 2],
|
||||
['a\\', '-', 2],
|
||||
['abc\\)', '-', 2],
|
||||
['\\(abc', '-', 2],
|
||||
['a]', 'a]', 0, 'found', 'a]'],
|
||||
['a[]]b', 'a]b', 0, 'found', 'a]b'],
|
||||
['a[^bc]d', 'aed', 0, 'found', 'aed'],
|
||||
['a[^bc]d', 'abd', 1],
|
||||
['a[^-b]c', 'adc', 0, 'found', 'adc'],
|
||||
['a[^-b]c', 'a-c', 1],
|
||||
['a[^]b]c', 'a]c', 1],
|
||||
['a[^]b]c', 'adc', 0, 'found', 'adc'],
|
||||
['\\ba\\b', 'a-', 0, '"-"', '-'],
|
||||
['\\ba\\b', '-a', 1],
|
||||
['\\ba\\b', '-a-', 1],
|
||||
['\\by\\b', 'xy', 1],
|
||||
['\\by\\b', 'yz', 1],
|
||||
['\\by\\b', 'xyz', 1],
|
||||
['ab\\|cd', 'abc', 0, 'found', 'ab'],
|
||||
['ab\\|cd', 'abcd', 0, 'found', 'ab'],
|
||||
['\\(\\)ef', 'def', 1],
|
||||
['$b', 'b', 1],
|
||||
['a(b', 'a(b', 0, 'found+"-"+g1', 'a(b-None'],
|
||||
['a(*b', 'ab', 0, 'found', 'ab'],
|
||||
['a(*b', 'a((b', 0, 'found', 'a((b'],
|
||||
['a\\\\b', 'a\\b', 0, 'found', 'a\\b'],
|
||||
['\\(\\(a\\)\\)', 'abc', 0, 'found+"-"+g1+"-"+g2', 'a-a-a'],
|
||||
['\\(a\\)b\\(c\\)', 'abc', 0, 'found+"-"+g1+"-"+g2', 'abc-a-c'],
|
||||
['a+b+c', 'aabbabc', 1],
|
||||
['\\(a+\\|b\\)*', 'ab', 0, 'found+"-"+g1', 'ab-b'],
|
||||
['\\(a+\\|b\\)+', 'ab', 0, 'found+"-"+g1', 'ab-b'],
|
||||
['\\(a+\\|b\\)?', 'ab', 0, 'found+"-"+g1', 'a-a'],
|
||||
['\\)\\(', '-', 2],
|
||||
['[^ab]*', 'cde', 0, 'found', 'cde'],
|
||||
['abc', '', 1],
|
||||
['a*', '', 0, 'found', ''],
|
||||
['a\\|b\\|c\\|d\\|e', 'e', 0, 'found', 'e'],
|
||||
['\\(a\\|b\\|c\\|d\\|e\\)f', 'ef', 0, 'found+"-"+g1', 'ef-e'],
|
||||
['abcd*efg', 'abcdefg', 0, 'found', 'abcdefg'],
|
||||
['ab*', 'xabyabbbz', 1],
|
||||
['ab*', 'xayabbbz', 1],
|
||||
['\\(ab\\|cd\\)e', 'abcde', 1],
|
||||
['[abhgefdc]ij', 'hij', 0, 'found', 'hij'],
|
||||
['^\\(ab\\|cd\\)e', 'abcde', 1, 'xg1y', 'xy'],
|
||||
['\\(abc\\|\\)ef', 'abcdef', 1],
|
||||
['\\(a\\|b\\)c*d', 'abcd', 1],
|
||||
['\\(ab\\|ab*\\)bc', 'abc', 0, 'found+"-"+g1', 'abc-a'],
|
||||
['a\\([bc]*\\)c*', 'abc', 0, 'found+"-"+g1', 'abc-bc'],
|
||||
['a\\([bc]*\\)\\(c*d\\)', 'abcd', 0, 'found+"-"+g1+"-"+g2', 'abcd-bc-d'],
|
||||
['a\\([bc]+\\)\\(c*d\\)', 'abcd', 0, 'found+"-"+g1+"-"+g2', 'abcd-bc-d'],
|
||||
['a\\([bc]*\\)\\(c+d\\)', 'abcd', 0, 'found+"-"+g1+"-"+g2', 'abcd-b-cd'],
|
||||
['a[bcd]*dcdcde', 'adcdcde', 0, 'found', 'adcdcde'],
|
||||
['a[bcd]+dcdcde', 'adcdcde', 1],
|
||||
['\\(ab\\|a\\)b*c', 'abc', 0, 'found+"-"+g1', 'abc-ab'],
|
||||
['\\(\\(a\\)\\(b\\)c\\)\\(d\\)', 'abcd', 0, 'g1+"-"+g2+"-"+g3+"-"+g4', 'abc-a-b-d'],
|
||||
['[a-zA-Z_][a-zA-Z0-9_]*', 'alpha', 0, 'found', 'alpha'],
|
||||
['^a\\(bc+\\|b[eh]\\)g\\|.h$', 'abh', 1],
|
||||
['\\(bc+d$\\|ef*g.\\|h?i\\(j\\|k\\)\\)', 'effgz', 0, 'found+"-"+g1+"-"+g2', 'effgz-effgz-None'],
|
||||
['\\(bc+d$\\|ef*g.\\|h?i\\(j\\|k\\)\\)', 'ij', 0, 'found+"-"+g1+"-"+g2', 'ij-ij-j'],
|
||||
['\\(bc+d$\\|ef*g.\\|h?i\\(j\\|k\\)\\)', 'effg', 1],
|
||||
['\\(bc+d$\\|ef*g.\\|h?i\\(j\\|k\\)\\)', 'bcdd', 1],
|
||||
['\\(bc+d$\\|ef*g.\\|h?i\\(j\\|k\\)\\)', 'reffgz', 1],
|
||||
['\\(\\(\\(\\(\\(\\(\\(\\(\\(a\\)\\)\\)\\)\\)\\)\\)\\)\\)', 'a', 0, 'found', 'a'],
|
||||
['multiple words of text', 'uh-uh', 1],
|
||||
['multiple words', 'multiple words, yeah', 0, 'found', 'multiple words'],
|
||||
['\\(.*\\)c\\(.*\\)', 'abcde', 0, 'found+"-"+g1+"-"+g2', 'abcde-ab-de'],
|
||||
['(\\(.*\\), \\(.*\\))', '(a, b)', 0, 'g2+"-"+g1', 'b-a'],
|
||||
['[k]', 'ab', 1],
|
||||
['a[-]?c', 'ac', 0, 'found', 'ac'],
|
||||
['\\(abc\\)\\1', 'abcabc', 0, 'g1', 'abc'],
|
||||
['\\([a-c]*\\)\\1', 'abcabc', 0, 'g1', 'abc'],
|
||||
['^\\(.+\\)?B', 'AB', 0, 'g1', 'A'],
|
||||
['\\(a+\\).\\1$', 'aaaaa', 0, 'found+"-"+g1', 'aaaaa-aa'],
|
||||
['^\\(a+\\).\\1$', 'aaaa', 1],
|
||||
['\\(abc\\)\\1', 'abcabc', 0, 'found+"-"+g1', 'abcabc-abc'],
|
||||
['\\([a-c]+\\)\\1', 'abcabc', 0, 'found+"-"+g1', 'abcabc-abc'],
|
||||
['\\(a\\)\\1', 'aa', 0, 'found+"-"+g1', 'aa-a'],
|
||||
['\\(a+\\)\\1', 'aa', 0, 'found+"-"+g1', 'aa-a'],
|
||||
['\\(a+\\)+\\1', 'aa', 0, 'found+"-"+g1', 'aa-a'],
|
||||
['\\(a\\).+\\1', 'aba', 0, 'found+"-"+g1', 'aba-a'],
|
||||
['\\(a\\)ba*\\1', 'aba', 0, 'found+"-"+g1', 'aba-a'],
|
||||
['\\(aa\\|a\\)a\\1$', 'aaa', 0, 'found+"-"+g1', 'aaa-a'],
|
||||
['\\(a\\|aa\\)a\\1$', 'aaa', 0, 'found+"-"+g1', 'aaa-a'],
|
||||
['\\(a+\\)a\\1$', 'aaa', 0, 'found+"-"+g1', 'aaa-a'],
|
||||
['\\([abc]*\\)\\1', 'abcabc', 0, 'found+"-"+g1', 'abcabc-abc'],
|
||||
['\\(a\\)\\(b\\)c\\|ab', 'ab', 0, 'found+"-"+g1+"-"+g2', 'ab-None-None'],
|
||||
['\\(a\\)+x', 'aaax', 0, 'found+"-"+g1', 'aaax-a'],
|
||||
['\\([ac]\\)+x', 'aacx', 0, 'found+"-"+g1', 'aacx-c'],
|
||||
['\\([^/]*/\\)*sub1/', 'd:msgs/tdir/sub1/trial/away.cpp', 0, 'found+"-"+g1', 'd:msgs/tdir/sub1/-tdir/'],
|
||||
['\\([^.]*\\)\\.\\([^:]*\\):[T ]+\\(.*\\)', 'track1.title:TBlah blah blah', 0, 'found+"-"+g1+"-"+g2+"-"+g3', 'track1.title:TBlah blah blah-track1-title-Blah blah blah'],
|
||||
['\\([^N]*N\\)+', 'abNNxyzN', 0, 'found+"-"+g1', 'abNNxyzN-xyzN'],
|
||||
['\\([^N]*N\\)+', 'abNNxyz', 0, 'found+"-"+g1', 'abNN-N'],
|
||||
['\\([abc]*\\)x', 'abcx', 0, 'found+"-"+g1', 'abcx-abc'],
|
||||
['\\([abc]*\\)x', 'abc', 1],
|
||||
['\\([xyz]*\\)x', 'abcx', 1],
|
||||
['\\(a\\)+b\\|aac', 'aac', 0, 'found+"-"+g1', 'aac-None'],
|
||||
['\\<a', 'a', 0, 'found', 'a'],
|
||||
['\\<a', '!', 1],
|
||||
['a\\<b', 'ab', 1],
|
||||
['a\\>', 'ab', 1],
|
||||
['a\\>', 'a!', 0, 'found', 'a'],
|
||||
['a\\>', 'a', 0, 'found', 'a'],
|
||||
]
|
||||
|
||||
# test suite for split()
|
||||
# element 0: pattern
|
||||
# 1: string to split
|
||||
# 3: compile result
|
||||
# 4: maxsplit
|
||||
# 5: splitted fields list
|
||||
split_regex_tests = [
|
||||
["[ |,]", "with you, nothing, and me", 0, 0, ["with","you","nothing","and","me"]],
|
||||
["[ |,]", "with you, nothing, and me", 0, 1, ["with", "you, nothing, and me"]],
|
||||
["\\ ", "send email to apply", 0, 0, ["send", "email", "to", "apply"]],
|
||||
["\\ ", "send email to apply", 0, 2, ["send", "email", "to apply"]],
|
||||
["[+ | -]", "+86-028-83201034", 0, 0, ["86", "028", "83201034"]],
|
||||
["[+ | -]", "+86-028-83201034", 0, 1, ["86", "028-83201034"]],
|
||||
["[*|#]", "slide show", 0, 0, ["slide show"]],
|
||||
["(", "whats ever", 0, 1, ["whats ever"]],
|
||||
["@#!~$%^&*()<>\n", "who knows", 0, 1, ["who knows"]],
|
||||
]
|
||||
|
||||
# test suite for findall()
|
||||
# element 0: pattern
|
||||
# 1: string to match
|
||||
# 3: compile result
|
||||
# 4: starting position
|
||||
# 5: grouped fields list
|
||||
|
||||
# reobj.find()
|
||||
findall_regex_tests = [
|
||||
["\\ ", "send email to apply", 0, 0, [" ", " ", " "]],
|
||||
["\\ ", "send email to apply", 0, 5, [" ", " "]],
|
||||
["[+ | -]", "+86-028-83201034", 0, 0, ["+", "-", "-"]],
|
||||
["[+ | -]", "+86-028-83201034", 0, 1, ["-", "-"]],
|
||||
["sl.*e\\|#", "slide show at Room #3", 0, 0, ["slide", "#"]],
|
||||
["w.+s\\|e.*r", "whats ever", 0, 0, ["whats", "ever"]],
|
||||
["Euler\\|Gauss", "Both Euler and Gauss are great mathematicians", 0, 0, ["Euler", "Gauss"]],
|
||||
]
|
||||
|
||||
# module re.findall()
|
||||
mod_findall_regex_tests = [
|
||||
["\\ ", "send email to apply", 0, 0, [" ", " ", " "]],
|
||||
["\\ ", "send email to apply", 0, 0, [" ", " ", " "]],
|
||||
["[+ | -]", "+86-028-83201034", 0, 0, ["+", "-", "-"]],
|
||||
["[+ | -]", "+86-028-83201034", 0, 0, ["+", "-", "-"]],
|
||||
["sl.*e\\|#", "slide show at Room #3", 0, 0, ["slide", "#"]],
|
||||
["w.+s\\|e.*r", "whats ever", 0, 0, ["whats", "ever"]],
|
||||
["Euler\\|Gauss", "Both Euler and Gauss are great mathematicians", 0, 0, ["Euler", "Gauss"]],
|
||||
]
|
||||
|
||||
# test for match object's groups() method
|
||||
# element 0: pattern
|
||||
# 1: string
|
||||
# 2: compile result
|
||||
# 3: matched fields, for groups()
|
||||
# 4: group index, valid when > 0, for start(), end(), and span()
|
||||
# 5: pattern's starting index in string, for start() and span()
|
||||
# 6: pattern's ending index in string, for end() and span
|
||||
matobj_groups_regex_tests = [
|
||||
["\\(abc\\(.*xyz\\)\\(.*31415926\\)\\)", "where is abc and flurry xyz, which is pi 31415926, derived from ms", 0, ["abc and flurry xyz, which is pi 31415926"," and flurry xyz",", which is pi 31415926"], 2, 12, 27],
|
||||
|
||||
["[a\\|b]\\(.+\\)shoe\\([t]+\\)d", "bbbshoetttdxrznmlkjp", 0, ["bb", "ttt"], 1, 1, 3],
|
||||
|
||||
["abcdef", "xyah2oewoyqe030uabcdefwhalsdewnkhgiohyczb", 0, [], -1, 0, 0],
|
||||
]
|
||||
|
Reference in New Issue
Block a user