@@ -1382,7 +1382,7 @@ class iterator : public object {
13821382private:
13831383 void advance () {
13841384 value = reinterpret_steal<object>(PyIter_Next (m_ptr));
1385- if (PyErr_Occurred ()) {
1385+ if (value. ptr () == nullptr && PyErr_Occurred ()) {
13861386 throw error_already_set ();
13871387 }
13881388 }
@@ -1809,16 +1809,16 @@ class capsule : public object {
18091809
18101810 explicit capsule (const void *value,
18111811 const char *name = nullptr ,
1812- void (* destructor)(PyObject *) = nullptr)
1812+ PyCapsule_Destructor destructor = nullptr )
18131813 : object(PyCapsule_New(const_cast <void *>(value), name, destructor), stolen_t{}) {
18141814 if (!m_ptr) {
18151815 throw error_already_set ();
18161816 }
18171817 }
18181818
1819- PYBIND11_DEPRECATED (" Please pass a destructor that takes a void pointer as input " )
1820- capsule (const void *value, void (*destruct)(PyObject *) )
1821- : object(PyCapsule_New(const_cast <void *>(value), nullptr , destruct ), stolen_t {}) {
1819+ PYBIND11_DEPRECATED (" Please use the ctor with value, name, destructor args " )
1820+ capsule (const void *value, PyCapsule_Destructor destructor )
1821+ : object(PyCapsule_New(const_cast <void *>(value), nullptr , destructor ), stolen_t {}) {
18221822 if (!m_ptr) {
18231823 throw error_already_set ();
18241824 }
@@ -1829,7 +1829,7 @@ class capsule : public object {
18291829 // guard if destructor called while err indicator is set
18301830 error_scope error_guard;
18311831 auto destructor = reinterpret_cast <void (*)(void *)>(PyCapsule_GetContext (o));
1832- if (PyErr_Occurred ()) {
1832+ if (destructor == nullptr && PyErr_Occurred ()) {
18331833 throw error_already_set ();
18341834 }
18351835 const char *name = get_name_in_error_scope (o);
@@ -1843,7 +1843,7 @@ class capsule : public object {
18431843 }
18441844 });
18451845
1846- if (!m_ptr || PyCapsule_SetContext (m_ptr, ( void *) destructor) != 0 ) {
1846+ if (!m_ptr || PyCapsule_SetContext (m_ptr, reinterpret_cast < void *>( destructor) ) != 0 ) {
18471847 throw error_already_set ();
18481848 }
18491849 }
@@ -1967,7 +1967,11 @@ class dict : public object {
19671967 void clear () /* py-non-const */ { PyDict_Clear (ptr ()); }
19681968 template <typename T>
19691969 bool contains (T &&key) const {
1970- return PyDict_Contains (m_ptr, detail::object_or_cast (std::forward<T>(key)).ptr ()) == 1 ;
1970+ auto result = PyDict_Contains (m_ptr, detail::object_or_cast (std::forward<T>(key)).ptr ());
1971+ if (result == -1 ) {
1972+ throw error_already_set ();
1973+ }
1974+ return result == 1 ;
19711975 }
19721976
19731977private:
@@ -2053,7 +2057,11 @@ class anyset : public object {
20532057 bool empty () const { return size () == 0 ; }
20542058 template <typename T>
20552059 bool contains (T &&val) const {
2056- return PySet_Contains (m_ptr, detail::object_or_cast (std::forward<T>(val)).ptr ()) == 1 ;
2060+ auto result = PySet_Contains (m_ptr, detail::object_or_cast (std::forward<T>(val)).ptr ());
2061+ if (result == -1 ) {
2062+ throw error_already_set ();
2063+ }
2064+ return result == 1 ;
20572065 }
20582066};
20592067
0 commit comments