summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Behnel <stefan_ml@behnel.de>2020-01-01 15:32:49 +0100
committerStefan Behnel <stefan_ml@behnel.de>2020-01-01 15:32:49 +0100
commit6c902b75ba7295f79ac8a11994218c250dd02b5f (patch)
tree2f3fc25a8be62ac406d714898122151b8767f525
parenta3d3ab54c516b023d6f313ca1bbe8c219852298f (diff)
parentefa8d692e62b17c58ee40434fb2c126156bfec5a (diff)
downloadcython-ipython711_master.tar.gz
Merge branch '0.29.x' into ipython711_masteripython711_master
-rw-r--r--Cython/Build/IpythonMagic.py19
-rw-r--r--Cython/Build/Tests/TestIpythonMagic.py23
2 files changed, 22 insertions, 20 deletions
diff --git a/Cython/Build/IpythonMagic.py b/Cython/Build/IpythonMagic.py
index cca7b5cb1..7aa7bf666 100644
--- a/Cython/Build/IpythonMagic.py
+++ b/Cython/Build/IpythonMagic.py
@@ -56,6 +56,8 @@ import copy
import distutils.log
import textwrap
+IO_ENCODING = sys.getfilesystemencoding()
+IS_PY2 = sys.version_info[0] < 3
try:
from importlib import reload
@@ -69,7 +71,6 @@ from distutils.command.build_ext import build_ext
from IPython.core import display
from IPython.core import magic_arguments
from IPython.core.magic import Magics, magics_class, cell_magic
-from IPython.utils import py3compat
try:
from IPython.paths import get_ipython_cache_dir
except ImportError:
@@ -97,6 +98,14 @@ PGO_CONFIG = {
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
class CythonMagics(Magics):
@@ -307,7 +316,7 @@ class CythonMagics(Magics):
key += (time.time(),)
if args.name:
- module_name = py3compat.unicode_to_str(args.name)
+ module_name = str(args.name) # no-op in Py3
else:
module_name = "_cython_magic_" + hashlib.sha1(str(key).encode('utf-8')).hexdigest()
html_file = os.path.join(lib_dir, module_name + '.html')
@@ -407,7 +416,7 @@ class CythonMagics(Magics):
def _cythonize(self, module_name, code, lib_dir, args, quiet=True):
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_src_files = list(map(str, args.src))
@@ -526,10 +535,10 @@ class CythonMagics(Magics):
build_extension = _build_ext(dist)
build_extension.finalize_options()
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
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
if extension is not None:
build_extension.extensions = [extension]
diff --git a/Cython/Build/Tests/TestIpythonMagic.py b/Cython/Build/Tests/TestIpythonMagic.py
index 5d9462762..ed4db98cb 100644
--- a/Cython/Build/Tests/TestIpythonMagic.py
+++ b/Cython/Build/Tests/TestIpythonMagic.py
@@ -14,15 +14,8 @@ from Cython.Compiler.Annotate import AnnotationCCodeWriter
try:
import IPython.testing.globalipapp
- from IPython.utils import py3compat
except ImportError:
# 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(_):
return None
else:
@@ -36,24 +29,24 @@ try:
except ImportError:
pass
-code = py3compat.str_to_unicode("""\
+code = u"""\
def f(x):
return 2*x
-""")
+"""
-cython3_code = py3compat.str_to_unicode("""\
+cython3_code = u"""\
def f(int x):
return 2 / x
def call(x):
return f(*(x,))
-""")
+"""
-pgo_cython3_code = cython3_code + py3compat.str_to_unicode("""\
+pgo_cython3_code = cython3_code + u"""\
def main():
for _ in range(100): call(5)
main()
-""")
+"""
if sys.platform == 'win32':
@@ -162,10 +155,10 @@ class TestIPythonMagic(CythonTest):
@skip_win32('Skip on Windows')
def test_extlibs(self):
ip = self._ip
- code = py3compat.str_to_unicode("""
+ code = u"""
from libc.math cimport sin
x = sin(0.0)
- """)
+ """
ip.user_ns['x'] = 1
ip.run_cell_magic('cython', '-l m', code)
self.assertEqual(ip.user_ns['x'], 0)