Skip to content

Commit

Permalink
Fix find_globals to handle Python 2->3 module renamings
Browse files Browse the repository at this point in the history
Stdlib's _compat_pickle is a big help here, but I've an uneasy feeling
it's not supposed to be a public API.
  • Loading branch information
mgedmin committed Feb 20, 2013
1 parent abad361 commit 433a00e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 18 additions & 3 deletions src/ZODB/broken.py
Expand Up @@ -12,8 +12,6 @@
#
##############################################################################
"""Broken object support
$Id$
"""

import sys
Expand All @@ -23,6 +21,13 @@

import ZODB.interfaces

try:
# Python 3
import _compat_pickle
except ImportError:
# Python 2
_compat_pickle = None

broken_cache = {}

@zope.interface.implementer(ZODB.interfaces.IBroken)
Expand All @@ -34,7 +39,7 @@ class Broken(object):
Broken objects don't really do much of anything, except hold their
state. The Broken class is used as a base class for creating
classes in leu of missing classes::
classes in lieu of missing classes::
>>> Atall = type('Atall', (Broken, ), {'__module__': 'not.there'})
Expand Down Expand Up @@ -145,6 +150,9 @@ def find_global(modulename, globalname,
>>> find_global('sys', 'path') is sys.path
True
>>> find_global('__builtin__', 'object') is object
True
If an object can't be found, a broken class is returned::
>>> broken = find_global('ZODB.not.there', 'atall')
Expand Down Expand Up @@ -188,6 +196,13 @@ def find_global(modulename, globalname,
>>> broken_cache.clear()
"""

if _compat_pickle is not None:
if (modulename, globalname) in _compat_pickle.NAME_MAPPING:
modulename, globalname = _compat_pickle.NAME_MAPPING[
(modulename, globalname)]
if modulename in _compat_pickle.IMPORT_MAPPING:
modulename = _compat_pickle.IMPORT_MAPPING[modulename]

# short circuit common case:
try:
return getattr(sys.modules[modulename], globalname)
Expand Down
2 changes: 1 addition & 1 deletion src/ZODB/persistentclass.txt
Expand Up @@ -273,7 +273,7 @@ If we copy an instance via export/import, the copy and the original
share the same class:

>>> file = connection.exportFile(p._p_oid)
>>> file.seek(0)
>>> _ = file.seek(0)
>>> cp = connection.importFile(file)
>>> file.close()
>>> cp.color
Expand Down

0 comments on commit 433a00e

Please sign in to comment.