Skip to content

Commit cf87452

Browse files
Do not chain multiple NotShareableError.
1 parent 2135a3f commit cf87452

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

Python/crossinterp.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,15 @@ _set_xid_lookup_failure(PyThreadState *tstate, PyObject *obj, const char *msg,
204204
{
205205
if (msg != NULL) {
206206
assert(obj == NULL);
207-
set_notshareableerror(tstate, cause, msg);
207+
set_notshareableerror(tstate, cause, 0, msg);
208208
}
209209
else if (obj == NULL) {
210210
msg = "object does not support cross-interpreter data";
211-
set_notshareableerror(tstate, cause, msg);
211+
set_notshareableerror(tstate, cause, 0, msg);
212212
}
213213
else {
214-
format_notshareableerror(
215-
tstate, cause, "%S does not support cross-interpreter data", obj);
214+
msg = "%S does not support cross-interpreter data";
215+
format_notshareableerror(tstate, cause, 0, msg, obj);
216216
}
217217
}
218218

@@ -225,7 +225,7 @@ _PyObject_CheckXIData(PyThreadState *tstate, PyObject *obj)
225225
}
226226
xidatafunc getdata = lookup_getdata(&ctx, obj);
227227
if (getdata == NULL) {
228-
if (!PyErr_Occurred()) {
228+
if (!_PyErr_Occurred(tstate)) {
229229
_set_xid_lookup_failure(tstate, obj, NULL, NULL);
230230
}
231231
return -1;
@@ -252,14 +252,18 @@ _PyObject_GetXIData(PyThreadState *tstate,
252252
xidatafunc getdata = lookup_getdata(&ctx, obj);
253253
if (getdata == NULL) {
254254
Py_DECREF(obj);
255-
if (!PyErr_Occurred()) {
255+
if (!_PyErr_Occurred(tstate)) {
256256
_set_xid_lookup_failure(tstate, obj, NULL, NULL);
257257
}
258258
return -1;
259259
}
260260
int res = getdata(tstate, obj, data);
261261
Py_DECREF(obj);
262262
if (res != 0) {
263+
PyObject *cause = _PyErr_GetRaisedException(tstate);
264+
assert(cause != NULL);
265+
_set_xid_lookup_failure(tstate, obj, NULL, cause);
266+
Py_XDECREF(cause);
263267
return -1;
264268
}
265269

@@ -1067,6 +1071,7 @@ _PyXI_ApplyError(_PyXI_error *error)
10671071
}
10681072
else if (error->code == _PyXI_ERR_NOT_SHAREABLE) {
10691073
// Propagate the exception directly.
1074+
assert(!_PyErr_Occurred(tstate));
10701075
_set_xid_lookup_failure(tstate, NULL, error->uncaught.msg, NULL);
10711076
}
10721077
else {

Python/crossinterp_data_lookup.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void
7373
_PyXIData_SetNotShareableError(PyThreadState *tstate, const char *msg)
7474
{
7575
PyObject *cause = NULL;
76-
set_notshareableerror(tstate, cause, msg);
76+
set_notshareableerror(tstate, cause, 1, msg);
7777
}
7878

7979
void
@@ -83,7 +83,7 @@ _PyXIData_FormatNotShareableError(PyThreadState *tstate,
8383
PyObject *cause = NULL;
8484
va_list vargs;
8585
va_start(vargs, format);
86-
format_notshareableerror_v(tstate, cause, format, vargs);
86+
format_notshareableerror_v(tstate, cause, 1, format, vargs);
8787
va_end(vargs);
8888
}
8989

Python/crossinterp_exceptions.h

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ _ensure_current_cause(PyThreadState *tstate, PyObject *cause)
1616
}
1717
assert(PyException_GetCause(exc) == NULL);
1818
PyException_SetCause(exc, cause);
19+
_PyErr_SetRaisedException(tstate, exc);
1920
}
2021

2122

@@ -80,41 +81,59 @@ get_notshareableerror_type(PyThreadState *tstate)
8081
}
8182

8283
static void
83-
set_notshareableerror(PyThreadState *tstate, PyObject *cause, const char *msg)
84+
_ensure_notshareableerror(PyThreadState *tstate,
85+
PyObject *cause, int force, PyObject *msgobj)
8486
{
87+
PyObject *ctx = _PyErr_GetRaisedException(tstate);
8588
PyObject *exctype = get_notshareableerror_type(tstate);
86-
if (exctype == NULL) {
89+
if (exctype != NULL) {
90+
if (!force && ctx != NULL && Py_TYPE(ctx) == (PyTypeObject *)exctype) {
91+
// A NotShareableError instance is already set.
92+
assert(cause == NULL);
93+
_PyErr_SetRaisedException(tstate, ctx);
94+
}
95+
}
96+
else {
8797
exctype = PyExc_ValueError;
8898
}
89-
// We have to set the context manually since _PyErr_SetObject() won't.
90-
PyObject *ctx = _PyErr_GetRaisedException(tstate);
91-
_PyErr_SetString(tstate, exctype, msg);
99+
_PyErr_SetObject(tstate, exctype, msgobj);
100+
// We have to set the context manually since _PyErr_SetObject() doesn't.
92101
_PyErr_ChainExceptions1Tstate(tstate, ctx);
93102
_ensure_current_cause(tstate, cause);
94103
}
95104

96105
static void
97-
format_notshareableerror_v(PyThreadState *tstate, PyObject *cause,
106+
set_notshareableerror(PyThreadState *tstate, PyObject *cause, int force, const char *msg)
107+
{
108+
PyObject *msgobj = PyUnicode_FromString(msg);
109+
if (msgobj == NULL) {
110+
assert(_PyErr_Occurred(tstate));
111+
}
112+
else {
113+
_ensure_notshareableerror(tstate, cause, force, msgobj);
114+
}
115+
}
116+
117+
static void
118+
format_notshareableerror_v(PyThreadState *tstate, PyObject *cause, int force,
98119
const char *format, va_list vargs)
99120
{
100-
PyObject *exctype = get_notshareableerror_type(tstate);
101-
if (exctype == NULL) {
102-
exctype = PyExc_ValueError;
121+
PyObject *msgobj = PyUnicode_FromFormatV(format, vargs);
122+
if (msgobj == NULL) {
123+
assert(_PyErr_Occurred(tstate));
124+
}
125+
else {
126+
_ensure_notshareableerror(tstate, cause, force, msgobj);
103127
}
104-
// We have to set the context manually since _PyErr_SetObject() won't.
105-
PyObject *ctx = _PyErr_GetRaisedException(tstate);
106-
_PyErr_FormatV(tstate, exctype, format, vargs);
107-
_PyErr_ChainExceptions1Tstate(tstate, ctx);
108-
_ensure_current_cause(tstate, cause);
109128
}
110129

111130
static void
112-
format_notshareableerror(PyThreadState *tstate, PyObject *cause,
131+
format_notshareableerror(PyThreadState *tstate, PyObject *cause, int force,
113132
const char *format, ...)
114133
{
115134
va_list vargs;
116135
va_start(vargs, format);
117-
format_notshareableerror_v(tstate, cause, format, vargs);
136+
format_notshareableerror_v(tstate, cause, force, format, vargs);
118137
va_end(vargs);
119138
}
120139

0 commit comments

Comments
 (0)