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`.
diff --git a/tests/test_builtin_casters.cpp b/tests/test_builtin_casters.cpp
index e5413c2..4508134 100644
--- a/tests/test_builtin_casters.cpp
+++ b/tests/test_builtin_casters.cpp
@@ -155,4 +155,9 @@
// 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;});
}