summaryrefslogtreecommitdiffstats
path: root/generator/generator_python.ml
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2011-03-07 19:28:30 +0000
committerRichard W.M. Jones <rjones@redhat.com>2011-03-08 15:11:07 +0000
commit1521c64102c36ec4ab75529d246c92802d3f310c (patch)
tree0d0ea513b293fe50a882e17f448559212f2540fa /generator/generator_python.ml
parentf3240bc31e9de78eb40f34841e06a42fc7a81fe7 (diff)
downloadlibguestfs-1521c64102c36ec4ab75529d246c92802d3f310c.tar.gz
libguestfs-1521c64102c36ec4ab75529d246c92802d3f310c.tar.xz
libguestfs-1521c64102c36ec4ab75529d246c92802d3f310c.zip
generator: Introduce error code (errcode) concept.
There was a lot of repeated code to map return types (eg. RErr) to error cases (eg. -1 or NULL). This commit introduces an error code type and two functions to map return types to error codes and error codes to strings. Cherry picked from commit 8037da06feea097716ce700f38c0eac0d5411a7c and rebased against stable 1.8 branch.
Diffstat (limited to 'generator/generator_python.ml')
-rw-r--r--generator/generator_python.ml46
1 files changed, 27 insertions, 19 deletions
diff --git a/generator/generator_python.ml b/generator/generator_python.ml
index bc570a88..937c092d 100644
--- a/generator/generator_python.ml
+++ b/generator/generator_python.ml
@@ -293,21 +293,20 @@ py_guestfs_close (PyObject *self, PyObject *args)
pr " struct guestfs_%s_argv *optargs = &optargs_s;\n" name;
);
- let error_code =
- match ret with
- | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1"
- | RInt64 _ -> pr " int64_t r;\n"; "-1"
- | RConstString _ | RConstOptString _ ->
- pr " const char *r;\n"; "NULL"
- | RString _ -> pr " char *r;\n"; "NULL"
- | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL"
- | RStruct (_, typ) -> pr " struct guestfs_%s *r;\n" typ; "NULL"
- | RStructList (_, typ) ->
- pr " struct guestfs_%s_list *r;\n" typ; "NULL"
- | RBufferOut _ ->
- pr " char *r;\n";
- pr " size_t size;\n";
- "NULL" in
+ (match ret with
+ | RErr | RInt _ | RBool _ -> pr " int r;\n"
+ | RInt64 _ -> pr " int64_t r;\n"
+ | RConstString _ | RConstOptString _ ->
+ pr " const char *r;\n"
+ | RString _ -> pr " char *r;\n"
+ | RStringList _ | RHashtable _ -> pr " char **r;\n"
+ | RStruct (_, typ) -> pr " struct guestfs_%s *r;\n" typ
+ | RStructList (_, typ) ->
+ pr " struct guestfs_%s_list *r;\n" typ
+ | RBufferOut _ ->
+ pr " char *r;\n";
+ pr " size_t size;\n"
+ );
List.iter (
function
@@ -457,10 +456,19 @@ py_guestfs_close (PyObject *self, PyObject *args)
pr " free (%s);\n" n
) args;
- pr " if (r == %s) {\n" error_code;
- pr " PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g));\n";
- pr " return NULL;\n";
- pr " }\n";
+ (match errcode_of_ret ret with
+ | `CannotReturnError -> ()
+ | `ErrorIsMinusOne ->
+ pr " if (r == -1) {\n";
+ pr " PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g));\n";
+ pr " return NULL;\n";
+ pr " }\n"
+ | `ErrorIsNULL ->
+ pr " if (r == NULL) {\n";
+ pr " PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g));\n";
+ pr " return NULL;\n";
+ pr " }\n"
+ );
pr "\n";
(match ret with