[wingide-users] Pickling modules with `copy_reg`

cool-RR cool-rr at cool-rr.com
Fri Apr 16 14:04:19 MDT 2010


On Fri, Apr 16, 2010 at 8:48 PM, Wingware Support <support at wingware.com>wrote:

> 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.
>

It's needed in order to work with `multiprocessing`. If you have any ideas
on how I could avoid having to pickle these things, I'd be happy to hear
them.


> 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
>

I figured out an hack:

'''
import __builtin__
def __import__(*args, **kwargs):
    return __builtin__.__import__(*args, **kwargs)
'''

Then I use this custom `__import__` in the reducer. It's an ugly hack, and
only needed when debugging with Wing.

Thanks for your help,
Ram.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/wingide-users/attachments/20100416/cab3f7ea/attachment.html


More information about the wingide-users mailing list