Commit 81d666f2 authored by Jeremy Hylton's avatar Jeremy Hylton

Add a level of redirection to change registration for persistent objects.

changed() in cPersistence.c now calls the jar's register() method.
Connection's register() method just calls get_transaction().register().

This redirection would allow specialized Connection's to change the
default policy on how the transaction manager is selected without
hacking the Transaction module.

The cPersistence.c code is based on _PyPersist_RegisterDataManager()
from the Zope3 code.
parent 5b1cc377
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
static char cPersistence_doc_string[] = static char cPersistence_doc_string[] =
"Defines Persistent mixin class for persistent objects.\n" "Defines Persistent mixin class for persistent objects.\n"
"\n" "\n"
"$Id: cPersistence.c,v 1.59 2002/04/15 18:42:51 jeremy Exp $\n"; "$Id: cPersistence.c,v 1.60 2002/06/14 15:29:30 jeremy Exp $\n";
#include "cPersistence.h" #include "cPersistence.h"
...@@ -214,35 +214,33 @@ deallocated(cPersistentObject *self) ...@@ -214,35 +214,33 @@ deallocated(cPersistentObject *self)
static int static int
changed(cPersistentObject *self) changed(cPersistentObject *self)
{ {
static PyObject *builtins=0, *get_transaction=0, *py_register=0;
PyObject *T;
if ((self->state == cPersistent_UPTODATE_STATE || if ((self->state == cPersistent_UPTODATE_STATE ||
self->state == cPersistent_STICKY_STATE) self->state == cPersistent_STICKY_STATE)
&& self->jar) && self->jar)
{ {
UNLESS (get_transaction) PyObject *meth, *arg, *result;
{ static PyObject *s_register;
UNLESS (py_register=PyString_FromString("register")) return -1;
UNLESS (T=PyImport_ImportModule("__main__")) return -1; if (s_register == NULL)
ASSIGN(T,PyObject_GetAttrString(T,"__builtins__")); s_register = PyString_InternFromString("register");
UNLESS (T) return -1; meth = PyObject_GetAttr((PyObject *)self->jar, s_register);
builtins=T; if (meth == NULL)
UNLESS (get_transaction=PyObject_GetAttrString(builtins, return -1;
"get_transaction")) arg = PyTuple_New(1);
PyErr_Clear(); if (arg == NULL) {
} Py_DECREF(meth);
if (get_transaction) return -1;
{
UNLESS (T=PyObject_CallObject(get_transaction,NULL)) return -1;
ASSIGN(T,PyObject_GetAttr(T,py_register));
UNLESS (T) return -1;
ASSIGN(T, PyObject_CallFunction(T,"O",self));
if (T) Py_DECREF(T);
else return -1;
} }
PyTuple_SET_ITEM(arg, 0, (PyObject *)self);
result = PyObject_Call(meth, arg, NULL);
PyTuple_SET_ITEM(arg, 0, NULL);
Py_DECREF(arg);
Py_DECREF(meth);
if (result == NULL)
return -1;
Py_DECREF(result);
self->state=cPersistent_CHANGED_STATE; self->state = cPersistent_CHANGED_STATE;
} }
return 0; return 0;
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
############################################################################## ##############################################################################
"""Database connection support """Database connection support
$Id: Connection.py,v 1.69 2002/06/12 19:40:47 jeremy Exp $""" $Id: Connection.py,v 1.70 2002/06/14 15:29:30 jeremy Exp $"""
from cPickleCache import PickleCache, MUCH_RING_CHECKING from cPickleCache import PickleCache, MUCH_RING_CHECKING
from POSException import ConflictError, ReadConflictError from POSException import ConflictError, ReadConflictError
...@@ -480,7 +480,18 @@ class Connection(ExportImport.ExportImport): ...@@ -480,7 +480,18 @@ class Connection(ExportImport.ExportImport):
except KeyError: except KeyError:
return self._version return self._version
def root(self): return self['\0\0\0\0\0\0\0\0'] def register(self, object):
"""Register an object with the appropriate transaction manager.
A subclass could override this method to customize the default
policy of one transaction manager for each thread.
"""
assert object._p_jar is self
assert object._p_oid is not None
get_transaction().register(object)
def root(self):
return self['\0\0\0\0\0\0\0\0']
def setstate(self, object): def setstate(self, object):
oid = object._p_oid oid = object._p_oid
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
static char cPersistence_doc_string[] = static char cPersistence_doc_string[] =
"Defines Persistent mixin class for persistent objects.\n" "Defines Persistent mixin class for persistent objects.\n"
"\n" "\n"
"$Id: cPersistence.c,v 1.59 2002/04/15 18:42:51 jeremy Exp $\n"; "$Id: cPersistence.c,v 1.60 2002/06/14 15:29:30 jeremy Exp $\n";
#include "cPersistence.h" #include "cPersistence.h"
...@@ -214,35 +214,33 @@ deallocated(cPersistentObject *self) ...@@ -214,35 +214,33 @@ deallocated(cPersistentObject *self)
static int static int
changed(cPersistentObject *self) changed(cPersistentObject *self)
{ {
static PyObject *builtins=0, *get_transaction=0, *py_register=0;
PyObject *T;
if ((self->state == cPersistent_UPTODATE_STATE || if ((self->state == cPersistent_UPTODATE_STATE ||
self->state == cPersistent_STICKY_STATE) self->state == cPersistent_STICKY_STATE)
&& self->jar) && self->jar)
{ {
UNLESS (get_transaction) PyObject *meth, *arg, *result;
{ static PyObject *s_register;
UNLESS (py_register=PyString_FromString("register")) return -1;
UNLESS (T=PyImport_ImportModule("__main__")) return -1; if (s_register == NULL)
ASSIGN(T,PyObject_GetAttrString(T,"__builtins__")); s_register = PyString_InternFromString("register");
UNLESS (T) return -1; meth = PyObject_GetAttr((PyObject *)self->jar, s_register);
builtins=T; if (meth == NULL)
UNLESS (get_transaction=PyObject_GetAttrString(builtins, return -1;
"get_transaction")) arg = PyTuple_New(1);
PyErr_Clear(); if (arg == NULL) {
} Py_DECREF(meth);
if (get_transaction) return -1;
{
UNLESS (T=PyObject_CallObject(get_transaction,NULL)) return -1;
ASSIGN(T,PyObject_GetAttr(T,py_register));
UNLESS (T) return -1;
ASSIGN(T, PyObject_CallFunction(T,"O",self));
if (T) Py_DECREF(T);
else return -1;
} }
PyTuple_SET_ITEM(arg, 0, (PyObject *)self);
result = PyObject_Call(meth, arg, NULL);
PyTuple_SET_ITEM(arg, 0, NULL);
Py_DECREF(arg);
Py_DECREF(meth);
if (result == NULL)
return -1;
Py_DECREF(result);
self->state=cPersistent_CHANGED_STATE; self->state = cPersistent_CHANGED_STATE;
} }
return 0; return 0;
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
static char cPersistence_doc_string[] = static char cPersistence_doc_string[] =
"Defines Persistent mixin class for persistent objects.\n" "Defines Persistent mixin class for persistent objects.\n"
"\n" "\n"
"$Id: cPersistence.c,v 1.59 2002/04/15 18:42:51 jeremy Exp $\n"; "$Id: cPersistence.c,v 1.60 2002/06/14 15:29:30 jeremy Exp $\n";
#include "cPersistence.h" #include "cPersistence.h"
...@@ -214,35 +214,33 @@ deallocated(cPersistentObject *self) ...@@ -214,35 +214,33 @@ deallocated(cPersistentObject *self)
static int static int
changed(cPersistentObject *self) changed(cPersistentObject *self)
{ {
static PyObject *builtins=0, *get_transaction=0, *py_register=0;
PyObject *T;
if ((self->state == cPersistent_UPTODATE_STATE || if ((self->state == cPersistent_UPTODATE_STATE ||
self->state == cPersistent_STICKY_STATE) self->state == cPersistent_STICKY_STATE)
&& self->jar) && self->jar)
{ {
UNLESS (get_transaction) PyObject *meth, *arg, *result;
{ static PyObject *s_register;
UNLESS (py_register=PyString_FromString("register")) return -1;
UNLESS (T=PyImport_ImportModule("__main__")) return -1; if (s_register == NULL)
ASSIGN(T,PyObject_GetAttrString(T,"__builtins__")); s_register = PyString_InternFromString("register");
UNLESS (T) return -1; meth = PyObject_GetAttr((PyObject *)self->jar, s_register);
builtins=T; if (meth == NULL)
UNLESS (get_transaction=PyObject_GetAttrString(builtins, return -1;
"get_transaction")) arg = PyTuple_New(1);
PyErr_Clear(); if (arg == NULL) {
} Py_DECREF(meth);
if (get_transaction) return -1;
{
UNLESS (T=PyObject_CallObject(get_transaction,NULL)) return -1;
ASSIGN(T,PyObject_GetAttr(T,py_register));
UNLESS (T) return -1;
ASSIGN(T, PyObject_CallFunction(T,"O",self));
if (T) Py_DECREF(T);
else return -1;
} }
PyTuple_SET_ITEM(arg, 0, (PyObject *)self);
result = PyObject_Call(meth, arg, NULL);
PyTuple_SET_ITEM(arg, 0, NULL);
Py_DECREF(arg);
Py_DECREF(meth);
if (result == NULL)
return -1;
Py_DECREF(result);
self->state=cPersistent_CHANGED_STATE; self->state = cPersistent_CHANGED_STATE;
} }
return 0; return 0;
......
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