Skip to content

Commit f814b6d

Browse files
committed
Adding cleanup from @jagerman
1 parent 70305cd commit f814b6d

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

include/pybind11/cast.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -981,18 +981,11 @@ struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value && !is_std_char_t
981981
if (std::is_floating_point<T>::value) {
982982
return PyFloat_FromDouble((double) src);
983983
} else if (sizeof(T) <= sizeof(ssize_t)) {
984-
#if PY_VERSION_HEX < 0x03000000
985984
// This returns a long automatically if needed
986985
if (std::is_signed<T>::value)
987-
return PyInt_FromSsize_t((ssize_t) src);
986+
return PYBIND11_LONG_FROM_SIGNED(src);
988987
else
989-
return PyInt_FromSize_t((size_t) src);
990-
#else
991-
if (std::is_signed<T>::value)
992-
return PyLong_FromSsize_t((ssize_t) src);
993-
else
994-
return PyLong_FromSize_t((size_t) src);
995-
#endif
988+
return PYBIND11_LONG_FROM_UNSIGNED(src);
996989
} else {
997990
if (std::is_signed<T>::value)
998991
return PyLong_FromLongLong((long long) src);

include/pybind11/detail/common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@
158158
#define PYBIND11_BYTES_SIZE PyBytes_Size
159159
#define PYBIND11_LONG_CHECK(o) PyLong_Check(o)
160160
#define PYBIND11_LONG_AS_LONGLONG(o) PyLong_AsLongLong(o)
161+
#define PYBIND11_LONG_FROM_SIGNED(o) PyLong_FromSsize_t((ssize_t) o)
162+
#define PYBIND11_LONG_FROM_UNSIGNED(o) PyLong_FromSize_t((size_t) o)
161163
#define PYBIND11_BYTES_NAME "bytes"
162164
#define PYBIND11_STRING_NAME "str"
163165
#define PYBIND11_SLICE_OBJECT PyObject
@@ -180,6 +182,8 @@
180182
#define PYBIND11_BYTES_SIZE PyString_Size
181183
#define PYBIND11_LONG_CHECK(o) (PyInt_Check(o) || PyLong_Check(o))
182184
#define PYBIND11_LONG_AS_LONGLONG(o) (PyInt_Check(o) ? (long long) PyLong_AsLong(o) : PyLong_AsLongLong(o))
185+
#define PYBIND11_LONG_FROM_SIGNED(o) PyInt_FromSsize_t((ssize_t) o) // Returns long if needed.
186+
#define PYBIND11_LONG_FROM_UNSIGNED(o) PyInt_FromSize_t((size_t) o) // Returns long if needed.
183187
#define PYBIND11_BYTES_NAME "str"
184188
#define PYBIND11_STRING_NAME "unicode"
185189
#define PYBIND11_SLICE_OBJECT PySliceObject

tests/test_builtin_casters.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,11 @@ def test_numpy_bool():
326326

327327

328328
def test_int_long():
329-
"""In Python 2, a C++ int should return a Python int if possible, not long."""
329+
"""In Python 2, a C++ int should return a Python int rather than long
330+
if possible: longs are not always accepted where ints are used (such
331+
as the argument to sys.exit()). A C++ long long is always a Python
332+
long."""
333+
330334
import sys
331335
must_be_long = type(getattr(sys, 'maxint', 1) + 1)
332336
isinstance(m.int_cast(), int)

0 commit comments

Comments
 (0)