Skip to content

Commit 29795a9

Browse files
committed
immortalize native Py_True and Py_False
1 parent 067f7ad commit 29795a9

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CApiContext.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ public CApiContext(PythonContext context, Object llvmLibrary, boolean useNativeB
318318
primitiveNativeWrapperCache[i] = PrimitiveNativeWrapper.createInt(value);
319319
}
320320

321+
// initialize Py_True and Py_False
322+
context.getTrue().setNativeWrapper(PrimitiveNativeWrapper.createBool(true));
323+
context.getFalse().setNativeWrapper(PrimitiveNativeWrapper.createBool(false));
324+
321325
this.gcTask = new BackgroundGCTask(context);
322326
}
323327

@@ -432,6 +436,12 @@ public PrimitiveNativeWrapper getCachedPrimitiveNativeWrapper(long l) {
432436
return getCachedPrimitiveNativeWrapper((int) l);
433437
}
434438

439+
public PrimitiveNativeWrapper getCachedBooleanPrimitiveNativeWrapper(boolean b) {
440+
PythonAbstractObjectNativeWrapper wrapper = b ? getContext().getTrue().getNativeWrapper() : getContext().getFalse().getNativeWrapper();
441+
assert wrapper.getRefCount() > 0;
442+
return (PrimitiveNativeWrapper) wrapper;
443+
}
444+
435445
/**
436446
* Returns or allocates (on demand) the native array {@code PyInterpreterState.small_ints} and
437447
* write all elements to it.

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/PrimitiveNativeWrapper.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2024, 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
@@ -255,6 +255,11 @@ void toNative(
255255
@Bind("$node") Node inliningTarget,
256256
@Cached CApiTransitions.FirstToNativeNode firstToNativeNode) {
257257
if (!isNative()) {
258+
if (isBool()) {
259+
assert (PythonContext.get(inliningTarget).getCApiContext().getCachedBooleanPrimitiveNativeWrapper(value != 0) == this);
260+
setNativePointer(firstToNativeNode.execute(inliningTarget, this, true /* immortal */));
261+
return;
262+
}
258263
// small int values are cached and will be immortal
259264
boolean immortal = isIntLike() && CApiGuards.isSmallLong(value);
260265
// if this wrapper wraps a small int value, this wrapper is one of the cached primitive

0 commit comments

Comments
 (0)