Failed to recognize nested try:
See original GitHub issueHi,
Running the parser against a file with “nested try” results in the following error:
pasta.base.annotate.AnnotationError: Expected 'error' but found 'try'
line 2: try:
I found this issue with
Python 2.7.8 :: Continuum Analytics, Inc.
Python 2.7.9 :: Continuum Analytics, Inc.
Python 2.7.12
(In python 3.4.5 works just fine)
Test.py
try:
try:
error = 1/0
except:
print('1')
finally:
print('2')
Script.py:
import os
import pasta
path = r'test.py'
with open(path) as file:
tree = pasta.parse(file.read())
Full Log:
Traceback (most recent call last):
File "script.py", line 10, in <module>
tree = pasta.parse(file.read())
File "w:\william\repo_pasta\pasta\__init__.py", line 25, in parse
annotator.visit(t)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 1048, in visit
super(AstAnnotator, self).visit(node)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 115, in visit
super(BaseVisitor, self).visit(node)
File "W:\alfasim\envs\test_pasta\lib\ast.py", line 241, in visit
return visitor(node)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 79, in wrapped
f(self, node, *args, **kwargs)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 168, in visit_Module
self.generic_visit(node)
File "W:\alfasim\envs\test_pasta\lib\ast.py", line 249, in generic_visit
self.visit(item)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 1048, in visit
super(AstAnnotator, self).visit(node)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 115, in visit
super(BaseVisitor, self).visit(node)
File "W:\alfasim\envs\test_pasta\lib\ast.py", line 241, in visit
return visitor(node)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 79, in wrapped
f(self, node, *args, **kwargs)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 367, in visit_TryFinally
self.visit(stmt)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 1048, in visit
super(AstAnnotator, self).visit(node)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 115, in visit
super(BaseVisitor, self).visit(node)
File "W:\alfasim\envs\test_pasta\lib\ast.py", line 241, in visit
return visitor(node)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 79, in wrapped
f(self, node, *args, **kwargs)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 379, in visit_TryExcept
self.visit(stmt)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 1048, in visit
super(AstAnnotator, self).visit(node)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 115, in visit
super(BaseVisitor, self).visit(node)
File "W:\alfasim\envs\test_pasta\lib\ast.py", line 241, in visit
return visitor(node)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 43, in wrapped
f(self, node, *args, **kwargs)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 470, in visit_Assign
self.visit(target)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 1048, in visit
super(AstAnnotator, self).visit(node)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 115, in visit
super(BaseVisitor, self).visit(node)
File "W:\alfasim\envs\test_pasta\lib\ast.py", line 241, in visit
return visitor(node)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 43, in wrapped
f(self, node, *args, **kwargs)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 747, in visit_Name
self.token(node.id)
File "w:\william\repo_pasta\pasta\base\annotate.py", line 1106, in token
token_val, token.src, token.start[0], token.line))
pasta.base.annotate.AnnotationError: Expected 'error' but found 'try'
line 2: try:
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Are nested Try/Catch blocks a bad idea? - Stack Overflow
There are certain circumstances where they're a good idea, e.g. one try/catch for the whole method and another inside a loop as you...
Read more >Avoiding Nested Try-Catch in Java - Veracode
In fact a common use case that leads to a nested Try-Catch happens when error recovery from an exception itself may raise an...
Read more >Is using nested try-catch blocks an anti-pattern?
Show activity on this post. In some cases a nested Try-Catch is unavoidable. For instance when the error recovery code itself can throw...
Read more >Nested Try Catch not working properly - Learn - UiPath Forum
In a nested try catch, the inner “catch” happens first, is my understanding, but can you please correct me if I am wrong?...
Read more >PHP Nested Try-Catch | Rollbar
In PHP, Try-catch blocks can be nested up to any desired levels and are handled in reverse order of appearance. Learn more now!...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @soupytwist
I tested here your last commit with the following file and everything was ok:
But, when I commented the
print('1')
the parser could not recognize the nested tryfile: test.py
Running the file to show that it’s a valid syntax =)
Running the parse on this test.py file:
cc @nicoddemus
re: @akov
It’s legal python, and by principle #1, we need to be able to output exactly as it is read in. I plan to support it.
@williamjamir Thanks for the follow-up! Will work on fixing this.