Skip to content

Commit 6f9ee0b

Browse files
committed
Unconditionally re-raise decoding errors
1 parent 6bedc4b commit 6f9ee0b

File tree

5 files changed

+23
-43
lines changed

5 files changed

+23
-43
lines changed

Include/internal/pycore_pyerrors.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ PyAPI_FUNC(void) _PyErr_SetString(
135135
* encoding (LC_CTYPE).
136136
*
137137
* Exceptions occurring in decoding take priority over the desired exception.
138-
* In those cases, this function returns -1. Otherwise this returns 0 if the
139-
* localized exception has been successfully set.
140138
*
141139
* Exported for '_ctypes' shared extensions.
142140
*/
143-
PyAPI_FUNC(int) _PyErr_SetLocaleString(PyObject *exception, const char *string);
141+
PyAPI_FUNC(void) _PyErr_SetLocaleString(
142+
PyObject *exception,
143+
const char *string);
144144

145145
PyAPI_FUNC(PyObject*) _PyErr_Format(
146146
PyThreadState *tstate,

Modules/_ctypes/_ctypes.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -984,11 +984,8 @@ CDataType_in_dll_impl(PyObject *type, PyTypeObject *cls, PyObject *dll,
984984
#ifdef USE_DLERROR
985985
const char *dlerr = dlerror();
986986
if (dlerr) {
987-
if (_PyErr_SetLocaleString(PyExc_ValueError, dlerr) == 0) {
988-
return NULL;
989-
}
990-
// Ignore decoding errors and fall back to the generic error.
991-
PyErr_Clear();
987+
_PyErr_SetLocaleString(PyExc_ValueError, dlerr);
988+
return NULL;
992989
}
993990
#endif
994991
#undef USE_DLERROR
@@ -3801,17 +3798,14 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
38013798
address = (PPROC)dlsym(handle, name);
38023799

38033800
if (!address) {
3804-
#ifdef USE_DLERROR
3801+
#ifdef USE_DLERROR
38053802
const char *dlerr = dlerror();
38063803
if (dlerr) {
3807-
if (_PyErr_SetLocaleString(PyExc_AttributeError, dlerr) == 0) {
3808-
Py_DECREF(ftuple);
3809-
return NULL;
3810-
}
3811-
// Ignore decoding errors and fall back to the generic error.
3812-
PyErr_Clear();
3804+
_PyErr_SetLocaleString(PyExc_AttributeError, dlerr);
3805+
Py_DECREF(ftuple);
3806+
return NULL;
38133807
}
3814-
#endif
3808+
#endif
38153809
PyErr_Format(PyExc_AttributeError, "function '%s' not found", name);
38163810
Py_DECREF(ftuple);
38173811
return NULL;

Modules/_ctypes/callproc.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,11 +1589,8 @@ static PyObject *py_dl_open(PyObject *self, PyObject *args)
15891589
if (!handle) {
15901590
const char *errmsg = dlerror();
15911591
if (errmsg) {
1592-
if (_PyErr_SetLocaleString(PyExc_OSError, errmsg) == 0) {
1593-
return NULL;
1594-
}
1595-
// Ignore decoding errors and fall back to the generic error.
1596-
PyErr_Clear();
1592+
_PyErr_SetLocaleString(PyExc_OSError, errmsg);
1593+
return NULL;
15971594
}
15981595
PyErr_SetString(PyExc_OSError, "dlopen() error");
15991596
return NULL;
@@ -1610,11 +1607,8 @@ static PyObject *py_dl_close(PyObject *self, PyObject *args)
16101607
if (dlclose(handle)) {
16111608
const char *errmsg = dlerror();
16121609
if (errmsg) {
1613-
if (_PyErr_SetLocaleString(PyExc_OSError, errmsg) == 0) {
1614-
return NULL;
1615-
}
1616-
// Ignore decoding errors and fall back to the generic error.
1617-
PyErr_Clear();
1610+
_PyErr_SetLocaleString(PyExc_OSError, errmsg);
1611+
return NULL;
16181612
}
16191613
PyErr_SetString(PyExc_OSError, "dlclose() error");
16201614
return NULL;
@@ -1653,11 +1647,8 @@ static PyObject *py_dl_sym(PyObject *self, PyObject *args)
16531647
#ifdef USE_DLERROR
16541648
const char *errmsg = dlerror();
16551649
if (errmsg) {
1656-
if (_PyErr_SetLocaleString(PyExc_OSError, errmsg) == 0) {
1657-
return NULL;
1658-
}
1659-
// Ignore decoding errors and fall back to the generic error below.
1660-
PyErr_Clear();
1650+
_PyErr_SetLocaleString(PyExc_OSError, errmsg);
1651+
return NULL;
16611652
}
16621653
#endif
16631654
#undef USE_DLERROR

Modules/_gdbmmodule.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,19 @@ get_gdbm_state(PyObject *module)
3737
/*
3838
* Set the gdbm error obtained by gdbm_strerror(gdbm_errno).
3939
*
40-
* If the error message cannot be decoded from the current locale
41-
* or if none exists, a generic (UTF-8) error message is used.
40+
* If no error message exists, a generic (UTF-8) error message
41+
* is used instead.
4242
*/
4343
static void
4444
set_gdbm_error(_gdbm_state *state, const char *generic_error)
4545
{
4646
const char *gdbm_errmsg = gdbm_strerror(gdbm_errno);
4747
if (gdbm_errmsg) {
48-
if (_PyErr_SetLocaleString(state->gdbm_error, gdbm_errmsg) == 0) {
49-
return;
50-
}
51-
// Ignore decoding errors and fall back to the generic error.
52-
PyErr_Clear();
48+
_PyErr_SetLocaleString(state->gdbm_error, gdbm_errmsg);
49+
}
50+
else {
51+
PyErr_SetString(state->gdbm_error, generic_error);
5352
}
54-
assert(!PyErr_Occurred());
55-
PyErr_SetString(state->gdbm_error, generic_error);
5653
}
5754

5855
/*[clinic input]

Python/errors.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,16 +301,14 @@ PyErr_SetString(PyObject *exception, const char *string)
301301
_PyErr_SetString(tstate, exception, string);
302302
}
303303

304-
int
304+
void
305305
_PyErr_SetLocaleString(PyObject *exception, const char *string)
306306
{
307307
PyObject *value = PyUnicode_DecodeLocale(string, "surrogateescape");
308308
if (value != NULL) {
309309
PyErr_SetObject(exception, value);
310310
Py_DECREF(value);
311-
return 0;
312311
}
313-
return -1;
314312
}
315313

316314
PyObject* _Py_HOT_FUNCTION

0 commit comments

Comments
 (0)