Skip to content

Commit 375935a

Browse files
committed
[GH-40] register __class__ cell var for class def scope
PullRequest: graalpython/378
2 parents 873bf44 + c8ed6b4 commit 375935a

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_object.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -128,3 +128,26 @@ def get_non_existing_attr():
128128
assert m1.my_attr == 10
129129
assert "my_attr" not in m1.__dict__
130130
assert m1.d == 10
131+
132+
133+
def test_class_attr():
134+
class AAA:
135+
def foo(self):
136+
assert __class__ == AAA
137+
assert self.__class__ == AAA
138+
139+
class BBB:
140+
pass
141+
142+
class CCC(AAA):
143+
def getclass(self):
144+
return BBB
145+
146+
__class__ = property(getclass)
147+
148+
def bar(self):
149+
assert __class__ == CCC
150+
assert self.__class__ == BBB
151+
152+
AAA().foo()
153+
CCC().bar()

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,7 +1760,7 @@ public Object visitClassdef(Python3Parser.ClassdefContext ctx) {
17601760
visitCallArglist(ctx.arglist(), argumentNodes, keywords, splatArguments);
17611761

17621762
environment.pushScope(ctx.scope);
1763-
1763+
environment.registerSpecialClassCellVar();
17641764
ExpressionNode body = asClassBody(ctx.suite().accept(this), qualName);
17651765
ClassBodyRootNode classBodyRoot = factory.createClassBodyRoot(deriveSourceSection(ctx), className, environment.getCurrentFrame(), body, environment.getExecutionCellSlots());
17661766
RootCallTarget ct = Truffle.getRuntime().createCallTarget(classBodyRoot);
@@ -1778,7 +1778,8 @@ public Object visitClassdef(Python3Parser.ClassdefContext ctx) {
17781778

17791779
ReadNode tempLocal = environment.makeTempLocalVariable();
17801780
ExpressionNode newClass = ((ExpressionNode) tempLocal).withSideEffect(
1781-
factory.createBlock(tempLocal.makeWriteNode(classDef), factory.createWriteCellVar((ExpressionNode) tempLocal, classBodyRoot, __CLASS__)));
1781+
factory.createBlock(tempLocal.makeWriteNode(classDef),
1782+
factory.createWriteCellVar((ExpressionNode) tempLocal, classBodyRoot, __CLASS__)));
17821783
return read.makeWriteNode(newClass);
17831784
}
17841785

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ private ReadNode findVariableNodeClass(String name) {
281281
if (isCellInCurrentScope(name)) {
282282
cellSlot = currentScope.findFrameSlot(name);
283283
}
284+
if (name.equals(__CLASS__)) {
285+
return (ReadNode) factory.createReadClassAttributeNode(name, null, currentScope.isFreeVar(name));
286+
}
284287
return (ReadNode) factory.createReadClassAttributeNode(name, cellSlot, currentScope.isFreeVar(name));
285288
}
286289

0 commit comments

Comments
 (0)