gobject: Add an explicit close call

This change binds guestfs_close(). It consequently results in RConstOptString
being able to throw an error.
This commit is contained in:
Matthew Booth
2012-01-25 15:10:01 +00:00
parent 6568dd85f4
commit 8bad4d1c47
4 changed files with 90 additions and 13 deletions

View File

@@ -156,6 +156,7 @@ struct _GuestfsSessionClass
GType guestfs_session_get_type(void);
GuestfsSession *guestfs_session_new(void);
gboolean guestfs_session_close(GuestfsSession *session, GError **err);
/* Guestfs::Tristate */
typedef enum
@@ -304,6 +305,16 @@ let generate_gobject_c_static () =
* images.
*/
/* Error quark */
#define GUESTFS_ERROR guestfs_error_quark()
static GQuark
guestfs_error_quark(void)
{
return g_quark_from_static_string(\"guestfs\");
}
#define GUESTFS_SESSION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ( \
(obj), \
GUESTFS_TYPE_SESSION, \
@@ -357,6 +368,29 @@ guestfs_session_new(void)
return GUESTFS_SESSION(g_object_new(GUESTFS_TYPE_SESSION, NULL));
}
/**
* guestfs_session_close:
*
* Close a libguestfs session.
*
* Returns: true on success, false on error
*/
gboolean
guestfs_session_close(GuestfsSession *session, GError **err)
{
guestfs_h *g = session->priv->g;
if (g == NULL) {
g_set_error_literal(err, GUESTFS_ERROR, 0, \"session is already closed\");
return FALSE;
}
guestfs_close(g);
session->priv->g = NULL;
return TRUE;
}
/* Guestfs::Tristate */
GType
guestfs_tristate_get_type(void)
@@ -374,16 +408,6 @@ guestfs_tristate_get_type(void)
return etype;
}
/* Error quark */
#define GUESTFS_ERROR guestfs_error_quark()
static GQuark
guestfs_error_quark(void)
{
return g_quark_from_static_string(\"guestfs\");
}
/* Cancellation handler */
static void
cancelled_handler(gpointer data)
@@ -606,7 +630,9 @@ let generate_gobject_c_methods () =
| RConstString _ | RString _ | RStringList _ | RHashtable _
| RBufferOut _ | RStruct _ | RStructList _ ->
"NULL"
| RConstOptString _ -> ""
| RConstOptString _ ->
"NULL" (* NULL is a valid return for RConstOptString. Error is
indicated by also setting *err to a non-NULL value *)
in
(* The comment header, including GI annotations for arguments and the
@@ -687,7 +713,15 @@ let generate_gobject_c_methods () =
pr " return %s;\n\n" gobject_error_return;
);
(* Get the guestfs handle, and ensure it isn't closed *)
pr " guestfs_h *g = session->priv->g;\n";
pr " if (g == NULL) {\n";
pr " g_set_error(err, GUESTFS_ERROR, 0,\n";
pr " \"attempt to call %%s after the session has been closed\",\n";
pr " \"%s\");\n" name;
pr " return %s;\n" gobject_error_return;
pr " }\n\n";
(* Optargs *)

View File

@@ -22,7 +22,8 @@ BUILT_SOURCES = \
EXTRA_DIST = \
$(BUILT_SOURCES) \
bindtests-manual.js \
run-bindtests
tests-misc.js \
run-tests
libname = libguestfs-gobject-1.0.la
@@ -60,7 +61,7 @@ gir_DATA = $(INTROSPECTION_GIRS)
typelibdir = $(libdir)/girepository-1.0
typelib_DATA = $(INTROSPECTION_GIRS:.gir=.typelib)
TESTS = run-bindtests
TESTS = run-tests
TESTS_ENVIRONMENT = GJS=$(GJS)
$(TESTS): $(typelib_DATA)

View File

@@ -26,3 +26,4 @@ fi
../run $GJS $srcdir/bindtests.js > bindtests.tmp
diff -u ${srcdir}/../bindtests bindtests.tmp
../run $GJS $srcdir/bindtests-manual.js 2>/dev/null
../run $GJS $srcdir/tests-misc.js 2>/dev/null

41
gobject/tests-misc.js Normal file
View File

@@ -0,0 +1,41 @@
// libguestfs miscellaneous gobject binding tests
// Copyright (C) 2012 Red Hat Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
const Guestfs = imports.gi.Guestfs;
var fail = false;
var g = new Guestfs.Session();
// Test close()
g.close();
var threw = false;
try {
var v = g.test0rconstoptstring('1');
} catch (error) {
threw = true;
if (!error.message.match(/closed/)) {
print("call after close threw unexpected error: " + error.message);
fail = true;
}
}
if (!threw) {
print("call after closed failed to throw an error");
fail = true;
}
fail ? 1 : 0;