@@ -2653,20 +2653,22 @@ set_remove_impl(PySetObject *so, PyObject *key)
26532653{
26542654 int rv ;
26552655
2656- rv = set_discard_key (so , key );
2657- if (rv < 0 ) {
2658- if (!PySet_Check (key ) || !PyErr_ExceptionMatches (PyExc_TypeError ))
2656+ Py_hash_t hash = PyObject_Hash (key );
2657+ if (hash == -1 ) {
2658+ if (!PySet_Check (key ) || !PyErr_ExceptionMatches (PyExc_TypeError )) {
2659+ set_unhashable_type (key );
26592660 return NULL ;
2661+ }
26602662 PyErr_Clear ();
2661- Py_hash_t hash ;
26622663 Py_BEGIN_CRITICAL_SECTION (key );
26632664 hash = frozenset_hash_impl (key );
26642665 Py_END_CRITICAL_SECTION ();
2665- rv = set_discard_entry (so , key , hash );
2666- if (rv < 0 )
2667- return NULL ;
26682666 }
26692667
2668+ rv = set_discard_entry (so , key , hash );
2669+ if (rv < 0 )
2670+ return NULL ;
2671+
26702672 if (rv == DISCARD_NOTFOUND ) {
26712673 _PyErr_SetKeyError (key );
26722674 return NULL ;
@@ -2693,19 +2695,21 @@ set_discard_impl(PySetObject *so, PyObject *key)
26932695{
26942696 int rv ;
26952697
2696- rv = set_discard_key (so , key );
2697- if (rv < 0 ) {
2698- if (!PySet_Check (key ) || !PyErr_ExceptionMatches (PyExc_TypeError ))
2698+ Py_hash_t hash = PyObject_Hash (key );
2699+ if (hash == -1 ) {
2700+ if (!PySet_Check (key ) || !PyErr_ExceptionMatches (PyExc_TypeError )) {
2701+ set_unhashable_type (key );
26992702 return NULL ;
2703+ }
27002704 PyErr_Clear ();
2701- Py_hash_t hash ;
27022705 Py_BEGIN_CRITICAL_SECTION (key );
27032706 hash = frozenset_hash_impl (key );
27042707 Py_END_CRITICAL_SECTION ();
2705- rv = set_discard_entry (so , key , hash );
2706- if (rv < 0 )
2707- return NULL ;
27082708 }
2709+
2710+ rv = set_discard_entry (so , key , hash );
2711+ if (rv < 0 )
2712+ return NULL ;
27092713 Py_RETURN_NONE ;
27102714}
27112715
0 commit comments