Skip to content

Commit 6c0ef26

Browse files
committed
Prevent ambiguous overload resolution
1 parent 511b937 commit 6c0ef26

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

.appveyor.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ install:
2222
- conda update -q conda
2323
- conda info -a
2424
- conda install gtest cmake -c conda-forge
25-
- conda install pytest numpy pybind11==2.2.4 xtensor==0.20.8 -c conda-forge
25+
- conda install pytest numpy pybind11==2.4.2 xtensor==0.20.8 -c conda-forge
2626
- "set PYTHONHOME=%MINICONDA%"
2727
- cmake -G "NMake Makefiles" -D CMAKE_INSTALL_PREFIX=%MINICONDA%\\Library -D BUILD_TESTS=ON -D PYTHON_EXECUTABLE=%MINICONDA%\\python.exe .
2828
- nmake test_xtensor_python

include/xtensor-python/pycontainer.hpp

+29
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,35 @@ namespace xt
124124

125125
PyArrayObject* python_array() const;
126126
size_type get_buffer_size() const;
127+
128+
private:
129+
130+
#if PYBIND11_VERSION_MAJOR == 2 && PYBIND11_VERSION_MINOR >= 3
131+
// Prevent ambiguous overload resolution for operators defined for
132+
// both xt::xcontainer and pybind11::object.
133+
using pybind11::object::operator~;
134+
using pybind11::object::operator+;
135+
using pybind11::object::operator-;
136+
using pybind11::object::operator*;
137+
using pybind11::object::operator/;
138+
using pybind11::object::operator|;
139+
using pybind11::object::operator&;
140+
using pybind11::object::operator^;
141+
using pybind11::object::operator<<;
142+
using pybind11::object::operator>>;
143+
144+
// Prevent ambiguous overload resolution for operators defined for
145+
// both xt::xcontainer_semantic and pybind11::object.
146+
using pybind11::object::operator+=;
147+
using pybind11::object::operator-=;
148+
using pybind11::object::operator*=;
149+
using pybind11::object::operator/=;
150+
using pybind11::object::operator|=;
151+
using pybind11::object::operator&=;
152+
using pybind11::object::operator^=;
153+
using pybind11::object::operator<<=;
154+
using pybind11::object::operator>>=;
155+
#endif
127156
};
128157

129158
namespace detail

test/test_pytensor.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,26 @@ namespace xt
240240
auto v = xt::view(arr, xt::all());
241241
EXPECT_EQ(v(0), 0.);
242242
}
243+
244+
TEST(pytensor, unary)
245+
{
246+
pytensor<int, 1> a = { 1, 2, 3 };
247+
pytensor<int, 1> res = -a;
248+
pytensor<int, 1> ref = { -1, -2, -3 };
249+
EXPECT_EQ(ref(0), res(0));
250+
EXPECT_EQ(ref(1), res(1));
251+
EXPECT_EQ(ref(1), res(1));
252+
}
253+
254+
TEST(pytensor, inplace_pybind11_overload)
255+
{
256+
// pybind11 overrrides a number of operators in pybind11::object.
257+
// This is testing that the right overload is picked up.
258+
pytensor<double, 1> a = { 1.0, 2.0, 3.0 };
259+
a /= 2;
260+
pytensor<double, 1> ref = { 0.5, 1.0, 1.5 };
261+
EXPECT_EQ(ref(0), a(0));
262+
EXPECT_EQ(ref(1), a(1));
263+
EXPECT_EQ(ref(1), a(1));
264+
}
243265
}

0 commit comments

Comments
 (0)