5
5
#include "pycore_ceval.h" // _Py_simple_func
6
6
#include "pycore_crossinterp.h" // struct _xid
7
7
#include "pycore_initconfig.h" // _PyStatus_OK()
8
+ #include "pycore_namespace.h" //_PyNamespace_New()
8
9
#include "pycore_pyerrors.h" // _PyErr_Clear()
9
10
#include "pycore_pystate.h" // _PyInterpreterState_GET()
10
11
#include "pycore_typeobject.h" // _PyType_GetModuleName()
@@ -1145,6 +1146,116 @@ _PyXI_excinfo_Apply(_PyXI_excinfo *info, PyObject *exctype)
1145
1146
Py_DECREF (formatted );
1146
1147
}
1147
1148
1149
+ static PyObject *
1150
+ _PyXI_excinfo_TypeAsObject (_PyXI_excinfo * info )
1151
+ {
1152
+ PyObject * ns = _PyNamespace_New (NULL );
1153
+ if (ns == NULL ) {
1154
+ return NULL ;
1155
+ }
1156
+ int empty = 1 ;
1157
+
1158
+ if (info -> type .name != NULL ) {
1159
+ PyObject * name = PyUnicode_FromString (info -> type .name );
1160
+ if (name == NULL ) {
1161
+ goto error ;
1162
+ }
1163
+ int res = PyObject_SetAttrString (ns , "__name__" , name );
1164
+ Py_DECREF (name );
1165
+ if (res < 0 ) {
1166
+ goto error ;
1167
+ }
1168
+ empty = 0 ;
1169
+ }
1170
+
1171
+ if (info -> type .qualname != NULL ) {
1172
+ PyObject * qualname = PyUnicode_FromString (info -> type .qualname );
1173
+ if (qualname == NULL ) {
1174
+ goto error ;
1175
+ }
1176
+ int res = PyObject_SetAttrString (ns , "__qualname__" , qualname );
1177
+ Py_DECREF (qualname );
1178
+ if (res < 0 ) {
1179
+ goto error ;
1180
+ }
1181
+ empty = 0 ;
1182
+ }
1183
+
1184
+ if (info -> type .module != NULL ) {
1185
+ PyObject * module = PyUnicode_FromString (info -> type .module );
1186
+ if (module == NULL ) {
1187
+ goto error ;
1188
+ }
1189
+ int res = PyObject_SetAttrString (ns , "__module__" , module );
1190
+ Py_DECREF (module );
1191
+ if (res < 0 ) {
1192
+ goto error ;
1193
+ }
1194
+ empty = 0 ;
1195
+ }
1196
+
1197
+ if (empty ) {
1198
+ Py_CLEAR (ns );
1199
+ }
1200
+
1201
+ return ns ;
1202
+
1203
+ error :
1204
+ Py_DECREF (ns );
1205
+ return NULL ;
1206
+ }
1207
+
1208
+ static PyObject *
1209
+ _PyXI_excinfo_AsObject (_PyXI_excinfo * info )
1210
+ {
1211
+ PyObject * ns = _PyNamespace_New (NULL );
1212
+ if (ns == NULL ) {
1213
+ return NULL ;
1214
+ }
1215
+ int res ;
1216
+
1217
+ PyObject * type = _PyXI_excinfo_TypeAsObject (info );
1218
+ if (type == NULL ) {
1219
+ if (PyErr_Occurred ()) {
1220
+ goto error ;
1221
+ }
1222
+ type = Py_NewRef (Py_None );
1223
+ }
1224
+ res = PyObject_SetAttrString (ns , "type" , type );
1225
+ Py_DECREF (type );
1226
+ if (res < 0 ) {
1227
+ goto error ;
1228
+ }
1229
+
1230
+ PyObject * msg = info -> msg != NULL
1231
+ ? PyUnicode_FromString (info -> msg )
1232
+ : Py_NewRef (Py_None );
1233
+ if (msg == NULL ) {
1234
+ goto error ;
1235
+ }
1236
+ res = PyObject_SetAttrString (ns , "msg" , msg );
1237
+ Py_DECREF (msg );
1238
+ if (res < 0 ) {
1239
+ goto error ;
1240
+ }
1241
+
1242
+ PyObject * formatted = _PyXI_excinfo_format (info );
1243
+ if (formatted == NULL ) {
1244
+ goto error ;
1245
+ }
1246
+ res = PyObject_SetAttrString (ns , "formatted" , formatted );
1247
+ Py_DECREF (formatted );
1248
+ if (res < 0 ) {
1249
+ goto error ;
1250
+ }
1251
+
1252
+ return ns ;
1253
+
1254
+ error :
1255
+ Py_DECREF (ns );
1256
+ return NULL ;
1257
+ }
1258
+
1148
1259
1149
1260
/***************************/
1150
1261
/* short-term data sharing */
@@ -1238,15 +1349,12 @@ _PyXI_InitError(_PyXI_error *error, PyObject *excobj, _PyXI_errcode code)
1238
1349
return failure ;
1239
1350
}
1240
1351
1241
- void
1242
- _PyXI_ApplyError (_PyXI_error * error , PyObject * exctype )
1352
+ PyObject *
1353
+ _PyXI_ApplyError (_PyXI_error * error )
1243
1354
{
1244
- if (exctype == NULL ) {
1245
- exctype = PyExc_RuntimeError ;
1246
- }
1247
1355
if (error -> code == _PyXI_ERR_UNCAUGHT_EXCEPTION ) {
1248
1356
// Raise an exception that proxies the propagated exception.
1249
- _PyXI_excinfo_Apply (& error -> uncaught , exctype );
1357
+ return _PyXI_excinfo_AsObject (& error -> uncaught );
1250
1358
}
1251
1359
else if (error -> code == _PyXI_ERR_NOT_SHAREABLE ) {
1252
1360
// Propagate the exception directly.
@@ -1259,13 +1367,14 @@ _PyXI_ApplyError(_PyXI_error *error, PyObject *exctype)
1259
1367
if (error -> uncaught .type .name != NULL || error -> uncaught .msg != NULL ) {
1260
1368
// __context__ will be set to a proxy of the propagated exception.
1261
1369
PyObject * exc = PyErr_GetRaisedException ();
1262
- _PyXI_excinfo_Apply (& error -> uncaught , exctype );
1370
+ _PyXI_excinfo_Apply (& error -> uncaught , PyExc_RuntimeError );
1263
1371
PyObject * exc2 = PyErr_GetRaisedException ();
1264
1372
PyException_SetContext (exc , exc2 );
1265
1373
PyErr_SetRaisedException (exc );
1266
1374
}
1267
1375
}
1268
1376
assert (PyErr_Occurred ());
1377
+ return NULL ;
1269
1378
}
1270
1379
1271
1380
/* shared namespaces */
@@ -1877,14 +1986,15 @@ _capture_current_exception(_PyXI_session *session)
1877
1986
session -> error = err ;
1878
1987
}
1879
1988
1880
- void
1881
- _PyXI_ApplyCapturedException (_PyXI_session * session , PyObject * excwrapper )
1989
+ PyObject *
1990
+ _PyXI_ApplyCapturedException (_PyXI_session * session )
1882
1991
{
1883
1992
assert (!PyErr_Occurred ());
1884
1993
assert (session -> error != NULL );
1885
- _PyXI_ApplyError (session -> error , excwrapper );
1886
- assert (PyErr_Occurred ());
1994
+ PyObject * res = _PyXI_ApplyError (session -> error );
1995
+ assert (( res == NULL ) != ( PyErr_Occurred () == NULL ));
1887
1996
session -> error = NULL ;
1997
+ return res ;
1888
1998
}
1889
1999
1890
2000
int
0 commit comments