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

View file

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

View file

@ -302,6 +302,24 @@ namespace swig {
{ {
return new SwigPyIteratorOpen_T<OutIter>(current, seq); 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; %newobject SwigPyIterator::operator - (ptrdiff_t n) const;
%nodirector SwigPyIterator; %nodirector SwigPyIterator;
#if defined(SWIGPYTHON_BUILTIN)
%feature("tp_iter") SwigPyIterator "&swig::make_output_iterator_builtin<swig::SwigPyIterator>";
#else
%extend SwigPyIterator { %extend SwigPyIterator {
%pythoncode {def __iter__(self): return self} %pythoncode {def __iter__(self): return self}
} }
#endif
%catches(swig::stop_iteration) SwigPyIterator::value() const; %catches(swig::stop_iteration) SwigPyIterator::value() const;
%catches(swig::stop_iteration) SwigPyIterator::incr(size_t n = 1); %catches(swig::stop_iteration) SwigPyIterator::incr(size_t n = 1);

View file

@ -244,6 +244,7 @@ typedef struct {
PyObject *destroy; PyObject *destroy;
int delargs; int delargs;
int implicitconv; int implicitconv;
PyTypeObject *pytype;
} SwigPyClientData; } SwigPyClientData;
SWIGRUNTIMEINLINE int SWIGRUNTIMEINLINE int
@ -310,6 +311,7 @@ SwigPyClientData_New(PyObject* obj)
data->delargs = 0; data->delargs = 0;
} }
data->implicitconv = 0; data->implicitconv = 0;
data->pytype = 0;
return data; return data;
} }
} }
@ -1068,7 +1070,18 @@ SWIG_Python_ConvertPtrAndOwn(PyObject *obj, void **ptr, swig_type_info *ty, int
if (obj == Py_None) { if (obj == Py_None) {
if (ptr) *ptr = 0; if (ptr) *ptr = 0;
return SWIG_OK; return SWIG_OK;
} else { }
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); SwigPyObject *sobj = SWIG_Python_GetSwigThis(obj);
if (own) if (own)
*own = 0; *own = 0;
@ -1149,7 +1162,6 @@ SWIG_Python_ConvertPtrAndOwn(PyObject *obj, void **ptr, swig_type_info *ty, int
return res; return res;
} }
} }
}
/* Convert a function ptr value */ /* Convert a function ptr value */
@ -1319,12 +1331,24 @@ SWIG_Python_InitShadowInstance(PyObject *args) {
SWIGRUNTIME PyObject * SWIGRUNTIME PyObject *
SWIG_Python_NewPointerObj(void *ptr, swig_type_info *type, int flags) { SWIG_Python_NewPointerObj(void *ptr, swig_type_info *type, int flags) {
if (!ptr) { if (!ptr)
return SWIG_Py_Void(); return SWIG_Py_Void();
} else {
int own = (flags & SWIG_POINTER_OWN) ? SWIG_POINTER_OWN : 0;
PyObject *robj = SwigPyObject_New(ptr, type, own);
SwigPyClientData *clientdata = type ? (SwigPyClientData *)(type->clientdata) : 0; 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)) { if (clientdata && !(flags & SWIG_POINTER_NOSHADOW)) {
PyObject *inst = SWIG_Python_NewShadowInstance(clientdata, robj); PyObject *inst = SWIG_Python_NewShadowInstance(clientdata, robj);
if (inst) { if (inst) {
@ -1334,7 +1358,6 @@ SWIG_Python_NewPointerObj(void *ptr, swig_type_info *type, int flags) {
} }
return robj; return robj;
} }
}
/* Create a new packed object */ /* 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(); 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 * Get type list
* -----------------------------------------------------------------------------*/ * -----------------------------------------------------------------------------*/

View file

@ -11,3 +11,4 @@
%insert(runtime) "pyapi.swg"; /* Python API */ %insert(runtime) "pyapi.swg"; /* Python API */
%insert(runtime) "pyrun.swg"; /* Python run-time code */ %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));
}

File diff suppressed because it is too large Load diff