Skip to content

Commit bb7777f

Browse files
committed
Support inheriting tp_as_buffer
1 parent e51159d commit bb7777f

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ freefunc get_tp_free(PyTypeObject* obj) {
358358
return obj->tp_free;
359359
}
360360

361+
/** to be used from Java code only; reads native 'tp_as_buffer' field */
362+
PyBufferProcs* get_tp_as_buffer(PyTypeObject* obj) {
363+
return obj->tp_as_buffer;
364+
}
365+
361366
/** to be used from Java code only; reads native 'tp_flags' field */
362367
unsigned long get_tp_flags(PyTypeObject* obj) {
363368
return obj->tp_flags;

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ static Object doTpAsBuffer(PythonManagedClass object, @SuppressWarnings("unused"
433433
@Cached BranchProfile notMemoryview,
434434
@Cached BranchProfile notBuffer,
435435
@Cached BranchProfile notMmap,
436+
@Cached LookupNativeMemberInMRONode lookupTpAsBufferNode,
436437
@Shared("getNativeNullNode") @Cached GetNativeNullNode getNativeNullNode,
437438
@Shared("nullToSulongNode") @Cached ToSulongNode toSulongNode) {
438439
PythonBuiltinClass pBytes = context.getCore().lookupType(PythonBuiltinClassType.PBytes);
@@ -460,8 +461,16 @@ static Object doTpAsBuffer(PythonManagedClass object, @SuppressWarnings("unused"
460461
return new PyBufferProcsWrapper(pMmap);
461462
}
462463
notMmap.enter();
463-
// NULL pointer
464-
return toSulongNode.execute(getNativeNullNode.execute());
464+
/*
465+
* Managed classes don't store PyBufferProcs objects and so there is no attribute. This
466+
* is why we use managedMemberName == "".
467+
*/
468+
Object result = lookupTpAsBufferNode.execute(object, NativeMember.TP_AS_BUFFER, "");
469+
if (result == PNone.NO_VALUE) {
470+
// NULL pointer
471+
return toSulongNode.execute(getNativeNullNode.execute());
472+
}
473+
return toSulongNode.execute(result);
465474
}
466475

467476
@Specialization(guards = "eq(TP_AS_SEQUENCE, key)")

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public enum NativeCAPISymbol implements NativeCExtSymbol {
7979
FUN_GET_TP_DICTOFFSET("get_tp_dictoffset"),
8080
FUN_GET_TP_BASICSIZE("get_tp_basicsize"),
8181
FUN_GET_TP_ITEMSIZE("get_tp_itemsize"),
82+
FUN_GET_TP_AS_BUFFER("get_tp_as_buffer"),
8283
FUN_GET_BYTE_ARRAY_TYPE_ID("get_byte_array_typeid"),
8384
FUN_GET_PTR_ARRAY_TYPE_ID("get_ptr_array_typeid"),
8485
FUN_PTR_COMPARE("truffle_ptr_compare"),

0 commit comments

Comments
 (0)