ANSI-fy the sources.

Convert to using PyArg_ParseTuple() as appropriate to allow method names
in error messages.
Re-indent to four-space indentation.
This commit is contained in:
Fred Drake 2000-07-08 05:00:07 +00:00
parent 0c89d4ec9a
commit e3a41c653a
1 changed files with 277 additions and 303 deletions

View File

@ -65,10 +65,7 @@ GDBM objects also support additional operations such as firstkey,\n\
nextkey, reorganize, and sync.";
static PyObject *
newdbmobject(file, flags, mode)
char *file;
int flags;
int mode;
newdbmobject(char *file, int flags, int mode)
{
dbmobject *dp;
@ -77,7 +74,7 @@ int mode;
return NULL;
dp->di_size = -1;
errno = 0;
if ( (dp->di_dbm = gdbm_open(file, 0, flags, mode, NULL)) == 0 ) {
if ((dp->di_dbm = gdbm_open(file, 0, flags, mode, NULL)) == 0) {
if (errno != 0)
PyErr_SetFromErrno(DbmError);
else
@ -91,29 +88,27 @@ int mode;
/* Methods */
static void
dbm_dealloc(dp)
register dbmobject *dp;
dbm_dealloc(register dbmobject *dp)
{
if ( dp->di_dbm )
if (dp->di_dbm)
gdbm_close(dp->di_dbm);
PyObject_Del(dp);
}
static int
dbm_length(dp)
dbmobject *dp;
dbm_length(dbmobject *dp)
{
if (dp->di_dbm == NULL) {
PyErr_SetString(DbmError, "GDBM object has already been closed");
return -1;
}
if ( dp->di_size < 0 ) {
if (dp->di_size < 0) {
datum key,okey;
int size;
okey.dsize=0;
size = 0;
for ( key=gdbm_firstkey(dp->di_dbm); key.dptr;
for (key=gdbm_firstkey(dp->di_dbm); key.dptr;
key = gdbm_nextkey(dp->di_dbm,okey)) {
size++;
if(okey.dsize) free(okey.dptr);
@ -125,9 +120,7 @@ dbm_length(dp)
}
static PyObject *
dbm_subscript(dp, key)
dbmobject *dp;
register PyObject *key;
dbm_subscript(dbmobject *dp, register PyObject *key)
{
PyObject *v;
datum drec, krec;
@ -141,7 +134,7 @@ register PyObject *key;
return NULL;
}
drec = gdbm_fetch(dp->di_dbm, krec);
if ( drec.dptr == 0 ) {
if (drec.dptr == 0) {
PyErr_SetString(PyExc_KeyError,
PyString_AS_STRING((PyStringObject *)key));
return NULL;
@ -152,13 +145,11 @@ register PyObject *key;
}
static int
dbm_ass_sub(dp, v, w)
dbmobject *dp;
PyObject *v, *w;
dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w)
{
datum krec, drec;
if ( !PyArg_Parse(v, "s#", &krec.dptr, &krec.dsize) ) {
if (!PyArg_Parse(v, "s#", &krec.dptr, &krec.dsize) ) {
PyErr_SetString(PyExc_TypeError,
"gdbm mappings have string indices only");
return -1;
@ -170,19 +161,20 @@ PyObject *v, *w;
}
dp->di_size = -1;
if (w == NULL) {
if ( gdbm_delete(dp->di_dbm, krec) < 0 ) {
if (gdbm_delete(dp->di_dbm, krec) < 0) {
PyErr_SetString(PyExc_KeyError,
PyString_AS_STRING((PyStringObject *)v));
return -1;
}
} else {
if ( !PyArg_Parse(w, "s#", &drec.dptr, &drec.dsize) ) {
}
else {
if (!PyArg_Parse(w, "s#", &drec.dptr, &drec.dsize)) {
PyErr_SetString(PyExc_TypeError,
"gdbm mappings have string elements only");
return -1;
}
errno = 0;
if ( gdbm_store(dp->di_dbm, krec, drec, GDBM_REPLACE) < 0 ) {
if (gdbm_store(dp->di_dbm, krec, drec, GDBM_REPLACE) < 0) {
if (errno != 0)
PyErr_SetFromErrno(DbmError);
else
@ -205,13 +197,11 @@ close() -> None\n\
Closes the database.";
static PyObject *
dbm_close(dp, args)
register dbmobject *dp;
PyObject *args;
dbm_close(register dbmobject *dp, PyObject *args)
{
if ( !PyArg_NoArgs(args) )
if (!PyArg_ParseTuple(args, ":close"))
return NULL;
if ( dp->di_dbm )
if (dp->di_dbm)
gdbm_close(dp->di_dbm);
dp->di_dbm = NULL;
Py_INCREF(Py_None);
@ -223,9 +213,7 @@ keys() -> list_of_keys\n\
Get a list of all keys in the database.";
static PyObject *
dbm_keys(dp, args)
register dbmobject *dp;
PyObject *args;
dbm_keys(register dbmobject *dp, PyObject *args)
{
register PyObject *v, *item;
datum key, nextkey;
@ -235,8 +223,7 @@ PyObject *args;
PyErr_BadInternalCall();
return NULL;
}
if (!PyArg_NoArgs(args))
if (!PyArg_ParseTuple(args, ":keys"))
return NULL;
check_dbmobject_open(dp);
@ -264,7 +251,6 @@ PyObject *args;
free(key.dptr);
key = nextkey;
}
return v;
}
@ -273,13 +259,11 @@ has_key(key) -> boolean\n\
Find out whether or not the database contains a given key.";
static PyObject *
dbm_has_key(dp, args)
register dbmobject *dp;
PyObject *args;
dbm_has_key(register dbmobject *dp, PyObject *args)
{
datum key;
if (!PyArg_Parse(args, "s#", &key.dptr, &key.dsize))
if (!PyArg_ParseTuple(args, "s#:has_key", &key.dptr, &key.dsize))
return NULL;
check_dbmobject_open(dp);
return PyInt_FromLong((long) gdbm_exists(dp->di_dbm, key));
@ -293,14 +277,12 @@ hash values, and won't be sorted by the key values. This method\n\
returns the starting key.";
static PyObject *
dbm_firstkey(dp, args)
register dbmobject *dp;
PyObject *args;
dbm_firstkey(register dbmobject *dp, PyObject *args)
{
register PyObject *v;
datum key;
if (!PyArg_NoArgs(args))
if (!PyArg_ParseTuple(args, ":firstkey"))
return NULL;
check_dbmobject_open(dp);
key = gdbm_firstkey(dp->di_dbm);
@ -308,7 +290,8 @@ PyObject *args;
v = PyString_FromStringAndSize(key.dptr, key.dsize);
free(key.dptr);
return v;
} else {
}
else {
Py_INCREF(Py_None);
return Py_None;
}
@ -326,14 +309,12 @@ to create a list in memory that contains them all:\n\
k = db.nextkey(k)";
static PyObject *
dbm_nextkey(dp, args)
register dbmobject *dp;
PyObject *args;
dbm_nextkey(register dbmobject *dp, PyObject *args)
{
register PyObject *v;
datum key, nextkey;
if (!PyArg_Parse(args, "s#", &key.dptr, &key.dsize))
if (!PyArg_ParseTuple(args, "s#:nextkey", &key.dptr, &key.dsize))
return NULL;
check_dbmobject_open(dp);
nextkey = gdbm_nextkey(dp->di_dbm, key);
@ -341,7 +322,8 @@ PyObject *args;
v = PyString_FromStringAndSize(nextkey.dptr, nextkey.dsize);
free(nextkey.dptr);
return v;
} else {
}
else {
Py_INCREF(Py_None);
return Py_None;
}
@ -356,11 +338,9 @@ by using this reorganization; otherwise, deleted file space will be\n\
kept and reused as new (key,value) pairs are added.";
static PyObject *
dbm_reorganize(dp, args)
register dbmobject *dp;
PyObject *args;
dbm_reorganize(register dbmobject *dp, PyObject *args)
{
if (!PyArg_NoArgs(args))
if (!PyArg_ParseTuple(args, ":reorganize"))
return NULL;
check_dbmobject_open(dp);
errno = 0;
@ -381,11 +361,9 @@ When the database has been opened in fast mode, this method forces\n\
any unwritten data to be written to the disk.";
static PyObject *
dbm_sync(dp, args)
register dbmobject *dp;
PyObject *args;
dbm_sync(register dbmobject *dp, PyObject *args)
{
if (!PyArg_NoArgs(args))
if (!PyArg_ParseTuple(args, ":sync"))
return NULL;
check_dbmobject_open(dp);
gdbm_sync(dp->di_dbm);
@ -394,20 +372,18 @@ dbm_sync(dp, args)
}
static PyMethodDef dbm_methods[] = {
{"close", (PyCFunction)dbm_close, 0, dbm_close__doc__},
{"keys", (PyCFunction)dbm_keys, 0, dbm_keys__doc__},
{"has_key", (PyCFunction)dbm_has_key, 0, dbm_has_key__doc__},
{"firstkey", (PyCFunction)dbm_firstkey, 0, dbm_firstkey__doc__},
{"nextkey", (PyCFunction)dbm_nextkey, 0, dbm_nextkey__doc__},
{"reorganize", (PyCFunction)dbm_reorganize, 0, dbm_reorganize__doc__},
{"sync", (PyCFunction)dbm_sync, 0, dbm_sync__doc__},
{"close", (PyCFunction)dbm_close, METH_VARARGS, dbm_close__doc__},
{"keys", (PyCFunction)dbm_keys, METH_VARARGS, dbm_keys__doc__},
{"has_key", (PyCFunction)dbm_has_key, METH_VARARGS, dbm_has_key__doc__},
{"firstkey", (PyCFunction)dbm_firstkey,METH_VARARGS, dbm_firstkey__doc__},
{"nextkey", (PyCFunction)dbm_nextkey, METH_VARARGS, dbm_nextkey__doc__},
{"reorganize",(PyCFunction)dbm_reorganize,METH_VARARGS, dbm_reorganize__doc__},
{"sync", (PyCFunction)dbm_sync, METH_VARARGS, dbm_sync__doc__},
{NULL, NULL} /* sentinel */
};
static PyObject *
dbm_getattr(dp, name)
dbmobject *dp;
char *name;
dbm_getattr(dbmobject *dp, char *name)
{
return Py_FindMethod(dbm_methods, (PyObject *)dp, name);
}
@ -460,16 +436,14 @@ The optional mode argument is the Unix mode of the file, used only\n\
when the database has to be created. It defaults to octal 0666. ";
static PyObject *
dbmopen(self, args)
PyObject *self;
PyObject *args;
dbmopen(PyObject *self, PyObject *args)
{
char *name;
char *flags = "r ";
int iflags;
int mode = 0666;
if ( !PyArg_ParseTuple(args, "s|si:open", &name, &flags, &mode) )
if (!PyArg_ParseTuple(args, "s|si:open", &name, &flags, &mode))
return NULL;
switch (flags[0]) {
case 'r':
@ -495,7 +469,7 @@ PyObject *args;
}
static PyMethodDef dbmmodule_methods[] = {
{ "open", (PyCFunction)dbmopen, 1, dbmopen__doc__},
{ "open", (PyCFunction)dbmopen, METH_VARARGS, dbmopen__doc__},
{ 0, 0 },
};