Converted to new style names by Skip Montanaro

This commit is contained in:
Guido van Rossum 1996-07-24 00:51:20 +00:00
parent 9687a921c8
commit dfe8ad969c
2 changed files with 319 additions and 322 deletions

View File

@ -33,8 +33,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- support more open flags - support more open flags
*/ */
#include "allobjects.h" #include "Python.h"
#include "modsupport.h"
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -44,18 +43,18 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
(it messes up the info required in the Setup file) */ (it messes up the info required in the Setup file) */
typedef struct { typedef struct {
OB_HEAD PyObject_HEAD
DB *di_bsddb; DB *di_bsddb;
int di_size; /* -1 means recompute */ int di_size; /* -1 means recompute */
} bsddbobject; } bsddbobject;
staticforward typeobject Bsddbtype; staticforward PyTypeObject Bsddbtype;
#define is_bsddbobject(v) ((v)->ob_type == &Bsddbtype) #define is_bsddbobject(v) ((v)->ob_type == &Bsddbtype)
static object *BsddbError; static PyObject *BsddbError;
static object * static PyObject *
newdbhashobject(file, flags, mode, newdbhashobject(file, flags, mode,
bsize, ffactor, nelem, cachesize, hash, lorder) bsize, ffactor, nelem, cachesize, hash, lorder)
char *file; char *file;
@ -71,7 +70,7 @@ newdbhashobject(file, flags, mode,
bsddbobject *dp; bsddbobject *dp;
HASHINFO info; HASHINFO info;
if ((dp = NEWOBJ(bsddbobject, &Bsddbtype)) == NULL) if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL)
return NULL; return NULL;
info.bsize = bsize; info.bsize = bsize;
@ -82,17 +81,17 @@ newdbhashobject(file, flags, mode,
info.lorder = lorder; info.lorder = lorder;
if ((dp->di_bsddb = dbopen(file, flags, mode, DB_HASH, &info)) == NULL) { if ((dp->di_bsddb = dbopen(file, flags, mode, DB_HASH, &info)) == NULL) {
err_errno(BsddbError); PyErr_SetFromErrno(BsddbError);
DECREF(dp); Py_DECREF(dp);
return NULL; return NULL;
} }
dp->di_size = -1; dp->di_size = -1;
return (object *)dp; return (PyObject *)dp;
} }
static object * static PyObject *
newdbbtobject(file, flags, mode, newdbbtobject(file, flags, mode,
btflags, cachesize, maxkeypage, minkeypage, psize, lorder) btflags, cachesize, maxkeypage, minkeypage, psize, lorder)
char *file; char *file;
@ -108,7 +107,7 @@ newdbbtobject(file, flags, mode,
bsddbobject *dp; bsddbobject *dp;
BTREEINFO info; BTREEINFO info;
if ((dp = NEWOBJ(bsddbobject, &Bsddbtype)) == NULL) if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL)
return NULL; return NULL;
info.flags = btflags; info.flags = btflags;
@ -121,17 +120,17 @@ newdbbtobject(file, flags, mode,
info.prefix = 0; info.prefix = 0;
if ((dp->di_bsddb = dbopen(file, flags, mode, DB_BTREE, &info)) == NULL) { if ((dp->di_bsddb = dbopen(file, flags, mode, DB_BTREE, &info)) == NULL) {
err_errno(BsddbError); PyErr_SetFromErrno(BsddbError);
DECREF(dp); Py_DECREF(dp);
return NULL; return NULL;
} }
dp->di_size = -1; dp->di_size = -1;
return (object *)dp; return (PyObject *)dp;
} }
static object * static PyObject *
newdbrnobject(file, flags, mode, newdbrnobject(file, flags, mode,
rnflags, cachesize, psize, lorder, reclen, bval, bfname) rnflags, cachesize, psize, lorder, reclen, bval, bfname)
char *file; char *file;
@ -148,7 +147,7 @@ newdbrnobject(file, flags, mode,
bsddbobject *dp; bsddbobject *dp;
RECNOINFO info; RECNOINFO info;
if ((dp = NEWOBJ(bsddbobject, &Bsddbtype)) == NULL) if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL)
return NULL; return NULL;
info.flags = rnflags; info.flags = rnflags;
@ -160,14 +159,14 @@ newdbrnobject(file, flags, mode,
info.bfname = bfname; info.bfname = bfname;
if ((dp->di_bsddb = dbopen(file, flags, mode, DB_RECNO, &info)) == NULL) { if ((dp->di_bsddb = dbopen(file, flags, mode, DB_RECNO, &info)) == NULL) {
err_errno(BsddbError); PyErr_SetFromErrno(BsddbError);
DECREF(dp); Py_DECREF(dp);
return NULL; return NULL;
} }
dp->di_size = -1; dp->di_size = -1;
return (object *)dp; return (PyObject *)dp;
} }
@ -180,7 +179,7 @@ bsddb_dealloc(dp)
fprintf(stderr, fprintf(stderr,
"Python bsddb: close errno %s in dealloc\n", errno); "Python bsddb: close errno %s in dealloc\n", errno);
} }
DEL(dp); PyMem_DEL(dp);
} }
static int static int
@ -196,7 +195,7 @@ bsddb_length(dp)
status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_NEXT)) status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_NEXT))
size++; size++;
if (status < 0) { if (status < 0) {
err_errno(BsddbError); PyErr_SetFromErrno(BsddbError);
return -1; return -1;
} }
dp->di_size = size; dp->di_size = size;
@ -204,18 +203,18 @@ bsddb_length(dp)
return dp->di_size; return dp->di_size;
} }
static object * static PyObject *
bsddb_subscript(dp, key) bsddb_subscript(dp, key)
bsddbobject *dp; bsddbobject *dp;
object *key; PyObject *key;
{ {
int status; int status;
object *v; PyObject *v;
DBT krec, drec; DBT krec, drec;
char *data; char *data;
int size; int size;
if (!getargs(key, "s#", &data, &size)) if (!PyArg_Parse(key, "s#", &data, &size))
return NULL; return NULL;
krec.data = data; krec.data = data;
krec.size = size; krec.size = size;
@ -223,27 +222,27 @@ bsddb_subscript(dp, key)
status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0); status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0);
if (status != 0) { if (status != 0) {
if (status < 0) if (status < 0)
err_errno(BsddbError); PyErr_SetFromErrno(BsddbError);
else else
err_setval(KeyError, key); PyErr_SetObject(PyExc_KeyError, key);
return NULL; return NULL;
} }
return newsizedstringobject((char *)drec.data, (int)drec.size); return PyString_FromStringAndSize((char *)drec.data, (int)drec.size);
} }
static int static int
bsddb_ass_sub(dp, key, value) bsddb_ass_sub(dp, key, value)
bsddbobject *dp; bsddbobject *dp;
object *key, *value; PyObject *key, *value;
{ {
int status; int status;
DBT krec, drec; DBT krec, drec;
char *data; char *data;
int size; int size;
if (!getargs(key, "s#", &data, &size)) { if (!PyArg_Parse(key, "s#", &data, &size)) {
err_setstr(TypeError, "bsddb key type must be string"); PyErr_SetString(PyExc_TypeError, "bsddb key type must be string");
return -1; return -1;
} }
krec.data = data; krec.data = data;
@ -253,8 +252,8 @@ bsddb_ass_sub(dp, key, value)
status = (dp->di_bsddb->del)(dp->di_bsddb, &krec, 0); status = (dp->di_bsddb->del)(dp->di_bsddb, &krec, 0);
} }
else { else {
if (!getargs(value, "s#", &data, &size)) { if (!PyArg_Parse(value, "s#", &data, &size)) {
err_setstr(TypeError, "bsddb value type must be string"); PyErr_SetString(PyExc_TypeError, "bsddb value type must be string");
return -1; return -1;
} }
drec.data = data; drec.data = data;
@ -263,115 +262,115 @@ bsddb_ass_sub(dp, key, value)
} }
if (status != 0) { if (status != 0) {
if (status < 0) if (status < 0)
err_errno(BsddbError); PyErr_SetFromErrno(BsddbError);
else else
err_setval(KeyError, key); PyErr_SetObject(PyExc_KeyError, key);
return -1; return -1;
} }
return 0; return 0;
} }
static mapping_methods bsddb_as_mapping = { static PyMappingMethods bsddb_as_mapping = {
(inquiry)bsddb_length, /*mp_length*/ (inquiry)bsddb_length, /*mp_length*/
(binaryfunc)bsddb_subscript, /*mp_subscript*/ (binaryfunc)bsddb_subscript, /*mp_subscript*/
(objobjargproc)bsddb_ass_sub, /*mp_ass_subscript*/ (objobjargproc)bsddb_ass_sub, /*mp_ass_subscript*/
}; };
static object * static PyObject *
bsddb_close(dp, args) bsddb_close(dp, args)
bsddbobject *dp; bsddbobject *dp;
object *args; PyObject *args;
{ {
if (!getnoarg(args)) if (!PyArg_NoArgs(args))
return NULL; return NULL;
if (dp->di_bsddb != NULL) { if (dp->di_bsddb != NULL) {
if ((dp->di_bsddb->close)(dp->di_bsddb) != 0) { if ((dp->di_bsddb->close)(dp->di_bsddb) != 0) {
dp->di_bsddb = NULL; dp->di_bsddb = NULL;
err_errno(BsddbError); PyErr_SetFromErrno(BsddbError);
return NULL; return NULL;
} }
} }
dp->di_bsddb = NULL; dp->di_bsddb = NULL;
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
static object * static PyObject *
bsddb_keys(dp, args) bsddb_keys(dp, args)
bsddbobject *dp; bsddbobject *dp;
object *args; PyObject *args;
{ {
object *list, *item; PyObject *list, *item;
DBT krec, drec; DBT krec, drec;
int status; int status;
int err; int err;
if (!getnoarg(args)) if (!PyArg_NoArgs(args))
return NULL; return NULL;
list = newlistobject(0); list = PyList_New(0);
if (list == NULL) if (list == NULL)
return NULL; return NULL;
for (status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_FIRST); for (status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_FIRST);
status == 0; status == 0;
status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_NEXT)) { status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_NEXT)) {
item = newsizedstringobject((char *)krec.data, (int)krec.size); item = PyString_FromStringAndSize((char *)krec.data, (int)krec.size);
if (item == NULL) { if (item == NULL) {
DECREF(list); Py_DECREF(list);
return NULL; return NULL;
} }
err = addlistitem(list, item); err = PyList_Append(list, item);
DECREF(item); Py_DECREF(item);
if (err != 0) { if (err != 0) {
DECREF(list); Py_DECREF(list);
return NULL; return NULL;
} }
} }
if (status < 0) { if (status < 0) {
err_errno(BsddbError); PyErr_SetFromErrno(BsddbError);
DECREF(list); Py_DECREF(list);
return NULL; return NULL;
} }
if (dp->di_size < 0) if (dp->di_size < 0)
dp->di_size = getlistsize(list); /* We just did the work for this... */ dp->di_size = PyList_Size(list); /* We just did the work for this... */
return list; return list;
} }
static object * static PyObject *
bsddb_has_key(dp, args) bsddb_has_key(dp, args)
bsddbobject *dp; bsddbobject *dp;
object *args; PyObject *args;
{ {
DBT krec, drec; DBT krec, drec;
int status; int status;
char *data; char *data;
int size; int size;
if (!getargs(args, "s#", &data, &size)) if (!PyArg_Parse(args, "s#", &data, &size))
return NULL; return NULL;
krec.data = data; krec.data = data;
krec.size = size; krec.size = size;
status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0); status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0);
if (status < 0) { if (status < 0) {
err_errno(BsddbError); PyErr_SetFromErrno(BsddbError);
return NULL; return NULL;
} }
return newintobject(status == 0); return PyInt_FromLong(status == 0);
} }
static object * static PyObject *
bsddb_set_location(dp, key) bsddb_set_location(dp, key)
bsddbobject *dp; bsddbobject *dp;
object *key; PyObject *key;
{ {
int status; int status;
object *v; PyObject *v;
DBT krec, drec; DBT krec, drec;
char *data; char *data;
int size; int size;
if (!getargs(key, "s#", &data, &size)) if (!PyArg_Parse(key, "s#", &data, &size))
return NULL; return NULL;
krec.data = data; krec.data = data;
krec.size = size; krec.size = size;
@ -379,25 +378,25 @@ bsddb_set_location(dp, key)
status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_CURSOR); status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_CURSOR);
if (status != 0) { if (status != 0) {
if (status < 0) if (status < 0)
err_errno(BsddbError); PyErr_SetFromErrno(BsddbError);
else else
err_setval(KeyError, key); PyErr_SetObject(PyExc_KeyError, key);
return NULL; return NULL;
} }
return mkvalue("s#s#", krec.data, krec.size, drec.data, drec.size); return Py_BuildValue("s#s#", krec.data, krec.size, drec.data, drec.size);
} }
static object * static PyObject *
bsddb_seq(dp, args, sequence_request) bsddb_seq(dp, args, sequence_request)
bsddbobject *dp; bsddbobject *dp;
object *args; PyObject *args;
int sequence_request; int sequence_request;
{ {
int status; int status;
DBT krec, drec; DBT krec, drec;
if (!getnoarg(args)) if (!PyArg_NoArgs(args))
return NULL; return NULL;
krec.data = 0; krec.data = 0;
@ -406,80 +405,80 @@ bsddb_seq(dp, args, sequence_request)
status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, sequence_request); status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, sequence_request);
if (status != 0) { if (status != 0) {
if (status < 0) if (status < 0)
err_errno(BsddbError); PyErr_SetFromErrno(BsddbError);
else else
err_setval(KeyError, args); PyErr_SetObject(PyExc_KeyError, args);
return NULL; return NULL;
} }
return mkvalue("s#s#", krec.data, krec.size, drec.data, drec.size); return Py_BuildValue("s#s#", krec.data, krec.size, drec.data, drec.size);
} }
static object * static PyObject *
bsddb_next(dp, key) bsddb_next(dp, key)
bsddbobject *dp; bsddbobject *dp;
object *key; PyObject *key;
{ {
return bsddb_seq(dp, key, R_NEXT); return bsddb_seq(dp, key, R_NEXT);
} }
static object * static PyObject *
bsddb_previous(dp, key) bsddb_previous(dp, key)
bsddbobject *dp; bsddbobject *dp;
object *key; PyObject *key;
{ {
return bsddb_seq(dp, key, R_PREV); return bsddb_seq(dp, key, R_PREV);
} }
static object * static PyObject *
bsddb_first(dp, key) bsddb_first(dp, key)
bsddbobject *dp; bsddbobject *dp;
object *key; PyObject *key;
{ {
return bsddb_seq(dp, key, R_FIRST); return bsddb_seq(dp, key, R_FIRST);
} }
static object * static PyObject *
bsddb_last(dp, key) bsddb_last(dp, key)
bsddbobject *dp; bsddbobject *dp;
object *key; PyObject *key;
{ {
return bsddb_seq(dp, key, R_LAST); return bsddb_seq(dp, key, R_LAST);
} }
static object * static PyObject *
bsddb_sync(dp, args) bsddb_sync(dp, args)
bsddbobject *dp; bsddbobject *dp;
object *args; PyObject *args;
{ {
int status; int status;
status = (dp->di_bsddb->sync)(dp->di_bsddb, 0); status = (dp->di_bsddb->sync)(dp->di_bsddb, 0);
if (status != 0) { if (status != 0) {
err_errno(BsddbError); PyErr_SetFromErrno(BsddbError);
return NULL; return NULL;
} }
return newintobject(status = 0); return PyInt_FromLong(status = 0);
} }
static struct methodlist bsddb_methods[] = { static PyMethodDef bsddb_methods[] = {
{"close", (method)bsddb_close}, {"close", (PyCFunction)bsddb_close},
{"keys", (method)bsddb_keys}, {"keys", (PyCFunction)bsddb_keys},
{"has_key", (method)bsddb_has_key}, {"has_key", (PyCFunction)bsddb_has_key},
{"set_location", (method)bsddb_set_location}, {"set_location", (PyCFunction)bsddb_set_location},
{"next", (method)bsddb_next}, {"next", (PyCFunction)bsddb_next},
{"previous", (method)bsddb_previous}, {"previous", (PyCFunction)bsddb_previous},
{"first", (method)bsddb_first}, {"first", (PyCFunction)bsddb_first},
{"last", (method)bsddb_last}, {"last", (PyCFunction)bsddb_last},
{"sync", (method)bsddb_sync}, {"sync", (PyCFunction)bsddb_sync},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
static object * static PyObject *
bsddb_getattr(dp, name) bsddb_getattr(dp, name)
object *dp; PyObject *dp;
char *name; char *name;
{ {
return findmethod(bsddb_methods, dp, name); return Py_FindMethod(bsddb_methods, dp, name);
} }
static typeobject Bsddbtype = { static PyTypeObject Bsddbtype = {
OB_HEAD_INIT(&Typetype) PyObject_HEAD_INIT(&PyType_Type)
0, 0,
"bsddb", "bsddb",
sizeof(bsddbobject), sizeof(bsddbobject),
@ -495,10 +494,10 @@ static typeobject Bsddbtype = {
&bsddb_as_mapping, /*tp_as_mapping*/ &bsddb_as_mapping, /*tp_as_mapping*/
}; };
static object * static PyObject *
bsdhashopen(self, args) bsdhashopen(self, args)
object *self; PyObject *self;
object *args; PyObject *args;
{ {
char *file; char *file;
char *flag = NULL; char *flag = NULL;
@ -511,7 +510,7 @@ bsdhashopen(self, args)
int hash = 0; /* XXX currently ignored */ int hash = 0; /* XXX currently ignored */
int lorder = 0; int lorder = 0;
if (!newgetargs(args, "s|siiiiiii", if (!PyArg_ParseTuple(args, "s|siiiiiii",
&file, &flag, &mode, &file, &flag, &mode,
&bsize, &ffactor, &nelem, &cachesize, &hash, &lorder)) &bsize, &ffactor, &nelem, &cachesize, &hash, &lorder))
return NULL; return NULL;
@ -526,7 +525,7 @@ bsdhashopen(self, args)
else if (flag[0] == 'n') else if (flag[0] == 'n')
flags = O_RDWR|O_CREAT|O_TRUNC; flags = O_RDWR|O_CREAT|O_TRUNC;
else { else {
err_setstr(BsddbError, PyErr_SetString(BsddbError,
"Flag should begin with 'r', 'w', 'c' or 'n'"); "Flag should begin with 'r', 'w', 'c' or 'n'");
return NULL; return NULL;
} }
@ -537,7 +536,7 @@ bsdhashopen(self, args)
else else
flags |= O_EXLOCK; flags |= O_EXLOCK;
#else #else
err_setstr(BsddbError, "locking not supported on this platform"); PyErr_SetString(BsddbError, "locking not supported on this platform");
return NULL; return NULL;
#endif #endif
} }
@ -546,10 +545,10 @@ bsdhashopen(self, args)
bsize, ffactor, nelem, cachesize, hash, lorder); bsize, ffactor, nelem, cachesize, hash, lorder);
} }
static object * static PyObject *
bsdbtopen(self, args) bsdbtopen(self, args)
object *self; PyObject *self;
object *args; PyObject *args;
{ {
char *file; char *file;
char *flag = NULL; char *flag = NULL;
@ -562,7 +561,7 @@ bsdbtopen(self, args)
unsigned int psize = 0; unsigned int psize = 0;
int lorder = 0; int lorder = 0;
if (!newgetargs(args, "s|siiiiiii", if (!PyArg_ParseTuple(args, "s|siiiiiii",
&file, &flag, &mode, &file, &flag, &mode,
&btflags, &cachesize, &maxkeypage, &minkeypage, &btflags, &cachesize, &maxkeypage, &minkeypage,
&psize, &lorder)) &psize, &lorder))
@ -578,7 +577,7 @@ bsdbtopen(self, args)
else if (flag[0] == 'n') else if (flag[0] == 'n')
flags = O_RDWR|O_CREAT|O_TRUNC; flags = O_RDWR|O_CREAT|O_TRUNC;
else { else {
err_setstr(BsddbError, PyErr_SetString(BsddbError,
"Flag should begin with 'r', 'w', 'c' or 'n'"); "Flag should begin with 'r', 'w', 'c' or 'n'");
return NULL; return NULL;
} }
@ -589,7 +588,7 @@ bsdbtopen(self, args)
else else
flags |= O_EXLOCK; flags |= O_EXLOCK;
#else #else
err_setstr(BsddbError, "locking not supported on this platform"); PyErr_SetString(BsddbError, "locking not supported on this platform");
return NULL; return NULL;
#endif #endif
} }
@ -599,10 +598,10 @@ bsdbtopen(self, args)
psize, lorder); psize, lorder);
} }
static object * static PyObject *
bsdrnopen(self, args) bsdrnopen(self, args)
object *self; PyObject *self;
object *args; PyObject *args;
{ {
char *file; char *file;
char *flag = NULL; char *flag = NULL;
@ -616,7 +615,7 @@ bsdrnopen(self, args)
char *bval = ""; char *bval = "";
char *bfname = NULL; char *bfname = NULL;
if (!newgetargs(args, "s|siiiiiiss", if (!PyArg_ParseTuple(args, "s|siiiiiiss",
&file, &flag, &mode, &file, &flag, &mode,
&rnflags, &cachesize, &psize, &lorder, &rnflags, &cachesize, &psize, &lorder,
&reclen, &bval, &bfname)) &reclen, &bval, &bfname))
@ -632,7 +631,7 @@ bsdrnopen(self, args)
else if (flag[0] == 'n') else if (flag[0] == 'n')
flags = O_RDWR|O_CREAT|O_TRUNC; flags = O_RDWR|O_CREAT|O_TRUNC;
else { else {
err_setstr(BsddbError, PyErr_SetString(BsddbError,
"Flag should begin with 'r', 'w', 'c' or 'n'"); "Flag should begin with 'r', 'w', 'c' or 'n'");
return NULL; return NULL;
} }
@ -643,12 +642,12 @@ bsdrnopen(self, args)
else else
flags |= O_EXLOCK; flags |= O_EXLOCK;
#else #else
err_setstr(BsddbError, "locking not supported on this platform"); PyErr_SetString(BsddbError, "locking not supported on this platform");
return NULL; return NULL;
#endif #endif
} }
else if (flag[1] != '\0') { else if (flag[1] != '\0') {
err_setstr(BsddbError, PyErr_SetString(BsddbError,
"Flag char 2 should be 'l' or absent"); "Flag char 2 should be 'l' or absent");
return NULL; return NULL;
} }
@ -657,20 +656,20 @@ bsdrnopen(self, args)
rnflags, cachesize, psize, lorder, bval[0], bfname); rnflags, cachesize, psize, lorder, bval[0], bfname);
} }
static struct methodlist bsddbmodule_methods[] = { static PyMethodDef bsddbmodule_methods[] = {
{"hashopen", (method)bsdhashopen, 1}, {"hashopen", (PyCFunction)bsdhashopen, 1},
{"btopen", (method)bsdbtopen, 1}, {"btopen", (PyCFunction)bsdbtopen, 1},
{"rnopen", (method)bsdrnopen, 1}, {"rnopen", (PyCFunction)bsdrnopen, 1},
{0, 0}, {0, 0},
}; };
void void
initbsddb() { initbsddb() {
object *m, *d; PyObject *m, *d;
m = initmodule("bsddb", bsddbmodule_methods); m = Py_InitModule("bsddb", bsddbmodule_methods);
d = getmoduledict(m); d = PyModule_GetDict(m);
BsddbError = newstringobject("bsddb.error"); BsddbError = PyString_FromString("bsddb.error");
if (BsddbError == NULL || dictinsert(d, "error", BsddbError)) if (BsddbError == NULL || PyDict_SetItemString(d, "error", BsddbError))
fatal("can't define bsddb.error"); Py_FatalError("can't define bsddb.error");
} }

View File

@ -31,24 +31,22 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* This uses Tatu Ylonen's copyleft-free reimplementation of /* This uses Tatu Ylonen's copyleft-free reimplementation of
GNU regular expressions */ GNU regular expressions */
#include "allobjects.h" #include "Python.h"
#include "modsupport.h"
#include "regexpr.h" #include "regexpr.h"
#include <ctype.h>
static object *RegexError; /* Exception */ static PyObject *RegexError; /* Exception */
typedef struct { typedef struct {
OB_HEAD PyObject_HEAD
struct re_pattern_buffer re_patbuf; /* The compiled expression */ struct re_pattern_buffer re_patbuf; /* The compiled expression */
struct re_registers re_regs; /* The registers from the last match */ struct re_registers re_regs; /* The registers from the last match */
char re_fastmap[256]; /* Storage for fastmap */ char re_fastmap[256]; /* Storage for fastmap */
object *re_translate; /* String object for translate table */ PyObject *re_translate; /* String object for translate table */
object *re_lastok; /* String object last matched/searched */ PyObject *re_lastok; /* String object last matched/searched */
object *re_groupindex; /* Group name to index dictionary */ PyObject *re_groupindex; /* Group name to index dictionary */
object *re_givenpat; /* Pattern with symbolic groups */ PyObject *re_givenpat; /* Pattern with symbolic groups */
object *re_realpat; /* Pattern without symbolic groups */ PyObject *re_realpat; /* Pattern without symbolic groups */
} regexobject; } regexobject;
/* Regex object methods */ /* Regex object methods */
@ -57,99 +55,99 @@ static void
reg_dealloc(re) reg_dealloc(re)
regexobject *re; regexobject *re;
{ {
XDEL(re->re_patbuf.buffer); PyMem_XDEL(re->re_patbuf.buffer);
XDECREF(re->re_translate); Py_XDECREF(re->re_translate);
XDECREF(re->re_lastok); Py_XDECREF(re->re_lastok);
XDECREF(re->re_groupindex); Py_XDECREF(re->re_groupindex);
XDECREF(re->re_givenpat); Py_XDECREF(re->re_givenpat);
XDECREF(re->re_realpat); Py_XDECREF(re->re_realpat);
DEL(re); PyMem_DEL(re);
} }
static object * static PyObject *
makeresult(regs) makeresult(regs)
struct re_registers *regs; struct re_registers *regs;
{ {
object *v = newtupleobject(RE_NREGS); PyObject *v = PyTuple_New(RE_NREGS);
if (v != NULL) { if (v != NULL) {
int i; int i;
for (i = 0; i < RE_NREGS; i++) { for (i = 0; i < RE_NREGS; i++) {
object *w; PyObject *w;
w = mkvalue("(ii)", regs->start[i], regs->end[i]); w = Py_BuildValue("(ii)", regs->start[i], regs->end[i]);
if (w == NULL) { if (w == NULL) {
XDECREF(v); Py_XDECREF(v);
v = NULL; v = NULL;
break; break;
} }
settupleitem(v, i, w); PyTuple_SetItem(v, i, w);
} }
} }
return v; return v;
} }
static object * static PyObject *
reg_match(re, args) reg_match(re, args)
regexobject *re; regexobject *re;
object *args; PyObject *args;
{ {
object *argstring; PyObject *argstring;
char *buffer; char *buffer;
int size; int size;
int offset; int offset;
int result; int result;
if (getargs(args, "S", &argstring)) { if (PyArg_Parse(args, "S", &argstring)) {
offset = 0; offset = 0;
} }
else { else {
err_clear(); PyErr_Clear();
if (!getargs(args, "(Si)", &argstring, &offset)) if (!PyArg_Parse(args, "(Si)", &argstring, &offset))
return NULL; return NULL;
} }
buffer = getstringvalue(argstring); buffer = PyString_AsString(argstring);
size = getstringsize(argstring); size = PyString_Size(argstring);
if (offset < 0 || offset > size) { if (offset < 0 || offset > size) {
err_setstr(RegexError, "match offset out of range"); PyErr_SetString(RegexError, "match offset out of range");
return NULL; return NULL;
} }
XDECREF(re->re_lastok); Py_XDECREF(re->re_lastok);
re->re_lastok = NULL; re->re_lastok = NULL;
result = re_match(&re->re_patbuf, buffer, size, offset, &re->re_regs); result = re_match(&re->re_patbuf, buffer, size, offset, &re->re_regs);
if (result < -1) { if (result < -1) {
/* Failure like stack overflow */ /* Failure like stack overflow */
err_setstr(RegexError, "match failure"); PyErr_SetString(RegexError, "match failure");
return NULL; return NULL;
} }
if (result >= 0) { if (result >= 0) {
INCREF(argstring); Py_INCREF(argstring);
re->re_lastok = argstring; re->re_lastok = argstring;
} }
return newintobject((long)result); /* Length of the match or -1 */ return PyInt_FromLong((long)result); /* Length of the match or -1 */
} }
static object * static PyObject *
reg_search(re, args) reg_search(re, args)
regexobject *re; regexobject *re;
object *args; PyObject *args;
{ {
object *argstring; PyObject *argstring;
char *buffer; char *buffer;
int size; int size;
int offset; int offset;
int range; int range;
int result; int result;
if (getargs(args, "S", &argstring)) { if (PyArg_Parse(args, "S", &argstring)) {
offset = 0; offset = 0;
} }
else { else {
err_clear(); PyErr_Clear();
if (!getargs(args, "(Si)", &argstring, &offset)) if (!PyArg_Parse(args, "(Si)", &argstring, &offset))
return NULL; return NULL;
} }
buffer = getstringvalue(argstring); buffer = PyString_AsString(argstring);
size = getstringsize(argstring); size = PyString_Size(argstring);
if (offset < 0 || offset > size) { if (offset < 0 || offset > size) {
err_setstr(RegexError, "search offset out of range"); PyErr_SetString(RegexError, "search offset out of range");
return NULL; return NULL;
} }
/* NB: In Emacs 18.57, the documentation for re_search[_2] and /* NB: In Emacs 18.57, the documentation for re_search[_2] and
@ -157,159 +155,159 @@ reg_search(re, args)
|range| positions are tried, while the code tries |range|+1 |range| positions are tried, while the code tries |range|+1
positions. It seems more productive to believe the code! */ positions. It seems more productive to believe the code! */
range = size - offset; range = size - offset;
XDECREF(re->re_lastok); Py_XDECREF(re->re_lastok);
re->re_lastok = NULL; re->re_lastok = NULL;
result = re_search(&re->re_patbuf, buffer, size, offset, range, result = re_search(&re->re_patbuf, buffer, size, offset, range,
&re->re_regs); &re->re_regs);
if (result < -1) { if (result < -1) {
/* Failure like stack overflow */ /* Failure like stack overflow */
err_setstr(RegexError, "match failure"); PyErr_SetString(RegexError, "match failure");
return NULL; return NULL;
} }
if (result >= 0) { if (result >= 0) {
INCREF(argstring); Py_INCREF(argstring);
re->re_lastok = argstring; re->re_lastok = argstring;
} }
return newintobject((long)result); /* Position of the match or -1 */ return PyInt_FromLong((long)result); /* Position of the match or -1 */
} }
static object * static PyObject *
reg_group(re, args) reg_group(re, args)
regexobject *re; regexobject *re;
object *args; PyObject *args;
{ {
int i, a, b; int i, a, b;
if (args != NULL && is_tupleobject(args)) { if (args != NULL && PyTuple_Check(args)) {
int n = gettuplesize(args); int n = PyTuple_Size(args);
object *res = newtupleobject(n); PyObject *res = PyTuple_New(n);
if (res == NULL) if (res == NULL)
return NULL; return NULL;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
object *v = reg_group(re, gettupleitem(args, i)); PyObject *v = reg_group(re, PyTuple_GetItem(args, i));
if (v == NULL) { if (v == NULL) {
DECREF(res); Py_DECREF(res);
return NULL; return NULL;
} }
settupleitem(res, i, v); PyTuple_SetItem(res, i, v);
} }
return res; return res;
} }
if (!getargs(args, "i", &i)) { if (!PyArg_Parse(args, "i", &i)) {
object *n; PyObject *n;
err_clear(); PyErr_Clear();
if (!getargs(args, "S", &n)) if (!PyArg_Parse(args, "S", &n))
return NULL; return NULL;
else { else {
object *index; PyObject *index;
if (re->re_groupindex == NULL) if (re->re_groupindex == NULL)
index = NULL; index = NULL;
else else
index = mappinglookup(re->re_groupindex, n); index = PyDict_GetItem(re->re_groupindex, n);
if (index == NULL) { if (index == NULL) {
err_setstr(RegexError, "group() group name doesn't exist"); PyErr_SetString(RegexError, "group() group name doesn't exist");
return NULL; return NULL;
} }
i = getintvalue(index); i = PyInt_AsLong(index);
} }
} }
if (i < 0 || i >= RE_NREGS) { if (i < 0 || i >= RE_NREGS) {
err_setstr(RegexError, "group() index out of range"); PyErr_SetString(RegexError, "group() index out of range");
return NULL; return NULL;
} }
if (re->re_lastok == NULL) { if (re->re_lastok == NULL) {
err_setstr(RegexError, PyErr_SetString(RegexError,
"group() only valid after successful match/search"); "group() only valid after successful match/search");
return NULL; return NULL;
} }
a = re->re_regs.start[i]; a = re->re_regs.start[i];
b = re->re_regs.end[i]; b = re->re_regs.end[i];
if (a < 0 || b < 0) { if (a < 0 || b < 0) {
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
return newsizedstringobject(getstringvalue(re->re_lastok)+a, b-a); return PyString_FromStringAndSize(PyString_AsString(re->re_lastok)+a, b-a);
} }
static struct methodlist reg_methods[] = { static struct PyMethodDef reg_methods[] = {
{"match", (method)reg_match}, {"match", (PyCFunction)reg_match},
{"search", (method)reg_search}, {"search", (PyCFunction)reg_search},
{"group", (method)reg_group}, {"group", (PyCFunction)reg_group},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
static object * static PyObject *
reg_getattr(re, name) reg_getattr(re, name)
regexobject *re; regexobject *re;
char *name; char *name;
{ {
if (strcmp(name, "regs") == 0) { if (strcmp(name, "regs") == 0) {
if (re->re_lastok == NULL) { if (re->re_lastok == NULL) {
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
return makeresult(&re->re_regs); return makeresult(&re->re_regs);
} }
if (strcmp(name, "last") == 0) { if (strcmp(name, "last") == 0) {
if (re->re_lastok == NULL) { if (re->re_lastok == NULL) {
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
INCREF(re->re_lastok); Py_INCREF(re->re_lastok);
return re->re_lastok; return re->re_lastok;
} }
if (strcmp(name, "translate") == 0) { if (strcmp(name, "translate") == 0) {
if (re->re_translate == NULL) { if (re->re_translate == NULL) {
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
INCREF(re->re_translate); Py_INCREF(re->re_translate);
return re->re_translate; return re->re_translate;
} }
if (strcmp(name, "groupindex") == 0) { if (strcmp(name, "groupindex") == 0) {
if (re->re_groupindex == NULL) { if (re->re_groupindex == NULL) {
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
INCREF(re->re_groupindex); Py_INCREF(re->re_groupindex);
return re->re_groupindex; return re->re_groupindex;
} }
if (strcmp(name, "realpat") == 0) { if (strcmp(name, "realpat") == 0) {
if (re->re_realpat == NULL) { if (re->re_realpat == NULL) {
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
INCREF(re->re_realpat); Py_INCREF(re->re_realpat);
return re->re_realpat; return re->re_realpat;
} }
if (strcmp(name, "givenpat") == 0) { if (strcmp(name, "givenpat") == 0) {
if (re->re_givenpat == NULL) { if (re->re_givenpat == NULL) {
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
INCREF(re->re_givenpat); Py_INCREF(re->re_givenpat);
return re->re_givenpat; return re->re_givenpat;
} }
if (strcmp(name, "__members__") == 0) { if (strcmp(name, "__members__") == 0) {
object *list = newlistobject(6); PyObject *list = PyList_New(6);
if (list) { if (list) {
setlistitem(list, 0, newstringobject("last")); PyList_SetItem(list, 0, PyString_FromString("last"));
setlistitem(list, 1, newstringobject("regs")); PyList_SetItem(list, 1, PyString_FromString("regs"));
setlistitem(list, 2, newstringobject("translate")); PyList_SetItem(list, 2, PyString_FromString("translate"));
setlistitem(list, 3, newstringobject("groupindex")); PyList_SetItem(list, 3, PyString_FromString("groupindex"));
setlistitem(list, 4, newstringobject("realpat")); PyList_SetItem(list, 4, PyString_FromString("realpat"));
setlistitem(list, 5, newstringobject("givenpat")); PyList_SetItem(list, 5, PyString_FromString("givenpat"));
if (err_occurred()) { if (PyErr_Occurred()) {
DECREF(list); Py_DECREF(list);
list = NULL; list = NULL;
} }
} }
return list; return list;
} }
return findmethod(reg_methods, (object *)re, name); return Py_FindMethod(reg_methods, (PyObject *)re, name);
} }
static typeobject Regextype = { static PyTypeObject Regextype = {
OB_HEAD_INIT(&Typetype) PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/ 0, /*ob_size*/
"regex", /*tp_name*/ "regex", /*tp_name*/
sizeof(regexobject), /*tp_size*/ sizeof(regexobject), /*tp_size*/
@ -323,90 +321,90 @@ static typeobject Regextype = {
0, /*tp_repr*/ 0, /*tp_repr*/
}; };
static object * static PyObject *
newregexobject(pattern, translate, givenpat, groupindex) newregexobject(pattern, translate, givenpat, groupindex)
object *pattern; PyObject *pattern;
object *translate; PyObject *translate;
object *givenpat; PyObject *givenpat;
object *groupindex; PyObject *groupindex;
{ {
regexobject *re; regexobject *re;
char *pat = getstringvalue(pattern); char *pat = PyString_AsString(pattern);
int size = getstringsize(pattern); int size = PyString_Size(pattern);
if (translate != NULL && getstringsize(translate) != 256) { if (translate != NULL && PyString_Size(translate) != 256) {
err_setstr(RegexError, PyErr_SetString(RegexError,
"translation table must be 256 bytes"); "translation table must be 256 bytes");
return NULL; return NULL;
} }
re = NEWOBJ(regexobject, &Regextype); re = PyObject_NEW(regexobject, &Regextype);
if (re != NULL) { if (re != NULL) {
char *error; char *error;
re->re_patbuf.buffer = NULL; re->re_patbuf.buffer = NULL;
re->re_patbuf.allocated = 0; re->re_patbuf.allocated = 0;
re->re_patbuf.fastmap = re->re_fastmap; re->re_patbuf.fastmap = re->re_fastmap;
if (translate) if (translate)
re->re_patbuf.translate = getstringvalue(translate); re->re_patbuf.translate = PyString_AsString(translate);
else else
re->re_patbuf.translate = NULL; re->re_patbuf.translate = NULL;
XINCREF(translate); Py_XINCREF(translate);
re->re_translate = translate; re->re_translate = translate;
re->re_lastok = NULL; re->re_lastok = NULL;
re->re_groupindex = groupindex; re->re_groupindex = groupindex;
INCREF(pattern); Py_INCREF(pattern);
re->re_realpat = pattern; re->re_realpat = pattern;
INCREF(givenpat); Py_INCREF(givenpat);
re->re_givenpat = givenpat; re->re_givenpat = givenpat;
error = re_compile_pattern(pat, size, &re->re_patbuf); error = re_compile_pattern(pat, size, &re->re_patbuf);
if (error != NULL) { if (error != NULL) {
err_setstr(RegexError, error); PyErr_SetString(RegexError, error);
DECREF(re); Py_DECREF(re);
re = NULL; re = NULL;
} }
} }
return (object *)re; return (PyObject *)re;
} }
static object * static PyObject *
regex_compile(self, args) regex_compile(self, args)
object *self; PyObject *self;
object *args; PyObject *args;
{ {
object *pat = NULL; PyObject *pat = NULL;
object *tran = NULL; PyObject *tran = NULL;
if (!getargs(args, "S", &pat)) { if (!PyArg_Parse(args, "S", &pat)) {
err_clear(); PyErr_Clear();
if (!getargs(args, "(SS)", &pat, &tran)) if (!PyArg_Parse(args, "(SS)", &pat, &tran))
return NULL; return NULL;
} }
return newregexobject(pat, tran, pat, NULL); return newregexobject(pat, tran, pat, NULL);
} }
static object * static PyObject *
symcomp(pattern, gdict) symcomp(pattern, gdict)
object *pattern; PyObject *pattern;
object *gdict; PyObject *gdict;
{ {
char *opat = getstringvalue(pattern); char *opat = PyString_AsString(pattern);
char *oend = opat + getstringsize(pattern); char *oend = opat + PyString_Size(pattern);
int group_count = 0; int group_count = 0;
int escaped = 0; int escaped = 0;
char *o = opat; char *o = opat;
char *n; char *n;
char name_buf[128]; char name_buf[128];
char *g; char *g;
object *npattern; PyObject *npattern;
int require_escape = re_syntax & RE_NO_BK_PARENS ? 0 : 1; int require_escape = re_syntax & RE_NO_BK_PARENS ? 0 : 1;
if (oend == opat) { if (oend == opat) {
INCREF(pattern); Py_INCREF(pattern);
return pattern; return pattern;
} }
npattern = newsizedstringobject((char*)NULL, getstringsize(pattern)); npattern = PyString_FromStringAndSize((char*)NULL, PyString_Size(pattern));
if (npattern == NULL) if (npattern == NULL)
return NULL; return NULL;
n = getstringvalue(npattern); n = PyString_AsString(npattern);
while (o < oend) { while (o < oend) {
if (*o == '(' && escaped == require_escape) { if (*o == '(' && escaped == require_escape) {
@ -423,16 +421,16 @@ symcomp(pattern, gdict)
g = name_buf; g = name_buf;
for (++o; o < oend;) { for (++o; o < oend;) {
if (*o == '>') { if (*o == '>') {
object *group_name = NULL; PyObject *group_name = NULL;
object *group_index = NULL; PyObject *group_index = NULL;
*g++ = '\0'; *g++ = '\0';
group_name = newstringobject(name_buf); group_name = PyString_FromString(name_buf);
group_index = newintobject(group_count); group_index = PyInt_FromLong(group_count);
if (group_name == NULL || group_index == NULL if (group_name == NULL || group_index == NULL
|| mappinginsert(gdict, group_name, group_index) != 0) { || PyDict_SetItem(gdict, group_name, group_index) != 0) {
XDECREF(group_name); Py_XDECREF(group_name);
XDECREF(group_index); Py_XDECREF(group_index);
XDECREF(npattern); Py_XDECREF(npattern);
return NULL; return NULL;
} }
++o; /* eat the '>' */ ++o; /* eat the '>' */
@ -468,7 +466,7 @@ symcomp(pattern, gdict)
} }
} }
if (resizestring(&npattern, n - getstringvalue(npattern)) == 0) if (_PyString_Resize(&npattern, n - PyString_AsString(npattern)) == 0)
return npattern; return npattern;
else { else {
return NULL; return NULL;
@ -476,89 +474,89 @@ symcomp(pattern, gdict)
} }
static object * static PyObject *
regex_symcomp(self, args) regex_symcomp(self, args)
object *self; PyObject *self;
object *args; PyObject *args;
{ {
object *pattern; PyObject *pattern;
object *tran = NULL; PyObject *tran = NULL;
object *gdict = NULL; PyObject *gdict = NULL;
object *npattern; PyObject *npattern;
if (!getargs(args, "S", &pattern)) { if (!PyArg_Parse(args, "S", &pattern)) {
err_clear(); PyErr_Clear();
if (!getargs(args, "(SS)", &pattern, &tran)) if (!PyArg_Parse(args, "(SS)", &pattern, &tran))
return NULL; return NULL;
} }
gdict = newmappingobject(); gdict = PyDict_New();
if (gdict == NULL if (gdict == NULL
|| (npattern = symcomp(pattern, gdict)) == NULL) { || (npattern = symcomp(pattern, gdict)) == NULL) {
DECREF(gdict); Py_DECREF(gdict);
DECREF(pattern); Py_DECREF(pattern);
return NULL; return NULL;
} }
return newregexobject(npattern, tran, pattern, gdict); return newregexobject(npattern, tran, pattern, gdict);
} }
static object *cache_pat; static PyObject *cache_pat;
static object *cache_prog; static PyObject *cache_prog;
static int static int
update_cache(pat) update_cache(pat)
object *pat; PyObject *pat;
{ {
if (pat != cache_pat) { if (pat != cache_pat) {
XDECREF(cache_pat); Py_XDECREF(cache_pat);
cache_pat = NULL; cache_pat = NULL;
XDECREF(cache_prog); Py_XDECREF(cache_prog);
cache_prog = regex_compile((object *)NULL, pat); cache_prog = regex_compile((PyObject *)NULL, pat);
if (cache_prog == NULL) if (cache_prog == NULL)
return -1; return -1;
cache_pat = pat; cache_pat = pat;
INCREF(cache_pat); Py_INCREF(cache_pat);
} }
return 0; return 0;
} }
static object * static PyObject *
regex_match(self, args) regex_match(self, args)
object *self; PyObject *self;
object *args; PyObject *args;
{ {
object *pat, *string; PyObject *pat, *string;
if (!getargs(args, "(SS)", &pat, &string)) if (!PyArg_Parse(args, "(SS)", &pat, &string))
return NULL; return NULL;
if (update_cache(pat) < 0) if (update_cache(pat) < 0)
return NULL; return NULL;
return reg_match((regexobject *)cache_prog, string); return reg_match((regexobject *)cache_prog, string);
} }
static object * static PyObject *
regex_search(self, args) regex_search(self, args)
object *self; PyObject *self;
object *args; PyObject *args;
{ {
object *pat, *string; PyObject *pat, *string;
if (!getargs(args, "(SS)", &pat, &string)) if (!PyArg_Parse(args, "(SS)", &pat, &string))
return NULL; return NULL;
if (update_cache(pat) < 0) if (update_cache(pat) < 0)
return NULL; return NULL;
return reg_search((regexobject *)cache_prog, string); return reg_search((regexobject *)cache_prog, string);
} }
static object * static PyObject *
regex_set_syntax(self, args) regex_set_syntax(self, args)
object *self, *args; PyObject *self, *args;
{ {
int syntax; int syntax;
if (!getintarg(args, &syntax)) if (!PyArg_Parse(args, "i", &syntax))
return NULL; return NULL;
syntax = re_set_syntax(syntax); syntax = re_set_syntax(syntax);
return newintobject((long)syntax); return PyInt_FromLong((long)syntax);
} }
static struct methodlist regex_global_methods[] = { static struct PyMethodDef regex_global_methods[] = {
{"compile", regex_compile, 0}, {"compile", regex_compile, 0},
{"symcomp", regex_symcomp, 0}, {"symcomp", regex_symcomp, 0},
{"match", regex_match, 0}, {"match", regex_match, 0},
@ -569,28 +567,28 @@ static struct methodlist regex_global_methods[] = {
initregex() initregex()
{ {
object *m, *d, *v; PyObject *m, *d, *v;
m = initmodule("regex", regex_global_methods); m = Py_InitModule("regex", regex_global_methods);
d = getmoduledict(m); d = PyModule_GetDict(m);
/* Initialize regex.error exception */ /* Initialize regex.error exception */
RegexError = newstringobject("regex.error"); RegexError = PyString_FromString("regex.error");
if (RegexError == NULL || dictinsert(d, "error", RegexError) != 0) if (RegexError == NULL || PyDict_SetItemString(d, "error", RegexError) != 0)
fatal("can't define regex.error"); Py_FatalError("can't define regex.error");
/* Initialize regex.casefold constant */ /* Initialize regex.casefold constant */
v = newsizedstringobject((char *)NULL, 256); v = PyString_FromStringAndSize((char *)NULL, 256);
if (v != NULL) { if (v != NULL) {
int i; int i;
char *s = getstringvalue(v); char *s = PyString_AsString(v);
for (i = 0; i < 256; i++) { for (i = 0; i < 256; i++) {
if (isupper(i)) if (isupper(i))
s[i] = tolower(i); s[i] = tolower(i);
else else
s[i] = i; s[i] = i;
} }
dictinsert(d, "casefold", v); PyDict_SetItemString(d, "casefold", v);
DECREF(v); Py_DECREF(v);
} }
} }