[lxc-devel] [PATCH] python3: binding update
Serge Hallyn
serge.hallyn at ubuntu.com
Thu Jan 16 20:56:24 UTC 2014
Quoting Stéphane Graber (stgraber at ubuntu.com):
> On Thu, Jan 16, 2014 at 01:09:58PM -0600, Serge Hallyn wrote:
> > Quoting Stéphane Graber (stgraber at ubuntu.com):
> > > This adds rename(new_name) to the binding as well as two new const,
> > > LXC_CLONE_KEEPBDEVTYPE and LXC_CLONE_MAYBE_SNAPSHOT.
> > >
> > > Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
> > > ---
> > > src/python-lxc/lxc.c | 25 +++++++++++++++++++++++++
> > > src/python-lxc/lxc/__init__.py | 14 ++++++++++++++
> > > 2 files changed, 39 insertions(+)
> > >
> > > diff --git a/src/python-lxc/lxc.c b/src/python-lxc/lxc.c
> > > index 4381ab8..33b3e41 100644
> > > --- a/src/python-lxc/lxc.c
> > > +++ b/src/python-lxc/lxc.c
> > > @@ -1038,6 +1038,23 @@ Container_reboot(Container *self, PyObject *args, PyObject *kwds)
> > > }
> > >
> > > static PyObject *
> > > +Container_rename(Container *self, PyObject *args, PyObject *kwds)
> > > +{
> > > + char *new_name = NULL;
> > > + static char *kwlist[] = {"new_name", NULL};
> > > +
> > > + if (! PyArg_ParseTupleAndKeywords(args, kwds, "s|", kwlist,
> > > + &new_name))
> > > + return NULL;
> > > +
> > > + if (self->container->rename(self->container, new_name)) {
> > > + Py_RETURN_TRUE;
> > > + }
> > > +
> > > + Py_RETURN_FALSE;
> > > +}
> > > +
> > > +static PyObject *
> > > Container_remove_device_node(Container *self, PyObject *args, PyObject *kwds)
> > > {
> > > static char *kwlist[] = {"src_path", "dest_path", NULL};
> > > @@ -1529,6 +1546,12 @@ static PyMethodDef Container_methods[] = {
> > > "\n"
> > > "Ask the container to reboot."
> > > },
> > > + {"rename", (PyCFunction)Container_rename,
> > > + METH_VARARGS|METH_KEYWORDS,
> > > + "rename(new_name) -> boolean\n"
> > > + "\n"
> > > + "Rename the container."
> > > + },
> > > {"remove_device_node", (PyCFunction)Container_remove_device_node,
> > > METH_VARARGS|METH_KEYWORDS,
> > > "remove_device_node(src_path, dest_path) -> boolean\n"
> > > @@ -1740,8 +1763,10 @@ PyInit__lxc(void)
> > > PYLXC_EXPORT_CONST(LXC_ATTACH_SET_PERSONALITY);
> > >
> > > /* clone: clone flags */
> > > + PYLXC_EXPORT_CONST(LXC_CLONE_KEEPBDEVTYPE);
> > > PYLXC_EXPORT_CONST(LXC_CLONE_KEEPMACADDR);
> > > PYLXC_EXPORT_CONST(LXC_CLONE_KEEPNAME);
> > > + PYLXC_EXPORT_CONST(LXC_CLONE_MAYBE_SNAPSHOT);
> > > PYLXC_EXPORT_CONST(LXC_CLONE_SNAPSHOT);
> > >
> > > /* create: create flags */
> > > diff --git a/src/python-lxc/lxc/__init__.py b/src/python-lxc/lxc/__init__.py
> > > index 0ca3e54..fa74297 100644
> > > --- a/src/python-lxc/lxc/__init__.py
> > > +++ b/src/python-lxc/lxc/__init__.py
> > > @@ -327,6 +327,18 @@ class Container(_lxc.Container):
> > >
> > > return ips
> > >
> > > + def rename(self, new_name):
> > > + """
> > > + Rename the container.
> > > + On success, returns the new Container object.
> > > + On failure, returns False.
> > > + """
> > > +
> > > + if _lxc.Container.rename(self, new_name):
> > > + return Container(new_name)
> >
> > Does this work? Will python automatically set self to new_name?
>
> No, it won't alter self, it'll return you a new Container object.
>
> So you should do something like:
> container = lxc.Container("p1")
> new_container = container.rename("p2")
>
> if new_container:
> container = new_container
>
> I originally tried to do "self = Container(new_name)" and while it
> doesn't fail, it doesn't alter self either and I didn't want to resort
> to even more magic :)
>
> The behavior I implemented also happens to be identical to that of clone().
Hm, ok. I wonder if there is a better way, but in any case
Acked-by: Serge E. Hallyn <serge.hallyn at ubuntu.com>
More information about the lxc-devel
mailing list