Skip to content

Only allow integer type_caster to call __int__ method when conversion is allowed; always call __index__ #2698

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

Merged
merged 11 commits into from
Jan 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1020,13 +1020,23 @@ struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value && !is_std_char_t
if (!src)
return false;

#if !defined(PYPY_VERSION)
auto index_check = [](PyObject *o) { return PyIndex_Check(o); };
#else
// In PyPy 7.3.3, `PyIndex_Check` is implemented by calling `__index__`,
// while CPython only considers the existence of `nb_index`/`__index__`.
auto index_check = [](PyObject *o) { return hasattr(o, "__index__"); };
#endif

if (std::is_floating_point<T>::value) {
if (convert || PyFloat_Check(src.ptr()))
py_value = (py_type) PyFloat_AsDouble(src.ptr());
else
return false;
} else if (PyFloat_Check(src.ptr())) {
return false;
} else if (!convert && !index_check(src.ptr()) && !PYBIND11_LONG_CHECK(src.ptr())) {
return false;
} else if (std::is_unsigned<py_type>::value) {
py_value = as_unsigned<py_type>(src.ptr());
} else { // signed integer:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_builtin_casters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ TEST_SUBMODULE(builtin_casters, m) {
m.def("i64_str", [](std::int64_t v) { return std::to_string(v); });
m.def("u64_str", [](std::uint64_t v) { return std::to_string(v); });

// test_int_convert
m.def("int_passthrough", [](int arg) { return arg; });
m.def("int_passthrough_noconvert", [](int arg) { return arg; }, py::arg{}.noconvert());

// test_tuple
m.def("pair_passthrough", [](std::pair<bool, std::string> input) {
return std::make_pair(input.second, input.first);
Expand Down
62 changes: 62 additions & 0 deletions tests/test_builtin_casters.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,68 @@ def test_integer_casting():
assert "incompatible function arguments" in str(excinfo.value)


def test_int_convert():
class DeepThought(object):
def __int__(self):
return 42

class ShallowThought(object):
pass

class FuzzyThought(object):
def __float__(self):
return 41.99999

class IndexedThought(object):
def __index__(self):
return 42

class RaisingThought(object):
def __index__(self):
raise ValueError

def __int__(self):
return 42

convert, noconvert = m.int_passthrough, m.int_passthrough_noconvert

def require_implicit(v):
pytest.raises(TypeError, noconvert, v)

def cant_convert(v):
pytest.raises(TypeError, convert, v)

assert convert(7) == 7
assert noconvert(7) == 7
cant_convert(3.14159)
assert convert(DeepThought()) == 42
require_implicit(DeepThought())
cant_convert(ShallowThought())
cant_convert(FuzzyThought())
if env.PY >= (3, 8):
# Before Python 3.8, `int(obj)` does not pick up on `obj.__index__`
assert convert(IndexedThought()) == 42
assert noconvert(IndexedThought()) == 42
cant_convert(RaisingThought()) # no fall-back to `__int__`if `__index__` raises


def test_numpy_int_convert():
np = pytest.importorskip("numpy")

convert, noconvert = m.int_passthrough, m.int_passthrough_noconvert

def require_implicit(v):
pytest.raises(TypeError, noconvert, v)

# `np.intc` is an alias that corresponds to a C++ `int`
assert convert(np.intc(42)) == 42
assert noconvert(np.intc(42)) == 42

# The implicit conversion from np.float32 is undesirable but currently accepted.
assert convert(np.float32(3.14159)) == 3
require_implicit(np.float32(3.14159))


def test_tuple(doc):
"""std::pair <-> tuple & std::tuple <-> tuple"""
assert m.pair_passthrough((True, "test")) == ("test", True)
Expand Down