##// END OF EJS Templates
- Fix problems with -pylab and custom namespaces....
fperez -
Show More
@@ -1,7 +1,7 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """sys.excepthook for IPython itself, leaves a detailed report on disk.
2 """sys.excepthook for IPython itself, leaves a detailed report on disk.
3
3
4 $Id: CrashHandler.py 1320 2006-05-23 18:29:11Z vivainio $"""
4 $Id: CrashHandler.py 1326 2006-05-25 02:07:11Z fperez $"""
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
7 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
@@ -87,7 +87,7 b' If you want to do it now, the following command will work (under Unix):'
87 mail -s 'IPython Crash Report' $self.mailto < $self.report_name
87 mail -s 'IPython Crash Report' $self.mailto < $self.report_name
88
88
89 To ensure accurate tracking of this issue, please file a report about it at:
89 To ensure accurate tracking of this issue, please file a report about it at:
90 http://www.scipy.net/roundup/ipython (IPython's online bug tracker).
90 http://projects.scipy.org/ipython/ipython/report
91 """)
91 """)
92 print >> sys.stderr, msg
92 print >> sys.stderr, msg
93
93
@@ -4,7 +4,7 b''
4 All the matplotlib support code was co-developed with John Hunter,
4 All the matplotlib support code was co-developed with John Hunter,
5 matplotlib's author.
5 matplotlib's author.
6
6
7 $Id: Shell.py 1313 2006-05-19 17:48:41Z fperez $"""
7 $Id: Shell.py 1326 2006-05-25 02:07:11Z fperez $"""
8
8
9 #*****************************************************************************
9 #*****************************************************************************
10 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
10 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
@@ -51,7 +51,8 b' class IPShell:'
51
51
52 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
52 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
53 debug=1,shell_class=InteractiveShell):
53 debug=1,shell_class=InteractiveShell):
54 self.IP = make_IPython(argv,user_ns=user_ns,user_global_ns=user_global_ns,
54 self.IP = make_IPython(argv,user_ns=user_ns,
55 user_global_ns=user_global_ns,
55 debug=debug,shell_class=shell_class)
56 debug=debug,shell_class=shell_class)
56
57
57 def mainloop(self,sys_exit=0,banner=None):
58 def mainloop(self,sys_exit=0,banner=None):
@@ -112,7 +113,8 b' class IPShellEmbed:'
112 <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and
113 <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and
113 by the IDL stop/continue commands."""
114 by the IDL stop/continue commands."""
114
115
115 def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None):
116 def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None,
117 user_ns=None):
116 """Note that argv here is a string, NOT a list."""
118 """Note that argv here is a string, NOT a list."""
117 self.set_banner(banner)
119 self.set_banner(banner)
118 self.set_exit_msg(exit_msg)
120 self.set_exit_msg(exit_msg)
@@ -129,9 +131,9 b' class IPShellEmbed:'
129 except:
131 except:
130 pass # not nested with IPython
132 pass # not nested with IPython
131
133
132 # FIXME. Passing user_ns breaks namespace handling.
134 self.IP = make_IPython(argv,rc_override=rc_override,
133 #self.IP = make_IPython(argv,user_ns=__main__.__dict__)
135 embedded=True,
134 self.IP = make_IPython(argv,rc_override=rc_override,embedded=True)
136 user_ns=user_ns)
135
137
136 # copy our own displayhook also
138 # copy our own displayhook also
137 self.sys_displayhook_embed = sys.displayhook
139 self.sys_displayhook_embed = sys.displayhook
@@ -387,7 +389,7 b' class MatplotlibShellBase:'
387 Given Python's MRO, this should be used as the FIRST class in the
389 Given Python's MRO, this should be used as the FIRST class in the
388 inheritance hierarchy, so that it overrides the relevant methods."""
390 inheritance hierarchy, so that it overrides the relevant methods."""
389
391
390 def _matplotlib_config(self,name):
392 def _matplotlib_config(self,name,user_ns):
391 """Return items needed to setup the user's shell with matplotlib"""
393 """Return items needed to setup the user's shell with matplotlib"""
392
394
393 # Initialize matplotlib to interactive mode always
395 # Initialize matplotlib to interactive mode always
@@ -432,14 +434,8 b' class MatplotlibShellBase:'
432
434
433 # This must be imported last in the matplotlib series, after
435 # This must be imported last in the matplotlib series, after
434 # backend/interactivity choices have been made
436 # backend/interactivity choices have been made
435 try:
437 import matplotlib.pylab as pylab
436 import matplotlib.pylab as pylab
438 self.pylab = pylab
437 self.pylab = pylab
438 self.pylab_name = 'pylab'
439 except ImportError:
440 import matplotlib.matlab as matlab
441 self.pylab = matlab
442 self.pylab_name = 'matlab'
443
439
444 self.pylab.show._needmain = False
440 self.pylab.show._needmain = False
445 # We need to detect at runtime whether show() is called by the user.
441 # We need to detect at runtime whether show() is called by the user.
@@ -447,16 +443,11 b' class MatplotlibShellBase:'
447 self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive)
443 self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive)
448
444
449 # Build a user namespace initialized with matplotlib/matlab features.
445 # Build a user namespace initialized with matplotlib/matlab features.
450 user_ns = {'__name__':'__main__',
446 user_ns = IPython.ipapi.make_user_ns(user_ns)
451 '__builtins__' : __builtin__ }
452
447
453 # Be careful not to remove the final \n in the code string below, or
454 # things will break badly with py22 (I think it's a python bug, 2.3 is
455 # OK).
456 pname = self.pylab_name # Python can't interpolate dotted var names
457 exec ("import matplotlib\n"
448 exec ("import matplotlib\n"
458 "import matplotlib.%(pname)s as %(pname)s\n"
449 "import matplotlib.pylab as pylab\n"
459 "from matplotlib.%(pname)s import *\n" % locals()) in user_ns
450 "from matplotlib.pylab import *") in user_ns
460
451
461 # Build matplotlib info banner
452 # Build matplotlib info banner
462 b="""
453 b="""
@@ -503,7 +494,7 b' class MatplotlibShell(MatplotlibShellBase,InteractiveShell):'
503
494
504 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
495 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
505 user_ns=None,user_global_ns=None,**kw):
496 user_ns=None,user_global_ns=None,**kw):
506 user_ns,b2 = self._matplotlib_config(name)
497 user_ns,b2 = self._matplotlib_config(name,user_ns)
507 InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
498 InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
508 banner2=b2,**kw)
499 banner2=b2,**kw)
509
500
@@ -61,6 +61,7 b' print "done!"'
61 '''
61 '''
62
62
63 # stdlib imports
63 # stdlib imports
64 import __builtin__
64 import sys
65 import sys
65
66
66 # our own
67 # our own
@@ -267,7 +268,7 b' class IPApi:'
267 (name,cf.f_code.co_name))
268 (name,cf.f_code.co_name))
268
269
269 def launch_new_instance(user_ns = None):
270 def launch_new_instance(user_ns = None):
270 """ Create and start a new ipython instance.
271 """ Make and start a new ipython instance.
271
272
272 This can be called even without having an already initialized
273 This can be called even without having an already initialized
273 ipython session running.
274 ipython session running.
@@ -275,19 +276,53 b' def launch_new_instance(user_ns = None):'
275 This is also used as the egg entry point for the 'ipython' script.
276 This is also used as the egg entry point for the 'ipython' script.
276
277
277 """
278 """
278 ses = create_session(user_ns)
279 ses = make_session(user_ns)
279 ses.mainloop()
280 ses.mainloop()
280
281
281
282
282 def create_session(user_ns = None):
283 def make_user_ns(user_ns = None):
283 """ Creates, but does not launch an IPython session.
284 """Return a valid user interactive namespace.
285
286 This builds a dict with the minimal information needed to operate as a
287 valid IPython user namespace, which you can pass to the various embedding
288 classes in ipython.
289 """
290
291 if user_ns is None:
292 # Set __name__ to __main__ to better match the behavior of the
293 # normal interpreter.
294 user_ns = {'__name__' :'__main__',
295 '__builtins__' : __builtin__,
296 }
297 else:
298 user_ns.setdefault('__name__','__main__')
299 user_ns.setdefault('__builtins__',__builtin__)
300
301 return user_ns
302
303
304 def make_user_global_ns(ns = None):
305 """Return a valid user global namespace.
306
307 Similar to make_user_ns(), but global namespaces are really only needed in
308 embedded applications, where there is a distinction between the user's
309 interactive namespace and the global one where ipython is running."""
310
311 if ns is None: ns = {}
312 return ns
313
314
315 def make_session(user_ns = None):
316 """Makes, but does not launch an IPython session.
284
317
285 Later on you can call obj.mainloop() on the returned object.
318 Later on you can call obj.mainloop() on the returned object.
319
320 Inputs:
321
322 - user_ns(None): a dict to be used as the user's namespace with initial
323 data.
286
324
287 This should *not* be run when a session exists already.
325 WARNING: This should *not* be run when a session exists already."""
288
326
289 """
290 if user_ns is not None:
291 user_ns["__name__"] = user_ns.get("__name__",'ipy_session')
292 import IPython
327 import IPython
293 return IPython.Shell.start(user_ns = user_ns)
328 return IPython.Shell.start(user_ns)
@@ -6,7 +6,7 b' Requires Python 2.3 or newer.'
6
6
7 This file contains all the classes and helper functions specific to IPython.
7 This file contains all the classes and helper functions specific to IPython.
8
8
9 $Id: iplib.py 1323 2006-05-24 10:26:30Z walter.doerwald $
9 $Id: iplib.py 1326 2006-05-25 02:07:11Z fperez $
10 """
10 """
11
11
12 #*****************************************************************************
12 #*****************************************************************************
@@ -258,17 +258,13 b' class InteractiveShell(object,Magic):'
258 # that if you need to access the built-in namespace directly, you
258 # that if you need to access the built-in namespace directly, you
259 # should start with "import __builtin__" (note, no 's') which will
259 # should start with "import __builtin__" (note, no 's') which will
260 # definitely give you a module. Yeah, it's somewhat confusing:-(.
260 # definitely give you a module. Yeah, it's somewhat confusing:-(.
261
262 if user_ns is None:
263 # Set __name__ to __main__ to better match the behavior of the
264 # normal interpreter.
265 user_ns = {'__name__' :'__main__',
266 '__builtins__' : __builtin__,
267 }
268
269 if user_global_ns is None:
270 user_global_ns = {}
271
261
262 # These routines return properly built dicts as needed by the rest of
263 # the code, and can also be used by extension writers to generate
264 # properly initialized namespaces.
265 user_ns = IPython.ipapi.make_user_ns(user_ns)
266 user_global_ns = IPython.ipapi.make_user_global_ns(user_global_ns)
267
272 # Assign namespaces
268 # Assign namespaces
273 # This is the namespace where all normal user variables live
269 # This is the namespace where all normal user variables live
274 self.user_ns = user_ns
270 self.user_ns = user_ns
@@ -1351,7 +1347,7 b' want to merge them back into the new files.""" % locals()'
1351
1347
1352 If an optional banner argument is given, it will override the
1348 If an optional banner argument is given, it will override the
1353 internally created default banner."""
1349 internally created default banner."""
1354
1350
1355 if self.rc.c: # Emulate Python's -c option
1351 if self.rc.c: # Emulate Python's -c option
1356 self.exec_init_cmd()
1352 self.exec_init_cmd()
1357 if banner is None:
1353 if banner is None:
@@ -1,5 +1,15 b''
1 2006-05-24 Fernando Perez <Fernando.Perez@colorado.edu>
1 2006-05-24 Fernando Perez <Fernando.Perez@colorado.edu>
2
2
3 * IPython/Shell.py (MatplotlibShellBase._matplotlib_config): Fix
4 nasty bug which was preventing custom namespaces with -pylab,
5 reported by M. Foord. Minor cleanup, remove old matplotlib.matlab
6 compatibility (long gone from mpl).
7
8 * IPython/ipapi.py (make_session): name change: create->make. We
9 use make in other places (ipmaker,...), it's shorter and easier to
10 type and say, etc. I'm trying to clean things before 0.7.2 so
11 that I can keep things stable wrt to ipapi in the chainsaw branch.
12
3 * ipython.el: fix the py-pdbtrack-input-prompt variable so that
13 * ipython.el: fix the py-pdbtrack-input-prompt variable so that
4 python-mode recognizes our debugger mode. Add support for
14 python-mode recognizes our debugger mode. Add support for
5 autoindent inside (X)emacs. After a patch sent in by Jin Liu
15 autoindent inside (X)emacs. After a patch sent in by Jin Liu
General Comments 0
You need to be logged in to leave comments. Login now