Pino Toscano
2017-May-11 12:37 UTC
[Libguestfs] [PATCH] 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
---
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;
}
--
2.9.3
Richard W.M. Jones
2017-May-12 15:21 UTC
Re: [Libguestfs] [PATCH] python: improve few exceptions thrown on error
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
Maybe Matching Threads
- [PATCH 1/2] Add type checking, support integers as value
- [PATCH v2] python: add simple wrappers for PyObject<->string functions
- [Bug #1406906] [PATCH] python: fix segmentation fault when setting non UTF-8 strings
- [PATCH] python: add simple wrappers for PyObject<->string functions
- [hivex] Segfault for an integer value to node_set_value