miniupnpc: Supports Python 3

This commit is contained in:
Christopher Foo 2012-08-28 21:25:00 -04:00
parent 34c5af41ad
commit 0c652db9dd
2 changed files with 48 additions and 4 deletions

View File

@ -131,6 +131,13 @@ pythonmodule: $(LIBRARY) miniupnpcmodule.c setup.py
installpythonmodule: pythonmodule installpythonmodule: pythonmodule
python setup.py install python setup.py install
pythonmodule3: $(LIBRARY) miniupnpcmodule.c setup.py
python3 setup.py build
touch $@
installpythonmodule3: pythonmodule3
python3 setup.py install
validateminixml: minixmlvalid validateminixml: minixmlvalid
@echo "minixml validation test" @echo "minixml validation test"
./minixmlvalid ./minixmlvalid
@ -144,7 +151,7 @@ validateminiwget: testminiwget minihttptestserver testminiwget.sh
clean: clean:
$(RM) $(LIBRARY) $(SHAREDLIBRARY) $(EXECUTABLES) $(OBJS) miniupnpcstrings.h $(RM) $(LIBRARY) $(SHAREDLIBRARY) $(EXECUTABLES) $(OBJS) miniupnpcstrings.h
# clean python stuff # clean python stuff
$(RM) pythonmodule validateminixml validateminiwget $(RM) pythonmodule pythonmodule3 validateminixml validateminiwget
$(RM) -r build/ dist/ $(RM) -r build/ dist/
#python setup.py clean #python setup.py clean
# clean jnaerator stuff # clean jnaerator stuff

View File

@ -25,6 +25,16 @@
#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False #define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
#endif #endif
/* for compatibility with Python < 3.0 */
#ifndef PyVarObject_HEAD_INIT
#define PyVarObject_HEAD_INIT(type, size) \
PyObject_HEAD_INIT(type) size,
#endif
#ifndef Py_TYPE
#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
#endif
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
/* Type-specific fields go here. */ /* Type-specific fields go here. */
@ -59,7 +69,7 @@ UPnPObject_dealloc(UPnPObject *self)
{ {
freeUPNPDevlist(self->devlist); freeUPNPDevlist(self->devlist);
FreeUPNPUrls(&self->urls); FreeUPNPUrls(&self->urls);
self->ob_type->tp_free((PyObject*)self); Py_TYPE(self)->tp_free((PyObject*)self);
} }
static PyObject * static PyObject *
@ -434,8 +444,8 @@ static PyMethodDef UPnP_methods[] = {
}; };
static PyTypeObject UPnPType = { static PyTypeObject UPnPType = {
PyObject_HEAD_INIT(NULL) PyVarObject_HEAD_INIT(NULL,
0, /*ob_size*/ 0) /*ob_size*/
"miniupnpc.UPnP", /*tp_name*/ "miniupnpc.UPnP", /*tp_name*/
sizeof(UPnPObject), /*tp_basicsize*/ sizeof(UPnPObject), /*tp_basicsize*/
0, /*tp_itemsize*/ 0, /*tp_itemsize*/
@ -484,11 +494,30 @@ static PyMethodDef miniupnpc_methods[] = {
{NULL} /* Sentinel */ {NULL} /* Sentinel */
}; };
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"miniupnpc", /* m_name */
"miniupnpc module.", /* m_doc */
-1, /* m_size */
miniupnpc_methods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};
#endif
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
#define PyMODINIT_FUNC void #define PyMODINIT_FUNC void
#endif #endif
PyMODINIT_FUNC PyMODINIT_FUNC
#if PY_MAJOR_VERSION >= 3
PyInit_miniupnpc(void)
#else
initminiupnpc(void) initminiupnpc(void)
#endif
{ {
PyObject* m; PyObject* m;
@ -498,10 +527,18 @@ initminiupnpc(void)
if (PyType_Ready(&UPnPType) < 0) if (PyType_Ready(&UPnPType) < 0)
return; return;
#if PY_MAJOR_VERSION >= 3
m = PyModule_Create(&moduledef);
#else
m = Py_InitModule3("miniupnpc", miniupnpc_methods, m = Py_InitModule3("miniupnpc", miniupnpc_methods,
"miniupnpc module."); "miniupnpc module.");
#endif
Py_INCREF(&UPnPType); Py_INCREF(&UPnPType);
PyModule_AddObject(m, "UPnP", (PyObject *)&UPnPType); PyModule_AddObject(m, "UPnP", (PyObject *)&UPnPType);
#if PY_MAJOR_VERSION >= 3
return m;
#endif
} }