Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion IPython/core/prefilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# Imports
#-----------------------------------------------------------------------------

from keyword import iskeyword
import re

from IPython.core.autocall import IPyAutocall
Expand Down Expand Up @@ -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))


#-----------------------------------------------------------------------------
Expand Down
24 changes: 24 additions & 0 deletions IPython/core/tests/test_prefilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down