##// END OF EJS Templates
revert fakemodule and oinspect mods, they broke unittest. monkeypatch is back
vivainio -
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 1602 2006-08-11 09:19:33Z vivainio $"""
8 $Id: FakeModule.py 1625 2006-08-12 10:34:44Z 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,9 +14,7 b' $Id: FakeModule.py 1602 2006-08-11 09:19:33Z vivainio $"""'
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 import types
17 class FakeModule:
18
19 class FakeModule(types.ModuleType):
20 """Simple class with attribute access to fake a module.
18 """Simple class with attribute access to fake a module.
21
19
22 This is not meant to replace a module, but to allow inserting a fake
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 Do NOT use this code for anything other than this IPython private hack."""
25 Do NOT use this code for anything other than this IPython private hack."""
28
26
29 def __init__(self,adict):
27 def __init__(self,adict):
30 types.ModuleType.__init__(self,adict['__name__'])
28
31 # It seems pydoc (and perhaps others) needs any module instance to
29 # It seems pydoc (and perhaps others) needs any module instance to
32 # implement a __nonzero__ method, so we add it if missing:
30 # implement a __nonzero__ method, so we add it if missing:
33 if '__nonzero__' not in adict:
31 if '__nonzero__' not in adict:
@@ -35,13 +33,14 b' class FakeModule(types.ModuleType):'
35 return 1
33 return 1
36 adict['__nonzero__'] = __nonzero__
34 adict['__nonzero__'] = __nonzero__
37
35
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
41
40
42 def __getattr__(self,key):
41 def __getattr__(self,key):
43 try:
42 try:
44 return self.__origdict[key]
43 return self.__dict__[key]
45 except KeyError, e:
44 except KeyError, e:
46 raise AttributeError("FakeModule object has no attribute %s" % e)
45 raise AttributeError("FakeModule object has no attribute %s" % e)
47
46
@@ -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 1602 2006-08-11 09:19:33Z vivainio $
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 import StringIO
30 import StringIO
31 import types
31 import types
32 import os
32 import os
33 import sys
33 # IPython's own
34 # IPython's own
34 from IPython import PyColorize
35 from IPython import PyColorize
35 from IPython.genutils import page,indent,Term,mkdict
36 from IPython.genutils import page,indent,Term,mkdict
@@ -38,6 +39,45 b' from IPython.wildcard import list_namespace'
38 from IPython.ColorANSI import *
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 # Builtin color schemes
81 # Builtin color schemes
42
82
43 Colors = TermColors # just a shorthand
83 Colors = TermColors # just a shorthand
@@ -1,3 +1,8 b''
1 2006-08-12 Ville Vainio <vivainio@gmail.com>
2
3 * Fakemodule.py, OInspect.py: Reverted 2006-08-11 mods,
4 they broke unittest
5
1 2006-08-11 Ville Vainio <vivainio@gmail.com>
6 2006-08-11 Ville Vainio <vivainio@gmail.com>
2
7
3 * Fakemodule.py, OInspect.py: remove 2006-08-09 monkepatch
8 * Fakemodule.py, OInspect.py: remove 2006-08-09 monkepatch
General Comments 0
You need to be logged in to leave comments. Login now