Show More
@@ -1,39 +1,42 b'' | |||
|
1 | 1 | """A shim module for deprecated imports |
|
2 | 2 | """ |
|
3 | 3 | # Copyright (c) IPython Development Team. |
|
4 | 4 | # Distributed under the terms of the Modified BSD License. |
|
5 | 5 | |
|
6 | import sys | |
|
6 | 7 | import types |
|
7 | 8 | |
|
8 | 9 | class ShimModule(types.ModuleType): |
|
9 | 10 | |
|
10 | 11 | def __init__(self, *args, **kwargs): |
|
11 | 12 | self._mirror = kwargs.pop("mirror") |
|
12 | 13 | super(ShimModule, self).__init__(*args, **kwargs) |
|
14 | if sys.version_info >= (3,4): | |
|
15 | self.__spec__ = __import__(self._mirror).__spec__ | |
|
13 | 16 | |
|
14 | 17 | def __getattr__(self, key): |
|
15 | 18 | # Use the equivalent of import_item(name), see below |
|
16 | 19 | name = "%s.%s" % (self._mirror, key) |
|
17 | 20 | |
|
18 | 21 | # NOTE: the code below was copied *verbatim* from |
|
19 | 22 | # importstring.import_item. For some very strange reason that makes no |
|
20 | 23 | # sense to me, if we call it *as a function*, it doesn't work. This |
|
21 | 24 | # has something to do with the deep bowels of the import machinery and |
|
22 | 25 | # I couldn't find a way to make the code work as a standard function |
|
23 | 26 | # call. But at least since it's an unmodified copy of import_item, |
|
24 | 27 | # which is used extensively and has a test suite, we can be reasonably |
|
25 | 28 | # confident this is OK. If anyone finds how to call the function, all |
|
26 | 29 | # the below could be replaced simply with: |
|
27 | 30 | # |
|
28 | 31 | # from IPython.utils.importstring import import_item |
|
29 | 32 | # return import_item('MIRROR.' + key) |
|
30 | 33 | |
|
31 | 34 | parts = name.rsplit('.', 1) |
|
32 | 35 | if len(parts) == 2: |
|
33 | 36 | # called with 'foo.bar....' |
|
34 | 37 | package, obj = parts |
|
35 | 38 | module = __import__(package, fromlist=[obj]) |
|
36 | 39 | return getattr(module, obj) |
|
37 | 40 | else: |
|
38 | 41 | # called with un-dotted string |
|
39 | 42 | return __import__(parts[0]) |
General Comments 0
You need to be logged in to leave comments.
Login now