This repository has been archived by the owner on May 13, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Patch pymongo a bit, so that the hash of the DBRef is not computed all
the time but cached. DBRef's are generally considered immutable.
- Loading branch information
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
# Make a package. | ||
|
||
from . import pymongo | ||
pymongo.patch() | ||
del pymongo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
############################################################################## | ||
# | ||
# Copyright (c) 2011 Zope Foundation and Contributors. | ||
# All Rights Reserved. | ||
# | ||
# This software is subject to the provisions of the Zope Public License, | ||
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. | ||
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED | ||
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS | ||
# FOR A PARTICULAR PURPOSE. | ||
# | ||
############################################################################## | ||
"""PyMongo Patches""" | ||
|
||
def lazy_hash(self): | ||
"""Get a hash value for this :class:`DBRef`. | ||
.. versionadded:: 1.1 | ||
""" | ||
if self.__hash is None: | ||
self.__hash = self.orig_hash() | ||
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 |