Show More
@@ -5,7 +5,7 b' Class which mimics a module.' | |||||
5 | Needed to allow pickle to correctly resolve namespaces during IPython |
|
5 | Needed to allow pickle to correctly resolve namespaces during IPython | |
6 | sessions. |
|
6 | sessions. | |
7 |
|
7 | |||
8 |
$Id: FakeModule.py 1 |
|
8 | $Id: FakeModule.py 1602 2006-08-11 09:19:33Z vivainio $""" | |
9 |
|
9 | |||
10 | #***************************************************************************** |
|
10 | #***************************************************************************** | |
11 | # Copyright (C) 2002-2004 Fernando Perez. <fperez@colorado.edu> |
|
11 | # Copyright (C) 2002-2004 Fernando Perez. <fperez@colorado.edu> | |
@@ -14,7 +14,9 b' $Id: FakeModule.py 1322 2006-05-24 07:51:39Z fperez $"""' | |||||
14 | # the file COPYING, distributed as part of this software. |
|
14 | # the file COPYING, distributed as part of this software. | |
15 | #***************************************************************************** |
|
15 | #***************************************************************************** | |
16 |
|
16 | |||
17 | class FakeModule: |
|
17 | import types | |
|
18 | ||||
|
19 | class FakeModule(types.ModuleType): | |||
18 | """Simple class with attribute access to fake a module. |
|
20 | """Simple class with attribute access to fake a module. | |
19 |
|
21 | |||
20 | This is not meant to replace a module, but to allow inserting a fake |
|
22 | This is not meant to replace a module, but to allow inserting a fake | |
@@ -25,7 +27,7 b' class FakeModule:' | |||||
25 | Do NOT use this code for anything other than this IPython private hack.""" |
|
27 | Do NOT use this code for anything other than this IPython private hack.""" | |
26 |
|
28 | |||
27 | def __init__(self,adict): |
|
29 | def __init__(self,adict): | |
28 |
|
30 | types.ModuleType.__init__(self,adict['__name__']) | ||
29 | # It seems pydoc (and perhaps others) needs any module instance to |
|
31 | # It seems pydoc (and perhaps others) needs any module instance to | |
30 | # implement a __nonzero__ method, so we add it if missing: |
|
32 | # implement a __nonzero__ method, so we add it if missing: | |
31 | if '__nonzero__' not in adict: |
|
33 | if '__nonzero__' not in adict: | |
@@ -33,17 +35,16 b' class FakeModule:' | |||||
33 | return 1 |
|
35 | return 1 | |
34 | adict['__nonzero__'] = __nonzero__ |
|
36 | adict['__nonzero__'] = __nonzero__ | |
35 |
|
37 | |||
36 | self.__dict__ = adict |
|
|||
37 |
|
||||
38 | # modules should have a __file__ attribute |
|
38 | # modules should have a __file__ attribute | |
39 | adict['__file__'] = __file__ |
|
39 | adict['__file__'] = __file__ | |
|
40 | self.__origdict = adict | |||
40 |
|
41 | |||
41 | def __getattr__(self,key): |
|
42 | def __getattr__(self,key): | |
42 | try: |
|
43 | try: | |
43 |
|
|
44 | return self.__origdict[key] | |
44 |
|
|
45 | except KeyError, e: | |
45 |
|
|
46 | raise AttributeError("FakeModule object has no attribute %s" % e) | |
46 |
|
47 | |||
47 | def __str__(self): |
|
48 | def __str__(self): | |
48 | return "<IPython.FakeModule instance>" |
|
49 | return "<IPython.FakeModule instance>" | |
49 |
|
50 |
@@ -6,7 +6,7 b' Uses syntax highlighting for presenting the various information elements.' | |||||
6 | Similar in spirit to the inspect module, but all calls take a name argument to |
|
6 | Similar in spirit to the inspect module, but all calls take a name argument to | |
7 | reference the name under which an object is being read. |
|
7 | reference the name under which an object is being read. | |
8 |
|
8 | |||
9 |
$Id: OInspect.py 1 |
|
9 | $Id: OInspect.py 1602 2006-08-11 09:19:33Z vivainio $ | |
10 | """ |
|
10 | """ | |
11 |
|
11 | |||
12 | #***************************************************************************** |
|
12 | #***************************************************************************** | |
@@ -30,7 +30,6 b' import string' | |||||
30 | import StringIO |
|
30 | import StringIO | |
31 | import types |
|
31 | import types | |
32 | import os |
|
32 | import os | |
33 | import sys |
|
|||
34 | # IPython's own |
|
33 | # IPython's own | |
35 | from IPython import PyColorize |
|
34 | from IPython import PyColorize | |
36 | from IPython.genutils import page,indent,Term,mkdict |
|
35 | from IPython.genutils import page,indent,Term,mkdict | |
@@ -39,45 +38,6 b' from IPython.wildcard import list_namespace' | |||||
39 | from IPython.ColorANSI import * |
|
38 | from IPython.ColorANSI import * | |
40 |
|
39 | |||
41 | #**************************************************************************** |
|
40 | #**************************************************************************** | |
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 | #**************************************************************************** |
|
|||
81 | # Builtin color schemes |
|
41 | # Builtin color schemes | |
82 |
|
42 | |||
83 | Colors = TermColors # just a shorthand |
|
43 | Colors = TermColors # just a shorthand |
@@ -1,3 +1,10 b'' | |||||
|
1 | 2006-08-11 Ville Vainio <vivainio@gmail.com> | |||
|
2 | ||||
|
3 | * Fakemodule.py, OInspect.py: remove 2006-08-09 monkepatch | |||
|
4 | by resolving issue properly, i.e. by inheriting FakeModule | |||
|
5 | from types.ModuleType. Pickling ipython interactive data | |||
|
6 | should still work as usual (testing appreciated). | |||
|
7 | ||||
1 | 2006-08-09 Fernando Perez <Fernando.Perez@colorado.edu> |
|
8 | 2006-08-09 Fernando Perez <Fernando.Perez@colorado.edu> | |
2 |
|
9 | |||
3 | * IPython/OInspect.py: monkeypatch inspect from the stdlib if |
|
10 | * IPython/OInspect.py: monkeypatch inspect from the stdlib if |
General Comments 0
You need to be logged in to leave comments.
Login now