[Python-ideas] Implement __add__ for set and frozenset
Arnaud Delobelle
arnodel at googlemail.com
Wed Jun 4 10:25:38 CEST 2008
2008/6/3 Raymond Hettinger <python at rcn.com>:
>> Given the other grossly inaccurate statement in that post, I conclude
>> that drinking and posting on python-ideas are incompatible occupations. So
>> I will refrain from any further claim on that subject till tomorrow
>> morning.
>
> No worries, except that mailman saves a copy, google indexes it, and
> all of your posts will be visible to your great-great grandchildren for
> all eternity. Other than that, it's just a casual idea session between
> friends ;-)
>
>
> Raymond
>
I have looked at setobject.c, it seems mostly straightforward to make
the change, e.g. for union this is the current union method:
static PyObject *
set_union(PySetObject *so, PyObject *other)
{
PySetObject *result;
result = (PySetObject *)set_copy(so);
if (result == NULL)
return NULL;
if ((PyObject *)so == other)
return (PyObject *)result;
if (set_update_internal(result, other) == -1) {
Py_DECREF(result);
return NULL;
}
return (PyObject *)result;
}
A slight change makes it accept variable number of arguments (note
that I haven't tried to optimize it by rearranging the order of
arguments):
static PyObject *
set_union(PySetObject *so, PyObject *args)
{
PySetObject *result;
PyObject *arg;
Py_ssize_t nargs;
Py_ssize_t i;
result = (PySetObject *)set_copy(so);
if (result == NULL)
return NULL;
nargs = PyTuple_GET_SIZE(args);
for (i = 0; i < nargs; i++) {
arg = PyTuple_GET_ITEM(args, i);
if ((PyObject *)so != arg && set_update_internal(result, arg) == -1) {
Py_DECREF(result);
return NULL;
}
}
return (PyObject *)result;
}
And then in set_methods[], change
{"union", (PyCFunction)set_union, METH_O,
union_doc},
to
{"union", (PyCFunction)set_union, METH_VARARGS,
vunion_doc},
I would go through the whole of setobjects.c and try to do this for
each relevant method. However I have not touched Python source code
before, and I am not at all confident that I would make a good job of
it. If someone offered to review my effort privately, I'd be happy to
have a go but OTOH I don't want to waste anyone's time.
--
Arnaud
More information about the Python-ideas
mailing list