1. Robert Kern
  2. kernmagic

Commits

Robert Kern  committed a5a52b5

BUG: Fix accessing the namespaces in 0.13

  • Participants
  • Parent commits 50acfe7
  • Branches default

Comments (0)

Files changed (1)

File kernmagic/mymagics.py

View file
  • Ignore whitespace
 from . import utils
 
 
+def get_shell(ipshell):
+    """ For forward compatibility with 0.13+.
+    """
+    return getattr(ipshell, 'shell', ipshell)
+
 def get_variable(ipshell, variable):
     """ Get a variable from the shell's namespace.
     """
+    ipshell = get_shell(ipshell)
     if variable not in ipshell.user_ns:
         try:
             obj = eval(variable, ipshell.user_global_ns, ipshell.user_ns)
     if args.encoding:
         contents = contents.decode(args.encoding)
 
-    self.user_ns[args.variable] = contents
+    get_shell(self).user_ns[args.variable] = contents
 
 
 @magic_arguments()
     for name in args.names:
         name = name.encode('ascii')
         var = factory(name, **kwds)
-        self.user_ns[name] = var
+        get_shell(self).user_ns[name] = var
         if not args.quiet:
             print '  %s' % name
 
         if args.no_call_func:
             raise UsageError("You cannot specify both a --call-func and "
                 "--no-call-func at the same time.")
-        global_ns = self.shell.user_global_ns
-        local_ns = self.shell.user_ns
+        global_ns = get_shell(self).user_global_ns
+        local_ns = get_shell(self).user_ns
         try:
             errcall = eval(args.call_func, global_ns, local_ns)
         except Exception, e:
     """Replace the IPython namespace with a DataContext.
 
 """
-    if hasattr(self.user_ns, 'subcontext'):
+    ipshell = get_shell(self)
+    if hasattr(ipshell.user_ns, 'subcontext'):
         # Toggle back to plain dict.
-        user_ns = self.user_ns.subcontext
+        user_ns = ipshell.user_ns.subcontext
     else:
         from enthought.contexts.api import DataContext
-        user_ns = DataContext(subcontext=self.user_ns)
+        user_ns = DataContext(subcontext=ipshell.user_ns)
         # Keep the plain dict as the globals.
-        self.user_global_ns = self.user_ns
-    self.user_ns = user_ns
+        ipshell.user_global_ns = ipshell.user_ns
+    ipshell.user_ns = user_ns
 
 
 class DoctestDemo(demo.IPythonDemo):
     from kernmagic.inplace_edit import Inplace
     args = parse_argstring(magic_inplace, arg)
 
-    inplace = Inplace.singleton(self.shell)
+    inplace = Inplace.singleton(get_shell(self))
     if args.function is None:
         # Check for commands.
         if args.dump: