Commit 433a00ef authored by Marius Gedminas's avatar Marius Gedminas

Fix find_globals to handle Python 2->3 module renamings

Stdlib's _compat_pickle is a big help here, but I've an uneasy feeling
it's not supposed to be a public API.
parent abad361f
......@@ -12,8 +12,6 @@
#
##############################################################################
"""Broken object support
$Id$
"""
import sys
......@@ -23,6 +21,13 @@ import zope.interface
import ZODB.interfaces
try:
# Python 3
import _compat_pickle
except ImportError:
# Python 2
_compat_pickle = None
broken_cache = {}
@zope.interface.implementer(ZODB.interfaces.IBroken)
......@@ -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'})
......@@ -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')
......@@ -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)
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment