Added support for operator overrides (PyNumberMethods) and
member variable access. test suite now croaks on inplaceadd. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/szager-python-builtin@12345 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
399ae62561
commit
b1682d4d80
4 changed files with 1933 additions and 1755 deletions
|
|
@ -1,3 +1,18 @@
|
|||
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;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
namespace {
|
||||
|
|
@ -10,6 +25,8 @@ template <typename _Tp> struct PySwigBuiltin : public SwigPyObject {
|
|||
typedef obj_type& reference;
|
||||
|
||||
static PyMethodDef methods[];
|
||||
static PyGetSetDef getset[];
|
||||
static PyNumberMethods number_methods;
|
||||
static PyTypeObject pytype;
|
||||
static SwigPyClientData clientdata;
|
||||
};
|
||||
|
|
@ -23,6 +40,29 @@ 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)
|
||||
{
|
||||
PyObject *tuple = PyTuple_New(1);
|
||||
assert(tuple);
|
||||
PyTuple_SET_ITEM(tuple, 0, b);
|
||||
PyObject *result = func(a, 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;
|
||||
}
|
||||
|
||||
} // namespace {
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -5,41 +5,53 @@
|
|||
|
||||
#ifdef __cplusplus
|
||||
|
||||
#define %pybinoperator(pyname,oper) %rename(pyname) oper; %pythonmaybecall oper
|
||||
|
||||
%pybinoperator(__add__, *::operator+);
|
||||
%pybinoperator(__pos__, *::operator+());
|
||||
%pybinoperator(__pos__, *::operator+() const);
|
||||
%pybinoperator(__sub__, *::operator-);
|
||||
%pybinoperator(__neg__, *::operator-());
|
||||
%pybinoperator(__neg__, *::operator-() const);
|
||||
%pybinoperator(__mul__, *::operator*);
|
||||
%pybinoperator(__div__, *::operator/);
|
||||
%pybinoperator(__mod__, *::operator%);
|
||||
%pybinoperator(__lshift__, *::operator<<);
|
||||
%pybinoperator(__rshift__, *::operator>>);
|
||||
%pybinoperator(__and__, *::operator&);
|
||||
%pybinoperator(__or__, *::operator|);
|
||||
%pybinoperator(__xor__, *::operator^);
|
||||
%pybinoperator(__lt__, *::operator<);
|
||||
%pybinoperator(__le__, *::operator<=);
|
||||
%pybinoperator(__gt__, *::operator>);
|
||||
%pybinoperator(__ge__, *::operator>=);
|
||||
%pybinoperator(__eq__, *::operator==);
|
||||
%pybinoperator(__ne__, *::operator!=);
|
||||
#if defined(SWIGPYTHON_BUILTIN)
|
||||
#define %pybinoperator(pyname,oper,functp,slot) %rename(pyname) oper; %pythonmaybecall oper; %feature("pyslot", #slot, functype=#functp) oper;
|
||||
#define %pycompare(pyname,oper,comptype) %rename(pyname) oper; %pythonmaybecall oper; %feature("pycompare", #comptype) oper;
|
||||
#else
|
||||
#define %pybinoperator(pyname,oper,functp,slot) %rename(pyname) oper; %pythonmaybecall oper
|
||||
#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);
|
||||
%pycompare(__lt__, *::operator<, Py_LT);
|
||||
%pycompare(__le__, *::operator<=, Py_LE);
|
||||
%pycompare(__gt__, *::operator>, Py_GT);
|
||||
%pycompare(__ge__, *::operator>=, Py_GE);
|
||||
%pycompare(__eq__, *::operator==, Py_EQ);
|
||||
%pycompare(__ne__, *::operator!=, Py_NE);
|
||||
|
||||
%feature("pyslot", "nb_truediv", functype="binary_func") *::operator/;
|
||||
|
||||
/* Special cases */
|
||||
%rename(__invert__) *::operator~;
|
||||
%feature("pyslot", "nb_invert", functype="unary_func") *::operator~;
|
||||
%rename(__call__) *::operator();
|
||||
%feature("pyslot", "tp_call", functype="ternary_func") *::operator();
|
||||
|
||||
#if defined(SWIGPYTHON_BUILTIN)
|
||||
%pybinoperator(__nonzero__, *::operator bool, unary_func, nb_nonzero);
|
||||
#else
|
||||
%feature("shadow") *::operator bool %{
|
||||
def __nonzero__(self):
|
||||
return $action(self)
|
||||
__bool__ = __nonzero__
|
||||
%};
|
||||
%rename(__nonzero__) *::operator bool;
|
||||
#endif
|
||||
|
||||
/* Ignored operators */
|
||||
%ignoreoperator(LNOT) operator!;
|
||||
|
|
@ -90,18 +102,22 @@ __bool__ = __nonzero__
|
|||
|
||||
*/
|
||||
|
||||
#define %pyinplaceoper(SwigPyOper, Oper) %delobject Oper; %newobject Oper; %rename(SwigPyOper) Oper
|
||||
#if defined(SWIGPYTHON_BUILTIN)
|
||||
#define %pyinplaceoper(SwigPyOper, Oper, functp, slot) %rename(SwigPyOper) Oper; %feature("pyslot", #slot, functype=#functp) Oper;
|
||||
#else
|
||||
#define %pyinplaceoper(SwigPyOper, Oper, functp, slot) %delobject Oper; %newobject Oper; %rename(SwigPyOper) Oper
|
||||
#endif
|
||||
|
||||
%pyinplaceoper(__iadd__ , *::operator +=);
|
||||
%pyinplaceoper(__isub__ , *::operator -=);
|
||||
%pyinplaceoper(__imul__ , *::operator *=);
|
||||
%pyinplaceoper(__idiv__ , *::operator /=);
|
||||
%pyinplaceoper(__imod__ , *::operator %=);
|
||||
%pyinplaceoper(__iand__ , *::operator &=);
|
||||
%pyinplaceoper(__ior__ , *::operator |=);
|
||||
%pyinplaceoper(__ixor__ , *::operator ^=);
|
||||
%pyinplaceoper(__ilshift__, *::operator <<=);
|
||||
%pyinplaceoper(__irshift__, *::operator >>=);
|
||||
%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);
|
||||
|
||||
|
||||
/* Finally, in python we need to mark the binary operations to fail as
|
||||
|
|
|
|||
|
|
@ -1076,8 +1076,13 @@ SWIG_Python_ConvertPtrAndOwn(PyObject *obj, void **ptr, swig_type_info *ty, int
|
|||
PyTypeObject *obj_tp;
|
||||
for (obj_tp = obj->ob_type; obj_tp; obj_tp = obj_tp->tp_base) {
|
||||
if (obj_tp == target_tp) {
|
||||
SwigPyObject *sobj = (SwigPyObject*) obj;
|
||||
if (own)
|
||||
*own = *own | sobj->own;
|
||||
if (flags & SWIG_POINTER_DISOWN)
|
||||
sobj->own = 0;
|
||||
if (ptr)
|
||||
*ptr = ((SwigPyObject*) obj)->ptr;
|
||||
*ptr = sobj->ptr;
|
||||
return SWIG_OK;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue