##// 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 826 elif esc == ESC_PAREN:
827 827 newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
828 828 else:
829 # Auto-paren.
830 # We only apply it to argument-less calls if the autocall
831 # parameter is set to 2. We only need to check that autocall is <
832 # 2, since this function isn't called unless it's at least 1.
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
829 # Auto-paren.
830 if force_auto:
831 # Don't rewrite if it is already a call.
832 do_rewrite = not the_rest.startswith('(')
837 833 else:
838 if not force_auto and the_rest.startswith('['):
839 if hasattr(obj,'__getitem__'):
840 # Don't autocall in this case: item access for an object
841 # which is BOTH callable and implements __getitem__.
842 newcmd = '%s %s' % (ifun,the_rest)
843 auto_rewrite = False
844 else:
845 # if the object doesn't support [] access, go ahead and
846 # autocall
847 newcmd = '%s(%s)' % (ifun.rstrip(),the_rest)
848 elif the_rest.endswith(';'):
849 newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
834 if not the_rest:
835 # We only apply it to argument-less calls if the autocall
836 # parameter is set to 2.
837 do_rewrite = (self.shell.autocall >= 2)
838 elif the_rest.startswith('[') and hasattr(obj, '__getitem__'):
839 # Don't autocall in this case: item access for an object
840 # which is BOTH callable and implements __getitem__.
841 do_rewrite = False
850 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 856 if auto_rewrite:
854 857 self.shell.auto_rewrite_input(newcmd)
855 858
@@ -133,7 +133,7 b' def test_handlers():'
133 133 ('len "abc"', 'len "abc"'),
134 134 ('autocallable', 'autocallable()'),
135 135 # Don't add extra brackets (gh-1117)
136 ('autocallable()', 'autocallable ()'),
136 ('autocallable()', 'autocallable()'),
137 137 (",list 1 2 3", 'list("1", "2", "3")'),
138 138 (";list 1 2 3", 'list("1 2 3")'),
139 139 ("/len range(1,4)", 'len(range(1,4))'),
@@ -150,7 +150,7 b' def test_handlers():'
150 150 ('len [1,2]', 'len([1,2])'), # len doesn't support __getitem__...
151 151 ('call_idx [1]', 'call_idx [1]'), # call_idx *does*..
152 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 155 ip.magic('autocall 2')
156 156 run([
General Comments 0
You need to be logged in to leave comments. Login now