diff --git a/Examples/test-suite/python/input.i b/Examples/test-suite/python/input.i index 6e2f90296..6cef3f9f5 100644 --- a/Examples/test-suite/python/input.i +++ b/Examples/test-suite/python/input.i @@ -6,8 +6,7 @@ if ($1) { $result = SWIG_From(int)(*$1); } else { - Py_INCREF(Py_None); - $result = Py_None; + $result = SWIG_Py_Void(); } } @@ -30,8 +29,7 @@ if ($1) { $result = SWIG_From(std::string)(*$1); } else { - Py_INCREF(Py_None); - $result = Py_None; + $result = SWIG_Py_Void(); } } diff --git a/Lib/python/pyapi.swg b/Lib/python/pyapi.swg index 28cd836a6..265ac8f63 100644 --- a/Lib/python/pyapi.swg +++ b/Lib/python/pyapi.swg @@ -4,6 +4,9 @@ #ifdef __cplusplus extern "C" { +#if 0 +} /* cc-mode */ +#endif #endif /* ----------------------------------------------------------------------------- @@ -16,16 +19,40 @@ extern "C" { /* Constant information structure */ typedef struct swig_const_info { - int type; - char *name; - long lvalue; - double dvalue; - void *pvalue; - swig_type_info **ptype; + int type; + char *name; + long lvalue; + double dvalue; + void *pvalue; + swig_type_info **ptype; } swig_const_info; +/* ----------------------------------------------------------------------------- + * Safe Py_None and Py_Void accessors + * ----------------------------------------------------------------------------- */ + +SWIGRUNTIME PyObject * +SWIG_Py_None(void) +{ + static PyObject *none = 0; + if (!none) { + none = Py_BuildValue(""); + Py_DECREF(none); + } + return none; +} + +SWIGRUNTIMEINLINE PyObject * +SWIG_Py_Void(void) +{ + PyObject *none = SWIG_Py_None(); + Py_INCREF(none); + return none; +} + + /* ----------------------------------------------------------------------------- * Append a value to the result obj * ----------------------------------------------------------------------------- */ @@ -33,7 +60,7 @@ SWIGINTERN PyObject* SWIG_Python_AppendOutput(PyObject* result, PyObject* obj) { if (!result) { result = obj; - } else if (result == Py_None) { + } else if (result == SWIG_Py_None()) { Py_DECREF(result); result = obj; } else { @@ -49,6 +76,9 @@ SWIG_Python_AppendOutput(PyObject* result, PyObject* obj) { } #ifdef __cplusplus +#if 0 +{ /* cc-mode */ +#endif } #endif diff --git a/Lib/python/pycontainer.swg b/Lib/python/pycontainer.swg index 2b02eae9b..09daa27e5 100644 --- a/Lib/python/pycontainer.swg +++ b/Lib/python/pycontainer.swg @@ -262,8 +262,7 @@ namespace swig { %ignore stop_iteration; %typemap(throws) stop_iteration { - Py_INCREF(Py_None); - PyErr_SetObject(PyExc_StopIteration, Py_None); + PyErr_SetObject(PyExc_StopIteration, SWIG_Py_Void()); } } diff --git a/Lib/python/pyinit.swg b/Lib/python/pyinit.swg index fbd058441..5d9b641db 100644 --- a/Lib/python/pyinit.swg +++ b/Lib/python/pyinit.swg @@ -96,7 +96,7 @@ swig_varlink_type(void) { const PyTypeObject tmp #endif = { - PyObject_HEAD_INIT(&PyType_Type) + PyObject_HEAD_INIT(NULL) 0, /* Number of items in variable part (ob_size) */ (char *)"swigvarlink", /* Type name (tp_name) */ sizeof(swig_varlinkobject), /* Basic size (tp_basicsize) */ @@ -137,6 +137,7 @@ swig_varlink_type(void) { type_init = 1; } #endif + varlink_type.ob_type = &PyType_Type; return &varlink_type; } diff --git a/Lib/python/pyrun.swg b/Lib/python/pyrun.swg index 74a3dbe48..e2a66c20d 100644 --- a/Lib/python/pyrun.swg +++ b/Lib/python/pyrun.swg @@ -262,24 +262,24 @@ SWIGRUNTIME PyObject* PySwigObject_append(PyObject* v, PyObject* next) { PySwigObject *sobj = (PySwigObject *) v; - if (!PySwigObject_Check(next)) + if (!PySwigObject_Check(next)) { return NULL; - + } sobj->next = next; Py_INCREF(next); - - Py_INCREF(Py_None); - return Py_None; + return SWIG_Py_Void(); } SWIGRUNTIME PyObject* PySwigObject_next(PyObject* v) { PySwigObject *sobj = (PySwigObject *) v; - PyObject *obj = sobj->next ? sobj->next : Py_None; - - Py_INCREF(obj); - return obj; + if (sobj->next) { + Py_INCREF(sobj->next); + return sobj->next; + } else { + return SWIG_Py_Void(); + } } SWIGINTERN PyObject* @@ -287,8 +287,7 @@ PySwigObject_disown(PyObject *v) { PySwigObject *sobj = (PySwigObject *)v; sobj->own = 0; - Py_INCREF(Py_None); - return Py_None; + return SWIG_Py_Void(); } SWIGINTERN PyObject* @@ -296,8 +295,7 @@ PySwigObject_acquire(PyObject *v) { PySwigObject *sobj = (PySwigObject *)v; sobj->own = SWIG_POINTER_OWN; - Py_INCREF(Py_None); - return Py_None; + return SWIG_Py_Void(); } SWIGINTERN PyObject* @@ -373,7 +371,7 @@ PySwigObject_type(void) { const PyTypeObject tmp #endif = { - PyObject_HEAD_INIT(&PyType_Type) + PyObject_HEAD_INIT(NULL) 0, /* ob_size */ (char *)"PySwigObject", /* tp_name */ sizeof(PySwigObject), /* tp_basicsize */ @@ -433,6 +431,7 @@ PySwigObject_type(void) { type_init = 1; } #endif + pyswigobject_type.ob_type = &PyType_Type; return &pyswigobject_type; } @@ -702,8 +701,8 @@ SWIG_Python_AcquirePtr(PyObject *obj, int own) { SWIGRUNTIME int SWIG_Python_ConvertPtrAndOwn(PyObject *obj, void **ptr, swig_type_info *ty, int flags, int *own) { if (!obj) return SWIG_ERROR; - if (obj == Py_None) { - *ptr = 0; + if (obj == SWIG_Py_None()) { + if (ptr) *ptr = 0; return SWIG_OK; } else { PySwigObject *sobj = SWIG_Python_GetSwigThis(obj); @@ -713,7 +712,7 @@ SWIG_Python_ConvertPtrAndOwn(PyObject *obj, void **ptr, swig_type_info *ty, int swig_type_info *to = sobj->ty; if (to == ty) { /* no type cast needed */ - *ptr = vptr; + if (ptr) *ptr = vptr; break; } else { swig_cast_info *tc = SWIG_TypeCheck(to->name,ty); @@ -725,7 +724,7 @@ SWIG_Python_ConvertPtrAndOwn(PyObject *obj, void **ptr, swig_type_info *ty, int } } } else { - *ptr = vptr; + if (ptr) *ptr = vptr; break; } } @@ -834,8 +833,7 @@ SWIG_Python_NewShadowInstance(PySwigClientData *data, PyObject *swig_this) SWIGRUNTIME PyObject * SWIG_Python_NewPointerObj(void *ptr, swig_type_info *type, int flags) { if (!ptr) { - Py_INCREF(Py_None); - return Py_None; + return SWIG_Py_Void(); } else if (!type) { if (!PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, "Swig: null type passed to NewPointerObj"); @@ -862,10 +860,8 @@ SWIGRUNTIME PyObject * SWIG_Python_NewPackedObj(void *ptr, size_t sz, swig_type_info *type) { PyObject *robj = 0; if (!ptr) { - Py_INCREF(Py_None); - return Py_None; + return SWIG_Py_Void(); } - robj = PySwigPacked_New((void *) ptr, sz, type); return robj; } diff --git a/Lib/python/pystrings.swg b/Lib/python/pystrings.swg index a90f333de..d55961497 100644 --- a/Lib/python/pystrings.swg +++ b/Lib/python/pystrings.swg @@ -66,8 +66,7 @@ SWIG_FromCharPtrAndSize(const char* carray, size_t size) return PyString_FromStringAndSize(carray, %numeric_cast(size,int)); } } else { - Py_INCREF(Py_None); - return Py_None; + return SWIG_Py_Void(); } } } diff --git a/Lib/python/pytypemaps.swg b/Lib/python/pytypemaps.swg index d378e8519..e58447a04 100644 --- a/Lib/python/pytypemaps.swg +++ b/Lib/python/pytypemaps.swg @@ -39,7 +39,7 @@ /* Python types */ #define SWIG_Object PyObject * -#define VOID_Object (Py_INCREF(Py_None) ? Py_None : 0) +#define VOID_Object SWIG_Py_Void() /* Overload of the output/constant/exception/dirout handling */ diff --git a/Lib/python/pywstrings.swg b/Lib/python/pywstrings.swg index 887774528..a9c8b4024 100644 --- a/Lib/python/pywstrings.swg +++ b/Lib/python/pywstrings.swg @@ -51,8 +51,7 @@ SWIG_FromWCharPtrAndSize(const wchar_t * carray, size_t size) return PyUnicode_FromWideChar(carray, %numeric_cast(size,int)); } } else { - Py_INCREF(Py_None); - return Py_None; + return SWIG_Py_Void(); } } } diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index a37af07d8..b2694608c 100644 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -237,7 +237,7 @@ public: } /* Set comparison with none for ConstructorToFunction */ - setSubclassInstanceCheck(NewString("$arg != Py_None")); + setSubclassInstanceCheck(NewString("$arg != SWIG_Py_None()")); /* Initialize all of the output files */ String *outfile = Getattr(n,"outfile"); @@ -1438,7 +1438,7 @@ public: /* if (constructor && (Getattr(n, "wrap:self") != 0)) { Wrapper_add_local(f, "subclassed", "int subclassed = 0"); - Printf(f->code, "subclassed = (arg1 != Py_None);\n"); + Printf(f->code, "subclassed = (arg1 != SWIG_Py_None());\n"); } */ @@ -2145,8 +2145,7 @@ public: tab4, "PyObject *obj;\n", tab4, "if (!PyArg_UnpackTuple(args,(char*)\"swigregister\", 1, 1,&obj)) return NULL;\n", tab4, "SWIG_TypeNewClientData(SWIGTYPE", SwigType_manglestr(ct),", SWIG_NewClientData(obj));\n", - tab4, "Py_INCREF(Py_None);\n", - tab4, "return Py_None;\n", + tab4, "return SWIG_Py_Void();\n", "}\n",NIL); String *cname = NewStringf("%s_swigregister", class_name); add_method(cname, cname, 0);