[pypy-issue] Issue #2653: __import__ cannot be overwritten in exec (pypy/pypy)

Michael Howitz issues-reply at bitbucket.org
Wed Sep 13 07:52:43 EDT 2017

New issue 2653: __import__ cannot be overwritten in exec

Michael Howitz:

Given the following code:


code = compile('import os', '<string>', 'exec')
glb = {'__builtins__': {'__import__': lambda *a: 42}}

exec(code, glb)


PyPy behaves differently from CPython:

* Python 2.7.13 prints `42`
* Python 3.6.2 prints `42`
* PyPy 5.8.0 prints `<module 'os' from '/opt/local/lib/pypy/lib-python/2.7/os.py'>`
* PyPy3 5.5.0 prints `<module 'os' from '/Users/mac/python/pypy3-v5.5.0-osx64/lib-python/3/os.py'>`

This prevents using a custom `__import__` function.

More information about the pypy-issue mailing list