Commit 7e798ef3 authored by iv's avatar iv

ERP5Workflow: PERF: improve ERP5PersistentMapping by using try/except

So that it will avoid checking a condition ('if self._object_dict is None')
which is very often false.
parent 13257f24
......@@ -6,13 +6,10 @@ from AccessControl import ClassSecurityInfo
from AccessControl import getSecurityManager
from App.special_dtml import DTMLFile
from zope.event import notify
from OFS.event import ObjectWillBeAddedEvent
from OFS.subscribers import compatibilityCall
from OFS.ObjectManager import BadRequestException
from zope.lifecycleevent import ObjectAddedEvent
from zope.lifecycleevent import ObjectRemovedEvent
from zope.container.contained import notifyContainerModified
from OFS.Folder import Folder
from Products.CMFCore.PortalFolder import PortalFolderBase
from Products.ERP5Type.Base import Base
......@@ -36,12 +33,14 @@ class ERP5PersistentMappingFolder(PortalFolderBase):
self._object_dict = PersistentMapping()
def __getattr__(self, name):
if self._object_dict is None:
raise AttributeError(name)
try:
return self._object_dict[name].__of__(self)
except KeyError:
raise AttributeError(name)
except TypeError:
if self._object_dict is None:
raise AttributeError(name)
raise
def _initPersistentObjects(self):
self._cleanup()
......@@ -55,14 +54,22 @@ class ERP5PersistentMappingFolder(PortalFolderBase):
return self._getOb(name)
def _setOb(self, object_id, object_value):
try:
self._object_dict[object_id] = object_value
except TypeError:
if self._object_dict is None:
self._object_dict = PersistentMapping()
self._object_dict[object_id] = object_value
else:
raise
def _delOb(self, object_id):
try:
del self._object_dict[object_id]
except TypeError:
if self._object_dict is None:
raise KeyError, object_id
del self._object_dict[object_id]
raise
def _getOb(self, object_id, default=_marker):
"""
......@@ -70,19 +77,20 @@ class ERP5PersistentMappingFolder(PortalFolderBase):
"""
try:
return self._object_dict[object_id].__of__(self)
except KeyError:
except KeyError, TypeError: # TypeError if _object_dict is None
if default is _marker:
raise
raise AttributeError(object_id)
else:
return default
def objectIds(self, spec=None, **kw):
# we don't plan to use meta_type now, so spec is just here for compatibility
assert(spec is None)
try:
return self._object_dict.keys()
except AttributeError:
if self._object_dict is None:
return []
return self._object_dict.keys()
raise
def objectCount(self):
return len(self._object_dict)
......@@ -108,10 +116,14 @@ class ERP5PersistentMappingFolder(PortalFolderBase):
# hack to get the portal type for a specified meta_type
portal_type = [meta_type[5:] for meta_type in spec
if meta_type.startswith('ERP5 ')]
try:
object_list = self._object_dict.values()
except AttributeError:
if self._object_dict is None:
return []
raise
object_list = self._object_dict.values()
if portal_type is not None:
if isinstance(portal_type, str):
portal_type = (portal_type,)
......@@ -158,6 +170,7 @@ class ERP5PersistentMappingFolder(PortalFolderBase):
if userid is not None:
ob.manage_setLocalRoles(userid, ['Owner'])
ob.manage_afterAdd(ob, self)
return id
def _cleanup(self):
......@@ -174,13 +187,12 @@ class ERP5PersistentMappingFolder(PortalFolderBase):
return cleaned
def has_key(self, key):
if (key in ('.', '..') or
key.startswith('_') or
key.startswith('aq_') or
key.endswith('__') or
self._object_dict is None):
try:
return self._object_dict.has_key(key)
except AttributeError:
if self._object_dict is None:
return False
return self._object_dict.get(key, None) is not None
raise
hasObject = has_key
......
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