diff --git a/Lib/python/pyinit.swg b/Lib/python/pyinit.swg new file mode 100644 index 000000000..3c7375556 --- /dev/null +++ b/Lib/python/pyinit.swg @@ -0,0 +1,26 @@ +/* ------------------------------------------------------------ + * The start of the Python initialization function + * ------------------------------------------------------------ */ + +%init %{ +#ifdef __cplusplus +extern "C" +#endif +SWIGEXPORT(void) SWIG_init(void) { + static PyObject *SWIG_globals = 0; + static int typeinit = 0; + PyObject *m, *d; + int i; + if (!SWIG_globals) SWIG_globals = SWIG_newvarlink(); + m = Py_InitModule((char *) SWIG_name, SwigMethods); + d = PyModule_GetDict(m); + + if (!typeinit) { + for (i = 0; swig_types_initial[i]; i++) { + swig_types[i] = SWIG_TypeRegister(swig_types_initial[i]); + } + typeinit = 1; + } + SWIG_InstallConstants(d,swig_const_table); +%} + diff --git a/Lib/python/pyrun.swg b/Lib/python/pyrun.swg index f525fecae..446542e24 100644 --- a/Lib/python/pyrun.swg +++ b/Lib/python/pyrun.swg @@ -26,6 +26,10 @@ extern "C" { /* Exception handling in wrappers */ #define SWIG_fail goto fail +#ifndef SWIG_NO_COBJECT_TYPES +#define SWIG_COBJECT_TYPES +#endif + /* Constant information structure */ typedef struct swig_const_info { int type; @@ -65,8 +69,8 @@ SWIGIMPORT(PyObject *) SWIG_Python_NewPointerObj(void *, swig_type_info * SWIGIMPORT(void *) SWIG_Python_MustGetPtr(PyObject *, swig_type_info *, int, int); SWIGIMPORT(PyObject *) SWIG_Python_newvarlink(void); SWIGIMPORT(void) SWIG_Python_addvarlink(PyObject *, char *, PyObject *(*)(void), int (*)(PyObject *)); -SWIGIMPORT(int) SWIG_Python_ConvertPacked(PyObject *, void *, int sz, swig_type_info *, int); -SWIGIMPORT(PyObject *) SWIG_Python_NewPackedObj(void *, int sz, swig_type_info *); +SWIGIMPORT(int) SWIG_Python_ConvertPacked(PyObject *, void *, size_t sz, swig_type_info *, int); +SWIGIMPORT(PyObject *) SWIG_Python_NewPackedObj(void *, size_t sz, swig_type_info *); SWIGIMPORT(void) SWIG_Python_InstallConstants(PyObject *d, swig_const_info constants[]); @@ -303,8 +307,8 @@ type_error: if (flags & SWIG_POINTER_EXCEPTION) { if (ty && c) { PyErr_Format(PyExc_TypeError, - "Type error. Got %s, expected %s", - c, ty->name); + "Type error. Got '%s', expected '%s'", + c, SWIG_TypePrettyName(ty)); } else { PyErr_SetString(PyExc_TypeError,"Expected a pointer"); } @@ -316,13 +320,20 @@ type_error: SWIGRUNTIME(void *) SWIG_Python_MustGetPtr(PyObject *obj, swig_type_info *ty, int argnum, int flags) { void *result; - SWIG_Python_ConvertPtr(obj, &result, ty, flags | SWIG_POINTER_EXCEPTION); + if (SWIG_Python_ConvertPtr(obj, &result, ty, flags) == -1) { + if (flags & SWIG_POINTER_EXCEPTION) { + PyErr_Clear(); + PyErr_Format(PyExc_TypeError, + "Type error in argument %i: expected a pointer of type '%s'.", + argnum, SWIG_TypePrettyName(ty)); + } + } return result; } /* Convert a packed value value */ SWIGRUNTIME(int) -SWIG_Python_ConvertPacked(PyObject *obj, void *ptr, int sz, swig_type_info *ty, int flags) { +SWIG_Python_ConvertPacked(PyObject *obj, void *ptr, size_t sz, swig_type_info *ty, int flags) { swig_type_info *tc; char *c = 0; @@ -343,8 +354,8 @@ type_error: if (flags) { if (ty && c) { PyErr_Format(PyExc_TypeError, - "Type error. Got %s, expected %s", - c, ty->name); + "Type error. Got '%s', expected '%s'", + c, SWIG_TypePrettyName(ty)); } else { PyErr_SetString(PyExc_TypeError,"Expected a pointer"); } @@ -390,7 +401,7 @@ SWIG_Python_NewPointerObj(void *ptr, swig_type_info *type, int own) { } SWIGRUNTIME(PyObject *) -SWIG_Python_NewPackedObj(void *ptr, int sz, swig_type_info *type) { +SWIG_Python_NewPackedObj(void *ptr, size_t sz, swig_type_info *type) { char result[1024]; char *r = result; if ((2*sz + 1 + strlen(type->name)) > 1000) return 0; diff --git a/Lib/python/pyruntime.swg b/Lib/python/pyruntime.swg new file mode 100644 index 000000000..aa79e20b6 --- /dev/null +++ b/Lib/python/pyruntime.swg @@ -0,0 +1,9 @@ +/* Python.h has to appear first */ + +%insert(runtime) %{ +#include +%} + +%insert(runtime) "precommon.swg"; +%insert(runtime) "common.swg"; /* Common type-checking code */ +%insert(runtime) "pyrun.swg"; /* Python run-time code */ diff --git a/Lib/python/pyswigtype.swg b/Lib/python/pyswigtype.swg index 53e2c88ec..4377fbbe5 100644 --- a/Lib/python/pyswigtype.swg +++ b/Lib/python/pyswigtype.swg @@ -4,19 +4,18 @@ /* Pointers, references, and arrays */ -%typemap(in) SWIGTYPE *, SWIGTYPE [] - "if ((SWIG_ConvertPtr($input,(void **)(&$1),$1_descriptor, - SWIG_POINTER_EXCEPTION | $disown)) == -1) SWIG_fail;"; +%typemap(in) SWIGTYPE *, SWIGTYPE [] + "$1 = ($1_ltype)SWIG_MustGetPtr($input, $descriptor, $argnum, SWIG_POINTER_EXCEPTION | $disown); + if (PyErr_Occurred()) SWIG_fail;"; %typemap(in) SWIGTYPE *DISOWN - "if ((SWIG_ConvertPtr($input,(void **)(&$1),$1_descriptor, - SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN)) == -1) SWIG_fail;"; + "$1 = ($1_ltype)SWIG_MustGetPtr($input, $descriptor, $argnum, SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN); + if (PyErr_Occurred()) SWIG_fail;"; /* Additional check for null references */ %typemap(in) SWIGTYPE & - "if ((SWIG_ConvertPtr($input,(void **)(&$1),$1_descriptor, - SWIG_POINTER_EXCEPTION | $disown)) == -1) - SWIG_fail; + "$1 = ($1_ltype)SWIG_MustGetPtr($input, $descriptor, $argnum, SWIG_POINTER_EXCEPTION | $disown); + if (PyErr_Occurred()) SWIG_fail; if ($1 == NULL) { PyErr_SetString(PyExc_TypeError,\"null reference\"); SWIG_fail; diff --git a/Lib/python/python.swg b/Lib/python/python.swg index 1bd8091bc..437893525 100644 --- a/Lib/python/python.swg +++ b/Lib/python/python.swg @@ -4,41 +4,30 @@ * Python configuration module. * ----------------------------------------------------------------------------- */ -/* Python.h has to appear first */ +/* ----------------------------------------------------------------------------- + * The runtime part + * ----------------------------------------------------------------------------- */ +%include -%insert(runtime) %{ -#include -%} - -%insert(runtime) "precommon.swg"; -%insert(runtime) "common.swg"; /* Common type-checking code */ -%insert(runtime) "pyrun.swg"; /* Python run-time code */ - -/* Special directive for shadow code */ - -#define %shadow %insert("shadow") -#define %pythoncode %insert("python") +/* ----------------------------------------------------------------------------- + * Special user directives + * ----------------------------------------------------------------------------- */ +%include +/* ----------------------------------------------------------------------------- + * Inner macros (ugly ones) + * ----------------------------------------------------------------------------- */ %include - /* ----------------------------------------------------------------------------- * Look for user fragments file. If not found, include empty system one. * ----------------------------------------------------------------------------- */ - %include "pyfragments.swg" - -/* ----------------------------------------------------------------------------- - * SWIGTYPE typemaps - * ----------------------------------------------------------------------------- */ - -%include - /* ----------------------------------------------------------------------------- * Typemap specializations * ----------------------------------------------------------------------------- */ - +%include %include %include %include @@ -61,28 +50,6 @@ %include /* ------------------------------------------------------------ - * The start of the Python initialization function + * The Python initialization function * ------------------------------------------------------------ */ - -%init %{ -#ifdef __cplusplus -extern "C" -#endif -SWIGEXPORT(void) SWIG_init(void) { - static PyObject *SWIG_globals = 0; - static int typeinit = 0; - PyObject *m, *d; - int i; - if (!SWIG_globals) SWIG_globals = SWIG_newvarlink(); - m = Py_InitModule((char *) SWIG_name, SwigMethods); - d = PyModule_GetDict(m); - - if (!typeinit) { - for (i = 0; swig_types_initial[i]; i++) { - swig_types[i] = SWIG_TypeRegister(swig_types_initial[i]); - } - typeinit = 1; - } - SWIG_InstallConstants(d,swig_const_table); -%} - +%include diff --git a/Lib/python/pyuserdir.swg b/Lib/python/pyuserdir.swg new file mode 100644 index 000000000..aa3eb40ee --- /dev/null +++ b/Lib/python/pyuserdir.swg @@ -0,0 +1,56 @@ +/* ----------------------------------------------------------------------------- + * Special user directives + * ----------------------------------------------------------------------------- */ + +/* shadow code */ +#define %shadow %insert("shadow") +#define %pythoncode %insert("python") + + +/* +Use the "nondynamic" feature to make a wrapped class behaves as a "nondynamic" +one, ie, a python class that doesn't dynamically add new attributes. + +For example, for the class + +%pythonnondynamic(1) A; +struct A +{ + int a; + int b; +}; + +you will get: + + aa = A() + aa.a = 1 # Ok + aa.b = 1 # Ok + aa.c = 3 # error + +Since "nondynamic" is a feature, if you use it like + +%pythonnondynamic(1); + +it will make all the wrapped classes nondynamic ones. + +The implementation is based on the recipe: + + http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252158 + +and works for modern (-modern) and plain python. We don't use __slots__, +so, it works with old python versions. + +You can also use the raw %feature form + +%feature("pythonnondynamic") A; + +or the inverse form + +%pythondynamic(0) A; + + +*/ + + +#define %pythonnondynamic(FLAG) %feature("python:nondynamic", #FLAG) +#define %pythondynamic(FLAG) %pythonnondynamic(!FLAG)