##// END OF EJS Templates
return an specific error for non python
Martín Gaitán -
Show More
@@ -30,7 +30,7 b' from IPython.testing.skipdoctest import skip_doctest'
30 30 from IPython.utils import py3compat
31 31 from IPython.utils.contexts import preserve_keys
32 32 from IPython.utils.path import get_py_filename, unquote_filename
33 from IPython.utils.warn import warn
33 from IPython.utils.warn import warn, error
34 34 from IPython.utils.text import get_text_list
35 35
36 36 #-----------------------------------------------------------------------------
@@ -99,11 +99,9 b' def extract_symbols(code, symbols):'
99 99 (["class A: pass", "def b(): return 42"], ['z'])
100 100 """
101 101 symbols = symbols.split(',')
102 try:
103 py_code = ast.parse(code)
104 except SyntaxError:
105 # ignores non python code
106 return [], symbols
102
103 # this will raise SyntaxError if code isn't valid Python
104 py_code = ast.parse(code)
107 105
108 106 marks = [(getattr(s, 'name', None), s.lineno) for s in py_code.body]
109 107 code = code.split('\n')
@@ -303,7 +301,13 b' class CodeMagics(Magics):'
303 301 contents = self.shell.find_user_code(args)
304 302
305 303 if 's' in opts:
306 blocks, not_found = extract_symbols(contents, opts['s'])
304 try:
305 blocks, not_found = extract_symbols(contents, opts['s'])
306 except SyntaxError:
307 # non python code
308 error("Unable to parse the input as valid Python code")
309 return
310
307 311 if len(not_found) == 1:
308 312 warn('The symbol `%s` was not found' % not_found[0])
309 313 elif len(not_found) > 1:
@@ -74,12 +74,13 b' def test_extract_symbols():'
74 74 nt.assert_equal(code.extract_symbols(source, symbols), exp)
75 75
76 76
77 def test_extract_symbols_ignores_non_python_code():
77 def test_extract_symbols_raises_exception_with_non_python_code():
78 78 source = ("=begin A Ruby program :)=end\n"
79 79 "def hello\n"
80 80 "puts 'Hello world'\n"
81 81 "end")
82 nt.assert_equal(code.extract_symbols(source, "hello"), ([], ['hello']))
82 with nt.assert_raises(SyntaxError):
83 code.extract_symbols(source, "hello")
83 84
84 85
85 86 def test_rehashx():
General Comments 0
You need to be logged in to leave comments. Login now