[Zope-Checkins] SVN: Zope/branches/Zope-2_8-branch/ Merged r40536 from 2.9 branch:

Florent Guillaume fg at nuxeo.com
Mon Dec 5 10:23:20 EST 2005


Log message for revision 40542:
  Merged r40536 from 2.9 branch:
  ObjectManager now has an hasObject method to test presence. This
  brings it in line with BTreeFolder.
  

Changed:
  U   Zope/branches/Zope-2_8-branch/doc/CHANGES.txt
  U   Zope/branches/Zope-2_8-branch/lib/python/OFS/ObjectManager.py
  U   Zope/branches/Zope-2_8-branch/lib/python/OFS/tests/testObjectManager.py

-=-
Modified: Zope/branches/Zope-2_8-branch/doc/CHANGES.txt
===================================================================
--- Zope/branches/Zope-2_8-branch/doc/CHANGES.txt	2005-12-05 15:15:05 UTC (rev 40541)
+++ Zope/branches/Zope-2_8-branch/doc/CHANGES.txt	2005-12-05 15:23:20 UTC (rev 40542)
@@ -58,6 +58,9 @@
 
     Other
 
+      - ObjectManager now has an hasObject method to test presence. This
+        brings it in line with BTreeFolder.
+
       - Made 'zopectl test' work for software homes which do not have
         an "inplace" build (it used to require that test.py be in
         $ZOPE_HOME/bin/;  now it will use $ZOPE_HOME as a fallback).

Modified: Zope/branches/Zope-2_8-branch/lib/python/OFS/ObjectManager.py
===================================================================
--- Zope/branches/Zope-2_8-branch/lib/python/OFS/ObjectManager.py	2005-12-05 15:15:05 UTC (rev 40541)
+++ Zope/branches/Zope-2_8-branch/lib/python/OFS/ObjectManager.py	2005-12-05 15:23:20 UTC (rev 40542)
@@ -261,6 +261,20 @@
             raise AttributeError, id
         return default
 
+    def hasObject(self, id):
+        """Indicate whether the folder has an item by ID.
+
+        This doesn't try to be more intelligent than _getOb, and doesn't
+        consult _objects (for performance reasons). The common use case
+        is to check that an object does *not* exist.
+        """
+        if (id in ('.', '..') or
+            id.startswith('_') or
+            id.startswith('aq_') or
+            id.endswith('__')):
+            return False
+        return getattr(aq_base(self), id, None) is not None
+
     def _setObject(self, id, object, roles=None, user=None, set_owner=1):
         v=self._checkId(id)
         if v is not None: id=v

Modified: Zope/branches/Zope-2_8-branch/lib/python/OFS/tests/testObjectManager.py
===================================================================
--- Zope/branches/Zope-2_8-branch/lib/python/OFS/tests/testObjectManager.py	2005-12-05 15:15:05 UTC (rev 40541)
+++ Zope/branches/Zope-2_8-branch/lib/python/OFS/tests/testObjectManager.py	2005-12-05 15:23:20 UTC (rev 40542)
@@ -303,6 +303,23 @@
         om2._setObject(ob.getId(), ob)
         self.assertRaises(DeleteFailed, om1._delObject, 'om2')
 
+    def test_hasObject(self):
+        om = self._makeOne()
+        self.failIf(om.hasObject('_properties'))
+        self.failIf(om.hasObject('_getOb'))
+        self.failIf(om.hasObject('__of__'))
+        self.failIf(om.hasObject('.'))
+        self.failIf(om.hasObject('..'))
+        self.failIf(om.hasObject('aq_base'))
+        om.zap__ = True
+        self.failIf(om.hasObject('zap__'))
+        self.failIf(om.hasObject('foo'))
+        si = SimpleItem('foo')
+        om._setObject('foo', si)
+        self.assert_(om.hasObject('foo'))
+        om._delObject('foo')
+        self.failIf(om.hasObject('foo'))
+
     def test_setObject_checkId_ok(self):
         om = self._makeOne()
         si = SimpleItem('1')



More information about the Zope-Checkins mailing list