Skip to content

Commit cd7d5d7

Browse files
committed
Add dlerror() comment, handle callproc.c
Signed-off-by: Georgios Alexopoulos <[email protected]>
1 parent 512a084 commit cd7d5d7

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

Modules/_ctypes/_ctypes.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,11 @@ CDataType_in_dll_impl(PyObject *type, PyTypeObject *cls, PyObject *dll,
967967
return NULL;
968968
}
969969
#else
970+
/* dlerror() always returns the latest error.
971+
*
972+
* Clear the previous value before calling dlsym(),
973+
* to ensure we can tell if our call resulted in an error.
974+
*/
970975
dlerror();
971976
address = (void *)dlsym(handle, name);
972977
#ifdef __CYGWIN__
@@ -3785,6 +3790,11 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
37853790
return NULL;
37863791
}
37873792
#else
3793+
/* dlerror() always returns the latest error.
3794+
*
3795+
* Clear the previous value before calling dlsym(),
3796+
* to ensure we can tell if our call resulted in an error.
3797+
*/
37883798
dlerror();
37893799
address = (PPROC)dlsym(handle, name);
37903800
#ifdef __CYGWIN__

Modules/_ctypes/callproc.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,10 +1623,23 @@ static PyObject *py_dl_sym(PyObject *self, PyObject *args)
16231623
if (PySys_Audit("ctypes.dlsym/handle", "O", args) < 0) {
16241624
return NULL;
16251625
}
1626+
1627+
/* dlerror() always returns the latest error.
1628+
*
1629+
* Clear the previous value before calling dlsym(),
1630+
* to ensure we can tell if our call resulted in an error.
1631+
*/
1632+
dlerror();
16261633
ptr = dlsym((void*)handle, name);
1627-
if (!ptr) {
1628-
PyErr_SetString(PyExc_OSError,
1629-
dlerror());
1634+
char *dlerr = dlerror();
1635+
if (dlerr) {
1636+
PyErr_SetString(PyExc_OSError, dlerr);
1637+
return NULL;
1638+
}
1639+
else if (!ptr) {
1640+
PyErr_Format(PyExc_OSError,
1641+
"symbol '%s' not found",
1642+
name);
16301643
return NULL;
16311644
}
16321645
return PyLong_FromVoidPtr(ptr);

0 commit comments

Comments
 (0)