Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | |
| 3 | /* set object implementation |
| 4 | written and maintained by Raymond D. Hettinger <python@rcn.com> |
| 5 | derived from sets.py written by Greg V. Wilson, Alex Martelli, |
| 6 | Guido van Rossum, Raymond Hettinger, and Tim Peters. |
| 7 | |
| 8 | Copyright (c) 2003 Python Software Foundation. |
| 9 | All rights reserved. |
| 10 | */ |
| 11 | |
| 12 | /* Fast access macros */ |
| 13 | |
| 14 | #define DICT_CONTAINS(d, k) (d->ob_type->tp_as_sequence->sq_contains(d, k)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 15 | |
| 16 | /* set object **********************************************************/ |
| 17 | |
| 18 | static PyObject * |
| 19 | make_new_set(PyTypeObject *type, PyObject *iterable) |
| 20 | { |
| 21 | PyObject *data; |
| 22 | PyObject *it = NULL; |
| 23 | PyObject *item; |
| 24 | PySetObject *so; |
| 25 | |
| 26 | /* Get iterator. */ |
| 27 | if (iterable != NULL) { |
| 28 | it = PyObject_GetIter(iterable); |
| 29 | if (it == NULL) |
| 30 | return NULL; |
| 31 | } |
| 32 | |
| 33 | data = PyDict_New(); |
| 34 | if (data == NULL) { |
| 35 | Py_DECREF(it); |
| 36 | return NULL; |
| 37 | } |
| 38 | |
| 39 | while (it != NULL && (item = PyIter_Next(it)) != NULL) { |
| 40 | if (PyDict_SetItem(data, item, Py_True) == -1) { |
| 41 | Py_DECREF(it); |
| 42 | Py_DECREF(data); |
| 43 | Py_DECREF(item); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 44 | return NULL; |
| 45 | } |
| 46 | Py_DECREF(item); |
| 47 | } |
| 48 | Py_XDECREF(it); |
| 49 | if (PyErr_Occurred()) { |
| 50 | Py_DECREF(data); |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | /* create PySetObject structure */ |
| 55 | so = (PySetObject *)type->tp_alloc(type, 0); |
| 56 | if (so == NULL) { |
| 57 | Py_DECREF(data); |
| 58 | return NULL; |
| 59 | } |
| 60 | so->data = data; |
| 61 | so->hash = -1; |
| 62 | |
| 63 | return (PyObject *)so; |
| 64 | } |
| 65 | |
| 66 | static PyObject * |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 67 | frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 68 | { |
| 69 | PyObject *iterable = NULL; |
| 70 | |
| 71 | if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable)) |
| 72 | return NULL; |
| 73 | return make_new_set(type, iterable); |
| 74 | } |
| 75 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 76 | static PyObject * |
| 77 | set_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 78 | { |
| 79 | PyObject *iterable = NULL; |
| 80 | |
| 81 | return make_new_set(type, NULL); |
| 82 | } |
| 83 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 84 | static void |
| 85 | set_dealloc(PySetObject *so) |
| 86 | { |
| 87 | PyObject_GC_UnTrack(so); |
| 88 | Py_XDECREF(so->data); |
| 89 | so->ob_type->tp_free(so); |
| 90 | } |
| 91 | |
| 92 | static int |
| 93 | set_traverse(PySetObject *so, visitproc visit, void *arg) |
| 94 | { |
| 95 | if (so->data) |
| 96 | return visit(so->data, arg); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static PyObject * |
| 101 | set_iter(PySetObject *so) |
| 102 | { |
| 103 | return PyObject_GetIter(so->data); |
| 104 | } |
| 105 | |
| 106 | static int |
| 107 | set_len(PySetObject *so) |
| 108 | { |
| 109 | return PyDict_Size(so->data); |
| 110 | } |
| 111 | |
| 112 | static int |
| 113 | set_contains(PySetObject *so, PyObject *key) |
| 114 | { |
| 115 | return DICT_CONTAINS(so->data, key); |
| 116 | } |
| 117 | |
| 118 | static PyObject * |
| 119 | set_copy(PySetObject *so) |
| 120 | { |
| 121 | PyObject *data; |
| 122 | PySetObject *newso; |
| 123 | |
| 124 | data = PyDict_Copy(so->data); |
| 125 | if (data == NULL) |
| 126 | return NULL; |
| 127 | |
| 128 | newso = (PySetObject *)(so->ob_type->tp_alloc(so->ob_type, 0)); |
| 129 | if (newso == NULL) { |
| 130 | Py_DECREF(data); |
| 131 | return NULL; |
| 132 | } |
| 133 | newso->data = data; |
| 134 | newso->hash = so->hash; |
| 135 | return (PyObject *)newso; |
| 136 | } |
| 137 | |
| 138 | PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set."); |
| 139 | |
| 140 | static PyObject * |
| 141 | set_union(PySetObject *so, PyObject *other) |
| 142 | { |
| 143 | PySetObject *result; |
| 144 | PyObject *item, *data, *it; |
| 145 | |
| 146 | result = (PySetObject *)set_copy(so); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 147 | if (result == NULL) |
| 148 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 149 | it = PyObject_GetIter(other); |
| 150 | if (it == NULL) { |
| 151 | Py_DECREF(result); |
| 152 | return NULL; |
| 153 | } |
| 154 | data = result->data; |
| 155 | while ((item = PyIter_Next(it)) != NULL) { |
| 156 | if (PyDict_SetItem(data, item, Py_True) == -1) { |
| 157 | Py_DECREF(it); |
| 158 | Py_DECREF(result); |
| 159 | Py_DECREF(item); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 160 | return NULL; |
| 161 | } |
| 162 | Py_DECREF(item); |
| 163 | } |
| 164 | Py_DECREF(it); |
| 165 | if (PyErr_Occurred()) { |
| 166 | Py_DECREF(result); |
| 167 | return NULL; |
| 168 | } |
| 169 | return (PyObject *)result; |
| 170 | } |
| 171 | |
| 172 | PyDoc_STRVAR(union_doc, |
| 173 | "Return the union of two sets as a new set.\n\ |
| 174 | \n\ |
| 175 | (i.e. all elements that are in either set.)"); |
| 176 | |
| 177 | static PyObject * |
| 178 | set_union_update(PySetObject *so, PyObject *other) |
| 179 | { |
| 180 | PyObject *item, *data, *it; |
| 181 | |
| 182 | it = PyObject_GetIter(other); |
| 183 | if (it == NULL) |
| 184 | return NULL; |
| 185 | data = so->data; |
| 186 | |
| 187 | while ((item = PyIter_Next(it)) != NULL) { |
| 188 | if (PyDict_SetItem(data, item, Py_True) == -1) { |
| 189 | Py_DECREF(it); |
| 190 | Py_DECREF(item); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 191 | return NULL; |
| 192 | } |
| 193 | Py_DECREF(item); |
| 194 | } |
| 195 | Py_DECREF(it); |
| 196 | if (PyErr_Occurred()) |
| 197 | return NULL; |
| 198 | Py_RETURN_NONE; |
| 199 | } |
| 200 | |
| 201 | PyDoc_STRVAR(union_update_doc, |
| 202 | "Update a set with the union of itself and another."); |
| 203 | |
| 204 | static PyObject * |
| 205 | set_or(PySetObject *so, PyObject *other) |
| 206 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 207 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 208 | Py_INCREF(Py_NotImplemented); |
| 209 | return Py_NotImplemented; |
| 210 | } |
| 211 | return set_union(so, other); |
| 212 | } |
| 213 | |
| 214 | static PyObject * |
| 215 | set_ior(PySetObject *so, PyObject *other) |
| 216 | { |
| 217 | PyObject *result; |
| 218 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 219 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 220 | Py_INCREF(Py_NotImplemented); |
| 221 | return Py_NotImplemented; |
| 222 | } |
| 223 | result = set_union_update(so, other); |
| 224 | if (result == NULL) |
| 225 | return NULL; |
| 226 | Py_DECREF(result); |
| 227 | Py_INCREF(so); |
| 228 | return (PyObject *)so; |
| 229 | } |
| 230 | |
| 231 | static PyObject * |
| 232 | set_intersection(PySetObject *so, PyObject *other) |
| 233 | { |
| 234 | PySetObject *result; |
| 235 | PyObject *item, *selfdata, *tgtdata, *it; |
| 236 | |
| 237 | result = (PySetObject *)make_new_set(so->ob_type, NULL); |
| 238 | if (result == NULL) |
| 239 | return NULL; |
| 240 | |
| 241 | it = PyObject_GetIter(other); |
| 242 | if (it == NULL) { |
| 243 | Py_DECREF(result); |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | selfdata = so->data; |
| 248 | tgtdata = result->data; |
| 249 | while ((item = PyIter_Next(it)) != NULL) { |
| 250 | if (DICT_CONTAINS(selfdata, item)) { |
| 251 | if (PyDict_SetItem(tgtdata, item, Py_True) == -1) { |
| 252 | Py_DECREF(it); |
| 253 | Py_DECREF(result); |
| 254 | Py_DECREF(item); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 255 | return NULL; |
| 256 | } |
| 257 | } |
| 258 | Py_DECREF(item); |
| 259 | } |
| 260 | Py_DECREF(it); |
| 261 | if (PyErr_Occurred()) { |
| 262 | Py_DECREF(result); |
| 263 | return NULL; |
| 264 | } |
| 265 | return (PyObject *)result; |
| 266 | } |
| 267 | |
| 268 | PyDoc_STRVAR(intersection_doc, |
| 269 | "Return the intersection of two sets as a new set.\n\ |
| 270 | \n\ |
| 271 | (i.e. all elements that are in both sets.)"); |
| 272 | |
| 273 | static PyObject * |
| 274 | set_intersection_update(PySetObject *so, PyObject *other) |
| 275 | { |
| 276 | PyObject *item, *selfdata, *it, *newdict, *tmp; |
| 277 | |
| 278 | newdict = PyDict_New(); |
| 279 | if (newdict == NULL) |
| 280 | return newdict; |
| 281 | |
| 282 | it = PyObject_GetIter(other); |
| 283 | if (it == NULL) { |
| 284 | Py_DECREF(newdict); |
| 285 | return NULL; |
| 286 | } |
| 287 | |
| 288 | selfdata = so->data; |
| 289 | while ((item = PyIter_Next(it)) != NULL) { |
| 290 | if (DICT_CONTAINS(selfdata, item)) { |
| 291 | if (PyDict_SetItem(newdict, item, Py_True) == -1) { |
| 292 | Py_DECREF(newdict); |
| 293 | Py_DECREF(it); |
| 294 | Py_DECREF(item); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 295 | return NULL; |
| 296 | } |
| 297 | } |
| 298 | Py_DECREF(item); |
| 299 | } |
| 300 | Py_DECREF(it); |
| 301 | if (PyErr_Occurred()) { |
| 302 | Py_DECREF(newdict); |
| 303 | return NULL; |
| 304 | } |
| 305 | tmp = so->data; |
| 306 | so->data = newdict; |
| 307 | Py_DECREF(tmp); |
| 308 | Py_RETURN_NONE; |
| 309 | } |
| 310 | |
| 311 | PyDoc_STRVAR(intersection_update_doc, |
| 312 | "Update a set with the intersection of itself and another."); |
| 313 | |
| 314 | static PyObject * |
| 315 | set_and(PySetObject *so, PyObject *other) |
| 316 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 317 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 318 | Py_INCREF(Py_NotImplemented); |
| 319 | return Py_NotImplemented; |
| 320 | } |
| 321 | return set_intersection(so, other); |
| 322 | } |
| 323 | |
| 324 | static PyObject * |
| 325 | set_iand(PySetObject *so, PyObject *other) |
| 326 | { |
| 327 | PyObject *result; |
| 328 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 329 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 330 | Py_INCREF(Py_NotImplemented); |
| 331 | return Py_NotImplemented; |
| 332 | } |
| 333 | result = set_intersection_update(so, other); |
| 334 | if (result == NULL) |
| 335 | return NULL; |
| 336 | Py_DECREF(result); |
| 337 | Py_INCREF(so); |
| 338 | return (PyObject *)so; |
| 339 | } |
| 340 | |
| 341 | static PyObject * |
| 342 | set_difference(PySetObject *so, PyObject *other) |
| 343 | { |
| 344 | PySetObject *result; |
| 345 | PyObject *item, *tgtdata, *it; |
| 346 | |
| 347 | result = (PySetObject *)set_copy(so); |
| 348 | if (result == NULL) |
| 349 | return NULL; |
| 350 | |
| 351 | it = PyObject_GetIter(other); |
| 352 | if (it == NULL) { |
| 353 | Py_DECREF(result); |
| 354 | return NULL; |
| 355 | } |
| 356 | |
| 357 | tgtdata = result->data; |
| 358 | while ((item = PyIter_Next(it)) != NULL) { |
| 359 | if (PyDict_DelItem(tgtdata, item) == -1) { |
| 360 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 361 | PyErr_Clear(); |
| 362 | else { |
| 363 | Py_DECREF(it); |
| 364 | Py_DECREF(result); |
| 365 | Py_DECREF(item); |
| 366 | return NULL; |
| 367 | } |
| 368 | } |
| 369 | Py_DECREF(item); |
| 370 | } |
| 371 | Py_DECREF(it); |
| 372 | if (PyErr_Occurred()) { |
| 373 | Py_DECREF(result); |
| 374 | return NULL; |
| 375 | } |
| 376 | return (PyObject *)result; |
| 377 | } |
| 378 | |
| 379 | PyDoc_STRVAR(difference_doc, |
| 380 | "Return the difference of two sets as a new set.\n\ |
| 381 | \n\ |
| 382 | (i.e. all elements that are in this set but not the other.)"); |
| 383 | |
| 384 | static PyObject * |
| 385 | set_difference_update(PySetObject *so, PyObject *other) |
| 386 | { |
| 387 | PyObject *item, *tgtdata, *it; |
| 388 | |
| 389 | it = PyObject_GetIter(other); |
| 390 | if (it == NULL) |
| 391 | return NULL; |
| 392 | |
| 393 | tgtdata = so->data; |
| 394 | while ((item = PyIter_Next(it)) != NULL) { |
| 395 | if (PyDict_DelItem(tgtdata, item) == -1) { |
| 396 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 397 | PyErr_Clear(); |
| 398 | else { |
| 399 | Py_DECREF(it); |
| 400 | Py_DECREF(item); |
| 401 | return NULL; |
| 402 | } |
| 403 | } |
| 404 | Py_DECREF(item); |
| 405 | } |
| 406 | Py_DECREF(it); |
| 407 | if (PyErr_Occurred()) |
| 408 | return NULL; |
| 409 | Py_RETURN_NONE; |
| 410 | } |
| 411 | |
| 412 | PyDoc_STRVAR(difference_update_doc, |
| 413 | "Remove all elements of another set from this set."); |
| 414 | |
| 415 | static PyObject * |
| 416 | set_sub(PySetObject *so, PyObject *other) |
| 417 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 418 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 419 | Py_INCREF(Py_NotImplemented); |
| 420 | return Py_NotImplemented; |
| 421 | } |
| 422 | return set_difference(so, other); |
| 423 | } |
| 424 | |
| 425 | static PyObject * |
| 426 | set_isub(PySetObject *so, PyObject *other) |
| 427 | { |
| 428 | PyObject *result; |
| 429 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 430 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 431 | Py_INCREF(Py_NotImplemented); |
| 432 | return Py_NotImplemented; |
| 433 | } |
| 434 | result = set_difference_update(so, other); |
| 435 | if (result == NULL) |
| 436 | return NULL; |
| 437 | Py_DECREF(result); |
| 438 | Py_INCREF(so); |
| 439 | return (PyObject *)so; |
| 440 | } |
| 441 | |
| 442 | static PyObject * |
| 443 | set_symmetric_difference(PySetObject *so, PyObject *other) |
| 444 | { |
| 445 | PySetObject *result, *otherset; |
| 446 | PyObject *item, *selfdata, *otherdata, *tgtdata, *it; |
| 447 | |
| 448 | selfdata = so->data; |
| 449 | |
| 450 | result = (PySetObject *)set_copy(so); |
| 451 | if (result == NULL) |
| 452 | return NULL; |
| 453 | tgtdata = result->data; |
| 454 | |
| 455 | otherset = (PySetObject *)make_new_set(so->ob_type, other); |
| 456 | if (otherset == NULL) { |
| 457 | Py_DECREF(result); |
| 458 | return NULL; |
| 459 | } |
| 460 | otherdata = otherset->data; |
| 461 | |
| 462 | it = PyObject_GetIter(otherdata); |
| 463 | if (it == NULL) { |
| 464 | Py_DECREF(otherset); |
| 465 | Py_DECREF(result); |
| 466 | return NULL; |
| 467 | } |
| 468 | |
| 469 | while ((item = PyIter_Next(it)) != NULL) { |
| 470 | if (PyDict_DelItem(tgtdata, item) == -1) { |
| 471 | PyErr_Clear(); |
| 472 | if (PyDict_SetItem(tgtdata, item, Py_True) == -1) { |
| 473 | Py_DECREF(it); |
| 474 | Py_DECREF(otherset); |
| 475 | Py_DECREF(result); |
| 476 | Py_DECREF(item); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 477 | return NULL; |
| 478 | } |
| 479 | } |
| 480 | Py_DECREF(item); |
| 481 | } |
| 482 | Py_DECREF(it); |
| 483 | Py_DECREF(otherset); |
| 484 | if (PyErr_Occurred()) { |
| 485 | Py_DECREF(result); |
| 486 | return NULL; |
| 487 | } |
| 488 | return (PyObject *)result; |
| 489 | } |
| 490 | |
| 491 | PyDoc_STRVAR(symmetric_difference_doc, |
| 492 | "Return the symmetric difference of two sets as a new set.\n\ |
| 493 | \n\ |
| 494 | (i.e. all elements that are in exactly one of the sets.)"); |
| 495 | |
| 496 | static PyObject * |
| 497 | set_symmetric_difference_update(PySetObject *so, PyObject *other) |
| 498 | { |
| 499 | PyObject *item, *selfdata, *it, *otherdata; |
| 500 | PySetObject *otherset = NULL; |
| 501 | |
| 502 | selfdata = so->data; |
| 503 | |
| 504 | if (PyDict_Check(other)) |
| 505 | otherdata = other; |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 506 | else if (PyAnySet_Check(other)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 507 | otherdata = ((PySetObject *)other)->data; |
| 508 | else { |
| 509 | otherset = (PySetObject *)make_new_set(so->ob_type, other); |
| 510 | if (otherset == NULL) |
| 511 | return NULL; |
| 512 | otherdata = otherset->data; |
| 513 | } |
| 514 | |
| 515 | it = PyObject_GetIter(otherdata); |
| 516 | if (it == NULL) |
| 517 | return NULL; |
| 518 | |
| 519 | while ((item = PyIter_Next(it)) != NULL) { |
| 520 | if (DICT_CONTAINS(selfdata, item)) { |
| 521 | if (PyDict_DelItem(selfdata, item) == -1) { |
| 522 | Py_XDECREF(otherset); |
| 523 | Py_DECREF(it); |
| 524 | Py_DECREF(item); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 525 | return NULL; |
| 526 | } |
| 527 | } else { |
| 528 | if (PyDict_SetItem(selfdata, item, Py_True) == -1) { |
| 529 | Py_XDECREF(otherset); |
| 530 | Py_DECREF(it); |
| 531 | Py_DECREF(item); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 532 | return NULL; |
| 533 | } |
| 534 | } |
| 535 | Py_DECREF(item); |
| 536 | } |
| 537 | Py_XDECREF(otherset); |
| 538 | Py_DECREF(it); |
| 539 | if (PyErr_Occurred()) |
| 540 | return NULL; |
| 541 | Py_RETURN_NONE; |
| 542 | } |
| 543 | |
| 544 | PyDoc_STRVAR(symmetric_difference_update_doc, |
| 545 | "Update a set with the symmetric difference of itself and another."); |
| 546 | |
| 547 | static PyObject * |
| 548 | set_xor(PySetObject *so, PyObject *other) |
| 549 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 550 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 551 | Py_INCREF(Py_NotImplemented); |
| 552 | return Py_NotImplemented; |
| 553 | } |
| 554 | return set_symmetric_difference(so, other); |
| 555 | } |
| 556 | |
| 557 | static PyObject * |
| 558 | set_ixor(PySetObject *so, PyObject *other) |
| 559 | { |
| 560 | PyObject *result; |
| 561 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 562 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 563 | Py_INCREF(Py_NotImplemented); |
| 564 | return Py_NotImplemented; |
| 565 | } |
| 566 | result = set_symmetric_difference_update(so, other); |
| 567 | if (result == NULL) |
| 568 | return NULL; |
| 569 | Py_DECREF(result); |
| 570 | Py_INCREF(so); |
| 571 | return (PyObject *)so; |
| 572 | } |
| 573 | |
| 574 | static PyObject * |
| 575 | set_issubset(PySetObject *so, PyObject *other) |
| 576 | { |
| 577 | PyObject *otherdata, *it, *item; |
| 578 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 579 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 580 | PyErr_SetString(PyExc_TypeError, "can only compare to a set"); |
| 581 | return NULL; |
| 582 | } |
| 583 | if (set_len(so) > set_len((PySetObject *)other)) |
| 584 | Py_RETURN_FALSE; |
| 585 | |
| 586 | it = PyObject_GetIter(so->data); |
| 587 | if (it == NULL) |
| 588 | return NULL; |
| 589 | |
| 590 | otherdata = ((PySetObject *)other)->data; |
| 591 | while ((item = PyIter_Next(it)) != NULL) { |
| 592 | if (!DICT_CONTAINS(otherdata, item)) { |
| 593 | Py_DECREF(it); |
| 594 | Py_DECREF(item); |
| 595 | Py_RETURN_FALSE; |
| 596 | } |
| 597 | Py_DECREF(item); |
| 598 | } |
| 599 | Py_DECREF(it); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 600 | if (PyErr_Occurred()) |
| 601 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 602 | Py_RETURN_TRUE; |
| 603 | } |
| 604 | |
| 605 | PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set."); |
| 606 | |
| 607 | static PyObject * |
| 608 | set_issuperset(PySetObject *so, PyObject *other) |
| 609 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 610 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 611 | PyErr_SetString(PyExc_TypeError, "can only compare to a set"); |
| 612 | return NULL; |
| 613 | } |
| 614 | return set_issubset((PySetObject *)other, (PyObject *)so); |
| 615 | } |
| 616 | |
| 617 | PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set."); |
| 618 | |
| 619 | static long |
| 620 | set_nohash(PyObject *self) |
| 621 | { |
| 622 | PyErr_SetString(PyExc_TypeError, "set objects are unhashable"); |
| 623 | return -1; |
| 624 | } |
| 625 | |
| 626 | static int |
| 627 | set_nocmp(PyObject *self) |
| 628 | { |
| 629 | PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()"); |
| 630 | return -1; |
| 631 | } |
| 632 | |
| 633 | static long |
| 634 | frozenset_hash(PyObject *self) |
| 635 | { |
| 636 | PyObject *it, *item; |
| 637 | PySetObject *so = (PySetObject *)self; |
| 638 | long hash = 0; |
| 639 | |
| 640 | if (so->hash != -1) |
| 641 | return so->hash; |
| 642 | |
| 643 | it = PyObject_GetIter(((PySetObject *)so)->data); |
| 644 | if (it == NULL) |
| 645 | return -1; |
| 646 | |
| 647 | while ((item = PyIter_Next(it)) != NULL) { |
| 648 | hash ^= PyObject_Hash(item); |
| 649 | Py_DECREF(item); |
| 650 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 651 | Py_DECREF(it); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 652 | if (PyErr_Occurred()) |
| 653 | return -1; |
| 654 | so->hash = hash; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 655 | return hash; |
| 656 | } |
| 657 | |
| 658 | static PyObject * |
| 659 | set_richcompare(PySetObject *v, PyObject *w, int op) |
| 660 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 661 | if(!PyAnySet_Check(w)) { |
| 662 | if (op == Py_EQ) |
| 663 | Py_RETURN_FALSE; |
| 664 | if (op == Py_NE) |
| 665 | Py_RETURN_TRUE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 666 | PyErr_SetString(PyExc_TypeError, "can only compare to a set"); |
| 667 | return NULL; |
| 668 | } |
| 669 | switch (op) { |
| 670 | case Py_EQ: |
| 671 | case Py_NE: |
| 672 | return PyObject_RichCompare(((PySetObject *)v)->data, |
| 673 | ((PySetObject *)w)->data, op); |
| 674 | case Py_LE: |
| 675 | return set_issubset((PySetObject *)v, w); |
| 676 | case Py_GE: |
| 677 | return set_issuperset((PySetObject *)v, w); |
| 678 | case Py_LT: |
| 679 | if (set_len(v) >= set_len((PySetObject *)w)) |
| 680 | Py_RETURN_FALSE; |
| 681 | return set_issubset((PySetObject *)v, w); |
| 682 | case Py_GT: |
| 683 | if (set_len(v) <= set_len((PySetObject *)w)) |
| 684 | Py_RETURN_FALSE; |
| 685 | return set_issuperset((PySetObject *)v, w); |
| 686 | } |
| 687 | Py_INCREF(Py_NotImplemented); |
| 688 | return Py_NotImplemented; |
| 689 | } |
| 690 | |
| 691 | static PyObject * |
| 692 | set_repr(PySetObject *so) |
| 693 | { |
| 694 | PyObject *keys, *result, *listrepr; |
| 695 | |
| 696 | keys = PyDict_Keys(so->data); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 697 | if (keys == NULL) |
| 698 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 699 | listrepr = PyObject_Repr(keys); |
| 700 | Py_DECREF(keys); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 701 | if (listrepr == NULL) |
| 702 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 703 | |
| 704 | result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name, |
| 705 | PyString_AS_STRING(listrepr)); |
| 706 | Py_DECREF(listrepr); |
| 707 | return result; |
| 708 | } |
| 709 | |
| 710 | static int |
| 711 | set_tp_print(PySetObject *so, FILE *fp, int flags) |
| 712 | { |
| 713 | PyObject *it, *item; |
| 714 | int firstpass=1; |
| 715 | |
| 716 | it = PyObject_GetIter(so->data); |
| 717 | if (it == NULL) |
| 718 | return -1; |
| 719 | fprintf(fp, "%s([", so->ob_type->tp_name); |
| 720 | |
| 721 | while ((item = PyIter_Next(it)) != NULL) { |
| 722 | if (firstpass == 1) |
| 723 | firstpass = 0; |
| 724 | else |
Raymond Hettinger | e2c277a | 2003-11-16 16:36:58 +0000 | [diff] [blame] | 725 | fprintf(fp, ", "); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 726 | if (PyObject_Print(item, fp, 0) != 0) { |
| 727 | Py_DECREF(it); |
| 728 | Py_DECREF(item); |
| 729 | return -1; |
| 730 | } |
| 731 | Py_DECREF(item); |
| 732 | } |
| 733 | Py_DECREF(it); |
| 734 | fprintf(fp, "])"); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 735 | if (PyErr_Occurred()) |
| 736 | return -1; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | static PyObject * |
| 741 | set_clear(PySetObject *so) |
| 742 | { |
| 743 | PyDict_Clear(so->data); |
| 744 | so->hash = -1; |
| 745 | Py_INCREF(Py_None); |
| 746 | return Py_None; |
| 747 | } |
| 748 | |
| 749 | PyDoc_STRVAR(clear_doc, "Remove all elements from this set."); |
| 750 | |
| 751 | static int |
| 752 | set_tp_clear(PySetObject *so) |
| 753 | { |
| 754 | PyDict_Clear(so->data); |
| 755 | so->hash = -1; |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | static PyObject * |
| 760 | set_add(PySetObject *so, PyObject *item) |
| 761 | { |
| 762 | if (PyDict_SetItem(so->data, item, Py_True) == -1) |
| 763 | return NULL; |
| 764 | Py_INCREF(Py_None); |
| 765 | return Py_None; |
| 766 | } |
| 767 | |
| 768 | PyDoc_STRVAR(add_doc, |
| 769 | "Add an element to a set.\n\ |
| 770 | \n\ |
| 771 | This has no effect if the element is already present."); |
| 772 | |
| 773 | static PyObject * |
| 774 | set_remove(PySetObject *so, PyObject *item) |
| 775 | { |
| 776 | if (PyDict_DelItem(so->data, item) == -1) |
| 777 | return NULL; |
| 778 | Py_INCREF(Py_None); |
| 779 | return Py_None; |
| 780 | } |
| 781 | |
| 782 | PyDoc_STRVAR(remove_doc, |
| 783 | "Remove an element from a set; it must be a member.\n\ |
| 784 | \n\ |
| 785 | If the element is not a member, raise a KeyError."); |
| 786 | |
| 787 | static PyObject * |
| 788 | set_discard(PySetObject *so, PyObject *item) |
| 789 | { |
| 790 | if (PyDict_DelItem(so->data, item) == -1) |
| 791 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 792 | PyErr_Clear(); |
| 793 | else |
| 794 | return NULL; |
| 795 | Py_INCREF(Py_None); |
| 796 | return Py_None; |
| 797 | } |
| 798 | |
| 799 | PyDoc_STRVAR(discard_doc, |
| 800 | "Remove an element from a set if it is a member.\n\ |
| 801 | \n\ |
| 802 | If the element is not a member, do nothing."); |
| 803 | |
| 804 | static PyObject * |
| 805 | set_pop(PySetObject *so) |
| 806 | { |
| 807 | PyObject *key, *value; |
| 808 | int pos = 0; |
| 809 | |
| 810 | if (!PyDict_Next(so->data, &pos, &key, &value)) { |
| 811 | PyErr_SetString(PyExc_KeyError, "pop from an empty set"); |
| 812 | return NULL; |
| 813 | } |
| 814 | Py_INCREF(key); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 815 | if (PyDict_DelItem(so->data, key) == -1) { |
| 816 | Py_DECREF(key); |
| 817 | return NULL; |
| 818 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 819 | return key; |
| 820 | } |
| 821 | |
| 822 | PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element."); |
| 823 | |
| 824 | static PyObject * |
| 825 | set_reduce(PySetObject *so) |
| 826 | { |
| 827 | PyObject *keys=NULL, *args=NULL, *result=NULL; |
| 828 | |
| 829 | keys = PyDict_Keys(so->data); |
| 830 | if (keys == NULL) |
| 831 | goto done; |
| 832 | args = PyTuple_Pack(1, keys); |
| 833 | if (args == NULL) |
| 834 | goto done; |
| 835 | result = PyTuple_Pack(2, so->ob_type, args); |
| 836 | done: |
| 837 | Py_XDECREF(args); |
| 838 | Py_XDECREF(keys); |
| 839 | return result; |
| 840 | } |
| 841 | |
| 842 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 843 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 844 | static int |
| 845 | set_init(PySetObject *self, PyObject *args, PyObject *kwds) |
| 846 | { |
| 847 | PyObject *iterable = NULL; |
| 848 | PyObject *result; |
| 849 | |
| 850 | if (!PyAnySet_Check(self)) |
| 851 | return -1; |
| 852 | if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable)) |
| 853 | return -1; |
| 854 | PyDict_Clear(self->data); |
| 855 | self->hash = -1; |
| 856 | if (iterable == NULL) |
| 857 | return 0; |
| 858 | result = set_union_update(self, iterable); |
| 859 | if (result != NULL) { |
| 860 | Py_DECREF(result); |
| 861 | return 0; |
| 862 | } |
| 863 | return -1; |
| 864 | } |
| 865 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 866 | static PySequenceMethods set_as_sequence = { |
| 867 | (inquiry)set_len, /* sq_length */ |
| 868 | 0, /* sq_concat */ |
| 869 | 0, /* sq_repeat */ |
| 870 | 0, /* sq_item */ |
| 871 | 0, /* sq_slice */ |
| 872 | 0, /* sq_ass_item */ |
| 873 | 0, /* sq_ass_slice */ |
| 874 | (objobjproc)set_contains, /* sq_contains */ |
| 875 | }; |
| 876 | |
| 877 | /* set object ********************************************************/ |
| 878 | |
| 879 | static PyMethodDef set_methods[] = { |
| 880 | {"add", (PyCFunction)set_add, METH_O, |
| 881 | add_doc}, |
| 882 | {"clear", (PyCFunction)set_clear, METH_NOARGS, |
| 883 | clear_doc}, |
| 884 | {"copy", (PyCFunction)set_copy, METH_NOARGS, |
| 885 | copy_doc}, |
| 886 | {"__copy__", (PyCFunction)set_copy, METH_NOARGS, |
| 887 | copy_doc}, |
| 888 | {"discard", (PyCFunction)set_discard, METH_O, |
| 889 | discard_doc}, |
| 890 | {"difference", (PyCFunction)set_difference, METH_O, |
| 891 | difference_doc}, |
| 892 | {"difference_update", (PyCFunction)set_difference_update, METH_O, |
| 893 | difference_update_doc}, |
| 894 | {"intersection",(PyCFunction)set_intersection, METH_O, |
| 895 | intersection_doc}, |
| 896 | {"intersection_update",(PyCFunction)set_intersection_update, METH_O, |
| 897 | intersection_update_doc}, |
| 898 | {"issubset", (PyCFunction)set_issubset, METH_O, |
| 899 | issubset_doc}, |
| 900 | {"issuperset", (PyCFunction)set_issuperset, METH_O, |
| 901 | issuperset_doc}, |
| 902 | {"pop", (PyCFunction)set_pop, METH_NOARGS, |
| 903 | pop_doc}, |
| 904 | {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS, |
| 905 | reduce_doc}, |
| 906 | {"remove", (PyCFunction)set_remove, METH_O, |
| 907 | remove_doc}, |
| 908 | {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O, |
| 909 | symmetric_difference_doc}, |
| 910 | {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O, |
| 911 | symmetric_difference_update_doc}, |
| 912 | {"union", (PyCFunction)set_union, METH_O, |
| 913 | union_doc}, |
| 914 | {"union_update",(PyCFunction)set_union_update, METH_O, |
| 915 | union_update_doc}, |
| 916 | {NULL, NULL} /* sentinel */ |
| 917 | }; |
| 918 | |
| 919 | static PyNumberMethods set_as_number = { |
| 920 | 0, /*nb_add*/ |
| 921 | (binaryfunc)set_sub, /*nb_subtract*/ |
| 922 | 0, /*nb_multiply*/ |
| 923 | 0, /*nb_divide*/ |
| 924 | 0, /*nb_remainder*/ |
| 925 | 0, /*nb_divmod*/ |
| 926 | 0, /*nb_power*/ |
| 927 | 0, /*nb_negative*/ |
| 928 | 0, /*nb_positive*/ |
| 929 | 0, /*nb_absolute*/ |
| 930 | 0, /*nb_nonzero*/ |
| 931 | 0, /*nb_invert*/ |
| 932 | 0, /*nb_lshift*/ |
| 933 | 0, /*nb_rshift*/ |
| 934 | (binaryfunc)set_and, /*nb_and*/ |
| 935 | (binaryfunc)set_xor, /*nb_xor*/ |
| 936 | (binaryfunc)set_or, /*nb_or*/ |
| 937 | 0, /*nb_coerce*/ |
| 938 | 0, /*nb_int*/ |
| 939 | 0, /*nb_long*/ |
| 940 | 0, /*nb_float*/ |
| 941 | 0, /*nb_oct*/ |
| 942 | 0, /*nb_hex*/ |
| 943 | 0, /*nb_inplace_add*/ |
| 944 | (binaryfunc)set_isub, /*nb_inplace_subtract*/ |
| 945 | 0, /*nb_inplace_multiply*/ |
| 946 | 0, /*nb_inplace_divide*/ |
| 947 | 0, /*nb_inplace_remainder*/ |
| 948 | 0, /*nb_inplace_power*/ |
| 949 | 0, /*nb_inplace_lshift*/ |
| 950 | 0, /*nb_inplace_rshift*/ |
| 951 | (binaryfunc)set_iand, /*nb_inplace_and*/ |
| 952 | (binaryfunc)set_ixor, /*nb_inplace_xor*/ |
| 953 | (binaryfunc)set_ior, /*nb_inplace_or*/ |
| 954 | }; |
| 955 | |
| 956 | PyDoc_STRVAR(set_doc, |
| 957 | "set(iterable) --> set object\n\ |
| 958 | \n\ |
| 959 | Build an unordered collection."); |
| 960 | |
| 961 | PyTypeObject PySet_Type = { |
| 962 | PyObject_HEAD_INIT(&PyType_Type) |
| 963 | 0, /* ob_size */ |
| 964 | "set", /* tp_name */ |
| 965 | sizeof(PySetObject), /* tp_basicsize */ |
| 966 | 0, /* tp_itemsize */ |
| 967 | /* methods */ |
| 968 | (destructor)set_dealloc, /* tp_dealloc */ |
| 969 | (printfunc)set_tp_print, /* tp_print */ |
| 970 | 0, /* tp_getattr */ |
| 971 | 0, /* tp_setattr */ |
| 972 | (cmpfunc)set_nocmp, /* tp_compare */ |
| 973 | (reprfunc)set_repr, /* tp_repr */ |
| 974 | &set_as_number, /* tp_as_number */ |
| 975 | &set_as_sequence, /* tp_as_sequence */ |
| 976 | 0, /* tp_as_mapping */ |
| 977 | set_nohash, /* tp_hash */ |
| 978 | 0, /* tp_call */ |
| 979 | 0, /* tp_str */ |
| 980 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 981 | 0, /* tp_setattro */ |
| 982 | 0, /* tp_as_buffer */ |
| 983 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES | |
| 984 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 985 | set_doc, /* tp_doc */ |
| 986 | (traverseproc)set_traverse, /* tp_traverse */ |
| 987 | (inquiry)set_tp_clear, /* tp_clear */ |
| 988 | (richcmpfunc)set_richcompare, /* tp_richcompare */ |
| 989 | 0, /* tp_weaklistoffset */ |
| 990 | (getiterfunc)set_iter, /* tp_iter */ |
| 991 | 0, /* tp_iternext */ |
| 992 | set_methods, /* tp_methods */ |
| 993 | 0, /* tp_members */ |
| 994 | 0, /* tp_getset */ |
| 995 | 0, /* tp_base */ |
| 996 | 0, /* tp_dict */ |
| 997 | 0, /* tp_descr_get */ |
| 998 | 0, /* tp_descr_set */ |
| 999 | 0, /* tp_dictoffset */ |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 1000 | (initproc)set_init, /* tp_init */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1001 | PyType_GenericAlloc, /* tp_alloc */ |
| 1002 | set_new, /* tp_new */ |
| 1003 | PyObject_GC_Del, /* tp_free */ |
| 1004 | }; |
| 1005 | |
| 1006 | /* frozenset object ********************************************************/ |
| 1007 | |
| 1008 | |
| 1009 | static PyMethodDef frozenset_methods[] = { |
| 1010 | {"copy", (PyCFunction)set_copy, METH_NOARGS, |
| 1011 | copy_doc}, |
| 1012 | {"__copy__", (PyCFunction)set_copy, METH_NOARGS, |
| 1013 | copy_doc}, |
| 1014 | {"difference",(PyCFunction)set_difference, METH_O, |
| 1015 | difference_doc}, |
| 1016 | {"intersection",(PyCFunction)set_intersection, METH_O, |
| 1017 | intersection_doc}, |
| 1018 | {"issubset",(PyCFunction)set_issubset, METH_O, |
| 1019 | issubset_doc}, |
| 1020 | {"issuperset",(PyCFunction)set_issuperset, METH_O, |
| 1021 | issuperset_doc}, |
| 1022 | {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS, |
| 1023 | reduce_doc}, |
| 1024 | {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O, |
| 1025 | symmetric_difference_doc}, |
| 1026 | {"union", (PyCFunction)set_union, METH_O, |
| 1027 | union_doc}, |
| 1028 | {NULL, NULL} /* sentinel */ |
| 1029 | }; |
| 1030 | |
| 1031 | static PyNumberMethods frozenset_as_number = { |
| 1032 | 0, /*nb_add*/ |
| 1033 | (binaryfunc)set_sub, /*nb_subtract*/ |
| 1034 | 0, /*nb_multiply*/ |
| 1035 | 0, /*nb_divide*/ |
| 1036 | 0, /*nb_remainder*/ |
| 1037 | 0, /*nb_divmod*/ |
| 1038 | 0, /*nb_power*/ |
| 1039 | 0, /*nb_negative*/ |
| 1040 | 0, /*nb_positive*/ |
| 1041 | 0, /*nb_absolute*/ |
| 1042 | 0, /*nb_nonzero*/ |
| 1043 | 0, /*nb_invert*/ |
| 1044 | 0, /*nb_lshift*/ |
| 1045 | 0, /*nb_rshift*/ |
| 1046 | (binaryfunc)set_and, /*nb_and*/ |
| 1047 | (binaryfunc)set_xor, /*nb_xor*/ |
| 1048 | (binaryfunc)set_or, /*nb_or*/ |
| 1049 | }; |
| 1050 | |
| 1051 | PyDoc_STRVAR(frozenset_doc, |
| 1052 | "frozenset(iterable) --> frozenset object\n\ |
| 1053 | \n\ |
| 1054 | Build an immutable unordered collection."); |
| 1055 | |
| 1056 | PyTypeObject PyFrozenSet_Type = { |
| 1057 | PyObject_HEAD_INIT(&PyType_Type) |
| 1058 | 0, /* ob_size */ |
| 1059 | "frozenset", /* tp_name */ |
| 1060 | sizeof(PySetObject), /* tp_basicsize */ |
| 1061 | 0, /* tp_itemsize */ |
| 1062 | /* methods */ |
| 1063 | (destructor)set_dealloc, /* tp_dealloc */ |
| 1064 | (printfunc)set_tp_print, /* tp_print */ |
| 1065 | 0, /* tp_getattr */ |
| 1066 | 0, /* tp_setattr */ |
| 1067 | (cmpfunc)set_nocmp, /* tp_compare */ |
| 1068 | (reprfunc)set_repr, /* tp_repr */ |
| 1069 | &frozenset_as_number, /* tp_as_number */ |
| 1070 | &set_as_sequence, /* tp_as_sequence */ |
| 1071 | 0, /* tp_as_mapping */ |
| 1072 | frozenset_hash, /* tp_hash */ |
| 1073 | 0, /* tp_call */ |
| 1074 | 0, /* tp_str */ |
| 1075 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1076 | 0, /* tp_setattro */ |
| 1077 | 0, /* tp_as_buffer */ |
| 1078 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES | |
| 1079 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1080 | frozenset_doc, /* tp_doc */ |
| 1081 | (traverseproc)set_traverse, /* tp_traverse */ |
| 1082 | 0, /* tp_clear */ |
| 1083 | (richcmpfunc)set_richcompare, /* tp_richcompare */ |
| 1084 | 0, /* tp_weaklistoffset */ |
| 1085 | (getiterfunc)set_iter, /* tp_iter */ |
| 1086 | 0, /* tp_iternext */ |
| 1087 | frozenset_methods, /* tp_methods */ |
| 1088 | 0, /* tp_members */ |
| 1089 | 0, /* tp_getset */ |
| 1090 | 0, /* tp_base */ |
| 1091 | 0, /* tp_dict */ |
| 1092 | 0, /* tp_descr_get */ |
| 1093 | 0, /* tp_descr_set */ |
| 1094 | 0, /* tp_dictoffset */ |
| 1095 | 0, /* tp_init */ |
| 1096 | PyType_GenericAlloc, /* tp_alloc */ |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame^] | 1097 | frozenset_new, /* tp_new */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1098 | PyObject_GC_Del, /* tp_free */ |
| 1099 | }; |