Skip to content

Commit c0a8037

Browse files
committed
Update macro to crash in debug mode when error is not set
1 parent f2ad0ef commit c0a8037

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Modules/_remote_debugging/_remote_debugging.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,22 @@ typedef enum _WIN32_THREADSTATE {
173173
#define THREAD_STATUS_GIL_REQUESTED (1 << 3)
174174
#define THREAD_STATUS_HAS_EXCEPTION (1 << 4)
175175

176-
/* Exception cause macro */
176+
/* Exception cause macro - chains context to existing exceptions in debug mode.
177+
* This macro assumes an exception has already been set by the failing function.
178+
* If no exception exists, this indicates a bug that should be fixed. */
177179
#define set_exception_cause(unwinder, exc_type, message) \
178180
do { \
179181
if (!PyErr_ExceptionMatches(PyExc_PermissionError)) { \
180-
if (!PyErr_Occurred()) { \
182+
if (PyErr_Occurred()) { \
183+
if (unwinder->debug) { \
184+
/* Chain exception with context */ \
185+
_PyErr_FormatFromCause(exc_type, "%s", message); \
186+
} \
187+
} else { \
188+
/* BUG: Exception should have been set by caller */ \
189+
/* Fallback prevents crash; assert catches bug in debug builds */ \
181190
PyErr_SetString(exc_type, message); \
182-
} else if (unwinder->debug) { \
183-
_PyErr_FormatFromCause(exc_type, "%s", message); \
191+
assert(PyErr_Occurred() && "function returned -1 without setting exception"); \
184192
} \
185193
} \
186194
} while (0)

0 commit comments

Comments
 (0)