Skip to content

Commit 6d4a0de

Browse files
committed
[GR-23256] Make test_scope pass
1 parent 1349c7f commit 6d4a0de

File tree

14 files changed

+190
-18
lines changed

14 files changed

+190
-18
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
43+
class BasicTests(unittest.TestCase):
44+
45+
def test_in_local(self):
46+
loc = {}
47+
glob = {}
48+
exec("""if 1:
49+
x = 3
50+
def f():
51+
x
52+
""", glob, loc)
53+
# normally the variables are placed in the local
54+
self.assertEqual(loc['x'], 3)
55+
56+
def test_put_to_global(self):
57+
loc = {}
58+
glob = {}
59+
exec("""if 1:
60+
x = 3
61+
def f():
62+
global x
63+
""", glob, loc)
64+
# if a variable is marked as global, the variable is not in the local, but in global directory
65+
self.assertEqual(glob['x'], 3)
66+
67+
def test_global_in_inner_scope(self):
68+
loc = {}
69+
glob = {'self': self}
70+
exec("""if 1:
71+
x = 4
72+
def f():
73+
self.assertEqual(x, 4)
74+
def g():
75+
global x
76+
f()
77+
""", glob, loc)
78+
self.assertEqual(glob['x'], 4)
79+
80+
def test_get_global_in_inner_scope(self):
81+
loc = {}
82+
glob = {'self': self}
83+
# if some scope declare a variable as global, then the inner scopes read
84+
# it as global as well.
85+
exec("""if 1:
86+
x = 5
87+
def f():
88+
x = 1
89+
self.assertEqual(x, 1)
90+
def g():
91+
global x
92+
def h():
93+
self.assertEqual(x, 5)
94+
h()
95+
g()
96+
f()
97+
""", glob, loc)
98+
self.assertEqual(glob['x'], 5)
99+
100+
def test_set_local_in_inner_scope(self):
101+
loc = {}
102+
glob = {'self': self}
103+
exec("""if 1:
104+
x = 6
105+
def f():
106+
x = 1
107+
self.assertEqual(x, 1)
108+
def g():
109+
global x
110+
def h():
111+
x = 11
112+
self.assertEqual(x, 11)
113+
h()
114+
g()
115+
f()
116+
""", glob, loc)
117+
self.assertEqual(glob['x'], 6)
118+
119+
120+
if __name__ == '__main__':
121+
unittest.main()

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
*graalpython.lib-python.3.test.test_scope.ScopeTests.testExtraNesting
1010
*graalpython.lib-python.3.test.test_scope.ScopeTests.testFreeVarInMethod
1111
*graalpython.lib-python.3.test.test_scope.ScopeTests.testFreeingCell
12+
*graalpython.lib-python.3.test.test_scope.ScopeTests.testGlobalInParallelNestedFunctions
1213
*graalpython.lib-python.3.test.test_scope.ScopeTests.testInteractionWithTraceFunc
1314
*graalpython.lib-python.3.test.test_scope.ScopeTests.testLambdas
15+
*graalpython.lib-python.3.test.test_scope.ScopeTests.testLeaks
1416
*graalpython.lib-python.3.test.test_scope.ScopeTests.testListCompLocalVars
1517
*graalpython.lib-python.3.test.test_scope.ScopeTests.testLocalsClass
1618
*graalpython.lib-python.3.test.test_scope.ScopeTests.testLocalsClass_WithTrace
@@ -26,6 +28,7 @@
2628
*graalpython.lib-python.3.test.test_scope.ScopeTests.testNonLocalGenerator
2729
*graalpython.lib-python.3.test.test_scope.ScopeTests.testNonLocalMethod
2830
*graalpython.lib-python.3.test.test_scope.ScopeTests.testRecursion
31+
*graalpython.lib-python.3.test.test_scope.ScopeTests.testScopeOfGlobalStmt
2932
*graalpython.lib-python.3.test.test_scope.ScopeTests.testSimpleAndRebinding
3033
*graalpython.lib-python.3.test.test_scope.ScopeTests.testSimpleNesting
3134
*graalpython.lib-python.3.test.test_scope.ScopeTests.testTopIsNotSignificant

graalpython/com.oracle.graal.python.test/testData/goldenFiles/BasicTests/global02.tast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ ModuleRootNode Name: <module 'global02'> SourceSection: [0,8]`global x`
22
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
33
FreeVars: None
44
NeedsCellFrame: False
5-
FrameDescriptor: 1 slots [x]
5+
FrameDescriptor: Empty
66
Documentation: None
77
InnerRootNode SourceSection: [0,8]`global x`
88
EmptyNode SourceSection: [0,8]`global x`

graalpython/com.oracle.graal.python.test/testData/goldenFiles/FuncDefTests/functionDef15.scope

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Scope: []
22
Kind: Module
3-
FrameDescriptor: [SOMETHING]
3+
FrameDescriptor: Empty
44
CellVars: Empty
55
FreeVars: Empty
66
Scope: install

graalpython/com.oracle.graal.python.test/testData/goldenFiles/FuncDefTests/functionDef15.tast

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ ModuleRootNode Name: <module 'functionDef15'> SourceSection: [0,114]`SOMETHING =
22
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
33
FreeVars: None
44
NeedsCellFrame: False
5-
FrameDescriptor: 1 slots [SOMETHING]
5+
FrameDescriptor: Empty
66
Documentation: None
77
InnerRootNode SourceSection: [0,114]`SOMETHING = NONE↵def...`
88
ExpressionWithSideEffects SourceSection: [0,114]`SOMETHING = NONE↵def...`
9-
WriteNameNodeGen SourceSection: [0,16]`SOMETHING = NONE`
9+
WriteGlobalNodeGen SourceSection: [0,16]`SOMETHING = NONE`
1010
Identifier: SOMETHING
11+
IsBuiltinClassProfile SourceSection: None
12+
CachedDispatchFirst SourceSection: None
1113
ReadNameNodeGen SourceSection: [12,16]`NONE`
1214
Identifier: NONE
1315
IsBuiltinClassProfile SourceSection: None

graalpython/com.oracle.graal.python.test/testData/testFiles/BasicTests/global01.scope

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Scope: []
22
Kind: Module
3-
FrameDescriptor: [c]
3+
FrameDescriptor: Empty
44
CellVars: Empty
55
FreeVars: Empty
66
Scope: add

graalpython/com.oracle.graal.python.test/testData/testFiles/BasicTests/global01.tast

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ ModuleRootNode Name: <module 'global01'> SourceSection: [0,137]`c = 0 # global v
22
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
33
FreeVars: None
44
NeedsCellFrame: False
5-
FrameDescriptor: 1 slots [c]
5+
FrameDescriptor: Empty
66
Documentation: None
77
InnerRootNode SourceSection: [0,137]`c = 0 # global varia...`
88
ExpressionWithSideEffects SourceSection: [0,137]`c = 0 # global varia...`
9-
WriteNameNodeGen SourceSection: [0,5]`c = 0`
9+
WriteGlobalNodeGen SourceSection: [0,5]`c = 0`
1010
Identifier: c
11+
IsBuiltinClassProfile SourceSection: None
12+
CachedDispatchFirst SourceSection: None
1113
IntegerLiteralNode SourceSection: [4,5]`0`
1214
Value: 0
1315
WriteNameNodeGen SourceSection: [25,111]`def add():↵ globa...`
@@ -76,7 +78,6 @@ ModuleRootNode Name: <module 'global01'> SourceSection: [0,137]`c = 0 # global v
7678
IsBuiltinClassProfile SourceSection: None
7779
CachedDispatchFirst SourceSection: None
7880
StringLiteralNode SourceSection: [123,133]`"In main:"`
79-
ReadNameNodeGen SourceSection: [135,136]`c`
81+
ReadGlobalOrBuiltinNodeGen SourceSection: [135,136]`c`
8082
Identifier: c
81-
IsBuiltinClassProfile SourceSection: None
82-
CachedDispatchFirst SourceSection: None
83+
ReadAttributeFromObjectNotTypeNodeGen SourceSection: None

graalpython/com.oracle.graal.python.test/testData/testFiles/RuntimeFileTests/site.scope

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Scope: []
22
Kind: Module
3-
FrameDescriptor: [ENABLE_USER_SITE, PREFIXES, USER_BASE, USER_SITE]
3+
FrameDescriptor: Empty
44
CellVars: Empty
55
FreeVars: Empty
66
Scope: _script

graalpython/com.oracle.graal.python.test/testData/testFiles/RuntimeFileTests/site.tast

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ ModuleRootNode Name: <module 'site'> SourceSection: [0,21269]`"""Append module s
22
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
33
FreeVars: None
44
NeedsCellFrame: False
5-
FrameDescriptor: 4 slots [USER_BASE, USER_SITE, PREFIXES, ENABLE_USER_SITE]
5+
FrameDescriptor: Empty
66
Documentation: `Append module search...`
77
InnerRootNode SourceSection: [0,21269]`"""Append module sea...`
88
ExpressionWithSideEffects SourceSection: [0,21269]`"""Append module sea...`
@@ -28,8 +28,10 @@ ModuleRootNode Name: <module 'site'> SourceSection: [0,21269]`"""Append module s
2828
ImportExpression SourceSection: None
2929
ImportNode SourceSection: None
3030
Module: _sitebuiltins
31-
WriteNameNodeGen SourceSection: [3140,3180]`PREFIXES = [sys.pref...`
31+
WriteGlobalNodeGen SourceSection: [3140,3180]`PREFIXES = [sys.pref...`
3232
Identifier: PREFIXES
33+
IsBuiltinClassProfile SourceSection: None
34+
CachedDispatchFirst SourceSection: None
3335
ListLiteralNode SourceSection: [3151,3180]`[sys.prefix, sys.exe...`
3436
GetAttributeNodeGen SourceSection: [3152,3162]`sys.prefix`
3537
GetFixedAttributeNodeGen SourceSection: None
@@ -56,14 +58,20 @@ ModuleRootNode Name: <module 'site'> SourceSection: [0,21269]`"""Append module s
5658
IsBuiltinClassProfile SourceSection: None
5759
CachedDispatchFirst SourceSection: None
5860
PythonObjectFactoryNodeGen SourceSection: None
59-
WriteNameNodeGen SourceSection: [3293,3316]`ENABLE_USER_SITE = N...`
61+
WriteGlobalNodeGen SourceSection: [3293,3316]`ENABLE_USER_SITE = N...`
6062
Identifier: ENABLE_USER_SITE
63+
IsBuiltinClassProfile SourceSection: None
64+
CachedDispatchFirst SourceSection: None
6165
ObjectLiteralNode SourceSection: [3312,3316]`None`
62-
WriteNameNodeGen SourceSection: [3490,3506]`USER_SITE = None`
66+
WriteGlobalNodeGen SourceSection: [3490,3506]`USER_SITE = None`
6367
Identifier: USER_SITE
68+
IsBuiltinClassProfile SourceSection: None
69+
CachedDispatchFirst SourceSection: None
6470
ObjectLiteralNode SourceSection: [3502,3506]`None`
65-
WriteNameNodeGen SourceSection: [3507,3523]`USER_BASE = None`
71+
WriteGlobalNodeGen SourceSection: [3507,3523]`USER_BASE = None`
6672
Identifier: USER_BASE
73+
IsBuiltinClassProfile SourceSection: None
74+
CachedDispatchFirst SourceSection: None
6775
ObjectLiteralNode SourceSection: [3519,3523]`None`
6876
WriteNameNodeGen SourceSection: [3526,3696]`def makepath(*paths)...`
6977
Identifier: makepath

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ public SSTNode registerGlobal(String[] names, int startOffset, int endOffset) {
161161
throw errors.raiseInvalidSyntax(source, createSourceSection(startOffset, endOffset), ErrorMessages.NAME_IS_ASSIGNED_BEFORE_GLOBAL, name);
162162
}
163163
scopeInfo.addExplicitGlobalVariable(name);
164-
globalScope.createSlotIfNotPresent(name);
164+
// place the global variable into global space, see test_global_statemnt.py
165+
globalScope.addExplicitGlobalVariable(name);
165166
}
166167
return new SimpleSSTNode(SimpleSSTNode.Type.EMPTY, startOffset, endOffset);
167168
}

0 commit comments

Comments
 (0)