[Python-checkins] python/dist/src/Modules _bsddb.c,1.34,1.35

greg at users.sourceforge.net greg at users.sourceforge.net
Mon Jun 28 00:06:50 EDT 2004


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11608/Modules

Modified Files:
	_bsddb.c 
Log Message:
Adds support for DB.pget and DBCursor.pget methods.

Based on a patch supplied by Ian Ward <ian at arevco.ca> on the pybsddb
mailing list 2004-03-26.


Index: _bsddb.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/_bsddb.c,v
retrieving revision 1.34
retrieving revision 1.35
diff -C2 -d -r1.34 -r1.35
*** _bsddb.c	28 Jun 2004 01:20:40 -0000	1.34
--- _bsddb.c	28 Jun 2004 04:06:47 -0000	1.35
***************
*** 98,102 ****
  #endif
  
! #define PY_BSDDB_VERSION "4.2.7"
  static char *rcs_id = "$Id$";
  
--- 98,102 ----
  #endif
  
! #define PY_BSDDB_VERSION "4.2.8"
  static char *rcs_id = "$Id$";
  
***************
*** 1464,1467 ****
--- 1464,1555 ----
  }
  
+ static PyObject*
+ DB_pget(DBObject* self, PyObject* args, PyObject* kwargs)
+ {
+     int err, flags=0;
+     PyObject* txnobj = NULL;
+     PyObject* keyobj;
+     PyObject* dfltobj = NULL;
+     PyObject* retval = NULL;
+     int dlen = -1;
+     int doff = -1;
+     DBT key, pkey, data;
+     DB_TXN *txn = NULL;
+     char* kwnames[] = {"key", "default", "txn", "flags", "dlen", "doff", NULL};
+ 
+     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOiii:pget", kwnames,
+                                      &keyobj, &dfltobj, &txnobj, &flags, &dlen,
+                                      &doff))
+         return NULL;
+ 
+     CHECK_DB_NOT_CLOSED(self);
+     if (!make_key_dbt(self, keyobj, &key, &flags))
+         return NULL;
+     if (!checkTxnObj(txnobj, &txn)) {
+         FREE_DBT(key);
+         return NULL;
+     }
+ 
+     CLEAR_DBT(data);
+     if (CHECK_DBFLAG(self, DB_THREAD)) {
+         /* Tell BerkeleyDB to malloc the return value (thread safe) */
+         data.flags = DB_DBT_MALLOC;
+     }
+     if (!add_partial_dbt(&data, dlen, doff)) {
+         FREE_DBT(key);
+         return NULL;
+     }
+ 
+     CLEAR_DBT(pkey);
+     pkey.flags = DB_DBT_MALLOC;
+     
+     MYDB_BEGIN_ALLOW_THREADS;
+     err = self->db->pget(self->db, txn, &key, &pkey, &data, flags);
+     MYDB_END_ALLOW_THREADS;
+ 
+     if ((err == DB_NOTFOUND) && (dfltobj != NULL)) {
+         err = 0;
+         Py_INCREF(dfltobj);
+         retval = dfltobj;
+     }
+     else if ((err == DB_NOTFOUND) && self->moduleFlags.getReturnsNone) {
+         err = 0;
+         Py_INCREF(Py_None);
+         retval = Py_None;
+     }
+     else if (!err) {
+         PyObject *pkeyObj;
+         PyObject *dataObj;
+         dataObj = PyString_FromStringAndSize(data.data, data.size);
+ 
+         if (self->primaryDBType == DB_RECNO ||
+             self->primaryDBType == DB_QUEUE)
+             pkeyObj = PyInt_FromLong(*(long *)pkey.data);
+         else
+             pkeyObj = PyString_FromStringAndSize(pkey.data, pkey.size);
+ 
+         if (flags & DB_SET_RECNO) /* return key , pkey and data */
+         {
+             PyObject *keyObj;
+             int type = _DB_get_type(self);
+             if (type == DB_RECNO || type == DB_QUEUE)
+                 keyObj = PyInt_FromLong(*(long *)key.data);
+             else
+                 keyObj = PyString_FromStringAndSize(key.data, key.size);
+             retval = Py_BuildValue("OOO", keyObj, pkeyObj, dataObj);
+         }
+         else /* return just the pkey and data */
+         {
+             retval = Py_BuildValue("OO", pkeyObj, dataObj);
+         }
+ 	FREE_DBT(pkey);
+         FREE_DBT(data);
+     }
+     FREE_DBT(key);
+ 
+     RETURN_IF_ERR();
+     return retval;
+ }
+ 
  
  /* Return size of entry */
***************
*** 2828,2831 ****
--- 2916,3019 ----
  }
  
+ static PyObject*
+ DBC_pget(DBCursorObject* self, PyObject* args, PyObject *kwargs)
+ {
+     int err, flags=0;
+     PyObject* keyobj = NULL;
+     PyObject* dataobj = NULL;
+     PyObject* retval = NULL;
+     int dlen = -1;
+     int doff = -1;
+     DBT key, pkey, data;
+     char* kwnames[] = { "key","data", "flags", "dlen", "doff", NULL };
+ 
+     CLEAR_DBT(key);
+     CLEAR_DBT(data);
+     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii:pget", &kwnames[2],
+ 				     &flags, &dlen, &doff))
+     {
+         PyErr_Clear();
+         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|ii:pget",
+                                          &kwnames[1], 
+ 					 &keyobj, &flags, &dlen, &doff))
+         {
+             PyErr_Clear();
+             if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOi|ii:pget",
+                                              kwnames, &keyobj, &dataobj,
+                                              &flags, &dlen, &doff))
+             {
+                 return NULL;
+ 	    }
+ 	}
+     }
+ 
+     CHECK_CURSOR_NOT_CLOSED(self);
+ 
+     if (keyobj && !make_key_dbt(self->mydb, keyobj, &key, NULL))
+         return NULL;
+     if ( (dataobj && !make_dbt(dataobj, &data)) ||
+          (!add_partial_dbt(&data, dlen, doff)) ) {
+         FREE_DBT(key);
+         return NULL;
+     }
+ 
+     if (CHECK_DBFLAG(self->mydb, DB_THREAD)) {
+         data.flags = DB_DBT_MALLOC;
+         if (!(key.flags & DB_DBT_REALLOC)) {
+             key.flags |= DB_DBT_MALLOC;
+         }
+     }
+ 
+     CLEAR_DBT(pkey);
+     pkey.flags = DB_DBT_MALLOC;
+ 
+     MYDB_BEGIN_ALLOW_THREADS;
+     err = self->dbc->c_pget(self->dbc, &key, &pkey, &data, flags);
+     MYDB_END_ALLOW_THREADS;
+ 
+     if ((err == DB_NOTFOUND) && self->mydb->moduleFlags.getReturnsNone) {
+         Py_INCREF(Py_None);
+         retval = Py_None;
+     }
+     else if (makeDBError(err)) {
+         retval = NULL;
+     }
+     else {
+         PyObject *pkeyObj;
+         PyObject *dataObj;
+         dataObj = PyString_FromStringAndSize(data.data, data.size);
+ 
+         if (self->mydb->primaryDBType == DB_RECNO ||
+             self->mydb->primaryDBType == DB_QUEUE)
+             pkeyObj = PyInt_FromLong(*(long *)pkey.data);
+         else
+             pkeyObj = PyString_FromStringAndSize(pkey.data, pkey.size);
+ 
+         if (flags & DB_SET_RECNO) /* return key, pkey and data */
+         {
+             PyObject *keyObj;
+             int type = _DB_get_type(self->mydb);
+             if (type == DB_RECNO || type == DB_QUEUE)
+                 keyObj = PyInt_FromLong(*(long *)key.data);
+             else
+                 keyObj = PyString_FromStringAndSize(key.data, key.size);
+             retval = Py_BuildValue("OOO", keyObj, pkeyObj, dataObj);
+             FREE_DBT(key);
+         }
+         else /* return just the pkey and data */
+         {
+             retval = Py_BuildValue("OO", pkeyObj, dataObj);
+         }
+         FREE_DBT(pkey);
+         FREE_DBT(data);
+     }
+     /* the only time REALLOC should be set is if we used an integer
+      * key that make_key_dbt malloc'd for us.  always free these. */
+     if (key.flags & DB_DBT_REALLOC) {
+         FREE_DBT(key);
+     }
+     return retval;
+ }
+ 
  
  static PyObject*
***************
*** 2975,2980 ****
          }
          FREE_DBT(data);
      }
-     FREE_DBT(key);
  
      return retval;
--- 3163,3173 ----
          }
          FREE_DBT(data);
+         FREE_DBT(key);
+     }
+     /* the only time REALLOC should be set is if we used an integer
+      * key that make_key_dbt malloc'd for us.  always free these. */
+     if (key.flags & DB_DBT_REALLOC) {
+         FREE_DBT(key);
      }
  
      return retval;
***************
*** 3045,3049 ****
      }
      /* the only time REALLOC should be set is if we used an integer
!      * key that make_dbt_key malloc'd for us.  always free these. */
      if (key.flags & DB_DBT_REALLOC) {
          FREE_DBT(key);
--- 3238,3242 ----
      }
      /* the only time REALLOC should be set is if we used an integer
!      * key that make_key_dbt malloc'd for us.  always free these. */
      if (key.flags & DB_DBT_REALLOC) {
          FREE_DBT(key);
***************
*** 4184,4187 ****
--- 4377,4381 ----
      {"fd",              (PyCFunction)DB_fd,             METH_VARARGS},
      {"get",             (PyCFunction)DB_get,            METH_VARARGS|METH_KEYWORDS},
+     {"pget",            (PyCFunction)DB_pget,           METH_VARARGS|METH_KEYWORDS},
      {"get_both",        (PyCFunction)DB_get_both,       METH_VARARGS|METH_KEYWORDS},
      {"get_byteswapped", (PyCFunction)DB_get_byteswapped,METH_VARARGS},
***************
*** 4243,4246 ****
--- 4437,4441 ----
      {"first",           (PyCFunction)DBC_first,         METH_VARARGS|METH_KEYWORDS},
      {"get",             (PyCFunction)DBC_get,           METH_VARARGS|METH_KEYWORDS},
+     {"pget",            (PyCFunction)DBC_pget,          METH_VARARGS|METH_KEYWORDS},
      {"get_recno",       (PyCFunction)DBC_get_recno,     METH_VARARGS},
      {"last",            (PyCFunction)DBC_last,          METH_VARARGS|METH_KEYWORDS},




More information about the Python-checkins mailing list