diff --git a/IPython/FakeModule.py b/IPython/FakeModule.py index 19f1746..b877dbb 100644 --- a/IPython/FakeModule.py +++ b/IPython/FakeModule.py @@ -5,7 +5,7 @@ Class which mimics a module. Needed to allow pickle to correctly resolve namespaces during IPython sessions. -$Id: FakeModule.py 1602 2006-08-11 09:19:33Z vivainio $""" +$Id: FakeModule.py 1625 2006-08-12 10:34:44Z vivainio $""" #***************************************************************************** # Copyright (C) 2002-2004 Fernando Perez. @@ -14,9 +14,7 @@ $Id: FakeModule.py 1602 2006-08-11 09:19:33Z vivainio $""" # the file COPYING, distributed as part of this software. #***************************************************************************** -import types - -class FakeModule(types.ModuleType): +class FakeModule: """Simple class with attribute access to fake a module. This is not meant to replace a module, but to allow inserting a fake @@ -27,7 +25,7 @@ class FakeModule(types.ModuleType): Do NOT use this code for anything other than this IPython private hack.""" def __init__(self,adict): - types.ModuleType.__init__(self,adict['__name__']) + # It seems pydoc (and perhaps others) needs any module instance to # implement a __nonzero__ method, so we add it if missing: if '__nonzero__' not in adict: @@ -35,16 +33,17 @@ class FakeModule(types.ModuleType): return 1 adict['__nonzero__'] = __nonzero__ + self.__dict__ = adict + # modules should have a __file__ attribute adict['__file__'] = __file__ - self.__origdict = adict def __getattr__(self,key): - try: - return self.__origdict[key] - except KeyError, e: - raise AttributeError("FakeModule object has no attribute %s" % e) - + try: + return self.__dict__[key] + except KeyError, e: + raise AttributeError("FakeModule object has no attribute %s" % e) + def __str__(self): return "" diff --git a/IPython/OInspect.py b/IPython/OInspect.py index e5292a2..2507a72 100644 --- a/IPython/OInspect.py +++ b/IPython/OInspect.py @@ -6,7 +6,7 @@ Uses syntax highlighting for presenting the various information elements. Similar in spirit to the inspect module, but all calls take a name argument to reference the name under which an object is being read. -$Id: OInspect.py 1602 2006-08-11 09:19:33Z vivainio $ +$Id: OInspect.py 1625 2006-08-12 10:34:44Z vivainio $ """ #***************************************************************************** @@ -30,6 +30,7 @@ import string import StringIO import types import os +import sys # IPython's own from IPython import PyColorize from IPython.genutils import page,indent,Term,mkdict @@ -38,6 +39,45 @@ from IPython.wildcard import list_namespace from IPython.ColorANSI import * #**************************************************************************** +# HACK!!! This is a crude fix for bugs in python 2.3's inspect module. We +# simply monkeypatch inspect with code copied from python 2.4. +if sys.version_info[:2] == (2,3): + from inspect import ismodule, getabsfile, modulesbyfile + def getmodule(object): + """Return the module an object was defined in, or None if not found.""" + if ismodule(object): + return object + if hasattr(object, '__module__'): + return sys.modules.get(object.__module__) + try: + file = getabsfile(object) + except TypeError: + return None + if file in modulesbyfile: + return sys.modules.get(modulesbyfile[file]) + for module in sys.modules.values(): + if hasattr(module, '__file__'): + modulesbyfile[ + os.path.realpath( + getabsfile(module))] = module.__name__ + if file in modulesbyfile: + return sys.modules.get(modulesbyfile[file]) + main = sys.modules['__main__'] + if not hasattr(object, '__name__'): + return None + if hasattr(main, object.__name__): + mainobject = getattr(main, object.__name__) + if mainobject is object: + return main + builtin = sys.modules['__builtin__'] + if hasattr(builtin, object.__name__): + builtinobject = getattr(builtin, object.__name__) + if builtinobject is object: + return builtin + + inspect.getmodule = getmodule + +#**************************************************************************** # Builtin color schemes Colors = TermColors # just a shorthand diff --git a/doc/ChangeLog b/doc/ChangeLog index aaa410b..b56f4de 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,8 @@ +2006-08-12 Ville Vainio + + * Fakemodule.py, OInspect.py: Reverted 2006-08-11 mods, + they broke unittest + 2006-08-11 Ville Vainio * Fakemodule.py, OInspect.py: remove 2006-08-09 monkepatch