Skip to content

Commit 2ceea7f

Browse files
committed
[GR-30771] [GR-31267] Fix access to PythonThreadState.
PullRequest: graalpython/1759
2 parents 7ead338 + 6a3dc43 commit 2ceea7f

File tree

50 files changed

+978
-552
lines changed

Some content is hidden

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

50 files changed

+978
-552
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/builtins/modules/ConversionNodeTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.oracle.graal.python.runtime.ExecutionContext.CalleeContext;
5454
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCalleeContext;
5555
import com.oracle.graal.python.runtime.PythonContext;
56+
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
5657
import com.oracle.graal.python.runtime.exception.PException;
5758
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
5859
import com.oracle.truffle.api.RootCallTarget;
@@ -64,8 +65,10 @@ public class ConversionNodeTests {
6465
@Rule public ExpectedException expectedException = ExpectedException.none();
6566

6667
protected static Object call(Object arg, ArgumentCastNodeWithRaise castNode) {
68+
PythonLanguage language = PythonLanguage.getCurrent();
6769
final PythonContext pythonContext = PythonLanguage.getContext();
68-
RootCallTarget callTarget = Truffle.getRuntime().createCallTarget(new PRootNode(null) {
70+
71+
RootCallTarget callTarget = Truffle.getRuntime().createCallTarget(new PRootNode(language) {
6972
@Child private CalleeContext calleeContext = CalleeContext.create();
7073
@Child private ArgumentCastNodeWithRaise node = castNode;
7174

@@ -94,11 +97,12 @@ public boolean isPythonInternal() {
9497
PArguments.setGlobals(arguments, PythonObjectFactory.getUncached().createDict());
9598
PArguments.setException(arguments, PException.NO_EXCEPTION);
9699
PArguments.setArgument(arguments, 0, arg);
97-
PFrame.Reference frameInfo = IndirectCalleeContext.enter(pythonContext, arguments, callTarget);
100+
PythonThreadState threadState = pythonContext.getThreadState(language);
101+
PFrame.Reference frameInfo = IndirectCalleeContext.enter(threadState, arguments, callTarget);
98102
try {
99103
return CallTargetInvokeNode.invokeUncached(callTarget, arguments);
100104
} finally {
101-
IndirectCalleeContext.exit(pythonContext, frameInfo);
105+
IndirectCalleeContext.exit(threadState, frameInfo);
102106
}
103107
} catch (PException e) {
104108
// materialize PException's error message since we are leaving Python

graalpython/com.oracle.graal.python.test/src/tests/test_int.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ def checkPIntSpec(self, tests, byteorder, signed=False):
639639
.format(test, byteorder, signed)) from err
640640

641641

642-
def test_SignedBitEndian(self):
642+
def test_SignedBigEndian(self):
643643
# Convert integers to signed big-endian byte arrays.
644644
tests1 = {
645645
0: b'\x00',
@@ -699,7 +699,7 @@ def test_UnsignedBigEndian(self):
699699
32767: b'\x7f\xff',
700700
32768: b'\x80\x00',
701701
65535: b'\xff\xff',
702-
65536: b'\x01\x00\x00'
702+
65536: b'\x01\x00\x00',
703703
}
704704
self.check(tests3, 'big', signed=False)
705705
self.checkPIntSpec(tests3, 'big', signed=False)
@@ -736,6 +736,8 @@ def test_WrongInput(self):
736736
self.assertRaises(OverflowError, (-1).to_bytes, 2, 'big', signed=False)
737737
self.assertRaises(OverflowError, (-1).to_bytes, 2, 'little', signed=False)
738738
self.assertRaises(OverflowError, (1).to_bytes, 0, 'big')
739+
self.assertRaises(OverflowError, (4294967296).to_bytes, 4, 'big')
740+
self.assertRaises(OverflowError, (4294967296).to_bytes, 4, 'little')
739741

740742
def test_WrongTypes(self):
741743
class MyTest():

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import com.oracle.graal.python.parser.PythonParserImpl;
5959
import com.oracle.graal.python.runtime.GilNode;
6060
import com.oracle.graal.python.runtime.PythonContext;
61+
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
6162
import com.oracle.graal.python.runtime.PythonCore;
6263
import com.oracle.graal.python.runtime.PythonOptions;
6364
import com.oracle.graal.python.runtime.PythonParser.ParserMode;
@@ -190,7 +191,8 @@ public final class PythonLanguage extends TruffleLanguage<PythonContext> {
190191
private Shape cApiSymbolCache;
191192
private Shape hpySymbolCache;
192193

193-
private final ContextThreadLocal<PythonContext.PythonThreadState> threadState = createContextThreadLocal(PythonContext.PythonThreadState::new);
194+
/** For fast access to the PythonThreadState object by the owning thread. */
195+
private final ContextThreadLocal<PythonThreadState> threadState = createContextThreadLocal(PythonContext.PythonThreadState::new);
194196

195197
public final ConcurrentHashMap<String, HiddenKey> typeHiddenKeys = new ConcurrentHashMap<>(TypeBuiltins.INITIAL_HIDDEN_TYPE_KEYS);
196198

@@ -216,6 +218,14 @@ public NodeFactory getNodeFactory() {
216218
return nodeFactory;
217219
}
218220

221+
/**
222+
* <b>DO NOT DIRECTLY USE THIS METHOD !!!</b> Instead, use
223+
* {@link PythonContext#getThreadState(PythonLanguage)}}.
224+
*/
225+
public ContextThreadLocal<PythonThreadState> getThreadStateLocal() {
226+
return threadState;
227+
}
228+
219229
@Override
220230
protected void finalizeContext(PythonContext context) {
221231
context.finalizeContext();
@@ -242,7 +252,7 @@ protected boolean patchContext(PythonContext context, Env newEnv) {
242252
@Override
243253
protected PythonContext createContext(Env env) {
244254
Python3Core newCore = new Python3Core(new PythonParserImpl(env), env.isNativeAccessAllowed());
245-
final PythonContext context = new PythonContext(this, env, newCore, threadState);
255+
final PythonContext context = new PythonContext(this, env, newCore);
246256
context.initializeHomeAndPrefixPaths(env, getLanguageHome());
247257

248258
Object[] engineOptionsUnroll = this.engineOptionsStorage;
@@ -773,7 +783,7 @@ protected void initializeMultiThreading(PythonContext context) {
773783

774784
@Override
775785
protected void initializeThread(PythonContext context, Thread thread) {
776-
context.attachThread(thread);
786+
context.attachThread(thread, threadState);
777787
}
778788

779789
@Override

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
import com.oracle.graal.python.nodes.object.GetClassNode;
6161
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
6262
import com.oracle.graal.python.runtime.PythonContext;
63+
import com.oracle.graal.python.runtime.PythonContext.GetThreadStateNode;
64+
import com.oracle.graal.python.runtime.PythonContextFactory.GetThreadStateNodeGen;
6365
import com.oracle.graal.python.runtime.exception.ExceptionUtils;
6466
import com.oracle.graal.python.runtime.exception.PException;
6567
import com.oracle.truffle.api.CompilerDirectives;
@@ -85,6 +87,7 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
8587
abstract static class RegisterNode extends PythonVarargsBuiltinNode {
8688
private static class AtExitRootNode extends RootNode {
8789
@Child private CallNode callNode = CallNode.create();
90+
@Child private GetThreadStateNode getThreadStateNode = GetThreadStateNodeGen.create();
8891

8992
private final ContextReference<PythonContext> contextRef = lookupContextReference(PythonLanguage.class);
9093

@@ -95,8 +98,8 @@ protected AtExitRootNode(TruffleLanguage<?> language) {
9598
@Override
9699
public Object execute(VirtualFrame frame) {
97100
PythonContext context = contextRef.get();
98-
context.setTopFrameInfo(PFrame.Reference.EMPTY);
99-
context.setCaughtException(PException.NO_EXCEPTION);
101+
getThreadStateNode.setTopFrameInfo(context, PFrame.Reference.EMPTY);
102+
getThreadStateNode.setCaughtException(context, PException.NO_EXCEPTION);
100103

101104
Object callable = frame.getArguments()[0];
102105
Object[] arguments = (Object[]) frame.getArguments()[1];
@@ -110,8 +113,8 @@ public Object execute(VirtualFrame frame) {
110113
handleException(context, e);
111114
throw e;
112115
} finally {
113-
context.popTopFrameInfo();
114-
context.setCaughtException(null);
116+
getThreadStateNode.clearTopFrameInfo(context);
117+
getThreadStateNode.setCaughtException(context, null);
115118
}
116119
}
117120

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@
258258
import com.oracle.truffle.api.Truffle;
259259
import com.oracle.truffle.api.dsl.Cached;
260260
import com.oracle.truffle.api.dsl.Cached.Shared;
261-
import com.oracle.truffle.api.dsl.CachedContext;
261+
import com.oracle.truffle.api.dsl.CachedLanguage;
262262
import com.oracle.truffle.api.dsl.Fallback;
263263
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
264264
import com.oracle.truffle.api.dsl.ReportPolymorphism;
@@ -2149,6 +2149,7 @@ Object type(Object cls, Object obj, PNone bases, PNone dict, PKeyword[] kwds,
21492149
@Megamorphic
21502150
@Specialization(guards = "isString(wName)")
21512151
Object typeNew(VirtualFrame frame, Object cls, Object wName, PTuple bases, PDict namespaceOrig, PKeyword[] kwds,
2152+
@CachedLanguage PythonLanguage language,
21522153
@Cached GetClassNode getClassNode,
21532154
@CachedLibrary(limit = "5") PythonObjectLibrary lib,
21542155
@CachedLibrary(limit = "3") HashingStorageLibrary hashingStoragelib,
@@ -2185,8 +2186,8 @@ Object typeNew(VirtualFrame frame, Object cls, Object wName, PTuple bases, PDict
21852186
assert SpecialMethodSlot.pushInitializedTypePlaceholder();
21862187
PDict namespace = factory().createDict();
21872188
namespace.setDictStorage(initNode.execute(frame, namespaceOrig, PKeyword.EMPTY_KEYWORDS));
2188-
PythonClass newType = typeMetaclass(frame, name, bases, namespace, metaclass, lib, hashingStoragelib, getDictAttrNode, getWeakRefAttrNode, getBestBaseNode, getItemSize, writeItemSize,
2189-
isIdentifier);
2189+
PythonClass newType = typeMetaclass(frame, language, name, bases, namespace, metaclass, lib, hashingStoragelib,
2190+
getDictAttrNode, getWeakRefAttrNode, getBestBaseNode, getItemSize, writeItemSize, isIdentifier);
21902191

21912192
for (DictEntry entry : hashingStoragelib.entries(namespace.getDictStorage())) {
21922193
Object setName = getSetNameNode.execute(entry.value);
@@ -2287,7 +2288,7 @@ private String getModuleNameFromGlobals(PythonObject globals, HashingStorageLibr
22872288
}
22882289
}
22892290

2290-
private PythonClass typeMetaclass(VirtualFrame frame, String name, PTuple bases, PDict namespace, Object metaclass,
2291+
private PythonClass typeMetaclass(VirtualFrame frame, PythonLanguage language, String name, PTuple bases, PDict namespace, Object metaclass,
22912292
PythonObjectLibrary lib, HashingStorageLibrary hashingStorageLib, LookupAttributeInMRONode getDictAttrNode,
22922293
LookupAttributeInMRONode getWeakRefAttrNode, GetBestBaseClassNode getBestBaseNode, GetItemsizeNode getItemSize, WriteAttributeToObjectNode writeItemSize,
22932294
IsIdentifierNode isIdentifier) {
@@ -2425,8 +2426,7 @@ private PythonClass typeMetaclass(VirtualFrame frame, String name, PTuple bases,
24252426
}
24262427
// Make slots into a tuple
24272428
}
2428-
PythonContext context = getContextRef().get();
2429-
Object state = IndirectCallContext.enter(frame, context, this);
2429+
Object state = IndirectCallContext.enter(frame, language, getContextRef(), this);
24302430
try {
24312431
pythonClass.setAttribute(__SLOTS__, slotsObject);
24322432
if (basesArray.length > 1) {
@@ -2442,7 +2442,7 @@ private PythonClass typeMetaclass(VirtualFrame frame, String name, PTuple bases,
24422442
addNativeSlots(pythonClass, newSlots);
24432443
}
24442444
} finally {
2445-
IndirectCallContext.exit(frame, context, state);
2445+
IndirectCallContext.exit(frame, language, getContextRef(), state);
24462446
}
24472447
Object dict = LookupAttributeInMRONode.lookupSlowPath(pythonClass, __DICT__);
24482448
if (!addDict && dict == PNone.NO_VALUE) {
@@ -3375,29 +3375,29 @@ PMemoryView fromArray(@SuppressWarnings("unused") Object cls, PArray object) {
33753375
}
33763376

33773377
@Specialization
3378-
PMemoryView fromMemoryView(@SuppressWarnings("unused") Object cls, PMemoryView object,
3379-
@Shared("c") @CachedContext(PythonLanguage.class) PythonContext context) {
3378+
PMemoryView fromMemoryView(@SuppressWarnings("unused") Object cls, PMemoryView object) {
33803379
object.checkReleased(this);
3381-
return factory().createMemoryView(context, object.getManagedBuffer(), object.getOwner(), object.getLength(),
3380+
return factory().createMemoryView(getContext(), object.getManagedBuffer(), object.getOwner(), object.getLength(),
33823381
object.isReadOnly(), object.getItemSize(), object.getFormat(), object.getFormatString(), object.getDimensions(),
33833382
object.getBufferPointer(), object.getOffset(), object.getBufferShape(), object.getBufferStrides(),
33843383
object.getBufferSuboffsets(), object.getFlags());
33853384
}
33863385

33873386
@Specialization
33883387
PMemoryView fromNative(VirtualFrame frame, @SuppressWarnings("unused") Object cls, PythonAbstractNativeObject object,
3388+
@CachedLanguage PythonLanguage language,
33893389
@Cached CExtNodes.ToSulongNode toSulongNode,
33903390
@Cached CExtNodes.AsPythonObjectNode asPythonObjectNode,
33913391
@Cached PCallCapiFunction callCapiFunction,
33923392
@Cached PythonCextBuiltins.DefaultCheckFunctionResultNode checkFunctionResultNode) {
33933393
PythonContext context = getContext();
3394-
Object state = IndirectCallContext.enter(frame, context, this);
3394+
Object state = IndirectCallContext.enter(frame, language, context, this);
33953395
try {
33963396
Object result = callCapiFunction.call(FUN_PY_TRUFFLE_MEMORYVIEW_FROM_OBJECT, toSulongNode.execute(object));
33973397
checkFunctionResultNode.execute(context, FUN_PY_TRUFFLE_MEMORYVIEW_FROM_OBJECT.getName(), result);
33983398
return (PMemoryView) asPythonObjectNode.execute(result);
33993399
} finally {
3400-
IndirectCallContext.exit(frame, context, state);
3400+
IndirectCallContext.exit(frame, language, getContextRef(), state);
34013401
}
34023402
}
34033403

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
import com.oracle.truffle.api.debug.Debugger;
204204
import com.oracle.truffle.api.dsl.Cached;
205205
import com.oracle.truffle.api.dsl.Cached.Shared;
206+
import com.oracle.truffle.api.dsl.CachedLanguage;
206207
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
207208
import com.oracle.truffle.api.dsl.ImportStatic;
208209
import com.oracle.truffle.api.dsl.ReportPolymorphism;
@@ -1097,16 +1098,17 @@ final boolean doRecursiveWithNode(VirtualFrame frame, Object instance, PTuple cl
10971098

10981099
@Specialization(guards = "depth >= getNodeRecursionLimit()")
10991100
final boolean doRecursiveWithLoop(VirtualFrame frame, Object instance, PTuple clsTuple,
1101+
@CachedLanguage PythonLanguage language,
11001102
@Cached("createNonRecursive()") RecursiveBinaryCheckBaseNode node) {
1101-
Object state = IndirectCallContext.enter(frame, getContext(), this);
1103+
Object state = IndirectCallContext.enter(frame, language, getContextRef(), this);
11021104
try {
11031105
// Note: we need actual recursion to trigger the stack overflow error like CPython
11041106
// Note: we need fresh RecursiveBinaryCheckBaseNode and cannot use "this", because
11051107
// children of this executed by other specializations may assume they'll always get
11061108
// non-null frame
11071109
return callRecursiveWithNodeTruffleBoundary(instance, clsTuple, node);
11081110
} finally {
1109-
IndirectCallContext.exit(frame, getContext(), state);
1111+
IndirectCallContext.exit(frame, language, getContextRef(), state);
11101112
}
11111113
}
11121114

0 commit comments

Comments
 (0)