Kaydet (Commit) 438e02df authored tarafından Raymond Hettinger's avatar Raymond Hettinger

* Refactor set.__contains__()

* Use Py_RETURN_NONE everywhere.
* Fix-up the firstpass check for the tp_print slot.
üst 0deab627
...@@ -158,18 +158,9 @@ set_contains(PySetObject *so, PyObject *key) ...@@ -158,18 +158,9 @@ set_contains(PySetObject *so, PyObject *key)
static PyObject * static PyObject *
set_direct_contains(PySetObject *so, PyObject *key) set_direct_contains(PySetObject *so, PyObject *key)
{ {
PyObject *tmp;
long result; long result;
result = PyDict_Contains(so->data, key); result = set_contains(so, key);
if (result == -1 && PyAnySet_Check(key)) {
PyErr_Clear();
tmp = frozenset_dict_wrapper(((PySetObject *)(key))->data);
if (tmp == NULL)
return NULL;
result = PyDict_Contains(so->data, tmp);
Py_DECREF(tmp);
}
if (result == -1) if (result == -1)
return NULL; return NULL;
return PyBool_FromLong(result); return PyBool_FromLong(result);
...@@ -655,8 +646,8 @@ frozenset_hash(PyObject *self) ...@@ -655,8 +646,8 @@ frozenset_hash(PyObject *self)
PySetObject *so = (PySetObject *)self; PySetObject *so = (PySetObject *)self;
PyObject *key, *value; PyObject *key, *value;
int pos = 0; int pos = 0;
long hash = 0; long hash = 0;
if (so->hash != -1) if (so->hash != -1)
return so->hash; return so->hash;
...@@ -728,11 +719,13 @@ static int ...@@ -728,11 +719,13 @@ static int
set_tp_print(PySetObject *so, FILE *fp, int flags) set_tp_print(PySetObject *so, FILE *fp, int flags)
{ {
PyObject *key, *value; PyObject *key, *value;
int pos = 0; int pos=0, firstpass=1;
fprintf(fp, "%s([", so->ob_type->tp_name); fprintf(fp, "%s([", so->ob_type->tp_name);
while (PyDict_Next(so->data, &pos, &key, &value)) { while (PyDict_Next(so->data, &pos, &key, &value)) {
if (pos) if (firstpass)
firstpass = 0;
else
fprintf(fp, ", "); fprintf(fp, ", ");
if (PyObject_Print(key, fp, 0) != 0) if (PyObject_Print(key, fp, 0) != 0)
return -1; return -1;
...@@ -746,8 +739,7 @@ set_clear(PySetObject *so) ...@@ -746,8 +739,7 @@ set_clear(PySetObject *so)
{ {
PyDict_Clear(so->data); PyDict_Clear(so->data);
so->hash = -1; so->hash = -1;
Py_INCREF(Py_None); Py_RETURN_NONE;
return Py_None;
} }
PyDoc_STRVAR(clear_doc, "Remove all elements from this set."); PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
...@@ -765,8 +757,7 @@ set_add(PySetObject *so, PyObject *item) ...@@ -765,8 +757,7 @@ set_add(PySetObject *so, PyObject *item)
{ {
if (PyDict_SetItem(so->data, item, Py_True) == -1) if (PyDict_SetItem(so->data, item, Py_True) == -1)
return NULL; return NULL;
Py_INCREF(Py_None); Py_RETURN_NONE;
return Py_None;
} }
PyDoc_STRVAR(add_doc, PyDoc_STRVAR(add_doc,
...@@ -790,8 +781,7 @@ set_remove(PySetObject *so, PyObject *item) ...@@ -790,8 +781,7 @@ set_remove(PySetObject *so, PyObject *item)
if (PyDict_DelItem(so->data, item) == -1) if (PyDict_DelItem(so->data, item) == -1)
return NULL; return NULL;
Py_INCREF(Py_None); Py_RETURN_NONE;
return Py_None;
} }
PyDoc_STRVAR(remove_doc, PyDoc_STRVAR(remove_doc,
...@@ -818,8 +808,7 @@ set_discard(PySetObject *so, PyObject *item) ...@@ -818,8 +808,7 @@ set_discard(PySetObject *so, PyObject *item)
return NULL; return NULL;
PyErr_Clear(); PyErr_Clear();
} }
Py_INCREF(Py_None); Py_RETURN_NONE;
return Py_None;
} }
PyDoc_STRVAR(discard_doc, PyDoc_STRVAR(discard_doc,
......
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