Update of /cvs-repository/Packages/ZODB
In directory cvs.zope.org:/tmp/cvs-serv32028/lib/python/ZODB

Modified Files:
      Tag: tseaver-hasattr_geddon-branch
        Connection.py DB.py ZApplication.py utils.py 
Log Message:

  - Removed all uses of the 'hasattr' builtin from the core, where
    the object being tested derives (or might) from Persistent.
    XXX:  currently, this branch imports a 'safe_hasattr' from ZODB.utils,
    which adds a dependency on ZODB for some packages;  we probably
    need a better location, and perhas a C implementation?


=== Packages/ZODB/Connection.py 1.98.4.11 => 1.98.4.11.2.1 ===
--- Packages/ZODB/Connection.py:1.98.4.11       Mon May  2 16:30:27 2005
+++ Packages/ZODB/Connection.py Sat May 28 20:41:37 2005
@@ -32,6 +32,7 @@
 from ConflictResolution import ResolvedSerial
 from Transaction import Transaction, get_transaction
 from ZODB.utils import oid_repr
+from ZODB.utils import safe_hasattr
 
 global_code_timestamp = 0
 
@@ -182,7 +183,7 @@
             klass=self._db._classFactory(self, module, name)
 
         if (args is None or
-            not args and not hasattr(klass,'__getinitargs__')):
+            not args and not safe_hasattr(klass,'__getinitargs__')):
             object=klass.__basicnew__()
         else:
             object = klass(*args)
@@ -375,7 +376,7 @@
         # def persistent_id(object,
         #                   self=self,
         #                   stackup=stackup, new_oid=self.new_oid):
-        #     if (not hasattr(object, '_p_oid') or
+        #     if (not safe_hasattr(object, '_p_oid') or
         #         type(object) is ClassType): return None
         #
         #     oid=object._p_oid
@@ -390,7 +391,7 @@
         #
         #     if klass is ExtensionKlass: return oid
         #
-        #     if hasattr(klass, '__getinitargs__'): return oid
+        #     if safe_hasattr(klass, '__getinitargs__'): return oid
         #
         #     module=getattr(klass,'__module__','')
         #     if module: klass=module, klass.__name__
@@ -421,7 +422,8 @@
                 self._creating.append(oid)
             else:
                 #XXX We should never get here
-                if invalid(oid) and not hasattr(object, '_p_resolveConflict'):
+                if (invalid(oid)
+                    and not safe_hasattr(object, '_p_resolveConflict')):
                     raise ConflictError(object=object)
                 self._modified.append(oid)
 
@@ -435,7 +437,7 @@
                 args=object.__name__, object.__bases__, dict
                 state=None
             else:
-                if hasattr(klass, '__getinitargs__'):
+                if safe_hasattr(klass, '__getinitargs__'):
                     args = object.__getinitargs__()
                     len(args) # XXX Assert it's a sequence
                 else:
@@ -459,7 +461,7 @@
             try: cache[oid]=object
             except:
                 # Dang, I bet its wrapped:
-                if hasattr(object, 'aq_base'):
+                if safe_hasattr(object, 'aq_base'):
                     cache[oid]=object.aq_base
                 else:
                     raise


=== Packages/ZODB/DB.py 1.53.2.5 => 1.53.2.5.22.1 ===
--- Packages/ZODB/DB.py:1.53.2.5        Fri May 21 19:06:03 2004
+++ Packages/ZODB/DB.py Sat May 28 20:41:37 2005
@@ -23,6 +23,7 @@
 from referencesf import referencesf
 from time import time, ctime
 from zLOG import LOG, ERROR
+from ZODB.utils import safe_hasattr
 
 from types import StringType
 
@@ -79,7 +80,8 @@
         # Setup storage
         self._storage=storage
         storage.registerDB(self, None)
-        if not hasattr(storage,'tpc_vote'): storage.tpc_vote=lambda *args: None
+        if not hasattr(storage,'tpc_vote'):
+            storage.tpc_vote=lambda *args: None
         try:
             storage.load('\0\0\0\0\0\0\0\0','')
         except KeyError:
@@ -206,7 +208,7 @@
             cn = conn_no[0]
             for oid, ob in con._cache_items():
                 id = ''
-                if hasattr(ob, '__dict__'):
+                if safe_hasattr(ob, '__dict__'):
                     d = ob.__dict__
                     if d.has_key('id'):
                         id = d['id']


=== Packages/ZODB/ZApplication.py 1.13 => 1.13.88.1 ===
--- Packages/ZODB/ZApplication.py:1.13  Tue Apr  8 14:48:22 2003
+++ Packages/ZODB/ZApplication.py       Sat May 28 20:41:37 2005
@@ -18,6 +18,8 @@
 """
 __version__='$Revision$'[11:-2]
 
+from ZODB.utils import safe_hasattr
+
 StringType=type('')
 connection_open_hooks = []
 
@@ -60,10 +62,11 @@
         v=conn.root()[aname]
 
         if name is not None:
-            if hasattr(v, '__bobo_traverse__'):
+            if safe_hasattr(v, '__bobo_traverse__'):
                 return v.__bobo_traverse__(REQUEST, name)
 
-            if hasattr(v,name): return getattr(v,name)
+            if safe_hasattr(v,name):
+                return getattr(v,name)
             return v[name]
 
         return v


=== Packages/ZODB/utils.py 1.17.4.4 => 1.17.4.4.22.1 ===
--- Packages/ZODB/utils.py:1.17.4.4     Sat Sep  4 00:45:03 2004
+++ Packages/ZODB/utils.py      Sat May 28 20:41:37 2005
@@ -113,3 +113,13 @@
     if isinstance(tid, StringType) and len(tid) == 8:
         result = "%s %s" % (result, TimeStamp.TimeStamp(tid))
     return result
+
+def safe_hasattr(obj, name, _marker=object()):
+    """Make sure we don't mask exceptions like hasattr().
+
+    We don't want exceptions other than AttributeError to be masked,
+    since that too often masks other programming errors.
+    Three-argument getattr() doesn't mask those, so we use that to
+    implement our own hasattr() replacement.
+    """
+    return getattr(obj, name, _marker) is not _marker

_______________________________________________
Zope-Checkins maillist  -  Zope-Checkins@zope.org
http://mail.zope.org/mailman/listinfo/zope-checkins

Reply via email to