##// END OF EJS Templates
Replace exec/eval with proper __import__....
Replace exec/eval with proper __import__. Unfortunately for some very weird reason, this code can't be called as a function, so I inlined a verbatim copy of importstring.import_item.

File last commit:

r11019:4350decf
r11019:4350decf
Show More
frontend.py
77 lines | 2.9 KiB | text/x-python | PythonLexer
Fernando Perez
Add proper warnings on use of the backwards compatibility shim.
r11015 """
Shim to maintain backwards compatibility with old frontend imports.
We have moved all contents of the old `frontend` subpackage into top-level
subpackages (`html`, `qt` and `terminal`). This will let code that was making
`from IPython.frontend...` calls continue working, though a warning will be
printed.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import print_function
Fernando Perez
Replace exec/eval with proper __import__....
r11019
# Stdlib
Fernando Perez
Add shim module to allow flattening of namespace.
r11008 import sys
import types
Fernando Perez
Replace exec/eval with proper __import__....
r11019 m = """\
*** WARNING*** : The top-level `frontend` package has been deprecated.
All its subpackages have been moved to the top `IPython` level."""
print(m, file=sys.stderr)
# FIXME: turn this into a Warning once we've fixed all our own imports.
#raise DeprecationWarning(m)
Fernando Perez
Add proper warnings on use of the backwards compatibility shim.
r11015 #-----------------------------------------------------------------------------
# Class declarations
#-----------------------------------------------------------------------------
Fernando Perez
Add shim module to allow flattening of namespace.
r11008
class ShimModule(types.ModuleType):
def __getattribute__(self, key):
Fernando Perez
Replace exec/eval with proper __import__....
r11019 # Use the equivalent of import_item(name), see below
name = 'IPython.' + key
# NOTE: the code below is copied *verbatim* from
# importstring.import_item. For some very strange reason that makes no
# sense to me, if we call it *as a function*, it doesn't work. This
# has something to do with the deep bowels of the import machinery and
# I couldn't find a way to make the code work as a standard function
# call. But at least since it's an unmodified copy of import_item,
# which is used extensively and has a test suite, we can be reasonably
# confident this is OK. If anyone finds how to call the function, all
# the below could be replaced simply with:
#
# from IPython.utils.importstring import import_item
# return import_item('IPython.' + key)
Fernando Perez
Add proper warnings on use of the backwards compatibility shim.
r11015
Fernando Perez
Replace exec/eval with proper __import__....
r11019 parts = name.rsplit('.', 1)
if len(parts) == 2:
# called with 'foo.bar....'
package, obj = parts
module = __import__(package, fromlist=[obj])
try:
pak = module.__dict__[obj]
except KeyError:
raise ImportError('No module named %s' % obj)
return pak
else:
# called with un-dotted string
return __import__(parts[0])
Fernando Perez
Add shim module to allow flattening of namespace.
r11008
Fernando Perez
Add proper warnings on use of the backwards compatibility shim.
r11015
# Unconditionally insert the shim into sys.modules so that further import calls
# trigger the custom attribute access above
Fernando Perez
Add shim module to allow flattening of namespace.
r11008 sys.modules['IPython.frontend'] = ShimModule('frontend')