python: improve few exceptions thrown on error

Make use of functions and types that fit more, and that do the same job:
- use PyErr_NoMemory() on malloc failure
- use PyErr_SetFromErrno when setting an exception from an errno
- throw TypeError if not getting a list when required
This commit is contained in:
Pino Toscano
2017-05-11 14:32:18 +02:00
parent f3f99a09ea
commit 3c0b7710c1

View File

@@ -237,7 +237,7 @@ guestfs_int_py_event_to_string (PyObject *self, PyObject *args)
str = guestfs_event_to_string (events);
if (str == NULL) {
PyErr_SetString (PyExc_RuntimeError, strerror (errno));
PyErr_SetFromErrno (PyExc_RuntimeError);
return NULL;
}
@@ -271,7 +271,7 @@ get_all_event_callbacks (guestfs_h *g, size_t *len_rtn)
/* Copy them into the return array. */
r = malloc (sizeof (PyObject *) * (*len_rtn));
if (r == NULL) {
PyErr_SetNone (PyExc_MemoryError);
PyErr_NoMemory ();
return NULL;
}
@@ -298,7 +298,7 @@ guestfs_int_py_get_string_list (PyObject *obj)
assert (obj);
if (!PyList_Check (obj)) {
PyErr_SetString (PyExc_RuntimeError, "expecting a list parameter");
PyErr_SetString (PyExc_TypeError, "expecting a list parameter");
return NULL;
}
@@ -310,7 +310,7 @@ guestfs_int_py_get_string_list (PyObject *obj)
len = (size_t) slen;
r = malloc (sizeof (char *) * (len+1));
if (r == NULL) {
PyErr_SetString (PyExc_RuntimeError, "get_string_list: out of memory");
PyErr_NoMemory ();
return NULL;
}