Skip to content

AttributeError: 'NoneType' object has no attribute 'children' at line 443 in visitor.py #25

Open

Description

@spacelis

Hi, when I tried to translate my java code it raised:

Traceback (most recent call last):
File "/opt/bin/j2py", line 259, in
sys.exit(runMain(configScript(sys.argv[1:])))
File "/opt/bin/j2py", line 57, in runMain
return runOneOrMany(options)
File "/opt/bin/j2py", line 83, in runOneOrMany
return runTransform(options)
File "/opt/bin/j2py", line 135, in runTransform
module.walk(tree)
File "/opt/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 86, in walk
visitor.walk(child, memo)
File "/opt/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 86, in walk
visitor.walk(child, memo)
File "/opt/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 86, in walk
visitor.walk(child, memo)
File "/opt/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 86, in walk
visitor.walk(child, memo)
File "/opt/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 86, in walk
visitor.walk(child, memo)
File "/opt/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 83, in walk
visitor = self.accept(tree, memo)
File "/opt/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 43, in accept
return call(node, memo)
File "/opt/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 471, in acceptIf
ifBlock.walk(node.children[1], memo)
File "/opt/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 86, in walk
visitor.walk(child, memo)
File "/opt/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 83, in walk
visitor = self.accept(tree, memo)
File "/opt/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 43, in accept
return call(node, memo)
File "/opt/lib/python2.7/site-packages/java2python/compiler/visitor.py", line 443, in acceptFor
if not node.firstChildOfType(tokens.BLOCK_SCOPE).children:
AttributeError: 'NoneType' object has no attribute 'children'

and found a quick walkaround by this patch:

diff --git a/java2python/compiler/visitor.py b/java2python/compiler/visitor.py
index 4109883..374e286 100644
--- a/java2python/compiler/visitor.py
+++ b/java2python/compiler/visitor.py
@@ -440,7 +440,7 @@ class MethodContent(Base):
         else:
             whileStat.expr.walk(cond, memo)
         whileBlock = self.factory.methodContent(parent=self)
-        if not node.firstChildOfType(tokens.BLOCK_SCOPE).children:
+        if node.firstChildOfType(tokens.BLOCK_SCOPE) and not node.firstChildOfType(tokens.BLOCK_SCOPE).children:
             self.factory.expr(left='pass', parent=whileBlock)
         else:
             whileBlock.walk(node.firstChildOfType(tokens.BLOCK_SCOPE), memo)

Activity

  1. jgandert commented on Dec 29, 2014

    @jgandert

    I executed j2py OpenSimplexNoise.java and I got the following exception:

    Traceback (most recent call last):
      File "/usr/local/bin/j2py", line 259, in <module>
        sys.exit(runMain(configScript(sys.argv[1:])))
      File "/usr/local/bin/j2py", line 57, in runMain
        return runOneOrMany(options)
      File "/usr/local/bin/j2py", line 83, in runOneOrMany
        return runTransform(options)
      File "/usr/local/bin/j2py", line 135, in runTransform
        module.walk(tree)
      File "/usr/local/lib/python2.7/dist-packages/java2python/compiler/visitor.py", line 86, in walk
        visitor.walk(child, memo)
      File "/usr/local/lib/python2.7/dist-packages/java2python/compiler/visitor.py", line 86, in walk
        visitor.walk(child, memo)
      File "/usr/local/lib/python2.7/dist-packages/java2python/compiler/visitor.py", line 86, in walk
        visitor.walk(child, memo)
      File "/usr/local/lib/python2.7/dist-packages/java2python/compiler/visitor.py", line 86, in walk
        visitor.walk(child, memo)
      File "/usr/local/lib/python2.7/dist-packages/java2python/compiler/visitor.py", line 86, in walk
        visitor.walk(child, memo)
      File "/usr/local/lib/python2.7/dist-packages/java2python/compiler/visitor.py", line 83, in walk
        visitor = self.accept(tree, memo)
      File "/usr/local/lib/python2.7/dist-packages/java2python/compiler/visitor.py", line 43, in accept
        return call(node, memo)
      File "/usr/local/lib/python2.7/dist-packages/java2python/compiler/visitor.py", line 443, in acceptFor
        if not node.firstChildOfType(tokens.BLOCK_SCOPE).children:
    AttributeError: 'NoneType' object has no attribute 'children'
    

    @spacelis Could you create a pull request with your patch?

  2. maurolacy commented on Jul 8, 2016

    @maurolacy

    This error is caused by a for loop without a block scope. To reproduce:

    class ForLoop3 {
        public static void main(String[] args) {
            for (int i = 0; i < 3; i++)
               System.out.println(i);
        }
    }

    When adding braces at the for scope, it works:

    class ForLoop3 {
        public static void main(String[] args) {
            for (int i = 0; i < 3; i++) {
               System.out.println(i);
            }
        }
    }

    The patch above avoids the error, but incorrect Python code is generated. This patch fixes the issue (fixed in my fork):

    diff --git a/java2python/compiler/visitor.py b/java2python/compiler/visitor.py
    index f62e53e..14b77a3 100644
    --- a/java2python/compiler/visitor.py
    +++ b/java2python/compiler/visitor.py
    @@ -434,6 +434,7 @@ class MethodContent(VarAcceptor, Base):
             tokens.FOR_EACH,
             tokens.METHOD_CALL,
             tokens.ARGUMENT_LIST,
    +        tokens.FOR,
         )
    
         def acceptExpr(self, node, memo):
    @@ -452,7 +453,9 @@ class MethodContent(VarAcceptor, Base):
             else:
                 whileStat.expr.walk(cond, memo)
             whileBlock = self.factory.methodContent(parent=self)
    -        if not node.firstChildOfType(tokens.BLOCK_SCOPE).children:
    +        if not node.firstChildOfType(tokens.BLOCK_SCOPE): # A for loop without braces
    +            whileBlock.walk(node.children[3], memo)
    +        elif not node.firstChildOfType(tokens.BLOCK_SCOPE).children:
                 self.factory.expr(left='pass', parent=whileBlock)
             else:
                 whileBlock.walk(node.firstChildOfType(tokens.BLOCK_SCOPE), memo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions