Skip to content
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

Fix DeprecationWarning about imp module #3237

Merged
merged 2 commits into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions numba/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

from __future__ import print_function, division, absolute_import

import imp
from .utils import PYVERSION

# `imp` deprecated since Py3.4, use `importlib` as replacement since Py3.1
if PYVERSION < (3, 1):
from imp import get_magic as _get_magic
bc_magic = _get_magic()
else:
from importlib.util import MAGIC_NUMBER as bc_magic

import marshal
import sys
from types import FunctionType, ModuleType
Expand Down Expand Up @@ -72,7 +80,7 @@ def _reduce_code(code):
"""
Reduce a code object to picklable components.
"""
return marshal.version, imp.get_magic(), marshal.dumps(code)
return marshal.version, bc_magic, marshal.dumps(code)

def _dummy_closure(x):
"""
Expand Down Expand Up @@ -106,7 +114,7 @@ def _rebuild_code(marshal_version, bytecode_magic, marshalled):
raise RuntimeError("incompatible marshal version: "
"interpreter has %r, marshalled code has %r"
% (marshal.version, marshal_version))
if imp.get_magic() != bytecode_magic:
if bc_magic != bytecode_magic:
raise RuntimeError("incompatible bytecode version")
return marshal.loads(marshalled)

20 changes: 20 additions & 0 deletions numba/tests/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,26 @@ def test_reuse(self):
g.disable_compile()
self.assertEqual(g(2, 4), 13)

def test_imp_deprecation(self):
"""
The imp module was deprecated in v3.4 in favour of importlib
"""
code = """if 1:
import pickle
import warnings
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always', DeprecationWarning)
from numba import njit
@njit
def foo(x):
return x + 1
foo(1)
serialized_foo = pickle.dumps(foo)
for x in w:
if 'serialize.py' in x.filename:
assert "the imp module is deprecated" not in x.msg
"""
subprocess.check_call([sys.executable, "-c", code])

if __name__ == '__main__':
unittest.main()