[Libguestfs] [PATCH] python: improve few exceptions thrown on error

Richard W.M. Jones rjones at redhat.com
Fri May 12 15:21:42 UTC 2017


On Thu, May 11, 2017 at 02:37:04PM +0200, Pino Toscano wrote:
> 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
> ---
>  python/handle.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/python/handle.c b/python/handle.c
> index 88024e1..9746dca 100644
> --- a/python/handle.c
> +++ b/python/handle.c
> @@ -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;
>    }

Seems reasonable, ACK.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v




More information about the Libguestfs mailing list