diff options
author | Raymond Hettinger <python@rcn.com> | 2003-12-13 11:26:12 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2003-12-13 11:26:12 (GMT) |
commit | 8f5cdaa784f555149adf5e94fd2e989f99d6b1db (patch) | |
tree | d6a8f66d75cc46f849b5933fb2a57b41e76a7d4f /Objects/setobject.c | |
parent | feb78c94fa444a75b912ca355c3c80d7453f03f0 (diff) | |
download | cpython-8f5cdaa784f555149adf5e94fd2e989f99d6b1db.zip cpython-8f5cdaa784f555149adf5e94fd2e989f99d6b1db.tar.gz cpython-8f5cdaa784f555149adf5e94fd2e989f99d6b1db.tar.bz2 |
* Added a new method flag, METH_COEXIST.
* Used the flag to optimize set.__contains__(), dict.__contains__(),
dict.__getitem__(), and list.__getitem__().
Diffstat (limited to 'Objects/setobject.c')
-rw-r--r-- | Objects/setobject.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/Objects/setobject.c b/Objects/setobject.c index c060077..8e70546 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -156,6 +156,28 @@ set_contains(PySetObject *so, PyObject *key) } static PyObject * +set_direct_contains(PySetObject *so, PyObject *key) +{ + PyObject *tmp; + long result; + + result = PyDict_Contains(so->data, 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) + return NULL; + return PyBool_FromLong(result); +} + +PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x."); + +static PyObject * set_copy(PySetObject *so) { return make_new_set(so->ob_type, (PyObject *)so); @@ -968,6 +990,8 @@ static PyMethodDef set_methods[] = { add_doc}, {"clear", (PyCFunction)set_clear, METH_NOARGS, clear_doc}, + {"__contains__", (PyCFunction)set_direct_contains, METH_O | METH_COEXIST, + contains_doc}, {"copy", (PyCFunction)set_copy, METH_NOARGS, copy_doc}, {"__copy__", (PyCFunction)set_copy, METH_NOARGS, @@ -1094,6 +1118,8 @@ PyTypeObject PySet_Type = { static PyMethodDef frozenset_methods[] = { + {"__contains__", (PyCFunction)set_direct_contains, METH_O | METH_COEXIST, + contains_doc}, {"copy", (PyCFunction)frozenset_copy, METH_NOARGS, copy_doc}, {"__copy__", (PyCFunction)frozenset_copy, METH_NOARGS, |