Repository: ipython/ipython

Issue:
ipython 7.11.0 breaks Cython's IpythonMagic
Removing `cast_bytes_py2` in #12009 breaks [Cython's IpythonMagic](https://github.com/cython/cython/blob/master/Cython/Build/IpythonMagic.py#L410):

```
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-84ee719fa1da> in <module>
----> 1 get_ipython().run_cell_magic('cython', '', '\ndef tmp():\n    return 0\n')

X:\Python37\lib\site-packages\IPython\core\interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2350             with self.builtin_trap:
   2351                 args = (magic_arg_s, cell)
-> 2352                 result = fn(*args, **kwargs)
   2353             return result
   2354 

<X:\Python37\lib\site-packages\decorator.py:decorator-gen-159> in cython(self, line, cell)

X:\Python37\lib\site-packages\IPython\core\magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

X:\Python37\lib\site-packages\Cython\Build\IpythonMagic.py in cython(self, line, cell)
    322         extension = None
    323         if need_cythonize:
--> 324             extensions = self._cythonize(module_name, code, lib_dir, args, quiet=args.quiet)
    325             if extensions is None:
    326                 # Compilation failed and printed error message

X:\Python37\lib\site-packages\Cython\Build\IpythonMagic.py in _cythonize(self, module_name, code, lib_dir, args, quiet)
    407     def _cythonize(self, module_name, code, lib_dir, args, quiet=True):
    408         pyx_file = os.path.join(lib_dir, module_name + '.pyx')
--> 409         pyx_file = py3compat.cast_bytes_py2(pyx_file, encoding=sys.getfilesystemencoding())
    410 
    411         c_include_dirs = args.include

AttributeError: module 'IPython.utils.py3compat' has no attribute 'cast_bytes_py2'
```


Discussion:
That last line shows that this is a Cython bug.

Also, it's a really easy fix. All they have to do is delete the line. `os.path.join` returns a string and then they use it as an arg to `io.open`.
> That last line shows that this is a Cython bug.

Sorry, I don't follow. I can't find in the IPython's whatsnew log that/when IPython.utils.py3compat was deprecated.

> All they have to do is delete the line.

Cython still supports Python 2.
Reopening, because Cython is widely used I might reintroduced this just for them for a few releases.

All of these functions were deprecated in April 2017 when we released IPython 6 that  dropped Python 2. We kept them around to give time for people to migrate. They are also highly IPython specific and usually cython have vendored fallback for these.

Resolving patch:
diff --git a/IPython/utils/py3compat.py b/IPython/utils/py3compat.py
--- a/IPython/utils/py3compat.py
+++ b/IPython/utils/py3compat.py
@@ -185,3 +185,10 @@ def u_format(s):
 PY2 = not PY3
 PYPY = platform.python_implementation() == "PyPy"
 
+# Cython still rely on that as a Dec 28 2019
+# See https://github.com/cython/cython/pull/3291 and
+# https://github.com/ipython/ipython/issues/12068
+def no_code(x, encoding=None):
+        return x
+unicode_to_str = cast_bytes_py2 = no_code
+


Test patch: