We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
cython_magic uses importlib, which doesn't ship with py2.6
Sorry, I don't have time to fix this right now, but wanted to leave a report.

`importlib` was not in the standard library for python2.6, though [it has been backported](http://pypi.python.org/pypi/importlib/1.0.1).

Trying to run `%load_ext cythonmagic` results in this traceback:

```
/ipython/IPython/extensions/cythonmagic.py in <module>()
     18 import io
     19 import os, sys
---> 20 from importlib import import_module
     21 import imp
     22 

ImportError: No module named importlib
```

not sure if we should make people install it themselves, or if it's better to just put a copy in `IPython.external`


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `IPython/extensions/cythonmagic.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 """
3 Cython related magics.
4 
5 Author:
6 * Brian Granger
7 
8 Parts of this code were taken from Cython.inline.
9 """
10 #-----------------------------------------------------------------------------
11 # Copyright (C) 2010-2011, IPython Development Team.
12 #
13 # Distributed under the terms of the Modified BSD License.
14 #
15 # The full license is in the file COPYING.txt, distributed with this software.
16 #-----------------------------------------------------------------------------
17 
18 import io
19 import os, sys
20 from importlib import import_module
21 import imp
22 
23 try:
24     import hashlib
25 except ImportError:
26     import md5 as hashlib
27 
28 from distutils.core import Distribution, Extension
29 from distutils.command.build_ext import build_ext
30 
31 from IPython.core.magic import Magics, magics_class, cell_magic
32 from IPython.testing.skipdoctest import skip_doctest
33 from IPython.core.magic_arguments import (
34     argument, magic_arguments, parse_argstring
35 )
36 from IPython.utils import py3compat
37 
38 import Cython
39 from Cython.Compiler.Errors import CompileError
40 from Cython.Compiler.Main import Context, default_options
41 from Cython.Build.Dependencies import cythonize
42 
43 
44 @magics_class
45 class CythonMagics(Magics):
46 
47     def __init__(self, shell):
48         super(CythonMagics,self).__init__(shell)
49         self._reloads = {}
50         self._code_cache = {}
51 
52     def _import_all(self, module):
53         for k,v in module.__dict__.items():
54             if not k.startswith('__'):
55                 self.shell.push({k:v})
56 
57     @cell_magic
58     def cython_inline(self, line, cell):
59         """Compile and run a Cython code cell using Cython.inline.
60 
61         This magic simply passes the body of the cell to Cython.inline
62         and returns the result. If the variables `a` and `b` are defined
63         in the user's namespace, here is a simple example that returns
64         their sum::
65         
66             %%cython_inline
67             return a+b
68 
69         For most purposes, we recommend the usage of the `%%cython` magic.
70         """
71         locs = self.shell.user_global_ns
72         globs = self.shell.user_ns
73         return Cython.inline(cell, locals=locs, globals=globs)
74 
75     @cell_magic
76     def cython_pyximport(self, line, cell):
77         """Compile and import a Cython code cell using pyximport.
78 
79         The contents of the cell are written to a `.pyx` file in the current
80         working directory, which is then imported using `pyximport`. This
81         magic requires a module name to be passed::
82         
83             %%cython_pyximport modulename
84             def f(x):
85                 return 2.0*x
86 
87         The compiled module is then imported and all of its symbols are injected into
88         the user's namespace. For most purposes, we recommend the usage of the
89         `%%cython` magic.
90         """
91         module_name = line.strip()
92         if not module_name:
93             raise ValueError('module name must be given')
94         fname = module_name + '.pyx'
95         with io.open(fname, 'w', encoding='utf-8') as f:
96             f.write(cell)
97         if 'pyximport' not in sys.modules:
98             import pyximport
99             pyximport.install(reload_support=True)
100         if module_name in self._reloads:
101             module = self._reloads[module_name]
102             reload(module)
103         else:
104             module = import_module(module_name)
105             self._reloads[module_name] = module
106         self._import_all(module)
107 
108     @magic_arguments()
109     @argument(
110         '-f', '--force', action='store_true', default=False,
111         help="Force the compilation of the pyx module even if it hasn't changed"
112     )
113     @cell_magic
114     def cython(self, line, cell):
115         """Compile and import everything from a Cython code cell.
116 
117         The contents of the cell are written to a `.pyx` file in the
118         directory `IPYTHONDIR/cython` using a filename with the hash of the code.
119         This file is then cythonized and compiled. The resulting module
120         is imported and all of its symbols are injected into the user's
121         namespace. The usage is similar to that of `%%cython_pyximport` but
122         you don't have to pass a module name::
123 
124         %%cython
125         def f(x):
126             return 2.0*x
127         """
128         args = parse_argstring(self.cython, line)
129         code = cell if cell.endswith('\n') else cell+'\n'
130         lib_dir=os.path.join(self.shell.ipython_dir, 'cython')
131         cython_include_dirs=['.']
132         force=args.force
133         quiet=True
134         ctx = Context(cython_include_dirs, default_options)
135         key = code, sys.version_info, sys.executable, Cython.__version__
136         module_name = "_cython_magic_" + hashlib.md5(str(key).encode('utf-8')).hexdigest()
137         so_ext = [ ext for ext,_,mod_type in imp.get_suffixes() if mod_type == imp.C_EXTENSION ][0]
138         module_path = os.path.join(lib_dir, module_name+so_ext)
139 
140         if not os.path.exists(lib_dir):
141             os.makedirs(lib_dir)
142 
143         if force or not os.path.isfile(module_path):
144             cflags = []
145             c_include_dirs = []
146             if 'numpy' in code:
147                 import numpy
148                 c_include_dirs.append(numpy.get_include())
149             pyx_file = os.path.join(lib_dir, module_name + '.pyx')
150             pyx_file = py3compat.cast_bytes_py2(pyx_file, encoding=sys.getfilesystemencoding())
151             with io.open(pyx_file, 'w', encoding='utf-8') as f:
152                 f.write(code)
153             extension = Extension(
154                 name = module_name,
155                 sources = [pyx_file],
156                 include_dirs = c_include_dirs,
157                 extra_compile_args = cflags
158             )
159             dist = Distribution()
160             config_files = dist.find_config_files()
161             try: 
162                 config_files.remove('setup.cfg')
163             except ValueError:
164                 pass
165             dist.parse_config_files(config_files)
166             build_extension = build_ext(dist)
167             build_extension.finalize_options()
168             try:
169                 build_extension.extensions = cythonize([extension], ctx=ctx, quiet=quiet)
170             except CompileError:
171                 return
172             build_extension.build_temp = os.path.dirname(pyx_file)
173             build_extension.build_lib  = lib_dir
174             build_extension.run()
175             self._code_cache[key] = module_name
176 
177         module = imp.load_dynamic(module_name, module_path)
178         self._import_all(module)
179 
180 
181 _loaded = False
182 
183 def load_ipython_extension(ip):
184     """Load the extension in IPython."""
185     global _loaded
186     if not _loaded:
187         ip.register_magics(CythonMagics)
188         _loaded = True
189
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/IPython/extensions/cythonmagic.py b/IPython/extensions/cythonmagic.py
--- a/IPython/extensions/cythonmagic.py
+++ b/IPython/extensions/cythonmagic.py
@@ -17,7 +17,6 @@
 
 import io
 import os, sys
-from importlib import import_module
 import imp
 
 try:
@@ -101,7 +100,8 @@
             module = self._reloads[module_name]
             reload(module)
         else:
-            module = import_module(module_name)
+            __import__(module_name)
+            module = sys.modules[module_name]
             self._reloads[module_name] = module
         self._import_all(module)

{"golden_diff": "diff --git a/IPython/extensions/cythonmagic.py b/IPython/extensions/cythonmagic.py\n--- a/IPython/extensions/cythonmagic.py\n+++ b/IPython/extensions/cythonmagic.py\n@@ -17,7 +17,6 @@\n \n import io\n import os, sys\n-from importlib import import_module\n import imp\n \n try:\n@@ -101,7 +100,8 @@\n             module = self._reloads[module_name]\n             reload(module)\n         else:\n-            module = import_module(module_name)\n+            __import__(module_name)\n+            module = sys.modules[module_name]\n             self._reloads[module_name] = module\n         self._import_all(module)\n", "issue": "cython_magic uses importlib, which doesn't ship with py2.6\nSorry, I don't have time to fix this right now, but wanted to leave a report.\n\n`importlib` was not in the standard library for python2.6, though [it has been backported](http://pypi.python.org/pypi/importlib/1.0.1).\n\nTrying to run `%load_ext cythonmagic` results in this traceback:\n\n```\n/ipython/IPython/extensions/cythonmagic.py in <module>()\n     18 import io\n     19 import os, sys\n---> 20 from importlib import import_module\n     21 import imp\n     22 \n\nImportError: No module named importlib\n```\n\nnot sure if we should make people install it themselves, or if it's better to just put a copy in `IPython.external`\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n\"\"\"\nCython related magics.\n\nAuthor:\n* Brian Granger\n\nParts of this code were taken from Cython.inline.\n\"\"\"\n#-----------------------------------------------------------------------------\n# Copyright (C) 2010-2011, IPython Development Team.\n#\n# Distributed under the terms of the Modified BSD License.\n#\n# The full license is in the file COPYING.txt, distributed with this software.\n#-----------------------------------------------------------------------------\n\nimport io\nimport os, sys\nfrom importlib import import_module\nimport imp\n\ntry:\n    import hashlib\nexcept ImportError:\n    import md5 as hashlib\n\nfrom distutils.core import Distribution, Extension\nfrom distutils.command.build_ext import build_ext\n\nfrom IPython.core.magic import Magics, magics_class, cell_magic\nfrom IPython.testing.skipdoctest import skip_doctest\nfrom IPython.core.magic_arguments import (\n    argument, magic_arguments, parse_argstring\n)\nfrom IPython.utils import py3compat\n\nimport Cython\nfrom Cython.Compiler.Errors import CompileError\nfrom Cython.Compiler.Main import Context, default_options\nfrom Cython.Build.Dependencies import cythonize\n\n\n@magics_class\nclass CythonMagics(Magics):\n\n    def __init__(self, shell):\n        super(CythonMagics,self).__init__(shell)\n        self._reloads = {}\n        self._code_cache = {}\n\n    def _import_all(self, module):\n        for k,v in module.__dict__.items():\n            if not k.startswith('__'):\n                self.shell.push({k:v})\n\n    @cell_magic\n    def cython_inline(self, line, cell):\n        \"\"\"Compile and run a Cython code cell using Cython.inline.\n\n        This magic simply passes the body of the cell to Cython.inline\n        and returns the result. If the variables `a` and `b` are defined\n        in the user's namespace, here is a simple example that returns\n        their sum::\n        \n            %%cython_inline\n            return a+b\n\n        For most purposes, we recommend the usage of the `%%cython` magic.\n        \"\"\"\n        locs = self.shell.user_global_ns\n        globs = self.shell.user_ns\n        return Cython.inline(cell, locals=locs, globals=globs)\n\n    @cell_magic\n    def cython_pyximport(self, line, cell):\n        \"\"\"Compile and import a Cython code cell using pyximport.\n\n        The contents of the cell are written to a `.pyx` file in the current\n        working directory, which is then imported using `pyximport`. This\n        magic requires a module name to be passed::\n        \n            %%cython_pyximport modulename\n            def f(x):\n                return 2.0*x\n\n        The compiled module is then imported and all of its symbols are injected into\n        the user's namespace. For most purposes, we recommend the usage of the\n        `%%cython` magic.\n        \"\"\"\n        module_name = line.strip()\n        if not module_name:\n            raise ValueError('module name must be given')\n        fname = module_name + '.pyx'\n        with io.open(fname, 'w', encoding='utf-8') as f:\n            f.write(cell)\n        if 'pyximport' not in sys.modules:\n            import pyximport\n            pyximport.install(reload_support=True)\n        if module_name in self._reloads:\n            module = self._reloads[module_name]\n            reload(module)\n        else:\n            module = import_module(module_name)\n            self._reloads[module_name] = module\n        self._import_all(module)\n\n    @magic_arguments()\n    @argument(\n        '-f', '--force', action='store_true', default=False,\n        help=\"Force the compilation of the pyx module even if it hasn't changed\"\n    )\n    @cell_magic\n    def cython(self, line, cell):\n        \"\"\"Compile and import everything from a Cython code cell.\n\n        The contents of the cell are written to a `.pyx` file in the\n        directory `IPYTHONDIR/cython` using a filename with the hash of the code.\n        This file is then cythonized and compiled. The resulting module\n        is imported and all of its symbols are injected into the user's\n        namespace. The usage is similar to that of `%%cython_pyximport` but\n        you don't have to pass a module name::\n\n        %%cython\n        def f(x):\n            return 2.0*x\n        \"\"\"\n        args = parse_argstring(self.cython, line)\n        code = cell if cell.endswith('\\n') else cell+'\\n'\n        lib_dir=os.path.join(self.shell.ipython_dir, 'cython')\n        cython_include_dirs=['.']\n        force=args.force\n        quiet=True\n        ctx = Context(cython_include_dirs, default_options)\n        key = code, sys.version_info, sys.executable, Cython.__version__\n        module_name = \"_cython_magic_\" + hashlib.md5(str(key).encode('utf-8')).hexdigest()\n        so_ext = [ ext for ext,_,mod_type in imp.get_suffixes() if mod_type == imp.C_EXTENSION ][0]\n        module_path = os.path.join(lib_dir, module_name+so_ext)\n\n        if not os.path.exists(lib_dir):\n            os.makedirs(lib_dir)\n\n        if force or not os.path.isfile(module_path):\n            cflags = []\n            c_include_dirs = []\n            if 'numpy' in code:\n                import numpy\n                c_include_dirs.append(numpy.get_include())\n            pyx_file = os.path.join(lib_dir, module_name + '.pyx')\n            pyx_file = py3compat.cast_bytes_py2(pyx_file, encoding=sys.getfilesystemencoding())\n            with io.open(pyx_file, 'w', encoding='utf-8') as f:\n                f.write(code)\n            extension = Extension(\n                name = module_name,\n                sources = [pyx_file],\n                include_dirs = c_include_dirs,\n                extra_compile_args = cflags\n            )\n            dist = Distribution()\n            config_files = dist.find_config_files()\n            try: \n                config_files.remove('setup.cfg')\n            except ValueError:\n                pass\n            dist.parse_config_files(config_files)\n            build_extension = build_ext(dist)\n            build_extension.finalize_options()\n            try:\n                build_extension.extensions = cythonize([extension], ctx=ctx, quiet=quiet)\n            except CompileError:\n                return\n            build_extension.build_temp = os.path.dirname(pyx_file)\n            build_extension.build_lib  = lib_dir\n            build_extension.run()\n            self._code_cache[key] = module_name\n\n        module = imp.load_dynamic(module_name, module_path)\n        self._import_all(module)\n\n\n_loaded = False\n\ndef load_ipython_extension(ip):\n    \"\"\"Load the extension in IPython.\"\"\"\n    global _loaded\n    if not _loaded:\n        ip.register_magics(CythonMagics)\n        _loaded = True\n", "path": "IPython/extensions/cythonmagic.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n\"\"\"\nCython related magics.\n\nAuthor:\n* Brian Granger\n\nParts of this code were taken from Cython.inline.\n\"\"\"\n#-----------------------------------------------------------------------------\n# Copyright (C) 2010-2011, IPython Development Team.\n#\n# Distributed under the terms of the Modified BSD License.\n#\n# The full license is in the file COPYING.txt, distributed with this software.\n#-----------------------------------------------------------------------------\n\nimport io\nimport os, sys\nimport imp\n\ntry:\n    import hashlib\nexcept ImportError:\n    import md5 as hashlib\n\nfrom distutils.core import Distribution, Extension\nfrom distutils.command.build_ext import build_ext\n\nfrom IPython.core.magic import Magics, magics_class, cell_magic\nfrom IPython.testing.skipdoctest import skip_doctest\nfrom IPython.core.magic_arguments import (\n    argument, magic_arguments, parse_argstring\n)\nfrom IPython.utils import py3compat\n\nimport Cython\nfrom Cython.Compiler.Errors import CompileError\nfrom Cython.Compiler.Main import Context, default_options\nfrom Cython.Build.Dependencies import cythonize\n\n\n@magics_class\nclass CythonMagics(Magics):\n\n    def __init__(self, shell):\n        super(CythonMagics,self).__init__(shell)\n        self._reloads = {}\n        self._code_cache = {}\n\n    def _import_all(self, module):\n        for k,v in module.__dict__.items():\n            if not k.startswith('__'):\n                self.shell.push({k:v})\n\n    @cell_magic\n    def cython_inline(self, line, cell):\n        \"\"\"Compile and run a Cython code cell using Cython.inline.\n\n        This magic simply passes the body of the cell to Cython.inline\n        and returns the result. If the variables `a` and `b` are defined\n        in the user's namespace, here is a simple example that returns\n        their sum::\n        \n            %%cython_inline\n            return a+b\n\n        For most purposes, we recommend the usage of the `%%cython` magic.\n        \"\"\"\n        locs = self.shell.user_global_ns\n        globs = self.shell.user_ns\n        return Cython.inline(cell, locals=locs, globals=globs)\n\n    @cell_magic\n    def cython_pyximport(self, line, cell):\n        \"\"\"Compile and import a Cython code cell using pyximport.\n\n        The contents of the cell are written to a `.pyx` file in the current\n        working directory, which is then imported using `pyximport`. This\n        magic requires a module name to be passed::\n        \n            %%cython_pyximport modulename\n            def f(x):\n                return 2.0*x\n\n        The compiled module is then imported and all of its symbols are injected into\n        the user's namespace. For most purposes, we recommend the usage of the\n        `%%cython` magic.\n        \"\"\"\n        module_name = line.strip()\n        if not module_name:\n            raise ValueError('module name must be given')\n        fname = module_name + '.pyx'\n        with io.open(fname, 'w', encoding='utf-8') as f:\n            f.write(cell)\n        if 'pyximport' not in sys.modules:\n            import pyximport\n            pyximport.install(reload_support=True)\n        if module_name in self._reloads:\n            module = self._reloads[module_name]\n            reload(module)\n        else:\n            __import__(module_name)\n            module = sys.modules[module_name]\n            self._reloads[module_name] = module\n        self._import_all(module)\n\n    @magic_arguments()\n    @argument(\n        '-f', '--force', action='store_true', default=False,\n        help=\"Force the compilation of the pyx module even if it hasn't changed\"\n    )\n    @cell_magic\n    def cython(self, line, cell):\n        \"\"\"Compile and import everything from a Cython code cell.\n\n        The contents of the cell are written to a `.pyx` file in the\n        directory `IPYTHONDIR/cython` using a filename with the hash of the code.\n        This file is then cythonized and compiled. The resulting module\n        is imported and all of its symbols are injected into the user's\n        namespace. The usage is similar to that of `%%cython_pyximport` but\n        you don't have to pass a module name::\n\n        %%cython\n        def f(x):\n            return 2.0*x\n        \"\"\"\n        args = parse_argstring(self.cython, line)\n        code = cell if cell.endswith('\\n') else cell+'\\n'\n        lib_dir=os.path.join(self.shell.ipython_dir, 'cython')\n        cython_include_dirs=['.']\n        force=args.force\n        quiet=True\n        ctx = Context(cython_include_dirs, default_options)\n        key = code, sys.version_info, sys.executable, Cython.__version__\n        module_name = \"_cython_magic_\" + hashlib.md5(str(key).encode('utf-8')).hexdigest()\n        so_ext = [ ext for ext,_,mod_type in imp.get_suffixes() if mod_type == imp.C_EXTENSION ][0]\n        module_path = os.path.join(lib_dir, module_name+so_ext)\n\n        if not os.path.exists(lib_dir):\n            os.makedirs(lib_dir)\n\n        if force or not os.path.isfile(module_path):\n            cflags = []\n            c_include_dirs = []\n            if 'numpy' in code:\n                import numpy\n                c_include_dirs.append(numpy.get_include())\n            pyx_file = os.path.join(lib_dir, module_name + '.pyx')\n            pyx_file = py3compat.cast_bytes_py2(pyx_file, encoding=sys.getfilesystemencoding())\n            with io.open(pyx_file, 'w', encoding='utf-8') as f:\n                f.write(code)\n            extension = Extension(\n                name = module_name,\n                sources = [pyx_file],\n                include_dirs = c_include_dirs,\n                extra_compile_args = cflags\n            )\n            dist = Distribution()\n            config_files = dist.find_config_files()\n            try: \n                config_files.remove('setup.cfg')\n            except ValueError:\n                pass\n            dist.parse_config_files(config_files)\n            build_extension = build_ext(dist)\n            build_extension.finalize_options()\n            try:\n                build_extension.extensions = cythonize([extension], ctx=ctx, quiet=quiet)\n            except CompileError:\n                return\n            build_extension.build_temp = os.path.dirname(pyx_file)\n            build_extension.build_lib  = lib_dir\n            build_extension.run()\n            self._code_cache[key] = module_name\n\n        module = imp.load_dynamic(module_name, module_path)\n        self._import_all(module)\n\n\n_loaded = False\n\ndef load_ipython_extension(ip):\n    \"\"\"Load the extension in IPython.\"\"\"\n    global _loaded\n    if not _loaded:\n        ip.register_magics(CythonMagics)\n        _loaded = True\n", "path": "IPython/extensions/cythonmagic.py"}]}