| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include "Python.h" |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
static void |
| 17 |
set_key_error(PyObject *arg) |
| 18 |
{ |
| 19 |
PyObject *tup; |
| 20 |
tup = PyTuple_Pack(1, arg); |
| 21 |
if (!tup) |
| 22 |
return; |
| 23 |
PyErr_SetObject(PyExc_KeyError, tup); |
| 24 |
Py_DECREF(tup); |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
#undef SHOW_CONVERSION_COUNTS |
| 29 |
|
| 30 |
|
| 31 |
#define PERTURB_SHIFT 5 |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
static PyObject *dummy = NULL; |
| 139 |
|
| 140 |
#ifdef Py_REF_DEBUG |
| 141 |
PyObject * |
| 142 |
_PyDict_Dummy(void) |
| 143 |
{ |
| 144 |
return dummy; |
| 145 |
} |
| 146 |
#endif |
| 147 |
|
| 148 |
|
| 149 |
static PyDictEntry * |
| 150 |
lookdict_string(PyDictObject *mp, PyObject *key, long hash); |
| 151 |
|
| 152 |
#ifdef SHOW_CONVERSION_COUNTS |
| 153 |
static long created = 0L; |
| 154 |
static long converted = 0L; |
| 155 |
|
| 156 |
static void |
| 157 |
show_counts(void) |
| 158 |
{ |
| 159 |
fprintf(stderr, "created %ld string dicts\n", created); |
| 160 |
fprintf(stderr, "converted %ld to normal dicts\n", converted); |
| 161 |
fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created); |
| 162 |
} |
| 163 |
#endif |
| 164 |
|
| 165 |
|
| 166 |
#undef SHOW_ALLOC_COUNT |
| 167 |
#ifdef SHOW_ALLOC_COUNT |
| 168 |
static size_t count_alloc = 0; |
| 169 |
static size_t count_reuse = 0; |
| 170 |
|
| 171 |
static void |
| 172 |
show_alloc(void) |
| 173 |
{ |
| 174 |
fprintf(stderr, "Dict allocations: %" PY_FORMAT_SIZE_T "d\n", |
| 175 |
count_alloc); |
| 176 |
fprintf(stderr, "Dict reuse through freelist: %" PY_FORMAT_SIZE_T |
| 177 |
"d\n", count_reuse); |
| 178 |
fprintf(stderr, "%.2f%% reuse rate\n\n", |
| 179 |
(100.0*count_reuse/(count_alloc+count_reuse))); |
| 180 |
} |
| 181 |
#endif |
| 182 |
|
| 183 |
|
| 184 |
#ifdef SHOW_TRACK_COUNT |
| 185 |
static Py_ssize_t count_untracked = 0; |
| 186 |
static Py_ssize_t count_tracked = 0; |
| 187 |
|
| 188 |
static void |
| 189 |
show_track(void) |
| 190 |
{ |
| 191 |
fprintf(stderr, "Dicts created: %" PY_FORMAT_SIZE_T "d\n", |
| 192 |
count_tracked + count_untracked); |
| 193 |
fprintf(stderr, "Dicts tracked by the GC: %" PY_FORMAT_SIZE_T |
| 194 |
"d\n", count_tracked); |
| 195 |
fprintf(stderr, "%.2f%% dict tracking rate\n\n", |
| 196 |
(100.0*count_tracked/(count_untracked+count_tracked))); |
| 197 |
} |
| 198 |
#endif |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
#define INIT_NONZERO_DICT_SLOTS(mp) do { \ |
| 211 |
(mp)->ma_table = (mp)->ma_smalltable; \ |
| 212 |
(mp)->ma_mask = PyDict_MINSIZE - 1; \ |
| 213 |
} while(0) |
| 214 |
|
| 215 |
#define EMPTY_TO_MINSIZE(mp) do { \ |
| 216 |
memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \ |
| 217 |
(mp)->ma_used = (mp)->ma_fill = 0; \ |
| 218 |
INIT_NONZERO_DICT_SLOTS(mp); \ |
| 219 |
} while(0) |
| 220 |
|
| 221 |
|
| 222 |
#ifndef PyDict_MAXFREELIST |
| 223 |
#define PyDict_MAXFREELIST 80 |
| 224 |
#endif |
| 225 |
static PyDictObject *free_list[PyDict_MAXFREELIST]; |
| 226 |
static int numfree = 0; |
| 227 |
|
| 228 |
void |
| 229 |
PyDict_Fini(void) |
| 230 |
{ |
| 231 |
PyDictObject *op; |
| 232 |
|
| 233 |
while (numfree) { |
| 234 |
op = free_list[--numfree]; |
| 235 |
assert(PyDict_CheckExact(op)); |
| 236 |
PyObject_GC_Del(op); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
PyObject * |
| 241 |
PyDict_New(void) |
| 242 |
{ |
| 243 |
register PyDictObject *mp; |
| 244 |
if (dummy == NULL) { |
| 245 |
dummy = PyString_FromString("<dummy key>"); |
| 246 |
if (dummy == NULL) |
| 247 |
return NULL; |
| 248 |
#ifdef SHOW_CONVERSION_COUNTS |
| 249 |
Py_AtExit(show_counts); |
| 250 |
#endif |
| 251 |
#ifdef SHOW_ALLOC_COUNT |
| 252 |
Py_AtExit(show_alloc); |
| 253 |
#endif |
| 254 |
#ifdef SHOW_TRACK_COUNT |
| 255 |
Py_AtExit(show_track); |
| 256 |
#endif |
| 257 |
} |
| 258 |
if (numfree) { |
| 259 |
mp = free_list[--numfree]; |
| 260 |
assert (mp != NULL); |
| 261 |
assert (Py_TYPE(mp) == &PyDict_Type); |
| 262 |
_Py_NewReference((PyObject *)mp); |
| 263 |
if (mp->ma_fill) { |
| 264 |
EMPTY_TO_MINSIZE(mp); |
| 265 |
} else { |
| 266 |
|
| 267 |
|
| 268 |
INIT_NONZERO_DICT_SLOTS(mp); |
| 269 |
} |
| 270 |
assert (mp->ma_used == 0); |
| 271 |
assert (mp->ma_table == mp->ma_smalltable); |
| 272 |
assert (mp->ma_mask == PyDict_MINSIZE - 1); |
| 273 |
#ifdef SHOW_ALLOC_COUNT |
| 274 |
count_reuse++; |
| 275 |
#endif |
| 276 |
} else { |
| 277 |
mp = PyObject_GC_New(PyDictObject, &PyDict_Type); |
| 278 |
if (mp == NULL) |
| 279 |
return NULL; |
| 280 |
EMPTY_TO_MINSIZE(mp); |
| 281 |
#ifdef SHOW_ALLOC_COUNT |
| 282 |
count_alloc++; |
| 283 |
#endif |
| 284 |
} |
| 285 |
mp->ma_lookup = lookdict_string; |
| 286 |
#ifdef SHOW_TRACK_COUNT |
| 287 |
count_untracked++; |
| 288 |
#endif |
| 289 |
#ifdef SHOW_CONVERSION_COUNTS |
| 290 |
++created; |
| 291 |
#endif |
| 292 |
return (PyObject *)mp; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
static PyDictEntry * |
| 320 |
lookdict(PyDictObject *mp, PyObject *key, register long hash) |
| 321 |
{ |
| 322 |
register size_t i; |
| 323 |
register size_t perturb; |
| 324 |
register PyDictEntry *freeslot; |
| 325 |
register size_t mask = (size_t)mp->ma_mask; |
| 326 |
PyDictEntry *ep0 = mp->ma_table; |
| 327 |
register PyDictEntry *ep; |
| 328 |
register int cmp; |
| 329 |
PyObject *startkey; |
| 330 |
|
| 331 |
i = (size_t)hash & mask; |
| 332 |
ep = &ep0[i]; |
| 333 |
if (ep->me_key == NULL || ep->me_key == key) |
| 334 |
return ep; |
| 335 |
|
| 336 |
if (ep->me_key == dummy) |
| 337 |
freeslot = ep; |
| 338 |
else { |
| 339 |
if (ep->me_hash == hash) { |
| 340 |
startkey = ep->me_key; |
| 341 |
Py_INCREF(startkey); |
| 342 |
cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
| 343 |
Py_DECREF(startkey); |
| 344 |
if (cmp < 0) |
| 345 |
return NULL; |
| 346 |
if (ep0 == mp->ma_table && ep->me_key == startkey) { |
| 347 |
if (cmp > 0) |
| 348 |
return ep; |
| 349 |
} |
| 350 |
else { |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
return lookdict(mp, key, hash); |
| 357 |
} |
| 358 |
} |
| 359 |
freeslot = NULL; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 365 |
i = (i << 2) + i + perturb + 1; |
| 366 |
ep = &ep0[i & mask]; |
| 367 |
if (ep->me_key == NULL) |
| 368 |
return freeslot == NULL ? ep : freeslot; |
| 369 |
if (ep->me_key == key) |
| 370 |
return ep; |
| 371 |
if (ep->me_hash == hash && ep->me_key != dummy) { |
| 372 |
startkey = ep->me_key; |
| 373 |
Py_INCREF(startkey); |
| 374 |
cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
| 375 |
Py_DECREF(startkey); |
| 376 |
if (cmp < 0) |
| 377 |
return NULL; |
| 378 |
if (ep0 == mp->ma_table && ep->me_key == startkey) { |
| 379 |
if (cmp > 0) |
| 380 |
return ep; |
| 381 |
} |
| 382 |
else { |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
return lookdict(mp, key, hash); |
| 389 |
} |
| 390 |
} |
| 391 |
else if (ep->me_key == dummy && freeslot == NULL) |
| 392 |
freeslot = ep; |
| 393 |
} |
| 394 |
assert(0); |
| 395 |
return 0; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
static PyDictEntry * |
| 408 |
lookdict_string(PyDictObject *mp, PyObject *key, register long hash) |
| 409 |
{ |
| 410 |
register size_t i; |
| 411 |
register size_t perturb; |
| 412 |
register PyDictEntry *freeslot; |
| 413 |
register size_t mask = (size_t)mp->ma_mask; |
| 414 |
PyDictEntry *ep0 = mp->ma_table; |
| 415 |
register PyDictEntry *ep; |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
if (!PyString_CheckExact(key)) { |
| 422 |
#ifdef SHOW_CONVERSION_COUNTS |
| 423 |
++converted; |
| 424 |
#endif |
| 425 |
mp->ma_lookup = lookdict; |
| 426 |
return lookdict(mp, key, hash); |
| 427 |
} |
| 428 |
i = hash & mask; |
| 429 |
ep = &ep0[i]; |
| 430 |
if (ep->me_key == NULL || ep->me_key == key) |
| 431 |
return ep; |
| 432 |
if (ep->me_key == dummy) |
| 433 |
freeslot = ep; |
| 434 |
else { |
| 435 |
if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key)) |
| 436 |
return ep; |
| 437 |
freeslot = NULL; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 443 |
i = (i << 2) + i + perturb + 1; |
| 444 |
ep = &ep0[i & mask]; |
| 445 |
if (ep->me_key == NULL) |
| 446 |
return freeslot == NULL ? ep : freeslot; |
| 447 |
if (ep->me_key == key |
| 448 |
|| (ep->me_hash == hash |
| 449 |
&& ep->me_key != dummy |
| 450 |
&& _PyString_Eq(ep->me_key, key))) |
| 451 |
return ep; |
| 452 |
if (ep->me_key == dummy && freeslot == NULL) |
| 453 |
freeslot = ep; |
| 454 |
} |
| 455 |
assert(0); |
| 456 |
return 0; |
| 457 |
} |
| 458 |
|
| 459 |
#ifdef SHOW_TRACK_COUNT |
| 460 |
#define INCREASE_TRACK_COUNT \ |
| 461 |
(count_tracked++, count_untracked--); |
| 462 |
#define DECREASE_TRACK_COUNT \ |
| 463 |
(count_tracked--, count_untracked++); |
| 464 |
#else |
| 465 |
#define INCREASE_TRACK_COUNT |
| 466 |
#define DECREASE_TRACK_COUNT |
| 467 |
#endif |
| 468 |
|
| 469 |
#define MAINTAIN_TRACKING(mp, key, value) \ |
| 470 |
do { \ |
| 471 |
if (!_PyObject_GC_IS_TRACKED(mp)) { \ |
| 472 |
if (_PyObject_GC_MAY_BE_TRACKED(key) || \ |
| 473 |
_PyObject_GC_MAY_BE_TRACKED(value)) { \ |
| 474 |
_PyObject_GC_TRACK(mp); \ |
| 475 |
INCREASE_TRACK_COUNT \ |
| 476 |
} \ |
| 477 |
} \ |
| 478 |
} while(0) |
| 479 |
|
| 480 |
void |
| 481 |
_PyDict_MaybeUntrack(PyObject *op) |
| 482 |
{ |
| 483 |
PyDictObject *mp; |
| 484 |
PyObject *value; |
| 485 |
Py_ssize_t mask, i; |
| 486 |
PyDictEntry *ep; |
| 487 |
|
| 488 |
if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op)) |
| 489 |
return; |
| 490 |
|
| 491 |
mp = (PyDictObject *) op; |
| 492 |
ep = mp->ma_table; |
| 493 |
mask = mp->ma_mask; |
| 494 |
for (i = 0; i <= mask; i++) { |
| 495 |
if ((value = ep[i].me_value) == NULL) |
| 496 |
continue; |
| 497 |
if (_PyObject_GC_MAY_BE_TRACKED(value) || |
| 498 |
_PyObject_GC_MAY_BE_TRACKED(ep[i].me_key)) |
| 499 |
return; |
| 500 |
} |
| 501 |
DECREASE_TRACK_COUNT |
| 502 |
_PyObject_GC_UNTRACK(op); |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
static int |
| 510 |
insertdict_by_entry(register PyDictObject *mp, PyObject *key, long hash, |
| 511 |
PyDictEntry *ep, PyObject *value) |
| 512 |
{ |
| 513 |
PyObject *old_value; |
| 514 |
|
| 515 |
MAINTAIN_TRACKING(mp, key, value); |
| 516 |
if (ep->me_value != NULL) { |
| 517 |
old_value = ep->me_value; |
| 518 |
ep->me_value = value; |
| 519 |
Py_DECREF(old_value); |
| 520 |
Py_DECREF(key); |
| 521 |
} |
| 522 |
else { |
| 523 |
if (ep->me_key == NULL) |
| 524 |
mp->ma_fill++; |
| 525 |
else { |
| 526 |
assert(ep->me_key == dummy); |
| 527 |
Py_DECREF(dummy); |
| 528 |
} |
| 529 |
ep->me_key = key; |
| 530 |
ep->me_hash = (Py_ssize_t)hash; |
| 531 |
ep->me_value = value; |
| 532 |
mp->ma_used++; |
| 533 |
} |
| 534 |
return 0; |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
static int |
| 545 |
insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value) |
| 546 |
{ |
| 547 |
register PyDictEntry *ep; |
| 548 |
|
| 549 |
assert(mp->ma_lookup != NULL); |
| 550 |
ep = mp->ma_lookup(mp, key, hash); |
| 551 |
if (ep == NULL) { |
| 552 |
Py_DECREF(key); |
| 553 |
Py_DECREF(value); |
| 554 |
return -1; |
| 555 |
} |
| 556 |
return insertdict_by_entry(mp, key, hash, ep, value); |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
static void |
| 568 |
insertdict_clean(register PyDictObject *mp, PyObject *key, long hash, |
| 569 |
PyObject *value) |
| 570 |
{ |
| 571 |
register size_t i; |
| 572 |
register size_t perturb; |
| 573 |
register size_t mask = (size_t)mp->ma_mask; |
| 574 |
PyDictEntry *ep0 = mp->ma_table; |
| 575 |
register PyDictEntry *ep; |
| 576 |
|
| 577 |
MAINTAIN_TRACKING(mp, key, value); |
| 578 |
i = hash & mask; |
| 579 |
ep = &ep0[i]; |
| 580 |
for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) { |
| 581 |
i = (i << 2) + i + perturb + 1; |
| 582 |
ep = &ep0[i & mask]; |
| 583 |
} |
| 584 |
assert(ep->me_value == NULL); |
| 585 |
mp->ma_fill++; |
| 586 |
ep->me_key = key; |
| 587 |
ep->me_hash = (Py_ssize_t)hash; |
| 588 |
ep->me_value = value; |
| 589 |
mp->ma_used++; |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
static int |
| 598 |
dictresize(PyDictObject *mp, Py_ssize_t minused) |
| 599 |
{ |
| 600 |
Py_ssize_t newsize; |
| 601 |
PyDictEntry *oldtable, *newtable, *ep; |
| 602 |
Py_ssize_t i; |
| 603 |
int is_oldtable_malloced; |
| 604 |
PyDictEntry small_copy[PyDict_MINSIZE]; |
| 605 |
|
| 606 |
assert(minused >= 0); |
| 607 |
|
| 608 |
|
| 609 |
for (newsize = PyDict_MINSIZE; |
| 610 |
newsize <= minused && newsize > 0; |
| 611 |
newsize <<= 1) |
| 612 |
; |
| 613 |
if (newsize <= 0) { |
| 614 |
PyErr_NoMemory(); |
| 615 |
return -1; |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
oldtable = mp->ma_table; |
| 620 |
assert(oldtable != NULL); |
| 621 |
is_oldtable_malloced = oldtable != mp->ma_smalltable; |
| 622 |
|
| 623 |
if (newsize == PyDict_MINSIZE) { |
| 624 |
|
| 625 |
newtable = mp->ma_smalltable; |
| 626 |
if (newtable == oldtable) { |
| 627 |
if (mp->ma_fill == mp->ma_used) { |
| 628 |
|
| 629 |
return 0; |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
assert(mp->ma_fill > mp->ma_used); |
| 638 |
memcpy(small_copy, oldtable, sizeof(small_copy)); |
| 639 |
oldtable = small_copy; |
| 640 |
} |
| 641 |
} |
| 642 |
else { |
| 643 |
newtable = PyMem_NEW(PyDictEntry, newsize); |
| 644 |
if (newtable == NULL) { |
| 645 |
PyErr_NoMemory(); |
| 646 |
return -1; |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
assert(newtable != oldtable); |
| 652 |
mp->ma_table = newtable; |
| 653 |
mp->ma_mask = newsize - 1; |
| 654 |
memset(newtable, 0, sizeof(PyDictEntry) * newsize); |
| 655 |
mp->ma_used = 0; |
| 656 |
i = mp->ma_fill; |
| 657 |
mp->ma_fill = 0; |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
for (ep = oldtable; i > 0; ep++) { |
| 662 |
if (ep->me_value != NULL) { |
| 663 |
--i; |
| 664 |
insertdict_clean(mp, ep->me_key, (long)ep->me_hash, |
| 665 |
ep->me_value); |
| 666 |
} |
| 667 |
else if (ep->me_key != NULL) { |
| 668 |
--i; |
| 669 |
assert(ep->me_key == dummy); |
| 670 |
Py_DECREF(ep->me_key); |
| 671 |
} |
| 672 |
|
| 673 |
} |
| 674 |
|
| 675 |
if (is_oldtable_malloced) |
| 676 |
PyMem_DEL(oldtable); |
| 677 |
return 0; |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
PyObject * |
| 686 |
_PyDict_NewPresized(Py_ssize_t minused) |
| 687 |
{ |
| 688 |
PyObject *op = PyDict_New(); |
| 689 |
|
| 690 |
if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) { |
| 691 |
Py_DECREF(op); |
| 692 |
return NULL; |
| 693 |
} |
| 694 |
return op; |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
PyObject * |
| 708 |
PyDict_GetItem(PyObject *op, PyObject *key) |
| 709 |
{ |
| 710 |
long hash; |
| 711 |
PyDictObject *mp = (PyDictObject *)op; |
| 712 |
PyDictEntry *ep; |
| 713 |
PyThreadState *tstate; |
| 714 |
if (!PyDict_Check(op)) |
| 715 |
return NULL; |
| 716 |
if (!PyString_CheckExact(key) || |
| 717 |
(hash = ((PyStringObject *) key)->ob_shash) == -1) |
| 718 |
{ |
| 719 |
hash = PyObject_Hash(key); |
| 720 |
if (hash == -1) { |
| 721 |
PyErr_Clear(); |
| 722 |
return NULL; |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
tstate = _PyThreadState_Current; |
| 732 |
if (tstate != NULL && tstate->curexc_type != NULL) { |
| 733 |
|
| 734 |
PyObject *err_type, *err_value, *err_tb; |
| 735 |
PyErr_Fetch(&err_type, &err_value, &err_tb); |
| 736 |
ep = (mp->ma_lookup)(mp, key, hash); |
| 737 |
|
| 738 |
PyErr_Restore(err_type, err_value, err_tb); |
| 739 |
if (ep == NULL) |
| 740 |
return NULL; |
| 741 |
} |
| 742 |
else { |
| 743 |
ep = (mp->ma_lookup)(mp, key, hash); |
| 744 |
if (ep == NULL) { |
| 745 |
PyErr_Clear(); |
| 746 |
return NULL; |
| 747 |
} |
| 748 |
} |
| 749 |
return ep->me_value; |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
PyObject * |
| 757 |
_PyDict_GetItemWithError(PyObject *op, PyObject *key) |
| 758 |
{ |
| 759 |
long hash; |
| 760 |
PyDictObject *mp = (PyDictObject *)op; |
| 761 |
PyDictEntry *ep; |
| 762 |
if (!PyDict_Check(op)) { |
| 763 |
PyErr_BadInternalCall(); |
| 764 |
return NULL; |
| 765 |
} |
| 766 |
if (!PyString_CheckExact(key) || |
| 767 |
(hash = ((PyStringObject *) key)->ob_shash) == -1) |
| 768 |
{ |
| 769 |
hash = PyObject_Hash(key); |
| 770 |
if (hash == -1) { |
| 771 |
return NULL; |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
ep = (mp->ma_lookup)(mp, key, hash); |
| 776 |
if (ep == NULL) { |
| 777 |
return NULL; |
| 778 |
} |
| 779 |
return ep->me_value; |
| 780 |
} |
| 781 |
|
| 782 |
static int |
| 783 |
dict_set_item_by_hash_or_entry(register PyObject *op, PyObject *key, |
| 784 |
long hash, PyDictEntry *ep, PyObject *value) |
| 785 |
{ |
| 786 |
register PyDictObject *mp; |
| 787 |
register Py_ssize_t n_used; |
| 788 |
|
| 789 |
mp = (PyDictObject *)op; |
| 790 |
assert(mp->ma_fill <= mp->ma_mask); |
| 791 |
n_used = mp->ma_used; |
| 792 |
Py_INCREF(value); |
| 793 |
Py_INCREF(key); |
| 794 |
if (ep == NULL) { |
| 795 |
if (insertdict(mp, key, hash, value) != 0) |
| 796 |
return -1; |
| 797 |
} |
| 798 |
else { |
| 799 |
if (insertdict_by_entry(mp, key, hash, ep, value) != 0) |
| 800 |
return -1; |
| 801 |
} |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2)) |
| 817 |
return 0; |
| 818 |
return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used); |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
int |
| 828 |
PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value) |
| 829 |
{ |
| 830 |
register long hash; |
| 831 |
|
| 832 |
if (!PyDict_Check(op)) { |
| 833 |
PyErr_BadInternalCall(); |
| 834 |
return -1; |
| 835 |
} |
| 836 |
assert(key); |
| 837 |
assert(value); |
| 838 |
if (PyString_CheckExact(key)) { |
| 839 |
hash = ((PyStringObject *)key)->ob_shash; |
| 840 |
if (hash == -1) |
| 841 |
hash = PyObject_Hash(key); |
| 842 |
} |
| 843 |
else { |
| 844 |
hash = PyObject_Hash(key); |
| 845 |
if (hash == -1) |
| 846 |
return -1; |
| 847 |
} |
| 848 |
return dict_set_item_by_hash_or_entry(op, key, hash, NULL, value); |
| 849 |
} |
| 850 |
|
| 851 |
static int |
| 852 |
delitem_common(PyDictObject *mp, PyDictEntry *ep) |
| 853 |
{ |
| 854 |
PyObject *old_value, *old_key; |
| 855 |
|
| 856 |
old_key = ep->me_key; |
| 857 |
Py_INCREF(dummy); |
| 858 |
ep->me_key = dummy; |
| 859 |
old_value = ep->me_value; |
| 860 |
ep->me_value = NULL; |
| 861 |
mp->ma_used--; |
| 862 |
Py_DECREF(old_value); |
| 863 |
Py_DECREF(old_key); |
| 864 |
return 0; |
| 865 |
} |
| 866 |
|
| 867 |
int |
| 868 |
PyDict_DelItem(PyObject *op, PyObject *key) |
| 869 |
{ |
| 870 |
register PyDictObject *mp; |
| 871 |
register long hash; |
| 872 |
register PyDictEntry *ep; |
| 873 |
|
| 874 |
if (!PyDict_Check(op)) { |
| 875 |
PyErr_BadInternalCall(); |
| 876 |
return -1; |
| 877 |
} |
| 878 |
assert(key); |
| 879 |
if (!PyString_CheckExact(key) || |
| 880 |
(hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 881 |
hash = PyObject_Hash(key); |
| 882 |
if (hash == -1) |
| 883 |
return -1; |
| 884 |
} |
| 885 |
mp = (PyDictObject *)op; |
| 886 |
ep = (mp->ma_lookup)(mp, key, hash); |
| 887 |
if (ep == NULL) |
| 888 |
return -1; |
| 889 |
if (ep->me_value == NULL) { |
| 890 |
set_key_error(key); |
| 891 |
return -1; |
| 892 |
} |
| 893 |
|
| 894 |
return delitem_common(mp, ep); |
| 895 |
} |
| 896 |
|
| 897 |
int |
| 898 |
_PyDict_DelItemIf(PyObject *op, PyObject *key, |
| 899 |
int (*predicate)(PyObject *value)) |
| 900 |
{ |
| 901 |
register PyDictObject *mp; |
| 902 |
register long hash; |
| 903 |
register PyDictEntry *ep; |
| 904 |
int res; |
| 905 |
|
| 906 |
if (!PyDict_Check(op)) { |
| 907 |
PyErr_BadInternalCall(); |
| 908 |
return -1; |
| 909 |
} |
| 910 |
assert(key); |
| 911 |
if (!PyString_CheckExact(key) || |
| 912 |
(hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 913 |
hash = PyObject_Hash(key); |
| 914 |
if (hash == -1) |
| 915 |
return -1; |
| 916 |
} |
| 917 |
mp = (PyDictObject *)op; |
| 918 |
ep = (mp->ma_lookup)(mp, key, hash); |
| 919 |
if (ep == NULL) |
| 920 |
return -1; |
| 921 |
if (ep->me_value == NULL) { |
| 922 |
set_key_error(key); |
| 923 |
return -1; |
| 924 |
} |
| 925 |
res = predicate(ep->me_value); |
| 926 |
if (res == -1) |
| 927 |
return -1; |
| 928 |
if (res > 0) |
| 929 |
return delitem_common(mp, ep); |
| 930 |
else |
| 931 |
return 0; |
| 932 |
} |
| 933 |
|
| 934 |
void |
| 935 |
PyDict_Clear(PyObject *op) |
| 936 |
{ |
| 937 |
PyDictObject *mp; |
| 938 |
PyDictEntry *ep, *table; |
| 939 |
int table_is_malloced; |
| 940 |
Py_ssize_t fill; |
| 941 |
PyDictEntry small_copy[PyDict_MINSIZE]; |
| 942 |
#ifdef Py_DEBUG |
| 943 |
Py_ssize_t i, n; |
| 944 |
#endif |
| 945 |
|
| 946 |
if (!PyDict_Check(op)) |
| 947 |
return; |
| 948 |
mp = (PyDictObject *)op; |
| 949 |
#ifdef Py_DEBUG |
| 950 |
n = mp->ma_mask + 1; |
| 951 |
i = 0; |
| 952 |
#endif |
| 953 |
|
| 954 |
table = mp->ma_table; |
| 955 |
assert(table != NULL); |
| 956 |
table_is_malloced = table != mp->ma_smalltable; |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
fill = mp->ma_fill; |
| 965 |
if (table_is_malloced) |
| 966 |
EMPTY_TO_MINSIZE(mp); |
| 967 |
|
| 968 |
else if (fill > 0) { |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
memcpy(small_copy, table, sizeof(small_copy)); |
| 974 |
table = small_copy; |
| 975 |
EMPTY_TO_MINSIZE(mp); |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
for (ep = table; fill > 0; ++ep) { |
| 984 |
#ifdef Py_DEBUG |
| 985 |
assert(i < n); |
| 986 |
++i; |
| 987 |
#endif |
| 988 |
if (ep->me_key) { |
| 989 |
--fill; |
| 990 |
Py_DECREF(ep->me_key); |
| 991 |
Py_XDECREF(ep->me_value); |
| 992 |
} |
| 993 |
#ifdef Py_DEBUG |
| 994 |
else |
| 995 |
assert(ep->me_value == NULL); |
| 996 |
#endif |
| 997 |
} |
| 998 |
|
| 999 |
if (table_is_malloced) |
| 1000 |
PyMem_DEL(table); |
| 1001 |
} |
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
int |
| 1019 |
PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue) |
| 1020 |
{ |
| 1021 |
register Py_ssize_t i; |
| 1022 |
register Py_ssize_t mask; |
| 1023 |
register PyDictEntry *ep; |
| 1024 |
|
| 1025 |
if (!PyDict_Check(op)) |
| 1026 |
return 0; |
| 1027 |
i = *ppos; |
| 1028 |
if (i < 0) |
| 1029 |
return 0; |
| 1030 |
ep = ((PyDictObject *)op)->ma_table; |
| 1031 |
mask = ((PyDictObject *)op)->ma_mask; |
| 1032 |
while (i <= mask && ep[i].me_value == NULL) |
| 1033 |
i++; |
| 1034 |
*ppos = i+1; |
| 1035 |
if (i > mask) |
| 1036 |
return 0; |
| 1037 |
if (pkey) |
| 1038 |
*pkey = ep[i].me_key; |
| 1039 |
if (pvalue) |
| 1040 |
*pvalue = ep[i].me_value; |
| 1041 |
return 1; |
| 1042 |
} |
| 1043 |
|
| 1044 |
|
| 1045 |
int |
| 1046 |
_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash) |
| 1047 |
{ |
| 1048 |
register Py_ssize_t i; |
| 1049 |
register Py_ssize_t mask; |
| 1050 |
register PyDictEntry *ep; |
| 1051 |
|
| 1052 |
if (!PyDict_Check(op)) |
| 1053 |
return 0; |
| 1054 |
i = *ppos; |
| 1055 |
if (i < 0) |
| 1056 |
return 0; |
| 1057 |
ep = ((PyDictObject *)op)->ma_table; |
| 1058 |
mask = ((PyDictObject *)op)->ma_mask; |
| 1059 |
while (i <= mask && ep[i].me_value == NULL) |
| 1060 |
i++; |
| 1061 |
*ppos = i+1; |
| 1062 |
if (i > mask) |
| 1063 |
return 0; |
| 1064 |
*phash = (long)(ep[i].me_hash); |
| 1065 |
if (pkey) |
| 1066 |
*pkey = ep[i].me_key; |
| 1067 |
if (pvalue) |
| 1068 |
*pvalue = ep[i].me_value; |
| 1069 |
return 1; |
| 1070 |
} |
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
static void |
| 1075 |
dict_dealloc(register PyDictObject *mp) |
| 1076 |
{ |
| 1077 |
register PyDictEntry *ep; |
| 1078 |
Py_ssize_t fill = mp->ma_fill; |
| 1079 |
|
| 1080 |
PyObject_GC_UnTrack(mp); |
| 1081 |
Py_TRASHCAN_SAFE_BEGIN(mp) |
| 1082 |
for (ep = mp->ma_table; fill > 0; ep++) { |
| 1083 |
if (ep->me_key) { |
| 1084 |
--fill; |
| 1085 |
Py_DECREF(ep->me_key); |
| 1086 |
Py_XDECREF(ep->me_value); |
| 1087 |
} |
| 1088 |
} |
| 1089 |
if (mp->ma_table != mp->ma_smalltable) |
| 1090 |
PyMem_DEL(mp->ma_table); |
| 1091 |
if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type) |
| 1092 |
free_list[numfree++] = mp; |
| 1093 |
else |
| 1094 |
Py_TYPE(mp)->tp_free((PyObject *)mp); |
| 1095 |
Py_TRASHCAN_SAFE_END(mp) |
| 1096 |
} |
| 1097 |
|
| 1098 |
static int |
| 1099 |
dict_print(register PyDictObject *mp, register FILE *fp, register int flags) |
| 1100 |
{ |
| 1101 |
register Py_ssize_t i; |
| 1102 |
register Py_ssize_t any; |
| 1103 |
int status; |
| 1104 |
|
| 1105 |
status = Py_ReprEnter((PyObject*)mp); |
| 1106 |
if (status != 0) { |
| 1107 |
if (status < 0) |
| 1108 |
return status; |
| 1109 |
Py_BEGIN_ALLOW_THREADS |
| 1110 |
fprintf(fp, "{...}"); |
| 1111 |
Py_END_ALLOW_THREADS |
| 1112 |
return 0; |
| 1113 |
} |
| 1114 |
|
| 1115 |
Py_BEGIN_ALLOW_THREADS |
| 1116 |
fprintf(fp, "{"); |
| 1117 |
Py_END_ALLOW_THREADS |
| 1118 |
any = 0; |
| 1119 |
for (i = 0; i <= mp->ma_mask; i++) { |
| 1120 |
PyDictEntry *ep = mp->ma_table + i; |
| 1121 |
PyObject *pvalue = ep->me_value; |
| 1122 |
if (pvalue != NULL) { |
| 1123 |
|
| 1124 |
|
| 1125 |
Py_INCREF(pvalue); |
| 1126 |
if (any++ > 0) { |
| 1127 |
Py_BEGIN_ALLOW_THREADS |
| 1128 |
fprintf(fp, ", "); |
| 1129 |
Py_END_ALLOW_THREADS |
| 1130 |
} |
| 1131 |
if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) { |
| 1132 |
Py_DECREF(pvalue); |
| 1133 |
Py_ReprLeave((PyObject*)mp); |
| 1134 |
return -1; |
| 1135 |
} |
| 1136 |
Py_BEGIN_ALLOW_THREADS |
| 1137 |
fprintf(fp, ": "); |
| 1138 |
Py_END_ALLOW_THREADS |
| 1139 |
if (PyObject_Print(pvalue, fp, 0) != 0) { |
| 1140 |
Py_DECREF(pvalue); |
| 1141 |
Py_ReprLeave((PyObject*)mp); |
| 1142 |
return -1; |
| 1143 |
} |
| 1144 |
Py_DECREF(pvalue); |
| 1145 |
} |
| 1146 |
} |
| 1147 |
Py_BEGIN_ALLOW_THREADS |
| 1148 |
fprintf(fp, "}"); |
| 1149 |
Py_END_ALLOW_THREADS |
| 1150 |
Py_ReprLeave((PyObject*)mp); |
| 1151 |
return 0; |
| 1152 |
} |
| 1153 |
|
| 1154 |
static PyObject * |
| 1155 |
dict_repr(PyDictObject *mp) |
| 1156 |
{ |
| 1157 |
Py_ssize_t i; |
| 1158 |
PyObject *s, *temp, *colon = NULL; |
| 1159 |
PyObject *pieces = NULL, *result = NULL; |
| 1160 |
PyObject *key, *value; |
| 1161 |
|
| 1162 |
i = Py_ReprEnter((PyObject *)mp); |
| 1163 |
if (i != 0) { |
| 1164 |
return i > 0 ? PyString_FromString("{...}") : NULL; |
| 1165 |
} |
| 1166 |
|
| 1167 |
if (mp->ma_used == 0) { |
| 1168 |
result = PyString_FromString("{}"); |
| 1169 |
goto Done; |
| 1170 |
} |
| 1171 |
|
| 1172 |
pieces = PyList_New(0); |
| 1173 |
if (pieces == NULL) |
| 1174 |
goto Done; |
| 1175 |
|
| 1176 |
colon = PyString_FromString(": "); |
| 1177 |
if (colon == NULL) |
| 1178 |
goto Done; |
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
i = 0; |
| 1183 |
while (PyDict_Next((PyObject *)mp, &i, &key, &value)) { |
| 1184 |
int status; |
| 1185 |
|
| 1186 |
Py_INCREF(value); |
| 1187 |
s = PyObject_Repr(key); |
| 1188 |
PyString_Concat(&s, colon); |
| 1189 |
PyString_ConcatAndDel(&s, PyObject_Repr(value)); |
| 1190 |
Py_DECREF(value); |
| 1191 |
if (s == NULL) |
| 1192 |
goto Done; |
| 1193 |
status = PyList_Append(pieces, s); |
| 1194 |
Py_DECREF(s); |
| 1195 |
if (status < 0) |
| 1196 |
goto Done; |
| 1197 |
} |
| 1198 |
|
| 1199 |
|
| 1200 |
assert(PyList_GET_SIZE(pieces) > 0); |
| 1201 |
s = PyString_FromString("{"); |
| 1202 |
if (s == NULL) |
| 1203 |
goto Done; |
| 1204 |
temp = PyList_GET_ITEM(pieces, 0); |
| 1205 |
PyString_ConcatAndDel(&s, temp); |
| 1206 |
PyList_SET_ITEM(pieces, 0, s); |
| 1207 |
if (s == NULL) |
| 1208 |
goto Done; |
| 1209 |
|
| 1210 |
s = PyString_FromString("}"); |
| 1211 |
if (s == NULL) |
| 1212 |
goto Done; |
| 1213 |
temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1); |
| 1214 |
PyString_ConcatAndDel(&temp, s); |
| 1215 |
PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp); |
| 1216 |
if (temp == NULL) |
| 1217 |
goto Done; |
| 1218 |
|
| 1219 |
|
| 1220 |
s = PyString_FromString(", "); |
| 1221 |
if (s == NULL) |
| 1222 |
goto Done; |
| 1223 |
result = _PyString_Join(s, pieces); |
| 1224 |
Py_DECREF(s); |
| 1225 |
|
| 1226 |
Done: |
| 1227 |
Py_XDECREF(pieces); |
| 1228 |
Py_XDECREF(colon); |
| 1229 |
Py_ReprLeave((PyObject *)mp); |
| 1230 |
return result; |
| 1231 |
} |
| 1232 |
|
| 1233 |
static Py_ssize_t |
| 1234 |
dict_length(PyDictObject *mp) |
| 1235 |
{ |
| 1236 |
return mp->ma_used; |
| 1237 |
} |
| 1238 |
|
| 1239 |
static PyObject * |
| 1240 |
dict_subscript(PyDictObject *mp, register PyObject *key) |
| 1241 |
{ |
| 1242 |
PyObject *v; |
| 1243 |
long hash; |
| 1244 |
PyDictEntry *ep; |
| 1245 |
assert(mp->ma_table != NULL); |
| 1246 |
if (!PyString_CheckExact(key) || |
| 1247 |
(hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 1248 |
hash = PyObject_Hash(key); |
| 1249 |
if (hash == -1) |
| 1250 |
return NULL; |
| 1251 |
} |
| 1252 |
ep = (mp->ma_lookup)(mp, key, hash); |
| 1253 |
if (ep == NULL) |
| 1254 |
return NULL; |
| 1255 |
v = ep->me_value; |
| 1256 |
if (v == NULL) { |
| 1257 |
if (!PyDict_CheckExact(mp)) { |
| 1258 |
|
| 1259 |
PyObject *missing, *res; |
| 1260 |
static PyObject *missing_str = NULL; |
| 1261 |
missing = _PyObject_LookupSpecial((PyObject *)mp, |
| 1262 |
"__missing__", |
| 1263 |
&missing_str); |
| 1264 |
if (missing != NULL) { |
| 1265 |
res = PyObject_CallFunctionObjArgs(missing, |
| 1266 |
key, NULL); |
| 1267 |
Py_DECREF(missing); |
| 1268 |
return res; |
| 1269 |
} |
| 1270 |
else if (PyErr_Occurred()) |
| 1271 |
return NULL; |
| 1272 |
} |
| 1273 |
set_key_error(key); |
| 1274 |
return NULL; |
| 1275 |
} |
| 1276 |
else |
| 1277 |
Py_INCREF(v); |
| 1278 |
return v; |
| 1279 |
} |
| 1280 |
|
| 1281 |
static int |
| 1282 |
dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w) |
| 1283 |
{ |
| 1284 |
if (w == NULL) |
| 1285 |
return PyDict_DelItem((PyObject *)mp, v); |
| 1286 |
else |
| 1287 |
return PyDict_SetItem((PyObject *)mp, v, w); |
| 1288 |
} |
| 1289 |
|
| 1290 |
static PyMappingMethods dict_as_mapping = { |
| 1291 |
(lenfunc)dict_length, |
| 1292 |
(binaryfunc)dict_subscript, |
| 1293 |
(objobjargproc)dict_ass_sub, |
| 1294 |
}; |
| 1295 |
|
| 1296 |
static PyObject * |
| 1297 |
dict_keys(register PyDictObject *mp) |
| 1298 |
{ |
| 1299 |
register PyObject *v; |
| 1300 |
register Py_ssize_t i, j; |
| 1301 |
PyDictEntry *ep; |
| 1302 |
Py_ssize_t mask, n; |
| 1303 |
|
| 1304 |
again: |
| 1305 |
n = mp->ma_used; |
| 1306 |
v = PyList_New(n); |
| 1307 |
if (v == NULL) |
| 1308 |
return NULL; |
| 1309 |
if (n != mp->ma_used) { |
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
Py_DECREF(v); |
| 1314 |
goto again; |
| 1315 |
} |
| 1316 |
ep = mp->ma_table; |
| 1317 |
mask = mp->ma_mask; |
| 1318 |
for (i = 0, j = 0; i <= mask; i++) { |
| 1319 |
if (ep[i].me_value != NULL) { |
| 1320 |
PyObject *key = ep[i].me_key; |
| 1321 |
Py_INCREF(key); |
| 1322 |
PyList_SET_ITEM(v, j, key); |
| 1323 |
j++; |
| 1324 |
} |
| 1325 |
} |
| 1326 |
assert(j == n); |
| 1327 |
return v; |
| 1328 |
} |
| 1329 |
|
| 1330 |
static PyObject * |
| 1331 |
dict_values(register PyDictObject *mp) |
| 1332 |
{ |
| 1333 |
register PyObject *v; |
| 1334 |
register Py_ssize_t i, j; |
| 1335 |
PyDictEntry *ep; |
| 1336 |
Py_ssize_t mask, n; |
| 1337 |
|
| 1338 |
again: |
| 1339 |
n = mp->ma_used; |
| 1340 |
v = PyList_New(n); |
| 1341 |
if (v == NULL) |
| 1342 |
return NULL; |
| 1343 |
if (n != mp->ma_used) { |
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
Py_DECREF(v); |
| 1348 |
goto again; |
| 1349 |
} |
| 1350 |
ep = mp->ma_table; |
| 1351 |
mask = mp->ma_mask; |
| 1352 |
for (i = 0, j = 0; i <= mask; i++) { |
| 1353 |
if (ep[i].me_value != NULL) { |
| 1354 |
PyObject *value = ep[i].me_value; |
| 1355 |
Py_INCREF(value); |
| 1356 |
PyList_SET_ITEM(v, j, value); |
| 1357 |
j++; |
| 1358 |
} |
| 1359 |
} |
| 1360 |
assert(j == n); |
| 1361 |
return v; |
| 1362 |
} |
| 1363 |
|
| 1364 |
static PyObject * |
| 1365 |
dict_items(register PyDictObject *mp) |
| 1366 |
{ |
| 1367 |
register PyObject *v; |
| 1368 |
register Py_ssize_t i, j, n; |
| 1369 |
Py_ssize_t mask; |
| 1370 |
PyObject *item, *key, *value; |
| 1371 |
PyDictEntry *ep; |
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
again: |
| 1378 |
n = mp->ma_used; |
| 1379 |
v = PyList_New(n); |
| 1380 |
if (v == NULL) |
| 1381 |
return NULL; |
| 1382 |
for (i = 0; i < n; i++) { |
| 1383 |
item = PyTuple_New(2); |
| 1384 |
if (item == NULL) { |
| 1385 |
Py_DECREF(v); |
| 1386 |
return NULL; |
| 1387 |
} |
| 1388 |
PyList_SET_ITEM(v, i, item); |
| 1389 |
} |
| 1390 |
if (n != mp->ma_used) { |
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
Py_DECREF(v); |
| 1395 |
goto again; |
| 1396 |
} |
| 1397 |
|
| 1398 |
ep = mp->ma_table; |
| 1399 |
mask = mp->ma_mask; |
| 1400 |
for (i = 0, j = 0; i <= mask; i++) { |
| 1401 |
if ((value=ep[i].me_value) != NULL) { |
| 1402 |
key = ep[i].me_key; |
| 1403 |
item = PyList_GET_ITEM(v, j); |
| 1404 |
Py_INCREF(key); |
| 1405 |
PyTuple_SET_ITEM(item, 0, key); |
| 1406 |
Py_INCREF(value); |
| 1407 |
PyTuple_SET_ITEM(item, 1, value); |
| 1408 |
j++; |
| 1409 |
} |
| 1410 |
} |
| 1411 |
assert(j == n); |
| 1412 |
return v; |
| 1413 |
} |
| 1414 |
|
| 1415 |
static PyObject * |
| 1416 |
dict_fromkeys(PyObject *cls, PyObject *args) |
| 1417 |
{ |
| 1418 |
PyObject *seq; |
| 1419 |
PyObject *value = Py_None; |
| 1420 |
PyObject *it; |
| 1421 |
PyObject *key; |
| 1422 |
PyObject *d; |
| 1423 |
int status; |
| 1424 |
|
| 1425 |
if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value)) |
| 1426 |
return NULL; |
| 1427 |
|
| 1428 |
d = PyObject_CallObject(cls, NULL); |
| 1429 |
if (d == NULL) |
| 1430 |
return NULL; |
| 1431 |
|
| 1432 |
if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) { |
| 1433 |
if (PyDict_CheckExact(seq)) { |
| 1434 |
PyDictObject *mp = (PyDictObject *)d; |
| 1435 |
PyObject *oldvalue; |
| 1436 |
Py_ssize_t pos = 0; |
| 1437 |
PyObject *key; |
| 1438 |
long hash; |
| 1439 |
|
| 1440 |
if (dictresize(mp, ((PyDictObject *)seq)->ma_used / 2 * 3)) { |
| 1441 |
Py_DECREF(d); |
| 1442 |
return NULL; |
| 1443 |
} |
| 1444 |
|
| 1445 |
while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) { |
| 1446 |
Py_INCREF(key); |
| 1447 |
Py_INCREF(value); |
| 1448 |
if (insertdict(mp, key, hash, value)) { |
| 1449 |
Py_DECREF(d); |
| 1450 |
return NULL; |
| 1451 |
} |
| 1452 |
} |
| 1453 |
return d; |
| 1454 |
} |
| 1455 |
if (PyAnySet_CheckExact(seq)) { |
| 1456 |
PyDictObject *mp = (PyDictObject *)d; |
| 1457 |
Py_ssize_t pos = 0; |
| 1458 |
PyObject *key; |
| 1459 |
long hash; |
| 1460 |
|
| 1461 |
if (dictresize(mp, PySet_GET_SIZE(seq) / 2 * 3)) { |
| 1462 |
Py_DECREF(d); |
| 1463 |
return NULL; |
| 1464 |
} |
| 1465 |
|
| 1466 |
while (_PySet_NextEntry(seq, &pos, &key, &hash)) { |
| 1467 |
Py_INCREF(key); |
| 1468 |
Py_INCREF(value); |
| 1469 |
if (insertdict(mp, key, hash, value)) { |
| 1470 |
Py_DECREF(d); |
| 1471 |
return NULL; |
| 1472 |
} |
| 1473 |
} |
| 1474 |
return d; |
| 1475 |
} |
| 1476 |
} |
| 1477 |
|
| 1478 |
it = PyObject_GetIter(seq); |
| 1479 |
if (it == NULL){ |
| 1480 |
Py_DECREF(d); |
| 1481 |
return NULL; |
| 1482 |
} |
| 1483 |
|
| 1484 |
if (PyDict_CheckExact(d)) { |
| 1485 |
while ((key = PyIter_Next(it)) != NULL) { |
| 1486 |
status = PyDict_SetItem(d, key, value); |
| 1487 |
Py_DECREF(key); |
| 1488 |
if (status < 0) |
| 1489 |
goto Fail; |
| 1490 |
} |
| 1491 |
} else { |
| 1492 |
while ((key = PyIter_Next(it)) != NULL) { |
| 1493 |
status = PyObject_SetItem(d, key, value); |
| 1494 |
Py_DECREF(key); |
| 1495 |
if (status < 0) |
| 1496 |
goto Fail; |
| 1497 |
} |
| 1498 |
} |
| 1499 |
|
| 1500 |
if (PyErr_Occurred()) |
| 1501 |
goto Fail; |
| 1502 |
Py_DECREF(it); |
| 1503 |
return d; |
| 1504 |
|
| 1505 |
Fail: |
| 1506 |
Py_DECREF(it); |
| 1507 |
Py_DECREF(d); |
| 1508 |
return NULL; |
| 1509 |
} |
| 1510 |
|
| 1511 |
static int |
| 1512 |
dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname) |
| 1513 |
{ |
| 1514 |
PyObject *arg = NULL; |
| 1515 |
int result = 0; |
| 1516 |
|
| 1517 |
if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) |
| 1518 |
result = -1; |
| 1519 |
|
| 1520 |
else if (arg != NULL) { |
| 1521 |
if (PyObject_HasAttrString(arg, "keys")) |
| 1522 |
result = PyDict_Merge(self, arg, 1); |
| 1523 |
else |
| 1524 |
result = PyDict_MergeFromSeq2(self, arg, 1); |
| 1525 |
} |
| 1526 |
if (result == 0 && kwds != NULL) |
| 1527 |
result = PyDict_Merge(self, kwds, 1); |
| 1528 |
return result; |
| 1529 |
} |
| 1530 |
|
| 1531 |
static PyObject * |
| 1532 |
dict_update(PyObject *self, PyObject *args, PyObject *kwds) |
| 1533 |
{ |
| 1534 |
if (dict_update_common(self, args, kwds, "update") != -1) |
| 1535 |
Py_RETURN_NONE; |
| 1536 |
return NULL; |
| 1537 |
} |
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
int |
| 1550 |
PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override) |
| 1551 |
{ |
| 1552 |
PyObject *it; |
| 1553 |
Py_ssize_t i; |
| 1554 |
PyObject *item; |
| 1555 |
PyObject *fast; |
| 1556 |
|
| 1557 |
assert(d != NULL); |
| 1558 |
assert(PyDict_Check(d)); |
| 1559 |
assert(seq2 != NULL); |
| 1560 |
|
| 1561 |
it = PyObject_GetIter(seq2); |
| 1562 |
if (it == NULL) |
| 1563 |
return -1; |
| 1564 |
|
| 1565 |
for (i = 0; ; ++i) { |
| 1566 |
PyObject *key, *value; |
| 1567 |
Py_ssize_t n; |
| 1568 |
|
| 1569 |
fast = NULL; |
| 1570 |
item = PyIter_Next(it); |
| 1571 |
if (item == NULL) { |
| 1572 |
if (PyErr_Occurred()) |
| 1573 |
goto Fail; |
| 1574 |
break; |
| 1575 |
} |
| 1576 |
|
| 1577 |
|
| 1578 |
fast = PySequence_Fast(item, ""); |
| 1579 |
if (fast == NULL) { |
| 1580 |
if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1581 |
PyErr_Format(PyExc_TypeError, |
| 1582 |
"cannot convert dictionary update " |
| 1583 |
"sequence element #%zd to a sequence", |
| 1584 |
i); |
| 1585 |
goto Fail; |
| 1586 |
} |
| 1587 |
n = PySequence_Fast_GET_SIZE(fast); |
| 1588 |
if (n != 2) { |
| 1589 |
PyErr_Format(PyExc_ValueError, |
| 1590 |
"dictionary update sequence element #%zd " |
| 1591 |
"has length %zd; 2 is required", |
| 1592 |
i, n); |
| 1593 |
goto Fail; |
| 1594 |
} |
| 1595 |
|
| 1596 |
|
| 1597 |
key = PySequence_Fast_GET_ITEM(fast, 0); |
| 1598 |
value = PySequence_Fast_GET_ITEM(fast, 1); |
| 1599 |
Py_INCREF(key); |
| 1600 |
Py_INCREF(value); |
| 1601 |
if (override || PyDict_GetItem(d, key) == NULL) { |
| 1602 |
int status = PyDict_SetItem(d, key, value); |
| 1603 |
if (status < 0) { |
| 1604 |
Py_DECREF(key); |
| 1605 |
Py_DECREF(value); |
| 1606 |
goto Fail; |
| 1607 |
} |
| 1608 |
} |
| 1609 |
Py_DECREF(key); |
| 1610 |
Py_DECREF(value); |
| 1611 |
Py_DECREF(fast); |
| 1612 |
Py_DECREF(item); |
| 1613 |
} |
| 1614 |
|
| 1615 |
i = 0; |
| 1616 |
goto Return; |
| 1617 |
Fail: |
| 1618 |
Py_XDECREF(item); |
| 1619 |
Py_XDECREF(fast); |
| 1620 |
i = -1; |
| 1621 |
Return: |
| 1622 |
Py_DECREF(it); |
| 1623 |
return Py_SAFE_DOWNCAST(i, Py_ssize_t, int); |
| 1624 |
} |
| 1625 |
|
| 1626 |
int |
| 1627 |
PyDict_Update(PyObject *a, PyObject *b) |
| 1628 |
{ |
| 1629 |
return PyDict_Merge(a, b, 1); |
| 1630 |
} |
| 1631 |
|
| 1632 |
int |
| 1633 |
PyDict_Merge(PyObject *a, PyObject *b, int override) |
| 1634 |
{ |
| 1635 |
register PyDictObject *mp, *other; |
| 1636 |
register Py_ssize_t i; |
| 1637 |
PyDictEntry *entry; |
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
if (a == NULL || !PyDict_Check(a) || b == NULL) { |
| 1645 |
PyErr_BadInternalCall(); |
| 1646 |
return -1; |
| 1647 |
} |
| 1648 |
mp = (PyDictObject*)a; |
| 1649 |
if (PyDict_Check(b)) { |
| 1650 |
other = (PyDictObject*)b; |
| 1651 |
if (other == mp || other->ma_used == 0) |
| 1652 |
|
| 1653 |
return 0; |
| 1654 |
if (mp->ma_used == 0) |
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
override = 1; |
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) { |
| 1665 |
if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0) |
| 1666 |
return -1; |
| 1667 |
} |
| 1668 |
for (i = 0; i <= other->ma_mask; i++) { |
| 1669 |
entry = &other->ma_table[i]; |
| 1670 |
if (entry->me_value != NULL && |
| 1671 |
(override || |
| 1672 |
PyDict_GetItem(a, entry->me_key) == NULL)) { |
| 1673 |
Py_INCREF(entry->me_key); |
| 1674 |
Py_INCREF(entry->me_value); |
| 1675 |
if (insertdict(mp, entry->me_key, |
| 1676 |
(long)entry->me_hash, |
| 1677 |
entry->me_value) != 0) |
| 1678 |
return -1; |
| 1679 |
} |
| 1680 |
} |
| 1681 |
} |
| 1682 |
else { |
| 1683 |
|
| 1684 |
PyObject *keys = PyMapping_Keys(b); |
| 1685 |
PyObject *iter; |
| 1686 |
PyObject *key, *value; |
| 1687 |
int status; |
| 1688 |
|
| 1689 |
if (keys == NULL) |
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
return -1; |
| 1696 |
|
| 1697 |
iter = PyObject_GetIter(keys); |
| 1698 |
Py_DECREF(keys); |
| 1699 |
if (iter == NULL) |
| 1700 |
return -1; |
| 1701 |
|
| 1702 |
for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) { |
| 1703 |
if (!override && PyDict_GetItem(a, key) != NULL) { |
| 1704 |
Py_DECREF(key); |
| 1705 |
continue; |
| 1706 |
} |
| 1707 |
value = PyObject_GetItem(b, key); |
| 1708 |
if (value == NULL) { |
| 1709 |
Py_DECREF(iter); |
| 1710 |
Py_DECREF(key); |
| 1711 |
return -1; |
| 1712 |
} |
| 1713 |
status = PyDict_SetItem(a, key, value); |
| 1714 |
Py_DECREF(key); |
| 1715 |
Py_DECREF(value); |
| 1716 |
if (status < 0) { |
| 1717 |
Py_DECREF(iter); |
| 1718 |
return -1; |
| 1719 |
} |
| 1720 |
} |
| 1721 |
Py_DECREF(iter); |
| 1722 |
if (PyErr_Occurred()) |
| 1723 |
|
| 1724 |
return -1; |
| 1725 |
} |
| 1726 |
return 0; |
| 1727 |
} |
| 1728 |
|
| 1729 |
static PyObject * |
| 1730 |
dict_copy(register PyDictObject *mp) |
| 1731 |
{ |
| 1732 |
return PyDict_Copy((PyObject*)mp); |
| 1733 |
} |
| 1734 |
|
| 1735 |
PyObject * |
| 1736 |
PyDict_Copy(PyObject *o) |
| 1737 |
{ |
| 1738 |
PyObject *copy; |
| 1739 |
|
| 1740 |
if (o == NULL || !PyDict_Check(o)) { |
| 1741 |
PyErr_BadInternalCall(); |
| 1742 |
return NULL; |
| 1743 |
} |
| 1744 |
copy = PyDict_New(); |
| 1745 |
if (copy == NULL) |
| 1746 |
return NULL; |
| 1747 |
if (PyDict_Merge(copy, o, 1) == 0) |
| 1748 |
return copy; |
| 1749 |
Py_DECREF(copy); |
| 1750 |
return NULL; |
| 1751 |
} |
| 1752 |
|
| 1753 |
Py_ssize_t |
| 1754 |
PyDict_Size(PyObject *mp) |
| 1755 |
{ |
| 1756 |
if (mp == NULL || !PyDict_Check(mp)) { |
| 1757 |
PyErr_BadInternalCall(); |
| 1758 |
return -1; |
| 1759 |
} |
| 1760 |
return ((PyDictObject *)mp)->ma_used; |
| 1761 |
} |
| 1762 |
|
| 1763 |
PyObject * |
| 1764 |
PyDict_Keys(PyObject *mp) |
| 1765 |
{ |
| 1766 |
if (mp == NULL || !PyDict_Check(mp)) { |
| 1767 |
PyErr_BadInternalCall(); |
| 1768 |
return NULL; |
| 1769 |
} |
| 1770 |
return dict_keys((PyDictObject *)mp); |
| 1771 |
} |
| 1772 |
|
| 1773 |
PyObject * |
| 1774 |
PyDict_Values(PyObject *mp) |
| 1775 |
{ |
| 1776 |
if (mp == NULL || !PyDict_Check(mp)) { |
| 1777 |
PyErr_BadInternalCall(); |
| 1778 |
return NULL; |
| 1779 |
} |
| 1780 |
return dict_values((PyDictObject *)mp); |
| 1781 |
} |
| 1782 |
|
| 1783 |
PyObject * |
| 1784 |
PyDict_Items(PyObject *mp) |
| 1785 |
{ |
| 1786 |
if (mp == NULL || !PyDict_Check(mp)) { |
| 1787 |
PyErr_BadInternalCall(); |
| 1788 |
return NULL; |
| 1789 |
} |
| 1790 |
return dict_items((PyDictObject *)mp); |
| 1791 |
} |
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
static PyObject * |
| 1802 |
characterize(PyDictObject *a, PyDictObject *b, PyObject **pval) |
| 1803 |
{ |
| 1804 |
PyObject *akey = NULL; |
| 1805 |
PyObject *aval = NULL; |
| 1806 |
Py_ssize_t i; |
| 1807 |
int cmp; |
| 1808 |
|
| 1809 |
for (i = 0; i <= a->ma_mask; i++) { |
| 1810 |
PyObject *thiskey, *thisaval, *thisbval; |
| 1811 |
if (a->ma_table[i].me_value == NULL) |
| 1812 |
continue; |
| 1813 |
thiskey = a->ma_table[i].me_key; |
| 1814 |
Py_INCREF(thiskey); |
| 1815 |
if (akey != NULL) { |
| 1816 |
cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT); |
| 1817 |
if (cmp < 0) { |
| 1818 |
Py_DECREF(thiskey); |
| 1819 |
goto Fail; |
| 1820 |
} |
| 1821 |
if (cmp > 0 || |
| 1822 |
i > a->ma_mask || |
| 1823 |
a->ma_table[i].me_value == NULL) |
| 1824 |
{ |
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
Py_DECREF(thiskey); |
| 1832 |
continue; |
| 1833 |
} |
| 1834 |
} |
| 1835 |
|
| 1836 |
|
| 1837 |
thisaval = a->ma_table[i].me_value; |
| 1838 |
assert(thisaval); |
| 1839 |
Py_INCREF(thisaval); |
| 1840 |
thisbval = PyDict_GetItem((PyObject *)b, thiskey); |
| 1841 |
if (thisbval == NULL) |
| 1842 |
cmp = 0; |
| 1843 |
else { |
| 1844 |
|
| 1845 |
cmp = PyObject_RichCompareBool( |
| 1846 |
thisaval, thisbval, Py_EQ); |
| 1847 |
if (cmp < 0) { |
| 1848 |
Py_DECREF(thiskey); |
| 1849 |
Py_DECREF(thisaval); |
| 1850 |
goto Fail; |
| 1851 |
} |
| 1852 |
} |
| 1853 |
if (cmp == 0) { |
| 1854 |
|
| 1855 |
Py_XDECREF(akey); |
| 1856 |
Py_XDECREF(aval); |
| 1857 |
akey = thiskey; |
| 1858 |
aval = thisaval; |
| 1859 |
} |
| 1860 |
else { |
| 1861 |
Py_DECREF(thiskey); |
| 1862 |
Py_DECREF(thisaval); |
| 1863 |
} |
| 1864 |
} |
| 1865 |
*pval = aval; |
| 1866 |
return akey; |
| 1867 |
|
| 1868 |
Fail: |
| 1869 |
Py_XDECREF(akey); |
| 1870 |
Py_XDECREF(aval); |
| 1871 |
*pval = NULL; |
| 1872 |
return NULL; |
| 1873 |
} |
| 1874 |
|
| 1875 |
static int |
| 1876 |
dict_compare(PyDictObject *a, PyDictObject *b) |
| 1877 |
{ |
| 1878 |
PyObject *adiff, *bdiff, *aval, *bval; |
| 1879 |
int res; |
| 1880 |
|
| 1881 |
|
| 1882 |
if (a->ma_used < b->ma_used) |
| 1883 |
return -1; |
| 1884 |
else if (a->ma_used > b->ma_used) |
| 1885 |
return 1; |
| 1886 |
|
| 1887 |
|
| 1888 |
bdiff = bval = NULL; |
| 1889 |
adiff = characterize(a, b, &aval); |
| 1890 |
if (adiff == NULL) { |
| 1891 |
assert(!aval); |
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
res = PyErr_Occurred() ? -1 : 0; |
| 1896 |
goto Finished; |
| 1897 |
} |
| 1898 |
bdiff = characterize(b, a, &bval); |
| 1899 |
if (bdiff == NULL && PyErr_Occurred()) { |
| 1900 |
assert(!bval); |
| 1901 |
res = -1; |
| 1902 |
goto Finished; |
| 1903 |
} |
| 1904 |
res = 0; |
| 1905 |
if (bdiff) { |
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
res = PyObject_Compare(adiff, bdiff); |
| 1911 |
} |
| 1912 |
if (res == 0 && bval != NULL) |
| 1913 |
res = PyObject_Compare(aval, bval); |
| 1914 |
|
| 1915 |
Finished: |
| 1916 |
Py_XDECREF(adiff); |
| 1917 |
Py_XDECREF(bdiff); |
| 1918 |
Py_XDECREF(aval); |
| 1919 |
Py_XDECREF(bval); |
| 1920 |
return res; |
| 1921 |
} |
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
static int |
| 1928 |
dict_equal(PyDictObject *a, PyDictObject *b) |
| 1929 |
{ |
| 1930 |
Py_ssize_t i; |
| 1931 |
|
| 1932 |
if (a->ma_used != b->ma_used) |
| 1933 |
|
| 1934 |
return 0; |
| 1935 |
|
| 1936 |
|
| 1937 |
for (i = 0; i <= a->ma_mask; i++) { |
| 1938 |
PyObject *aval = a->ma_table[i].me_value; |
| 1939 |
if (aval != NULL) { |
| 1940 |
int cmp; |
| 1941 |
PyObject *bval; |
| 1942 |
PyObject *key = a->ma_table[i].me_key; |
| 1943 |
|
| 1944 |
|
| 1945 |
Py_INCREF(aval); |
| 1946 |
|
| 1947 |
Py_INCREF(key); |
| 1948 |
bval = PyDict_GetItem((PyObject *)b, key); |
| 1949 |
if (bval == NULL) { |
| 1950 |
Py_DECREF(key); |
| 1951 |
Py_DECREF(aval); |
| 1952 |
return 0; |
| 1953 |
} |
| 1954 |
cmp = PyObject_RichCompareBool(aval, bval, Py_EQ); |
| 1955 |
Py_DECREF(key); |
| 1956 |
Py_DECREF(aval); |
| 1957 |
if (cmp <= 0) |
| 1958 |
return cmp; |
| 1959 |
} |
| 1960 |
} |
| 1961 |
return 1; |
| 1962 |
} |
| 1963 |
|
| 1964 |
static PyObject * |
| 1965 |
dict_richcompare(PyObject *v, PyObject *w, int op) |
| 1966 |
{ |
| 1967 |
int cmp; |
| 1968 |
PyObject *res; |
| 1969 |
|
| 1970 |
if (!PyDict_Check(v) || !PyDict_Check(w)) { |
| 1971 |
res = Py_NotImplemented; |
| 1972 |
} |
| 1973 |
else if (op == Py_EQ || op == Py_NE) { |
| 1974 |
cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w); |
| 1975 |
if (cmp < 0) |
| 1976 |
return NULL; |
| 1977 |
res = (cmp == (op == Py_EQ)) ? Py_True : Py_False; |
| 1978 |
} |
| 1979 |
else { |
| 1980 |
|
| 1981 |
if (PyErr_WarnPy3k("dict inequality comparisons not supported " |
| 1982 |
"in 3.x", 1) < 0) { |
| 1983 |
return NULL; |
| 1984 |
} |
| 1985 |
res = Py_NotImplemented; |
| 1986 |
} |
| 1987 |
Py_INCREF(res); |
| 1988 |
return res; |
| 1989 |
} |
| 1990 |
|
| 1991 |
static PyObject * |
| 1992 |
dict_contains(register PyDictObject *mp, PyObject *key) |
| 1993 |
{ |
| 1994 |
long hash; |
| 1995 |
PyDictEntry *ep; |
| 1996 |
|
| 1997 |
if (!PyString_CheckExact(key) || |
| 1998 |
(hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 1999 |
hash = PyObject_Hash(key); |
| 2000 |
if (hash == -1) |
| 2001 |
return NULL; |
| 2002 |
} |
| 2003 |
ep = (mp->ma_lookup)(mp, key, hash); |
| 2004 |
if (ep == NULL) |
| 2005 |
return NULL; |
| 2006 |
return PyBool_FromLong(ep->me_value != NULL); |
| 2007 |
} |
| 2008 |
|
| 2009 |
static PyObject * |
| 2010 |
dict_has_key(register PyDictObject *mp, PyObject *key) |
| 2011 |
{ |
| 2012 |
if (PyErr_WarnPy3k("dict.has_key() not supported in 3.x; " |
| 2013 |
"use the in operator", 1) < 0) |
| 2014 |
return NULL; |
| 2015 |
return dict_contains(mp, key); |
| 2016 |
} |
| 2017 |
|
| 2018 |
static PyObject * |
| 2019 |
dict_get(register PyDictObject *mp, PyObject *args) |
| 2020 |
{ |
| 2021 |
PyObject *key; |
| 2022 |
PyObject *failobj = Py_None; |
| 2023 |
PyObject *val = NULL; |
| 2024 |
long hash; |
| 2025 |
PyDictEntry *ep; |
| 2026 |
|
| 2027 |
if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj)) |
| 2028 |
return NULL; |
| 2029 |
|
| 2030 |
if (!PyString_CheckExact(key) || |
| 2031 |
(hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 2032 |
hash = PyObject_Hash(key); |
| 2033 |
if (hash == -1) |
| 2034 |
return NULL; |
| 2035 |
} |
| 2036 |
ep = (mp->ma_lookup)(mp, key, hash); |
| 2037 |
if (ep == NULL) |
| 2038 |
return NULL; |
| 2039 |
val = ep->me_value; |
| 2040 |
if (val == NULL) |
| 2041 |
val = failobj; |
| 2042 |
Py_INCREF(val); |
| 2043 |
return val; |
| 2044 |
} |
| 2045 |
|
| 2046 |
|
| 2047 |
static PyObject * |
| 2048 |
dict_setdefault(register PyDictObject *mp, PyObject *args) |
| 2049 |
{ |
| 2050 |
PyObject *key; |
| 2051 |
PyObject *failobj = Py_None; |
| 2052 |
PyObject *val = NULL; |
| 2053 |
long hash; |
| 2054 |
PyDictEntry *ep; |
| 2055 |
|
| 2056 |
if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj)) |
| 2057 |
return NULL; |
| 2058 |
|
| 2059 |
if (!PyString_CheckExact(key) || |
| 2060 |
(hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 2061 |
hash = PyObject_Hash(key); |
| 2062 |
if (hash == -1) |
| 2063 |
return NULL; |
| 2064 |
} |
| 2065 |
ep = (mp->ma_lookup)(mp, key, hash); |
| 2066 |
if (ep == NULL) |
| 2067 |
return NULL; |
| 2068 |
val = ep->me_value; |
| 2069 |
if (val == NULL) { |
| 2070 |
if (dict_set_item_by_hash_or_entry((PyObject*)mp, key, hash, ep, |
| 2071 |
failobj) == 0) |
| 2072 |
val = failobj; |
| 2073 |
} |
| 2074 |
Py_XINCREF(val); |
| 2075 |
return val; |
| 2076 |
} |
| 2077 |
|
| 2078 |
|
| 2079 |
static PyObject * |
| 2080 |
dict_clear(register PyDictObject *mp) |
| 2081 |
{ |
| 2082 |
PyDict_Clear((PyObject *)mp); |
| 2083 |
Py_RETURN_NONE; |
| 2084 |
} |
| 2085 |
|
| 2086 |
static PyObject * |
| 2087 |
dict_pop(PyDictObject *mp, PyObject *args) |
| 2088 |
{ |
| 2089 |
long hash; |
| 2090 |
PyDictEntry *ep; |
| 2091 |
PyObject *old_value, *old_key; |
| 2092 |
PyObject *key, *deflt = NULL; |
| 2093 |
|
| 2094 |
if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt)) |
| 2095 |
return NULL; |
| 2096 |
if (mp->ma_used == 0) { |
| 2097 |
if (deflt) { |
| 2098 |
Py_INCREF(deflt); |
| 2099 |
return deflt; |
| 2100 |
} |
| 2101 |
set_key_error(key); |
| 2102 |
return NULL; |
| 2103 |
} |
| 2104 |
if (!PyString_CheckExact(key) || |
| 2105 |
(hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 2106 |
hash = PyObject_Hash(key); |
| 2107 |
if (hash == -1) |
| 2108 |
return NULL; |
| 2109 |
} |
| 2110 |
ep = (mp->ma_lookup)(mp, key, hash); |
| 2111 |
if (ep == NULL) |
| 2112 |
return NULL; |
| 2113 |
if (ep->me_value == NULL) { |
| 2114 |
if (deflt) { |
| 2115 |
Py_INCREF(deflt); |
| 2116 |
return deflt; |
| 2117 |
} |
| 2118 |
set_key_error(key); |
| 2119 |
return NULL; |
| 2120 |
} |
| 2121 |
old_key = ep->me_key; |
| 2122 |
Py_INCREF(dummy); |
| 2123 |
ep->me_key = dummy; |
| 2124 |
old_value = ep->me_value; |
| 2125 |
ep->me_value = NULL; |
| 2126 |
mp->ma_used--; |
| 2127 |
Py_DECREF(old_key); |
| 2128 |
return old_value; |
| 2129 |
} |
| 2130 |
|
| 2131 |
static PyObject * |
| 2132 |
dict_popitem(PyDictObject *mp) |
| 2133 |
{ |
| 2134 |
Py_ssize_t i = 0; |
| 2135 |
PyDictEntry *ep; |
| 2136 |
PyObject *res; |
| 2137 |
|
| 2138 |
|
| 2139 |
|
| 2140 |
|
| 2141 |
|
| 2142 |
|
| 2143 |
|
| 2144 |
|
| 2145 |
|
| 2146 |
|
| 2147 |
res = PyTuple_New(2); |
| 2148 |
if (res == NULL) |
| 2149 |
return NULL; |
| 2150 |
if (mp->ma_used == 0) { |
| 2151 |
Py_DECREF(res); |
| 2152 |
PyErr_SetString(PyExc_KeyError, |
| 2153 |
"popitem(): dictionary is empty"); |
| 2154 |
return NULL; |
| 2155 |
} |
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
|
| 2162 |
ep = &mp->ma_table[0]; |
| 2163 |
if (ep->me_value == NULL) { |
| 2164 |
i = ep->me_hash; |
| 2165 |
|
| 2166 |
|
| 2167 |
|
| 2168 |
|
| 2169 |
|
| 2170 |
if (i > mp->ma_mask || i < 1) |
| 2171 |
i = 1; |
| 2172 |
while ((ep = &mp->ma_table[i])->me_value == NULL) { |
| 2173 |
i++; |
| 2174 |
if (i > mp->ma_mask) |
| 2175 |
i = 1; |
| 2176 |
} |
| 2177 |
} |
| 2178 |
PyTuple_SET_ITEM(res, 0, ep->me_key); |
| 2179 |
PyTuple_SET_ITEM(res, 1, ep->me_value); |
| 2180 |
Py_INCREF(dummy); |
| 2181 |
ep->me_key = dummy; |
| 2182 |
ep->me_value = NULL; |
| 2183 |
mp->ma_used--; |
| 2184 |
assert(mp->ma_table[0].me_value == NULL); |
| 2185 |
mp->ma_table[0].me_hash = i + 1; |
| 2186 |
return res; |
| 2187 |
} |
| 2188 |
|
| 2189 |
static int |
| 2190 |
dict_traverse(PyObject *op, visitproc visit, void *arg) |
| 2191 |
{ |
| 2192 |
Py_ssize_t i = 0; |
| 2193 |
PyObject *pk; |
| 2194 |
PyObject *pv; |
| 2195 |
|
| 2196 |
while (PyDict_Next(op, &i, &pk, &pv)) { |
| 2197 |
Py_VISIT(pk); |
| 2198 |
Py_VISIT(pv); |
| 2199 |
} |
| 2200 |
return 0; |
| 2201 |
} |
| 2202 |
|
| 2203 |
static int |
| 2204 |
dict_tp_clear(PyObject *op) |
| 2205 |
{ |
| 2206 |
PyDict_Clear(op); |
| 2207 |
return 0; |
| 2208 |
} |
| 2209 |
|
| 2210 |
|
| 2211 |
extern PyTypeObject PyDictIterKey_Type; |
| 2212 |
extern PyTypeObject PyDictIterValue_Type; |
| 2213 |
extern PyTypeObject PyDictIterItem_Type; |
| 2214 |
static PyObject *dictiter_new(PyDictObject *, PyTypeObject *); |
| 2215 |
|
| 2216 |
static PyObject * |
| 2217 |
dict_iterkeys(PyDictObject *dict) |
| 2218 |
{ |
| 2219 |
return dictiter_new(dict, &PyDictIterKey_Type); |
| 2220 |
} |
| 2221 |
|
| 2222 |
static PyObject * |
| 2223 |
dict_itervalues(PyDictObject *dict) |
| 2224 |
{ |
| 2225 |
return dictiter_new(dict, &PyDictIterValue_Type); |
| 2226 |
} |
| 2227 |
|
| 2228 |
static PyObject * |
| 2229 |
dict_iteritems(PyDictObject *dict) |
| 2230 |
{ |
| 2231 |
return dictiter_new(dict, &PyDictIterItem_Type); |
| 2232 |
} |
| 2233 |
|
| 2234 |
static PyObject * |
| 2235 |
dict_sizeof(PyDictObject *mp) |
| 2236 |
{ |
| 2237 |
Py_ssize_t res; |
| 2238 |
|
| 2239 |
res = _PyObject_SIZE(Py_TYPE(mp)); |
| 2240 |
if (mp->ma_table != mp->ma_smalltable) |
| 2241 |
res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry); |
| 2242 |
return PyInt_FromSsize_t(res); |
| 2243 |
} |
| 2244 |
|
| 2245 |
PyDoc_STRVAR(has_key__doc__, |
| 2246 |
"D.has_key(k) -> True if D has a key k, else False"); |
| 2247 |
|
| 2248 |
PyDoc_STRVAR(contains__doc__, |
| 2249 |
"D.__contains__(k) -> True if D has a key k, else False"); |
| 2250 |
|
| 2251 |
PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]"); |
| 2252 |
|
| 2253 |
PyDoc_STRVAR(sizeof__doc__, |
| 2254 |
"D.__sizeof__() -> size of D in memory, in bytes"); |
| 2255 |
|
| 2256 |
PyDoc_STRVAR(get__doc__, |
| 2257 |
"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None."); |
| 2258 |
|
| 2259 |
PyDoc_STRVAR(setdefault_doc__, |
| 2260 |
"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D"); |
| 2261 |
|
| 2262 |
PyDoc_STRVAR(pop__doc__, |
| 2263 |
"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\ |
| 2264 |
If key is not found, d is returned if given, otherwise KeyError is raised"); |
| 2265 |
|
| 2266 |
PyDoc_STRVAR(popitem__doc__, |
| 2267 |
"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\ |
| 2268 |
2-tuple; but raise KeyError if D is empty."); |
| 2269 |
|
| 2270 |
PyDoc_STRVAR(keys__doc__, |
| 2271 |
"D.keys() -> list of D's keys"); |
| 2272 |
|
| 2273 |
PyDoc_STRVAR(items__doc__, |
| 2274 |
"D.items() -> list of D's (key, value) pairs, as 2-tuples"); |
| 2275 |
|
| 2276 |
PyDoc_STRVAR(values__doc__, |
| 2277 |
"D.values() -> list of D's values"); |
| 2278 |
|
| 2279 |
PyDoc_STRVAR(update__doc__, |
| 2280 |
"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n" |
| 2281 |
"If E present and has a .keys() method, does: for k in E: D[k] = E[k]\n\ |
| 2282 |
If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v\n\ |
| 2283 |
In either case, this is followed by: for k in F: D[k] = F[k]"); |
| 2284 |
|
| 2285 |
PyDoc_STRVAR(fromkeys__doc__, |
| 2286 |
"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\ |
| 2287 |
v defaults to None."); |
| 2288 |
|
| 2289 |
PyDoc_STRVAR(clear__doc__, |
| 2290 |
"D.clear() -> None. Remove all items from D."); |
| 2291 |
|
| 2292 |
PyDoc_STRVAR(copy__doc__, |
| 2293 |
"D.copy() -> a shallow copy of D"); |
| 2294 |
|
| 2295 |
PyDoc_STRVAR(iterkeys__doc__, |
| 2296 |
"D.iterkeys() -> an iterator over the keys of D"); |
| 2297 |
|
| 2298 |
PyDoc_STRVAR(itervalues__doc__, |
| 2299 |
"D.itervalues() -> an iterator over the values of D"); |
| 2300 |
|
| 2301 |
PyDoc_STRVAR(iteritems__doc__, |
| 2302 |
"D.iteritems() -> an iterator over the (key, value) items of D"); |
| 2303 |
|
| 2304 |
|
| 2305 |
static PyObject *dictkeys_new(PyObject *); |
| 2306 |
static PyObject *dictitems_new(PyObject *); |
| 2307 |
static PyObject *dictvalues_new(PyObject *); |
| 2308 |
|
| 2309 |
PyDoc_STRVAR(viewkeys__doc__, |
| 2310 |
"D.viewkeys() -> a set-like object providing a view on D's keys"); |
| 2311 |
PyDoc_STRVAR(viewitems__doc__, |
| 2312 |
"D.viewitems() -> a set-like object providing a view on D's items"); |
| 2313 |
PyDoc_STRVAR(viewvalues__doc__, |
| 2314 |
"D.viewvalues() -> an object providing a view on D's values"); |
| 2315 |
|
| 2316 |
static PyMethodDef mapp_methods[] = { |
| 2317 |
{"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST, |
| 2318 |
contains__doc__}, |
| 2319 |
{"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST, |
| 2320 |
getitem__doc__}, |
| 2321 |
{"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS, |
| 2322 |
sizeof__doc__}, |
| 2323 |
{"has_key", (PyCFunction)dict_has_key, METH_O, |
| 2324 |
has_key__doc__}, |
| 2325 |
{"get", (PyCFunction)dict_get, METH_VARARGS, |
| 2326 |
get__doc__}, |
| 2327 |
{"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS, |
| 2328 |
setdefault_doc__}, |
| 2329 |
{"pop", (PyCFunction)dict_pop, METH_VARARGS, |
| 2330 |
pop__doc__}, |
| 2331 |
{"popitem", (PyCFunction)dict_popitem, METH_NOARGS, |
| 2332 |
popitem__doc__}, |
| 2333 |
{"keys", (PyCFunction)dict_keys, METH_NOARGS, |
| 2334 |
keys__doc__}, |
| 2335 |
{"items", (PyCFunction)dict_items, METH_NOARGS, |
| 2336 |
items__doc__}, |
| 2337 |
{"values", (PyCFunction)dict_values, METH_NOARGS, |
| 2338 |
values__doc__}, |
| 2339 |
{"viewkeys", (PyCFunction)dictkeys_new, METH_NOARGS, |
| 2340 |
viewkeys__doc__}, |
| 2341 |
{"viewitems", (PyCFunction)dictitems_new, METH_NOARGS, |
| 2342 |
viewitems__doc__}, |
| 2343 |
{"viewvalues", (PyCFunction)dictvalues_new, METH_NOARGS, |
| 2344 |
viewvalues__doc__}, |
| 2345 |
{"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS, |
| 2346 |
update__doc__}, |
| 2347 |
{"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS, |
| 2348 |
fromkeys__doc__}, |
| 2349 |
{"clear", (PyCFunction)dict_clear, METH_NOARGS, |
| 2350 |
clear__doc__}, |
| 2351 |
{"copy", (PyCFunction)dict_copy, METH_NOARGS, |
| 2352 |
copy__doc__}, |
| 2353 |
{"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS, |
| 2354 |
iterkeys__doc__}, |
| 2355 |
{"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS, |
| 2356 |
itervalues__doc__}, |
| 2357 |
{"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS, |
| 2358 |
iteritems__doc__}, |
| 2359 |
{NULL, NULL} |
| 2360 |
}; |
| 2361 |
|
| 2362 |
|
| 2363 |
int |
| 2364 |
PyDict_Contains(PyObject *op, PyObject *key) |
| 2365 |
{ |
| 2366 |
long hash; |
| 2367 |
PyDictObject *mp = (PyDictObject *)op; |
| 2368 |
PyDictEntry *ep; |
| 2369 |
|
| 2370 |
if (!PyString_CheckExact(key) || |
| 2371 |
(hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 2372 |
hash = PyObject_Hash(key); |
| 2373 |
if (hash == -1) |
| 2374 |
return -1; |
| 2375 |
} |
| 2376 |
ep = (mp->ma_lookup)(mp, key, hash); |
| 2377 |
return ep == NULL ? -1 : (ep->me_value != NULL); |
| 2378 |
} |
| 2379 |
|
| 2380 |
|
| 2381 |
int |
| 2382 |
_PyDict_Contains(PyObject *op, PyObject *key, long hash) |
| 2383 |
{ |
| 2384 |
PyDictObject *mp = (PyDictObject *)op; |
| 2385 |
PyDictEntry *ep; |
| 2386 |
|
| 2387 |
ep = (mp->ma_lookup)(mp, key, hash); |
| 2388 |
return ep == NULL ? -1 : (ep->me_value != NULL); |
| 2389 |
} |
| 2390 |
|
| 2391 |
|
| 2392 |
static PySequenceMethods dict_as_sequence = { |
| 2393 |
0, |
| 2394 |
0, |
| 2395 |
0, |
| 2396 |
0, |
| 2397 |
0, |
| 2398 |
0, |
| 2399 |
0, |
| 2400 |
PyDict_Contains, |
| 2401 |
0, |
| 2402 |
0, |
| 2403 |
}; |
| 2404 |
|
| 2405 |
static PyObject * |
| 2406 |
dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2407 |
{ |
| 2408 |
PyObject *self; |
| 2409 |
|
| 2410 |
assert(type != NULL && type->tp_alloc != NULL); |
| 2411 |
self = type->tp_alloc(type, 0); |
| 2412 |
if (self != NULL) { |
| 2413 |
PyDictObject *d = (PyDictObject *)self; |
| 2414 |
|
| 2415 |
assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0); |
| 2416 |
INIT_NONZERO_DICT_SLOTS(d); |
| 2417 |
d->ma_lookup = lookdict_string; |
| 2418 |
|
| 2419 |
if (type == &PyDict_Type) |
| 2420 |
_PyObject_GC_UNTRACK(d); |
| 2421 |
#ifdef SHOW_CONVERSION_COUNTS |
| 2422 |
++created; |
| 2423 |
#endif |
| 2424 |
#ifdef SHOW_TRACK_COUNT |
| 2425 |
if (_PyObject_GC_IS_TRACKED(d)) |
| 2426 |
count_tracked++; |
| 2427 |
else |
| 2428 |
count_untracked++; |
| 2429 |
#endif |
| 2430 |
} |
| 2431 |
return self; |
| 2432 |
} |
| 2433 |
|
| 2434 |
static int |
| 2435 |
dict_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2436 |
{ |
| 2437 |
return dict_update_common(self, args, kwds, "dict"); |
| 2438 |
} |
| 2439 |
|
| 2440 |
static PyObject * |
| 2441 |
dict_iter(PyDictObject *dict) |
| 2442 |
{ |
| 2443 |
return dictiter_new(dict, &PyDictIterKey_Type); |
| 2444 |
} |
| 2445 |
|
| 2446 |
PyDoc_STRVAR(dictionary_doc, |
| 2447 |
"dict() -> new empty dictionary\n" |
| 2448 |
"dict(mapping) -> new dictionary initialized from a mapping object's\n" |
| 2449 |
" (key, value) pairs\n" |
| 2450 |
"dict(iterable) -> new dictionary initialized as if via:\n" |
| 2451 |
" d = {}\n" |
| 2452 |
" for k, v in iterable:\n" |
| 2453 |
" d[k] = v\n" |
| 2454 |
"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n" |
| 2455 |
" in the keyword argument list. For example: dict(one=1, two=2)"); |
| 2456 |
|
| 2457 |
PyTypeObject PyDict_Type = { |
| 2458 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2459 |
"dict", |
| 2460 |
sizeof(PyDictObject), |
| 2461 |
0, |
| 2462 |
(destructor)dict_dealloc, |
| 2463 |
(printfunc)dict_print, |
| 2464 |
0, |
| 2465 |
0, |
| 2466 |
(cmpfunc)dict_compare, |
| 2467 |
(reprfunc)dict_repr, |
| 2468 |
0, |
| 2469 |
&dict_as_sequence, |
| 2470 |
&dict_as_mapping, |
| 2471 |
(hashfunc)PyObject_HashNotImplemented, |
| 2472 |
0, |
| 2473 |
0, |
| 2474 |
PyObject_GenericGetAttr, |
| 2475 |
0, |
| 2476 |
0, |
| 2477 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2478 |
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, |
| 2479 |
dictionary_doc, |
| 2480 |
dict_traverse, |
| 2481 |
dict_tp_clear, |
| 2482 |
dict_richcompare, |
| 2483 |
0, |
| 2484 |
(getiterfunc)dict_iter, |
| 2485 |
0, |
| 2486 |
mapp_methods, |
| 2487 |
0, |
| 2488 |
0, |
| 2489 |
0, |
| 2490 |
0, |
| 2491 |
0, |
| 2492 |
0, |
| 2493 |
0, |
| 2494 |
dict_init, |
| 2495 |
PyType_GenericAlloc, |
| 2496 |
dict_new, |
| 2497 |
PyObject_GC_Del, |
| 2498 |
}; |
| 2499 |
|
| 2500 |
|
| 2501 |
|
| 2502 |
PyObject * |
| 2503 |
PyDict_GetItemString(PyObject *v, const char *key) |
| 2504 |
{ |
| 2505 |
PyObject *kv, *rv; |
| 2506 |
kv = PyString_FromString(key); |
| 2507 |
if (kv == NULL) |
| 2508 |
return NULL; |
| 2509 |
rv = PyDict_GetItem(v, kv); |
| 2510 |
Py_DECREF(kv); |
| 2511 |
return rv; |
| 2512 |
} |
| 2513 |
|
| 2514 |
int |
| 2515 |
PyDict_SetItemString(PyObject *v, const char *key, PyObject *item) |
| 2516 |
{ |
| 2517 |
PyObject *kv; |
| 2518 |
int err; |
| 2519 |
kv = PyString_FromString(key); |
| 2520 |
if (kv == NULL) |
| 2521 |
return -1; |
| 2522 |
PyString_InternInPlace(&kv); |
| 2523 |
err = PyDict_SetItem(v, kv, item); |
| 2524 |
Py_DECREF(kv); |
| 2525 |
return err; |
| 2526 |
} |
| 2527 |
|
| 2528 |
int |
| 2529 |
PyDict_DelItemString(PyObject *v, const char *key) |
| 2530 |
{ |
| 2531 |
PyObject *kv; |
| 2532 |
int err; |
| 2533 |
kv = PyString_FromString(key); |
| 2534 |
if (kv == NULL) |
| 2535 |
return -1; |
| 2536 |
err = PyDict_DelItem(v, kv); |
| 2537 |
Py_DECREF(kv); |
| 2538 |
return err; |
| 2539 |
} |
| 2540 |
|
| 2541 |
|
| 2542 |
|
| 2543 |
typedef struct { |
| 2544 |
PyObject_HEAD |
| 2545 |
PyDictObject *di_dict; |
| 2546 |
Py_ssize_t di_used; |
| 2547 |
Py_ssize_t di_pos; |
| 2548 |
PyObject* di_result; |
| 2549 |
Py_ssize_t len; |
| 2550 |
} dictiterobject; |
| 2551 |
|
| 2552 |
static PyObject * |
| 2553 |
dictiter_new(PyDictObject *dict, PyTypeObject *itertype) |
| 2554 |
{ |
| 2555 |
dictiterobject *di; |
| 2556 |
di = PyObject_GC_New(dictiterobject, itertype); |
| 2557 |
if (di == NULL) |
| 2558 |
return NULL; |
| 2559 |
Py_INCREF(dict); |
| 2560 |
di->di_dict = dict; |
| 2561 |
di->di_used = dict->ma_used; |
| 2562 |
di->di_pos = 0; |
| 2563 |
di->len = dict->ma_used; |
| 2564 |
if (itertype == &PyDictIterItem_Type) { |
| 2565 |
di->di_result = PyTuple_Pack(2, Py_None, Py_None); |
| 2566 |
if (di->di_result == NULL) { |
| 2567 |
Py_DECREF(di); |
| 2568 |
return NULL; |
| 2569 |
} |
| 2570 |
} |
| 2571 |
else |
| 2572 |
di->di_result = NULL; |
| 2573 |
_PyObject_GC_TRACK(di); |
| 2574 |
return (PyObject *)di; |
| 2575 |
} |
| 2576 |
|
| 2577 |
static void |
| 2578 |
dictiter_dealloc(dictiterobject *di) |
| 2579 |
{ |
| 2580 |
|
| 2581 |
_PyObject_GC_UNTRACK(di); |
| 2582 |
Py_XDECREF(di->di_dict); |
| 2583 |
Py_XDECREF(di->di_result); |
| 2584 |
PyObject_GC_Del(di); |
| 2585 |
} |
| 2586 |
|
| 2587 |
static int |
| 2588 |
dictiter_traverse(dictiterobject *di, visitproc visit, void *arg) |
| 2589 |
{ |
| 2590 |
Py_VISIT(di->di_dict); |
| 2591 |
Py_VISIT(di->di_result); |
| 2592 |
return 0; |
| 2593 |
} |
| 2594 |
|
| 2595 |
static PyObject * |
| 2596 |
dictiter_len(dictiterobject *di) |
| 2597 |
{ |
| 2598 |
Py_ssize_t len = 0; |
| 2599 |
if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used) |
| 2600 |
len = di->len; |
| 2601 |
return PyInt_FromSize_t(len); |
| 2602 |
} |
| 2603 |
|
| 2604 |
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 2605 |
|
| 2606 |
static PyMethodDef dictiter_methods[] = { |
| 2607 |
{"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc}, |
| 2608 |
{NULL, NULL} |
| 2609 |
}; |
| 2610 |
|
| 2611 |
static PyObject *dictiter_iternextkey(dictiterobject *di) |
| 2612 |
{ |
| 2613 |
PyObject *key; |
| 2614 |
register Py_ssize_t i, mask; |
| 2615 |
register PyDictEntry *ep; |
| 2616 |
PyDictObject *d = di->di_dict; |
| 2617 |
|
| 2618 |
if (d == NULL) |
| 2619 |
return NULL; |
| 2620 |
assert (PyDict_Check(d)); |
| 2621 |
|
| 2622 |
if (di->di_used != d->ma_used) { |
| 2623 |
PyErr_SetString(PyExc_RuntimeError, |
| 2624 |
"dictionary changed size during iteration"); |
| 2625 |
di->di_used = -1; |
| 2626 |
return NULL; |
| 2627 |
} |
| 2628 |
|
| 2629 |
i = di->di_pos; |
| 2630 |
if (i < 0) |
| 2631 |
goto fail; |
| 2632 |
ep = d->ma_table; |
| 2633 |
mask = d->ma_mask; |
| 2634 |
while (i <= mask && ep[i].me_value == NULL) |
| 2635 |
i++; |
| 2636 |
di->di_pos = i+1; |
| 2637 |
if (i > mask) |
| 2638 |
goto fail; |
| 2639 |
di->len--; |
| 2640 |
key = ep[i].me_key; |
| 2641 |
Py_INCREF(key); |
| 2642 |
return key; |
| 2643 |
|
| 2644 |
fail: |
| 2645 |
di->di_dict = NULL; |
| 2646 |
Py_DECREF(d); |
| 2647 |
return NULL; |
| 2648 |
} |
| 2649 |
|
| 2650 |
PyTypeObject PyDictIterKey_Type = { |
| 2651 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2652 |
"dictionary-keyiterator", |
| 2653 |
sizeof(dictiterobject), |
| 2654 |
0, |
| 2655 |
|
| 2656 |
(destructor)dictiter_dealloc, |
| 2657 |
0, |
| 2658 |
0, |
| 2659 |
0, |
| 2660 |
0, |
| 2661 |
0, |
| 2662 |
0, |
| 2663 |
0, |
| 2664 |
0, |
| 2665 |
0, |
| 2666 |
0, |
| 2667 |
0, |
| 2668 |
PyObject_GenericGetAttr, |
| 2669 |
0, |
| 2670 |
0, |
| 2671 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 2672 |
0, |
| 2673 |
(traverseproc)dictiter_traverse, |
| 2674 |
0, |
| 2675 |
0, |
| 2676 |
0, |
| 2677 |
PyObject_SelfIter, |
| 2678 |
(iternextfunc)dictiter_iternextkey, |
| 2679 |
dictiter_methods, |
| 2680 |
0, |
| 2681 |
}; |
| 2682 |
|
| 2683 |
static PyObject *dictiter_iternextvalue(dictiterobject *di) |
| 2684 |
{ |
| 2685 |
PyObject *value; |
| 2686 |
register Py_ssize_t i, mask; |
| 2687 |
register PyDictEntry *ep; |
| 2688 |
PyDictObject *d = di->di_dict; |
| 2689 |
|
| 2690 |
if (d == NULL) |
| 2691 |
return NULL; |
| 2692 |
assert (PyDict_Check(d)); |
| 2693 |
|
| 2694 |
if (di->di_used != d->ma_used) { |
| 2695 |
PyErr_SetString(PyExc_RuntimeError, |
| 2696 |
"dictionary changed size during iteration"); |
| 2697 |
di->di_used = -1; |
| 2698 |
return NULL; |
| 2699 |
} |
| 2700 |
|
| 2701 |
i = di->di_pos; |
| 2702 |
mask = d->ma_mask; |
| 2703 |
if (i < 0 || i > mask) |
| 2704 |
goto fail; |
| 2705 |
ep = d->ma_table; |
| 2706 |
while ((value=ep[i].me_value) == NULL) { |
| 2707 |
i++; |
| 2708 |
if (i > mask) |
| 2709 |
goto fail; |
| 2710 |
} |
| 2711 |
di->di_pos = i+1; |
| 2712 |
di->len--; |
| 2713 |
Py_INCREF(value); |
| 2714 |
return value; |
| 2715 |
|
| 2716 |
fail: |
| 2717 |
di->di_dict = NULL; |
| 2718 |
Py_DECREF(d); |
| 2719 |
return NULL; |
| 2720 |
} |
| 2721 |
|
| 2722 |
PyTypeObject PyDictIterValue_Type = { |
| 2723 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2724 |
"dictionary-valueiterator", |
| 2725 |
sizeof(dictiterobject), |
| 2726 |
0, |
| 2727 |
|
| 2728 |
(destructor)dictiter_dealloc, |
| 2729 |
0, |
| 2730 |
0, |
| 2731 |
0, |
| 2732 |
0, |
| 2733 |
0, |
| 2734 |
0, |
| 2735 |
0, |
| 2736 |
0, |
| 2737 |
0, |
| 2738 |
0, |
| 2739 |
0, |
| 2740 |
PyObject_GenericGetAttr, |
| 2741 |
0, |
| 2742 |
0, |
| 2743 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 2744 |
0, |
| 2745 |
(traverseproc)dictiter_traverse, |
| 2746 |
0, |
| 2747 |
0, |
| 2748 |
0, |
| 2749 |
PyObject_SelfIter, |
| 2750 |
(iternextfunc)dictiter_iternextvalue, |
| 2751 |
dictiter_methods, |
| 2752 |
0, |
| 2753 |
}; |
| 2754 |
|
| 2755 |
static PyObject *dictiter_iternextitem(dictiterobject *di) |
| 2756 |
{ |
| 2757 |
PyObject *key, *value, *result; |
| 2758 |
register Py_ssize_t i, mask; |
| 2759 |
register PyDictEntry *ep; |
| 2760 |
PyDictObject *d = di->di_dict; |
| 2761 |
|
| 2762 |
if (d == NULL) |
| 2763 |
return NULL; |
| 2764 |
assert (PyDict_Check(d)); |
| 2765 |
|
| 2766 |
if (di->di_used != d->ma_used) { |
| 2767 |
PyErr_SetString(PyExc_RuntimeError, |
| 2768 |
"dictionary changed size during iteration"); |
| 2769 |
di->di_used = -1; |
| 2770 |
return NULL; |
| 2771 |
} |
| 2772 |
|
| 2773 |
i = di->di_pos; |
| 2774 |
if (i < 0) |
| 2775 |
goto fail; |
| 2776 |
ep = d->ma_table; |
| 2777 |
mask = d->ma_mask; |
| 2778 |
while (i <= mask && ep[i].me_value == NULL) |
| 2779 |
i++; |
| 2780 |
di->di_pos = i+1; |
| 2781 |
if (i > mask) |
| 2782 |
goto fail; |
| 2783 |
|
| 2784 |
di->len--; |
| 2785 |
key = ep[i].me_key; |
| 2786 |
value = ep[i].me_value; |
| 2787 |
Py_INCREF(key); |
| 2788 |
Py_INCREF(value); |
| 2789 |
result = di->di_result; |
| 2790 |
if (Py_REFCNT(result) == 1) { |
| 2791 |
PyObject *oldkey = PyTuple_GET_ITEM(result, 0); |
| 2792 |
PyObject *oldvalue = PyTuple_GET_ITEM(result, 1); |
| 2793 |
PyTuple_SET_ITEM(result, 0, key); |
| 2794 |
PyTuple_SET_ITEM(result, 1, value); |
| 2795 |
Py_INCREF(result); |
| 2796 |
Py_DECREF(oldkey); |
| 2797 |
Py_DECREF(oldvalue); |
| 2798 |
} else { |
| 2799 |
result = PyTuple_New(2); |
| 2800 |
if (result == NULL) |
| 2801 |
return NULL; |
| 2802 |
PyTuple_SET_ITEM(result, 0, key); |
| 2803 |
PyTuple_SET_ITEM(result, 1, value); |
| 2804 |
} |
| 2805 |
return result; |
| 2806 |
|
| 2807 |
fail: |
| 2808 |
di->di_dict = NULL; |
| 2809 |
Py_DECREF(d); |
| 2810 |
return NULL; |
| 2811 |
} |
| 2812 |
|
| 2813 |
PyTypeObject PyDictIterItem_Type = { |
| 2814 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2815 |
"dictionary-itemiterator", |
| 2816 |
sizeof(dictiterobject), |
| 2817 |
0, |
| 2818 |
|
| 2819 |
(destructor)dictiter_dealloc, |
| 2820 |
0, |
| 2821 |
0, |
| 2822 |
0, |
| 2823 |
0, |
| 2824 |
0, |
| 2825 |
0, |
| 2826 |
0, |
| 2827 |
0, |
| 2828 |
0, |
| 2829 |
0, |
| 2830 |
0, |
| 2831 |
PyObject_GenericGetAttr, |
| 2832 |
0, |
| 2833 |
0, |
| 2834 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 2835 |
0, |
| 2836 |
(traverseproc)dictiter_traverse, |
| 2837 |
0, |
| 2838 |
0, |
| 2839 |
0, |
| 2840 |
PyObject_SelfIter, |
| 2841 |
(iternextfunc)dictiter_iternextitem, |
| 2842 |
dictiter_methods, |
| 2843 |
0, |
| 2844 |
}; |
| 2845 |
|
| 2846 |
|
| 2847 |
|
| 2848 |
|
| 2849 |
|
| 2850 |
|
| 2851 |
|
| 2852 |
typedef struct { |
| 2853 |
PyObject_HEAD |
| 2854 |
PyDictObject *dv_dict; |
| 2855 |
} dictviewobject; |
| 2856 |
|
| 2857 |
|
| 2858 |
static void |
| 2859 |
dictview_dealloc(dictviewobject *dv) |
| 2860 |
{ |
| 2861 |
|
| 2862 |
_PyObject_GC_UNTRACK(dv); |
| 2863 |
Py_XDECREF(dv->dv_dict); |
| 2864 |
PyObject_GC_Del(dv); |
| 2865 |
} |
| 2866 |
|
| 2867 |
static int |
| 2868 |
dictview_traverse(dictviewobject *dv, visitproc visit, void *arg) |
| 2869 |
{ |
| 2870 |
Py_VISIT(dv->dv_dict); |
| 2871 |
return 0; |
| 2872 |
} |
| 2873 |
|
| 2874 |
static Py_ssize_t |
| 2875 |
dictview_len(dictviewobject *dv) |
| 2876 |
{ |
| 2877 |
Py_ssize_t len = 0; |
| 2878 |
if (dv->dv_dict != NULL) |
| 2879 |
len = dv->dv_dict->ma_used; |
| 2880 |
return len; |
| 2881 |
} |
| 2882 |
|
| 2883 |
static PyObject * |
| 2884 |
dictview_new(PyObject *dict, PyTypeObject *type) |
| 2885 |
{ |
| 2886 |
dictviewobject *dv; |
| 2887 |
if (dict == NULL) { |
| 2888 |
PyErr_BadInternalCall(); |
| 2889 |
return NULL; |
| 2890 |
} |
| 2891 |
if (!PyDict_Check(dict)) { |
| 2892 |
|
| 2893 |
PyErr_Format(PyExc_TypeError, |
| 2894 |
"%s() requires a dict argument, not '%s'", |
| 2895 |
type->tp_name, dict->ob_type->tp_name); |
| 2896 |
return NULL; |
| 2897 |
} |
| 2898 |
dv = PyObject_GC_New(dictviewobject, type); |
| 2899 |
if (dv == NULL) |
| 2900 |
return NULL; |
| 2901 |
Py_INCREF(dict); |
| 2902 |
dv->dv_dict = (PyDictObject *)dict; |
| 2903 |
_PyObject_GC_TRACK(dv); |
| 2904 |
return (PyObject *)dv; |
| 2905 |
} |
| 2906 |
|
| 2907 |
|
| 2908 |
|
| 2909 |
|
| 2910 |
|
| 2911 |
|
| 2912 |
|
| 2913 |
|
| 2914 |
|
| 2915 |
|
| 2916 |
|
| 2917 |
static int |
| 2918 |
all_contained_in(PyObject *self, PyObject *other) |
| 2919 |
{ |
| 2920 |
PyObject *iter = PyObject_GetIter(self); |
| 2921 |
int ok = 1; |
| 2922 |
|
| 2923 |
if (iter == NULL) |
| 2924 |
return -1; |
| 2925 |
for (;;) { |
| 2926 |
PyObject *next = PyIter_Next(iter); |
| 2927 |
if (next == NULL) { |
| 2928 |
if (PyErr_Occurred()) |
| 2929 |
ok = -1; |
| 2930 |
break; |
| 2931 |
} |
| 2932 |
ok = PySequence_Contains(other, next); |
| 2933 |
Py_DECREF(next); |
| 2934 |
if (ok <= 0) |
| 2935 |
break; |
| 2936 |
} |
| 2937 |
Py_DECREF(iter); |
| 2938 |
return ok; |
| 2939 |
} |
| 2940 |
|
| 2941 |
static PyObject * |
| 2942 |
dictview_richcompare(PyObject *self, PyObject *other, int op) |
| 2943 |
{ |
| 2944 |
Py_ssize_t len_self, len_other; |
| 2945 |
int ok; |
| 2946 |
PyObject *result; |
| 2947 |
|
| 2948 |
assert(self != NULL); |
| 2949 |
assert(PyDictViewSet_Check(self)); |
| 2950 |
assert(other != NULL); |
| 2951 |
|
| 2952 |
if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) { |
| 2953 |
Py_INCREF(Py_NotImplemented); |
| 2954 |
return Py_NotImplemented; |
| 2955 |
} |
| 2956 |
|
| 2957 |
len_self = PyObject_Size(self); |
| 2958 |
if (len_self < 0) |
| 2959 |
return NULL; |
| 2960 |
len_other = PyObject_Size(other); |
| 2961 |
if (len_other < 0) |
| 2962 |
return NULL; |
| 2963 |
|
| 2964 |
ok = 0; |
| 2965 |
switch(op) { |
| 2966 |
|
| 2967 |
case Py_NE: |
| 2968 |
case Py_EQ: |
| 2969 |
if (len_self == len_other) |
| 2970 |
ok = all_contained_in(self, other); |
| 2971 |
if (op == Py_NE && ok >= 0) |
| 2972 |
ok = !ok; |
| 2973 |
break; |
| 2974 |
|
| 2975 |
case Py_LT: |
| 2976 |
if (len_self < len_other) |
| 2977 |
ok = all_contained_in(self, other); |
| 2978 |
break; |
| 2979 |
|
| 2980 |
case Py_LE: |
| 2981 |
if (len_self <= len_other) |
| 2982 |
ok = all_contained_in(self, other); |
| 2983 |
break; |
| 2984 |
|
| 2985 |
case Py_GT: |
| 2986 |
if (len_self > len_other) |
| 2987 |
ok = all_contained_in(other, self); |
| 2988 |
break; |
| 2989 |
|
| 2990 |
case Py_GE: |
| 2991 |
if (len_self >= len_other) |
| 2992 |
ok = all_contained_in(other, self); |
| 2993 |
break; |
| 2994 |
|
| 2995 |
} |
| 2996 |
if (ok < 0) |
| 2997 |
return NULL; |
| 2998 |
result = ok ? Py_True : Py_False; |
| 2999 |
Py_INCREF(result); |
| 3000 |
return result; |
| 3001 |
} |
| 3002 |
|
| 3003 |
static PyObject * |
| 3004 |
dictview_repr(dictviewobject *dv) |
| 3005 |
{ |
| 3006 |
PyObject *seq; |
| 3007 |
PyObject *seq_str; |
| 3008 |
PyObject *result = NULL; |
| 3009 |
Py_ssize_t rc; |
| 3010 |
|
| 3011 |
rc = Py_ReprEnter((PyObject *)dv); |
| 3012 |
if (rc != 0) { |
| 3013 |
return rc > 0 ? PyString_FromString("...") : NULL; |
| 3014 |
} |
| 3015 |
seq = PySequence_List((PyObject *)dv); |
| 3016 |
if (seq == NULL) { |
| 3017 |
goto Done; |
| 3018 |
} |
| 3019 |
seq_str = PyObject_Repr(seq); |
| 3020 |
Py_DECREF(seq); |
| 3021 |
|
| 3022 |
if (seq_str == NULL) { |
| 3023 |
goto Done; |
| 3024 |
} |
| 3025 |
result = PyString_FromFormat("%s(%s)", Py_TYPE(dv)->tp_name, |
| 3026 |
PyString_AS_STRING(seq_str)); |
| 3027 |
Py_DECREF(seq_str); |
| 3028 |
|
| 3029 |
Done: |
| 3030 |
Py_ReprLeave((PyObject *)dv); |
| 3031 |
return result; |
| 3032 |
} |
| 3033 |
|
| 3034 |
|
| 3035 |
|
| 3036 |
static PyObject * |
| 3037 |
dictkeys_iter(dictviewobject *dv) |
| 3038 |
{ |
| 3039 |
if (dv->dv_dict == NULL) { |
| 3040 |
Py_RETURN_NONE; |
| 3041 |
} |
| 3042 |
return dictiter_new(dv->dv_dict, &PyDictIterKey_Type); |
| 3043 |
} |
| 3044 |
|
| 3045 |
static int |
| 3046 |
dictkeys_contains(dictviewobject *dv, PyObject *obj) |
| 3047 |
{ |
| 3048 |
if (dv->dv_dict == NULL) |
| 3049 |
return 0; |
| 3050 |
return PyDict_Contains((PyObject *)dv->dv_dict, obj); |
| 3051 |
} |
| 3052 |
|
| 3053 |
static PySequenceMethods dictkeys_as_sequence = { |
| 3054 |
(lenfunc)dictview_len, |
| 3055 |
0, |
| 3056 |
0, |
| 3057 |
0, |
| 3058 |
0, |
| 3059 |
0, |
| 3060 |
0, |
| 3061 |
(objobjproc)dictkeys_contains, |
| 3062 |
}; |
| 3063 |
|
| 3064 |
static PyObject* |
| 3065 |
dictviews_sub(PyObject* self, PyObject *other) |
| 3066 |
{ |
| 3067 |
PyObject *result = PySet_New(self); |
| 3068 |
PyObject *tmp; |
| 3069 |
if (result == NULL) |
| 3070 |
return NULL; |
| 3071 |
|
| 3072 |
|
| 3073 |
tmp = PyObject_CallMethod(result, "difference_update", "(O)", other); |
| 3074 |
if (tmp == NULL) { |
| 3075 |
Py_DECREF(result); |
| 3076 |
return NULL; |
| 3077 |
} |
| 3078 |
|
| 3079 |
Py_DECREF(tmp); |
| 3080 |
return result; |
| 3081 |
} |
| 3082 |
|
| 3083 |
static PyObject* |
| 3084 |
dictviews_and(PyObject* self, PyObject *other) |
| 3085 |
{ |
| 3086 |
PyObject *result = PySet_New(self); |
| 3087 |
PyObject *tmp; |
| 3088 |
if (result == NULL) |
| 3089 |
return NULL; |
| 3090 |
|
| 3091 |
tmp = PyObject_CallMethod(result, "intersection_update", "(O)", other); |
| 3092 |
if (tmp == NULL) { |
| 3093 |
Py_DECREF(result); |
| 3094 |
return NULL; |
| 3095 |
} |
| 3096 |
|
| 3097 |
Py_DECREF(tmp); |
| 3098 |
return result; |
| 3099 |
} |
| 3100 |
|
| 3101 |
static PyObject* |
| 3102 |
dictviews_or(PyObject* self, PyObject *other) |
| 3103 |
{ |
| 3104 |
PyObject *result = PySet_New(self); |
| 3105 |
PyObject *tmp; |
| 3106 |
if (result == NULL) |
| 3107 |
return NULL; |
| 3108 |
|
| 3109 |
tmp = PyObject_CallMethod(result, "update", "(O)", other); |
| 3110 |
if (tmp == NULL) { |
| 3111 |
Py_DECREF(result); |
| 3112 |
return NULL; |
| 3113 |
} |
| 3114 |
|
| 3115 |
Py_DECREF(tmp); |
| 3116 |
return result; |
| 3117 |
} |
| 3118 |
|
| 3119 |
static PyObject* |
| 3120 |
dictviews_xor(PyObject* self, PyObject *other) |
| 3121 |
{ |
| 3122 |
PyObject *result = PySet_New(self); |
| 3123 |
PyObject *tmp; |
| 3124 |
if (result == NULL) |
| 3125 |
return NULL; |
| 3126 |
|
| 3127 |
tmp = PyObject_CallMethod(result, "symmetric_difference_update", "(O)", other); |
| 3128 |
if (tmp == NULL) { |
| 3129 |
Py_DECREF(result); |
| 3130 |
return NULL; |
| 3131 |
} |
| 3132 |
|
| 3133 |
Py_DECREF(tmp); |
| 3134 |
return result; |
| 3135 |
} |
| 3136 |
|
| 3137 |
static PyNumberMethods dictviews_as_number = { |
| 3138 |
0, |
| 3139 |
(binaryfunc)dictviews_sub, |
| 3140 |
0, |
| 3141 |
0, |
| 3142 |
0, |
| 3143 |
0, |
| 3144 |
0, |
| 3145 |
0, |
| 3146 |
0, |
| 3147 |
0, |
| 3148 |
0, |
| 3149 |
0, |
| 3150 |
0, |
| 3151 |
0, |
| 3152 |
(binaryfunc)dictviews_and, |
| 3153 |
(binaryfunc)dictviews_xor, |
| 3154 |
(binaryfunc)dictviews_or, |
| 3155 |
}; |
| 3156 |
|
| 3157 |
static PyMethodDef dictkeys_methods[] = { |
| 3158 |
{NULL, NULL} |
| 3159 |
}; |
| 3160 |
|
| 3161 |
PyTypeObject PyDictKeys_Type = { |
| 3162 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3163 |
"dict_keys", |
| 3164 |
sizeof(dictviewobject), |
| 3165 |
0, |
| 3166 |
|
| 3167 |
(destructor)dictview_dealloc, |
| 3168 |
0, |
| 3169 |
0, |
| 3170 |
0, |
| 3171 |
0, |
| 3172 |
(reprfunc)dictview_repr, |
| 3173 |
&dictviews_as_number, |
| 3174 |
&dictkeys_as_sequence, |
| 3175 |
0, |
| 3176 |
0, |
| 3177 |
0, |
| 3178 |
0, |
| 3179 |
PyObject_GenericGetAttr, |
| 3180 |
0, |
| 3181 |
0, |
| 3182 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 3183 |
Py_TPFLAGS_CHECKTYPES, |
| 3184 |
0, |
| 3185 |
(traverseproc)dictview_traverse, |
| 3186 |
0, |
| 3187 |
dictview_richcompare, |
| 3188 |
0, |
| 3189 |
(getiterfunc)dictkeys_iter, |
| 3190 |
0, |
| 3191 |
dictkeys_methods, |
| 3192 |
0, |
| 3193 |
}; |
| 3194 |
|
| 3195 |
static PyObject * |
| 3196 |
dictkeys_new(PyObject *dict) |
| 3197 |
{ |
| 3198 |
return dictview_new(dict, &PyDictKeys_Type); |
| 3199 |
} |
| 3200 |
|
| 3201 |
|
| 3202 |
|
| 3203 |
static PyObject * |
| 3204 |
dictitems_iter(dictviewobject *dv) |
| 3205 |
{ |
| 3206 |
if (dv->dv_dict == NULL) { |
| 3207 |
Py_RETURN_NONE; |
| 3208 |
} |
| 3209 |
return dictiter_new(dv->dv_dict, &PyDictIterItem_Type); |
| 3210 |
} |
| 3211 |
|
| 3212 |
static int |
| 3213 |
dictitems_contains(dictviewobject *dv, PyObject *obj) |
| 3214 |
{ |
| 3215 |
int result; |
| 3216 |
PyObject *key, *value, *found; |
| 3217 |
if (dv->dv_dict == NULL) |
| 3218 |
return 0; |
| 3219 |
if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2) |
| 3220 |
return 0; |
| 3221 |
key = PyTuple_GET_ITEM(obj, 0); |
| 3222 |
value = PyTuple_GET_ITEM(obj, 1); |
| 3223 |
found = PyDict_GetItem((PyObject *)dv->dv_dict, key); |
| 3224 |
if (found == NULL) { |
| 3225 |
if (PyErr_Occurred()) |
| 3226 |
return -1; |
| 3227 |
return 0; |
| 3228 |
} |
| 3229 |
Py_INCREF(found); |
| 3230 |
result = PyObject_RichCompareBool(value, found, Py_EQ); |
| 3231 |
Py_DECREF(found); |
| 3232 |
return result; |
| 3233 |
} |
| 3234 |
|
| 3235 |
static PySequenceMethods dictitems_as_sequence = { |
| 3236 |
(lenfunc)dictview_len, |
| 3237 |
0, |
| 3238 |
0, |
| 3239 |
0, |
| 3240 |
0, |
| 3241 |
0, |
| 3242 |
0, |
| 3243 |
(objobjproc)dictitems_contains, |
| 3244 |
}; |
| 3245 |
|
| 3246 |
static PyMethodDef dictitems_methods[] = { |
| 3247 |
{NULL, NULL} |
| 3248 |
}; |
| 3249 |
|
| 3250 |
PyTypeObject PyDictItems_Type = { |
| 3251 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3252 |
"dict_items", |
| 3253 |
sizeof(dictviewobject), |
| 3254 |
0, |
| 3255 |
|
| 3256 |
(destructor)dictview_dealloc, |
| 3257 |
0, |
| 3258 |
0, |
| 3259 |
0, |
| 3260 |
0, |
| 3261 |
(reprfunc)dictview_repr, |
| 3262 |
&dictviews_as_number, |
| 3263 |
&dictitems_as_sequence, |
| 3264 |
0, |
| 3265 |
0, |
| 3266 |
0, |
| 3267 |
0, |
| 3268 |
PyObject_GenericGetAttr, |
| 3269 |
0, |
| 3270 |
0, |
| 3271 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 3272 |
Py_TPFLAGS_CHECKTYPES, |
| 3273 |
0, |
| 3274 |
(traverseproc)dictview_traverse, |
| 3275 |
0, |
| 3276 |
dictview_richcompare, |
| 3277 |
0, |
| 3278 |
(getiterfunc)dictitems_iter, |
| 3279 |
0, |
| 3280 |
dictitems_methods, |
| 3281 |
0, |
| 3282 |
}; |
| 3283 |
|
| 3284 |
static PyObject * |
| 3285 |
dictitems_new(PyObject *dict) |
| 3286 |
{ |
| 3287 |
return dictview_new(dict, &PyDictItems_Type); |
| 3288 |
} |
| 3289 |
|
| 3290 |
|
| 3291 |
|
| 3292 |
static PyObject * |
| 3293 |
dictvalues_iter(dictviewobject *dv) |
| 3294 |
{ |
| 3295 |
if (dv->dv_dict == NULL) { |
| 3296 |
Py_RETURN_NONE; |
| 3297 |
} |
| 3298 |
return dictiter_new(dv->dv_dict, &PyDictIterValue_Type); |
| 3299 |
} |
| 3300 |
|
| 3301 |
static PySequenceMethods dictvalues_as_sequence = { |
| 3302 |
(lenfunc)dictview_len, |
| 3303 |
0, |
| 3304 |
0, |
| 3305 |
0, |
| 3306 |
0, |
| 3307 |
0, |
| 3308 |
0, |
| 3309 |
(objobjproc)0, |
| 3310 |
}; |
| 3311 |
|
| 3312 |
static PyMethodDef dictvalues_methods[] = { |
| 3313 |
{NULL, NULL} |
| 3314 |
}; |
| 3315 |
|
| 3316 |
PyTypeObject PyDictValues_Type = { |
| 3317 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3318 |
"dict_values", |
| 3319 |
sizeof(dictviewobject), |
| 3320 |
0, |
| 3321 |
|
| 3322 |
(destructor)dictview_dealloc, |
| 3323 |
0, |
| 3324 |
0, |
| 3325 |
0, |
| 3326 |
0, |
| 3327 |
(reprfunc)dictview_repr, |
| 3328 |
0, |
| 3329 |
&dictvalues_as_sequence, |
| 3330 |
0, |
| 3331 |
0, |
| 3332 |
0, |
| 3333 |
0, |
| 3334 |
PyObject_GenericGetAttr, |
| 3335 |
0, |
| 3336 |
0, |
| 3337 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 3338 |
0, |
| 3339 |
(traverseproc)dictview_traverse, |
| 3340 |
0, |
| 3341 |
0, |
| 3342 |
0, |
| 3343 |
(getiterfunc)dictvalues_iter, |
| 3344 |
0, |
| 3345 |
dictvalues_methods, |
| 3346 |
0, |
| 3347 |
}; |
| 3348 |
|
| 3349 |
static PyObject * |
| 3350 |
dictvalues_new(PyObject *dict) |
| 3351 |
{ |
| 3352 |
return dictview_new(dict, &PyDictValues_Type); |
| 3353 |
} |