Commits

Armin Rigo committed aa47c63

Use dlerror() to report error conditions more precisely.

  • Participants
  • Parent commits f8991af

Comments (0)

Files changed (1)

c/_cffi_backend.c

                      ct->ct_name);
         return NULL;
     }
+    dlerror();   /* clear error condition */
     funcptr = dlsym(dlobj->dl_handle, funcname);
     if (funcptr == NULL) {
-        PyErr_Format(PyExc_KeyError, "function '%s' not found in library '%s'",
-                     funcname, dlobj->dl_name);
+        const char *error = dlerror();
+        PyErr_Format(PyExc_KeyError,
+                     "function '%s' not found in library '%s': %s",
+                     funcname, dlobj->dl_name, error);
         return NULL;
     }
 
                           &CTypeDescr_Type, &ct, &varname))
         return NULL;
 
+    dlerror();   /* clear error condition */
     data = dlsym(dlobj->dl_handle, varname);
     if (data == NULL) {
-        PyErr_Format(PyExc_KeyError, "variable '%s' not found in library '%s'",
-                     varname, dlobj->dl_name);
-        return NULL;
+        const char *error = dlerror();
+        if (error != NULL) {
+            PyErr_Format(PyExc_KeyError,
+                         "variable '%s' not found in library '%s': %s",
+                         varname, dlobj->dl_name, error);
+            return NULL;
+        }
     }
     return convert_to_object(data, ct);
 }
                           &CTypeDescr_Type, &ct, &varname, &value))
         return NULL;
 
+    dlerror();   /* clear error condition */
     data = dlsym(dlobj->dl_handle, varname);
     if (data == NULL) {
-        PyErr_Format(PyExc_KeyError, "variable '%s' not found in library '%s'",
-                     varname, dlobj->dl_name);
+        const char *error = dlerror();
+        PyErr_Format(PyExc_KeyError,
+                     "variable '%s' not found in library '%s': %s",
+                     varname, dlobj->dl_name, error);
         return NULL;
     }
     if (convert_from_object(data, ct, value) < 0)
     printable_filename = filename_or_null ? filename_or_null : "<None>";
     handle = dlopen(filename_or_null, flags);
     if (handle == NULL) {
+        const char *error = dlerror();
         PyErr_Format(PyExc_OSError, "cannot load library %s: %s",
-                     printable_filename, dlerror());
+                     printable_filename, error);
         return NULL;
     }