Repository navigation
Description
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
jgandert commentedon Dec 29, 2014
I executed j2py OpenSimplexNoise.java and I got the following exception:
@spacelis Could you create a pull request with your patch?
maurolacy commentedon Jul 8, 2016
This error is caused by a for loop without a block scope. To reproduce:
When adding braces at the for scope, it works:
The patch above avoids the error, but incorrect Python code is generated. This patch fixes the issue (fixed in my fork):