cPickle vs pickle discrepancy

Carl Banks pavlovevidence at gmail.com
Thu Jan 8 14:34:59 EST 2009


On Jan 8, 12:27 pm, "Zac Burns" <zac... at gmail.com> wrote:
> Thanks for your patience waiting for me to isolate the problem.
>
> | Package
> --__init__.py ->empty
> --Package.py ->empty
> --Module.py
>      import cPickle
>      class C(object):
>         pass
>      def fail():
>         return cPickle.dumps(C(), -1)
>
> import Package.Module
> Package.Module.fail()
>
> The failure seems to happen because pickle correctly does an absolute
> import and cPickle incorrectly relatively imports Package.py and fails
> to find Module underneath.
>
> The package and the module named package was because in there was a
> main class defined with the same name as the package in that file and
> Module.py contained support code for the package.

I'd call it a bug.  Nice detecitve work pinpointing it.

I suspect the issue disappears (accidentally) in 2.6, though.  From
the What's New in Python 2.6 doc:

C API: the PyImport_Import and PyImport_ImportModule functions now
default to absolute imports, not relative imports. This will affect C
extensions that import other modules.

Presumably __import__ also defaults to absolute (since PyImport_Import
calls it) so the two modules should both use absolute imports in 2.6.


Carl Banks



More information about the Python-list mailing list