diff --git a/IPython/FakeModule.py b/IPython/FakeModule.py index 588934c..9905d52 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 2723 2007-09-07 07:44:16Z fperez $""" +$Id: FakeModule.py 2754 2007-09-09 10:16:59Z fperez $""" #***************************************************************************** # Copyright (C) 2002-2004 Fernando Perez. @@ -25,28 +25,19 @@ class FakeModule(types.ModuleType): sessions. Do NOT use this code for anything other than this IPython private hack.""" - def __init__(self,adict): + def __init__(self,adict=None): + + # tmp to force __dict__ instance creation, else self.__dict__ fails + self.__iptmp = None + # 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: - def __nonzero__(): - return 1 - adict['__nonzero__'] = __nonzero__ + self.__dict__.setdefault('__nonzero__',lambda : True) + self.__dict__.setdefault('__file__',__file__) - self._dict_ = adict + # cleanup our temp trick + del self.__iptmp - # modules should have a __file__ attribute - adict.setdefault('__file__',__file__) - - def __getattr__(self,key): - try: - return self._dict_[key] - except KeyError, e: - raise AttributeError("FakeModule object has no attribute %s" % e) - - def __str__(self): - return "" - - def __repr__(self): - return str(self) + if adict is not None: + self.__dict__.update(adict) diff --git a/IPython/Magic.py b/IPython/Magic.py index dbabcda..abee16f 100644 --- a/IPython/Magic.py +++ b/IPython/Magic.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Magic functions for InteractiveShell. -$Id: Magic.py 2748 2007-09-08 14:32:40Z vivainio $""" +$Id: Magic.py 2754 2007-09-09 10:16:59Z fperez $""" #***************************************************************************** # Copyright (C) 2001 Janko Hauser and @@ -1522,12 +1522,15 @@ Currently the magic system has the following functions:\n""" prog_ns = self.shell.user_ns __name__save = self.shell.user_ns['__name__'] prog_ns['__name__'] = '__main__' + main_mod = FakeModule(prog_ns) else: if opts.has_key('n'): name = os.path.splitext(os.path.basename(filename))[0] else: name = '__main__' - prog_ns = {'__name__':name} + main_mod = FakeModule() + prog_ns = main_mod.__dict__ + prog_ns['__name__'] = name # Since '%run foo' emulates 'python foo.py' at the cmd line, we must # set the __file__ global in the script's namespace @@ -1540,7 +1543,7 @@ Currently the magic system has the following functions:\n""" else: restore_main = False - sys.modules[prog_ns['__name__']] = FakeModule(prog_ns) + sys.modules[prog_ns['__name__']] = main_mod stats = None try: diff --git a/IPython/iplib.py b/IPython/iplib.py index 9e1b6c3..f10ccd1 100644 --- a/IPython/iplib.py +++ b/IPython/iplib.py @@ -6,7 +6,7 @@ Requires Python 2.3 or newer. This file contains all the classes and helper functions specific to IPython. -$Id: iplib.py 2746 2007-09-08 14:00:21Z vivainio $ +$Id: iplib.py 2754 2007-09-09 10:16:59Z fperez $ """ #***************************************************************************** @@ -2391,7 +2391,23 @@ want to merge them back into the new files.""" % locals() """A safe version of the builtin execfile(). This version will never throw an exception, and knows how to handle - ipython logs as well.""" + ipython logs as well. + + :Parameters: + fname : string + Name of the file to be executed. + + where : tuple + One or two namespaces, passed to execfile() as (globals,locals). + If only one is given, it is passed as both. + + :Keywords: + islog : boolean (False) + + quiet : boolean (True) + + exit_ignore : boolean (False) + """ def syspath_cleanup(): """Internal cleanup routine for sys.path.""" @@ -2424,6 +2440,7 @@ want to merge them back into the new files.""" % locals() kw.setdefault('islog',0) kw.setdefault('quiet',1) kw.setdefault('exit_ignore',0) + first = xfile.readline() loghead = str(self.loghead_tpl).split('\n',1)[0].strip() xfile.close() diff --git a/doc/ChangeLog b/doc/ChangeLog index a38260e..11e45e9 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,10 @@ +2007-09-09 Fernando Perez + + * IPython/FakeModule.py (FakeModule.__init__): further fixes for + doctest support. + + * IPython/iplib.py (safe_execfile): minor docstring improvements. + 2007-09-08 Ville Vainio * Magic.py (%pushd, %popd, %dirs): Fix dir stack - push *current*