Skip to content

Commit 676507c

Browse files
Fix a refleak in an error case.
1 parent a3a8d05 commit 676507c

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

Python/crossinterp.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,13 @@ static void
9292
sync_module_capture_exc(PyThreadState *tstate, struct sync_module *data)
9393
{
9494
assert(_PyErr_Occurred(tstate));
95-
data->cached.failed = _PyErr_GetRaisedException(tstate);
96-
_PyErr_SetRaisedException(tstate, Py_NewRef(data->cached.failed));
95+
PyObject *context = data->cached.failed;
96+
PyObject *exc = _PyErr_GetRaisedException(tstate);
97+
_PyErr_SetRaisedException(tstate, Py_NewRef(exc));
98+
if (context != NULL) {
99+
PyException_SetContext(exc, context);
100+
}
101+
data->cached.failed = exc;
97102
}
98103

99104

@@ -667,7 +672,7 @@ _PyPickle_Loads(struct _unpickle_context *ctx, PyObject *pickled)
667672

668673
finally:
669674
if (exc != NULL) {
670-
ctx->main.cached.failed = _PyErr_GetRaisedException(tstate);
675+
sync_module_capture_exc(tstate, &ctx->main);
671676
// We restore the original exception.
672677
// It might make sense to chain it (__context__).
673678
_PyErr_SetRaisedException(tstate, exc);

0 commit comments

Comments
 (0)