def my_func():
        ip0 = get_ipython0()
        if ip0 is None:
            return func()
        # We have a real ipython running...
        user_ns = ip0.IP.user_ns
        user_global_ns = ip0.IP.user_global_ns

        # Previously the isolation was attempted with a deep copy of the user
        # dicts, but we found cases where this didn't work correctly. I'm not
        # quite sure why, but basically it did damage the user namespace, such
        # that later tests stopped working correctly.  Instead we use a simpler
        # approach, just computing the list of added keys to the namespace and
        # eliminating those afterwards.  Existing keys that may have been
        # modified remain modified.  So far this has proven to be robust.

        # Compute set of old local/global keys
        old_locals = set(user_ns.keys())
        old_globals = set(user_global_ns.keys())
        try:
            out = func()
        finally:
            # Find new keys, and if any, remove them
            new_locals = set(user_ns.keys()) - old_locals
            new_globals = set(user_global_ns.keys()) - old_globals
            for k in new_locals:
                del user_ns[k]
            for k in new_globals:
                del user_global_ns[k]
        # Undo the hack at creation of PrefilterFrontEnd
        from IPython import iplib
        iplib.InteractiveShell.isthreaded = False
        return out
Пример #2
0
 def my_func(*args, **kwargs):
     ipython0 = get_ipython0().IP
     user_ns = deepcopy(ipython0.user_ns)
     global_ns = deepcopy(ipython0.global_ns)
     try:
         func(*args, **kwargs)
     finally:
         ipython0.user_ns = user_ns
         ipython0.global_ns = global_ns
Пример #3
0
 def __init__(self):
     ipython0 = get_ipython0().IP
     self.out = StringIO()
     PrefilterFrontEnd.__init__(self, ipython0=ipython0)
     # Clean up the namespace for isolation between tests
     user_ns = self.ipython0.user_ns
     # We need to keep references to things so that they don't
     # get garbage collected (this stinks).
     self.shadow_ns = dict()
     for i in self.ipython0.magic_who_ls():
         self.shadow_ns[i] = user_ns.pop(i)
     # Some more code for isolation (yeah, crazy)
     self._on_enter()
     self.out.flush()
     self.out.reset()
     self.out.truncate()