[pypy-issue] Issue #3099: "TypeError: reload() argument must be module" when object is instance of types.ModuleType child class (pypy/pypy)

Kirill Smelkov issues-reply at bitbucket.org
Thu Oct 17 14:24:38 EDT 2019


New issue 3099: "TypeError: reload() argument must be module" when object is instance of types.ModuleType child class
https://bitbucket.org/pypy/pypy/issues/3099/typeerror-reload-argument-must-be-module

Kirill Smelkov:

Hello up there. I’ve hit another compatibility bug compared to CPython2.7 when using lazy module imports \(either [https://pypi.org/project/Importing/](https://pypi.org/project/Importing/) or [https://pypi.org/project/lazy-import/](https://pypi.org/project/lazy-import/)\). Please consider the following minimal example which demonstrates the problem:

\(mymod.py\)

```
print 'mymod loaded'

def test():
    print 'mymod.test'
```

\(test.py\)

```python
import types
import sys

#MyModType = types.ModuleType
class MyModType(types.ModuleType):
    pass

x_mymod = MyModType('mymod')
sys.modules['mymod'] = x_mymod

import mymod
assert mymod is x_mymod

print 'reload ...'
reload(mymod)
print 'access ...'
mymod.test()
```

When run with CPython 2.7 `reload(mymod)` works ok:

```
$ /usr/bin/python --version
Python 2.7.16
$ /usr/bin/python test.py 
reload ...
mymod loaded
access ...
mymod.test
```

However when run with PyPy 2.7-7.2 it breaks on the `reload`:

```
$ pypy --version
Python 2.7.13 (4a68d8d3d2fc, Oct 10 2019, 06:43:13)
[PyPy 7.2.0 with GCC 6.2.0 20160901]
$ pypy test.py 
reload ...
Traceback (most recent call last):
  File "test.py", line 15, in <module>
    reload(mymod)
TypeError: reload() argument must be module
```

I use `peak.utils.imports.whenImported(X)` to automatically apply enhancements my library comes with, if/when third-party software is used. It is a pity that it breaks with PyPy.

Thanks beforehand,  
Kirill




More information about the pypy-issue mailing list