/* Fast access macros */
#define DICT_CONTAINS(d, k) (d->ob_type->tp_as_sequence->sq_contains(d, k))
-#define IS_SET(so) (so->ob_type == &PySet_Type || so->ob_type == &PyFrozenSet_Type)
/* set object **********************************************************/
Py_DECREF(it);
Py_DECREF(data);
Py_DECREF(item);
- PyErr_SetString(PyExc_TypeError,
- "all set entries must be immutable");
return NULL;
}
Py_DECREF(item);
}
static PyObject *
-set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *iterable = NULL;
return make_new_set(type, iterable);
}
+static PyObject *
+set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+ PyObject *iterable = NULL;
+
+ return make_new_set(type, NULL);
+}
+
static void
set_dealloc(PySetObject *so)
{
PyObject *item, *data, *it;
result = (PySetObject *)set_copy(so);
+ if (result == NULL)
+ return NULL;
it = PyObject_GetIter(other);
if (it == NULL) {
Py_DECREF(result);
Py_DECREF(it);
Py_DECREF(result);
Py_DECREF(item);
- PyErr_SetString(PyExc_TypeError,
- "all set entries must be immutable");
return NULL;
}
Py_DECREF(item);
if (PyDict_SetItem(data, item, Py_True) == -1) {
Py_DECREF(it);
Py_DECREF(item);
- PyErr_SetString(PyExc_TypeError,
- "all set entries must be immutable");
return NULL;
}
Py_DECREF(item);
static PyObject *
set_or(PySetObject *so, PyObject *other)
{
- if (!IS_SET(so) || !IS_SET(other)) {
+ if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
{
PyObject *result;
- if (!IS_SET(other)) {
+ if (!PyAnySet_Check(other)) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
Py_DECREF(it);
Py_DECREF(result);
Py_DECREF(item);
- PyErr_SetString(PyExc_TypeError,
- "all set entries must be immutable");
return NULL;
}
}
Py_DECREF(newdict);
Py_DECREF(it);
Py_DECREF(item);
- PyErr_SetString(PyExc_TypeError,
- "all set entries must be immutable");
return NULL;
}
}
static PyObject *
set_and(PySetObject *so, PyObject *other)
{
- if (!IS_SET(so) || !IS_SET(other)) {
+ if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
{
PyObject *result;
- if (!IS_SET(other)) {
+ if (!PyAnySet_Check(other)) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
static PyObject *
set_sub(PySetObject *so, PyObject *other)
{
- if (!IS_SET(so) || !IS_SET(other)) {
+ if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
{
PyObject *result;
- if (!IS_SET(other)) {
+ if (!PyAnySet_Check(other)) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
Py_DECREF(otherset);
Py_DECREF(result);
Py_DECREF(item);
- PyErr_SetString(PyExc_TypeError,
- "all set entries must be immutable");
return NULL;
}
}
if (PyDict_Check(other))
otherdata = other;
- else if (IS_SET(other))
+ else if (PyAnySet_Check(other))
otherdata = ((PySetObject *)other)->data;
else {
otherset = (PySetObject *)make_new_set(so->ob_type, other);
Py_XDECREF(otherset);
Py_DECREF(it);
Py_DECREF(item);
- PyErr_SetString(PyExc_TypeError,
- "all set entries must be immutable");
return NULL;
}
} else {
Py_XDECREF(otherset);
Py_DECREF(it);
Py_DECREF(item);
- PyErr_SetString(PyExc_TypeError,
- "all set entries must be immutable");
return NULL;
}
}
static PyObject *
set_xor(PySetObject *so, PyObject *other)
{
- if (!IS_SET(so) || !IS_SET(other)) {
+ if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
{
PyObject *result;
- if (!IS_SET(other)) {
+ if (!PyAnySet_Check(other)) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
{
PyObject *otherdata, *it, *item;
- if (!IS_SET(other)) {
+ if (!PyAnySet_Check(other)) {
PyErr_SetString(PyExc_TypeError, "can only compare to a set");
return NULL;
}
Py_DECREF(item);
}
Py_DECREF(it);
+ if (PyErr_Occurred())
+ return NULL;
Py_RETURN_TRUE;
}
static PyObject *
set_issuperset(PySetObject *so, PyObject *other)
{
- if (!IS_SET(other)) {
+ if (!PyAnySet_Check(other)) {
PyErr_SetString(PyExc_TypeError, "can only compare to a set");
return NULL;
}
hash ^= PyObject_Hash(item);
Py_DECREF(item);
}
- so->hash = hash;
Py_DECREF(it);
+ if (PyErr_Occurred())
+ return -1;
+ so->hash = hash;
return hash;
}
static PyObject *
set_richcompare(PySetObject *v, PyObject *w, int op)
{
- /* XXX factor out is_set test */
- if (op == Py_EQ && !IS_SET(w))
- Py_RETURN_FALSE;
- else if (op == Py_NE && !IS_SET(w))
- Py_RETURN_TRUE;
- if (!IS_SET(w)) {
+ if(!PyAnySet_Check(w)) {
+ if (op == Py_EQ)
+ Py_RETURN_FALSE;
+ if (op == Py_NE)
+ Py_RETURN_TRUE;
PyErr_SetString(PyExc_TypeError, "can only compare to a set");
return NULL;
}
PyObject *keys, *result, *listrepr;
keys = PyDict_Keys(so->data);
+ if (keys == NULL)
+ return NULL;
listrepr = PyObject_Repr(keys);
Py_DECREF(keys);
+ if (listrepr == NULL)
+ return NULL;
result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
PyString_AS_STRING(listrepr));
}
Py_DECREF(it);
fprintf(fp, "])");
+ if (PyErr_Occurred())
+ return -1;
return 0;
}
return NULL;
}
Py_INCREF(key);
- if (PyDict_DelItem(so->data, key) == -1)
- PyErr_Clear();
+ if (PyDict_DelItem(so->data, key) == -1) {
+ Py_DECREF(key);
+ return NULL;
+ }
return key;
}
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
+static int
+set_init(PySetObject *self, PyObject *args, PyObject *kwds)
+{
+ PyObject *iterable = NULL;
+ PyObject *result;
+
+ if (!PyAnySet_Check(self))
+ return -1;
+ if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
+ return -1;
+ PyDict_Clear(self->data);
+ self->hash = -1;
+ if (iterable == NULL)
+ return 0;
+ result = set_union_update(self, iterable);
+ if (result != NULL) {
+ Py_DECREF(result);
+ return 0;
+ }
+ return -1;
+}
+
static PySequenceMethods set_as_sequence = {
(inquiry)set_len, /* sq_length */
0, /* sq_concat */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
- 0, /* tp_init */
+ (initproc)set_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
set_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
0, /* tp_dictoffset */
0, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
- set_new, /* tp_new */
+ frozenset_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
};