Permalink
Browse files

Change namespace initialisation code so that user_ns and user_global_…

…ns will typically be the same object.
  • Loading branch information...
takluyver committed Apr 16, 2011
1 parent 7977a47 commit afe807877b8aa01c402408dd97164502c89cc26c
Showing with 33 additions and 72 deletions.
  1. +32 −71 IPython/core/interactiveshell.py
  2. +1 −1 IPython/testing/globalipapp.py
@@ -813,25 +813,44 @@ def init_create_namespaces(self, user_ns=None, user_global_ns=None):
# 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:-(.
-
- # 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, user_global_ns = self.make_user_namespaces(user_ns,
- user_global_ns)
+
+ # We must ensure that __builtin__ (without the final 's') is always
+ # available and pointing to the __builtin__ *module*. For more details:
+ # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
+
+ separate_user_ns = True
+ if user_ns is None:
+ user_ns = {}
+ separate_user_ns = False
+
+ # Set __name__ to __main__ to better match the behavior of the
+ # normal interpreter.
+ user_ns.setdefault('__name__','__main__')
+ user_ns.setdefault('__builtin__',__builtin__)
+ user_ns.setdefault('__builtins__',__builtin__)
+
+ if (user_global_ns is not None) and \
+ not isinstance(user_global_ns, dict):
+ raise TypeError("user_global_ns must be a true dict; got %r"
+ % type(user_global_ns))
# Assign namespaces
- # This is the namespace where all normal user variables live
-
+ # user_ns the namespace where all normal user variables live
self.user_ns_mod = FakeModule(user_ns)
- if type(user_ns) == dict:
+ if not separate_user_ns:
# In the normal case, we replace user_ns with a module dict, so that
# user-defined objects are in the __main__ module, and can be pickled.
self.user_ns = self.user_ns_mod.__dict__
else:
# Otherwise, we will need to manually update it after execution.
self.user_ns = user_ns
- self.user_global_ns = user_global_ns
+
+ # The global namespace will normally be identical to the user namespace.
+ # The exceptions are certain embedding cases.
+ if user_global_ns is None:
+ self.user_global_ns = self.user_ns
+ else:
+ self.user_global_ns = user_global_ns
# An auxiliary namespace that checks what parts of the user_ns were
# loaded at startup, so we can list later only variables defined in
@@ -874,8 +893,8 @@ def init_create_namespaces(self, user_ns=None, user_global_ns=None):
# A table holding all the namespaces IPython deals with, so that
# introspection facilities can search easily.
- self.ns_table = {'user':user_ns,
- 'user_global':user_global_ns,
+ self.ns_table = {'user':self.user_ns,
+ 'user_global':self.user_global_ns,
'internal':self.internal_ns,
'builtin':__builtin__.__dict__
}
@@ -888,65 +907,7 @@ def init_create_namespaces(self, user_ns=None, user_global_ns=None):
# clears them manually and carefully.
self.ns_refs_table = [ self.user_ns_hidden,
self.internal_ns, self._main_ns_cache ]
-
- def make_user_namespaces(self, user_ns=None, user_global_ns=None):
- """Return a valid local and global user interactive namespaces.
-
- 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. The default implementation returns the
- same dict for both the locals and the globals to allow functions to
- refer to variables in the namespace. Customized implementations can
- return different dicts. The locals dictionary can actually be anything
- following the basic mapping protocol of a dict, but the globals dict
- must be a true dict, not even a subclass. It is recommended that any
- custom object for the locals namespace synchronize with the globals
- dict somehow.
-
- Raises TypeError if the provided globals namespace is not a true dict.
-
- Parameters
- ----------
- user_ns : dict-like, optional
- The current user namespace. The items in this namespace should
- be included in the output. If None, an appropriate blank
- namespace should be created.
- user_global_ns : dict, optional
- The current user global namespace. The items in this namespace
- should be included in the output. If None, an appropriate
- blank namespace should be created.
-
- Returns
- -------
- A pair of dictionary-like object to be used as the local namespace
- of the interpreter and a dict to be used as the global namespace.
- """
-
-
- # We must ensure that __builtin__ (without the final 's') is always
- # available and pointing to the __builtin__ *module*. For more details:
- # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
-
- if user_ns is None:
- # Set __name__ to __main__ to better match the behavior of the
- # normal interpreter.
- user_ns = {'__name__' :'__main__',
- '__builtin__' : __builtin__,
- '__builtins__' : __builtin__,
- }
- else:
- user_ns.setdefault('__name__','__main__')
- user_ns.setdefault('__builtin__',__builtin__)
- user_ns.setdefault('__builtins__',__builtin__)
-
- if user_global_ns is None:
- user_global_ns = user_ns
- if type(user_global_ns) is not dict:
- raise TypeError("user_global_ns must be a true dict; got %r"
- % type(user_global_ns))
-
- return user_ns, user_global_ns
-
+
def init_sys_modules(self):
# We need to insert into sys.modules something that looks like a
# module but which accesses the IPython namespace, for shelve and
@@ -161,7 +161,7 @@ def start_ipython():
# Create and initialize our test-friendly IPython instance.
shell = TerminalInteractiveShell.instance(config=config,
user_ns=ipnsdict(),
- user_global_ns={'__name__':'__test_main__'}
+ user_global_ns=None
)
# A few more tweaks needed for playing nicely with doctests...

0 comments on commit afe8078

Please sign in to comment.