diff options
author | Michael Thon <m7thon@users.noreply.github.com> | 2016-06-12 23:29:31 +0200 |
---|---|---|
committer | Michael Thon <m7thon@users.noreply.github.com> | 2016-06-12 23:29:31 +0200 |
commit | 59f72544fafda209fb49e08992bae702da82ee7c (patch) | |
tree | 782ae7fdd5c08017700211aab0609b9a23144ee7 /Lib/python/pyopers.swg | |
parent | 84b06fa21b61c96eb7da49d73edb66e17de10821 (diff) | |
download | swig-59f72544fafda209fb49e08992bae702da82ee7c.tar.gz |
[Python] improved wrapping of division operators
The division operators `operator /` and `operator /=` are now
wrapped as `__truediv__` and `__itruediv__`, with alias methods
`__div__` and `__idiv__` generated for compatibility with Python 2.
This allows correct wrapping independent of the `-py3` flag, and
fixes these operators for Python 2 when using `from __future__
import division` (this was broken).
Diffstat (limited to 'Lib/python/pyopers.swg')
-rw-r--r-- | Lib/python/pyopers.swg | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/Lib/python/pyopers.swg b/Lib/python/pyopers.swg index 292c593a8..6bdc4ee01 100644 --- a/Lib/python/pyopers.swg +++ b/Lib/python/pyopers.swg @@ -103,11 +103,6 @@ %pybinoperator(__neg__, *::operator-(), unaryfunc, nb_negative); %pybinoperator(__neg__, *::operator-() const, unaryfunc, nb_negative); %pybinoperator(__mul__, *::operator*, binaryfunc, nb_multiply); -#if defined(SWIGPYTHON_PY3) -%pybinoperator(__truediv__, *::operator/, binaryfunc, nb_divide); -#else -%pybinoperator(__div__, *::operator/, binaryfunc, nb_divide); -#endif %pybinoperator(__mod__, *::operator%, binaryfunc, nb_remainder); %pybinoperator(__lshift__, *::operator<<, binaryfunc, nb_lshift); %pybinoperator(__rshift__, *::operator>>, binaryfunc, nb_rshift); @@ -129,6 +124,7 @@ #if defined(SWIGPYTHON_BUILTIN) %pybinoperator(__nonzero__, *::operator bool, inquiry, nb_nonzero); +%pybinoperator(__truediv__, *::operator/ , binaryfunc, nb_divide); #else %feature("shadow") *::operator bool %{ def __nonzero__(self): @@ -136,6 +132,13 @@ def __nonzero__(self): __bool__ = __nonzero__ %}; %rename(__nonzero__) *::operator bool; +%feature("shadow") *::operator/ %{ +def __truediv__(self, *args): + return $action(self, *args) +__div__ = __truediv__ +%}; +%rename(__truediv__) *::operator/; +%pythonmaybecall *::operator/; #endif /* Ignored operators */ @@ -202,11 +205,6 @@ __bool__ = __nonzero__ %pyinplaceoper(__iadd__ , *::operator +=, binaryfunc, nb_inplace_add); %pyinplaceoper(__isub__ , *::operator -=, binaryfunc, nb_inplace_subtract); %pyinplaceoper(__imul__ , *::operator *=, binaryfunc, nb_inplace_multiply); -#if defined(SWIGPYTHON_PY3) -%pyinplaceoper(__itruediv__ , *::operator /=, binaryfunc, nb_inplace_divide); -#else -%pyinplaceoper(__idiv__ , *::operator /=, binaryfunc, nb_inplace_divide); -#endif %pyinplaceoper(__imod__ , *::operator %=, binaryfunc, nb_inplace_remainder); %pyinplaceoper(__iand__ , *::operator &=, binaryfunc, nb_inplace_and); %pyinplaceoper(__ior__ , *::operator |=, binaryfunc, nb_inplace_or); @@ -214,6 +212,19 @@ __bool__ = __nonzero__ %pyinplaceoper(__ilshift__, *::operator <<=, binaryfunc, nb_inplace_lshift); %pyinplaceoper(__irshift__, *::operator >>=, binaryfunc, nb_inplace_rshift); +/* Special cases */ +#if defined(SWIGPYTHON_BUILTIN) +%pyinplaceoper(__itruediv__ , *::operator /=, binaryfunc, nb_inplace_divide); +#else +%delobject *::operator /=; +%newobject *::operator /=; +%feature("shadow") *::operator /= %{ +def __itruediv__(self, *args): + return $action(self, *args) +__idiv__ = __itruediv__ +%}; +%rename(__itruediv__) *::operator /=; +#endif /* Finally, in python we need to mark the binary operations to fail as 'maybecall' methods */ @@ -228,6 +239,7 @@ __bool__ = __nonzero__ %pybinopermaybecall(neg); %pybinopermaybecall(mul); %pybinopermaybecall(div); +%pybinopermaybecall(truediv); %pybinopermaybecall(mod); %pybinopermaybecall(lshift); %pybinopermaybecall(rshift); |