Skip to content

Commit 5f2a539

Browse files
committed
Expose PyStopIterationObject.value
1 parent 162ad5d commit 5f2a539

File tree

5 files changed

+37
-13
lines changed

5 files changed

+37
-13
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ declare_type(PyMethodDescr_Type, method_descriptor, PyMethodDescrObject);
187187
declare_type(PyGetSetDescr_Type, getset_descriptor, PyGetSetDescrObject);
188188
declare_type(PyMemberDescr_Type, member_descriptor, PyMemberDescrObject);
189189
declare_type(_PyExc_BaseException, BaseException, PyBaseExceptionObject);
190+
declare_type(_PyExc_StopIteration, StopIteration, PyStopIterationObject);
190191
declare_type(PyBuffer_Type, buffer, PyBufferDecorator);
191192
declare_type(PyFunction_Type, function, PyFunctionObject);
192193
declare_type(PyMethod_Type, method, PyMethodObject);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ typedef struct {
8686

8787
PyAPI_DATA(PyTypeObject) PyBuffer_Type;
8888
PyAPI_DATA(PyTypeObject) _PyExc_BaseException;
89+
PyAPI_DATA(PyTypeObject) _PyExc_StopIteration;
8990

9091
typedef void (*init_upcall)();
9192

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include <pyerrors.h>
4444

4545
PyTypeObject _PyExc_BaseException = PY_TRUFFLE_TYPE("BaseException", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_BASE_EXC_SUBCLASS, sizeof(PyBaseExceptionObject));
46+
PyTypeObject _PyExc_StopIteration = PY_TRUFFLE_TYPE("StopIteration", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_BASE_EXC_SUBCLASS, sizeof(PyStopIterationObject));
4647

4748
#define PY_EXCEPTION(__EXC_NAME__) (UPCALL_CEXT_O(polyglot_from_string("PyTruffle_Type", SRC_CS), polyglot_from_string(__EXC_NAME__, SRC_CS)))
4849

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

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
118118
import com.oracle.graal.python.builtins.objects.complex.PComplex;
119119
import com.oracle.graal.python.builtins.objects.dict.PDict;
120+
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
120121
import com.oracle.graal.python.builtins.objects.floats.PFloat;
121122
import com.oracle.graal.python.builtins.objects.frame.PFrame;
122123
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
@@ -149,6 +150,7 @@
149150
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetSuperClassNode;
150151
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetTypeFlagsNode;
151152
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
153+
import com.oracle.graal.python.lib.PyObjectGetAttr;
152154
import com.oracle.graal.python.lib.PyObjectSizeNode;
153155
import com.oracle.graal.python.nodes.ErrorMessages;
154156
import com.oracle.graal.python.nodes.PGuards;
@@ -1259,22 +1261,38 @@ static Object doPCode(PCode object, PythonNativeWrapper nativeWrapper, String ke
12591261
return doPCodeCached(object, nativeWrapper, key, NativeMember.byName(key), factory, toSulongNode);
12601262
}
12611263

1262-
// TODO: fallback guard
1263-
@Specialization
1264-
static Object doGeneric(@SuppressWarnings("unused") Object object, DynamicObjectNativeWrapper nativeWrapper, String key,
1264+
@Specialization(guards = {"eq(VALUE, key)", "isStopIteration(exception, getClassNode, isSubtypeNode)"}, limit = "1")
1265+
static Object doException(PBaseException exception, @SuppressWarnings("unused") PythonNativeWrapper nativeWrapper, @SuppressWarnings("unused") String key,
1266+
@Shared("getClassNode") @SuppressWarnings("unused") @Cached GetClassNode getClassNode,
1267+
@SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode,
1268+
@Cached PyObjectGetAttr getAttr,
1269+
@Shared("toSulongNode") @Cached ToSulongNode toSulongNode) {
1270+
return toSulongNode.execute(getAttr.execute(null, exception, "value"));
1271+
}
1272+
1273+
protected boolean isStopIteration(PBaseException exception, GetClassNode getClassNode, IsSubtypeNode isSubtypeNode) {
1274+
return isSubtypeNode.execute(getClassNode.execute(exception), PythonBuiltinClassType.StopIteration);
1275+
}
1276+
1277+
@Fallback
1278+
static Object doGeneric(@SuppressWarnings("unused") Object object, PythonNativeWrapper nativeWrapper, String key,
12651279
@CachedLibrary(limit = "1") HashingStorageLibrary lib,
12661280
@Shared("toSulongNode") @Cached ToSulongNode toSulongNode,
12671281
@Shared("getNativeNullNode") @Cached GetNativeNullNode getNativeNullNode) throws UnknownIdentifierException {
1268-
// This is the preliminary generic case: There are native members we know that they
1269-
// exist but we do currently not represent them. So, store them into a dynamic object
1270-
// such that native code at least reads the value that was written before.
1271-
if (nativeWrapper.isMemberReadable(key)) {
1272-
logGeneric(key);
1273-
DynamicObjectStorage nativeMemberStore = nativeWrapper.getNativeMemberStore();
1274-
if (nativeMemberStore != null) {
1275-
return lib.getItem(nativeMemberStore, key);
1282+
if (nativeWrapper instanceof DynamicObjectNativeWrapper) {
1283+
DynamicObjectNativeWrapper dynamicWrapper = (DynamicObjectNativeWrapper) nativeWrapper;
1284+
// This is the preliminary generic case: There are native members we know that they
1285+
// exist but we do currently not represent them. So, store them into a dynamic
1286+
// object
1287+
// such that native code at least reads the value that was written before.
1288+
if (dynamicWrapper.isMemberReadable(key)) {
1289+
logGeneric(key);
1290+
DynamicObjectStorage nativeMemberStore = dynamicWrapper.getNativeMemberStore();
1291+
if (nativeMemberStore != null) {
1292+
return lib.getItem(nativeMemberStore, key);
1293+
}
1294+
return toSulongNode.execute(getNativeNullNode.execute());
12761295
}
1277-
return toSulongNode.execute(getNativeNullNode.execute());
12781296
}
12791297
throw UnknownIdentifierException.create(key);
12801298
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,10 @@ public enum NativeMember {
281281
CO_NAMES("co_names", OBJECT),
282282
CO_VARNAMES("co_varnames", OBJECT),
283283
CO_FREEVARS("co_freevars", OBJECT),
284-
CO_CELLVARS("co_cellvars", OBJECT);
284+
CO_CELLVARS("co_cellvars", OBJECT),
285+
286+
// PyStopIterationObject
287+
VALUE("value", OBJECT);
285288

286289
private final String memberName;
287290
private final NativeMemberType type;

0 commit comments

Comments
 (0)