Skip to content

Commit 4db0a17

Browse files
author
Adam Hrbac
committed
Implement more token features
1 parent 9691759 commit 4db0a17

File tree

6 files changed

+46
-3
lines changed

6 files changed

+46
-3
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/contextvars/ContextBuiltins.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,19 @@ Object doGetDefault(PContextVarsContext self, Object key, Object def,
124124

125125
}
126126

127+
@Builtin(name = "__contains__", minNumOfPositionalArgs = 2)
128+
@GenerateNodeFactory
129+
public abstract static class Contains extends PythonBuiltinNode {
130+
@Specialization
131+
boolean doIn(PContextVarsContext self, Object key) {
132+
if (key instanceof PContextVar) {
133+
PContextVar var = (PContextVar) key;
134+
return self.contextVarValues.lookup(var, var.getHash()) != null;
135+
}
136+
return false;
137+
}
138+
}
139+
127140
private static Object getContextVar(PContextVarsContext self, Object key, Object def,
128141
@Cached PRaiseNode raise) {
129142
if (key instanceof PContextVar) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/contextvars/ContextVarBuiltins.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ public abstract static class ResetNode extends PythonBinaryBuiltinNode {
118118
Object reset(PContextVar self, PContextVarsToken token,
119119
@Cached PRaiseNode raise) {
120120
if (self == token.getVar()) {
121+
token.use(raise);
121122
PythonContext.PythonThreadState threadState = getContext().getThreadState(getLanguage());
122-
if (token.getOldValue() == PContextVarsToken.MISSING) {
123+
if (token.getOldValue() == null) {
123124
PContextVarsContext context = threadState.getContextVarsContext();
124125
context.contextVarValues = context.contextVarValues.without(self, self.getHash());
125126
} else {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/contextvars/PContextVarsToken.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,32 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.contextvars;
4242

43+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4344
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
45+
import com.oracle.graal.python.nodes.ErrorMessages;
46+
import com.oracle.graal.python.nodes.PRaiseNode;
4447
import com.oracle.truffle.api.object.Shape;
4548

4649
public class PContextVarsToken extends PythonBuiltinObject {
4750
public static final Object MISSING = new Object();
4851
private final PContextVar var;
4952
private final Object oldValue;
5053

54+
private boolean used = false;
55+
5156
public PContextVarsToken(PContextVar var, Object oldValue, Object cls, Shape instanceShape) {
5257
super(cls, instanceShape);
5358
this.var = var;
5459
this.oldValue = oldValue;
5560
}
5661

62+
public void use(PRaiseNode raise) {
63+
if (used) {
64+
throw raise.raise(PythonBuiltinClassType.RuntimeError, ErrorMessages.TOKEN_ALREADY_USED, this);
65+
}
66+
used = true;
67+
}
68+
5769
public PContextVar getVar() {
5870
return var;
5971
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/contextvars/TokenBuiltins.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,23 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.contextvars;
4242

43+
import static com.oracle.graal.python.util.PythonUtils.tsLiteral;
44+
4345
import java.util.List;
4446

4547
import com.oracle.graal.python.builtins.Builtin;
4648
import com.oracle.graal.python.builtins.CoreFunctions;
49+
import com.oracle.graal.python.builtins.Python3Core;
4750
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4851
import com.oracle.graal.python.builtins.PythonBuiltins;
52+
import com.oracle.graal.python.nodes.attributes.WriteAttributeToBuiltinTypeNode;
53+
import com.oracle.graal.python.nodes.attributes.WriteAttributeToBuiltinTypeNodeGen;
4954
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5055
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
5156
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
5257
import com.oracle.truffle.api.dsl.NodeFactory;
5358
import com.oracle.truffle.api.dsl.Specialization;
59+
import com.oracle.truffle.api.strings.TruffleString;
5460

5561
@CoreFunctions(extendClasses = PythonBuiltinClassType.ContextVarsToken)
5662
public class TokenBuiltins extends PythonBuiltins {
@@ -74,7 +80,17 @@ public Object doVar(PContextVarsToken self) {
7480
public abstract static class OldValueAttr extends PythonBuiltinNode {
7581
@Specialization
7682
public Object doOld(PContextVarsToken self) {
77-
return self.getOldValue();
83+
Object oldValue = self.getOldValue();
84+
return oldValue == null ? PContextVarsToken.MISSING : oldValue;
7885
}
7986
}
87+
88+
private static final TruffleString MISSING_NAME = tsLiteral("MISSING");
89+
90+
@Override
91+
public void postInitialize(Python3Core core) {
92+
super.postInitialize(core);
93+
WriteAttributeToBuiltinTypeNode addMissing = WriteAttributeToBuiltinTypeNodeGen.getUncached();
94+
addMissing.execute(PythonBuiltinClassType.ContextVarsToken, MISSING_NAME, PContextVarsToken.MISSING);
95+
}
8096
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,4 +1458,5 @@ public abstract class ErrorMessages {
14581458
public static final TruffleString SETTRACE_NOT_IMPLEMENTED = tsLiteral("sys.settrace is only implemented for the bytecode interpreter.");
14591459
public static final TruffleString ATTRIBUTE_VALUE_MUST_BE_BOOL = tsLiteral("attribute value type must be bool");
14601460
public static final TruffleString HPY_UNEXPECTED_HPY_NULL = tsLiteral("unexpected HPy_NULL");
1461+
public static final TruffleString TOKEN_ALREADY_USED = tsLiteral("%s has already been used once");
14611462
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,6 @@ public final PContextVarsContext copyContextVarsContext(PContextVarsContext orig
14571457
}
14581458

14591459
public final PContextVarsToken createContextVarsToken(PContextVar var, Object oldValue) {
1460-
return trace(new PContextVarsToken(var, oldValue == null ? PContextVarsToken.MISSING : oldValue, PythonBuiltinClassType.ContextVarsToken, getShape(PythonBuiltinClassType.ContextVarsToken)));
1460+
return trace(new PContextVarsToken(var, oldValue, PythonBuiltinClassType.ContextVarsToken, getShape(PythonBuiltinClassType.ContextVarsToken)));
14611461
}
14621462
}

0 commit comments

Comments
 (0)