Commit ed6b9636 authored by Jeremy Hylton's avatar Jeremy Hylton

Remove object_from_oid() which appeared to be mis-used about half the

time.  Instead use std PyDict_GetItem() and incref only when
necessary.
parent 64c82063
...@@ -88,7 +88,7 @@ process must skip such objects, rather than deactivating them. ...@@ -88,7 +88,7 @@ process must skip such objects, rather than deactivating them.
static char cPickleCache_doc_string[] = static char cPickleCache_doc_string[] =
"Defines the PickleCache used by ZODB Connection objects.\n" "Defines the PickleCache used by ZODB Connection objects.\n"
"\n" "\n"
"$Id: cPickleCache.c,v 1.76 2003/04/01 16:01:54 jeremy Exp $\n"; "$Id: cPickleCache.c,v 1.77 2003/04/01 16:17:50 jeremy Exp $\n";
#define ASSIGN(V,E) {PyObject *__e; __e=(E); Py_XDECREF(V); (V)=__e;} #define ASSIGN(V,E) {PyObject *__e; __e=(E); Py_XDECREF(V); (V)=__e;}
#define UNLESS(E) if(!(E)) #define UNLESS(E) if(!(E))
...@@ -147,18 +147,6 @@ static int cc_ass_sub(ccobject *self, PyObject *key, PyObject *v); ...@@ -147,18 +147,6 @@ static int cc_ass_sub(ccobject *self, PyObject *key, PyObject *v);
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
/* somewhat of a replacement for PyDict_GetItem(self->data....
however this returns a *new* reference */
static PyObject *
object_from_oid(ccobject *self, PyObject *key)
{
PyObject *v = PyDict_GetItem(self->data, key);
if (!v)
return NULL;
Py_INCREF(v);
return v;
}
#define OBJECT_FROM_RING(SELF, HERE, CTX) \ #define OBJECT_FROM_RING(SELF, HERE, CTX) \
((cPersistentObject *)(((char *)here) - offsetof(cPersistentObject, ring))) ((cPersistentObject *)(((char *)here) - offsetof(cPersistentObject, ring)))
...@@ -308,7 +296,7 @@ cc_minimize(ccobject *self, PyObject *args) ...@@ -308,7 +296,7 @@ cc_minimize(ccobject *self, PyObject *args)
static void static void
_invalidate(ccobject *self, PyObject *key) _invalidate(ccobject *self, PyObject *key)
{ {
PyObject *v = object_from_oid(self, key); PyObject *v = PyDict_GetItem(self->data, key);
if (!v) if (!v)
return; return;
...@@ -329,7 +317,6 @@ _invalidate(ccobject *self, PyObject *key) ...@@ -329,7 +317,6 @@ _invalidate(ccobject *self, PyObject *key)
if (PyObject_DelAttr(v, py__p_changed) < 0) if (PyObject_DelAttr(v, py__p_changed) < 0)
PyErr_Clear(); PyErr_Clear();
} }
Py_XDECREF(v);
} }
static PyObject * static PyObject *
...@@ -379,17 +366,16 @@ cc_get(ccobject *self, PyObject *args) ...@@ -379,17 +366,16 @@ cc_get(ccobject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "O|O:get", &key, &d)) if (!PyArg_ParseTuple(args, "O|O:get", &key, &d))
return NULL; return NULL;
r = (PyObject *)object_from_oid(self, key); r = PyDict_GetItem(self->data, key);
if (!r) { if (!r) {
if (d) { if (d) {
r = d; r = d;
Py_INCREF(r);
} else { } else {
PyErr_SetObject(PyExc_KeyError, key); PyErr_SetObject(PyExc_KeyError, key);
return NULL; return NULL;
} }
} }
Py_INCREF(r);
return r; return r;
} }
...@@ -670,11 +656,12 @@ cc_subscript(ccobject *self, PyObject *key) ...@@ -670,11 +656,12 @@ cc_subscript(ccobject *self, PyObject *key)
{ {
PyObject *r; PyObject *r;
r = (PyObject *)object_from_oid(self, key); r = PyDict_GetItem(self->data, key);
if (r == NULL) { if (r == NULL) {
PyErr_SetObject(PyExc_KeyError, key); PyErr_SetObject(PyExc_KeyError, key);
return NULL; return NULL;
} }
Py_INCREF(r);
return r; return r;
} }
...@@ -739,16 +726,14 @@ cc_add_item(ccobject *self, PyObject *key, PyObject *v) ...@@ -739,16 +726,14 @@ cc_add_item(ccobject *self, PyObject *key, PyObject *v)
} }
Py_DECREF(jar); Py_DECREF(jar);
object_again = object_from_oid(self, key); object_again = PyDict_GetItem(self->data, key);
if (object_again) { if (object_again) {
if (object_again != v) { if (object_again != v) {
Py_DECREF(object_again);
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"Can not re-register object under a different oid"); "Can not re-register object under a different oid");
return -1; return -1;
} else { } else {
/* re-register under the same oid - no work needed */ /* re-register under the same oid - no work needed */
Py_DECREF(object_again);
return 0; return 0;
} }
} }
...@@ -805,7 +790,7 @@ cc_del_item(ccobject *self, PyObject *key) ...@@ -805,7 +790,7 @@ cc_del_item(ccobject *self, PyObject *key)
cPersistentObject *p; cPersistentObject *p;
/* unlink this item from the ring */ /* unlink this item from the ring */
v = (PyObject *)object_from_oid(self, key); v = PyDict_GetItem(self->data, key);
if (v == NULL) if (v == NULL)
return -1; return -1;
...@@ -832,8 +817,6 @@ cc_del_item(ccobject *self, PyObject *key) ...@@ -832,8 +817,6 @@ cc_del_item(ccobject *self, PyObject *key)
p->cache = NULL; p->cache = NULL;
} }
Py_DECREF(v);
if (PyDict_DelItem(self->data, key) < 0) { if (PyDict_DelItem(self->data, key) < 0) {
PyErr_SetString(PyExc_RuntimeError, PyErr_SetString(PyExc_RuntimeError,
"unexpectedly couldn't remove key in cc_ass_sub"); "unexpectedly couldn't remove key in cc_ass_sub");
......
...@@ -88,7 +88,7 @@ process must skip such objects, rather than deactivating them. ...@@ -88,7 +88,7 @@ process must skip such objects, rather than deactivating them.
static char cPickleCache_doc_string[] = static char cPickleCache_doc_string[] =
"Defines the PickleCache used by ZODB Connection objects.\n" "Defines the PickleCache used by ZODB Connection objects.\n"
"\n" "\n"
"$Id: cPickleCache.c,v 1.76 2003/04/01 16:01:54 jeremy Exp $\n"; "$Id: cPickleCache.c,v 1.77 2003/04/01 16:17:50 jeremy Exp $\n";
#define ASSIGN(V,E) {PyObject *__e; __e=(E); Py_XDECREF(V); (V)=__e;} #define ASSIGN(V,E) {PyObject *__e; __e=(E); Py_XDECREF(V); (V)=__e;}
#define UNLESS(E) if(!(E)) #define UNLESS(E) if(!(E))
...@@ -147,18 +147,6 @@ static int cc_ass_sub(ccobject *self, PyObject *key, PyObject *v); ...@@ -147,18 +147,6 @@ static int cc_ass_sub(ccobject *self, PyObject *key, PyObject *v);
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
/* somewhat of a replacement for PyDict_GetItem(self->data....
however this returns a *new* reference */
static PyObject *
object_from_oid(ccobject *self, PyObject *key)
{
PyObject *v = PyDict_GetItem(self->data, key);
if (!v)
return NULL;
Py_INCREF(v);
return v;
}
#define OBJECT_FROM_RING(SELF, HERE, CTX) \ #define OBJECT_FROM_RING(SELF, HERE, CTX) \
((cPersistentObject *)(((char *)here) - offsetof(cPersistentObject, ring))) ((cPersistentObject *)(((char *)here) - offsetof(cPersistentObject, ring)))
...@@ -308,7 +296,7 @@ cc_minimize(ccobject *self, PyObject *args) ...@@ -308,7 +296,7 @@ cc_minimize(ccobject *self, PyObject *args)
static void static void
_invalidate(ccobject *self, PyObject *key) _invalidate(ccobject *self, PyObject *key)
{ {
PyObject *v = object_from_oid(self, key); PyObject *v = PyDict_GetItem(self->data, key);
if (!v) if (!v)
return; return;
...@@ -329,7 +317,6 @@ _invalidate(ccobject *self, PyObject *key) ...@@ -329,7 +317,6 @@ _invalidate(ccobject *self, PyObject *key)
if (PyObject_DelAttr(v, py__p_changed) < 0) if (PyObject_DelAttr(v, py__p_changed) < 0)
PyErr_Clear(); PyErr_Clear();
} }
Py_XDECREF(v);
} }
static PyObject * static PyObject *
...@@ -379,17 +366,16 @@ cc_get(ccobject *self, PyObject *args) ...@@ -379,17 +366,16 @@ cc_get(ccobject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "O|O:get", &key, &d)) if (!PyArg_ParseTuple(args, "O|O:get", &key, &d))
return NULL; return NULL;
r = (PyObject *)object_from_oid(self, key); r = PyDict_GetItem(self->data, key);
if (!r) { if (!r) {
if (d) { if (d) {
r = d; r = d;
Py_INCREF(r);
} else { } else {
PyErr_SetObject(PyExc_KeyError, key); PyErr_SetObject(PyExc_KeyError, key);
return NULL; return NULL;
} }
} }
Py_INCREF(r);
return r; return r;
} }
...@@ -670,11 +656,12 @@ cc_subscript(ccobject *self, PyObject *key) ...@@ -670,11 +656,12 @@ cc_subscript(ccobject *self, PyObject *key)
{ {
PyObject *r; PyObject *r;
r = (PyObject *)object_from_oid(self, key); r = PyDict_GetItem(self->data, key);
if (r == NULL) { if (r == NULL) {
PyErr_SetObject(PyExc_KeyError, key); PyErr_SetObject(PyExc_KeyError, key);
return NULL; return NULL;
} }
Py_INCREF(r);
return r; return r;
} }
...@@ -739,16 +726,14 @@ cc_add_item(ccobject *self, PyObject *key, PyObject *v) ...@@ -739,16 +726,14 @@ cc_add_item(ccobject *self, PyObject *key, PyObject *v)
} }
Py_DECREF(jar); Py_DECREF(jar);
object_again = object_from_oid(self, key); object_again = PyDict_GetItem(self->data, key);
if (object_again) { if (object_again) {
if (object_again != v) { if (object_again != v) {
Py_DECREF(object_again);
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"Can not re-register object under a different oid"); "Can not re-register object under a different oid");
return -1; return -1;
} else { } else {
/* re-register under the same oid - no work needed */ /* re-register under the same oid - no work needed */
Py_DECREF(object_again);
return 0; return 0;
} }
} }
...@@ -805,7 +790,7 @@ cc_del_item(ccobject *self, PyObject *key) ...@@ -805,7 +790,7 @@ cc_del_item(ccobject *self, PyObject *key)
cPersistentObject *p; cPersistentObject *p;
/* unlink this item from the ring */ /* unlink this item from the ring */
v = (PyObject *)object_from_oid(self, key); v = PyDict_GetItem(self->data, key);
if (v == NULL) if (v == NULL)
return -1; return -1;
...@@ -832,8 +817,6 @@ cc_del_item(ccobject *self, PyObject *key) ...@@ -832,8 +817,6 @@ cc_del_item(ccobject *self, PyObject *key)
p->cache = NULL; p->cache = NULL;
} }
Py_DECREF(v);
if (PyDict_DelItem(self->data, key) < 0) { if (PyDict_DelItem(self->data, key) < 0) {
PyErr_SetString(PyExc_RuntimeError, PyErr_SetString(PyExc_RuntimeError,
"unexpectedly couldn't remove key in cc_ass_sub"); "unexpectedly couldn't remove key in cc_ass_sub");
......
...@@ -88,7 +88,7 @@ process must skip such objects, rather than deactivating them. ...@@ -88,7 +88,7 @@ process must skip such objects, rather than deactivating them.
static char cPickleCache_doc_string[] = static char cPickleCache_doc_string[] =
"Defines the PickleCache used by ZODB Connection objects.\n" "Defines the PickleCache used by ZODB Connection objects.\n"
"\n" "\n"
"$Id: cPickleCache.c,v 1.76 2003/04/01 16:01:54 jeremy Exp $\n"; "$Id: cPickleCache.c,v 1.77 2003/04/01 16:17:50 jeremy Exp $\n";
#define ASSIGN(V,E) {PyObject *__e; __e=(E); Py_XDECREF(V); (V)=__e;} #define ASSIGN(V,E) {PyObject *__e; __e=(E); Py_XDECREF(V); (V)=__e;}
#define UNLESS(E) if(!(E)) #define UNLESS(E) if(!(E))
...@@ -147,18 +147,6 @@ static int cc_ass_sub(ccobject *self, PyObject *key, PyObject *v); ...@@ -147,18 +147,6 @@ static int cc_ass_sub(ccobject *self, PyObject *key, PyObject *v);
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
/* somewhat of a replacement for PyDict_GetItem(self->data....
however this returns a *new* reference */
static PyObject *
object_from_oid(ccobject *self, PyObject *key)
{
PyObject *v = PyDict_GetItem(self->data, key);
if (!v)
return NULL;
Py_INCREF(v);
return v;
}
#define OBJECT_FROM_RING(SELF, HERE, CTX) \ #define OBJECT_FROM_RING(SELF, HERE, CTX) \
((cPersistentObject *)(((char *)here) - offsetof(cPersistentObject, ring))) ((cPersistentObject *)(((char *)here) - offsetof(cPersistentObject, ring)))
...@@ -308,7 +296,7 @@ cc_minimize(ccobject *self, PyObject *args) ...@@ -308,7 +296,7 @@ cc_minimize(ccobject *self, PyObject *args)
static void static void
_invalidate(ccobject *self, PyObject *key) _invalidate(ccobject *self, PyObject *key)
{ {
PyObject *v = object_from_oid(self, key); PyObject *v = PyDict_GetItem(self->data, key);
if (!v) if (!v)
return; return;
...@@ -329,7 +317,6 @@ _invalidate(ccobject *self, PyObject *key) ...@@ -329,7 +317,6 @@ _invalidate(ccobject *self, PyObject *key)
if (PyObject_DelAttr(v, py__p_changed) < 0) if (PyObject_DelAttr(v, py__p_changed) < 0)
PyErr_Clear(); PyErr_Clear();
} }
Py_XDECREF(v);
} }
static PyObject * static PyObject *
...@@ -379,17 +366,16 @@ cc_get(ccobject *self, PyObject *args) ...@@ -379,17 +366,16 @@ cc_get(ccobject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "O|O:get", &key, &d)) if (!PyArg_ParseTuple(args, "O|O:get", &key, &d))
return NULL; return NULL;
r = (PyObject *)object_from_oid(self, key); r = PyDict_GetItem(self->data, key);
if (!r) { if (!r) {
if (d) { if (d) {
r = d; r = d;
Py_INCREF(r);
} else { } else {
PyErr_SetObject(PyExc_KeyError, key); PyErr_SetObject(PyExc_KeyError, key);
return NULL; return NULL;
} }
} }
Py_INCREF(r);
return r; return r;
} }
...@@ -670,11 +656,12 @@ cc_subscript(ccobject *self, PyObject *key) ...@@ -670,11 +656,12 @@ cc_subscript(ccobject *self, PyObject *key)
{ {
PyObject *r; PyObject *r;
r = (PyObject *)object_from_oid(self, key); r = PyDict_GetItem(self->data, key);
if (r == NULL) { if (r == NULL) {
PyErr_SetObject(PyExc_KeyError, key); PyErr_SetObject(PyExc_KeyError, key);
return NULL; return NULL;
} }
Py_INCREF(r);
return r; return r;
} }
...@@ -739,16 +726,14 @@ cc_add_item(ccobject *self, PyObject *key, PyObject *v) ...@@ -739,16 +726,14 @@ cc_add_item(ccobject *self, PyObject *key, PyObject *v)
} }
Py_DECREF(jar); Py_DECREF(jar);
object_again = object_from_oid(self, key); object_again = PyDict_GetItem(self->data, key);
if (object_again) { if (object_again) {
if (object_again != v) { if (object_again != v) {
Py_DECREF(object_again);
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"Can not re-register object under a different oid"); "Can not re-register object under a different oid");
return -1; return -1;
} else { } else {
/* re-register under the same oid - no work needed */ /* re-register under the same oid - no work needed */
Py_DECREF(object_again);
return 0; return 0;
} }
} }
...@@ -805,7 +790,7 @@ cc_del_item(ccobject *self, PyObject *key) ...@@ -805,7 +790,7 @@ cc_del_item(ccobject *self, PyObject *key)
cPersistentObject *p; cPersistentObject *p;
/* unlink this item from the ring */ /* unlink this item from the ring */
v = (PyObject *)object_from_oid(self, key); v = PyDict_GetItem(self->data, key);
if (v == NULL) if (v == NULL)
return -1; return -1;
...@@ -832,8 +817,6 @@ cc_del_item(ccobject *self, PyObject *key) ...@@ -832,8 +817,6 @@ cc_del_item(ccobject *self, PyObject *key)
p->cache = NULL; p->cache = NULL;
} }
Py_DECREF(v);
if (PyDict_DelItem(self->data, key) < 0) { if (PyDict_DelItem(self->data, key) < 0) {
PyErr_SetString(PyExc_RuntimeError, PyErr_SetString(PyExc_RuntimeError,
"unexpectedly couldn't remove key in cc_ass_sub"); "unexpectedly couldn't remove key in cc_ass_sub");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment