Commit 68e8117a authored by Robert Bradshaw's avatar Robert Bradshaw Committed by GitHub

Merge pull request #1625 from syrte/master

Let IPython magic only import things in `"__all__"`
parents af4737d7 350f462a
...@@ -87,15 +87,24 @@ from .Dependencies import cythonize ...@@ -87,15 +87,24 @@ from .Dependencies import cythonize
class CythonMagics(Magics): class CythonMagics(Magics):
def __init__(self, shell): def __init__(self, shell):
super(CythonMagics,self).__init__(shell) super(CythonMagics, self).__init__(shell)
self._reloads = {} self._reloads = {}
self._code_cache = {} self._code_cache = {}
self._pyximport_installed = False self._pyximport_installed = False
def _import_all(self, module): def _import_all(self, module):
for k,v in module.__dict__.items(): mdict = module.__dict__
if not k.startswith('__'): if '__all__' in mdict:
self.shell.push({k:v}) keys = mdict['__all__']
else:
keys = [k for k in mdict if not k.startswith('_')]
for k in keys:
try:
self.shell.push({k: mdict[k]})
except KeyError:
msg = "'module' object has no attribute '%s'" % k
raise AttributeError(msg)
@cell_magic @cell_magic
def cython_inline(self, line, cell): def cython_inline(self, line, cell):
...@@ -228,7 +237,7 @@ class CythonMagics(Magics): ...@@ -228,7 +237,7 @@ class CythonMagics(Magics):
... ...
""" """
args = magic_arguments.parse_argstring(self.cython, line) args = magic_arguments.parse_argstring(self.cython, line)
code = cell if cell.endswith('\n') else cell+'\n' code = cell if cell.endswith('\n') else cell + '\n'
lib_dir = os.path.join(get_ipython_cache_dir(), 'cython') lib_dir = os.path.join(get_ipython_cache_dir(), 'cython')
quiet = True quiet = True
key = code, line, sys.version_info, sys.executable, cython_version key = code, line, sys.version_info, sys.executable, cython_version
...@@ -266,14 +275,14 @@ class CythonMagics(Magics): ...@@ -266,14 +275,14 @@ class CythonMagics(Magics):
with io.open(pyx_file, 'w', encoding='utf-8') as f: with io.open(pyx_file, 'w', encoding='utf-8') as f:
f.write(code) f.write(code)
extension = Extension( extension = Extension(
name = module_name, name=module_name,
sources = [pyx_file] + c_src_files, sources=[pyx_file] + c_src_files,
include_dirs = c_include_dirs, include_dirs=c_include_dirs,
library_dirs = args.library_dirs, library_dirs=args.library_dirs,
extra_compile_args = args.compile_args, extra_compile_args=args.compile_args,
extra_link_args = args.link_args, extra_link_args=args.link_args,
libraries = args.lib, libraries=args.lib,
language = 'c++' if args.cplus else 'c', language='c++' if args.cplus else 'c',
) )
build_extension = self._get_build_extension() build_extension = self._get_build_extension()
try: try:
...@@ -293,7 +302,7 @@ class CythonMagics(Magics): ...@@ -293,7 +302,7 @@ class CythonMagics(Magics):
if not have_module: if not have_module:
build_extension.build_temp = os.path.dirname(pyx_file) build_extension.build_temp = os.path.dirname(pyx_file)
build_extension.build_lib = lib_dir build_extension.build_lib = lib_dir
build_extension.run() build_extension.run()
self._code_cache[key] = module_name self._code_cache[key] = module_name
...@@ -361,10 +370,10 @@ class CythonMagics(Magics): ...@@ -361,10 +370,10 @@ class CythonMagics(Magics):
return html return html
__doc__ = __doc__.format( __doc__ = __doc__.format(
# rST doesn't see the -+ flag as part of an option list, so we # rST doesn't see the -+ flag as part of an option list, so we
# hide it from the module-level docstring. # hide it from the module-level docstring.
CYTHON_DOC = dedent(CythonMagics.cython.__doc__\ CYTHON_DOC=dedent(CythonMagics.cython.__doc__\
.replace('-+, --cplus','--cplus ')), .replace('-+, --cplus', '--cplus ')),
CYTHON_INLINE_DOC = dedent(CythonMagics.cython_inline.__doc__), CYTHON_INLINE_DOC=dedent(CythonMagics.cython_inline.__doc__),
CYTHON_PYXIMPORT_DOC = dedent(CythonMagics.cython_pyximport.__doc__), CYTHON_PYXIMPORT_DOC=dedent(CythonMagics.cython_pyximport.__doc__),
) )
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