New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

importlib.reload() does not re-execute module-level code #2659

Open
DanielHerding opened this Issue Oct 15, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@DanielHerding

DanielHerding commented Oct 15, 2018

Module-level code (e.g. attribute initialization) is not re-executed when re-importing a module through importlib.reload().

Given these classes:
test1.py:

print("Module test1 loading...")

my_attribute = "<Default value>"

test2.py:

import sys
sys.path.append('.')

import test1
import importlib

print(test1.my_attribute)

test1.my_attribute = "<Modified value>"
print(test1.my_attribute)

importlib.reload(test1)
print(test1.my_attribute)

Output before cythonizing is:

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
>>> import test2
Module test1 loading...
<Default value>
<Modified value>
Module test1 loading...
<Default value>

Output after cythonizing is:

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
>>> import test2
Module test1 loading...
<Default value>
<Modified value>
<Modified value>

The importlib documentation at https://docs.python.org/3/library/importlib.html specifies:

When reload() is executed:
Python module’s code is recompiled and the module-level code re-executed, defining a new set of objects which are bound to names in the module’s dictionary by reusing the loader which originally loaded the module.

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
Used Cython version 0.28.5 on Windows 7

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.

@scoder scoder added R: duplicate and removed R: duplicate labels Oct 15, 2018

@scoder

This comment has been minimized.

Contributor

scoder commented Oct 15, 2018

See #1919.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment