cleaning + comments, and add the nondynamic feature handlers
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@6266 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
1ed8f4209a
commit
d7b22b7d90
6 changed files with 132 additions and 64 deletions
26
SWIG/Lib/python/pyinit.swg
Normal file
26
SWIG/Lib/python/pyinit.swg
Normal file
|
|
@ -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);
|
||||||
|
%}
|
||||||
|
|
||||||
|
|
@ -26,6 +26,10 @@ extern "C" {
|
||||||
/* Exception handling in wrappers */
|
/* Exception handling in wrappers */
|
||||||
#define SWIG_fail goto fail
|
#define SWIG_fail goto fail
|
||||||
|
|
||||||
|
#ifndef SWIG_NO_COBJECT_TYPES
|
||||||
|
#define SWIG_COBJECT_TYPES
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Constant information structure */
|
/* Constant information structure */
|
||||||
typedef struct swig_const_info {
|
typedef struct swig_const_info {
|
||||||
int type;
|
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(void *) SWIG_Python_MustGetPtr(PyObject *, swig_type_info *, int, int);
|
||||||
SWIGIMPORT(PyObject *) SWIG_Python_newvarlink(void);
|
SWIGIMPORT(PyObject *) SWIG_Python_newvarlink(void);
|
||||||
SWIGIMPORT(void) SWIG_Python_addvarlink(PyObject *, char *, PyObject *(*)(void), int (*)(PyObject *));
|
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(int) SWIG_Python_ConvertPacked(PyObject *, void *, size_t sz, swig_type_info *, int);
|
||||||
SWIGIMPORT(PyObject *) SWIG_Python_NewPackedObj(void *, int sz, swig_type_info *);
|
SWIGIMPORT(PyObject *) SWIG_Python_NewPackedObj(void *, size_t sz, swig_type_info *);
|
||||||
SWIGIMPORT(void) SWIG_Python_InstallConstants(PyObject *d, swig_const_info constants[]);
|
SWIGIMPORT(void) SWIG_Python_InstallConstants(PyObject *d, swig_const_info constants[]);
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -303,8 +307,8 @@ type_error:
|
||||||
if (flags & SWIG_POINTER_EXCEPTION) {
|
if (flags & SWIG_POINTER_EXCEPTION) {
|
||||||
if (ty && c) {
|
if (ty && c) {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"Type error. Got %s, expected %s",
|
"Type error. Got '%s', expected '%s'",
|
||||||
c, ty->name);
|
c, SWIG_TypePrettyName(ty));
|
||||||
} else {
|
} else {
|
||||||
PyErr_SetString(PyExc_TypeError,"Expected a pointer");
|
PyErr_SetString(PyExc_TypeError,"Expected a pointer");
|
||||||
}
|
}
|
||||||
|
|
@ -316,13 +320,20 @@ type_error:
|
||||||
SWIGRUNTIME(void *)
|
SWIGRUNTIME(void *)
|
||||||
SWIG_Python_MustGetPtr(PyObject *obj, swig_type_info *ty, int argnum, int flags) {
|
SWIG_Python_MustGetPtr(PyObject *obj, swig_type_info *ty, int argnum, int flags) {
|
||||||
void *result;
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Convert a packed value value */
|
/* Convert a packed value value */
|
||||||
SWIGRUNTIME(int)
|
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;
|
swig_type_info *tc;
|
||||||
char *c = 0;
|
char *c = 0;
|
||||||
|
|
||||||
|
|
@ -343,8 +354,8 @@ type_error:
|
||||||
if (flags) {
|
if (flags) {
|
||||||
if (ty && c) {
|
if (ty && c) {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"Type error. Got %s, expected %s",
|
"Type error. Got '%s', expected '%s'",
|
||||||
c, ty->name);
|
c, SWIG_TypePrettyName(ty));
|
||||||
} else {
|
} else {
|
||||||
PyErr_SetString(PyExc_TypeError,"Expected a pointer");
|
PyErr_SetString(PyExc_TypeError,"Expected a pointer");
|
||||||
}
|
}
|
||||||
|
|
@ -390,7 +401,7 @@ SWIG_Python_NewPointerObj(void *ptr, swig_type_info *type, int own) {
|
||||||
}
|
}
|
||||||
|
|
||||||
SWIGRUNTIME(PyObject *)
|
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 result[1024];
|
||||||
char *r = result;
|
char *r = result;
|
||||||
if ((2*sz + 1 + strlen(type->name)) > 1000) return 0;
|
if ((2*sz + 1 + strlen(type->name)) > 1000) return 0;
|
||||||
|
|
|
||||||
9
SWIG/Lib/python/pyruntime.swg
Normal file
9
SWIG/Lib/python/pyruntime.swg
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
/* Python.h has to appear first */
|
||||||
|
|
||||||
|
%insert(runtime) %{
|
||||||
|
#include <Python.h>
|
||||||
|
%}
|
||||||
|
|
||||||
|
%insert(runtime) "precommon.swg";
|
||||||
|
%insert(runtime) "common.swg"; /* Common type-checking code */
|
||||||
|
%insert(runtime) "pyrun.swg"; /* Python run-time code */
|
||||||
|
|
@ -4,19 +4,18 @@
|
||||||
|
|
||||||
/* Pointers, references, and arrays */
|
/* Pointers, references, and arrays */
|
||||||
|
|
||||||
%typemap(in) SWIGTYPE *, SWIGTYPE []
|
%typemap(in) SWIGTYPE *, SWIGTYPE []
|
||||||
"if ((SWIG_ConvertPtr($input,(void **)(&$1),$1_descriptor,
|
"$1 = ($1_ltype)SWIG_MustGetPtr($input, $descriptor, $argnum, SWIG_POINTER_EXCEPTION | $disown);
|
||||||
SWIG_POINTER_EXCEPTION | $disown)) == -1) SWIG_fail;";
|
if (PyErr_Occurred()) SWIG_fail;";
|
||||||
|
|
||||||
%typemap(in) SWIGTYPE *DISOWN
|
%typemap(in) SWIGTYPE *DISOWN
|
||||||
"if ((SWIG_ConvertPtr($input,(void **)(&$1),$1_descriptor,
|
"$1 = ($1_ltype)SWIG_MustGetPtr($input, $descriptor, $argnum, SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN);
|
||||||
SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN)) == -1) SWIG_fail;";
|
if (PyErr_Occurred()) SWIG_fail;";
|
||||||
|
|
||||||
/* Additional check for null references */
|
/* Additional check for null references */
|
||||||
%typemap(in) SWIGTYPE &
|
%typemap(in) SWIGTYPE &
|
||||||
"if ((SWIG_ConvertPtr($input,(void **)(&$1),$1_descriptor,
|
"$1 = ($1_ltype)SWIG_MustGetPtr($input, $descriptor, $argnum, SWIG_POINTER_EXCEPTION | $disown);
|
||||||
SWIG_POINTER_EXCEPTION | $disown)) == -1)
|
if (PyErr_Occurred()) SWIG_fail;
|
||||||
SWIG_fail;
|
|
||||||
if ($1 == NULL) {
|
if ($1 == NULL) {
|
||||||
PyErr_SetString(PyExc_TypeError,\"null reference\");
|
PyErr_SetString(PyExc_TypeError,\"null reference\");
|
||||||
SWIG_fail;
|
SWIG_fail;
|
||||||
|
|
|
||||||
|
|
@ -4,41 +4,30 @@
|
||||||
* Python configuration module.
|
* Python configuration module.
|
||||||
* ----------------------------------------------------------------------------- */
|
* ----------------------------------------------------------------------------- */
|
||||||
|
|
||||||
/* Python.h has to appear first */
|
/* -----------------------------------------------------------------------------
|
||||||
|
* The runtime part
|
||||||
|
* ----------------------------------------------------------------------------- */
|
||||||
|
%include <pyruntime.swg>
|
||||||
|
|
||||||
%insert(runtime) %{
|
/* -----------------------------------------------------------------------------
|
||||||
#include <Python.h>
|
* Special user directives
|
||||||
%}
|
* ----------------------------------------------------------------------------- */
|
||||||
|
%include <pyuserdir.swg>
|
||||||
%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")
|
|
||||||
|
|
||||||
|
/* -----------------------------------------------------------------------------
|
||||||
|
* Inner macros (ugly ones)
|
||||||
|
* ----------------------------------------------------------------------------- */
|
||||||
%include <pymacros.swg>
|
%include <pymacros.swg>
|
||||||
|
|
||||||
|
|
||||||
/* -----------------------------------------------------------------------------
|
/* -----------------------------------------------------------------------------
|
||||||
* Look for user fragments file. If not found, include empty system one.
|
* Look for user fragments file. If not found, include empty system one.
|
||||||
* ----------------------------------------------------------------------------- */
|
* ----------------------------------------------------------------------------- */
|
||||||
|
|
||||||
%include "pyfragments.swg"
|
%include "pyfragments.swg"
|
||||||
|
|
||||||
|
|
||||||
/* -----------------------------------------------------------------------------
|
|
||||||
* SWIGTYPE typemaps
|
|
||||||
* ----------------------------------------------------------------------------- */
|
|
||||||
|
|
||||||
%include <pyswigtype.swg>
|
|
||||||
|
|
||||||
/* -----------------------------------------------------------------------------
|
/* -----------------------------------------------------------------------------
|
||||||
* Typemap specializations
|
* Typemap specializations
|
||||||
* ----------------------------------------------------------------------------- */
|
* ----------------------------------------------------------------------------- */
|
||||||
|
%include <pyswigtype.swg>
|
||||||
%include <pyinout.swg>
|
%include <pyinout.swg>
|
||||||
%include <pyvoid.swg>
|
%include <pyvoid.swg>
|
||||||
%include <pyobject.swg>
|
%include <pyobject.swg>
|
||||||
|
|
@ -61,28 +50,6 @@
|
||||||
%include <pythonkw.swg>
|
%include <pythonkw.swg>
|
||||||
|
|
||||||
/* ------------------------------------------------------------
|
/* ------------------------------------------------------------
|
||||||
* The start of the Python initialization function
|
* The Python initialization function
|
||||||
* ------------------------------------------------------------ */
|
* ------------------------------------------------------------ */
|
||||||
|
%include <pyinit.swg>
|
||||||
%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);
|
|
||||||
%}
|
|
||||||
|
|
||||||
|
|
|
||||||
56
SWIG/Lib/python/pyuserdir.swg
Normal file
56
SWIG/Lib/python/pyuserdir.swg
Normal file
|
|
@ -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)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue