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:: ...@@ -56,33 +56,49 @@ Example::
""" """
import warnings import warnings
from UserDict import IterableUserDict
from Shared.DC.ZRDB.TM import TM
from threading import local from threading import local
from transaction import get as get_transaction
import transaction.interfaces import transaction.interfaces
import zope.interface import zope.interface
class TransactionalVariable(TM, IterableUserDict): class TransactionalVariable(dict):
"""TransactionalVariable provides a dict-like look-n-feel. """TransactionalVariable provides a dict-like look-n-feel.
This class must not be used directly outside. This class must not be used directly outside.
""" """
_finalize = None zope.interface.implements(transaction.interfaces.IDataManager)
def _begin(self, *ignored): _unregistered = True
pass
def _finish(self, *ignored): def sortKey(self):
self.clear() 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() self.clear()
def __hash__(self): tpc_finish = abort
return hash(id(self))
# override all methods that may add entries to the dict
def __setitem__(self, key, value): def __setitem__(self, key, value):
IterableUserDict.__setitem__(self, key, value) if self._unregistered:
self._register() 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() transactional_variable_pool = local()
......
...@@ -54,9 +54,11 @@ implements_tuple_list = [ ...@@ -54,9 +54,11 @@ implements_tuple_list = [
class TestERP5TypeInterfaces(ERP5TypeTestCase): class TestERP5TypeInterfaces(ERP5TypeTestCase):
"""Tests implementation of interfaces""" """Tests implementation of interfaces"""
def testTransactionalResource(self): def testTransactionIDataManager(self):
from Products.ERP5Type.TransactionalVariable import TransactionalResource from Products.ERP5Type.TransactionalVariable import \
TransactionalVariable, TransactionalResource
from transaction.interfaces import IDataManager from transaction.interfaces import IDataManager
verifyClass(IDataManager, TransactionalVariable)
verifyClass(IDataManager, TransactionalResource) verifyClass(IDataManager, TransactionalResource)
def makeTestMethod(import_tuple, interface): 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