diff --git a/IPython/CrashHandler.py b/IPython/CrashHandler.py index 6477786..8e4b273 100644 --- a/IPython/CrashHandler.py +++ b/IPython/CrashHandler.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """sys.excepthook for IPython itself, leaves a detailed report on disk. -$Id: CrashHandler.py 1320 2006-05-23 18:29:11Z vivainio $""" +$Id: CrashHandler.py 1326 2006-05-25 02:07:11Z fperez $""" #***************************************************************************** # Copyright (C) 2001-2006 Fernando Perez. @@ -87,7 +87,7 @@ If you want to do it now, the following command will work (under Unix): mail -s 'IPython Crash Report' $self.mailto < $self.report_name To ensure accurate tracking of this issue, please file a report about it at: -http://www.scipy.net/roundup/ipython (IPython's online bug tracker). +http://projects.scipy.org/ipython/ipython/report """) print >> sys.stderr, msg diff --git a/IPython/Shell.py b/IPython/Shell.py index 8ec4900..441986f 100644 --- a/IPython/Shell.py +++ b/IPython/Shell.py @@ -4,7 +4,7 @@ All the matplotlib support code was co-developed with John Hunter, matplotlib's author. -$Id: Shell.py 1313 2006-05-19 17:48:41Z fperez $""" +$Id: Shell.py 1326 2006-05-25 02:07:11Z fperez $""" #***************************************************************************** # Copyright (C) 2001-2006 Fernando Perez @@ -51,7 +51,8 @@ class IPShell: def __init__(self,argv=None,user_ns=None,user_global_ns=None, debug=1,shell_class=InteractiveShell): - self.IP = make_IPython(argv,user_ns=user_ns,user_global_ns=user_global_ns, + self.IP = make_IPython(argv,user_ns=user_ns, + user_global_ns=user_global_ns, debug=debug,shell_class=shell_class) def mainloop(self,sys_exit=0,banner=None): @@ -112,7 +113,8 @@ class IPShellEmbed: on Dec. 06/01 concerning similar uses of pyrepl, and by the IDL stop/continue commands.""" - def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None): + def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None, + user_ns=None): """Note that argv here is a string, NOT a list.""" self.set_banner(banner) self.set_exit_msg(exit_msg) @@ -129,9 +131,9 @@ class IPShellEmbed: except: pass # not nested with IPython - # FIXME. Passing user_ns breaks namespace handling. - #self.IP = make_IPython(argv,user_ns=__main__.__dict__) - self.IP = make_IPython(argv,rc_override=rc_override,embedded=True) + self.IP = make_IPython(argv,rc_override=rc_override, + embedded=True, + user_ns=user_ns) # copy our own displayhook also self.sys_displayhook_embed = sys.displayhook @@ -387,7 +389,7 @@ class MatplotlibShellBase: Given Python's MRO, this should be used as the FIRST class in the inheritance hierarchy, so that it overrides the relevant methods.""" - def _matplotlib_config(self,name): + def _matplotlib_config(self,name,user_ns): """Return items needed to setup the user's shell with matplotlib""" # Initialize matplotlib to interactive mode always @@ -432,14 +434,8 @@ class MatplotlibShellBase: # This must be imported last in the matplotlib series, after # backend/interactivity choices have been made - try: - import matplotlib.pylab as pylab - self.pylab = pylab - self.pylab_name = 'pylab' - except ImportError: - import matplotlib.matlab as matlab - self.pylab = matlab - self.pylab_name = 'matlab' + import matplotlib.pylab as pylab + self.pylab = pylab self.pylab.show._needmain = False # We need to detect at runtime whether show() is called by the user. @@ -447,16 +443,11 @@ class MatplotlibShellBase: self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive) # Build a user namespace initialized with matplotlib/matlab features. - user_ns = {'__name__':'__main__', - '__builtins__' : __builtin__ } + user_ns = IPython.ipapi.make_user_ns(user_ns) - # Be careful not to remove the final \n in the code string below, or - # things will break badly with py22 (I think it's a python bug, 2.3 is - # OK). - pname = self.pylab_name # Python can't interpolate dotted var names exec ("import matplotlib\n" - "import matplotlib.%(pname)s as %(pname)s\n" - "from matplotlib.%(pname)s import *\n" % locals()) in user_ns + "import matplotlib.pylab as pylab\n" + "from matplotlib.pylab import *") in user_ns # Build matplotlib info banner b=""" @@ -503,7 +494,7 @@ class MatplotlibShell(MatplotlibShellBase,InteractiveShell): def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), user_ns=None,user_global_ns=None,**kw): - user_ns,b2 = self._matplotlib_config(name) + user_ns,b2 = self._matplotlib_config(name,user_ns) InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns, banner2=b2,**kw) diff --git a/IPython/ipapi.py b/IPython/ipapi.py index 563ad83..2ad5658 100644 --- a/IPython/ipapi.py +++ b/IPython/ipapi.py @@ -61,6 +61,7 @@ print "done!" ''' # stdlib imports +import __builtin__ import sys # our own @@ -267,7 +268,7 @@ class IPApi: (name,cf.f_code.co_name)) def launch_new_instance(user_ns = None): - """ Create and start a new ipython instance. + """ Make and start a new ipython instance. This can be called even without having an already initialized ipython session running. @@ -275,19 +276,53 @@ def launch_new_instance(user_ns = None): This is also used as the egg entry point for the 'ipython' script. """ - ses = create_session(user_ns) + ses = make_session(user_ns) ses.mainloop() -def create_session(user_ns = None): - """ Creates, but does not launch an IPython session. +def make_user_ns(user_ns = None): + """Return a valid user interactive namespace. + + This builds a dict with the minimal information needed to operate as a + valid IPython user namespace, which you can pass to the various embedding + classes in ipython. + """ + + if user_ns is None: + # Set __name__ to __main__ to better match the behavior of the + # normal interpreter. + user_ns = {'__name__' :'__main__', + '__builtins__' : __builtin__, + } + else: + user_ns.setdefault('__name__','__main__') + user_ns.setdefault('__builtins__',__builtin__) + + return user_ns + + +def make_user_global_ns(ns = None): + """Return a valid user global namespace. + + Similar to make_user_ns(), but global namespaces are really only needed in + embedded applications, where there is a distinction between the user's + interactive namespace and the global one where ipython is running.""" + + if ns is None: ns = {} + return ns + + +def make_session(user_ns = None): + """Makes, but does not launch an IPython session. Later on you can call obj.mainloop() on the returned object. + + Inputs: + + - user_ns(None): a dict to be used as the user's namespace with initial + data. - This should *not* be run when a session exists already. - - """ - if user_ns is not None: - user_ns["__name__"] = user_ns.get("__name__",'ipy_session') + WARNING: This should *not* be run when a session exists already.""" + import IPython - return IPython.Shell.start(user_ns = user_ns) + return IPython.Shell.start(user_ns) diff --git a/IPython/iplib.py b/IPython/iplib.py index b73f702..cb4de0a 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 1323 2006-05-24 10:26:30Z walter.doerwald $ +$Id: iplib.py 1326 2006-05-25 02:07:11Z fperez $ """ #***************************************************************************** @@ -258,17 +258,13 @@ class InteractiveShell(object,Magic): # that if you need to access the built-in namespace directly, you # should start with "import __builtin__" (note, no 's') which will # definitely give you a module. Yeah, it's somewhat confusing:-(. - - if user_ns is None: - # Set __name__ to __main__ to better match the behavior of the - # normal interpreter. - user_ns = {'__name__' :'__main__', - '__builtins__' : __builtin__, - } - - if user_global_ns is None: - user_global_ns = {} + # These routines return properly built dicts as needed by the rest of + # the code, and can also be used by extension writers to generate + # properly initialized namespaces. + user_ns = IPython.ipapi.make_user_ns(user_ns) + user_global_ns = IPython.ipapi.make_user_global_ns(user_global_ns) + # Assign namespaces # This is the namespace where all normal user variables live self.user_ns = user_ns @@ -1351,7 +1347,7 @@ want to merge them back into the new files.""" % locals() If an optional banner argument is given, it will override the internally created default banner.""" - + if self.rc.c: # Emulate Python's -c option self.exec_init_cmd() if banner is None: diff --git a/doc/ChangeLog b/doc/ChangeLog index 8c9ccad..9dedde4 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,5 +1,15 @@ 2006-05-24 Fernando Perez + * IPython/Shell.py (MatplotlibShellBase._matplotlib_config): Fix + nasty bug which was preventing custom namespaces with -pylab, + reported by M. Foord. Minor cleanup, remove old matplotlib.matlab + compatibility (long gone from mpl). + + * IPython/ipapi.py (make_session): name change: create->make. We + use make in other places (ipmaker,...), it's shorter and easier to + type and say, etc. I'm trying to clean things before 0.7.2 so + that I can keep things stable wrt to ipapi in the chainsaw branch. + * ipython.el: fix the py-pdbtrack-input-prompt variable so that python-mode recognizes our debugger mode. Add support for autoindent inside (X)emacs. After a patch sent in by Jin Liu