Commit e801d21a authored by Stefan Behnel's avatar Stefan Behnel

Streamline __Pyx_PySet_Remove() helper function. Add comments about unclean...

Streamline __Pyx_PySet_Remove() helper function. Add comments about unclean return values of new helper functions.
Closes #2042.
parent 822ac414
...@@ -397,17 +397,13 @@ static CYTHON_INLINE int __Pyx_dict_iter_next( ...@@ -397,17 +397,13 @@ static CYTHON_INLINE int __Pyx_dict_iter_next(
} }
/////////////// py_set_discard_unhashable.proto ///////////////
static int __Pyx_PySet_DiscardUnhashable(PyObject *set, PyObject *key); /* proto */
/////////////// py_set_discard_unhashable /////////////// /////////////// py_set_discard_unhashable ///////////////
static int __Pyx_PySet_DiscardUnhashable(PyObject *set, PyObject *key) { static int __Pyx_PySet_DiscardUnhashable(PyObject *set, PyObject *key) {
PyObject *tmpkey; PyObject *tmpkey;
int rv; int rv;
if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError)) if (likely(!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError)))
return -1; return -1;
PyErr_Clear(); PyErr_Clear();
tmpkey = PyFrozenSet_New(key); tmpkey = PyFrozenSet_New(key);
...@@ -427,14 +423,13 @@ static CYTHON_INLINE int __Pyx_PySet_Discard(PyObject *set, PyObject *key); /*pr ...@@ -427,14 +423,13 @@ static CYTHON_INLINE int __Pyx_PySet_Discard(PyObject *set, PyObject *key); /*pr
//@requires: py_set_discard_unhashable //@requires: py_set_discard_unhashable
static CYTHON_INLINE int __Pyx_PySet_Discard(PyObject *set, PyObject *key) { static CYTHON_INLINE int __Pyx_PySet_Discard(PyObject *set, PyObject *key) {
int rv; int found = PySet_Discard(set, key);
// Convert *key* to frozenset if necessary
rv = PySet_Discard(set, key); if (unlikely(found < 0)) {
/* Convert *key* to frozenset if necessary */ found = __Pyx_PySet_DiscardUnhashable(set, key);
if (unlikely(rv < 0)) {
return __Pyx_PySet_DiscardUnhashable(set, key);
} }
return rv; // note: returns -1 on error, 0 (not found) or 1 (found) otherwise => error check for -1 or < 0 works
return found;
} }
...@@ -445,16 +440,13 @@ static CYTHON_INLINE int __Pyx_PySet_Remove(PyObject *set, PyObject *key); /*pro ...@@ -445,16 +440,13 @@ static CYTHON_INLINE int __Pyx_PySet_Remove(PyObject *set, PyObject *key); /*pro
/////////////// py_set_remove /////////////// /////////////// py_set_remove ///////////////
//@requires: py_set_discard_unhashable //@requires: py_set_discard_unhashable
static CYTHON_INLINE int __Pyx_PySet_Remove(PyObject *set, PyObject *key) { static int __Pyx_PySet_RemoveNotFound(PyObject *set, PyObject *key, int found) {
int rv; // Convert *key* to frozenset if necessary
if (unlikely(found < 0)) {
rv = PySet_Discard(set, key); found = __Pyx_PySet_DiscardUnhashable(set, key);
/* Convert *key* to frozenset if necessary */
if (unlikely(rv < 0)) {
rv = __Pyx_PySet_DiscardUnhashable(set, key);
} }
if (rv == 0) { if (likely(found == 0)) {
/* Not found */ // Not found
PyObject *tup; PyObject *tup;
tup = PyTuple_Pack(1, key); tup = PyTuple_Pack(1, key);
if (!tup) if (!tup)
...@@ -463,6 +455,16 @@ static CYTHON_INLINE int __Pyx_PySet_Remove(PyObject *set, PyObject *key) { ...@@ -463,6 +455,16 @@ static CYTHON_INLINE int __Pyx_PySet_Remove(PyObject *set, PyObject *key) {
Py_DECREF(tup); Py_DECREF(tup);
return -1; return -1;
} }
// note: returns -1 on error, 0 (not found) or 1 (found) otherwise => error check for -1 or < 0 works
return found;
}
static CYTHON_INLINE int __Pyx_PySet_Remove(PyObject *set, PyObject *key) {
int found = PySet_Discard(set, key);
if (unlikely(found != 1)) {
// note: returns -1 on error, 0 (not found) or 1 (found) otherwise => error check for -1 or < 0 works
return __Pyx_PySet_RemoveNotFound(set, key, found);
}
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment