Skip to content

Commit a47ca66

Browse files
committed
[GR-36954] Intrinsify _contextvars.
PullRequest: graalpython/2151
2 parents 4b72be5 + 230b5df commit a47ca66

File tree

8 files changed

+257
-81
lines changed

8 files changed

+257
-81
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@
204204
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyDebugHandleBuiltins;
205205
import com.oracle.graal.python.builtins.objects.code.CodeBuiltins;
206206
import com.oracle.graal.python.builtins.objects.complex.ComplexBuiltins;
207+
import com.oracle.graal.python.builtins.objects.contextvars.ContextVarBuiltins;
207208
import com.oracle.graal.python.builtins.objects.deque.DequeBuiltins;
208209
import com.oracle.graal.python.builtins.objects.deque.DequeIterBuiltins;
209210
import com.oracle.graal.python.builtins.objects.dict.DefaultDictBuiltins;
@@ -375,7 +376,6 @@ private static String[] initializeCoreFiles() {
375376
"_sysconfig",
376377
"zipimport",
377378
"java",
378-
"_contextvars",
379379
"pip_hook",
380380
"_struct"));
381381
// add service loader defined python file extensions
@@ -537,6 +537,7 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
537537
new WeakRefModuleBuiltins(),
538538
new ReferenceTypeBuiltins(),
539539
new WarningsModuleBuiltins(),
540+
new ContextVarBuiltins(),
540541
// exceptions
541542
new SystemExitBuiltins(),
542543
new ImportErrorBuiltins(),

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/PythonBuiltinClassType.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package com.oracle.graal.python.builtins;
2727

2828
import static com.oracle.graal.python.nodes.BuiltinNames.BUILTINS;
29+
import static com.oracle.graal.python.nodes.BuiltinNames.CONTEXTVARS;
2930
import static com.oracle.graal.python.nodes.BuiltinNames.DEFAULTDICT;
3031
import static com.oracle.graal.python.nodes.BuiltinNames.DEQUE;
3132
import static com.oracle.graal.python.nodes.BuiltinNames.DEQUE_ITER;
@@ -367,6 +368,11 @@ public enum PythonBuiltinClassType implements TruffleObject {
367368
UnicodeWarning("UnicodeWarning", BUILTINS, Flags.EXCEPTION),
368369
UserWarning("UserWarning", BUILTINS, Flags.EXCEPTION),
369370

371+
// contextvars
372+
ContextVarsToken("Token", CONTEXTVARS, Flags.PUBLIC_DERIVED_WODICT),
373+
ContextVarsContext("Context", CONTEXTVARS, Flags.PUBLIC_DERIVED_WODICT),
374+
ContextVar("ContextVar", CONTEXTVARS, Flags.PUBLIC_DERIVED_WODICT),
375+
370376
// A marker for @Builtin that is not a class. Must always come last.
371377
nil(null);
372378

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ContextvarsModuleBuiltins.java

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -40,20 +40,70 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules;
4242

43-
import java.util.Collections;
43+
import static com.oracle.graal.python.nodes.BuiltinNames.CONTEXTVARS;
44+
45+
import com.oracle.graal.python.builtins.Builtin;
4446
import java.util.List;
4547

4648
import com.oracle.graal.python.builtins.CoreFunctions;
49+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4750
import com.oracle.graal.python.builtins.PythonBuiltins;
51+
import com.oracle.graal.python.builtins.objects.PNone;
52+
import com.oracle.graal.python.builtins.objects.contextvars.PContextVar;
53+
import com.oracle.graal.python.lib.PyObjectLookupAttr;
54+
import com.oracle.graal.python.nodes.call.CallNode;
4855
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
56+
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
57+
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
58+
import com.oracle.graal.python.nodes.statement.ImportNode;
59+
import com.oracle.truffle.api.dsl.Cached;
60+
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
4961
import com.oracle.truffle.api.dsl.NodeFactory;
62+
import com.oracle.truffle.api.dsl.Specialization;
63+
import com.oracle.truffle.api.frame.VirtualFrame;
5064

51-
@CoreFunctions(defineModule = "_contextvars")
65+
@CoreFunctions(defineModule = CONTEXTVARS)
5266
public class ContextvarsModuleBuiltins extends PythonBuiltins {
5367

5468
@Override
5569
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
56-
return Collections.emptyList();
70+
return ContextvarsModuleBuiltinsFactory.getFactories();
71+
}
72+
73+
@Builtin(name = "copy_context", minNumOfPositionalArgs = 0)
74+
@GenerateNodeFactory
75+
public abstract static class GetDefaultEncodingNode extends PythonBuiltinNode {
76+
@Specialization
77+
protected Object copyCtx() {
78+
throw raise(PythonBuiltinClassType.NotImplementedError);
79+
}
80+
}
81+
82+
@Builtin(name = "ContextVar", minNumOfPositionalArgs = 2, parameterNames = {"cls", "name", "default"}, constructsClass = PythonBuiltinClassType.ContextVar)
83+
@GenerateNodeFactory
84+
public abstract static class ContextVarNode extends PythonTernaryBuiltinNode {
85+
@Specialization
86+
protected Object construct(VirtualFrame frame, Object cls, String name, PNone def,
87+
@Cached("createImportThreading()") ImportNode.ImportExpression threadingImport,
88+
@Cached PyObjectLookupAttr lookupAttrNode,
89+
@Cached CallNode callNode) {
90+
return constructDef(frame, cls, name, PContextVar.NO_DEFAULT, threadingImport, lookupAttrNode, callNode);
91+
}
92+
93+
@Specialization(guards = "!isPNone(def)")
94+
protected Object constructDef(VirtualFrame frame, Object cls, String name, Object def,
95+
@Cached("createImportThreading()") ImportNode.ImportExpression threadingImport,
96+
@Cached PyObjectLookupAttr lookupAttrNode,
97+
@Cached CallNode callNode) {
98+
Object threading = threadingImport.execute(frame);
99+
Object localCallable = lookupAttrNode.execute(frame, threading, "local");
100+
Object local = callNode.execute(frame, localCallable);
101+
return factory().createContextVar(name, def, local);
102+
}
103+
104+
protected ImportNode.ImportExpression createImportThreading() {
105+
return ImportNode.createAsExpression("threading");
106+
}
57107
}
58108

59109
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.builtins.objects.contextvars;
42+
43+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.LookupError;
44+
45+
import com.oracle.graal.python.builtins.Builtin;
46+
import java.util.List;
47+
48+
import com.oracle.graal.python.builtins.CoreFunctions;
49+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
50+
import com.oracle.graal.python.builtins.PythonBuiltins;
51+
import com.oracle.graal.python.builtins.objects.PNone;
52+
import com.oracle.graal.python.lib.PyObjectLookupAttr;
53+
import com.oracle.graal.python.lib.PyObjectSetAttr;
54+
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
55+
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
56+
import com.oracle.truffle.api.dsl.Cached;
57+
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
58+
import com.oracle.truffle.api.dsl.NodeFactory;
59+
import com.oracle.truffle.api.dsl.Specialization;
60+
import com.oracle.truffle.api.frame.VirtualFrame;
61+
62+
@CoreFunctions(extendClasses = PythonBuiltinClassType.ContextVar)
63+
public final class ContextVarBuiltins extends PythonBuiltins {
64+
65+
@Override
66+
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
67+
return ContextVarBuiltinsFactory.getFactories();
68+
}
69+
70+
@Builtin(name = "get", minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2)
71+
@GenerateNodeFactory
72+
public abstract static class GetNode extends PythonBinaryBuiltinNode {
73+
@Specialization
74+
Object get(VirtualFrame frame, PContextVar self, PNone def,
75+
@Cached PyObjectLookupAttr lookupAtrrNode) {
76+
return get(frame, self, PContextVar.NO_DEFAULT, lookupAtrrNode);
77+
}
78+
79+
@Specialization(guards = "!isPNone(def)")
80+
Object get(VirtualFrame frame, PContextVar self, Object def,
81+
@Cached PyObjectLookupAttr lookupAtrrNode) {
82+
Object value = lookupAtrrNode.execute(frame, self.getLocal(), "value");
83+
if (value != PNone.NO_VALUE) {
84+
return value;
85+
}
86+
if (def != PContextVar.NO_DEFAULT) {
87+
return def;
88+
}
89+
if (self.getDefault() != PContextVar.NO_DEFAULT) {
90+
return self.getDefault();
91+
}
92+
throw raise(LookupError);
93+
}
94+
}
95+
96+
@Builtin(name = "set", minNumOfPositionalArgs = 2)
97+
@GenerateNodeFactory
98+
public abstract static class SetNode extends PythonBinaryBuiltinNode {
99+
@Specialization
100+
static Object get(VirtualFrame frame, PContextVar self, Object value,
101+
@Cached PyObjectSetAttr setAtrrNode) {
102+
setAtrrNode.execute(frame, self.getLocal(), "value", value);
103+
return PNone.NONE;
104+
}
105+
}
106+
107+
@Builtin(name = "reset", minNumOfPositionalArgs = 2)
108+
@GenerateNodeFactory
109+
public abstract static class ResetNode extends PythonBinaryBuiltinNode {
110+
@SuppressWarnings("unused")
111+
@Specialization
112+
static Object reset(PContextVar self, Object token) {
113+
return PNone.NONE;
114+
}
115+
}
116+
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.builtins.objects.contextvars;
42+
43+
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
44+
import com.oracle.truffle.api.object.Shape;
45+
46+
public final class PContextVar extends PythonBuiltinObject {
47+
private final String name;
48+
private final Object def;
49+
private final Object local;
50+
51+
public static final Object NO_DEFAULT = new Object();
52+
53+
public PContextVar(Object cls, Shape instanceShape, String name, Object def, Object local) {
54+
super(cls, instanceShape);
55+
this.name = name;
56+
this.def = def;
57+
this.local = local;
58+
}
59+
60+
public String getName() {
61+
return name;
62+
}
63+
64+
public Object getDefault() {
65+
return def;
66+
}
67+
68+
public Object getLocal() {
69+
return local;
70+
}
71+
72+
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/BuiltinNames.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ public abstract class BuiltinNames {
172172
public static final String WRAPPER_DESCRIPTOR = "wrapper_descriptor";
173173
public static final String SIMPLE_QUEUE = "SimpleQueue";
174174
public static final String EMPTY = "Empty";
175+
public static final String CONTEXTVARS = "_contextvars";
175176

176177
public static final String DICT_KEYITERATOR = "dict_keyiterator";
177178
public static final String DICT_VALUEITERATOR = "dict_valueiterator";

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import com.oracle.graal.python.builtins.objects.common.LocalsStorage;
8181
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
8282
import com.oracle.graal.python.builtins.objects.complex.PComplex;
83+
import com.oracle.graal.python.builtins.objects.contextvars.PContextVar;
8384
import com.oracle.graal.python.builtins.objects.deque.PDeque;
8485
import com.oracle.graal.python.builtins.objects.deque.PDequeIter;
8586
import com.oracle.graal.python.builtins.objects.dict.PDefaultDict;
@@ -1430,4 +1431,8 @@ public final PSimpleQueue createSimpleQueue(Object cls) {
14301431
public final PDebugHandle createDebugHandle(GraalHPyHandle handle) {
14311432
return trace(new PDebugHandle(PythonBuiltinClassType.DebugHandle, getShape(PythonBuiltinClassType.DebugHandle), handle));
14321433
}
1434+
1435+
public final PContextVar createContextVar(String name, Object def, Object local) {
1436+
return trace(new PContextVar(PythonBuiltinClassType.ContextVar, getShape(PythonBuiltinClassType.ContextVar), name, def, local));
1437+
}
14331438
}

0 commit comments

Comments
 (0)