Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operator fixes #867

Merged
merged 3 commits into from
May 21, 2017
Merged

Operator fixes #867

merged 3 commits into from
May 21, 2017

Conversation

jagerman
Copy link
Member

This fixes a couple issues in operators.h.

The first is that /= isn't being defined properly under Python 3 (__idiv__ is set rather than __itruediv__) so it doesn't get called. Instead Python converts x /= 3 to x = x / 3 and ends up create a new object via __truediv__, which probably works in some cases, but isn't correct.

The second issue (third commit) is that the __truediv__ (and variants) are needed under Python 2 with from __future__ import division, otherwise division will fail (it does not fall back to calling an available __div__). The fix here adds a second binding under the alternate names under Python 2.

The Python method for /= was set as `__idiv__`, which should be
`__itruediv__` under Python 3.

This wasn't totally broken in that without it defined, Python constructs
a new object by calling __truediv__.  The operator tests, however,
didn't actually test the /= operator: when I added it, I saw an extra
construction, leading to the problem.  This commit also includes tests
for the previously untested *= operator, and adds some element-wise
vector multiplication and division operators.
- realign \ at end of macro lines
- use 'using A = B;' rather than 'typedef B A;'
- use conditional_t
@dean0x7d
Copy link
Member

LGTM!

Python 2 requires both `__div__` and `__truediv__` (and variants) for
compatibility with both regular Python 2 and Python 2 under `from
__future__ import division`.  Without both, division fails in one or the
other case.
@jagerman jagerman merged commit 35998a0 into pybind:master May 21, 2017
@dean0x7d dean0x7d modified the milestone: v2.2 Aug 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants