8000 Merge pull request #12266 from eric-wieser/fix-systemerror · numpy/numpy@ebe9d44 · GitHub
[go: up one dir, main page]

Skip to content

Commit ebe9d44

Browse files
authored
Merge pull request #12266 from eric-wieser/fix-systemerror
BUG: Avoid SystemErrors by checking the return value of PyPrint
2 parents cc761fe + 9751469 commit ebe9d44

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

numpy/core/src/multiarray/dtype_transfer.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@
5151
#endif
5252
/**********************************************/
5353

54+
#if NPY_DT_DBG_TRACING
55+
/*
56+
* Thin wrapper around print that ignores exceptions
57+
*/
58+
static void
59+
_safe_print(PyObject *obj)
60+
{
61+
if (PyObject_Print(obj, stdout, 0) < 0) {
62+
PyErr_Clear();
63+
printf("<error during print>");
64+
}
65+
}
66+
#endif
67+
5468
/*
5569
* Returns a transfer function which DECREFs any references in src_type.
5670
*
@@ -1042,9 +1056,9 @@ get_nbo_cast_datetime_transfer_function(int aligned,
10421056

10431057
#if NPY_DT_DBG_TRACING
10441058
printf("Dtype transfer from ");
1045-
PyObject_Print((PyObject *)src_dtype, stdout, 0);
1059+
_safe_print((PyObject *)src_dtype);
10461060
printf(" to ");
1047-
PyObject_Print((PyObject *)dst_dtype, stdout, 0);
1061+
_safe_print((PyObject *)dst_dtype);
10481062
printf("\n");
10491063
printf("has conversion fraction %lld/%lld\n", num, denom);
10501064
#endif
@@ -1089,9 +1103,9 @@ get_nbo_datetime_to_string_transfer_function(int aligned,
10891103

10901104
#if NPY_DT_DBG_TRACING
10911105
printf("Dtype transfer from ");
1092-
PyObject_Print((PyObject *)src_dtype, stdout, 0);
1106+
_safe_print((PyObject *)src_dtype);
10931107
printf(" to ");
1094-
PyObject_Print((PyObject *)dst_dtype, stdout, 0);
1108+
_safe_print((PyObject *)dst_dtype);
10951109
printf("\n");
10961110
#endif
10971111

@@ -1211,9 +1225,9 @@ get_nbo_string_to_datetime_transfer_function(int aligned,
12111225

12121226
#if NPY_DT_DBG_TRACING
12131227
printf("Dtype transfer from ");
1214-
PyObject_Print((PyObject *)src_dtype, stdout, 0);
1228+
_safe_print((PyObject *)src_dtype);
12151229
printf(" to ");
1216-
PyObject_Print((PyObject *)dst_dtype, stdout, 0);
1230+
_safe_print((PyObject *)dst_dtype);
12171231
printf("\n");
12181232
#endif
12191233

@@ -3421,9 +3435,13 @@ PyArray_GetDTypeTransferFunction(int aligned,
34213435

34223436
#if NPY_DT_DBG_TRACING
34233437
printf("Calculating dtype transfer from ");
3424-
PyObject_Print((PyObject *)src_dtype, stdout, 0);
3438+
if (PyObject_Print((PyObject *)src_dtype, stdout, 0) < 0) {
3439+
return NPY_FAIL;
3440+
}
34253441
printf(" to ");
3426-
PyObject_Print((PyObject *)dst_dtype, stdout, 0);
3442+
if (PyObject_Print((PyObject *)dst_dtype, stdout, 0) < 0) {
3443+
return NPY_FAIL;
3444+
}
34273445
printf("\n");
34283446
#endif
34293447

0 commit comments

Comments
 (0)
0