Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
importlib.reload() does not re-execute module-level code #2659
Module-level code (e.g. attribute initialization) is not re-executed when re-importing a module through importlib.reload().
Given these classes:
Output before cythonizing is:
Output after cythonizing is:
The importlib documentation at https://docs.python.org/3/library/importlib.html specifies:
In the case of a cythonized module, recompiling the code is obviously not necessary; however, the module-level code should be re-executed to retain the runtime semantics.
In my case, there is a unit test; each test method reads and modifies a module-level attribute. In the setUp() method, I reload the module so that the test methods don't influence each other. This unit test fails after cythonizing the module.
Used compiler directive: "always_allow_keywords": True
Workaround: Create a "reinit()" method which does the same as the module-level code; call that method instead of reloading the module. This workaround is not very elegant, as it forces me to change my module code just to make it unit-testable.