diff --git a/Lib/python/pyopers.swg b/Lib/python/pyopers.swg index e9999e9c6..0317e7968 100644 --- a/Lib/python/pyopers.swg +++ b/Lib/python/pyopers.swg @@ -4,6 +4,7 @@ #ifdef __cplusplus + %define %pybinoperator(pyname,oper) %rename(pyname) oper; %pythonmaybecall oper; @@ -30,31 +31,6 @@ %pybinoperator(__eq__, *::operator==); %pybinoperator(__ne__, *::operator!=); -%define %pybinoperation(oper) -%pythonmaybecall __ ## oper ## __; -%pythonmaybecall __r ## oper ## __; -%enddef - -%pybinoperation(add); -%pybinoperation(pos); -%pybinoperation(pos); -%pybinoperation(sub); -%pybinoperation(neg); -%pybinoperation(neg); -%pybinoperation(mul); -%pybinoperation(div); -%pybinoperation(mod); -%pybinoperation(lshift); -%pybinoperation(rshift); -%pybinoperation(and); -%pybinoperation(or); -%pybinoperation(xor); -%pybinoperation(lt); -%pybinoperation(le); -%pybinoperation(gt); -%pybinoperation(ge); -%pybinoperation(eq); -%pybinoperation(ne); /* Special cases */ @@ -63,19 +39,14 @@ /* Ignored operators */ %ignorewarn("361:operator! ignored") operator!; -%ignorewarn("361:operator not ignored") operator not; %ignorewarn("381:operator&& ignored") operator&&; -%ignorewarn("381:operator and ignored") operator and; %ignorewarn("382:operator|| ignored") operator||; -%ignorewarn("382:operator or ignored") operator or; %ignorewarn("362:operator= ignored") *::operator=; %ignorewarn("383:operator++ ignored") *::operator++; %ignorewarn("384:operator-- ignored") *::operator--; %ignorewarn("386:operator->* ignored") *::operator->*; %ignorewarn("389:operator[] ignored (consider using %extend)") *::operator[]; - - /* Inplace operator declarations. @@ -115,21 +86,70 @@ */ -%define %swig_inplace_oper(Oper, PyOper) -%feature("self:disown") *::Oper; -%feature("new") *::Oper; -%rename(__##PyOper##__) *::Oper; +%define %pyinplaceoper(PyOper, Oper) +%feature("self:disown") Oper; +%feature("new") Oper; +%rename(PyOper) Oper; %enddef -%swig_inplace_oper(operator +=, iadd); -%swig_inplace_oper(operator -=, isub); -%swig_inplace_oper(operator *=, imul); -%swig_inplace_oper(operator /=, idiv); -%swig_inplace_oper(operator %=, imod); -%swig_inplace_oper(operator &=, iand); -%swig_inplace_oper(operator |=, ior); -%swig_inplace_oper(operator ^=, ixor); -%swig_inplace_oper(operator <<=, ilshift); -%swig_inplace_oper(operator >>=, irshift); +%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 >>=); + + + +/* C++ operator aliases */ + +%ignorewarn("381:operator and ignored") operator and; // `and' `&&' +%ignorewarn("361:operator not ignored") operator not; // `not' `!' +%ignorewarn("382:operator or ignored") operator or; // `or' `||' +%pyinplaceoper(__iand__, *::operator and_eq); // `and_eq' `&=' +%pybinoperator(__and__, *::operator bitand) // `bitand' `&' +%pybinoperator(__or__, *::operator bitor); // `bitor' `|' +%rename(__invert__) *::operator compl; // `compl' `~' +%pybinoperator(__ne__, *::operator not_eq); // `not_eq' `!=' +%pyinplaceoper(__ior__, *::operator or_eq); // `or_eq' `|=' +%pybinoperator(__xor__, *::operator xor); // `xor' `^' +%pyinplaceoper(__ixor__, *::operator xor_eq); // `xor_eq' `^=' + + +/* Finally, in python we need to mark the binary operations to fail as + 'maybecall' methods */ + +%define %pybinopermaybecall(oper) +%pythonmaybecall __ ## oper ## __; +%pythonmaybecall __r ## oper ## __; +%enddef + +%pybinopermaybecall(add); +%pybinopermaybecall(pos); +%pybinopermaybecall(pos); +%pybinopermaybecall(sub); +%pybinopermaybecall(neg); +%pybinopermaybecall(neg); +%pybinopermaybecall(mul); +%pybinopermaybecall(div); +%pybinopermaybecall(mod); +%pybinopermaybecall(lshift); +%pybinopermaybecall(rshift); +%pybinopermaybecall(and); +%pybinopermaybecall(or); +%pybinopermaybecall(xor); +%pybinopermaybecall(lt); +%pybinopermaybecall(le); +%pybinopermaybecall(gt); +%pybinopermaybecall(ge); +%pybinopermaybecall(eq); +%pybinopermaybecall(ne); #endif + + + diff --git a/Source/CParse/cscanner.c b/Source/CParse/cscanner.c index e18fc3c4a..b5af761af 100644 --- a/Source/CParse/cscanner.c +++ b/Source/CParse/cscanner.c @@ -1240,8 +1240,16 @@ int yylex(void) { || (strcmp(t,"new[]") == 0) || (strcmp(t,"delete[]") == 0) || (strcmp(t,"and") == 0) - || (strcmp(t,"or") == 0) + || (strcmp(t,"and_eq") == 0) + || (strcmp(t,"bitand") == 0) + || (strcmp(t,"bitor") == 0) + || (strcmp(t,"compl") == 0) || (strcmp(t,"not") == 0) + || (strcmp(t,"not_eq") == 0) + || (strcmp(t,"or") == 0) + || (strcmp(t,"or_eq") == 0) + || (strcmp(t,"xor") == 0) + || (strcmp(t,"xor_eq") == 0) )) { /* retract(strlen(t));*/ retract(count);