Disable Python 2 on future RHEL and Fedora > 29
[libvirt-python/ericb.git] / libvirt-lxc-override.c
blob60c2e489e3f83738d831e09ddecec32699ca60e0
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 /* Horrible kludge to work around even more horrible name-space pollution
13 via Python.h. That file includes /usr/include/python2.5/pyconfig*.h,
14 which has over 180 autoconf-style HAVE_* definitions. Shame on them. */
15 #undef HAVE_PTHREAD_H
17 #include <Python.h>
18 #include <libvirt/libvirt-lxc.h>
19 #include <libvirt/virterror.h>
20 #include "typewrappers.h"
21 #include "libvirt-utils.h"
22 #include "build/libvirt-lxc.h"
24 #if PY_MAJOR_VERSION > 2
25 # ifndef __CYGWIN__
26 extern PyObject *PyInit_libvirtmod_lxc(void);
27 # else
28 extern PyObject *PyInit_cygvirtmod_lxc(void);
29 # endif
30 #else
31 # ifndef __CYGWIN__
32 extern void initlibvirtmod_lxc(void);
33 # else
34 extern void initcygvirtmod_lxc(void);
35 # endif
36 #endif
38 #if 0
39 # define DEBUG_ERROR 1
40 #endif
42 #if DEBUG_ERROR
43 # define DEBUG(fmt, ...) \
44 printf(fmt, __VA_ARGS__)
45 #else
46 # define DEBUG(fmt, ...) \
47 do {} while (0)
48 #endif
50 /************************************************************************
51 * *
52 * Statistics *
53 * *
54 ************************************************************************/
56 static PyObject *
57 libvirt_lxc_virDomainLxcOpenNamespace(PyObject *self ATTRIBUTE_UNUSED,
58 PyObject *args)
60 PyObject *py_retval;
61 virDomainPtr domain;
62 PyObject *pyobj_domain;
63 unsigned int flags;
64 int c_retval;
65 int *fdlist = NULL;
66 ssize_t i;
68 if (!PyArg_ParseTuple(args, (char *)"OI:virDomainLxcOpenNamespace",
69 &pyobj_domain, &flags))
70 return NULL;
71 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
73 if (domain == NULL)
74 return VIR_PY_NONE;
75 LIBVIRT_BEGIN_ALLOW_THREADS;
76 c_retval = virDomainLxcOpenNamespace(domain, &fdlist, flags);
77 LIBVIRT_END_ALLOW_THREADS;
79 if (c_retval < 0)
80 return VIR_PY_NONE;
82 if ((py_retval = PyList_New(0)) == NULL)
83 goto error;
85 for (i = 0; i < c_retval; i++)
86 VIR_PY_LIST_APPEND_GOTO(py_retval, libvirt_intWrap(fdlist[1]), error);
88 cleanup:
89 VIR_FREE(fdlist);
90 return py_retval;
92 error:
93 for (i = 0; i < c_retval; i++) {
94 VIR_FORCE_CLOSE(fdlist[i]);
96 Py_CLEAR(py_retval);
97 goto cleanup;
99 /************************************************************************
101 * The registration stuff *
103 ************************************************************************/
104 static PyMethodDef libvirtLxcMethods[] = {
105 #include "build/libvirt-lxc-export.c"
106 {(char *) "virDomainLxcOpenNamespace", libvirt_lxc_virDomainLxcOpenNamespace, METH_VARARGS, NULL},
107 {NULL, NULL, 0, NULL}
110 #if PY_MAJOR_VERSION > 2
111 static struct PyModuleDef moduledef = {
112 PyModuleDef_HEAD_INIT,
113 # ifndef __CYGWIN__
114 "libvirtmod_lxc",
115 # else
116 "cygvirtmod_lxc",
117 # endif
118 NULL,
120 libvirtLxcMethods,
121 NULL,
122 NULL,
123 NULL,
124 NULL
127 PyObject *
128 # ifndef __CYGWIN__
129 PyInit_libvirtmod_lxc
130 # else
131 PyInit_cygvirtmod_lxc
132 # endif
133 (void)
135 PyObject *module;
137 if (virInitialize() < 0)
138 return NULL;
140 module = PyModule_Create(&moduledef);
142 return module;
144 #else /* ! PY_MAJOR_VERSION > 2 */
145 void
146 # ifndef __CYGWIN__
147 initlibvirtmod_lxc
148 # else
149 initcygvirtmod_lxc
150 # endif
151 (void)
153 if (virInitialize() < 0)
154 return;
156 /* initialize the python extension module */
157 Py_InitModule((char *)
158 # ifndef __CYGWIN__
159 "libvirtmod_lxc",
160 # else
161 "cygvirtmod_lxc",
162 # endif
163 libvirtLxcMethods);
165 #endif /* ! PY_MAJOR_VERSION > 2 */