Should importlib.reload restore a deleted attribute in Python 3.6?

风流意气都作罢 提交于 2019-11-29 14:51:08

I believe this is an (intended? unintended?) effect of PEP 489, an overhaul of extension module initialization. The PEP includes the following section:

Module Reloading

Reloading an extension module using importlib.reload() will continue to have no effect, except re-setting import-related attributes.

Due to limitations in shared library loading (both dlopen on POSIX and LoadModuleEx on Windows), it is not generally possible to load a modified library after it has changed on disk.

Use cases for reloading other than trying out a new version of the module are too rare to require all module authors to keep reloading in mind. If reload-like functionality is needed, authors can export a dedicated function for it.

The code change that appears to be responsible for this behavior was introduced in the commit that implemented PEP 489.

Even Python 3.4 didn't support truly reloading an extension module from a changed file; the closest it had was code to save a copy of the module's dict after initialization and copy the contents back into the module's actual dict on a reload. That code still exists, but it is no longer triggered for reloads, and I don't know if it was ever intended to be triggered for reloads. I believe that code is currently only used for subinterpreters.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!