Skip to content

Commit bb757f6

Browse files
author
Adam Hrbac
committed
Refractor and fix MISSING
1 parent e5b6769 commit bb757f6

File tree

9 files changed

+39
-36
lines changed

9 files changed

+39
-36
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5050
import com.oracle.graal.python.builtins.PythonBuiltins;
5151
import com.oracle.graal.python.builtins.objects.PNone;
52-
import com.oracle.graal.python.builtins.objects.contextvars.PContext;
52+
import com.oracle.graal.python.builtins.objects.contextvars.PContextVarsContext;
5353
import com.oracle.graal.python.builtins.objects.contextvars.PContextVar;
5454
import com.oracle.graal.python.nodes.ErrorMessages;
5555
import com.oracle.graal.python.nodes.PRaiseNode;
@@ -79,8 +79,8 @@ public abstract static class GetDefaultEncodingNode extends PythonBuiltinNode {
7979
@Specialization
8080
protected Object copyCtx() {
8181
PythonContext.PythonThreadState threadState = getContext().getThreadState(getLanguage());
82-
PContext ret = factory().createContextVarsContext();
83-
ret.contextVarValues = threadState.getContext(factory()).contextVarValues;
82+
PContextVarsContext ret = factory().createContextVarsContext();
83+
ret.contextVarValues = threadState.getContextVarsContext().contextVarValues;
8484
return ret;
8585
}
8686
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
7474
@GenerateNodeFactory
7575
public abstract static class GetContextVar extends PythonBinaryBuiltinNode {
7676
@Specialization
77-
Object get(PContext self, Object key, @Cached PRaiseNode raise) {
77+
Object get(PContextVarsContext self, Object key, @Cached PRaiseNode raise) {
7878
return getContextVar(self, key, null, raise);
7979
}
8080
}
@@ -83,7 +83,7 @@ Object get(PContext self, Object key, @Cached PRaiseNode raise) {
8383
@GenerateNodeFactory
8484
public abstract static class Run extends PythonBuiltinNode {
8585
@Specialization
86-
Object get(VirtualFrame frame, PContext self, Object fun, Object[] args, PKeyword[] keywords, @Cached CallNode call) {
86+
Object get(VirtualFrame frame, PContextVarsContext self, Object fun, Object[] args, PKeyword[] keywords, @Cached CallNode call) {
8787
PythonContext.PythonThreadState threadState = getContext().getThreadState(getLanguage());
8888
self.enter(threadState);
8989
try {
@@ -98,8 +98,8 @@ Object get(VirtualFrame frame, PContext self, Object fun, Object[] args, PKeywor
9898
@GenerateNodeFactory
9999
public abstract static class Copy extends PythonUnaryBuiltinNode {
100100
@Specialization
101-
Object doCopy(PContext self) {
102-
PContext ret = factory().createContextVarsContext();
101+
Object doCopy(PContextVarsContext self) {
102+
PContextVarsContext ret = factory().createContextVarsContext();
103103
ret.contextVarValues = self.contextVarValues;
104104
return ret;
105105
}
@@ -109,18 +109,18 @@ Object doCopy(PContext self) {
109109
@GenerateNodeFactory
110110
public abstract static class GetMethod extends PythonBuiltinNode {
111111
@Specialization(guards = "isNoValue(def)")
112-
Object doGet(PContext self, Object key, Object def, @Cached PRaiseNode raise) {
112+
Object doGet(PContextVarsContext self, Object key, Object def, @Cached PRaiseNode raise) {
113113
return doGetDefault(self, key, PNone.NONE, raise);
114114
}
115115

116116
@Specialization(guards = "!isNoValue(def)")
117-
Object doGetDefault(PContext self, Object key, Object def, @Cached PRaiseNode raise) {
117+
Object doGetDefault(PContextVarsContext self, Object key, Object def, @Cached PRaiseNode raise) {
118118
return getContextVar(self, key, def, raise);
119119
}
120120

121121
}
122122

123-
private static Object getContextVar(PContext self, Object key, Object def, @Cached PRaiseNode raise) {
123+
private static Object getContextVar(PContextVarsContext self, Object key, Object def, @Cached PRaiseNode raise) {
124124
if (key instanceof PContextVar) {
125125
PContextVar ctxVar = (PContextVar) key;
126126
Object value = self.contextVarValues.lookup(key, ctxVar.getHash());

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,15 @@ Object set(VirtualFrame frame, PContextVar self, Object value) {
115115
@GenerateNodeFactory
116116
public abstract static class ResetNode extends PythonBinaryBuiltinNode {
117117
@Specialization
118-
Object reset(PContextVar self, PToken token, @Cached PRaiseNode raise) {
118+
Object reset(PContextVar self, PContextVarsToken token, @Cached PRaiseNode raise) {
119119
if (self == token.getVar()) {
120120
PythonContext.PythonThreadState threadState = getContext().getThreadState(getLanguage());
121-
self.setValue(threadState, token.getOldValue());
121+
if (token.getOldValue() == PContextVarsToken.MISSING) {
122+
PContextVarsContext context = threadState.getContextVarsContext();
123+
context.contextVarValues = context.contextVarValues.without(self, self.getHash());
124+
} else {
125+
self.setValue(threadState, token.getOldValue());
126+
}
122127
} else {
123128
throw raise.raise(ValueError, ErrorMessages.TOKEN_FOR_DIFFERENT_CONTEXTVAR, token);
124129
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
4444
import com.oracle.graal.python.runtime.PythonContext;
45-
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
4645
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4746
import com.oracle.truffle.api.object.Shape;
4847
import com.oracle.truffle.api.strings.TruffleString;
@@ -75,12 +74,12 @@ public Object getDefault() {
7574

7675
@TruffleBoundary
7776
public Object getValue(PythonContext.PythonThreadState state) {
78-
return state.getContext(PythonObjectFactory.getUncached()).contextVarValues.lookup(this, getHash());
77+
return state.getContextVarsContext().contextVarValues.lookup(this, getHash());
7978
}
8079

8180
@TruffleBoundary
8281
public void setValue(PythonContext.PythonThreadState state, Object value) {
83-
PContext current = state.getContext(PythonObjectFactory.getUncached());
82+
PContextVarsContext current = state.getContextVarsContext();
8483
current.contextVarValues = current.contextVarValues.withEntry(new Hamt.Entry(this, getHash(), value));
8584
}
8685
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/contextvars/PContext.java renamed to graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/contextvars/PContextVarsContext.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,29 @@
4545
import com.oracle.graal.python.nodes.ErrorMessages;
4646
import com.oracle.graal.python.nodes.PRaiseNode;
4747
import com.oracle.graal.python.runtime.PythonContext;
48-
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
4948
import com.oracle.truffle.api.object.Shape;
5049

51-
public class PContext extends PythonBuiltinObject {
50+
public class PContextVarsContext extends PythonBuiltinObject {
5251
public Hamt contextVarValues = new Hamt();
53-
private PContext previousContext = null;
52+
private PContextVarsContext previousContext = null;
5453

5554
public void enter(PythonContext.PythonThreadState threadState) {
5655
if (previousContext != null) {
5756
throw PRaiseNode.getUncached().raise(PythonBuiltinClassType.RuntimeError, ErrorMessages.CANNOT_ENTER_CONTEXT_ALREADY_ENTERED, this);
5857
}
59-
previousContext = threadState.getContext(PythonObjectFactory.getUncached());
58+
previousContext = threadState.getContextVarsContext();
6059
assert previousContext != null : "ThreadState had null Context. This should not happen";
6160
threadState.setContext(this);
6261
}
6362

6463
public void leave(PythonContext.PythonThreadState threadState) {
65-
assert threadState.getContext(null) == this : "leaving a context which is not currently entered";
64+
assert threadState.getContextVarsContext() == this : "leaving a context which is not currently entered";
6665
assert previousContext != null : "entered context has no previous context";
6766
threadState.setContext(previousContext);
6867
previousContext = null;
6968
}
7069

71-
public PContext(Object cls, Shape instanceShape) {
70+
public PContextVarsContext(Object cls, Shape instanceShape) {
7271
super(cls, instanceShape);
7372
}
7473
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@
4343
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
4444
import com.oracle.truffle.api.object.Shape;
4545

46-
public class PToken extends PythonBuiltinObject {
46+
public class PContextVarsToken extends PythonBuiltinObject {
4747
public static final Object MISSING = new Object();
4848
private final PContextVar var;
4949
private final Object oldValue;
5050

51-
public PToken(PContextVar var, Object oldValue, Object cls, Shape instanceShape) {
51+
public PContextVarsToken(PContextVar var, Object oldValue, Object cls, Shape instanceShape) {
5252
super(cls, instanceShape);
5353
this.var = var;
5454
this.oldValue = oldValue;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
6464
@GenerateNodeFactory
6565
public abstract static class VarAttr extends PythonBuiltinNode {
6666
@Specialization
67-
public Object doVar(PToken self) {
67+
public Object doVar(PContextVarsToken self) {
6868
return self.getVar();
6969
}
7070
}
@@ -73,7 +73,7 @@ public Object doVar(PToken self) {
7373
@GenerateNodeFactory
7474
public abstract static class OldValueAttr extends PythonBuiltinNode {
7575
@Specialization
76-
public Object doOld(PToken self) {
76+
public Object doOld(PContextVarsToken self) {
7777
return self.getOldValue();
7878
}
7979
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
import java.util.concurrent.locks.ReentrantLock;
8888
import java.util.logging.Level;
8989

90+
import com.oracle.graal.python.builtins.objects.contextvars.PContextVarsContext;
9091
import org.graalvm.nativeimage.ImageInfo;
9192
import org.graalvm.options.OptionKey;
9293

@@ -108,7 +109,6 @@
108109
import com.oracle.graal.python.builtins.objects.common.HashingStorage;
109110
import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary;
110111
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
111-
import com.oracle.graal.python.builtins.objects.contextvars.PContext;
112112
import com.oracle.graal.python.builtins.objects.dict.PDict;
113113
import com.oracle.graal.python.builtins.objects.frame.PFrame;
114114
import com.oracle.graal.python.builtins.objects.frame.PFrame.Reference;
@@ -249,7 +249,7 @@ public static final class PythonThreadState {
249249

250250
boolean contextInitialized = false;
251251

252-
PContext context;
252+
PContextVarsContext context;
253253

254254
/*
255255
* The constructor needs to have this particular signature such that we can use it for
@@ -334,15 +334,15 @@ public void setNativeWrapper(PThreadState nativeWrapper) {
334334
this.nativeWrapper = nativeWrapper;
335335
}
336336

337-
public PContext getContext(PythonObjectFactory factory) {
337+
public PContextVarsContext getContextVarsContext() {
338338
if (!contextInitialized) {
339-
context = factory.createContextVarsContext();
339+
context = PythonObjectFactory.getUncached().createContextVarsContext();
340340
contextInitialized = true;
341341
}
342342
return context;
343343
}
344344

345-
public void setContext(PContext context) {
345+
public void setContext(PContextVarsContext context) {
346346
this.context = context;
347347
}
348348

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@
7878
import com.oracle.graal.python.builtins.objects.common.LocalsStorage;
7979
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
8080
import com.oracle.graal.python.builtins.objects.complex.PComplex;
81-
import com.oracle.graal.python.builtins.objects.contextvars.PContext;
81+
import com.oracle.graal.python.builtins.objects.contextvars.PContextVarsContext;
8282
import com.oracle.graal.python.builtins.objects.contextvars.PContextVar;
83-
import com.oracle.graal.python.builtins.objects.contextvars.PToken;
83+
import com.oracle.graal.python.builtins.objects.contextvars.PContextVarsToken;
8484
import com.oracle.graal.python.builtins.objects.deque.PDeque;
8585
import com.oracle.graal.python.builtins.objects.deque.PDequeIter;
8686
import com.oracle.graal.python.builtins.objects.dict.PDefaultDict;
@@ -1448,11 +1448,11 @@ public final PContextVar createContextVar(TruffleString name, Object def) {
14481448
return trace(new PContextVar(PythonBuiltinClassType.ContextVar, getShape(PythonBuiltinClassType.ContextVar), name, def));
14491449
}
14501450

1451-
public final PContext createContextVarsContext() {
1452-
return trace(new PContext(PythonBuiltinClassType.ContextVarsContext, getShape(PythonBuiltinClassType.ContextVarsContext)));
1451+
public final PContextVarsContext createContextVarsContext() {
1452+
return trace(new PContextVarsContext(PythonBuiltinClassType.ContextVarsContext, getShape(PythonBuiltinClassType.ContextVarsContext)));
14531453
}
14541454

1455-
public final PToken createContextVarsToken(PContextVar var, Object oldValue) {
1456-
return trace(new PToken(var, oldValue, PythonBuiltinClassType.ContextVarsToken, getShape(PythonBuiltinClassType.ContextVarsToken)));
1455+
public final PContextVarsToken createContextVarsToken(PContextVar var, Object oldValue) {
1456+
return trace(new PContextVarsToken(var, oldValue == null ? PContextVarsToken.MISSING : oldValue, PythonBuiltinClassType.ContextVarsToken, getShape(PythonBuiltinClassType.ContextVarsToken)));
14571457
}
14581458
}

0 commit comments

Comments
 (0)