Permalink
Browse files

Simplify logic for deciding when to rewrite expressions for autocall.

  • Loading branch information...
1 parent b8b1050 commit bff57c02afa5f8b09727a32371ad66d46b0a879f @bfroehle bfroehle committed with Dec 8, 2011
Showing with 26 additions and 23 deletions.
  1. +24 −21 IPython/core/prefilter.py
  2. +2 −2 IPython/core/tests/test_handlers.py
View
@@ -826,30 +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) \
- or the_rest.startswith("("):
- 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)
@@ -133,7 +133,7 @@ def test_handlers():
('len "abc"', 'len "abc"'),
('autocallable', 'autocallable()'),
# Don't add extra brackets (gh-1117)
- ('autocallable()', 'autocallable ()'),
@bfroehle

bfroehle Dec 8, 2011

Need to revert this change as well, as the tests expect 'autocallable ()'. My bad.

@takluyver

takluyver Dec 8, 2011

Owner

It's alright, it's a change I made (I should really have done a separate commit for it, but I wanted to go to bed).

The difference is that previously, if it decided not to rewrite the line, it simply used string formatting: newcmd = '%s %s' % (ifun,the_rest), which forced a single space in there. I've told it to use the default prefilter handler (normal_handler) instead. I think the new behaviour is better - when we're not rewriting it, I'd rather not change the input at all.

+ ('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))'),
@@ -150,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
@bfroehle

bfroehle Dec 8, 2011

Oops, not sure what happened here.

+ ('len', 'len'), # only at 2 does it auto-call on single args
])
ip.magic('autocall 2')
run([

0 comments on commit bff57c0

Please sign in to comment.