mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
Matching Python 2 int behavior on Python 2 (#1186)
Pybind11's default conversion to int always produces a long on Python 2 (`int`s and `long`s were unified in Python 3). This patch fixes `int` handling to match Python 2 on Python 2; for short types (`size_t` or smaller), the number will be returned as an `int` if possible, otherwise `long`. Requires Python 2.5+. This is needed for things like `sys.exit`, which refuse to accept a `long`.
This commit is contained in:
committed by
Jason Rhinelander
parent
3b265787f2
commit
cf0d0f9d5a
@@ -155,4 +155,9 @@ TEST_SUBMODULE(builtin_casters, m) {
|
||||
// test_complex
|
||||
m.def("complex_cast", [](float x) { return "{}"_s.format(x); });
|
||||
m.def("complex_cast", [](std::complex<float> x) { return "({}, {})"_s.format(x.real(), x.imag()); });
|
||||
|
||||
// test int vs. long (Python 2)
|
||||
m.def("int_cast", []() {return (int) 42;});
|
||||
m.def("long_cast", []() {return (long) 42;});
|
||||
m.def("longlong_cast", []() {return ULLONG_MAX;});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user