AttributeError: 'NoneType' object has no attribute 'children' at line 443 in visitor.py
Hi, when I tried to translate my java code it raised:
Traceback (most recent call last):
File "/opt/bin/j2py", line 259, in
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)
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?
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)