First cut. Works for wrapping OpenAccess, but there's plenty

left to do.

Currently, the test suite hurls at director_stl.



git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/szager-python-builtin@12332 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Stefan Zager 2010-12-07 21:50:00 +00:00
commit ee3a6623da
8 changed files with 1520 additions and 1040 deletions

28
Lib/python/builtin.swg Normal file
View file

@ -0,0 +1,28 @@
#ifdef __cplusplus
namespace {
template <typename _Tp> struct PySwigBuiltin : public SwigPyObject {
typedef PySwigBuiltin<_Tp> this_type;
typedef _Tp obj_type;
typedef obj_type* pointer;
typedef obj_type& reference;
static PyMethodDef methods[];
static PyTypeObject pytype;
static SwigPyClientData clientdata;
};
template <typename _Tp> void py_builtin_dealloc (PyObject *pyobj)
{
typedef PySwigBuiltin<_Tp> builtin_type;
builtin_type *obj = (builtin_type*) pyobj;
if (obj->own)
delete reinterpret_cast<_Tp*>(obj->ptr);
(*pyobj->ob_type->tp_free)(pyobj);
}
} // namespace {
#endif

View file

@ -580,6 +580,9 @@ namespace swig
%fragment("SwigPySequence_Cont");
# if defined(SWIGPYTHON_BUILTIN)
%feature("tp_iter") Sequence "&swig::make_output_iterator_builtin< Sequence >";
# else
%newobject iterator(PyObject **PYTHON_SELF);
%extend {
swig::SwigPyIterator* iterator(PyObject **PYTHON_SELF) {
@ -588,6 +591,8 @@ namespace swig
%pythoncode {def __iter__(self): return self.iterator()}
}
# endif // SWIGPYTHON_BUILTIN
#endif //SWIG_EXPORT_ITERATOR_METHODS
%enddef

View file

@ -320,7 +320,8 @@ SWIGEXPORT
void
#endif
SWIG_init(void) {
PyObject *m, *d;
PyObject *m, *d, *md;
PyTypeObject *builtin_type;
#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef SWIG_module = {
PyModuleDef_HEAD_INIT,
@ -335,6 +336,13 @@ SWIG_init(void) {
};
#endif
#if defined(SWIGPYTHON_BUILTIN)
PyTypeObject *builtin_pytype = 0;
swig_type_info *builtin_basetype = 0;
SWIG_Python_builtin_imports();
#endif
/* Fix SwigMethods to carry the callback ptrs when needed */
SWIG_Python_FixMethods(SwigMethods, swig_const_table, swig_types, swig_type_initial);
@ -343,7 +351,7 @@ SWIG_init(void) {
#else
m = Py_InitModule((char *) SWIG_name, SwigMethods);
#endif
d = PyModule_GetDict(m);
md = d = PyModule_GetDict(m);
SWIG_InitializeModule(0);
SWIG_InstallConstants(d,swig_const_table);

View file

@ -302,6 +302,24 @@ namespace swig {
{
return new SwigPyIteratorOpen_T<OutIter>(current, seq);
}
template <typename Sequence>
inline PyObject* make_output_iterator_builtin (PyObject *pyself)
{
SwigPyObject *builtin_obj = (SwigPyObject*) pyself;
Sequence *seq = reinterpret_cast< Sequence * >(builtin_obj->ptr);
if (!seq)
return SWIG_Py_Void();
SwigPyIterator *iter = make_output_iterator(seq->begin(), seq->begin(), seq->end(), pyself);
return SWIG_NewPointerObj(iter, SWIGTYPE_p_swig__SwigPyIterator, SWIG_POINTER_OWN);
}
template <>
inline PyObject* make_output_iterator_builtin<SwigPyIterator> (PyObject *pyself)
{
Py_INCREF(pyself);
return pyself;
}
}
}
@ -329,9 +347,14 @@ namespace swig
%newobject SwigPyIterator::operator - (ptrdiff_t n) const;
%nodirector SwigPyIterator;
#if defined(SWIGPYTHON_BUILTIN)
%feature("tp_iter") SwigPyIterator "&swig::make_output_iterator_builtin<swig::SwigPyIterator>";
#else
%extend SwigPyIterator {
%pythoncode {def __iter__(self): return self}
}
#endif
%catches(swig::stop_iteration) SwigPyIterator::value() const;
%catches(swig::stop_iteration) SwigPyIterator::incr(size_t n = 1);

View file

@ -244,6 +244,7 @@ typedef struct {
PyObject *destroy;
int delargs;
int implicitconv;
PyTypeObject *pytype;
} SwigPyClientData;
SWIGRUNTIMEINLINE int
@ -310,6 +311,7 @@ SwigPyClientData_New(PyObject* obj)
data->delargs = 0;
}
data->implicitconv = 0;
data->pytype = 0;
return data;
}
}
@ -1064,91 +1066,101 @@ 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) {
if (ptr) *ptr = 0;
return SWIG_OK;
} else {
if (!obj) return SWIG_ERROR;
if (obj == Py_None) {
if (ptr) *ptr = 0;
return SWIG_OK;
}
if (ty && ty->clientdata && ((SwigPyClientData*) ty->clientdata)->pytype) {
PyTypeObject *target_tp = ((SwigPyClientData*) ty->clientdata)->pytype;
PyTypeObject *obj_tp;
for (obj_tp = obj->ob_type; obj_tp; obj_tp = obj_tp->tp_base) {
if (obj_tp == target_tp) {
if (ptr)
*ptr = ((SwigPyObject*) obj)->ptr;
return SWIG_OK;
}
}
}
SwigPyObject *sobj = SWIG_Python_GetSwigThis(obj);
if (own)
*own = 0;
*own = 0;
while (sobj) {
void *vptr = sobj->ptr;
if (ty) {
swig_type_info *to = sobj->ty;
if (to == ty) {
/* no type cast needed */
if (ptr) *ptr = vptr;
break;
void *vptr = sobj->ptr;
if (ty) {
swig_type_info *to = sobj->ty;
if (to == ty) {
/* no type cast needed */
if (ptr) *ptr = vptr;
break;
} else {
swig_cast_info *tc = SWIG_TypeCheck(to->name,ty);
if (!tc) {
sobj = (SwigPyObject *)sobj->next;
} else {
if (ptr) {
int newmemory = 0;
*ptr = SWIG_TypeCast(tc,vptr,&newmemory);
if (newmemory == SWIG_CAST_NEW_MEMORY) {
assert(own); /* badly formed typemap which will lead to a memory leak - it must set and use own to delete *ptr */
if (own)
*own = *own | SWIG_CAST_NEW_MEMORY;
}
}
break;
}
}
} else {
swig_cast_info *tc = SWIG_TypeCheck(to->name,ty);
if (!tc) {
sobj = (SwigPyObject *)sobj->next;
} else {
if (ptr) {
int newmemory = 0;
*ptr = SWIG_TypeCast(tc,vptr,&newmemory);
if (newmemory == SWIG_CAST_NEW_MEMORY) {
assert(own); /* badly formed typemap which will lead to a memory leak - it must set and use own to delete *ptr */
if (own)
*own = *own | SWIG_CAST_NEW_MEMORY;
}
}
if (ptr) *ptr = vptr;
break;
}
}
} else {
if (ptr) *ptr = vptr;
break;
}
}
if (sobj) {
if (own)
*own = *own | sobj->own;
if (flags & SWIG_POINTER_DISOWN) {
sobj->own = 0;
}
return SWIG_OK;
} else {
int res = SWIG_ERROR;
if (flags & SWIG_POINTER_IMPLICIT_CONV) {
SwigPyClientData *data = ty ? (SwigPyClientData *) ty->clientdata : 0;
if (data && !data->implicitconv) {
PyObject *klass = data->klass;
if (klass) {
PyObject *impconv;
data->implicitconv = 1; /* avoid recursion and call 'explicit' constructors*/
impconv = SWIG_Python_CallFunctor(klass, obj);
data->implicitconv = 0;
if (PyErr_Occurred()) {
PyErr_Clear();
impconv = 0;
}
if (impconv) {
SwigPyObject *iobj = SWIG_Python_GetSwigThis(impconv);
if (iobj) {
void *vptr;
res = SWIG_Python_ConvertPtrAndOwn((PyObject*)iobj, &vptr, ty, 0, 0);
if (SWIG_IsOK(res)) {
if (ptr) {
*ptr = vptr;
/* transfer the ownership to 'ptr' */
iobj->own = 0;
res = SWIG_AddCast(res);
res = SWIG_AddNewMask(res);
} else {
res = SWIG_AddCast(res);
}
}
}
Py_DECREF(impconv);
}
}
if (own)
*own = *own | sobj->own;
if (flags & SWIG_POINTER_DISOWN) {
sobj->own = 0;
}
}
return res;
return SWIG_OK;
} else {
int res = SWIG_ERROR;
if (flags & SWIG_POINTER_IMPLICIT_CONV) {
SwigPyClientData *data = ty ? (SwigPyClientData *) ty->clientdata : 0;
if (data && !data->implicitconv) {
PyObject *klass = data->klass;
if (klass) {
PyObject *impconv;
data->implicitconv = 1; /* avoid recursion and call 'explicit' constructors*/
impconv = SWIG_Python_CallFunctor(klass, obj);
data->implicitconv = 0;
if (PyErr_Occurred()) {
PyErr_Clear();
impconv = 0;
}
if (impconv) {
SwigPyObject *iobj = SWIG_Python_GetSwigThis(impconv);
if (iobj) {
void *vptr;
res = SWIG_Python_ConvertPtrAndOwn((PyObject*)iobj, &vptr, ty, 0, 0);
if (SWIG_IsOK(res)) {
if (ptr) {
*ptr = vptr;
/* transfer the ownership to 'ptr' */
iobj->own = 0;
res = SWIG_AddCast(res);
res = SWIG_AddNewMask(res);
} else {
res = SWIG_AddCast(res);
}
}
}
Py_DECREF(impconv);
}
}
}
}
return res;
}
}
}
/* Convert a function ptr value */
@ -1319,21 +1331,32 @@ SWIG_Python_InitShadowInstance(PyObject *args) {
SWIGRUNTIME PyObject *
SWIG_Python_NewPointerObj(void *ptr, swig_type_info *type, int flags) {
if (!ptr) {
return SWIG_Py_Void();
} else {
int own = (flags & SWIG_POINTER_OWN) ? SWIG_POINTER_OWN : 0;
PyObject *robj = SwigPyObject_New(ptr, type, own);
if (!ptr)
return SWIG_Py_Void();
SwigPyClientData *clientdata = type ? (SwigPyClientData *)(type->clientdata) : 0;
int own = (flags & SWIG_POINTER_OWN) ? SWIG_POINTER_OWN : 0;
if (clientdata && clientdata->pytype) {
SwigPyObject *newobj = PyObject_New(SwigPyObject, clientdata->pytype);
if (newobj) {
newobj->ptr = ptr;
newobj->ty = type;
newobj->own = own;
newobj->next = 0;
return (PyObject*) newobj;
}
return SWIG_Py_Void();
}
PyObject *robj = SwigPyObject_New(ptr, type, own);
if (clientdata && !(flags & SWIG_POINTER_NOSHADOW)) {
PyObject *inst = SWIG_Python_NewShadowInstance(clientdata, robj);
if (inst) {
Py_DECREF(robj);
robj = inst;
}
PyObject *inst = SWIG_Python_NewShadowInstance(clientdata, robj);
if (inst) {
Py_DECREF(robj);
robj = inst;
}
}
return robj;
}
}
/* Create a new packed object */
@ -1343,6 +1366,19 @@ SWIG_Python_NewPackedObj(void *ptr, size_t sz, swig_type_info *type) {
return ptr ? SwigPyPacked_New((void *) ptr, sz, type) : SWIG_Py_Void();
}
SWIGRUNTIME int
SWIG_Python_NewBuiltinObj(PyObject *self, void *ptr, swig_type_info *type, int flags) {
assert(self);
SwigPyClientData *clientdata = (SwigPyClientData *)(type->clientdata);
assert(clientdata);
assert(clientdata->pytype);
int own = (flags & SWIG_POINTER_OWN) ? SWIG_POINTER_OWN : 0;
SwigPyObject *newobj = (SwigPyObject*) self;
newobj->ptr = ptr;
newobj->own = own;
return 0;
}
/* -----------------------------------------------------------------------------*
* Get type list
* -----------------------------------------------------------------------------*/

View file

@ -11,3 +11,4 @@
%insert(runtime) "pyapi.swg"; /* Python API */
%insert(runtime) "pyrun.swg"; /* Python run-time code */
%insert(runtime) "builtin.swg"; /* Specialization for classes with single inheritance */

View file

@ -96,3 +96,20 @@
}
/* -------------------------------------------------------------
* Output typemap for the __init__ method of a built-in type.
* ------------------------------------------------------------ */
/* Pointers, references */
%typemap(builtin_init,noblock=1) SWIGTYPE *, SWIGTYPE &, SWIGTYPE[] {
%set_output(SWIG_Python_NewBuiltinObj(self, %as_voidptr($1), $descriptor, $owner | %newpointer_flags));
}
%typemap(builtin_init, noblock=1) SWIGTYPE *const& {
%set_output(SWIG_Python_NewBuiltinObj(self, %as_voidptr(*$1), $*descriptor, $owner | %newpointer_flags));
}
/* Return by value */
%typemap(builtin_init, noblock=1) SWIGTYPE {
%set_output(SWIG_Python_NewBuiltinObj(self, %new_copy($1, $ltype), $&descriptor, SWIG_POINTER_OWN | %newpointer_flags));
}