python: fix fd leak in generator.py
[libvirt-python/ericb.git] / libvirt-lxc-override.c
blobc80668ef4552ddb527267d6701a63915198c3408
1 /*
2 * libvir.c: this modules implements the main part of the glue of the
3 * libvir library and the Python interpreter. It provides the
4 * entry points where an automatically generated stub is
5 * unpractical
7 * Copyright (C) 2012-2013 Red Hat, Inc.
9 * Daniel Veillard <veillard@redhat.com>
12 #include <config.h>
14 /* Horrible kludge to work around even more horrible name-space pollution
15 via Python.h. That file includes /usr/include/python2.5/pyconfig*.h,
16 which has over 180 autoconf-style HAVE_* definitions. Shame on them. */
17 #undef HAVE_PTHREAD_H
19 #include <Python.h>
20 #include "libvirt/libvirt-lxc.h"
21 #include "libvirt/virterror.h"
22 #include "typewrappers.h"
23 #include "libvirt-lxc.h"
24 #include "viralloc.h"
25 #include "virfile.h"
27 #ifndef __CYGWIN__
28 extern void initlibvirtmod_lxc(void);
29 #else
30 extern void initcygvirtmod_lxc(void);
31 #endif
33 #if 0
34 # define DEBUG_ERROR 1
35 #endif
37 #if DEBUG_ERROR
38 # define DEBUG(fmt, ...) \
39 printf(fmt, __VA_ARGS__)
40 #else
41 # define DEBUG(fmt, ...) \
42 do {} while (0)
43 #endif
45 /* The two-statement sequence "Py_INCREF(Py_None); return Py_None;"
46 is so common that we encapsulate it here. Now, each use is simply
47 return VIR_PY_NONE; */
48 #define VIR_PY_NONE (Py_INCREF (Py_None), Py_None)
49 #define VIR_PY_INT_FAIL (libvirt_intWrap(-1))
50 #define VIR_PY_INT_SUCCESS (libvirt_intWrap(0))
52 /************************************************************************
53 * *
54 * Statistics *
55 * *
56 ************************************************************************/
58 static PyObject *
59 libvirt_lxc_virDomainLxcOpenNamespace(PyObject *self ATTRIBUTE_UNUSED,
60 PyObject *args) {
61 PyObject *py_retval;
62 virDomainPtr domain;
63 PyObject *pyobj_domain;
64 unsigned int flags;
65 int c_retval;
66 int *fdlist = NULL;
67 int i;
69 if (!PyArg_ParseTuple(args, (char *)"Oi:virDomainLxcOpenNamespace",
70 &pyobj_domain, &flags))
71 return NULL;
72 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
74 if (domain == NULL)
75 return VIR_PY_NONE;
76 LIBVIRT_BEGIN_ALLOW_THREADS;
77 c_retval = virDomainLxcOpenNamespace(domain, &fdlist, flags);
78 LIBVIRT_END_ALLOW_THREADS;
80 if (c_retval < 0)
81 return VIR_PY_NONE;
83 py_retval = PyList_New(c_retval);
84 for (i = 0 ; i < c_retval ; i++) {
85 PyObject *item = NULL;
87 if ((item = PyInt_FromLong(fdlist[i])) == NULL)
88 goto error;
90 if (PyList_Append(py_retval, item) < 0) {
91 Py_DECREF(item);
92 goto error;
95 return py_retval;
97 error:
98 for (i = 0 ; i < c_retval ; i++) {
99 VIR_FORCE_CLOSE(fdlist[i]);
101 VIR_FREE(fdlist);
102 return VIR_PY_NONE;
104 /************************************************************************
106 * The registration stuff *
108 ************************************************************************/
109 static PyMethodDef libvirtLxcMethods[] = {
110 #include "libvirt-lxc-export.c"
111 {(char *) "virDomainLxcOpenNamespace", libvirt_lxc_virDomainLxcOpenNamespace, METH_VARARGS, NULL},
112 {NULL, NULL, 0, NULL}
115 void
116 #ifndef __CYGWIN__
117 initlibvirtmod_lxc
118 #else
119 initcygvirtmod_lxc
120 #endif
121 (void)
123 static int initialized = 0;
125 if (initialized != 0)
126 return;
128 if (virInitialize() < 0)
129 return;
131 /* initialize the python extension module */
132 Py_InitModule((char *)
133 #ifndef __CYGWIN__
134 "libvirtmod_lxc"
135 #else
136 "cygvirtmod_lxc"
137 #endif
138 , libvirtLxcMethods);
140 initialized = 1;