From ee0f717b7051688a01302d0dcc45e501d58631b4 2013-10-03 22:27:55 From: Thomas Kluyver Date: 2013-10-03 22:27:55 Subject: [PATCH] Merge pull request #4237 from takluyver/keywords-shadow Keywords should shadow magic functions --- diff --git a/IPython/core/prefilter.py b/IPython/core/prefilter.py index 15aba20..ae7a01b 100644 --- a/IPython/core/prefilter.py +++ b/IPython/core/prefilter.py @@ -24,6 +24,7 @@ Authors: # Imports #----------------------------------------------------------------------------- +from keyword import iskeyword import re from IPython.core.autocall import IPyAutocall @@ -80,7 +81,8 @@ def is_shadowed(identifier, ip): # This is much safer than calling ofind, which can change state return (identifier in ip.user_ns \ or identifier in ip.user_global_ns \ - or identifier in ip.ns_table['builtin']) + or identifier in ip.ns_table['builtin']\ + or iskeyword(identifier)) #----------------------------------------------------------------------------- diff --git a/IPython/core/tests/test_prefilter.py b/IPython/core/tests/test_prefilter.py index b54abf0..83d8e90 100644 --- a/IPython/core/tests/test_prefilter.py +++ b/IPython/core/tests/test_prefilter.py @@ -23,6 +23,30 @@ def test_prefilter(): for raw, correct in pairs: nt.assert_equal(ip.prefilter(raw), correct) +def test_prefilter_shadowed(): + def dummy_magic(line): pass + + prev_automagic_state = ip.automagic + ip.automagic = True + ip.autocall = 0 + + try: + # These should not be transformed - they are shadowed by other names + for name in ['if', 'zip', 'get_ipython']: # keyword, builtin, global + ip.register_magic_function(dummy_magic, magic_name=name) + res = ip.prefilter(name+' foo') + nt.assert_equal(res, name+' foo') + del ip.magics_manager.magics['line'][name] + + # These should be transformed + for name in ['fi', 'piz', 'nohtypi_teg']: + ip.register_magic_function(dummy_magic, magic_name=name) + res = ip.prefilter(name+' foo') + nt.assert_not_equal(res, name+' foo') + del ip.magics_manager.magics['line'][name] + + finally: + ip.automagic = prev_automagic_state def test_autocall_binops(): """See https://github.com/ipython/ipython/issues/81"""