jparise / python-reloader

Dependency-based Python Module Reloader
http://www.indelible.org/ink/python-reloading/
MIT License
135 stars 27 forks source link

Can't reload module that has __reload__ defined #23

Open overfl0 opened 7 years ago

overfl0 commented 7 years ago

Hello, I'm having an issue when I'm trying to reload a module that has the __reload__ function defines. I've narrowed the problem to even just having `logging' imported:

module.py:

import logging
a = 42
def __reload__(state):
    pass

Run.py:

import reloader
reloader.enable(blacklist=['logging'])  # It's pointless here, isn't it?

import module
reloader.reload(module)

I think the issue seems to be with deepcopying the logginng module. Should it also be deleted in _deepcopy_module_dict() as you're doing with __builtins__? (just a wild guess)

The errors seem to differ each time I run run.py. I'm getting one of those on python 3.5: TypeError: cannot serialize '_io.TextIOWrapper' object AttributeError: 'NoneType' object has no attribute 'update' TypeError: cannot create 'sys.version_info' instances TypeError: cannot create 'sys.flags' instances TypeError: can't pickle _thread.lock objects

Example stacktrace:

Traceback (most recent call last):
  File "run.py", line 5, in <module>
    reloader.reload(module)
  File "C:\Python35-x64\lib\site-packages\reloader.py", line 146, in reload
    _reload(m, set())
  File "C:\Python35-x64\lib\site-packages\reloader.py", line 130, in _reload
    d = _deepcopy_module_dict(m)
  File "C:\Python35-x64\lib\site-packages\reloader.py", line 87, in _deepcopy_module_dict
    return copy.deepcopy(d)
  File "C:\Python35-x64\lib\copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "C:\Python35-x64\lib\copy.py", line 243, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Python35-x64\lib\copy.py", line 182, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "C:\Python35-x64\lib\copy.py", line 297, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\Python35-x64\lib\copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "C:\Python35-x64\lib\copy.py", line 243, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Python35-x64\lib\copy.py", line 182, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "C:\Python35-x64\lib\copy.py", line 306, in _reconstruct
    y.__dict__.update(state)
AttributeError: 'NoneType' object has no attribute 'update'
jparise commented 7 years ago

It's been quite some time since I looked at this code.

The "deep copy" here allows the __reload__() function to receive a copy of the original module that can be used to restore state. I can't remember why I made it a "deep" copy (imported modules and all) rather than a "shallow" copy, which could provide just the top-level attributes. Perhaps the latter doesn't work if those attributes references imported types?

That being said, it seems reasonable to specifically exclude logging from the deep copy if that's the true source of your problem, but I'd also like to find a more general solution before closing out this issue.