question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Failed to recognize nested try:

See original GitHub issue

Hi,

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:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
williamjamircommented, Jan 17, 2018

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

0reactions
soupytwistcommented, Jan 30, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found