@@ -661,15 +661,17 @@ struct generic_item {
661661struct sequence_item {
662662 using key_type = size_t ;
663663
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));
666667 if (!result) { throw error_already_set (); }
667668 return reinterpret_steal<object>(result);
668669 }
669670
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) {
671673 // 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 ) {
673675 throw error_already_set ();
674676 }
675677 }
@@ -678,15 +680,17 @@ struct sequence_item {
678680struct list_item {
679681 using key_type = size_t ;
680682
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));
683686 if (!result) { throw error_already_set (); }
684687 return reinterpret_borrow<object>(result);
685688 }
686689
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) {
688692 // 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 ) {
690694 throw error_already_set ();
691695 }
692696 }
@@ -695,15 +699,17 @@ struct list_item {
695699struct tuple_item {
696700 using key_type = size_t ;
697701
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));
700705 if (!result) { throw error_already_set (); }
701706 return reinterpret_borrow<object>(result);
702707 }
703708
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) {
705711 // 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 ) {
707713 throw error_already_set ();
708714 }
709715 }
@@ -1043,8 +1049,9 @@ class str : public object {
10431049public:
10441050 PYBIND11_OBJECT_CVT (str, object, PYBIND11_STR_CHECK_FUN, raw_str)
10451051
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 {}) {
10481055 if (!m_ptr) pybind11_fail (" Could not allocate string object!" );
10491056 }
10501057
@@ -1116,8 +1123,9 @@ class bytes : public object {
11161123 if (!m_ptr) pybind11_fail (" Could not allocate bytes object!" );
11171124 }
11181125
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 {}) {
11211129 if (!m_ptr) pybind11_fail (" Could not allocate bytes object!" );
11221130 }
11231131
@@ -1160,7 +1168,7 @@ inline str::str(const bytes& b) {
11601168 ssize_t length = 0 ;
11611169 if (PYBIND11_BYTES_AS_STRING_AND_SIZE (b.ptr (), &buffer, &length))
11621170 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));
11641172 if (!obj)
11651173 pybind11_fail (" Could not allocate string object!" );
11661174 m_ptr = obj.release ().ptr ();
@@ -1172,8 +1180,9 @@ class bytearray : public object {
11721180public:
11731181 PYBIND11_OBJECT_CVT (bytearray, object, PyByteArray_Check, PyByteArray_FromObject)
11741182
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 {}) {
11771186 if (!m_ptr) pybind11_fail (" Could not allocate bytearray object!" );
11781187 }
11791188
@@ -1398,7 +1407,10 @@ class capsule : public object {
13981407class tuple : public object {
13991408public:
14001409 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{}) {
14021414 if (!m_ptr) pybind11_fail (" Could not allocate tuple object!" );
14031415 }
14041416 size_t size () const { return (size_t ) PyTuple_Size (m_ptr); }
@@ -1467,7 +1479,10 @@ class sequence : public object {
14671479class list : public object {
14681480public:
14691481 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{}) {
14711486 if (!m_ptr) pybind11_fail (" Could not allocate list object!" );
14721487 }
14731488 size_t size () const { return (size_t ) PyList_Size (m_ptr); }
@@ -1479,9 +1494,12 @@ class list : public object {
14791494 template <typename T> void append (T &&val) /* py-non-const */ {
14801495 PyList_Append (m_ptr, detail::object_or_cast (std::forward<T>(val)).ptr ());
14811496 }
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 ());
14851503 }
14861504};
14871505
0 commit comments