[Checkins] SVN: mongopersist/trunk/src/mongopersist/pymongo.py The BSON c-code references the bson.dbref.DBRef class directly and that

Stephen Richter cvs-admin at zope.org
Mon Apr 9 00:41:30 UTC 2012


Log message for revision 125114:
  The BSON c-code references the bson.dbref.DBRef class directly and that 
  reference cannot be replaced. So we are doing our best to overwrite as 
  much as possible. The two features are:
  
  * Single, lazy computation of hash.
  
  * Disabling of the arbitrary attribute feature.
  
  The latter just makes several methods much faster, which really matters 
  when loading many objects (2-4%).
  
  

Changed:
  U   mongopersist/trunk/src/mongopersist/pymongo.py

-=-
Modified: mongopersist/trunk/src/mongopersist/pymongo.py
===================================================================
--- mongopersist/trunk/src/mongopersist/pymongo.py	2012-04-09 00:35:56 UTC (rev 125113)
+++ mongopersist/trunk/src/mongopersist/pymongo.py	2012-04-09 00:41:27 UTC (rev 125114)
@@ -12,21 +12,29 @@
 #
 ##############################################################################
 """PyMongo Patches"""
+from __future__ import absolute_import
+from bson.son import SON
+from copy import deepcopy
 
-def lazy_hash(self):
-    """Get a hash value for this :class:`DBRef`.
+def DBRef__init__(self, collection, id, database=None, _extra=None):
+    self._DBRef__collection = collection
+    self._DBRef__id = id
+    self._DBRef__database = database
+    self._DBRef__kwargs = {}
+    self._hash = None
 
-    .. versionadded:: 1.1
-    """
-    if self.__hash is None:
-        self.__hash = self.orig_hash()
-    return self.__hash
+def DBRef__hash__(self):
+    if self._hash is None:
+        self._hash = hash(
+            (self._DBRef__collection, self._DBRef__id, self._DBRef__database))
+    return self._hash
 
 def patch():
     # ObjectId should get patched too, but it is hard, since it uses slots
     # *and* rquires the original object reference to be around (otherwise it
     # creates BSON encoding errors.
     import bson.dbref
-    bson.dbref.DBRef.__hash = None
-    bson.dbref.DBRef.orig_hash = bson.dbref.DBRef.__hash__
-    bson.dbref.DBRef.__hash__ = lazy_hash
+    bson.dbref.DBRef.__init__ = DBRef__init__
+    bson.dbref.DBRef.__hash__ = DBRef__hash__
+    del bson.dbref.DBRef.__getattr__
+    del bson.dbref.DBRef.__setstate__



More information about the checkins mailing list