GithubHelp home page GithubHelp logo

Comments (5)

williamjamir avatar williamjamir commented on May 18, 2024 1

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.

soupytwist avatar soupytwist commented on May 18, 2024

Hmm, actually it seems this is an issue in 2.7.6 as well. Thanks for reporting!

from pasta.

soupytwist avatar soupytwist commented on May 18, 2024

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.

akov avatar akov commented on May 18, 2024

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.

soupytwist avatar soupytwist commented on May 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.