[IPython-dev] Replacing the user context with a non-dict

Robert Kern robert.kern@gmail....
Fri Jun 27 18:59:38 CDT 2008


In iplib.py, we have this piece of code

                 # Embedded instances require separate global/local namespaces
                 # so they can see both the surrounding (local) namespace and
                 # the module-level globals when called inside another function.
                 if self.embedded:
                     exec code_obj in self.user_global_ns, self.user_ns
                 # Normal (non-embedded) instances should only have a single
                 # namespace for user code execution, otherwise functions won't
                 # see interactive top-level globals.
                 else:
                     exec code_obj in self.user_ns

Can we add a hook in here to allow the namespace to be more flexible? For some 
background: Python 2.4 allows one to use a non-dict mapping for the locals 
namespace in an exec statement, but a real dict is necessary for the globals. If 
there is only one namespace, it is considered a globals dict.

I would like to replace the namespace with a non-dict mapping to issue 
notifications when the namespace is changed. If the latter exec statement were 
changed to always execute in both namespaces (if the shell is not embedded, by 
default both user_global_ns and user_ns would point to the same dictionary), 
then I think I could replace user_ns with my non-dict and user_globals_ns with 
the real dict that underlies my non-dict. This would give me my feature while 
still avoiding the problem noted in the comment. I've tested this locally.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco



More information about the IPython-dev mailing list