Show More
@@ -5,7 +5,7 b' Class which mimics a module.' | |||
|
5 | 5 | Needed to allow pickle to correctly resolve namespaces during IPython |
|
6 | 6 | sessions. |
|
7 | 7 | |
|
8 |
$Id: FakeModule.py 16 |
|
|
8 | $Id: FakeModule.py 1625 2006-08-12 10:34:44Z vivainio $""" | |
|
9 | 9 | |
|
10 | 10 | #***************************************************************************** |
|
11 | 11 | # Copyright (C) 2002-2004 Fernando Perez. <fperez@colorado.edu> |
@@ -14,9 +14,7 b' $Id: FakeModule.py 1602 2006-08-11 09:19:33Z vivainio $"""' | |||
|
14 | 14 | # the file COPYING, distributed as part of this software. |
|
15 | 15 | #***************************************************************************** |
|
16 | 16 | |
|
17 | import types | |
|
18 | ||
|
19 | class FakeModule(types.ModuleType): | |
|
17 | class FakeModule: | |
|
20 | 18 | """Simple class with attribute access to fake a module. |
|
21 | 19 | |
|
22 | 20 | This is not meant to replace a module, but to allow inserting a fake |
@@ -27,7 +25,7 b' class FakeModule(types.ModuleType):' | |||
|
27 | 25 | Do NOT use this code for anything other than this IPython private hack.""" |
|
28 | 26 | |
|
29 | 27 | def __init__(self,adict): |
|
30 | types.ModuleType.__init__(self,adict['__name__']) | |
|
28 | ||
|
31 | 29 | # It seems pydoc (and perhaps others) needs any module instance to |
|
32 | 30 | # implement a __nonzero__ method, so we add it if missing: |
|
33 | 31 | if '__nonzero__' not in adict: |
@@ -35,16 +33,17 b' class FakeModule(types.ModuleType):' | |||
|
35 | 33 | return 1 |
|
36 | 34 | adict['__nonzero__'] = __nonzero__ |
|
37 | 35 | |
|
36 | self.__dict__ = adict | |
|
37 | ||
|
38 | 38 | # modules should have a __file__ attribute |
|
39 | 39 | adict['__file__'] = __file__ |
|
40 | self.__origdict = adict | |
|
41 | 40 | |
|
42 | 41 | def __getattr__(self,key): |
|
43 | try: | |
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
|
47 | ||
|
42 | try: | |
|
43 | return self.__dict__[key] | |
|
44 | except KeyError, e: | |
|
45 | raise AttributeError("FakeModule object has no attribute %s" % e) | |
|
46 | ||
|
48 | 47 | def __str__(self): |
|
49 | 48 | return "<IPython.FakeModule instance>" |
|
50 | 49 |
@@ -6,7 +6,7 b' Uses syntax highlighting for presenting the various information elements.' | |||
|
6 | 6 | Similar in spirit to the inspect module, but all calls take a name argument to |
|
7 | 7 | reference the name under which an object is being read. |
|
8 | 8 | |
|
9 |
$Id: OInspect.py 16 |
|
|
9 | $Id: OInspect.py 1625 2006-08-12 10:34:44Z vivainio $ | |
|
10 | 10 | """ |
|
11 | 11 | |
|
12 | 12 | #***************************************************************************** |
@@ -30,6 +30,7 b' import string' | |||
|
30 | 30 | import StringIO |
|
31 | 31 | import types |
|
32 | 32 | import os |
|
33 | import sys | |
|
33 | 34 | # IPython's own |
|
34 | 35 | from IPython import PyColorize |
|
35 | 36 | from IPython.genutils import page,indent,Term,mkdict |
@@ -38,6 +39,45 b' from IPython.wildcard import list_namespace' | |||
|
38 | 39 | from IPython.ColorANSI import * |
|
39 | 40 | |
|
40 | 41 | #**************************************************************************** |
|
42 | # HACK!!! This is a crude fix for bugs in python 2.3's inspect module. We | |
|
43 | # simply monkeypatch inspect with code copied from python 2.4. | |
|
44 | if sys.version_info[:2] == (2,3): | |
|
45 | from inspect import ismodule, getabsfile, modulesbyfile | |
|
46 | def getmodule(object): | |
|
47 | """Return the module an object was defined in, or None if not found.""" | |
|
48 | if ismodule(object): | |
|
49 | return object | |
|
50 | if hasattr(object, '__module__'): | |
|
51 | return sys.modules.get(object.__module__) | |
|
52 | try: | |
|
53 | file = getabsfile(object) | |
|
54 | except TypeError: | |
|
55 | return None | |
|
56 | if file in modulesbyfile: | |
|
57 | return sys.modules.get(modulesbyfile[file]) | |
|
58 | for module in sys.modules.values(): | |
|
59 | if hasattr(module, '__file__'): | |
|
60 | modulesbyfile[ | |
|
61 | os.path.realpath( | |
|
62 | getabsfile(module))] = module.__name__ | |
|
63 | if file in modulesbyfile: | |
|
64 | return sys.modules.get(modulesbyfile[file]) | |
|
65 | main = sys.modules['__main__'] | |
|
66 | if not hasattr(object, '__name__'): | |
|
67 | return None | |
|
68 | if hasattr(main, object.__name__): | |
|
69 | mainobject = getattr(main, object.__name__) | |
|
70 | if mainobject is object: | |
|
71 | return main | |
|
72 | builtin = sys.modules['__builtin__'] | |
|
73 | if hasattr(builtin, object.__name__): | |
|
74 | builtinobject = getattr(builtin, object.__name__) | |
|
75 | if builtinobject is object: | |
|
76 | return builtin | |
|
77 | ||
|
78 | inspect.getmodule = getmodule | |
|
79 | ||
|
80 | #**************************************************************************** | |
|
41 | 81 | # Builtin color schemes |
|
42 | 82 | |
|
43 | 83 | Colors = TermColors # just a shorthand |
General Comments 0
You need to be logged in to leave comments.
Login now