1994-08-08 05:06:37 -03:00
|
|
|
|
|
|
|
/* DBM module using dictionary interface */
|
1998-03-03 18:02:24 -04:00
|
|
|
/* Author: Anthony Baxter, after dbmmodule.c */
|
|
|
|
/* Doc strings: Mitch Chapman */
|
1994-08-08 05:06:37 -03:00
|
|
|
|
|
|
|
|
1996-12-17 15:55:33 -04:00
|
|
|
#include "Python.h"
|
1994-08-08 05:06:37 -03:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include "gdbm.h"
|
|
|
|
|
2001-02-28 12:44:18 -04:00
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
1998-10-03 02:13:27 -03:00
|
|
|
#include "gdbmerrno.h"
|
|
|
|
extern const char * gdbm_strerror(gdbm_error);
|
|
|
|
#endif
|
|
|
|
|
2015-04-17 15:05:18 -03:00
|
|
|
/*[clinic input]
|
|
|
|
module _gdbm
|
|
|
|
class _gdbm.gdbm "dbmobject *" "&Dbmtype"
|
|
|
|
[clinic start generated code]*/
|
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=113927c6170729b2]*/
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(gdbmmodule__doc__,
|
|
|
|
"This module provides an interface to the GNU DBM (GDBM) library.\n\
|
1998-03-03 18:02:24 -04:00
|
|
|
\n\
|
|
|
|
This module is quite similar to the dbm module, but uses GDBM instead to\n\
|
2015-04-17 15:05:18 -03:00
|
|
|
provide some additional functionality. Please note that the file formats\n\
|
|
|
|
created by GDBM and dbm are incompatible.\n\
|
1998-03-03 18:02:24 -04:00
|
|
|
\n\
|
|
|
|
GDBM objects behave like mappings (dictionaries), except that keys and\n\
|
2015-04-17 15:05:18 -03:00
|
|
|
values are always immutable bytes-like objects or strings. Printing\n\
|
|
|
|
a GDBM object doesn't print the keys and values, and the items() and\n\
|
|
|
|
values() methods are not supported.");
|
1998-03-03 18:02:24 -04:00
|
|
|
|
1994-08-08 05:06:37 -03:00
|
|
|
typedef struct {
|
2000-07-08 02:00:07 -03:00
|
|
|
PyObject_HEAD
|
2010-05-09 12:52:27 -03:00
|
|
|
int di_size; /* -1 means recompute */
|
2000-07-08 02:00:07 -03:00
|
|
|
GDBM_FILE di_dbm;
|
1994-08-08 05:06:37 -03:00
|
|
|
} dbmobject;
|
|
|
|
|
2002-07-17 13:30:39 -03:00
|
|
|
static PyTypeObject Dbmtype;
|
1994-08-08 05:06:37 -03:00
|
|
|
|
2015-04-17 15:05:18 -03:00
|
|
|
#include "clinic/_gdbmmodule.c.h"
|
|
|
|
|
2007-12-18 22:45:37 -04:00
|
|
|
#define is_dbmobject(v) (Py_TYPE(v) == &Dbmtype)
|
1997-07-17 19:56:01 -03:00
|
|
|
#define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \
|
2000-07-08 02:00:07 -03:00
|
|
|
{ PyErr_SetString(DbmError, "GDBM object has already been closed"); \
|
|
|
|
return NULL; }
|
1997-07-17 19:56:01 -03:00
|
|
|
|
|
|
|
|
1994-08-08 05:06:37 -03:00
|
|
|
|
1996-12-17 15:55:33 -04:00
|
|
|
static PyObject *DbmError;
|
1994-08-08 05:06:37 -03:00
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(gdbm_object__doc__,
|
|
|
|
"This object represents a GDBM database.\n\
|
1998-03-03 18:02:24 -04:00
|
|
|
GDBM objects behave like mappings (dictionaries), except that keys and\n\
|
2015-04-17 15:05:18 -03:00
|
|
|
values are always immutable bytes-like objects or strings. Printing\n\
|
|
|
|
a GDBM object doesn't print the keys and values, and the items() and\n\
|
|
|
|
values() methods are not supported.\n\
|
1998-03-03 18:02:24 -04:00
|
|
|
\n\
|
|
|
|
GDBM objects also support additional operations such as firstkey,\n\
|
2002-06-13 17:33:02 -03:00
|
|
|
nextkey, reorganize, and sync.");
|
1998-03-03 18:02:24 -04:00
|
|
|
|
1996-12-17 15:55:33 -04:00
|
|
|
static PyObject *
|
2015-04-17 15:05:18 -03:00
|
|
|
newdbmobject(const char *file, int flags, int mode)
|
1994-08-08 05:06:37 -03:00
|
|
|
{
|
2000-07-08 02:00:07 -03:00
|
|
|
dbmobject *dp;
|
|
|
|
|
|
|
|
dp = PyObject_New(dbmobject, &Dbmtype);
|
|
|
|
if (dp == NULL)
|
|
|
|
return NULL;
|
|
|
|
dp->di_size = -1;
|
|
|
|
errno = 0;
|
2015-04-17 15:05:18 -03:00
|
|
|
if ((dp->di_dbm = gdbm_open((char *)file, 0, flags, mode, NULL)) == 0) {
|
2000-07-08 02:00:07 -03:00
|
|
|
if (errno != 0)
|
2018-09-27 16:54:34 -03:00
|
|
|
PyErr_SetFromErrnoWithFilename(DbmError, file);
|
2000-07-08 02:00:07 -03:00
|
|
|
else
|
|
|
|
PyErr_SetString(DbmError, gdbm_strerror(gdbm_errno));
|
|
|
|
Py_DECREF(dp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return (PyObject *)dp;
|
1994-08-08 05:06:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
static void
|
2013-08-13 15:18:52 -03:00
|
|
|
dbm_dealloc(dbmobject *dp)
|
1994-08-08 05:06:37 -03:00
|
|
|
{
|
2000-07-08 02:00:07 -03:00
|
|
|
if (dp->di_dbm)
|
|
|
|
gdbm_close(dp->di_dbm);
|
|
|
|
PyObject_Del(dp);
|
1994-08-08 05:06:37 -03:00
|
|
|
}
|
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
static Py_ssize_t
|
2000-07-08 02:00:07 -03:00
|
|
|
dbm_length(dbmobject *dp)
|
1994-08-08 05:06:37 -03:00
|
|
|
{
|
2000-07-08 02:00:07 -03:00
|
|
|
if (dp->di_dbm == NULL) {
|
2007-05-23 17:51:02 -03:00
|
|
|
PyErr_SetString(DbmError, "GDBM object has already been closed");
|
|
|
|
return -1;
|
2000-07-08 02:00:07 -03:00
|
|
|
}
|
|
|
|
if (dp->di_size < 0) {
|
|
|
|
datum key,okey;
|
|
|
|
int size;
|
|
|
|
okey.dsize=0;
|
2006-03-01 18:54:36 -04:00
|
|
|
okey.dptr=NULL;
|
2000-07-08 02:00:07 -03:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
for (key=gdbm_firstkey(dp->di_dbm); key.dptr;
|
|
|
|
key = gdbm_nextkey(dp->di_dbm,okey)) {
|
|
|
|
size++;
|
|
|
|
if(okey.dsize) free(okey.dptr);
|
|
|
|
okey=key;
|
1997-07-17 19:56:01 -03:00
|
|
|
}
|
2000-07-08 02:00:07 -03:00
|
|
|
dp->di_size = size;
|
|
|
|
}
|
|
|
|
return dp->di_size;
|
1994-08-08 05:06:37 -03:00
|
|
|
}
|
|
|
|
|
1996-12-17 15:55:33 -04:00
|
|
|
static PyObject *
|
2013-08-13 15:18:52 -03:00
|
|
|
dbm_subscript(dbmobject *dp, PyObject *key)
|
1994-08-08 05:06:37 -03:00
|
|
|
{
|
2000-07-08 02:00:07 -03:00
|
|
|
PyObject *v;
|
|
|
|
datum drec, krec;
|
|
|
|
|
|
|
|
if (!PyArg_Parse(key, "s#", &krec.dptr, &krec.dsize) )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (dp->di_dbm == NULL) {
|
|
|
|
PyErr_SetString(DbmError,
|
|
|
|
"GDBM object has already been closed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
drec = gdbm_fetch(dp->di_dbm, krec);
|
|
|
|
if (drec.dptr == 0) {
|
2007-05-23 17:51:02 -03:00
|
|
|
PyErr_SetObject(PyExc_KeyError, key);
|
2000-07-08 02:00:07 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-05-26 10:28:38 -03:00
|
|
|
v = PyBytes_FromStringAndSize(drec.dptr, drec.dsize);
|
2000-07-08 02:00:07 -03:00
|
|
|
free(drec.dptr);
|
|
|
|
return v;
|
1994-08-08 05:06:37 -03:00
|
|
|
}
|
|
|
|
|
2015-04-17 15:05:18 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_gdbm.gdbm.get
|
|
|
|
|
|
|
|
key: object
|
|
|
|
default: object = None
|
|
|
|
/
|
|
|
|
|
|
|
|
Get the value for key, or default if not present.
|
|
|
|
[clinic start generated code]*/
|
2010-12-04 05:14:36 -04:00
|
|
|
|
|
|
|
static PyObject *
|
2015-04-17 15:05:18 -03:00
|
|
|
_gdbm_gdbm_get_impl(dbmobject *self, PyObject *key, PyObject *default_value)
|
|
|
|
/*[clinic end generated code: output=19b7c585ad4f554a input=a9c20423f34c17b6]*/
|
2010-12-04 05:14:36 -04:00
|
|
|
{
|
2015-04-17 15:05:18 -03:00
|
|
|
PyObject *res;
|
2010-12-04 05:14:36 -04:00
|
|
|
|
2015-04-17 15:05:18 -03:00
|
|
|
res = dbm_subscript(self, key);
|
2010-12-04 05:14:36 -04:00
|
|
|
if (res == NULL && PyErr_ExceptionMatches(PyExc_KeyError)) {
|
|
|
|
PyErr_Clear();
|
2015-04-17 15:05:18 -03:00
|
|
|
Py_INCREF(default_value);
|
|
|
|
return default_value;
|
2010-12-04 05:14:36 -04:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1994-08-08 05:06:37 -03:00
|
|
|
static int
|
2000-07-08 02:00:07 -03:00
|
|
|
dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w)
|
1994-08-08 05:06:37 -03:00
|
|
|
{
|
2000-07-08 02:00:07 -03:00
|
|
|
datum krec, drec;
|
|
|
|
|
|
|
|
if (!PyArg_Parse(v, "s#", &krec.dptr, &krec.dsize) ) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
2008-11-25 15:19:17 -04:00
|
|
|
"gdbm mappings have bytes or string indices only");
|
2000-07-08 02:00:07 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (dp->di_dbm == NULL) {
|
|
|
|
PyErr_SetString(DbmError,
|
2007-05-23 17:51:02 -03:00
|
|
|
"GDBM object has already been closed");
|
|
|
|
return -1;
|
2000-07-08 02:00:07 -03:00
|
|
|
}
|
|
|
|
dp->di_size = -1;
|
|
|
|
if (w == NULL) {
|
|
|
|
if (gdbm_delete(dp->di_dbm, krec) < 0) {
|
2007-05-23 17:51:02 -03:00
|
|
|
PyErr_SetObject(PyExc_KeyError, v);
|
2000-07-08 02:00:07 -03:00
|
|
|
return -1;
|
1997-07-17 19:56:01 -03:00
|
|
|
}
|
2000-07-08 02:00:07 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!PyArg_Parse(w, "s#", &drec.dptr, &drec.dsize)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
2008-11-25 15:19:17 -04:00
|
|
|
"gdbm mappings have byte or string elements only");
|
2000-07-08 02:00:07 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
if (gdbm_store(dp->di_dbm, krec, drec, GDBM_REPLACE) < 0) {
|
|
|
|
if (errno != 0)
|
|
|
|
PyErr_SetFromErrno(DbmError);
|
|
|
|
else
|
|
|
|
PyErr_SetString(DbmError,
|
|
|
|
gdbm_strerror(gdbm_errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
1994-08-08 05:06:37 -03:00
|
|
|
}
|
|
|
|
|
2015-04-17 15:05:18 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_gdbm.gdbm.setdefault
|
|
|
|
|
|
|
|
key: object
|
|
|
|
default: object = None
|
|
|
|
/
|
|
|
|
|
|
|
|
Get value for key, or set it to default and return default if not present.
|
|
|
|
[clinic start generated code]*/
|
2010-12-04 05:14:36 -04:00
|
|
|
|
|
|
|
static PyObject *
|
2015-04-17 15:05:18 -03:00
|
|
|
_gdbm_gdbm_setdefault_impl(dbmobject *self, PyObject *key,
|
|
|
|
PyObject *default_value)
|
|
|
|
/*[clinic end generated code: output=88760ee520329012 input=0db46b69e9680171]*/
|
2010-12-04 05:14:36 -04:00
|
|
|
{
|
2015-04-17 15:05:18 -03:00
|
|
|
PyObject *res;
|
2010-12-04 05:14:36 -04:00
|
|
|
|
2015-04-17 15:05:18 -03:00
|
|
|
res = dbm_subscript(self, key);
|
2010-12-04 05:14:36 -04:00
|
|
|
if (res == NULL && PyErr_ExceptionMatches(PyExc_KeyError)) {
|
|
|
|
PyErr_Clear();
|
2015-04-17 15:05:18 -03:00
|
|
|
if (dbm_ass_sub(self, key, default_value) < 0)
|
2010-12-04 05:14:36 -04:00
|
|
|
return NULL;
|
2015-04-17 15:05:18 -03:00
|
|
|
return dbm_subscript(self, key);
|
2010-12-04 05:14:36 -04:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1996-12-17 15:55:33 -04:00
|
|
|
static PyMappingMethods dbm_as_mapping = {
|
2010-05-09 12:52:27 -03:00
|
|
|
(lenfunc)dbm_length, /*mp_length*/
|
2000-07-08 02:00:07 -03:00
|
|
|
(binaryfunc)dbm_subscript, /*mp_subscript*/
|
|
|
|
(objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/
|
1994-08-08 05:06:37 -03:00
|
|
|
};
|
|
|
|
|
2015-04-17 15:05:18 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_gdbm.gdbm.close
|
|
|
|
|
|
|
|
Close the database.
|
|
|
|
[clinic start generated code]*/
|
1998-03-03 18:02:24 -04:00
|
|
|
|
1996-12-17 15:55:33 -04:00
|
|
|
static PyObject *
|
2015-04-17 15:05:18 -03:00
|
|
|
_gdbm_gdbm_close_impl(dbmobject *self)
|
|
|
|
/*[clinic end generated code: output=23512a594598b563 input=0a203447379b45fd]*/
|
1995-07-07 19:37:11 -03:00
|
|
|
{
|
2015-04-17 15:05:18 -03:00
|
|
|
if (self->di_dbm)
|
|
|
|
gdbm_close(self->di_dbm);
|
|
|
|
self->di_dbm = NULL;
|
2017-01-23 03:47:21 -04:00
|
|
|
Py_RETURN_NONE;
|
1995-07-07 19:37:11 -03:00
|
|
|
}
|
|
|
|
|
2007-05-23 17:51:02 -03:00
|
|
|
/* XXX Should return a set or a set view */
|
2015-04-17 15:05:18 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_gdbm.gdbm.keys
|
|
|
|
|
|
|
|
Get a list of all keys in the database.
|
|
|
|
[clinic start generated code]*/
|
1998-03-03 18:02:24 -04:00
|
|
|
|
1996-12-17 15:55:33 -04:00
|
|
|
static PyObject *
|
2015-04-17 15:05:18 -03:00
|
|
|
_gdbm_gdbm_keys_impl(dbmobject *self)
|
|
|
|
/*[clinic end generated code: output=cb4b1776c3645dcc input=1832ee0a3132cfaf]*/
|
1994-08-08 05:06:37 -03:00
|
|
|
{
|
2013-08-13 15:18:52 -03:00
|
|
|
PyObject *v, *item;
|
2000-07-08 02:00:07 -03:00
|
|
|
datum key, nextkey;
|
|
|
|
int err;
|
|
|
|
|
2015-04-17 15:05:18 -03:00
|
|
|
if (self == NULL || !is_dbmobject(self)) {
|
2000-07-08 02:00:07 -03:00
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-04-17 15:05:18 -03:00
|
|
|
check_dbmobject_open(self);
|
2000-07-08 02:00:07 -03:00
|
|
|
|
|
|
|
v = PyList_New(0);
|
|
|
|
if (v == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2015-04-17 15:05:18 -03:00
|
|
|
key = gdbm_firstkey(self->di_dbm);
|
2000-07-08 02:00:07 -03:00
|
|
|
while (key.dptr) {
|
2008-05-26 10:28:38 -03:00
|
|
|
item = PyBytes_FromStringAndSize(key.dptr, key.dsize);
|
2000-07-08 02:00:07 -03:00
|
|
|
if (item == NULL) {
|
|
|
|
free(key.dptr);
|
|
|
|
Py_DECREF(v);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
err = PyList_Append(v, item);
|
|
|
|
Py_DECREF(item);
|
|
|
|
if (err != 0) {
|
|
|
|
free(key.dptr);
|
|
|
|
Py_DECREF(v);
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-04-17 15:05:18 -03:00
|
|
|
nextkey = gdbm_nextkey(self->di_dbm, key);
|
2000-07-08 02:00:07 -03:00
|
|
|
free(key.dptr);
|
|
|
|
key = nextkey;
|
|
|
|
}
|
|
|
|
return v;
|
1994-08-08 05:06:37 -03:00
|
|
|
}
|
|
|
|
|
2006-08-19 20:18:48 -03:00
|
|
|
static int
|
|
|
|
dbm_contains(PyObject *self, PyObject *arg)
|
1994-08-08 05:06:37 -03:00
|
|
|
{
|
2006-08-19 20:18:48 -03:00
|
|
|
dbmobject *dp = (dbmobject *)self;
|
2000-07-08 02:00:07 -03:00
|
|
|
datum key;
|
2013-10-24 18:06:52 -03:00
|
|
|
Py_ssize_t size;
|
2000-07-08 02:00:07 -03:00
|
|
|
|
2006-08-19 20:18:48 -03:00
|
|
|
if ((dp)->di_dbm == NULL) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_SetString(DbmError,
|
|
|
|
"GDBM object has already been closed");
|
|
|
|
return -1;
|
2006-08-19 20:18:48 -03:00
|
|
|
}
|
2013-10-24 18:06:52 -03:00
|
|
|
if (PyUnicode_Check(arg)) {
|
2017-01-22 17:07:07 -04:00
|
|
|
key.dptr = (char *)PyUnicode_AsUTF8AndSize(arg, &size);
|
2013-10-24 18:06:52 -03:00
|
|
|
key.dsize = size;
|
|
|
|
if (key.dptr == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (!PyBytes_Check(arg)) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
2013-10-24 18:06:52 -03:00
|
|
|
"gdbm key must be bytes or string, not %.100s",
|
2010-05-09 12:52:27 -03:00
|
|
|
arg->ob_type->tp_name);
|
|
|
|
return -1;
|
2006-08-19 20:18:48 -03:00
|
|
|
}
|
2013-10-24 18:06:52 -03:00
|
|
|
else {
|
|
|
|
key.dptr = PyBytes_AS_STRING(arg);
|
|
|
|
key.dsize = PyBytes_GET_SIZE(arg);
|
|
|
|
}
|
2006-08-19 20:18:48 -03:00
|
|
|
return gdbm_exists(dp->di_dbm, key);
|
1994-08-08 05:06:37 -03:00
|
|
|
}
|
|
|
|
|
2006-08-19 20:18:48 -03:00
|
|
|
static PySequenceMethods dbm_as_sequence = {
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* sq_length */
|
|
|
|
0, /* sq_concat */
|
|
|
|
0, /* sq_repeat */
|
|
|
|
0, /* sq_item */
|
|
|
|
0, /* sq_slice */
|
|
|
|
0, /* sq_ass_item */
|
|
|
|
0, /* sq_ass_slice */
|
|
|
|
dbm_contains, /* sq_contains */
|
|
|
|
0, /* sq_inplace_concat */
|
|
|
|
0, /* sq_inplace_repeat */
|
2006-08-19 20:18:48 -03:00
|
|
|
};
|
|
|
|
|
2015-04-17 15:05:18 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_gdbm.gdbm.firstkey
|
|
|
|
|
|
|
|
Return the starting key for the traversal.
|
|
|
|
|
|
|
|
It's possible to loop over every key in the database using this method
|
|
|
|
and the nextkey() method. The traversal is ordered by GDBM's internal
|
|
|
|
hash values, and won't be sorted by the key values.
|
|
|
|
[clinic start generated code]*/
|
1998-03-03 18:02:24 -04:00
|
|
|
|
1996-12-17 15:55:33 -04:00
|
|
|
static PyObject *
|
2015-04-17 15:05:18 -03:00
|
|
|
_gdbm_gdbm_firstkey_impl(dbmobject *self)
|
|
|
|
/*[clinic end generated code: output=9ff85628d84b65d2 input=0dbd6a335d69bba0]*/
|
1995-03-16 12:07:34 -04:00
|
|
|
{
|
2013-08-13 15:18:52 -03:00
|
|
|
PyObject *v;
|
2000-07-08 02:00:07 -03:00
|
|
|
datum key;
|
|
|
|
|
2015-04-17 15:05:18 -03:00
|
|
|
check_dbmobject_open(self);
|
|
|
|
key = gdbm_firstkey(self->di_dbm);
|
2000-07-08 02:00:07 -03:00
|
|
|
if (key.dptr) {
|
2008-05-26 10:28:38 -03:00
|
|
|
v = PyBytes_FromStringAndSize(key.dptr, key.dsize);
|
2000-07-08 02:00:07 -03:00
|
|
|
free(key.dptr);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
else {
|
2017-01-23 03:47:21 -04:00
|
|
|
Py_RETURN_NONE;
|
2000-07-08 02:00:07 -03:00
|
|
|
}
|
1995-03-16 12:07:34 -04:00
|
|
|
}
|
|
|
|
|
2015-04-17 15:05:18 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_gdbm.gdbm.nextkey
|
|
|
|
|
2015-05-08 03:30:09 -03:00
|
|
|
key: str(accept={str, robuffer}, zeroes=True)
|
2015-04-17 15:05:18 -03:00
|
|
|
/
|
|
|
|
|
|
|
|
Returns the key that follows key in the traversal.
|
|
|
|
|
|
|
|
The following code prints every key in the database db, without having
|
|
|
|
to create a list in memory that contains them all:
|
|
|
|
|
|
|
|
k = db.firstkey()
|
|
|
|
while k != None:
|
|
|
|
print(k)
|
|
|
|
k = db.nextkey(k)
|
|
|
|
[clinic start generated code]*/
|
1998-03-03 18:02:24 -04:00
|
|
|
|
1996-12-17 15:55:33 -04:00
|
|
|
static PyObject *
|
2015-04-17 15:05:18 -03:00
|
|
|
_gdbm_gdbm_nextkey_impl(dbmobject *self, const char *key,
|
|
|
|
Py_ssize_clean_t key_length)
|
2015-05-08 03:30:09 -03:00
|
|
|
/*[clinic end generated code: output=192ab892de6eb2f6 input=1f1606943614e36f]*/
|
1995-03-16 12:07:34 -04:00
|
|
|
{
|
2013-08-13 15:18:52 -03:00
|
|
|
PyObject *v;
|
2015-04-17 15:05:18 -03:00
|
|
|
datum dbm_key, nextkey;
|
2000-07-08 02:00:07 -03:00
|
|
|
|
2015-04-17 15:05:18 -03:00
|
|
|
dbm_key.dptr = (char *)key;
|
|
|
|
dbm_key.dsize = key_length;
|
|
|
|
check_dbmobject_open(self);
|
|
|
|
nextkey = gdbm_nextkey(self->di_dbm, dbm_key);
|
2000-07-08 02:00:07 -03:00
|
|
|
if (nextkey.dptr) {
|
2008-05-26 10:28:38 -03:00
|
|
|
v = PyBytes_FromStringAndSize(nextkey.dptr, nextkey.dsize);
|
2000-07-08 02:00:07 -03:00
|
|
|
free(nextkey.dptr);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
else {
|
2017-01-23 03:47:21 -04:00
|
|
|
Py_RETURN_NONE;
|
2000-07-08 02:00:07 -03:00
|
|
|
}
|
1995-03-16 12:07:34 -04:00
|
|
|
}
|
|
|
|
|
2015-04-17 15:05:18 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_gdbm.gdbm.reorganize
|
|
|
|
|
|
|
|
Reorganize the database.
|
|
|
|
|
|
|
|
If you have carried out a lot of deletions and would like to shrink
|
|
|
|
the space used by the GDBM file, this routine will reorganize the
|
|
|
|
database. GDBM will not shorten the length of a database file except
|
|
|
|
by using this reorganization; otherwise, deleted file space will be
|
|
|
|
kept and reused as new (key,value) pairs are added.
|
|
|
|
[clinic start generated code]*/
|
1998-03-03 18:02:24 -04:00
|
|
|
|
1996-12-17 15:55:33 -04:00
|
|
|
static PyObject *
|
2015-04-17 15:05:18 -03:00
|
|
|
_gdbm_gdbm_reorganize_impl(dbmobject *self)
|
|
|
|
/*[clinic end generated code: output=38d9624df92e961d input=f6bea85bcfd40dd2]*/
|
1995-03-16 12:07:34 -04:00
|
|
|
{
|
2015-04-17 15:05:18 -03:00
|
|
|
check_dbmobject_open(self);
|
2000-07-08 02:00:07 -03:00
|
|
|
errno = 0;
|
2015-04-17 15:05:18 -03:00
|
|
|
if (gdbm_reorganize(self->di_dbm) < 0) {
|
2000-07-08 02:00:07 -03:00
|
|
|
if (errno != 0)
|
|
|
|
PyErr_SetFromErrno(DbmError);
|
|
|
|
else
|
|
|
|
PyErr_SetString(DbmError, gdbm_strerror(gdbm_errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-01-23 03:47:21 -04:00
|
|
|
Py_RETURN_NONE;
|
1995-03-16 12:07:34 -04:00
|
|
|
}
|
|
|
|
|
2015-04-17 15:05:18 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_gdbm.gdbm.sync
|
|
|
|
|
|
|
|
Flush the database to the disk file.
|
|
|
|
|
|
|
|
When the database has been opened in fast mode, this method forces
|
|
|
|
any unwritten data to be written to the disk.
|
|
|
|
[clinic start generated code]*/
|
1998-03-03 18:02:24 -04:00
|
|
|
|
1997-03-25 13:39:56 -04:00
|
|
|
static PyObject *
|
2015-04-17 15:05:18 -03:00
|
|
|
_gdbm_gdbm_sync_impl(dbmobject *self)
|
|
|
|
/*[clinic end generated code: output=488b15f47028f125 input=2a47d2c9e153ab8a]*/
|
1997-03-25 13:39:56 -04:00
|
|
|
{
|
2015-04-17 15:05:18 -03:00
|
|
|
check_dbmobject_open(self);
|
|
|
|
gdbm_sync(self->di_dbm);
|
2017-01-23 03:47:21 -04:00
|
|
|
Py_RETURN_NONE;
|
1997-03-25 13:39:56 -04:00
|
|
|
}
|
|
|
|
|
2013-11-17 01:59:51 -04:00
|
|
|
static PyObject *
|
|
|
|
dbm__enter__(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
Py_INCREF(self);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
dbm__exit__(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
_Py_IDENTIFIER(close);
|
|
|
|
return _PyObject_CallMethodId(self, &PyId_close, NULL);
|
|
|
|
}
|
|
|
|
|
1996-12-17 15:55:33 -04:00
|
|
|
static PyMethodDef dbm_methods[] = {
|
2015-04-17 15:05:18 -03:00
|
|
|
_GDBM_GDBM_CLOSE_METHODDEF
|
|
|
|
_GDBM_GDBM_KEYS_METHODDEF
|
|
|
|
_GDBM_GDBM_FIRSTKEY_METHODDEF
|
|
|
|
_GDBM_GDBM_NEXTKEY_METHODDEF
|
|
|
|
_GDBM_GDBM_REORGANIZE_METHODDEF
|
|
|
|
_GDBM_GDBM_SYNC_METHODDEF
|
|
|
|
_GDBM_GDBM_GET_METHODDEF
|
|
|
|
_GDBM_GDBM_GET_METHODDEF
|
|
|
|
_GDBM_GDBM_SETDEFAULT_METHODDEF
|
2013-11-17 01:59:51 -04:00
|
|
|
{"__enter__", dbm__enter__, METH_NOARGS, NULL},
|
|
|
|
{"__exit__", dbm__exit__, METH_VARARGS, NULL},
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
1994-08-08 05:06:37 -03:00
|
|
|
};
|
|
|
|
|
1996-12-17 15:55:33 -04:00
|
|
|
static PyTypeObject Dbmtype = {
|
Merged revisions 56467-56482 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r56477 | martin.v.loewis | 2007-07-21 09:04:38 +0200 (Sa, 21 Jul 2007) | 11 lines
Merged revisions 56466-56476 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r56476 | martin.v.loewis | 2007-07-21 08:55:02 +0200 (Sa, 21 Jul 2007) | 4 lines
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and
PyVarObject_HEAD_INIT.
........
................
r56478 | martin.v.loewis | 2007-07-21 09:47:23 +0200 (Sa, 21 Jul 2007) | 2 lines
PEP 3123: Use proper C inheritance for PyObject.
................
r56479 | martin.v.loewis | 2007-07-21 10:06:55 +0200 (Sa, 21 Jul 2007) | 3 lines
Add longintrepr.h to Python.h, so that the compiler can
see that PyFalse is really some kind of PyObject*.
................
r56480 | martin.v.loewis | 2007-07-21 10:47:18 +0200 (Sa, 21 Jul 2007) | 2 lines
Qualify SHIFT, MASK, BASE.
................
r56482 | martin.v.loewis | 2007-07-21 19:10:57 +0200 (Sa, 21 Jul 2007) | 2 lines
Correctly refer to _ob_next.
................
2007-07-21 14:22:18 -03:00
|
|
|
PyVarObject_HEAD_INIT(0, 0)
|
2008-05-26 07:29:35 -03:00
|
|
|
"_gdbm.gdbm",
|
2000-07-08 02:00:07 -03:00
|
|
|
sizeof(dbmobject),
|
|
|
|
0,
|
|
|
|
(destructor)dbm_dealloc, /*tp_dealloc*/
|
|
|
|
0, /*tp_print*/
|
2008-07-02 19:38:47 -03:00
|
|
|
0, /*tp_getattr*/
|
2000-07-08 02:00:07 -03:00
|
|
|
0, /*tp_setattr*/
|
2009-02-02 16:36:42 -04:00
|
|
|
0, /*tp_reserved*/
|
2000-07-08 02:00:07 -03:00
|
|
|
0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
2006-08-19 20:18:48 -03:00
|
|
|
&dbm_as_sequence, /*tp_as_sequence*/
|
2000-07-08 02:00:07 -03:00
|
|
|
&dbm_as_mapping, /*tp_as_mapping*/
|
|
|
|
0, /*tp_hash*/
|
|
|
|
0, /*tp_call*/
|
|
|
|
0, /*tp_str*/
|
|
|
|
0, /*tp_getattro*/
|
|
|
|
0, /*tp_setattro*/
|
|
|
|
0, /*tp_as_buffer*/
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
Py_TPFLAGS_DEFAULT, /*tp_xxx4*/
|
2000-07-08 02:00:07 -03:00
|
|
|
gdbm_object__doc__, /*tp_doc*/
|
2008-07-02 19:38:47 -03:00
|
|
|
0, /*tp_traverse*/
|
|
|
|
0, /*tp_clear*/
|
|
|
|
0, /*tp_richcompare*/
|
|
|
|
0, /*tp_weaklistoffset*/
|
|
|
|
0, /*tp_iter*/
|
|
|
|
0, /*tp_iternext*/
|
|
|
|
dbm_methods, /*tp_methods*/
|
1994-08-08 05:06:37 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------- */
|
|
|
|
|
2015-04-17 15:05:18 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_gdbm.open as dbmopen
|
2018-02-26 10:02:22 -04:00
|
|
|
filename: unicode
|
2015-04-17 15:05:18 -03:00
|
|
|
flags: str="r"
|
|
|
|
mode: int(py_default="0o666") = 0o666
|
|
|
|
/
|
|
|
|
|
|
|
|
Open a dbm database and return a dbm object.
|
|
|
|
|
|
|
|
The filename argument is the name of the database file.
|
|
|
|
|
|
|
|
The optional flags argument can be 'r' (to open an existing database
|
|
|
|
for reading only -- default), 'w' (to open an existing database for
|
|
|
|
reading and writing), 'c' (which creates the database if it doesn't
|
|
|
|
exist), or 'n' (which always creates a new empty database).
|
|
|
|
|
|
|
|
Some versions of gdbm support additional flags which must be
|
|
|
|
appended to one of the flags described above. The module constant
|
|
|
|
'open_flags' is a string of valid additional flags. The 'f' flag
|
|
|
|
opens the database in fast mode; altered data will not automatically
|
|
|
|
be written to the disk after every change. This results in faster
|
|
|
|
writes to the database, but may result in an inconsistent database
|
|
|
|
if the program crashes while the database is still open. Use the
|
|
|
|
sync() method to force any unwritten data to be written to the disk.
|
|
|
|
The 's' flag causes all database operations to be synchronized to
|
|
|
|
disk. The 'u' flag disables locking of the database file.
|
|
|
|
|
|
|
|
The optional mode argument is the Unix mode of the file, used only
|
|
|
|
when the database has to be created. It defaults to octal 0o666.
|
|
|
|
[clinic start generated code]*/
|
1998-03-03 18:02:24 -04:00
|
|
|
|
1996-12-17 15:55:33 -04:00
|
|
|
static PyObject *
|
2018-02-26 10:02:22 -04:00
|
|
|
dbmopen_impl(PyObject *module, PyObject *filename, const char *flags,
|
|
|
|
int mode)
|
|
|
|
/*[clinic end generated code: output=9527750f5df90764 input=3be0b0875974b928]*/
|
1994-08-08 05:06:37 -03:00
|
|
|
{
|
2000-07-08 02:00:07 -03:00
|
|
|
int iflags;
|
|
|
|
|
|
|
|
switch (flags[0]) {
|
|
|
|
case 'r':
|
|
|
|
iflags = GDBM_READER;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
iflags = GDBM_WRITER;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
iflags = GDBM_WRCREAT;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
iflags = GDBM_NEWDB;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PyErr_SetString(DbmError,
|
2000-12-17 03:14:13 -04:00
|
|
|
"First flag must be one of 'r', 'w', 'c' or 'n'");
|
2000-07-08 02:00:07 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-12-17 03:14:13 -04:00
|
|
|
for (flags++; *flags != '\0'; flags++) {
|
|
|
|
char buf[40];
|
|
|
|
switch (*flags) {
|
|
|
|
#ifdef GDBM_FAST
|
|
|
|
case 'f':
|
|
|
|
iflags |= GDBM_FAST;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef GDBM_SYNC
|
|
|
|
case 's':
|
|
|
|
iflags |= GDBM_SYNC;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef GDBM_NOLOCK
|
|
|
|
case 'u':
|
|
|
|
iflags |= GDBM_NOLOCK;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2001-11-28 16:27:42 -04:00
|
|
|
PyOS_snprintf(buf, sizeof(buf), "Flag '%c' is not supported.",
|
2010-05-09 12:52:27 -03:00
|
|
|
*flags);
|
2000-12-17 03:14:13 -04:00
|
|
|
PyErr_SetString(DbmError, buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-26 10:02:22 -04:00
|
|
|
PyObject *filenamebytes = PyUnicode_EncodeFSDefault(filename);
|
|
|
|
if (filenamebytes == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
const char *name = PyBytes_AS_STRING(filenamebytes);
|
|
|
|
if (strlen(name) != (size_t)PyBytes_GET_SIZE(filenamebytes)) {
|
|
|
|
Py_DECREF(filenamebytes);
|
|
|
|
PyErr_SetString(PyExc_ValueError, "embedded null character");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyObject *self = newdbmobject(name, iflags, mode);
|
|
|
|
Py_DECREF(filenamebytes);
|
|
|
|
return self;
|
1994-08-08 05:06:37 -03:00
|
|
|
}
|
|
|
|
|
2015-12-25 13:53:18 -04:00
|
|
|
static const char dbmmodule_open_flags[] = "rwcn"
|
2000-12-17 03:14:13 -04:00
|
|
|
#ifdef GDBM_FAST
|
|
|
|
"f"
|
|
|
|
#endif
|
|
|
|
#ifdef GDBM_SYNC
|
|
|
|
"s"
|
|
|
|
#endif
|
|
|
|
#ifdef GDBM_NOLOCK
|
|
|
|
"u"
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
1996-12-17 15:55:33 -04:00
|
|
|
static PyMethodDef dbmmodule_methods[] = {
|
2015-04-17 15:05:18 -03:00
|
|
|
DBMOPEN_METHODDEF
|
2000-07-08 02:00:07 -03:00
|
|
|
{ 0, 0 },
|
1994-08-08 05:06:37 -03:00
|
|
|
};
|
|
|
|
|
2008-06-11 02:26:20 -03:00
|
|
|
|
|
|
|
static struct PyModuleDef _gdbmmodule = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
"_gdbm",
|
|
|
|
gdbmmodule__doc__,
|
|
|
|
-1,
|
|
|
|
dbmmodule_methods,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL
|
2008-06-11 02:26:20 -03:00
|
|
|
};
|
|
|
|
|
2002-08-01 23:27:13 -03:00
|
|
|
PyMODINIT_FUNC
|
2008-06-11 02:26:20 -03:00
|
|
|
PyInit__gdbm(void) {
|
2018-06-19 18:29:22 -03:00
|
|
|
PyObject *m;
|
2000-07-08 02:00:07 -03:00
|
|
|
|
2006-08-19 20:18:48 -03:00
|
|
|
if (PyType_Ready(&Dbmtype) < 0)
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
2008-06-11 02:26:20 -03:00
|
|
|
m = PyModule_Create(&_gdbmmodule);
|
2018-06-19 18:29:22 -03:00
|
|
|
if (m == NULL) {
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
2018-06-19 18:29:22 -03:00
|
|
|
}
|
|
|
|
|
2017-04-16 04:46:38 -03:00
|
|
|
DbmError = PyErr_NewException("_gdbm.error", PyExc_OSError, NULL);
|
2018-06-19 18:29:22 -03:00
|
|
|
if (DbmError == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
Py_INCREF(DbmError);
|
|
|
|
if (PyModule_AddObject(m, "error", DbmError) < 0) {
|
|
|
|
Py_DECREF(DbmError);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PyModule_AddStringConstant(m, "open_flags",
|
|
|
|
dbmmodule_open_flags) < 0) {
|
|
|
|
goto error;
|
2000-12-17 03:14:13 -04:00
|
|
|
}
|
2018-06-19 18:29:22 -03:00
|
|
|
|
2018-06-20 10:23:30 -03:00
|
|
|
#if defined(GDBM_VERSION_MAJOR) && defined(GDBM_VERSION_MINOR) && \
|
|
|
|
defined(GDBM_VERSION_PATCH)
|
2018-06-19 18:29:22 -03:00
|
|
|
PyObject *obj = Py_BuildValue("iii", GDBM_VERSION_MAJOR,
|
|
|
|
GDBM_VERSION_MINOR, GDBM_VERSION_PATCH);
|
|
|
|
if (obj == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (PyModule_AddObject(m, "_GDBM_VERSION", obj) < 0) {
|
|
|
|
Py_DECREF(obj);
|
|
|
|
goto error;
|
|
|
|
}
|
2018-06-20 10:23:30 -03:00
|
|
|
#endif
|
2018-06-19 18:29:22 -03:00
|
|
|
|
2008-06-11 02:26:20 -03:00
|
|
|
return m;
|
2018-06-19 18:29:22 -03:00
|
|
|
|
|
|
|
error:
|
|
|
|
Py_DECREF(m);
|
|
|
|
return NULL;
|
1994-08-08 05:06:37 -03:00
|
|
|
}
|