Skip to content

Commit a3aae38

Browse files
msimacektimfel
authored andcommitted
Only define tp_as_sequence/mapping when sq/mp_lenght is defined
1 parent 8e56386 commit a3aae38

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextSlotBuiltins.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,8 +1247,13 @@ Object get(PythonManagedClass object,
12471247
abstract static class Py_get_PyTypeObject_tp_as_mapping extends CApiUnaryBuiltinNode {
12481248

12491249
@Specialization
1250-
public Object get(PythonManagedClass object) {
1251-
return new PyMappingMethodsWrapper(object);
1250+
public Object get(PythonManagedClass object,
1251+
@Cached LookupNativeSlotNode lookupLen) {
1252+
if (lookupLen.execute(object, SlotMethodDef.MP_LENGTH) != getNULL()) {
1253+
return new PyMappingMethodsWrapper(object);
1254+
} else {
1255+
return getNULL();
1256+
}
12521257
}
12531258
}
12541259

@@ -1266,8 +1271,13 @@ static Object get(PythonManagedClass object) {
12661271
abstract static class Py_get_PyTypeObject_tp_as_sequence extends CApiUnaryBuiltinNode {
12671272

12681273
@Specialization
1269-
public Object get(PythonManagedClass object) {
1270-
return new PySequenceMethodsWrapper(object);
1274+
public Object get(PythonManagedClass object,
1275+
@Cached LookupNativeSlotNode lookupLen) {
1276+
if (lookupLen.execute(object, SlotMethodDef.SQ_LENGTH) != getNULL()) {
1277+
return new PySequenceMethodsWrapper(object);
1278+
} else {
1279+
return getNULL();
1280+
}
12711281
}
12721282
}
12731283

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,13 +513,25 @@ Object doTpAsBuffer(PythonManagedClass object, @SuppressWarnings("unused") Pytho
513513
}
514514

515515
@Specialization(guards = "eq(TP_AS_SEQUENCE, key)")
516-
Object doTpAsSequence(PythonManagedClass object, @SuppressWarnings("unused") PythonNativeWrapper nativeWrapper, @SuppressWarnings("unused") String key) {
517-
return new PySequenceMethodsWrapper(object);
516+
Object doTpAsSequence(PythonManagedClass object, @SuppressWarnings("unused") PythonNativeWrapper nativeWrapper, @SuppressWarnings("unused") String key,
517+
@Cached LookupNativeSlotNode lookupLen) {
518+
Object nativeNull = getContext().getNativeNull().getPtr();
519+
if (lookupLen.execute(object, SlotMethodDef.SQ_LENGTH) != nativeNull) {
520+
return new PySequenceMethodsWrapper(object);
521+
} else {
522+
return nativeNull;
523+
}
518524
}
519525

520526
@Specialization(guards = "eq(TP_AS_MAPPING, key)")
521-
Object doTpAsMapping(PythonManagedClass object, @SuppressWarnings("unused") PythonNativeWrapper nativeWrapper, @SuppressWarnings("unused") String key) {
522-
return new PyMappingMethodsWrapper(object);
527+
Object doTpAsMapping(PythonManagedClass object, @SuppressWarnings("unused") PythonNativeWrapper nativeWrapper, @SuppressWarnings("unused") String key,
528+
@Cached LookupNativeSlotNode lookupLen) {
529+
Object nativeNull = getContext().getNativeNull().getPtr();
530+
if (lookupLen.execute(object, SlotMethodDef.MP_LENGTH) != nativeNull) {
531+
return new PyMappingMethodsWrapper(object);
532+
} else {
533+
return nativeNull;
534+
}
523535
}
524536

525537
@Specialization(guards = "eq(TP_NEW, key)")

0 commit comments

Comments
 (0)