Add missing %python:maybecall to operator overloads.

This ensures NotImplemented is returned on error so that the Python
interpreter will handle the operators correctly instead of throwing an
exception. NotImplemented was not being returned for non-builtin wrappers
when the operator overload did not have a function overload.

See PEP 207 and https://docs.python.org/3/library/constants.html#NotImplemented

Mentioned in SF patch #303 and SF bug #1208.
This commit is contained in:
William S Fulton 2017-06-19 19:12:23 +01:00
commit 687cf9c9c1
4 changed files with 98 additions and 6 deletions

View file

@ -1894,6 +1894,14 @@ Also, be aware that certain operators don't map cleanly to Python. For instance
overloaded assignment operators don't map to Python semantics and will be ignored.
</p>
<p>
Operator overloading is implemented in the <tt>pyopers.swg</tt> library file.
In particular overloaded operators are marked with the <tt>python:maybecall</tt> feature, also known as <tt>%pythonmaybecall</tt>.
This feature forces SWIG to generate code that return an instance of Python's <tt>NotImplemented</tt>
instead of raising an exception when the comparison fails, that is, on any kind of error.
This follows the guidelines in <a href="https://www.python.org/dev/peps/pep-0207/">PEP 207 - Rich Comparisons</a> and <a href="https://docs.python.org/3/library/constants.html#NotImplemented">NotImplemented Python constant</a>.
</p>
<H3><a name="Python_nn25">36.3.12 C++ namespaces</a></H3>