Added sequence and mapping support for builtin types.

Fixed object ownership in %pyswigbinoperator.

Test suite now fails on li_std_vector_extra.



git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/szager-python-builtin@12346 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Stefan Zager 2010-12-14 21:11:02 +00:00
commit 8a7ad756d0
4 changed files with 212 additions and 82 deletions

View file

@ -1,16 +1,97 @@
extern int
pyswig_setter_closure (PyObject *obj, PyObject *val, void *closure)
{
if (!closure)
return -1;
PyObject *tuple = PyTuple_New(1);
assert(tuple);
PyTuple_SET_ITEM(tuple, 0, val);
Py_XINCREF(val);
PyObject *result = ((PyCFunction) closure)(obj, tuple);
Py_DECREF(tuple);
Py_XDECREF(result);
return result ? 0 : -1;
#define PYSWIG_BINARYFUNC_CLOSURE(wrapper) \
SWIGINTERN PyObject* \
wrapper##_closure (PyObject *a, PyObject *b) \
{ \
PyObject *tuple = PyTuple_New(1); \
assert(tuple); \
PyTuple_SET_ITEM(tuple, 0, b); \
Py_XINCREF(b); \
PyObject *result = wrapper(a, tuple); \
Py_DECREF(tuple); \
return result; \
}
#define PYSWIG_TERNARYFUNC_CLOSURE(wrapper) \
SWIGINTERN PyObject* \
wrapper##_closure (PyObject *a, PyObject *b, PyObject *c) \
{ \
PyObject *tuple = PyTuple_New(2); \
assert(tuple); \
PyTuple_SET_ITEM(tuple, 0, b); \
PyTuple_SET_ITEM(tuple, 1, c); \
Py_XINCREF(b); \
Py_XINCREF(c); \
PyObject *result = wrapper(a, tuple); \
Py_DECREF(tuple); \
return result; \
}
#define PYSWIG_LENFUNC_CLOSURE(wrapper) \
SWIGINTERN Py_ssize_t \
wrapper##_closure (PyObject *a) \
{ \
PyObject *resultobj = wrapper(a, NULL); \
Py_ssize_t result = _PyLong_AsSsize_t(resultobj); \
Py_DECREF(resultobj); \
return result; \
}
#define PYSWIG_SSIZESSIZEARGFUNC_CLOSURE(wrapper) \
SWIGINTERN PyObject* \
wrapper##_closure (PyObject *a, Py_ssize_t b, Py_ssize_t c) \
{ \
PyObject *tuple = PyTuple_New(2); \
assert(tuple); \
PyTuple_SET_ITEM(tuple, 0, _PyLong_FromSsize_t(b)); \
PyTuple_SET_ITEM(tuple, 1, _PyLong_FromSsize_t(c)); \
PyObject *result = wrapper(a, tuple); \
Py_DECREF(tuple); \
return result; \
}
#define PYSWIG_SSIZESSIZEOBJARGPROC_CLOSURE(wrapper) \
SWIGINTERN int \
wrapper##_closure (PyObject *a, Py_ssize_t b, Py_ssize_t c, PyObject *d) \
{ \
PyObject *tuple = PyTuple_New(3); \
assert(tuple); \
PyTuple_SET_ITEM(tuple, 0, _PyLong_FromSsize_t(b)); \
PyTuple_SET_ITEM(tuple, 1, _PyLong_FromSsize_t(c)); \
PyTuple_SET_ITEM(tuple, 2, d); \
Py_XINCREF(d); \
PyObject *resultobj = wrapper(a, tuple); \
int result = resultobj ? 0 : -1; \
Py_DECREF(tuple); \
Py_XDECREF(resultobj); \
return result; \
}
#define PYSWIG_SSIZEARGFUNC_CLOSURE(wrapper) \
SWIGINTERN PyObject* \
wrapper##_closure (PyObject *a, Py_ssize_t b) \
{ \
PyObject *tuple = PyTuple_New(1); \
assert(tuple); \
PyTuple_SET_ITEM(tuple, 0, _PyLong_FromSsize_t(b)); \
PyObject *result = wrapper(a, tuple); \
Py_DECREF(tuple); \
return result; \
}
#define PYSWIG_SSIZEOBJARGPROC_CLOSURE(wrapper) \
SWIGINTERN int \
wrapper##_closure (PyObject *a, Py_ssize_t b, PyObject *c) \
{ \
PyObject *tuple = PyTuple_New(2); \
assert(tuple); \
PyTuple_SET_ITEM(tuple, 0, _PyLong_FromSsize_t(b)); \
PyTuple_SET_ITEM(tuple, 1, c); \
Py_XINCREF(c); \
PyObject *resultobj = wrapper(a, tuple); \
int result = resultobj ? 0 : -1; \
Py_XDECREF(resultobj); \
Py_DECREF(tuple); \
return result; \
}
#ifdef __cplusplus
@ -27,6 +108,8 @@ template <typename _Tp> struct PySwigBuiltin : public SwigPyObject {
static PyMethodDef methods[];
static PyGetSetDef getset[];
static PyNumberMethods number_methods;
static PySequenceMethods sequence_methods;
static PyMappingMethods mapping_methods;
static PyTypeObject pytype;
static SwigPyClientData clientdata;
};
@ -40,27 +123,19 @@ template <typename _Tp> void py_builtin_dealloc (PyObject *pyobj)
(*pyobj->ob_type->tp_free)(pyobj);
}
template <PyCFunction func> PyObject*
pyswig_binaryfunc_closure (PyObject *a, PyObject *b)
SWIGINTERN int
pyswig_setter_closure (PyObject *obj, PyObject *val, void *closure)
{
if (!closure)
return -1;
PyObject *tuple = PyTuple_New(1);
assert(tuple);
PyTuple_SET_ITEM(tuple, 0, b);
PyObject *result = func(a, tuple);
PyTuple_SET_ITEM(tuple, 0, val);
Py_XINCREF(val);
PyObject *result = ((PyCFunction) closure)(obj, tuple);
Py_DECREF(tuple);
return result;
}
template <PyCFunction func> PyObject*
pyswig_ternaryfunc_closure (PyObject *a, PyObject *b, PyObject *c)
{
PyObject *tuple = PyTuple_New(2);
assert(tuple);
PyTuple_SET_ITEM(tuple, 0, b);
PyTuple_SET_ITEM(tuple, 1, c);
PyObject *result = func(a, tuple);
Py_DECREF(tuple);
return result;
Py_XDECREF(result);
return result ? 0 : -1;
}
} // namespace {

View file

@ -599,7 +599,6 @@ namespace swig
/**** The python container methods ****/
%define %swig_container_methods(Container...)
%newobject __getslice__;
@ -618,8 +617,16 @@ namespace swig
return self->size();
}
}
#if defined(SWIGPYTHON_BUILTIN)
%pybinoperator(__nonzero__, __nonzero__, unaryfunc, nb_nonzero);
%pybinoperator(__len__, __len__, lenfunc, sq_length);
#endif // SWIGPYTHON_BUILTIN
%enddef
%define %swig_sequence_methods_common(Sequence...)
%swig_sequence_iterator(%arg(Sequence))
%swig_container_methods(%arg(Sequence))
@ -697,6 +704,14 @@ namespace swig
}
}
#if defined(SWIGPYTHON_BUILTIN)
%pybinoperator(__getitem__, __getitem__, ssizeargfunc, sq_item);
%pybinoperator(__getslice__, __getslice__, ssizessizeargfunc, sq_slice);
%pybinoperator(__setitem__, __setitem__, ssizeobjargproc, sq_ass_item);
%pybinoperator(__setslice__, __setslice__, ssizessizeobjargproc, sq_ass_slice);
#endif // SWIGPYTHON_BUILTIN
%enddef
%define %swig_sequence_methods(Sequence...)
@ -714,6 +729,7 @@ namespace swig
self->push_back(x);
}
}
%enddef
%define %swig_sequence_methods_val(Sequence...)
@ -731,6 +747,7 @@ namespace swig
self->push_back(x);
}
}
%enddef

View file

@ -13,20 +13,20 @@
#define %pycompare(pyname,oper,comptype) %pybinoperator(pyname,oper,,comptype)
#endif
%pybinoperator(__add__, *::operator+, binary_func, nb_add);
%pybinoperator(__pos__, *::operator+(), unary_func, nb_positive);
%pybinoperator(__pos__, *::operator+() const, unary_func, nb_positive);
%pybinoperator(__sub__, *::operator-, binary_func, nb_subtract);
%pybinoperator(__neg__, *::operator-(), unary_func, nb_negative);
%pybinoperator(__neg__, *::operator-() const, unary_func, nb_negative);
%pybinoperator(__mul__, *::operator*, binary_func, nb_multiply);
%pybinoperator(__div__, *::operator/, binary_func, nb_div);
%pybinoperator(__mod__, *::operator%, binary_func, nb_remainder);
%pybinoperator(__lshift__, *::operator<<, binary_func, nb_lshift);
%pybinoperator(__rshift__, *::operator>>, binary_func, nb_rshift);
%pybinoperator(__and__, *::operator&, binary_func, nb_and);
%pybinoperator(__or__, *::operator|, binary_func, nb_or);
%pybinoperator(__xor__, *::operator^, binary_func, nb_xor);
%pybinoperator(__add__, *::operator+, binaryfunc, nb_add);
%pybinoperator(__pos__, *::operator+(), unaryfunc, nb_positive);
%pybinoperator(__pos__, *::operator+() const, unaryfunc, nb_positive);
%pybinoperator(__sub__, *::operator-, binaryfunc, nb_subtract);
%pybinoperator(__neg__, *::operator-(), unaryfunc, nb_negative);
%pybinoperator(__neg__, *::operator-() const, unaryfunc, nb_negative);
%pybinoperator(__mul__, *::operator*, binaryfunc, nb_multiply);
%pybinoperator(__div__, *::operator/, binaryfunc, nb_div);
%pybinoperator(__mod__, *::operator%, binaryfunc, nb_remainder);
%pybinoperator(__lshift__, *::operator<<, binaryfunc, nb_lshift);
%pybinoperator(__rshift__, *::operator>>, binaryfunc, nb_rshift);
%pybinoperator(__and__, *::operator&, binaryfunc, nb_and);
%pybinoperator(__or__, *::operator|, binaryfunc, nb_or);
%pybinoperator(__xor__, *::operator^, binaryfunc, nb_xor);
%pycompare(__lt__, *::operator<, Py_LT);
%pycompare(__le__, *::operator<=, Py_LE);
%pycompare(__gt__, *::operator>, Py_GT);
@ -34,16 +34,16 @@
%pycompare(__eq__, *::operator==, Py_EQ);
%pycompare(__ne__, *::operator!=, Py_NE);
%feature("pyslot", "nb_truediv", functype="binary_func") *::operator/;
%feature("pyslot", "nb_truediv", functype="binaryfunc") *::operator/;
/* Special cases */
%rename(__invert__) *::operator~;
%feature("pyslot", "nb_invert", functype="unary_func") *::operator~;
%feature("pyslot", "nb_invert", functype="unaryfunc") *::operator~;
%rename(__call__) *::operator();
%feature("pyslot", "tp_call", functype="ternary_func") *::operator();
%feature("pyslot", "tp_call", functype="ternaryfunc") *::operator();
#if defined(SWIGPYTHON_BUILTIN)
%pybinoperator(__nonzero__, *::operator bool, unary_func, nb_nonzero);
%pybinoperator(__nonzero__, *::operator bool, unaryfunc, nb_nonzero);
#else
%feature("shadow") *::operator bool %{
def __nonzero__(self):
@ -103,21 +103,21 @@ __bool__ = __nonzero__
*/
#if defined(SWIGPYTHON_BUILTIN)
#define %pyinplaceoper(SwigPyOper, Oper, functp, slot) %rename(SwigPyOper) Oper; %feature("pyslot", #slot, functype=#functp) Oper;
#define %pyinplaceoper(SwigPyOper, Oper, functp, slot) %delobject Oper; %newobject Oper; %feature("pyslot", #slot, functype=#functp) Oper; %rename(SwigPyOper) Oper
#else
#define %pyinplaceoper(SwigPyOper, Oper, functp, slot) %delobject Oper; %newobject Oper; %rename(SwigPyOper) Oper
#endif
%pyinplaceoper(__iadd__ , *::operator +=, binary_func, nb_inplace_add);
%pyinplaceoper(__isub__ , *::operator -=, binary_func, nb_inplace_subtract);
%pyinplaceoper(__imul__ , *::operator *=, binary_func, nb_inplace_multiply);
%pyinplaceoper(__idiv__ , *::operator /=, binary_func, nb_inplace_divide);
%pyinplaceoper(__imod__ , *::operator %=, binary_func, nb_inplace_remainder);
%pyinplaceoper(__iand__ , *::operator &=, binary_func, nb_inplace_and);
%pyinplaceoper(__ior__ , *::operator |=, binary_func, nb_inplace_or);
%pyinplaceoper(__ixor__ , *::operator ^=, binary_func, nb_inplace_xor);
%pyinplaceoper(__ilshift__, *::operator <<=, binary_func, nb_inplace_lshift);
%pyinplaceoper(__irshift__, *::operator >>=, binary_func, nb_inplace_rshift);
%pyinplaceoper(__iadd__ , *::operator +=, binaryfunc, nb_inplace_add);
%pyinplaceoper(__isub__ , *::operator -=, binaryfunc, nb_inplace_subtract);
%pyinplaceoper(__imul__ , *::operator *=, binaryfunc, nb_inplace_multiply);
%pyinplaceoper(__idiv__ , *::operator /=, binaryfunc, nb_inplace_divide);
%pyinplaceoper(__imod__ , *::operator %=, binaryfunc, nb_inplace_remainder);
%pyinplaceoper(__iand__ , *::operator &=, binaryfunc, nb_inplace_and);
%pyinplaceoper(__ior__ , *::operator |=, binaryfunc, nb_inplace_or);
%pyinplaceoper(__ixor__ , *::operator ^=, binaryfunc, nb_inplace_xor);
%pyinplaceoper(__ilshift__, *::operator <<=, binaryfunc, nb_inplace_lshift);
%pyinplaceoper(__irshift__, *::operator >>=, binaryfunc, nb_inplace_rshift);
/* Finally, in python we need to mark the binary operations to fail as