Show More
@@ -510,7 +510,7 b' class MatplotlibShellBase:' | |||
|
510 | 510 | Given Python's MRO, this should be used as the FIRST class in the |
|
511 | 511 | inheritance hierarchy, so that it overrides the relevant methods.""" |
|
512 | 512 | |
|
513 | def _matplotlib_config(self,name,user_ns): | |
|
513 | def _matplotlib_config(self,name,user_ns,user_global_ns=None): | |
|
514 | 514 | """Return items needed to setup the user's shell with matplotlib""" |
|
515 | 515 | |
|
516 | 516 | # Initialize matplotlib to interactive mode always |
@@ -564,7 +564,8 b' class MatplotlibShellBase:' | |||
|
564 | 564 | self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive) |
|
565 | 565 | |
|
566 | 566 | # Build a user namespace initialized with matplotlib/matlab features. |
|
567 |
user_ns = IPython.ipapi.make_user_ns(user_ns |
|
|
567 | user_ns, user_global_ns = IPython.ipapi.make_user_namespaces(user_ns, | |
|
568 | user_global_ns) | |
|
568 | 569 | |
|
569 | 570 | # Import numpy as np/pyplot as plt are conventions we're trying to |
|
570 | 571 | # somewhat standardize on. Making them available to users by default |
@@ -584,7 +585,7 b' class MatplotlibShellBase:' | |||
|
584 | 585 | Welcome to pylab, a matplotlib-based Python environment. |
|
585 | 586 | For more information, type 'help(pylab)'. |
|
586 | 587 | """ |
|
587 | return user_ns,b | |
|
588 | return user_ns,user_global_ns,b | |
|
588 | 589 | |
|
589 | 590 | def mplot_exec(self,fname,*where,**kw): |
|
590 | 591 | """Execute a matplotlib script. |
@@ -624,7 +625,7 b' class MatplotlibShell(MatplotlibShellBase,InteractiveShell):' | |||
|
624 | 625 | |
|
625 | 626 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
626 | 627 | user_ns=None,user_global_ns=None,**kw): |
|
627 | user_ns,b2 = self._matplotlib_config(name,user_ns) | |
|
628 | user_ns,user_global_ns,b2 = self._matplotlib_config(name,user_ns,user_global_ns) | |
|
628 | 629 | InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns, |
|
629 | 630 | banner2=b2,**kw) |
|
630 | 631 |
@@ -567,17 +567,7 b' def make_user_ns(user_ns = None):' | |||
|
567 | 567 | classes in ipython. |
|
568 | 568 | """ |
|
569 | 569 | |
|
570 | if user_ns is None: | |
|
571 | # Set __name__ to __main__ to better match the behavior of the | |
|
572 | # normal interpreter. | |
|
573 | user_ns = {'__name__' :'__main__', | |
|
574 | '__builtins__' : __builtin__, | |
|
575 | } | |
|
576 | else: | |
|
577 | user_ns.setdefault('__name__','__main__') | |
|
578 | user_ns.setdefault('__builtins__',__builtin__) | |
|
579 | ||
|
580 | return user_ns | |
|
570 | raise NotImplementedError | |
|
581 | 571 | |
|
582 | 572 | |
|
583 | 573 | def make_user_global_ns(ns = None): |
@@ -587,8 +577,56 b' def make_user_global_ns(ns = None):' | |||
|
587 | 577 | embedded applications, where there is a distinction between the user's |
|
588 | 578 | interactive namespace and the global one where ipython is running.""" |
|
589 | 579 | |
|
590 | if ns is None: ns = {} | |
|
591 | return ns | |
|
580 | raise NotImplementedError | |
|
581 | ||
|
582 | # Record the true objects in order to be able to test if the user has overridden | |
|
583 | # these API functions. | |
|
584 | _make_user_ns = make_user_ns | |
|
585 | _make_user_global_ns = make_user_global_ns | |
|
586 | ||
|
587 | ||
|
588 | def make_user_namespaces(user_ns = None,user_global_ns = None): | |
|
589 | """Return a valid local and global user interactive namespaces. | |
|
590 | ||
|
591 | This builds a dict with the minimal information needed to operate as a | |
|
592 | valid IPython user namespace, which you can pass to the various embedding | |
|
593 | classes in ipython. The default implementation returns the same dict for | |
|
594 | both the locals and the globals to allow functions to refer to variables in | |
|
595 | the namespace. Customized implementations can return different dicts. The | |
|
596 | locals dictionary can actually be anything following the basic mapping | |
|
597 | protocol of a dict, but the globals dict must be a true dict, not even | |
|
598 | a subclass. It is recommended that any custom object for the locals | |
|
599 | namespace synchronize with the globals dict somehow. | |
|
600 | ||
|
601 | Raises TypeError if the provided globals namespace is not a true dict. | |
|
602 | """ | |
|
603 | ||
|
604 | if user_ns is None: | |
|
605 | if make_user_ns is not _make_user_ns: | |
|
606 | # Old API overridden. | |
|
607 | # FIXME: Issue DeprecationWarning, or just let the old API live on? | |
|
608 | user_ns = make_user_ns(user_ns) | |
|
609 | else: | |
|
610 | # Set __name__ to __main__ to better match the behavior of the | |
|
611 | # normal interpreter. | |
|
612 | user_ns = {'__name__' :'__main__', | |
|
613 | '__builtins__' : __builtin__, | |
|
614 | } | |
|
615 | else: | |
|
616 | user_ns.setdefault('__name__','__main__') | |
|
617 | user_ns.setdefault('__builtins__',__builtin__) | |
|
618 | ||
|
619 | if user_global_ns is None: | |
|
620 | if make_user_global_ns is not _make_user_global_ns: | |
|
621 | # Old API overridden. | |
|
622 | user_global_ns = make_user_global_ns(user_global_ns) | |
|
623 | else: | |
|
624 | user_global_ns = user_ns | |
|
625 | if type(user_global_ns) is not dict: | |
|
626 | raise TypeError("user_global_ns must be a true dict; got %r" | |
|
627 | % type(user_global_ns)) | |
|
628 | ||
|
629 | return user_ns, user_global_ns | |
|
592 | 630 | |
|
593 | 631 | |
|
594 | 632 | def make_session(user_ns = None, shellclass = None): |
@@ -207,7 +207,7 b' class InteractiveShell(object,Magic):' | |||
|
207 | 207 | isthreaded = False |
|
208 | 208 | |
|
209 | 209 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
210 |
user_ns |
|
|
210 | user_ns=None,user_global_ns=None,banner2='', | |
|
211 | 211 | custom_exceptions=((),None),embedded=False): |
|
212 | 212 | |
|
213 | 213 | # log system |
@@ -254,7 +254,8 b' class InteractiveShell(object,Magic):' | |||
|
254 | 254 | # the locals argument. But we do carry a user_global_ns namespace |
|
255 | 255 | # given as the exec 'globals' argument, This is useful in embedding |
|
256 | 256 | # situations where the ipython shell opens in a context where the |
|
257 | # distinction between locals and globals is meaningful. | |
|
257 | # distinction between locals and globals is meaningful. For | |
|
258 | # non-embedded contexts, it is just the same object as the user_ns dict. | |
|
258 | 259 | |
|
259 | 260 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
260 | 261 | # level as a dict instead of a module. This is a manual fix, but I |
@@ -284,14 +285,12 b' class InteractiveShell(object,Magic):' | |||
|
284 | 285 | # These routines return properly built dicts as needed by the rest of |
|
285 | 286 | # the code, and can also be used by extension writers to generate |
|
286 | 287 | # properly initialized namespaces. |
|
287 |
user_ns = IPython.ipapi.make_user_ns(user_ns |
|
|
288 | user_global_ns = IPython.ipapi.make_user_global_ns(user_global_ns) | |
|
289 | ||
|
288 | user_ns, user_global_ns = IPython.ipapi.make_user_namespaces(user_ns, | |
|
289 | user_global_ns) | |
|
290 | ||
|
290 | 291 | # Assign namespaces |
|
291 | 292 | # This is the namespace where all normal user variables live |
|
292 | 293 | self.user_ns = user_ns |
|
293 | # Embedded instances require a separate namespace for globals. | |
|
294 | # Normally this one is unused by non-embedded instances. | |
|
295 | 294 | self.user_global_ns = user_global_ns |
|
296 | 295 | # A namespace to keep track of internal data structures to prevent |
|
297 | 296 | # them from cluttering user-visible stuff. Will be updated later |
@@ -2071,16 +2070,7 b' want to merge them back into the new files.""" % locals()' | |||
|
2071 | 2070 | try: |
|
2072 | 2071 | try: |
|
2073 | 2072 | self.hooks.pre_runcode_hook() |
|
2074 | # Embedded instances require separate global/local namespaces | |
|
2075 | # so they can see both the surrounding (local) namespace and | |
|
2076 | # the module-level globals when called inside another function. | |
|
2077 | if self.embedded: | |
|
2078 | exec code_obj in self.user_global_ns, self.user_ns | |
|
2079 | # Normal (non-embedded) instances should only have a single | |
|
2080 | # namespace for user code execution, otherwise functions won't | |
|
2081 | # see interactive top-level globals. | |
|
2082 | else: | |
|
2083 | exec code_obj in self.user_ns | |
|
2073 | exec code_obj in self.user_global_ns, self.user_ns | |
|
2084 | 2074 | finally: |
|
2085 | 2075 | # Reset our crash handler in place |
|
2086 | 2076 | sys.excepthook = old_excepthook |
General Comments 0
You need to be logged in to leave comments.
Login now