Commit fe3315ba authored by Stefan Behnel's avatar Stefan Behnel Committed by GitHub

Remove legacy imports from IPython integration since the "py3compat" module...

Remove legacy imports from IPython integration since the "py3compat" module changed in IPython 7.11. (GH-3297)
parent ada8dbd8
...@@ -56,6 +56,8 @@ import copy ...@@ -56,6 +56,8 @@ import copy
import distutils.log import distutils.log
import textwrap import textwrap
IO_ENCODING = sys.getfilesystemencoding()
IS_PY2 = sys.version_info[0] < 3
try: try:
reload reload
...@@ -73,7 +75,6 @@ from distutils.command.build_ext import build_ext ...@@ -73,7 +75,6 @@ from distutils.command.build_ext import build_ext
from IPython.core import display from IPython.core import display
from IPython.core import magic_arguments from IPython.core import magic_arguments
from IPython.core.magic import Magics, magics_class, cell_magic from IPython.core.magic import Magics, magics_class, cell_magic
from IPython.utils import py3compat
try: try:
from IPython.paths import get_ipython_cache_dir from IPython.paths import get_ipython_cache_dir
except ImportError: except ImportError:
...@@ -101,6 +102,14 @@ PGO_CONFIG = { ...@@ -101,6 +102,14 @@ PGO_CONFIG = {
PGO_CONFIG['mingw32'] = PGO_CONFIG['gcc'] PGO_CONFIG['mingw32'] = PGO_CONFIG['gcc']
if IS_PY2:
def encode_fs(name):
return name if isinstance(name, bytes) else name.encode(IO_ENCODING)
else:
def encode_fs(name):
return name
@magics_class @magics_class
class CythonMagics(Magics): class CythonMagics(Magics):
...@@ -306,7 +315,7 @@ class CythonMagics(Magics): ...@@ -306,7 +315,7 @@ class CythonMagics(Magics):
key += (time.time(),) key += (time.time(),)
if args.name: if args.name:
module_name = py3compat.unicode_to_str(args.name) module_name = str(args.name) # no-op in Py3
else: else:
module_name = "_cython_magic_" + hashlib.md5(str(key).encode('utf-8')).hexdigest() module_name = "_cython_magic_" + hashlib.md5(str(key).encode('utf-8')).hexdigest()
html_file = os.path.join(lib_dir, module_name + '.html') html_file = os.path.join(lib_dir, module_name + '.html')
...@@ -406,7 +415,7 @@ class CythonMagics(Magics): ...@@ -406,7 +415,7 @@ class CythonMagics(Magics):
def _cythonize(self, module_name, code, lib_dir, args, quiet=True): def _cythonize(self, module_name, code, lib_dir, args, quiet=True):
pyx_file = os.path.join(lib_dir, module_name + '.pyx') pyx_file = os.path.join(lib_dir, module_name + '.pyx')
pyx_file = py3compat.cast_bytes_py2(pyx_file, encoding=sys.getfilesystemencoding()) pyx_file = encode_fs(pyx_file)
c_include_dirs = args.include c_include_dirs = args.include
c_src_files = list(map(str, args.src)) c_src_files = list(map(str, args.src))
...@@ -526,10 +535,10 @@ class CythonMagics(Magics): ...@@ -526,10 +535,10 @@ class CythonMagics(Magics):
build_extension = _build_ext(dist) build_extension = _build_ext(dist)
build_extension.finalize_options() build_extension.finalize_options()
if temp_dir: if temp_dir:
temp_dir = py3compat.cast_bytes_py2(temp_dir, encoding=sys.getfilesystemencoding()) temp_dir = encode_fs(temp_dir)
build_extension.build_temp = temp_dir build_extension.build_temp = temp_dir
if lib_dir: if lib_dir:
lib_dir = py3compat.cast_bytes_py2(lib_dir, encoding=sys.getfilesystemencoding()) lib_dir = encode_fs(lib_dir)
build_extension.build_lib = lib_dir build_extension.build_lib = lib_dir
if extension is not None: if extension is not None:
build_extension.extensions = [extension] build_extension.extensions = [extension]
......
...@@ -13,15 +13,8 @@ from Cython.TestUtils import CythonTest ...@@ -13,15 +13,8 @@ from Cython.TestUtils import CythonTest
try: try:
import IPython.testing.globalipapp import IPython.testing.globalipapp
from IPython.utils import py3compat
except ImportError: except ImportError:
# Disable tests and fake helpers for initialisation below. # Disable tests and fake helpers for initialisation below.
class _py3compat(object):
def str_to_unicode(self, s):
return s
py3compat = _py3compat()
def skip_if_not_installed(_): def skip_if_not_installed(_):
return None return None
else: else:
...@@ -35,24 +28,24 @@ try: ...@@ -35,24 +28,24 @@ try:
except ImportError: except ImportError:
pass pass
code = py3compat.str_to_unicode("""\ code = u"""\
def f(x): def f(x):
return 2*x return 2*x
""") """
cython3_code = py3compat.str_to_unicode("""\ cython3_code = u"""\
def f(int x): def f(int x):
return 2 / x return 2 / x
def call(x): def call(x):
return f(*(x,)) return f(*(x,))
""") """
pgo_cython3_code = cython3_code + py3compat.str_to_unicode("""\ pgo_cython3_code = cython3_code + u"""\
def main(): def main():
for _ in range(100): call(5) for _ in range(100): call(5)
main() main()
""") """
if sys.platform == 'win32': if sys.platform == 'win32':
...@@ -161,10 +154,10 @@ class TestIPythonMagic(CythonTest): ...@@ -161,10 +154,10 @@ class TestIPythonMagic(CythonTest):
@skip_win32('Skip on Windows') @skip_win32('Skip on Windows')
def test_extlibs(self): def test_extlibs(self):
ip = self._ip ip = self._ip
code = py3compat.str_to_unicode(""" code = u"""
from libc.math cimport sin from libc.math cimport sin
x = sin(0.0) x = sin(0.0)
""") """
ip.user_ns['x'] = 1 ip.user_ns['x'] = 1
ip.run_cell_magic('cython', '-l m', code) ip.run_cell_magic('cython', '-l m', code)
self.assertEqual(ip.user_ns['x'], 0) self.assertEqual(ip.user_ns['x'], 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment