Skip to content

Commit

Permalink
Merge pull request #1121 from takluyver/i1117
Browse files Browse the repository at this point in the history
Don't transform function calls on IPyAutocall objects
  • Loading branch information
takluyver committed Dec 9, 2011
2 parents 814d5b9 + bff57c0 commit 7ea3e2f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
44 changes: 24 additions & 20 deletions IPython/core/prefilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,29 +826,33 @@ def handle(self, line_info):
elif esc == ESC_PAREN:
newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
else:
# Auto-paren.
# We only apply it to argument-less calls if the autocall
# parameter is set to 2. We only need to check that autocall is <
# 2, since this function isn't called unless it's at least 1.
if not the_rest and (self.shell.autocall < 2) and not force_auto:
newcmd = '%s %s' % (ifun,the_rest)
auto_rewrite = False
# Auto-paren.
if force_auto:
# Don't rewrite if it is already a call.
do_rewrite = not the_rest.startswith('(')
else:
if not force_auto and the_rest.startswith('['):
if hasattr(obj,'__getitem__'):
# Don't autocall in this case: item access for an object
# which is BOTH callable and implements __getitem__.
newcmd = '%s %s' % (ifun,the_rest)
auto_rewrite = False
else:
# if the object doesn't support [] access, go ahead and
# autocall
newcmd = '%s(%s)' % (ifun.rstrip(),the_rest)
elif the_rest.endswith(';'):
newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
if not the_rest:
# We only apply it to argument-less calls if the autocall
# parameter is set to 2.
do_rewrite = (self.shell.autocall >= 2)
elif the_rest.startswith('[') and hasattr(obj, '__getitem__'):
# Don't autocall in this case: item access for an object
# which is BOTH callable and implements __getitem__.
do_rewrite = False
else:
newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
do_rewrite = True

# Figure out the rewritten command
if do_rewrite:
if the_rest.endswith(';'):
newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
else:
newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
else:
normal_handler = self.prefilter_manager.get_handler_by_name('normal')
return normal_handler.handle(line_info)

# Display the rewritten call
if auto_rewrite:
self.shell.auto_rewrite_input(newcmd)

Expand Down
4 changes: 3 additions & 1 deletion IPython/core/tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ def test_handlers():
run([
('len "abc"', 'len "abc"'),
('autocallable', 'autocallable()'),
# Don't add extra brackets (gh-1117)
('autocallable()', 'autocallable()'),
(",list 1 2 3", 'list("1", "2", "3")'),
(";list 1 2 3", 'list("1 2 3")'),
("/len range(1,4)", 'len(range(1,4))'),
Expand All @@ -148,7 +150,7 @@ def test_handlers():
('len [1,2]', 'len([1,2])'), # len doesn't support __getitem__...
('call_idx [1]', 'call_idx [1]'), # call_idx *does*..
('call_idx 1', 'call_idx(1)'),
('len', 'len '), # only at 2 does it auto-call on single args
('len', 'len'), # only at 2 does it auto-call on single args
])
ip.magic('autocall 2')
run([
Expand Down

0 comments on commit 7ea3e2f

Please sign in to comment.