normalized flag arg and made flag, mode default args; minor cosmetics

This commit is contained in:
Guido van Rossum 1995-07-07 22:37:11 +00:00
parent 55ea8a7b5b
commit 807b7bec06
2 changed files with 62 additions and 29 deletions

View File

@ -60,7 +60,7 @@ newdbmobject(file, flags, mode)
if ( (dp->di_dbm = dbm_open(file, flags, mode)) == 0 ) { if ( (dp->di_dbm = dbm_open(file, flags, mode)) == 0 ) {
err_errno(DbmError); err_errno(DbmError);
DECREF(dp); DECREF(dp);
return 0; return NULL;
} }
return (object *)dp; return (object *)dp;
} }
@ -107,12 +107,12 @@ dbm_subscript(dp, key)
drec = dbm_fetch(dp->di_dbm, krec); drec = dbm_fetch(dp->di_dbm, krec);
if ( drec.dptr == 0 ) { if ( drec.dptr == 0 ) {
err_setstr(KeyError, GETSTRINGVALUE((stringobject *)key)); err_setstr(KeyError, GETSTRINGVALUE((stringobject *)key));
return 0; return NULL;
} }
if ( dbm_error(dp->di_dbm) ) { if ( dbm_error(dp->di_dbm) ) {
dbm_clearerr(dp->di_dbm); dbm_clearerr(dp->di_dbm);
err_setstr(DbmError, ""); err_setstr(DbmError, "");
return 0; return NULL;
} }
return newsizedstringobject(drec.dptr, drec.dsize); return newsizedstringobject(drec.dptr, drec.dsize);
} }
@ -161,6 +161,21 @@ static mapping_methods dbm_as_mapping = {
(objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/ (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/
}; };
static object *
dbm__close(dp, args)
register dbmobject *dp;
object *args;
{
if ( !getnoarg(args) )
return NULL;
if ( dp->di_dbm )
dbm_close(dp->di_dbm);
dp->di_dbm = NULL;
DEL(dp);
INCREF(None);
return None;
}
static object * static object *
dbm_keys(dp, args) dbm_keys(dp, args)
register dbmobject *dp; register dbmobject *dp;
@ -170,10 +185,6 @@ dbm_keys(dp, args)
datum key; datum key;
int err; int err;
if (dp == NULL || !is_dbmobject(dp)) {
err_badcall();
return NULL;
}
if (!getnoarg(args)) if (!getnoarg(args))
return NULL; return NULL;
v = newlistobject(0); v = newlistobject(0);
@ -196,7 +207,6 @@ dbm_keys(dp, args)
return v; return v;
} }
static object * static object *
dbm_has_key(dp, args) dbm_has_key(dp, args)
register dbmobject *dp; register dbmobject *dp;
@ -211,6 +221,7 @@ dbm_has_key(dp, args)
} }
static struct methodlist dbm_methods[] = { static struct methodlist dbm_methods[] = {
{"close", (method)dbm__close},
{"keys", (method)dbm_keys}, {"keys", (method)dbm_keys},
{"has_key", (method)dbm_has_key}, {"has_key", (method)dbm_has_key},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
@ -227,7 +238,7 @@ dbm_getattr(dp, name)
static typeobject Dbmtype = { static typeobject Dbmtype = {
OB_HEAD_INIT(&Typetype) OB_HEAD_INIT(&Typetype)
0, 0,
"Dbm_dictionary", "dbm",
sizeof(dbmobject), sizeof(dbmobject),
0, 0,
(destructor)dbm_dealloc, /*tp_dealloc*/ (destructor)dbm_dealloc, /*tp_dealloc*/
@ -248,27 +259,33 @@ dbmopen(self, args)
object *self; object *self;
object *args; object *args;
{ {
char *name, *flags; char *name;
int iflags, mode; char *flags = "r";
int iflags;
int mode = 0666;
if ( !getargs(args, "(ssi)", &name, &flags, &mode) ) if ( !newgetargs(args, "s|si", &name, &flags, &mode) )
return 0; return NULL;
if ( strcmp(flags, "r") == 0 ) if ( strcmp(flags, "r") == 0 )
iflags = O_RDONLY; iflags = O_RDONLY;
else if ( strcmp(flags, "w") == 0 ) else if ( strcmp(flags, "w") == 0 )
iflags = O_WRONLY|O_CREAT; iflags = O_RDWR;
else if ( strcmp(flags, "rw") == 0 ) else if ( strcmp(flags, "rw") == 0 ) /* B/W compat */
iflags = O_RDWR|O_CREAT; iflags = O_RDWR|O_CREAT;
else if ( strcmp(flags, "c") == 0 )
iflags = O_RDWR|O_CREAT;
else if ( strcmp(flags, "n") == 0 )
iflags = O_RDWR|O_CREAT|O_TRUNC;
else { else {
err_setstr(DbmError, err_setstr(DbmError,
"Flags should be one of 'r', 'w' or 'rw'"); "Flags should be one of 'r', 'w', 'c' or 'n'");
return 0; return NULL;
} }
return newdbmobject(name, iflags, mode); return newdbmobject(name, iflags, mode);
} }
static struct methodlist dbmmodule_methods[] = { static struct methodlist dbmmodule_methods[] = {
{ "open", (method)dbmopen }, { "open", (method)dbmopen, 1 },
{ 0, 0 }, { 0, 0 },
}; };
@ -282,5 +299,3 @@ initdbm() {
if ( DbmError == NULL || dictinsert(d, "error", DbmError) ) if ( DbmError == NULL || dictinsert(d, "error", DbmError) )
fatal("can't define dbm.error"); fatal("can't define dbm.error");
} }

View File

@ -68,7 +68,7 @@ newdbmobject(file, flags, mode)
else else
err_setstr(DbmError, (char *) gdbm_strerror(gdbm_errno)); err_setstr(DbmError, (char *) gdbm_strerror(gdbm_errno));
DECREF(dp); DECREF(dp);
return 0; return NULL;
} }
return (object *)dp; return (object *)dp;
} }
@ -119,7 +119,7 @@ dbm_subscript(dp, key)
drec = gdbm_fetch(dp->di_dbm, krec); drec = gdbm_fetch(dp->di_dbm, krec);
if ( drec.dptr == 0 ) { if ( drec.dptr == 0 ) {
err_setstr(KeyError, GETSTRINGVALUE((stringobject *)key)); err_setstr(KeyError, GETSTRINGVALUE((stringobject *)key));
return 0; return NULL;
} }
v = newsizedstringobject(drec.dptr, drec.dsize); v = newsizedstringobject(drec.dptr, drec.dsize);
free(drec.dptr); free(drec.dptr);
@ -167,6 +167,21 @@ static mapping_methods dbm_as_mapping = {
(objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/ (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/
}; };
static object *
dbm_close(dp, args)
register dbmobject *dp;
object *args;
{
if ( !getnoarg(args) )
return NULL;
if ( dp->di_dbm )
gdbm_close(dp->di_dbm);
dp->di_dbm = NULL;
DEL(dp);
INCREF(None);
return None;
}
static object * static object *
dbm_keys(dp, args) dbm_keys(dp, args)
register dbmobject *dp; register dbmobject *dp;
@ -271,6 +286,7 @@ dbm_reorganize(dp, args)
} }
static struct methodlist dbm_methods[] = { static struct methodlist dbm_methods[] = {
{"close", (method)dbm_close},
{"keys", (method)dbm_keys}, {"keys", (method)dbm_keys},
{"has_key", (method)dbm_has_key}, {"has_key", (method)dbm_has_key},
{"firstkey", (method)dbm_firstkey}, {"firstkey", (method)dbm_firstkey},
@ -290,7 +306,7 @@ dbm_getattr(dp, name)
static typeobject Dbmtype = { static typeobject Dbmtype = {
OB_HEAD_INIT(&Typetype) OB_HEAD_INIT(&Typetype)
0, 0,
"Gdbm_dictionary", "gdbm",
sizeof(dbmobject), sizeof(dbmobject),
0, 0,
(destructor)dbm_dealloc, /*tp_dealloc*/ (destructor)dbm_dealloc, /*tp_dealloc*/
@ -311,12 +327,14 @@ dbmopen(self, args)
object *self; object *self;
object *args; object *args;
{ {
char *name, *flags; char *name;
int iflags, mode; char *flags = "r";
int iflags;
int mode = 0666;
/* XXXX add other flags */ /* XXXX add other flags */
if ( !getargs(args, "(ssi)", &name, &flags, &mode) ) if ( !newgetargs(args, "s|si", &name, &flags, &mode) )
return 0; return NULL;
if ( strcmp(flags, "r") == 0 ) if ( strcmp(flags, "r") == 0 )
iflags = GDBM_READER; iflags = GDBM_READER;
else if ( strcmp(flags, "w") == 0 ) else if ( strcmp(flags, "w") == 0 )
@ -328,13 +346,13 @@ dbmopen(self, args)
else { else {
err_setstr(DbmError, err_setstr(DbmError,
"Flags should be one of 'r', 'w', 'c' or 'n'"); "Flags should be one of 'r', 'w', 'c' or 'n'");
return 0; return NULL;
} }
return newdbmobject(name, iflags, mode); return newdbmobject(name, iflags, mode);
} }
static struct methodlist dbmmodule_methods[] = { static struct methodlist dbmmodule_methods[] = {
{ "open", (method)dbmopen }, { "open", (method)dbmopen, 1 },
{ 0, 0 }, { 0, 0 },
}; };