##// END OF EJS Templates
Simplify logic for deciding when to rewrite expressions for autocall.
Bradley Froehle -
Show More
@@ -826,30 +826,33 b' class AutoHandler(PrefilterHandler):'
826 elif esc == ESC_PAREN:
826 elif esc == ESC_PAREN:
827 newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
827 newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
828 else:
828 else:
829 # Auto-paren.
829 # Auto-paren.
830 # We only apply it to argument-less calls if the autocall
830 if force_auto:
831 # parameter is set to 2. We only need to check that autocall is <
831 # Don't rewrite if it is already a call.
832 # 2, since this function isn't called unless it's at least 1.
832 do_rewrite = not the_rest.startswith('(')
833 if (not the_rest and (self.shell.autocall < 2) and not force_auto) \
834 or the_rest.startswith("("):
835 newcmd = '%s %s' % (ifun,the_rest)
836 auto_rewrite = False
837 else:
833 else:
838 if not force_auto and the_rest.startswith('['):
834 if not the_rest:
839 if hasattr(obj,'__getitem__'):
835 # We only apply it to argument-less calls if the autocall
840 # Don't autocall in this case: item access for an object
836 # parameter is set to 2.
841 # which is BOTH callable and implements __getitem__.
837 do_rewrite = (self.shell.autocall >= 2)
842 newcmd = '%s %s' % (ifun,the_rest)
838 elif the_rest.startswith('[') and hasattr(obj, '__getitem__'):
843 auto_rewrite = False
839 # Don't autocall in this case: item access for an object
844 else:
840 # which is BOTH callable and implements __getitem__.
845 # if the object doesn't support [] access, go ahead and
841 do_rewrite = False
846 # autocall
847 newcmd = '%s(%s)' % (ifun.rstrip(),the_rest)
848 elif the_rest.endswith(';'):
849 newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
850 else:
842 else:
851 newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
843 do_rewrite = True
852
844
845 # Figure out the rewritten command
846 if do_rewrite:
847 if the_rest.endswith(';'):
848 newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
849 else:
850 newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
851 else:
852 normal_handler = self.prefilter_manager.get_handler_by_name('normal')
853 return normal_handler.handle(line_info)
854
855 # Display the rewritten call
853 if auto_rewrite:
856 if auto_rewrite:
854 self.shell.auto_rewrite_input(newcmd)
857 self.shell.auto_rewrite_input(newcmd)
855
858
@@ -133,7 +133,7 b' def test_handlers():'
133 ('len "abc"', 'len "abc"'),
133 ('len "abc"', 'len "abc"'),
134 ('autocallable', 'autocallable()'),
134 ('autocallable', 'autocallable()'),
135 # Don't add extra brackets (gh-1117)
135 # Don't add extra brackets (gh-1117)
136 ('autocallable()', 'autocallable ()'),
136 ('autocallable()', 'autocallable()'),
137 (",list 1 2 3", 'list("1", "2", "3")'),
137 (",list 1 2 3", 'list("1", "2", "3")'),
138 (";list 1 2 3", 'list("1 2 3")'),
138 (";list 1 2 3", 'list("1 2 3")'),
139 ("/len range(1,4)", 'len(range(1,4))'),
139 ("/len range(1,4)", 'len(range(1,4))'),
@@ -150,7 +150,7 b' def test_handlers():'
150 ('len [1,2]', 'len([1,2])'), # len doesn't support __getitem__...
150 ('len [1,2]', 'len([1,2])'), # len doesn't support __getitem__...
151 ('call_idx [1]', 'call_idx [1]'), # call_idx *does*..
151 ('call_idx [1]', 'call_idx [1]'), # call_idx *does*..
152 ('call_idx 1', 'call_idx(1)'),
152 ('call_idx 1', 'call_idx(1)'),
153 ('len', 'len '), # only at 2 does it auto-call on single args
153 ('len', 'len'), # only at 2 does it auto-call on single args
154 ])
154 ])
155 ip.magic('autocall 2')
155 ip.magic('autocall 2')
156 run([
156 run([
General Comments 0
You need to be logged in to leave comments. Login now