From 79129bb6b0e6488b581046cf95c0a08eb9940b84 Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Mon, 1 Jul 2019 17:19:55 -0500 Subject: [PATCH 1/2] py_codegen: Fix deprecated use of imp module. Also, this changes an error message to use an '%r' format string for reporting the bytecode version magic, to avoid printing unreadable characters. --- pytools/py_codegen.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/pytools/py_codegen.py b/pytools/py_codegen.py index 97aef19..4f43291 100644 --- a/pytools/py_codegen.py +++ b/pytools/py_codegen.py @@ -23,12 +23,19 @@ THE SOFTWARE. """ import marshal -import imp from types import FunctionType, ModuleType import six +try: + from importlib.util import MAGIC_NUMBER as BYTECODE_VERSION +except ImportError: + # Pre-3.4 + import imp + BYTECODE_VERSION = imp.get_magic() + + # loosely based on # http://effbot.org/zone/python-code-generator.htm @@ -140,7 +147,7 @@ class PicklableModule(object): elif k not in _empty_module_dict: nondefault_globals[k] = v - return (1, imp.get_magic(), functions, modules, nondefault_globals) + return (1, BYTECODE_VERSION, functions, modules, nondefault_globals) def __setstate__(self, obj): if obj[0] == 0: @@ -151,10 +158,10 @@ class PicklableModule(object): else: raise ValueError("unknown version of PicklableModule") - if magic != imp.get_magic(): + if magic != BYTECODE_VERSION: raise ValueError("cannot unpickle function binary: " - "incorrect magic value (got: %s, expected: %s)" - % (magic, imp.get_magic())) + "incorrect magic value (got: '%r', expected: '%r')" + % (magic, BYTECODE_VERSION)) mod_globals = _empty_module_dict.copy() mod_globals.update(nondefault_globals) -- GitLab From 65a9d22ed9163a9b8f9120ee30f7256230d150dd Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Mon, 1 Jul 2019 17:30:34 -0500 Subject: [PATCH 2/2] Avoid double-quoting of %r-formatted string --- pytools/py_codegen.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytools/py_codegen.py b/pytools/py_codegen.py index 4f43291..e2a5144 100644 --- a/pytools/py_codegen.py +++ b/pytools/py_codegen.py @@ -160,7 +160,7 @@ class PicklableModule(object): if magic != BYTECODE_VERSION: raise ValueError("cannot unpickle function binary: " - "incorrect magic value (got: '%r', expected: '%r')" + "incorrect magic value (got: %r, expected: %r)" % (magic, BYTECODE_VERSION)) mod_globals = _empty_module_dict.copy() -- GitLab