@@ -661,15 +661,17 @@ struct generic_item {
661
661
struct sequence_item {
662
662
using key_type = size_t ;
663
663
664
- static object get (handle obj, size_t index) {
665
- PyObject *result = PySequence_GetItem (obj.ptr (), static_cast <ssize_t >(index ));
664
+ template <typename IdxType, detail::enable_if_t <std::is_integral<IdxType>::value, int > = 0 >
665
+ static object get (handle obj, const IdxType &index) {
666
+ PyObject *result = PySequence_GetItem (obj.ptr (), ssize_t_cast (index ));
666
667
if (!result) { throw error_already_set (); }
667
668
return reinterpret_steal<object>(result);
668
669
}
669
670
670
- static void set (handle obj, size_t index, handle val) {
671
+ template <typename IdxType, detail::enable_if_t <std::is_integral<IdxType>::value, int > = 0 >
672
+ static void set (handle obj, const IdxType &index, handle val) {
671
673
// PySequence_SetItem does not steal a reference to 'val'
672
- if (PySequence_SetItem (obj.ptr (), static_cast < ssize_t > (index ), val.ptr ()) != 0 ) {
674
+ if (PySequence_SetItem (obj.ptr (), ssize_t_cast (index ), val.ptr ()) != 0 ) {
673
675
throw error_already_set ();
674
676
}
675
677
}
@@ -678,15 +680,17 @@ struct sequence_item {
678
680
struct list_item {
679
681
using key_type = size_t ;
680
682
681
- static object get (handle obj, size_t index) {
682
- PyObject *result = PyList_GetItem (obj.ptr (), static_cast <ssize_t >(index ));
683
+ template <typename IdxType, detail::enable_if_t <std::is_integral<IdxType>::value, int > = 0 >
684
+ static object get (handle obj, const IdxType &index) {
685
+ PyObject *result = PyList_GetItem (obj.ptr (), ssize_t_cast (index ));
683
686
if (!result) { throw error_already_set (); }
684
687
return reinterpret_borrow<object>(result);
685
688
}
686
689
687
- static void set (handle obj, size_t index, handle val) {
690
+ template <typename IdxType, detail::enable_if_t <std::is_integral<IdxType>::value, int > = 0 >
691
+ static void set (handle obj, const IdxType &index, handle val) {
688
692
// PyList_SetItem steals a reference to 'val'
689
- if (PyList_SetItem (obj.ptr (), static_cast < ssize_t > (index ), val.inc_ref ().ptr ()) != 0 ) {
693
+ if (PyList_SetItem (obj.ptr (), ssize_t_cast (index ), val.inc_ref ().ptr ()) != 0 ) {
690
694
throw error_already_set ();
691
695
}
692
696
}
@@ -695,15 +699,17 @@ struct list_item {
695
699
struct tuple_item {
696
700
using key_type = size_t ;
697
701
698
- static object get (handle obj, size_t index) {
699
- PyObject *result = PyTuple_GetItem (obj.ptr (), static_cast <ssize_t >(index ));
702
+ template <typename IdxType, detail::enable_if_t <std::is_integral<IdxType>::value, int > = 0 >
703
+ static object get (handle obj, const IdxType &index) {
704
+ PyObject *result = PyTuple_GetItem (obj.ptr (), ssize_t_cast (index ));
700
705
if (!result) { throw error_already_set (); }
701
706
return reinterpret_borrow<object>(result);
702
707
}
703
708
704
- static void set (handle obj, size_t index, handle val) {
709
+ template <typename IdxType, detail::enable_if_t <std::is_integral<IdxType>::value, int > = 0 >
710
+ static void set (handle obj, const IdxType &index, handle val) {
705
711
// PyTuple_SetItem steals a reference to 'val'
706
- if (PyTuple_SetItem (obj.ptr (), static_cast < ssize_t > (index ), val.inc_ref ().ptr ()) != 0 ) {
712
+ if (PyTuple_SetItem (obj.ptr (), ssize_t_cast (index ), val.inc_ref ().ptr ()) != 0 ) {
707
713
throw error_already_set ();
708
714
}
709
715
}
@@ -1043,8 +1049,9 @@ class str : public object {
1043
1049
public:
1044
1050
PYBIND11_OBJECT_CVT (str, object, PYBIND11_STR_CHECK_FUN, raw_str)
1045
1051
1046
- str (const char *c, size_t n)
1047
- : object(PyUnicode_FromStringAndSize(c, (ssize_t ) n), stolen_t {}) {
1052
+ template <typename SzType, detail::enable_if_t <std::is_integral<SzType>::value, int > = 0 >
1053
+ str (const char *c, const SzType &n)
1054
+ : object(PyUnicode_FromStringAndSize(c, ssize_t_cast(n)), stolen_t {}) {
1048
1055
if (!m_ptr) pybind11_fail (" Could not allocate string object!" );
1049
1056
}
1050
1057
@@ -1116,8 +1123,9 @@ class bytes : public object {
1116
1123
if (!m_ptr) pybind11_fail (" Could not allocate bytes object!" );
1117
1124
}
1118
1125
1119
- bytes (const char *c, size_t n)
1120
- : object(PYBIND11_BYTES_FROM_STRING_AND_SIZE(c, (ssize_t ) n), stolen_t {}) {
1126
+ template <typename SzType, detail::enable_if_t <std::is_integral<SzType>::value, int > = 0 >
1127
+ bytes (const char *c, const SzType &n)
1128
+ : object(PYBIND11_BYTES_FROM_STRING_AND_SIZE(c, ssize_t_cast(n)), stolen_t {}) {
1121
1129
if (!m_ptr) pybind11_fail (" Could not allocate bytes object!" );
1122
1130
}
1123
1131
@@ -1160,7 +1168,7 @@ inline str::str(const bytes& b) {
1160
1168
ssize_t length = 0 ;
1161
1169
if (PYBIND11_BYTES_AS_STRING_AND_SIZE (b.ptr (), &buffer, &length))
1162
1170
pybind11_fail (" Unable to extract bytes contents!" );
1163
- auto obj = reinterpret_steal<object>(PyUnicode_FromStringAndSize (buffer, ( ssize_t ) length));
1171
+ auto obj = reinterpret_steal<object>(PyUnicode_FromStringAndSize (buffer, length));
1164
1172
if (!obj)
1165
1173
pybind11_fail (" Could not allocate string object!" );
1166
1174
m_ptr = obj.release ().ptr ();
@@ -1172,8 +1180,9 @@ class bytearray : public object {
1172
1180
public:
1173
1181
PYBIND11_OBJECT_CVT (bytearray, object, PyByteArray_Check, PyByteArray_FromObject)
1174
1182
1175
- bytearray (const char *c, size_t n)
1176
- : object(PyByteArray_FromStringAndSize(c, (ssize_t ) n), stolen_t {}) {
1183
+ template <typename SzType, detail::enable_if_t <std::is_integral<SzType>::value, int > = 0 >
1184
+ bytearray (const char *c, const SzType &n)
1185
+ : object(PyByteArray_FromStringAndSize(c, ssize_t_cast(n)), stolen_t {}) {
1177
1186
if (!m_ptr) pybind11_fail (" Could not allocate bytearray object!" );
1178
1187
}
1179
1188
@@ -1398,7 +1407,10 @@ class capsule : public object {
1398
1407
class tuple : public object {
1399
1408
public:
1400
1409
PYBIND11_OBJECT_CVT (tuple, object, PyTuple_Check, PySequence_Tuple)
1401
- explicit tuple (size_t size = 0 ) : object(PyTuple_New((ssize_t ) size), stolen_t{}) {
1410
+ template <typename SzType = ssize_t ,
1411
+ detail::enable_if_t <std::is_integral<SzType>::value, int > = 0 >
1412
+ // Some compilers generate link errors when using `const SzType &` here:
1413
+ explicit tuple (SzType size = 0 ) : object(PyTuple_New(ssize_t_cast(size)), stolen_t{}) {
1402
1414
if (!m_ptr) pybind11_fail (" Could not allocate tuple object!" );
1403
1415
}
1404
1416
size_t size () const { return (size_t ) PyTuple_Size (m_ptr); }
@@ -1467,7 +1479,10 @@ class sequence : public object {
1467
1479
class list : public object {
1468
1480
public:
1469
1481
PYBIND11_OBJECT_CVT (list, object, PyList_Check, PySequence_List)
1470
- explicit list (size_t size = 0 ) : object(PyList_New((ssize_t ) size), stolen_t{}) {
1482
+ template <typename SzType = ssize_t ,
1483
+ detail::enable_if_t <std::is_integral<SzType>::value, int > = 0 >
1484
+ // Some compilers generate link errors when using `const SzType &` here:
1485
+ explicit list (SzType size = 0 ) : object(PyList_New(ssize_t_cast(size)), stolen_t{}) {
1471
1486
if (!m_ptr) pybind11_fail (" Could not allocate list object!" );
1472
1487
}
1473
1488
size_t size () const { return (size_t ) PyList_Size (m_ptr); }
@@ -1479,9 +1494,12 @@ class list : public object {
1479
1494
template <typename T> void append (T &&val) /* py-non-const */ {
1480
1495
PyList_Append (m_ptr, detail::object_or_cast (std::forward<T>(val)).ptr ());
1481
1496
}
1482
- template <typename T> void insert (size_t index, T &&val) /* py-non-const */ {
1483
- PyList_Insert (m_ptr, static_cast <ssize_t >(index ),
1484
- detail::object_or_cast (std::forward<T>(val)).ptr ());
1497
+ template <typename IdxType,
1498
+ typename ValType,
1499
+ detail::enable_if_t <std::is_integral<IdxType>::value, int > = 0 >
1500
+ void insert (const IdxType &index, ValType &&val) /* py-non-const */ {
1501
+ PyList_Insert (
1502
+ m_ptr, ssize_t_cast (index ), detail::object_or_cast (std::forward<ValType>(val)).ptr ());
1485
1503
}
1486
1504
};
1487
1505
0 commit comments