Commit cdae5066 authored by Julien Muchembled's avatar Julien Muchembled

Optimize getTransactionalVariable, and fix it for setdefault/update

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@45667 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent eababf60
......@@ -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()
......
......@@ -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):
......
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