--- embedaddon/miniupnpc/miniupnpcmodule.c 2012/02/21 23:16:22 1.1.1.1 +++ embedaddon/miniupnpc/miniupnpcmodule.c 2013/07/22 00:36:10 1.1.1.2 @@ -1,8 +1,8 @@ -/* $Id: miniupnpcmodule.c,v 1.1.1.1 2012/02/21 23:16:22 misho Exp $*/ +/* $Id: miniupnpcmodule.c,v 1.1.1.2 2013/07/22 00:36:10 misho Exp $*/ /* Project : miniupnp * Author : Thomas BERNARD * website : http://miniupnp.tuxfamily.org/ - * copyright (c) 2007-2009 Thomas Bernard + * copyright (c) 2007-2012 Thomas Bernard * This software is subjet to the conditions detailed in the * provided LICENCE file. */ #include @@ -25,6 +25,16 @@ #define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False #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 { PyObject_HEAD /* Type-specific fields go here. */ @@ -59,7 +69,7 @@ UPnPObject_dealloc(UPnPObject *self) { freeUPNPDevlist(self->devlist); FreeUPNPUrls(&self->urls); - self->ob_type->tp_free((PyObject*)self); + Py_TYPE(self)->tp_free((PyObject*)self); } static PyObject * @@ -72,7 +82,7 @@ UPnP_discover(UPnPObject *self) { freeUPNPDevlist(self->devlist); self->devlist = 0; - } + } Py_BEGIN_ALLOW_THREADS self->devlist = upnpDiscover((int)self->discoverdelay/*timeout in ms*/, 0/* multicast if*/, @@ -215,7 +225,7 @@ Py_END_ALLOW_THREADS } /* AddPortMapping(externalPort, protocol, internalHost, internalPort, desc, - * remoteHost) + * remoteHost) * protocol is 'UDP' or 'TCP' */ static PyObject * UPnP_addportmapping(UPnPObject *self, PyObject *args) @@ -300,7 +310,7 @@ Py_END_ALLOW_THREADS } } -/* GetSpecificPortMapping(ePort, proto) +/* GetSpecificPortMapping(ePort, proto) * proto = 'UDP' or 'TCP' */ static PyObject * UPnP_getspecificportmapping(UPnPObject *self, PyObject *args) @@ -434,8 +444,8 @@ static PyMethodDef UPnP_methods[] = { }; static PyTypeObject UPnPType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ + PyVarObject_HEAD_INIT(NULL, + 0) /*ob_size*/ "miniupnpc.UPnP", /*tp_name*/ sizeof(UPnPObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ @@ -472,7 +482,7 @@ static PyTypeObject UPnPType = { 0, /* tp_dictoffset */ 0,/*(initproc)UPnP_init,*/ /* tp_init */ 0, /* tp_alloc */ -#ifndef WIN32 +#ifndef _WIN32 PyType_GenericNew,/*UPnP_new,*/ /* tp_new */ #else 0, @@ -484,24 +494,51 @@ static PyMethodDef miniupnpc_methods[] = { {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 */ #define PyMODINIT_FUNC void #endif + PyMODINIT_FUNC -initminiupnpc(void) +#if PY_MAJOR_VERSION >= 3 +PyInit_miniupnpc(void) +#else +initminiupnpc(void) +#endif { PyObject* m; -#ifdef WIN32 +#ifdef _WIN32 UPnPType.tp_new = PyType_GenericNew; #endif if (PyType_Ready(&UPnPType) < 0) return; +#if PY_MAJOR_VERSION >= 3 + m = PyModule_Create(&moduledef); +#else m = Py_InitModule3("miniupnpc", miniupnpc_methods, "miniupnpc module."); +#endif Py_INCREF(&UPnPType); PyModule_AddObject(m, "UPnP", (PyObject *)&UPnPType); + +#if PY_MAJOR_VERSION >= 3 + return m; +#endif }