Pino Toscano
2016-May-05 14:09 UTC
[Libguestfs] [PATCH] python: use constants instead of raw values
Extend the internal libguestfsmod module with the CREATE values, so there is no need to hardcode values in GuestFS.__init__. --- generator/python.ml | 7 +++++-- python/guestfs-py-byhand.c | 7 +++++++ python/guestfs-py.h | 2 ++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/generator/python.ml b/generator/python.ml index 9744b8f..470abe7 100644 --- a/generator/python.ml +++ b/generator/python.ml @@ -597,6 +597,9 @@ moduleinit (void) m = Py_InitModule ((char *) \"libguestfsmod\", methods); #endif + if (m != NULL) + guestfs_int_py_extend_module (m); + return m; /* m might be NULL if module init failed */ } @@ -719,9 +722,9 @@ class GuestFS(object): \"\"\" flags = 0 if not environment: - flags |= 1 + flags |= libguestfsmod.GUESTFS_CREATE_NO_ENVIRONMENT if not close_on_exit: - flags |= 2 + flags |= libguestfsmod.GUESTFS_CREATE_NO_CLOSE_ON_EXIT self._o = libguestfsmod.create(flags) self._python_return_dict = python_return_dict diff --git a/python/guestfs-py-byhand.c b/python/guestfs-py-byhand.c index cf8576f..9e2debf 100644 --- a/python/guestfs-py-byhand.c +++ b/python/guestfs-py-byhand.c @@ -37,6 +37,13 @@ static PyObject **get_all_event_callbacks (guestfs_h *g, size_t *len_rtn); +void +guestfs_int_py_extend_module (PyObject *module) +{ + PyModule_AddIntMacro(module, GUESTFS_CREATE_NO_ENVIRONMENT); + PyModule_AddIntMacro(module, GUESTFS_CREATE_NO_CLOSE_ON_EXIT); +} + PyObject * guestfs_int_py_create (PyObject *self, PyObject *args) { diff --git a/python/guestfs-py.h b/python/guestfs-py.h index da5f483..f2246a6 100644 --- a/python/guestfs-py.h +++ b/python/guestfs-py.h @@ -59,6 +59,8 @@ put_handle (guestfs_h *g) #endif } +extern void guestfs_int_py_extend_module (PyObject *module); + extern PyObject *guestfs_int_py_create (PyObject *self, PyObject *args); extern PyObject *guestfs_int_py_close (PyObject *self, PyObject *args); extern PyObject *guestfs_int_py_set_event_callback (PyObject *self, PyObject *args); -- 2.5.5
Richard W.M. Jones
2016-May-05 21:00 UTC
Re: [Libguestfs] [PATCH] python: use constants instead of raw values
On Thu, May 05, 2016 at 04:09:35PM +0200, Pino Toscano wrote:> Extend the internal libguestfsmod module with the CREATE values, so > there is no need to hardcode values in GuestFS.__init__. > --- > generator/python.ml | 7 +++++-- > python/guestfs-py-byhand.c | 7 +++++++ > python/guestfs-py.h | 2 ++ > 3 files changed, 14 insertions(+), 2 deletions(-) > > diff --git a/generator/python.ml b/generator/python.ml > index 9744b8f..470abe7 100644 > --- a/generator/python.ml > +++ b/generator/python.ml > @@ -597,6 +597,9 @@ moduleinit (void) > m = Py_InitModule ((char *) \"libguestfsmod\", methods); > #endif > > + if (m != NULL) > + guestfs_int_py_extend_module (m); > + > return m; /* m might be NULL if module init failed */ > } > > @@ -719,9 +722,9 @@ class GuestFS(object): > \"\"\" > flags = 0 > if not environment: > - flags |= 1 > + flags |= libguestfsmod.GUESTFS_CREATE_NO_ENVIRONMENT > if not close_on_exit: > - flags |= 2 > + flags |= libguestfsmod.GUESTFS_CREATE_NO_CLOSE_ON_EXIT > self._o = libguestfsmod.create(flags) > self._python_return_dict = python_return_dict > > diff --git a/python/guestfs-py-byhand.c b/python/guestfs-py-byhand.c > index cf8576f..9e2debf 100644 > --- a/python/guestfs-py-byhand.c > +++ b/python/guestfs-py-byhand.c > @@ -37,6 +37,13 @@ > > static PyObject **get_all_event_callbacks (guestfs_h *g, size_t *len_rtn); > > +void > +guestfs_int_py_extend_module (PyObject *module) > +{ > + PyModule_AddIntMacro(module, GUESTFS_CREATE_NO_ENVIRONMENT); > + PyModule_AddIntMacro(module, GUESTFS_CREATE_NO_CLOSE_ON_EXIT); > +} > + > PyObject * > guestfs_int_py_create (PyObject *self, PyObject *args) > { > diff --git a/python/guestfs-py.h b/python/guestfs-py.h > index da5f483..f2246a6 100644 > --- a/python/guestfs-py.h > +++ b/python/guestfs-py.h > @@ -59,6 +59,8 @@ put_handle (guestfs_h *g) > #endif > } > > +extern void guestfs_int_py_extend_module (PyObject *module); > + > extern PyObject *guestfs_int_py_create (PyObject *self, PyObject *args); > extern PyObject *guestfs_int_py_close (PyObject *self, PyObject *args); > extern PyObject *guestfs_int_py_set_event_callback (PyObject *self, PyObject *args); > --ACK. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html
Apparently Analagous Threads
- [PATCH 0/2] python: improved UTF8 decoding error handling
- [PATCH 2/2] python: unicode decode handler error scheme setter
- very fast OLS regression?
- Re: make install ignoring PREFIX for bash_completion
- [PATCH 12/13] syntax-check: fix require_config_h_first check