##// END OF EJS Templates
Fixes for handling of global variables in embedded ipython instances (I ran...
fperez -
r39:59aaa965
parent child
Show More
@@ -4,7 +4,7
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 882 2005-09-20 23:17:41Z fperez $"""
7 $Id: Shell.py 921 2005-11-13 06:51:34Z fperez $"""
8
8
9 #*****************************************************************************
9 #*****************************************************************************
10 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
10 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
@@ -132,7 +132,6 class IPShellEmbed:
132 #self.IP = make_IPython(argv,user_ns=__main__.__dict__)
132 #self.IP = make_IPython(argv,user_ns=__main__.__dict__)
133 self.IP = make_IPython(argv,rc_override=rc_override,embedded=True)
133 self.IP = make_IPython(argv,rc_override=rc_override,embedded=True)
134
134
135 self.IP.name_space_init()
136 # mark this as an embedded instance so we know if we get a crash
135 # mark this as an embedded instance so we know if we get a crash
137 # post-mortem
136 # post-mortem
138 self.IP.rc.embedded = 1
137 self.IP.rc.embedded = 1
@@ -613,9 +612,9 class IPShellGTK(threading.Thread):
613
612
614 if self.gtk.pygtk_version >= (2,4,0):
613 if self.gtk.pygtk_version >= (2,4,0):
615 import gobject
614 import gobject
616 gobject.timeout_add(self.TIMEOUT, self.on_timer)
615 gobject.idle_add(self.on_timer)
617 else:
616 else:
618 self.gtk.timeout_add(self.TIMEOUT, self.on_timer)
617 self.gtk.idle_add(self.on_timer)
619
618
620 if sys.platform != 'win32':
619 if sys.platform != 'win32':
621 try:
620 try:
@@ -6,7 +6,7 Requires Python 2.1 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 908 2005-09-26 16:05:48Z fperez $
9 $Id: iplib.py 921 2005-11-13 06:51:34Z fperez $
10 """
10 """
11
11
12 #*****************************************************************************
12 #*****************************************************************************
@@ -49,6 +49,7 import UserList # don't subclass list so this works with Python2.1
49 from pprint import pprint, pformat
49 from pprint import pprint, pformat
50 import cPickle as pickle
50 import cPickle as pickle
51 import traceback
51 import traceback
52 from codeop import CommandCompiler
52
53
53 # IPython's own modules
54 # IPython's own modules
54 import IPython
55 import IPython
@@ -506,7 +507,7 class InteractiveShell(code.InteractiveConsole, Logger, Magic):
506 """An enhanced console for Python."""
507 """An enhanced console for Python."""
507
508
508 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
509 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
509 user_ns = None,banner2='',
510 user_ns = None,user_global_ns=None,banner2='',
510 custom_exceptions=((),None)):
511 custom_exceptions=((),None)):
511
512
512 # Put a reference to self in builtins so that any form of embedded or
513 # Put a reference to self in builtins so that any form of embedded or
@@ -531,7 +532,21 class InteractiveShell(code.InteractiveConsole, Logger, Magic):
531 __builtin__.exit += _exit
532 __builtin__.exit += _exit
532 __builtin__.quit += _exit
533 __builtin__.quit += _exit
533
534
534 # Create the namespace where the user will operate:
535 # compiler command
536 self.compile = CommandCompiler()
537
538 # User input buffer
539 self.buffer = []
540
541 # Default name given in compilation of code
542 self.filename = '<ipython console>'
543
544 # Create the namespace where the user will operate. user_ns is
545 # normally the only one used, and it is passed to the exec calls as
546 # the locals argument. But we do carry a user_global_ns namespace
547 # given as the exec 'globals' argument, This is useful in embedding
548 # situations where the ipython shell opens in a context where the
549 # distinction between locals and globals is meaningful.
535
550
536 # FIXME. For some strange reason, __builtins__ is showing up at user
551 # FIXME. For some strange reason, __builtins__ is showing up at user
537 # level as a dict instead of a module. This is a manual fix, but I
552 # level as a dict instead of a module. This is a manual fix, but I
@@ -562,11 +577,16 class InteractiveShell(code.InteractiveConsole, Logger, Magic):
562 if user_ns is None:
577 if user_ns is None:
563 # Set __name__ to __main__ to better match the behavior of the
578 # Set __name__ to __main__ to better match the behavior of the
564 # normal interpreter.
579 # normal interpreter.
565 self.user_ns = {'__name__' :'__main__',
580 user_ns = {'__name__' :'__main__',
566 '__builtins__' : __builtin__,
581 '__builtins__' : __builtin__,
567 }
582 }
568 else:
583
569 self.user_ns = user_ns
584 if user_global_ns is None:
585 user_global_ns = {}
586
587 # assign namespaces
588 self.user_ns = user_ns
589 self.user_global_ns = user_global_ns
570
590
571 # The user namespace MUST have a pointer to the shell itself.
591 # The user namespace MUST have a pointer to the shell itself.
572 self.user_ns[name] = self
592 self.user_ns[name] = self
@@ -611,8 +631,7 class InteractiveShell(code.InteractiveConsole, Logger, Magic):
611 no_alias[key] = 1
631 no_alias[key] = 1
612 no_alias.update(__builtin__.__dict__)
632 no_alias.update(__builtin__.__dict__)
613 self.no_alias = no_alias
633 self.no_alias = no_alias
614
634
615
616 # make global variables for user access to these
635 # make global variables for user access to these
617 self.user_ns['_ih'] = self.input_hist
636 self.user_ns['_ih'] = self.input_hist
618 self.user_ns['_oh'] = self.output_hist
637 self.user_ns['_oh'] = self.output_hist
@@ -654,7 +673,6 class InteractiveShell(code.InteractiveConsole, Logger, Magic):
654 }
673 }
655
674
656 # class initializations
675 # class initializations
657 code.InteractiveConsole.__init__(self,locals = self.user_ns)
658 Logger.__init__(self,log_ns = self.user_ns)
676 Logger.__init__(self,log_ns = self.user_ns)
659 Magic.__init__(self,self)
677 Magic.__init__(self,self)
660
678
@@ -1303,18 +1321,12 want to merge them back into the new files.""" % locals()
1303 if len(self.inputcache) >= self.CACHELENGTH:
1321 if len(self.inputcache) >= self.CACHELENGTH:
1304 self.inputcache.pop() # This not :-)
1322 self.inputcache.pop() # This not :-)
1305
1323
1306 def name_space_init(self):
1307 """Create local namespace."""
1308 # We want this to be a method to facilitate embedded initialization.
1309 code.InteractiveConsole.__init__(self,self.user_ns)
1310
1311 def mainloop(self,banner=None):
1324 def mainloop(self,banner=None):
1312 """Creates the local namespace and starts the mainloop.
1325 """Creates the local namespace and starts the mainloop.
1313
1326
1314 If an optional banner argument is given, it will override the
1327 If an optional banner argument is given, it will override the
1315 internally created default banner."""
1328 internally created default banner."""
1316
1329
1317 self.name_space_init()
1318 if self.rc.c: # Emulate Python's -c option
1330 if self.rc.c: # Emulate Python's -c option
1319 self.exec_init_cmd()
1331 self.exec_init_cmd()
1320 if banner is None:
1332 if banner is None:
@@ -1355,12 +1367,6 want to merge them back into the new files.""" % locals()
1355 globals get overwritten). In the future this will be cleaned up, as
1367 globals get overwritten). In the future this will be cleaned up, as
1356 there is no fundamental reason why it can't work perfectly."""
1368 there is no fundamental reason why it can't work perfectly."""
1357
1369
1358 # Patch for global embedding to make sure that things don't overwrite
1359 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1360 # FIXME. Test this a bit more carefully (the if.. is new)
1361 if local_ns is None and global_ns is None:
1362 self.user_ns.update(__main__.__dict__)
1363
1364 # Get locals and globals from caller
1370 # Get locals and globals from caller
1365 if local_ns is None or global_ns is None:
1371 if local_ns is None or global_ns is None:
1366 call_frame = sys._getframe(stack_depth).f_back
1372 call_frame = sys._getframe(stack_depth).f_back
@@ -1371,12 +1377,16 want to merge them back into the new files.""" % locals()
1371 global_ns = call_frame.f_globals
1377 global_ns = call_frame.f_globals
1372
1378
1373 # Update namespaces and fire up interpreter
1379 # Update namespaces and fire up interpreter
1374 self.user_ns.update(local_ns)
1380 self.user_ns = local_ns
1375 self.interact(header)
1381 self.user_global_ns = global_ns
1382
1383 # Patch for global embedding to make sure that things don't overwrite
1384 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1385 # FIXME. Test this a bit more carefully (the if.. is new)
1386 if local_ns is None and global_ns is None:
1387 self.user_global_ns.update(__main__.__dict__)
1376
1388
1377 # Remove locals from namespace
1389 self.interact(header)
1378 for k in local_ns:
1379 del self.user_ns[k]
1380
1390
1381 def interact(self, banner=None):
1391 def interact(self, banner=None):
1382 """Closely emulate the interactive Python console.
1392 """Closely emulate the interactive Python console.
@@ -1619,7 +1629,7 want to merge them back into the new files.""" % locals()
1619 outflag = 1 # happens in more places, so it's easier as default
1629 outflag = 1 # happens in more places, so it's easier as default
1620 try:
1630 try:
1621 try:
1631 try:
1622 exec code_obj in self.locals
1632 exec code_obj in self.user_global_ns, self.user_ns
1623 finally:
1633 finally:
1624 # Reset our crash handler in place
1634 # Reset our crash handler in place
1625 sys.excepthook = old_excepthook
1635 sys.excepthook = old_excepthook
@@ -1,3 +1,16
1 2005-11-12 <Fernando.Perez@colorado.edu>
2
3 * IPython/iplib.py (embed_mainloop): Fix handling of globals for
4 embedded instances. I added a user_global_ns attribute to the
5 InteractiveShell class to handle this.
6
7 2005-10-31 Fernando Perez <Fernando.Perez@colorado.edu>
8
9 * IPython/Shell.py (IPShellGTK.mainloop): Change timeout_add to
10 idle_add, which fixes horrible keyboard lag problems under gtk 2.6
11 (reported under win32, but may happen also in other platforms).
12 Bug report and fix courtesy of Sean Moore <smm-AT-logic.bm>
13
1 2005-10-15 Fernando Perez <Fernando.Perez@colorado.edu>
14 2005-10-15 Fernando Perez <Fernando.Perez@colorado.edu>
2
15
3 * IPython/Magic.py (magic_psearch): new support for wildcard
16 * IPython/Magic.py (magic_psearch): new support for wildcard
General Comments 0
You need to be logged in to leave comments. Login now