diff --git a/product/ERP5Type/TransactionalVariable.py b/product/ERP5Type/TransactionalVariable.py index c7a1443cb918c53cb0286ed9b1d9ac441b7c3df9..aa7899b8193b408f578de7e811a871a45b92a2a6 100644 --- a/product/ERP5Type/TransactionalVariable.py +++ b/product/ERP5Type/TransactionalVariable.py @@ -56,33 +56,49 @@ Example:: """ import warnings -from UserDict import IterableUserDict -from Shared.DC.ZRDB.TM import TM from threading import local +from transaction import get as get_transaction import transaction.interfaces import zope.interface -class TransactionalVariable(TM, IterableUserDict): +class TransactionalVariable(dict): """TransactionalVariable provides a dict-like look-n-feel. This class must not be used directly outside. """ - _finalize = None + zope.interface.implements(transaction.interfaces.IDataManager) - def _begin(self, *ignored): - pass + _unregistered = True - def _finish(self, *ignored): - self.clear() + def sortKey(self): + return None - def _abort(self, *ignored): + commit = tpc_vote = tpc_begin = tpc_abort = lambda self, transaction: None + + def abort(self, txn): + self._unregistered = True self.clear() - def __hash__(self): - return hash(id(self)) + tpc_finish = abort + + # override all methods that may add entries to the dict def __setitem__(self, key, value): - IterableUserDict.__setitem__(self, key, value) - self._register() + if self._unregistered: + get_transaction().join(self) + self._unregistered = False + return dict.__setitem__(self, key, value) + + def setdefault(self, key, failobj=None): + if self._unregistered: + get_transaction().join(self) + self._unregistered = False + return dict.setdefault(self, key, failobj) + + def update(self, *args, **kw): + if self._unregistered: + get_transaction().join(self) + self._unregistered = False + return dict.update(self, *args, **kw) transactional_variable_pool = local() diff --git a/product/ERP5Type/tests/testERP5TypeInterfaces.py b/product/ERP5Type/tests/testERP5TypeInterfaces.py index bb315b024e35de232f7eab6f699b17bac67979aa..27cef37e8ea9639312db3e9e0d1e6856015dc0a5 100644 --- a/product/ERP5Type/tests/testERP5TypeInterfaces.py +++ b/product/ERP5Type/tests/testERP5TypeInterfaces.py @@ -54,9 +54,11 @@ implements_tuple_list = [ class TestERP5TypeInterfaces(ERP5TypeTestCase): """Tests implementation of interfaces""" - def testTransactionalResource(self): - from Products.ERP5Type.TransactionalVariable import TransactionalResource + def testTransactionIDataManager(self): + from Products.ERP5Type.TransactionalVariable import \ + TransactionalVariable, TransactionalResource from transaction.interfaces import IDataManager + verifyClass(IDataManager, TransactionalVariable) verifyClass(IDataManager, TransactionalResource) def makeTestMethod(import_tuple, interface):