From 8fff75b0f43ab2d7ad640ae9e7e5b697c777ccff 2010-04-23 03:43:59 From: Fernando Perez Date: 2010-04-23 03:43:59 Subject: [PATCH] Fix autocall misfiring on print for python 2.6 and newer. Closes Bug #414967 Thanks to Robert Kern for the key tip on how to read the compiler flag needed to detect if print_function had been imported. --- diff --git a/IPython/core/magic.py b/IPython/core/magic.py index 85b5a79..b27c577 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -16,6 +16,7 @@ #----------------------------------------------------------------------------- import __builtin__ +import __future__ import bdb import inspect import os @@ -209,9 +210,7 @@ python-profiler package from non-free.""") Has special code to detect magic functions. """ - oname = oname.strip() - alias_ns = None if namespaces is None: # Namespaces to search in: @@ -225,8 +224,16 @@ python-profiler package from non-free.""") alias_ns = self.shell.alias_manager.alias_table # initialize results to 'null' - found = 0; obj = None; ospace = None; ds = None; - ismagic = 0; isalias = 0; parent = None + found = False; obj = None; ospace = None; ds = None; + ismagic = False; isalias = False; parent = None + + # We need to special-case 'print', which as of python2.6 registers as a + # function but should only be treated as one if print_function was + # loaded with a future import. In this case, just bail. + if (oname == 'print' and not (self.shell.compile.compiler.flags & + __future__.CO_FUTURE_PRINT_FUNCTION)): + return {'found':found, 'obj':obj, 'namespace':ospace, + 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} # Look for the given name by splitting it in parts. If the head is # found, then we look for all the remaining parts as members, and only @@ -251,10 +258,10 @@ python-profiler package from non-free.""") break else: # If we finish the for loop (no break), we got all members - found = 1 + found = True ospace = nsname if ns == alias_ns: - isalias = 1 + isalias = True break # namespace loop # Try to see if it's magic @@ -263,14 +270,14 @@ python-profiler package from non-free.""") oname = oname[1:] obj = getattr(self,'magic_'+oname,None) if obj is not None: - found = 1 + found = True ospace = 'IPython internal' - ismagic = 1 + ismagic = True # Last try: special-case some literals like '', [], {}, etc: if not found and oname_head in ["''",'""','[]','{}','()']: obj = eval(oname_head) - found = 1 + found = True ospace = 'Interactive' return {'found':found, 'obj':obj, 'namespace':ospace,