##// END OF EJS Templates
Fixed #10322 - Made autocall to ignore raw & string literals
chillaranand -
Show More
@@ -510,6 +510,12 b' class AutocallChecker(PrefilterChecker):'
510 if not oinfo['found']:
510 if not oinfo['found']:
511 return None
511 return None
512
512
513 ignored_funs = ['b', 'f', 'r', 'u', 'br', 'rb', 'fr', 'rf']
514 ifun = line_info.ifun
515 line = line_info.line
516 if ifun.lower() in ignored_funs and (line.startswith(ifun + "'") or line.startswith(ifun + '"')):
517 return None
518
513 if callable(oinfo['obj']) \
519 if callable(oinfo['obj']) \
514 and (not self.exclude_regexp.match(line_info.the_rest)) \
520 and (not self.exclude_regexp.match(line_info.the_rest)) \
515 and self.function_name_regexp.match(line_info.ifun):
521 and self.function_name_regexp.match(line_info.ifun):
@@ -625,7 +631,7 b' class AutoHandler(PrefilterHandler):'
625 elif esc == ESC_PAREN:
631 elif esc == ESC_PAREN:
626 newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
632 newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
627 else:
633 else:
628 # Auto-paren.
634 # Auto-paren.
629 if force_auto:
635 if force_auto:
630 # Don't rewrite if it is already a call.
636 # Don't rewrite if it is already a call.
631 do_rewrite = not the_rest.startswith('(')
637 do_rewrite = not the_rest.startswith('(')
@@ -646,11 +652,11 b' class AutoHandler(PrefilterHandler):'
646 if the_rest.endswith(';'):
652 if the_rest.endswith(';'):
647 newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
653 newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
648 else:
654 else:
649 newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
655 newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
650 else:
656 else:
651 normal_handler = self.prefilter_manager.get_handler_by_name('normal')
657 normal_handler = self.prefilter_manager.get_handler_by_name('normal')
652 return normal_handler.handle(line_info)
658 return normal_handler.handle(line_info)
653
659
654 # Display the rewritten call
660 # Display the rewritten call
655 if auto_rewrite:
661 if auto_rewrite:
656 self.shell.auto_rewrite_input(newcmd)
662 self.shell.auto_rewrite_input(newcmd)
@@ -5,41 +5,69 b' directory, which we are removing. For now putting this here ensures at least'
5 we do run the test, though ultimately this functionality should all be tested
5 we do run the test, though ultimately this functionality should all be tested
6 with better-isolated tests that don't rely on the global instance in iptest.
6 with better-isolated tests that don't rely on the global instance in iptest.
7 """
7 """
8 from IPython.core.splitinput import LineInfo
9 from IPython.core.prefilter import AutocallChecker
8 from IPython.utils import py3compat
10 from IPython.utils import py3compat
11 from IPython.testing.globalipapp import get_ipython
12
13
14 ip = get_ipython()
15
9
16
10 @py3compat.doctest_refactor_print
17 @py3compat.doctest_refactor_print
11 def doctest_autocall():
18 def doctest_autocall():
12 """
19 """
13 In [1]: def f1(a,b,c):
20 In [1]: def f1(a,b,c):
14 ...: return a+b+c
21 ...: return a+b+c
15 ...:
22 ...:
16
23
17 In [2]: def f2(a):
24 In [2]: def f2(a):
18 ...: return a + a
25 ...: return a + a
19 ...:
26 ...:
20
27
21 In [3]: ;f2 a b c
28 In [3]: def r(x):
22 Out[3]: 'a b ca b c'
29 ...: return True
30 ...:
23
31
24 In [4]: assert _ == "a b ca b c"
32 In [4]: ;f2 a b c
33 Out[4]: 'a b ca b c'
25
34
26 In [5]: ,f1 a b c
35 In [5]: assert _ == "a b ca b c"
27 Out[5]: 'abc'
28
36
29 In [6]: assert _ == 'abc'
37 In [6]: ,f1 a b c
38 Out[6]: 'abc'
30
39
31 In [7]: print _
40 In [7]: assert _ == 'abc'
41
42 In [8]: print _
32 abc
43 abc
33
44
34 In [8]: /f1 1,2,3
45 In [9]: /f1 1,2,3
35 Out[8]: 6
46 Out[9]: 6
47
48 In [10]: assert _ == 6
36
49
37 In [9]: assert _ == 6
50 In [11]: /f2 4
51 Out[11]: 8
38
52
39 In [10]: /f2 4
53 In [12]: assert _ == 8
40 Out[10]: 8
41
54
42 In [11]: assert _ == 8
55 In [12]: del f1, f2
43
56
44 In [11]: del f1, f2
57 In [13]: ,r a
58 Out[13]: True
59
60 In [14]: assert _ == True
61
62 In [15]: r'a'
63 Out[15]: 'a'
64
65 In [16]: assert _ == 'a'
45 """
66 """
67
68
69 def test_autocall_should_ignore_raw_strings():
70 line_info = LineInfo("r'a'")
71 pm = ip.prefilter_manager
72 ac = AutocallChecker(shell=pm.shell, prefilter_manager=pm, config=pm.config)
73 assert ac.check(line_info) is None
General Comments 0
You need to be logged in to leave comments. Login now