Skip to content

Commit c17ee1e

Browse files
committed
Clarify difference between ManagedNativeBuffer subclasses
1 parent 3acbb73 commit c17ee1e

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
import com.oracle.graal.python.annotations.ArgumentClinic;
109109
import com.oracle.graal.python.builtins.Builtin;
110110
import com.oracle.graal.python.builtins.CoreFunctions;
111+
import com.oracle.graal.python.builtins.Python3Core;
111112
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
112113
import com.oracle.graal.python.builtins.PythonBuiltins;
113114
import com.oracle.graal.python.builtins.modules.WarningsModuleBuiltins.WarnNode;
@@ -157,7 +158,7 @@
157158
import com.oracle.graal.python.builtins.objects.map.PMap;
158159
import com.oracle.graal.python.builtins.objects.memoryview.CExtPyBuffer;
159160
import com.oracle.graal.python.builtins.objects.memoryview.ManagedBuffer;
160-
import com.oracle.graal.python.builtins.objects.memoryview.ManagedNativeBuffer.ManagedNativeCExtBuffer;
161+
import com.oracle.graal.python.builtins.objects.memoryview.ManagedNativeBuffer.ManagedNativeBufferFromSlot;
161162
import com.oracle.graal.python.builtins.objects.memoryview.MemoryViewNodes;
162163
import com.oracle.graal.python.builtins.objects.memoryview.PBuffer;
163164
import com.oracle.graal.python.builtins.objects.memoryview.PMemoryView;
@@ -248,7 +249,6 @@
248249
import com.oracle.graal.python.parser.PythonSSTNodeFactory;
249250
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
250251
import com.oracle.graal.python.runtime.PythonContext;
251-
import com.oracle.graal.python.builtins.Python3Core;
252252
import com.oracle.graal.python.runtime.exception.PException;
253253
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
254254
import com.oracle.graal.python.runtime.sequence.storage.ObjectSequenceStorage;
@@ -3424,7 +3424,7 @@ PMemoryView fromManaged(@SuppressWarnings("unused") Object cls, Object object,
34243424
Object releaseBufferAttr = readReleaseBufferNode.execute(type, TypeBuiltins.TYPE_RELEASEBUFFER);
34253425
ManagedBuffer managedBuffer = null;
34263426
if (releaseBufferAttr != PNone.NO_VALUE) {
3427-
managedBuffer = new ManagedNativeCExtBuffer(buffer, object, releaseBufferAttr);
3427+
managedBuffer = new ManagedNativeBufferFromSlot(buffer, object, releaseBufferAttr);
34283428
}
34293429

34303430
int[] shape = buffer.getShape();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import com.oracle.graal.python.annotations.ArgumentClinic;
6868
import com.oracle.graal.python.builtins.Builtin;
6969
import com.oracle.graal.python.builtins.CoreFunctions;
70+
import com.oracle.graal.python.builtins.Python3Core;
7071
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
7172
import com.oracle.graal.python.builtins.PythonBuiltins;
7273
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodes.GetterRoot;
@@ -171,7 +172,7 @@
171172
import com.oracle.graal.python.builtins.objects.iterator.PSequenceIterator;
172173
import com.oracle.graal.python.builtins.objects.list.PList;
173174
import com.oracle.graal.python.builtins.objects.memoryview.ManagedBuffer;
174-
import com.oracle.graal.python.builtins.objects.memoryview.ManagedNativeBuffer.ManagedNativeCApiBuffer;
175+
import com.oracle.graal.python.builtins.objects.memoryview.ManagedNativeBuffer.ManagedNativeBufferFromType;
175176
import com.oracle.graal.python.builtins.objects.memoryview.MemoryViewNodes;
176177
import com.oracle.graal.python.builtins.objects.memoryview.PMemoryView;
177178
import com.oracle.graal.python.builtins.objects.module.PythonModule;
@@ -244,7 +245,6 @@
244245
import com.oracle.graal.python.runtime.PythonContext;
245246
import com.oracle.graal.python.runtime.PythonContext.GetThreadStateNode;
246247
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
247-
import com.oracle.graal.python.builtins.Python3Core;
248248
import com.oracle.graal.python.runtime.PythonOptions;
249249
import com.oracle.graal.python.runtime.exception.ExceptionUtils;
250250
import com.oracle.graal.python.runtime.exception.PException;
@@ -1705,7 +1705,7 @@ Object wrap(VirtualFrame frame, Object bufferStructPointer, Object ownerObj, Obj
17051705
int flags = initFlagsNode.execute(ndim, itemsize, shape, strides, suboffsets);
17061706
ManagedBuffer managedBuffer = null;
17071707
if (!lib.isNull(bufferStructPointer)) {
1708-
managedBuffer = new ManagedNativeCApiBuffer(bufferStructPointer);
1708+
managedBuffer = new ManagedNativeBufferFromType(bufferStructPointer);
17091709
}
17101710
PMemoryView memoryview = factory().createMemoryView(context, managedBuffer, owner, len, readonly, itemsize,
17111711
BufferFormat.forMemoryView(format),

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/memoryview/ManagedNativeBuffer.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,29 @@
4848
* Rough equivalent of CPython's {@code _PyManagedBuffer_Type}
4949
*/
5050
public abstract class ManagedNativeBuffer extends ManagedBuffer {
51-
public static final class ManagedNativeCApiBuffer extends ManagedNativeBuffer {
52-
// Pointer to native Py_buffer
51+
52+
/**
53+
* Object used for tracking the lifetime of a Python native buffer (i.e. {@code Py_buffer}) when
54+
* created via a buffer-like native type (i.e. via {@code tp_as_buffer->bf_getbuffer}). The
55+
* {@link #bufferStructPointer} is meant to be used for calling {@code PyBuffer_Release} (which
56+
* will call the corresponding {@code tp_as_bufer->bf_releasebuffer} of the buffer's owner).
57+
*/
58+
public static final class ManagedNativeBufferFromType extends ManagedNativeBuffer {
59+
/** Pointer to native Py_buffer */
5360
final Object bufferStructPointer;
5461

55-
public ManagedNativeCApiBuffer(Object bufferStructPointer) {
62+
public ManagedNativeBufferFromType(Object bufferStructPointer) {
5663
assert bufferStructPointer != null;
5764
this.bufferStructPointer = bufferStructPointer;
5865
}
59-
60-
public Object getBufferStructPointer() {
61-
return bufferStructPointer;
62-
}
6366
}
6467

65-
public static final class ManagedNativeCExtBuffer extends ManagedNativeBuffer {
68+
/**
69+
* Object used for tracking the lifetime of a Python native buffer when created via a buffer
70+
* slot (i.e. {@code Py_bf_getbuffer}) and if there is a corresponding release slot (
71+
* {@code Py_bf_releasebuffer}).
72+
*/
73+
public static final class ManagedNativeBufferFromSlot extends ManagedNativeBuffer {
6674
final CExtPyBuffer buffer;
6775
/**
6876
* This is the self for the release function. It will in most cases be the same as
@@ -72,7 +80,7 @@ public static final class ManagedNativeCExtBuffer extends ManagedNativeBuffer {
7280
final Object self;
7381
final Object releaseFunction;
7482

75-
public ManagedNativeCExtBuffer(CExtPyBuffer buffer, Object self, Object releaseFunction) {
83+
public ManagedNativeBufferFromSlot(CExtPyBuffer buffer, Object self, Object releaseFunction) {
7684
assert buffer != null;
7785
this.buffer = buffer;
7886
this.self = self;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/memoryview/MemoryViewNodes.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
import com.oracle.graal.python.builtins.objects.common.BufferStorageNodes;
5454
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
5555
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
56-
import com.oracle.graal.python.builtins.objects.memoryview.ManagedNativeBuffer.ManagedNativeCApiBuffer;
57-
import com.oracle.graal.python.builtins.objects.memoryview.ManagedNativeBuffer.ManagedNativeCExtBuffer;
56+
import com.oracle.graal.python.builtins.objects.memoryview.ManagedNativeBuffer.ManagedNativeBufferFromSlot;
57+
import com.oracle.graal.python.builtins.objects.memoryview.ManagedNativeBuffer.ManagedNativeBufferFromType;
5858
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
5959
import com.oracle.graal.python.lib.PyIndexCheckNode;
6060
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
@@ -671,13 +671,13 @@ public final void execute(VirtualFrame frame, PythonLanguage language, PythonBui
671671
}
672672

673673
@Specialization
674-
static void doCApiCached(ManagedNativeCApiBuffer buffer,
674+
static void doCApiCached(ManagedNativeBufferFromType buffer,
675675
@Cached PCallCapiFunction callReleaseNode) {
676676
callReleaseNode.call(NativeCAPISymbol.FUN_PY_TRUFFLE_RELEASE_BUFFER, buffer.bufferStructPointer);
677677
}
678678

679679
@Specialization
680-
static void doCExtBuffer(ManagedNativeCExtBuffer buffer,
680+
static void doCExtBuffer(ManagedNativeBufferFromSlot buffer,
681681
@Cached CallNode callNode) {
682682
callNode.execute(buffer.releaseFunction, buffer.self, buffer.buffer);
683683
}

0 commit comments

Comments
 (0)