Skip to content

Commit 15da5f6

Browse files
committed
[GR-23264]Make test_super pass
PullRequest: graalpython/1203
2 parents 0ebb8d2 + 4e80558 commit 15da5f6

File tree

102 files changed

+7926
-7455
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+7926
-7455
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/parser/ClassDefTests.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,58 @@ private void checkScopeAndTree() throws Exception {
239239
checkScopeFromFile(testFile, true);
240240
checkTreeFromFile(testFile, true);
241241
}
242+
243+
@Test
244+
public void using__class__01() throws Exception {
245+
checkScopeAndTree(
246+
"class X:\n" +
247+
" def fn(self):\n" +
248+
" return __class__\n");
249+
}
250+
251+
@Test
252+
public void using__class__02() throws Exception {
253+
checkScopeAndTree(
254+
"class X:\n" +
255+
" def createY(this):\n" +
256+
" class Y:\n" +
257+
" z = __class__\n" +
258+
" def methodY(this):\n" +
259+
" y = __class__\n" +
260+
" return Y\n" +
261+
" def methodX(this):\n" +
262+
" yy = __class__\n");
263+
}
264+
265+
@Test
266+
public void using__class__03() throws Exception {
267+
checkScopeAndTree(
268+
"class XN:\n" +
269+
" def fn1(self):\n" +
270+
" nonlocal __class__\n" +
271+
" __class__ = YN\n" +
272+
" def fn2(self):\n" +
273+
" return __class__\n" +
274+
" def fn3(slef):\n" +
275+
" nonlocal __class__\n" +
276+
" return __class__\n");
277+
}
278+
279+
@Test
280+
public void usingSuper01() throws Exception {
281+
checkScopeAndTree(
282+
"class X:\n" +
283+
" def run(self):\n" +
284+
" def f():\n" +
285+
" super()\n");
286+
}
287+
288+
@Test
289+
public void usingSuper02() throws Exception {
290+
checkScopeAndTree(
291+
"class X:\n" +
292+
" def run(self):\n" +
293+
" super()\n");
294+
}
295+
242296
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
import unittest
41+
from test.support import run_unittest
42+
import types
43+
44+
def find_code_object(t):
45+
for ob in t:
46+
if type(ob) is types.CodeType:
47+
return ob
48+
49+
class BasicTests(unittest.TestCase):
50+
51+
def tearDown(self):
52+
nonlocal __class__
53+
__class__ = BasicTests
54+
55+
def test_locals01(self):
56+
57+
class X:
58+
self.assertFalse ('__class__' in locals(), "__class__ can not be available in class function")
59+
def methodX(this):
60+
self.assertFalse ('__class__' in locals(), "__class__ should not be in locals() in a method, where is not used")
61+
X().methodX()
62+
63+
64+
def test_locals02(self):
65+
66+
class X:
67+
self.assertFalse ('__class__' in locals(), "__class__ can not be available in class function")
68+
def methodX(this):
69+
z = __class__
70+
self.assertTrue ('__class__' in locals(), "__class__ has to be in locals() in a method, where is used")
71+
self.assertEqual(z, X, "__class__ has to contain the class object X")
72+
X().methodX()
73+
74+
75+
def test_inClassFunction(self):
76+
77+
class X:
78+
def createY(this):
79+
class Y:
80+
z = __class__
81+
self.assertFalse ('__class__' in locals(), "__class__ can not be available in class function")
82+
self.assertEqual(z, X, "__class__ has to contain the enclosing class object in a class function")
83+
def methodY(this):
84+
y = __class__
85+
self.assertTrue ('__class__' in locals(), "__class__ has to be in locals() in a method, where is used")
86+
self.assertEqual(y, Y, "__class__ has to contain the class object Y")
87+
return Y
88+
89+
def methodX(this):
90+
yy = __class__
91+
self.assertTrue ('__class__' in locals(), "__class__ has to be in locals() in a method, where is used")
92+
self.assertEqual(yy, X, "__class__ has to contain the class object X")
93+
X().methodX()
94+
X().createY()().methodY()
95+
96+
def test_dir_in_method_without_usage(self):
97+
98+
class X:
99+
def method(this):
100+
self.assertFalse('__class__' in dir(), "__class__ can not be listed (dir()) in method without usage")
101+
X().method()
102+
103+
def test_dir_in_method_with_usage(self):
104+
105+
class X66:
106+
def method(this):
107+
y = __class__
108+
self.assertTrue('__class__' in dir(), "__class__ has to be listed (dir()) in method with usage")
109+
self.assertEqual(y.__name__, "X66")
110+
X66().method()
111+
112+
def test_dir_in_method_overwrite_class(self):
113+
114+
class X66:
115+
def method(this):
116+
__class__ = 10
117+
self.assertTrue('__class__' in dir(), "__class__ has to be listed (dir()) in method with usage")
118+
self.assertEqual(__class__, 10)
119+
X66().method()
120+
121+
def test_cells_empty(self):
122+
123+
class X:
124+
pass
125+
126+
co = find_code_object(self.test_cells_empty.__code__.co_consts)
127+
self.assertEqual('X', co.co_name, "Expected code object with name 'X'")
128+
self.assertFalse('__class__' in co.co_cellvars, "__class__ should not be in cellvars of X")
129+
self.assertFalse('__class__' in co.co_freevars, "__class__ should not be in freevars of X")
130+
131+
def test_only_freevars(self):
132+
133+
class X22:
134+
def m(self):
135+
z = __class__
136+
137+
co = find_code_object(self.test_only_freevars.__code__.co_consts)
138+
self.assertEqual('X22', co.co_name, "Expected code object with name 'X'")
139+
self.assertTrue('__class__' in co.co_cellvars, "__class__ should not be in cellvars of X")
140+
self.assertFalse('__class__' in co.co_freevars, "__class__ should not be in freevars of X")
141+
co = X22.m.__code__
142+
self.assertFalse('__class__' in co.co_cellvars, "__class__ should not be in cellvars of X.m")
143+
self.assertTrue('__class__' in co.co_freevars, "__class__ has to be in freevars of X.m")
144+
145+
def test_nonlocal__class__usage(self):
146+
class YN:
147+
pass
148+
class XN:
149+
def fn1(self):
150+
nonlocal __class__
151+
__class__ = YN
152+
def fn2(self):
153+
return __class__
154+
def fn3(slef):
155+
nonlocal __class__
156+
return __class__
157+
158+
x = XN()
159+
self.assertIs(XN, x.fn2())
160+
self.assertIs(XN, x.fn3())
161+
x.fn1()
162+
self.assertIs(YN, x.fn2())
163+
self.assertIs(YN, x.fn3())
164+
165+
if __name__ == '__main__':
166+
unittest.main()

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_descr.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_deepcopy_recursive
1818
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_diamond_inheritance
1919
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_dict_constructors
20+
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_dir
2021
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_doc_descriptor
2122
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_dynamics
2223
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_evil_type_name

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_super.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
*graalpython.lib-python.3.test.test_super.TestSuper.test___class___instancemethod
44
*graalpython.lib-python.3.test.test_super.TestSuper.test___class___new
55
*graalpython.lib-python.3.test.test_super.TestSuper.test___class___staticmethod
6+
*graalpython.lib-python.3.test.test_super.TestSuper.test___classcell___expected_behaviour
67
*graalpython.lib-python.3.test.test_super.TestSuper.test___classcell___overwrite
78
*graalpython.lib-python.3.test.test_super.TestSuper.test_basics_working
89
*graalpython.lib-python.3.test.test_super.TestSuper.test_cell_as_self
910
*graalpython.lib-python.3.test.test_super.TestSuper.test_class_getattr_working
1011
*graalpython.lib-python.3.test.test_super.TestSuper.test_class_methods_still_working
12+
*graalpython.lib-python.3.test.test_super.TestSuper.test_obscure_super_errors
1113
*graalpython.lib-python.3.test.test_super.TestSuper.test_subclass_no_override_working
1214
*graalpython.lib-python.3.test.test_super.TestSuper.test_super_in_class_methods_working
1315
*graalpython.lib-python.3.test.test_super.TestSuper.test_super_init_leaks
1416
*graalpython.lib-python.3.test.test_super.TestSuper.test_super_with_closure
1517
*graalpython.lib-python.3.test.test_super.TestSuper.test_unbound_method_transfer_working
18+
*graalpython.lib-python.3.test.test_super.TestSuper.test_various___class___pathologies

graalpython/com.oracle.graal.python.test/testData/goldenFiles/ClassDefTests/classDef01.scope

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ Scope: []
55
FreeVars: Empty
66
Scope: foo
77
Kind: Class
8-
FrameDescriptor: [__class__]
9-
CellVars: [__class__]
8+
FrameDescriptor: Empty
9+
CellVars: Empty
1010
FreeVars: Empty

graalpython/com.oracle.graal.python.test/testData/goldenFiles/ClassDefTests/classDef01.tast

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ ModuleRootNode Name: <module 'classDef01'> SourceSection: [0,16]`class foo():pas
2222
FreeVarSlots: None
2323
ExecutionSlots:
2424
FreeVarsSlots: None
25-
CellVarsSlots: __class__,
25+
CellVarsSlots: None
2626
ClassBodyRootNode SourceSection: [0,16]`class foo():pass`
2727
Name: foo
2828
Signature: varArgs=False, varKeywordArgs=False, noArguments=False, positionalOnly=True, requiresKeywordArgs=False
2929
Param Names: namespace
30-
CelVars: __class__
30+
CelVars: None
3131
FreeVars: None
3232
NeedsCellFrame: False
33-
FrameDescriptor: 1 slots [__class__]
33+
FrameDescriptor: Empty
3434
ExecutionSlots:
3535
FreeVarsSlots: None
36-
CellVarsSlots: __class__,
36+
CellVarsSlots: None
3737
InnerRootNode SourceSection: [0,16]`class foo():pass`
3838
ReturnTargetNode SourceSection: [0,16]`class foo():pass`
3939
Body: BlockNode SourceSection: None
@@ -49,13 +49,5 @@ ModuleRootNode Name: <module 'classDef01'> SourceSection: [0,16]`class foo():pas
4949
FunctionBodyNode SourceSection: [12,16]`pass`
5050
ExpressionStatementNode SourceSection: [12,16]`pass`
5151
EmptyNode SourceSection: [12,16]`pass`
52-
SetItemNodeGen SourceSection: None
53-
ArgumentExpressionNode SourceSection: None
54-
ReadIndexedArgumentNodeGen SourceSection: None
55-
Index: 0
56-
StringLiteralNode SourceSection: None
57-
ReadLocalVariableNode SourceSection: None
58-
Frame: [0,__class__,Illegal]
59-
ReadVariableFromFrameNodeGen SourceSection: None
6052
Return Expresssion: ObjectLiteralNode SourceSection: None
6153
StringLiteralNode SourceSection: None

graalpython/com.oracle.graal.python.test/testData/goldenFiles/ClassDefTests/classDef02.scope

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ Scope: []
55
FreeVars: Empty
66
Scope: foo
77
Kind: Class
8-
FrameDescriptor: [__class__]
9-
CellVars: [__class__]
8+
FrameDescriptor: Empty
9+
CellVars: Empty
1010
FreeVars: Empty

graalpython/com.oracle.graal.python.test/testData/goldenFiles/ClassDefTests/classDef02.tast

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ ModuleRootNode Name: <module 'classDef02'> SourceSection: [0,22]`class foo(objec
2222
FreeVarSlots: None
2323
ExecutionSlots:
2424
FreeVarsSlots: None
25-
CellVarsSlots: __class__,
25+
CellVarsSlots: None
2626
ClassBodyRootNode SourceSection: [0,22]`class foo(object):pa...`
2727
Name: foo
2828
Signature: varArgs=False, varKeywordArgs=False, noArguments=False, positionalOnly=True, requiresKeywordArgs=False
2929
Param Names: namespace
30-
CelVars: __class__
30+
CelVars: None
3131
FreeVars: None
3232
NeedsCellFrame: False
33-
FrameDescriptor: 1 slots [__class__]
33+
FrameDescriptor: Empty
3434
ExecutionSlots:
3535
FreeVarsSlots: None
36-
CellVarsSlots: __class__,
36+
CellVarsSlots: None
3737
InnerRootNode SourceSection: [0,22]`class foo(object):pa...`
3838
ReturnTargetNode SourceSection: [0,22]`class foo(object):pa...`
3939
Body: BlockNode SourceSection: None
@@ -49,14 +49,6 @@ ModuleRootNode Name: <module 'classDef02'> SourceSection: [0,22]`class foo(objec
4949
FunctionBodyNode SourceSection: [18,22]`pass`
5050
ExpressionStatementNode SourceSection: [18,22]`pass`
5151
EmptyNode SourceSection: [18,22]`pass`
52-
SetItemNodeGen SourceSection: None
53-
ArgumentExpressionNode SourceSection: None
54-
ReadIndexedArgumentNodeGen SourceSection: None
55-
Index: 0
56-
StringLiteralNode SourceSection: None
57-
ReadLocalVariableNode SourceSection: None
58-
Frame: [0,__class__,Illegal]
59-
ReadVariableFromFrameNodeGen SourceSection: None
6052
Return Expresssion: ObjectLiteralNode SourceSection: None
6153
StringLiteralNode SourceSection: None
6254
ReadNameNodeGen SourceSection: [10,16]`object`

graalpython/com.oracle.graal.python.test/testData/goldenFiles/ClassDefTests/classDef05.scope

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ Scope: []
1010
FreeVars: Empty
1111
Scope: DerivedClassName
1212
Kind: Class
13-
FrameDescriptor: [__class__]
14-
CellVars: [__class__]
13+
FrameDescriptor: Empty
14+
CellVars: Empty
1515
FreeVars: Empty

graalpython/com.oracle.graal.python.test/testData/goldenFiles/ClassDefTests/classDef05.tast

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ ModuleRootNode Name: <module 'classDef05'> SourceSection: [0,63]`def fn():↵ c
4949
FreeVarSlots: None
5050
ExecutionSlots:
5151
FreeVarsSlots: None
52-
CellVarsSlots: __class__,
52+
CellVarsSlots: None
5353
ClassBodyRootNode SourceSection: [12,63]`class DerivedClassNa...`
5454
Name: DerivedClassName
5555
Signature: varArgs=False, varKeywordArgs=False, noArguments=False, positionalOnly=True, requiresKeywordArgs=False
5656
Param Names: namespace
57-
CelVars: __class__
57+
CelVars: None
5858
FreeVars: None
5959
NeedsCellFrame: False
60-
FrameDescriptor: 1 slots [__class__]
60+
FrameDescriptor: Empty
6161
ExecutionSlots:
6262
FreeVarsSlots: None
63-
CellVarsSlots: __class__,
63+
CellVarsSlots: None
6464
InnerRootNode SourceSection: [12,63]`class DerivedClassNa...`
6565
ReturnTargetNode SourceSection: [12,63]`class DerivedClassNa...`
6666
Body: BlockNode SourceSection: None
@@ -76,14 +76,6 @@ ModuleRootNode Name: <module 'classDef05'> SourceSection: [0,63]`def fn():↵ c
7676
FunctionBodyNode SourceSection: [59,63]`pass`
7777
ExpressionStatementNode SourceSection: [59,63]`pass`
7878
EmptyNode SourceSection: [59,63]`pass`
79-
SetItemNodeGen SourceSection: None
80-
ArgumentExpressionNode SourceSection: None
81-
ReadIndexedArgumentNodeGen SourceSection: None
82-
Index: 0
83-
StringLiteralNode SourceSection: None
84-
ReadLocalVariableNode SourceSection: None
85-
Frame: [0,__class__,Illegal]
86-
ReadVariableFromFrameNodeGen SourceSection: None
8779
Return Expresssion: ObjectLiteralNode SourceSection: None
8880
StringLiteralNode SourceSection: None
8981
GetAttributeNodeGen SourceSection: [35,56]`modname.BaseClassNam...`

0 commit comments

Comments
 (0)