Comments (5)
Hi @soupytwist
I tested here your last commit with the following file and everything was ok:
try:
print('1')
try:
error = 1/0
except:
print('2')
finally:
print('3')
But, when I commented the print('1')
the parser could not recognize the nested try
file: test.py
try:
# print('1')
try:
error = 1/0
except:
print('2')
finally:
print('3')
Running the file to show that it's a valid syntax =)
(pasta_py27) william@laptop:/opt/test_pasta$ python --version
Python 2.7.14
(pasta_py27) william@laptop:/opt/test_pasta$ python test.py
2
3
Running the parse on this test.py file:
(pasta_py27) william@laptop:/opt/test_pasta$ python script.py
Traceback (most recent call last):
File "script.py", line 8, in <module>
tree = pasta.parse(file.read())
File "/opt/pasta/pasta/__init__.py", line 25, in parse
annotator.visit(t)
File "/opt/pasta/pasta/base/annotate.py", line 1055, in visit
super(AstAnnotator, self).visit(node)
File "/opt/pasta/pasta/base/annotate.py", line 115, in visit
super(BaseVisitor, self).visit(node)
File "/home/william/miniconda3/envs/google_pasta_py27/lib/python2.7/ast.py", line 241, in visit
return visitor(node)
File "/opt/pasta/pasta/base/annotate.py", line 79, in wrapped
f(self, node, *args, **kwargs)
File "/opt/pasta/pasta/base/annotate.py", line 168, in visit_Module
self.generic_visit(node)
File "/home/william/miniconda3/envs/google_pasta_py27/lib/python2.7/ast.py", line 249, in generic_visit
self.visit(item)
File "/opt/pasta/pasta/base/annotate.py", line 1055, in visit
super(AstAnnotator, self).visit(node)
File "/opt/pasta/pasta/base/annotate.py", line 115, in visit
super(BaseVisitor, self).visit(node)
File "/home/william/miniconda3/envs/google_pasta_py27/lib/python2.7/ast.py", line 241, in visit
return visitor(node)
File "/opt/pasta/pasta/base/annotate.py", line 79, in wrapped
f(self, node, *args, **kwargs)
File "/opt/pasta/pasta/base/annotate.py", line 373, in visit_TryFinally
self.visit(stmt)
File "/opt/pasta/pasta/base/annotate.py", line 1055, in visit
super(AstAnnotator, self).visit(node)
File "/opt/pasta/pasta/base/annotate.py", line 115, in visit
super(BaseVisitor, self).visit(node)
File "/home/william/miniconda3/envs/google_pasta_py27/lib/python2.7/ast.py", line 241, in visit
return visitor(node)
File "/opt/pasta/pasta/base/annotate.py", line 79, in wrapped
f(self, node, *args, **kwargs)
File "/opt/pasta/pasta/base/annotate.py", line 386, in visit_TryExcept
self.visit(stmt)
File "/opt/pasta/pasta/base/annotate.py", line 1055, in visit
super(AstAnnotator, self).visit(node)
File "/opt/pasta/pasta/base/annotate.py", line 115, in visit
super(BaseVisitor, self).visit(node)
File "/home/william/miniconda3/envs/google_pasta_py27/lib/python2.7/ast.py", line 241, in visit
return visitor(node)
File "/opt/pasta/pasta/base/annotate.py", line 43, in wrapped
f(self, node, *args, **kwargs)
File "/opt/pasta/pasta/base/annotate.py", line 477, in visit_Assign
self.visit(target)
File "/opt/pasta/pasta/base/annotate.py", line 1055, in visit
super(AstAnnotator, self).visit(node)
File "/opt/pasta/pasta/base/annotate.py", line 115, in visit
super(BaseVisitor, self).visit(node)
File "/home/william/miniconda3/envs/google_pasta_py27/lib/python2.7/ast.py", line 241, in visit
return visitor(node)
File "/opt/pasta/pasta/base/annotate.py", line 43, in wrapped
f(self, node, *args, **kwargs)
File "/opt/pasta/pasta/base/annotate.py", line 754, in visit_Name
self.token(node.id)
File "/opt/pasta/pasta/base/annotate.py", line 1118, in token
token_val, token.src, token.start[0], token.line))
pasta.base.annotate.AnnotationError: Expected 'error' but found 'try'
line 3: try:
cc @nicoddemus
from pasta.
Hmm, actually it seems this is an issue in 2.7.6 as well. Thanks for reporting!
from pasta.
Aye, this is a difficult case because these two parse to the same ast:
try:
try:
a
except:
b
finally:
c
and
try:
a
except:
b
finally:
c
Both result in:
"Module(body=[TryFinally(body=[TryExcept(body=[Expr(value=Name(id='a', ctx=Load()))], handlers=[ExceptHandler(type=None, name=None, body=[Expr(value=Name(id='b', ctx=Load()))])], orelse=[])], finalbody=[Expr(value=Name(id='c', ctx=Load()))])])"
The extra desugaring throws off the annotator.
from pasta.
Hmm should we commit to supporting cases like this?
It is a bit weird to respect it since the ast doesn't really respect this pattern (which I assume is because these two forms are semantically equivalent anyway).
from pasta.
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.
from pasta.
Related Issues (20)
- Python 3.10 support HOT 3
- Default formatting Subscript slice is inconsistent in python3.9+
- Support for with statement with multiple arguments
- Support for 'nonlocal' keywords
- Colgate
- f-strings over multiple lines HOT 4
- Auto-detect indentation in a file and reuse on unformatted code
- Support mixing tabs and spaces in python2 HOT 1
- Lacking test covered in codegen_test.py
- Bad indentation when rewriting funcion body (new bug) HOT 3
- Bad indentation when using try..finally HOT 2
- Fails to parse usages of metaclass HOT 3
- Support formatting-preserving insertion into collections
- IndentationError when appending new node to function body HOT 3
- Support for async code HOT 1
- Extend scope.analyze to support PEP 484 forward references
- CI doesn't works
- Some tests are broken HOT 1
- MIgrate from setuptools test to tox
- Incorrect rename behavior
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pasta.