[wingide-users] Pickling modules with `copy_reg`

Wingware Support support at wingware.com
Fri Apr 16 12:48:55 MDT 2010


cool-RR wrote:
> I am trying to use the `copy_reg` module to teach the pickle module 
> how to pickle modules:
>
> '''
> def reduce_module(module):
>     return (__import__, (module.__name__, {}, {}, ['']))
>
> copy_reg.pickle(types.ModuleType, reduce_module)
> '''
>
> This works when I run it normally, but when I run it under debug in 
> Wing, I get:
>
> '''pickle.PicklingError: Can't pickle <built-in function 
> wingdb_import_hook>: it's not found as tdbgtracer26.wingdb_import_hook'''
>
> I think what happens is that Python is able to pickle `__import__`, 
> but not the hook that replaces it in the debugger.
>
> What should I do?

The replacement import hook is in a C extension module, so can't be 
pickled.  I'm not certain
the idea of pickling modules makes that much sense and pickling code is 
in general
potentially problematic across versions and/or from a security standpoint.

But a possible solution would be to store the value of __import__ at 
startup and temporarily
restore it during the pickle operation.  However, this would only work 
if you use wingdbstub
instead of launching debug from the IDE, since in the latter case 
__import__ would already
be set before your code gains control (you would need to put the import 
wingdbstub after
the code that saves __import__). There may also be a way to get at the 
original/builtin import
hook but I'm finding it at the moment.

-- 

Stephan Deibel
Wingware | Python IDE
Advancing Software Development

www.wingware.com



More information about the wingide-users mailing list