Skip to content

Commit 0f282c5

Browse files
committed
fix class decorator execution
1 parent 95ccbfe commit 0f282c5

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonTreeTranslator.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -854,23 +854,13 @@ public Object visitDecorated(Python3Parser.DecoratedContext ctx) {
854854
ExpressionNode definition;
855855
String definitionName;
856856
if (ctx.classdef() != null) {
857-
definition = (ExpressionNode) ctx.classdef().accept(this);
857+
definition = asDefinition(ctx.classdef().accept(this));
858858
definitionName = ctx.classdef().NAME().getText();
859859
} else if (ctx.funcdef() != null) {
860-
Object accept = ctx.funcdef().accept(this);
861-
if (accept instanceof WriteNode) {
862-
definition = ((WriteNode) accept).getRhs();
863-
} else {
864-
definition = (ExpressionNode) accept;
865-
}
860+
definition = asDefinition(ctx.funcdef().accept(this));
866861
definitionName = ctx.funcdef().NAME().getText();
867862
} else if (ctx.async_funcdef() != null) {
868-
Object accept = ctx.async_funcdef().accept(this);
869-
if (accept instanceof WriteNode) {
870-
definition = ((WriteNode) accept).getRhs();
871-
} else {
872-
definition = (ExpressionNode) accept;
873-
}
863+
definition = asDefinition(ctx.async_funcdef().accept(this));
874864
definitionName = ctx.async_funcdef().funcdef().NAME().getText();
875865
} else {
876866
throw new RuntimeException("unsupported decorated definition");
@@ -882,6 +872,18 @@ public Object visitDecorated(Python3Parser.DecoratedContext ctx) {
882872
return environment.findVariable(definitionName).makeWriteNode(definition);
883873
}
884874

875+
private static ExpressionNode asDefinition(Object accept) {
876+
ExpressionNode definition;
877+
if (accept instanceof WriteNode) {
878+
definition = ((WriteNode) accept).getRhs();
879+
} else if (accept instanceof ExpressionNode) {
880+
definition = (ExpressionNode) accept;
881+
} else {
882+
throw new IllegalArgumentException();
883+
}
884+
return definition;
885+
}
886+
885887
private ExpressionNode getRhsImport(Python3Parser.Dotted_nameContext ctx, ExpressionNode importNode) {
886888
ExpressionNode rhsImport = importNode;
887889
for (int i = 1;; i++) {
@@ -1707,8 +1709,13 @@ public Object visitClassdef(Python3Parser.ClassdefContext ctx) {
17071709
ExpressionNode owner = factory.createGetAttribute(factory.createBuiltinsLiteral(), __BUILD_CLASS__);
17081710
ExpressionNode classDef = PythonCallNode.create(owner, argumentNodes.toArray(new ExpressionNode[0]), keywords.toArray(new ExpressionNode[0]), splatArguments[0], splatArguments[1]);
17091711
deriveSourceSection(ctx, classDef);
1712+
17101713
ReadNode read = environment.findVariable(className);
1711-
return factory.createBlock(read.makeWriteNode(classDef), factory.createWriteCellVar((ExpressionNode) read, classBodyRoot, __CLASS__));
1714+
1715+
ReadNode tempLocal = environment.makeTempLocalVariable();
1716+
ExpressionNode newClass = ((ExpressionNode) tempLocal).withSideEffect(
1717+
factory.createBlock(tempLocal.makeWriteNode(classDef), factory.createWriteCellVar((ExpressionNode) tempLocal, classBodyRoot, __CLASS__)));
1718+
return read.makeWriteNode(newClass);
17121719
}
17131720

17141721
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)