Skip to content

Commit 79a3b4c

Browse files
author
Nikos Vasileiou
committed
Handle exception on AST parsing
1 parent 3953fb3 commit 79a3b4c

3 files changed

Lines changed: 23 additions & 15 deletions

File tree

tests/native/core/test_parsing.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,20 @@ def test_exceptions_on_import(self):
111111
assert ex.errors[0][0] == 'myfile.py'
112112
assert isinstance(ex.errors[0][1], SyntaxError)
113113

114-
def test_exceptions_on_function_call(self):
114+
def test_ignore_exceptions_on_function_call(self):
115115
src = TEMPLATE.format(
116116
_import='from transifex.native import translate',
117-
call1='33', # should produce an error
118-
call2='_',
117+
call1='33', # should produce an error
118+
call2='translate',
119119
)
120120
ex = Extractor()
121121
results = ex.extract_strings(src, 'myfile.py')
122-
assert results == []
123-
assert ex.errors[0][0] == 'myfile.py'
124-
assert isinstance(ex.errors[0][1], AttributeError)
125-
# Num/Constant discrepancy between python versions
126-
assert re.search(
127-
re.escape("Invalid module/function format on line 6 col 0: '") +
128-
r'Num|Constant' +
129-
re.escape("' object has no attribute 'attr'"),
130-
ex.errors[0][1].args[0]
131-
)
122+
assert results == [
123+
SourceString(
124+
u'Les données', u'opération', _comment='comment', _tags=['t1', 't2'],
125+
_charlimit=33, _occurrences=['myfile.py:7'],
126+
),
127+
]
132128

133129
def _assert(self, src):
134130
ex = Extractor()

transifex/native/parsing.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,13 @@ def visit_Call(self, node):
379379
"""
380380
self.generic_visit(node)
381381

382-
current_module_path, current_func_name = get_func_parts(node)
382+
try:
383+
# get_func_parts can raise an error is the function
384+
# call name cannot be retrieved.
385+
# In that case, just bail-out.
386+
current_module_path, current_func_name = get_func_parts(node)
387+
except:
388+
return
383389

384390
# Check against all supported function calls and if there is a match
385391
# add the node for later processing

transifex/native/tools/migrations/gettext.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,13 @@ def _transform_call(self, func_call_node, visitor, attree):
514514
level of the migration
515515
:rtype: Tuple[unicode, int]
516516
"""
517-
module_path, func_name = get_func_parts(func_call_node)
517+
try:
518+
# get_func_parts can raise an error is the function
519+
# call name cannot be retrieved.
520+
# In that case, just bail-out.
521+
module_path, func_name = get_func_parts(func_call_node)
522+
except:
523+
return
518524

519525
for import_obj in visitor.imports:
520526
if module_path != import_obj.module \

0 commit comments

Comments
 (0)