Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1 | /* Ordered Dictionary object implementation. |
| 2 | |
| 3 | This implementation is necessarily explicitly equivalent to the pure Python |
| 4 | OrderedDict class in Lib/collections/__init__.py. The strategy there |
| 5 | involves using a doubly-linked-list to capture the order. We keep to that |
| 6 | strategy, using a lower-level linked-list. |
| 7 | |
| 8 | About the Linked-List |
| 9 | ===================== |
| 10 | |
| 11 | For the linked list we use a basic doubly-linked-list. Using a circularly- |
| 12 | linked-list does have some benefits, but they don't apply so much here |
| 13 | since OrderedDict is focused on the ends of the list (for the most part). |
| 14 | Furthermore, there are some features of generic linked-lists that we simply |
| 15 | don't need for OrderedDict. Thus a simple custom implementation meets our |
| 16 | needs. Alternatives to our simple approach include the QCIRCLE_* |
| 17 | macros from BSD's queue.h, and the linux's list.h. |
| 18 | |
| 19 | Getting O(1) Node Lookup |
| 20 | ------------------------ |
| 21 | |
| 22 | One invariant of Python's OrderedDict is that it preserves time complexity |
| 23 | of dict's methods, particularly the O(1) operations. Simply adding a |
| 24 | linked-list on top of dict is not sufficient here; operations for nodes in |
| 25 | the middle of the linked-list implicitly require finding the node first. |
| 26 | With a simple linked-list like we're using, that is an O(n) operation. |
| 27 | Consequently, methods like __delitem__() would change from O(1) to O(n), |
| 28 | which is unacceptable. |
| 29 | |
| 30 | In order to preserve O(1) performance for node removal (finding nodes), we |
| 31 | must do better than just looping through the linked-list. Here are options |
| 32 | we've considered: |
| 33 | |
| 34 | 1. use a second dict to map keys to nodes (a la the pure Python version). |
| 35 | 2. keep a simple hash table mirroring the order of dict's, mapping each key |
| 36 | to the corresponding node in the linked-list. |
| 37 | 3. use a version of shared keys (split dict) that allows non-unicode keys. |
| 38 | 4. have the value stored for each key be a (value, node) pair, and adjust |
| 39 | __getitem__(), get(), etc. accordingly. |
| 40 | |
| 41 | The approach with the least performance impact (time and space) is #2, |
| 42 | mirroring the key order of dict's dk_enties with an array of node pointers. |
| 43 | While lookdict() and friends (dk_lookup) don't give us the index into the |
| 44 | array, we make use of pointer arithmetic to get that index. An alternative |
| 45 | would be to refactor lookdict() to provide the index, explicitly exposing |
| 46 | the implementation detail. We could even just use a custom lookup function |
| 47 | for OrderedDict that facilitates our need. However, both approaches are |
| 48 | significantly more complicated than just using pointer arithmetic. |
| 49 | |
| 50 | The catch with mirroring the hash table ordering is that we have to keep |
| 51 | the ordering in sync through any dict resizes. However, that order only |
| 52 | matters during node lookup. We can simply defer any potential resizing |
| 53 | until we need to do a lookup. |
| 54 | |
| 55 | Linked-List Nodes |
| 56 | ----------------- |
| 57 | |
| 58 | The current implementation stores a pointer to the associated key only. |
| 59 | One alternative would be to store a pointer to the PyDictKeyEntry instead. |
| 60 | This would save one pointer de-reference per item, which is nice during |
| 61 | calls to values() and items(). However, it adds unnecessary overhead |
| 62 | otherwise, so we stick with just the key. |
| 63 | |
| 64 | Linked-List API |
| 65 | --------------- |
| 66 | |
| 67 | As noted, the linked-list implemented here does not have all the bells and |
| 68 | whistles. However, we recognize that the implementation may need to |
| 69 | change to accommodate performance improvements or extra functionality. To |
| 70 | that end, We use a simple API to interact with the linked-list. Here's a |
| 71 | summary of the methods/macros: |
| 72 | |
| 73 | Node info: |
| 74 | |
| 75 | * _odictnode_KEY(node) |
| 76 | * _odictnode_VALUE(od, node) |
| 77 | * _odictnode_PREV(node) |
| 78 | * _odictnode_NEXT(node) |
| 79 | |
| 80 | Linked-List info: |
| 81 | |
| 82 | * _odict_FIRST(od) |
| 83 | * _odict_LAST(od) |
| 84 | * _odict_EMPTY(od) |
| 85 | * _odict_FOREACH(od, node) - used in place of `for (node=...)` |
| 86 | |
| 87 | For adding nodes: |
| 88 | |
| 89 | * _odict_add_head(od, node) |
| 90 | * _odict_add_tail(od, node) |
| 91 | * _odict_add_new_node(od, key) |
| 92 | |
| 93 | For removing nodes: |
| 94 | |
| 95 | * _odict_pop_node(od, node, key) |
| 96 | * _odict_clear_node(od, node) |
| 97 | * _odict_clear_nodes(od, clear_each) |
| 98 | |
| 99 | Others: |
| 100 | |
| 101 | * _odict_initialize(od) |
| 102 | * _odict_find_node(od, key) |
| 103 | * _odict_keys_equal(od1, od2) |
| 104 | |
| 105 | Used, but specific to the linked-list implementation: |
| 106 | |
| 107 | * _odict_free_fast_nodes(od) |
| 108 | |
| 109 | And here's a look at how the linked-list relates to the OrderedDict API: |
| 110 | |
| 111 | ============ === === ==== ==== ==== === ==== ===== ==== ==== === ==== === === |
| 112 | method key val prev next mem 1st last empty iter find add rmv clr keq |
| 113 | ============ === === ==== ==== ==== === ==== ===== ==== ==== === ==== === === |
| 114 | __del__ ~ X |
| 115 | __delitem__ free ~ node |
| 116 | __eq__ ~ X |
| 117 | __iter__ X X |
| 118 | __new__ X X |
| 119 | __reduce__ X ~ X |
| 120 | __repr__ X X X |
| 121 | __reversed__ X X |
| 122 | __setitem__ key |
| 123 | __sizeof__ size X |
| 124 | clear ~ ~ X |
| 125 | copy X X X |
| 126 | items X X X |
| 127 | keys X X |
| 128 | move_to_end X X X ~ h/t key |
| 129 | pop free key |
| 130 | popitem X X free X X node |
| 131 | setdefault ~ ? ~ |
| 132 | values X X |
| 133 | ============ === === ==== ==== ==== === ==== ===== ==== ==== === ==== === === |
| 134 | |
| 135 | __delitem__ is the only method that directly relies on finding an arbitrary |
| 136 | node in the linked-list. Everything else is iteration or relates to the |
| 137 | ends of the linked-list. |
| 138 | |
| 139 | Situation that Endangers Consistency |
| 140 | ------------------------------------ |
| 141 | Using a raw linked-list for OrderedDict exposes a key situation that can |
| 142 | cause problems. If a node is stored in a variable, there is a chance that |
| 143 | the node may have been deallocated before the variable gets used, thus |
| 144 | potentially leading to a segmentation fault. A key place where this shows |
| 145 | up is during iteration through the linked list (via _odict_FOREACH or |
| 146 | otherwise). |
| 147 | |
| 148 | A number of solutions are available to resolve this situation: |
| 149 | |
| 150 | * defer looking up the node until as late as possible and certainly after |
| 151 | any code that could possibly result in a deletion; |
| 152 | * if the node is needed both before and after a point where the node might |
| 153 | be removed, do a check before using the node at the "after" location to |
| 154 | see if the node is still valid; |
| 155 | * like the last one, but simply pull the node again to ensure it's right; |
| 156 | * keep the key in the variable instead of the node and then look up the |
| 157 | node using the key at the point where the node is needed (this is what |
| 158 | we do for the iterators). |
| 159 | |
| 160 | Another related problem, preserving consistent ordering during iteration, |
| 161 | is described below. That one is not exclusive to using linked-lists. |
| 162 | |
| 163 | |
| 164 | Challenges from Subclassing dict |
| 165 | ================================ |
| 166 | |
| 167 | OrderedDict subclasses dict, which is an unusual relationship between two |
| 168 | builtin types (other than the base object type). Doing so results in |
| 169 | some complication and deserves further explanation. There are two things |
| 170 | to consider here. First, in what circumstances or with what adjustments |
| 171 | can OrderedDict be used as a drop-in replacement for dict (at the C level)? |
| 172 | Second, how can the OrderedDict implementation leverage the dict |
| 173 | implementation effectively without introducing unnecessary coupling or |
| 174 | inefficiencies? |
| 175 | |
| 176 | This second point is reflected here and in the implementation, so the |
| 177 | further focus is on the first point. It is worth noting that for |
| 178 | overridden methods, the dict implementation is deferred to as much as |
| 179 | possible. Furthermore, coupling is limited to as little as is reasonable. |
| 180 | |
| 181 | Concrete API Compatibility |
| 182 | -------------------------- |
| 183 | |
| 184 | Use of the concrete C-API for dict (PyDict_*) with OrderedDict is |
| 185 | problematic. (See http://bugs.python.org/issue10977.) The concrete API |
| 186 | has a number of hard-coded assumptions tied to the dict implementation. |
| 187 | This is, in part, due to performance reasons, which is understandable |
| 188 | given the part dict plays in Python. |
| 189 | |
| 190 | Any attempt to replace dict with OrderedDict for any role in the |
| 191 | interpreter (e.g. **kwds) faces a challenge. Such any effort must |
| 192 | recognize that the instances in affected locations currently interact with |
| 193 | the concrete API. |
| 194 | |
| 195 | Here are some ways to address this challenge: |
| 196 | |
| 197 | 1. Change the relevant usage of the concrete API in CPython and add |
| 198 | PyDict_CheckExact() calls to each of the concrete API funcions. |
| 199 | 2. Adjust the relevant concrete API functions to explicitly accommodate |
| 200 | OrderedDict. |
| 201 | 3. As with #1, add the checks, but improve the abstract API with smart fast |
| 202 | paths for dict and OrderedDict, and refactor CPython to use the abstract |
| 203 | API. Improvements to the abstract API would be valuable regardless. |
| 204 | |
| 205 | Adding the checks to the concrete API would help make any interpreter |
| 206 | switch to OrderedDict less painful for extension modules. However, this |
| 207 | won't work. The equivalent C API call to `dict.__setitem__(obj, k, v)` |
| 208 | is 'PyDict_SetItem(obj, k, v)`. This illustrates how subclasses in C call |
| 209 | the base class's methods, since there is no equivalent of super() in the |
| 210 | C API. Calling into Python for parent class API would work, but some |
| 211 | extension modules already rely on this feature of the concrete API. |
| 212 | |
| 213 | For reference, here is a breakdown of some of the dict concrete API: |
| 214 | |
| 215 | ========================== ============= ======================= |
| 216 | concrete API uses abstract API |
| 217 | ========================== ============= ======================= |
| 218 | PyDict_Check PyMapping_Check |
| 219 | (PyDict_CheckExact) - |
| 220 | (PyDict_New) - |
| 221 | (PyDictProxy_New) - |
| 222 | PyDict_Clear - |
| 223 | PyDict_Contains PySequence_Contains |
| 224 | PyDict_Copy - |
| 225 | PyDict_SetItem PyObject_SetItem |
| 226 | PyDict_SetItemString PyMapping_SetItemString |
| 227 | PyDict_DelItem PyMapping_DelItem |
| 228 | PyDict_DelItemString PyMapping_DelItemString |
| 229 | PyDict_GetItem - |
| 230 | PyDict_GetItemWithError PyObject_GetItem |
| 231 | _PyDict_GetItemIdWithError - |
| 232 | PyDict_GetItemString PyMapping_GetItemString |
| 233 | PyDict_Items PyMapping_Items |
| 234 | PyDict_Keys PyMapping_Keys |
| 235 | PyDict_Values PyMapping_Values |
| 236 | PyDict_Size PyMapping_Size |
| 237 | PyMapping_Length |
| 238 | PyDict_Next PyIter_Next |
| 239 | _PyDict_Next - |
| 240 | PyDict_Merge - |
| 241 | PyDict_Update - |
| 242 | PyDict_MergeFromSeq2 - |
| 243 | PyDict_ClearFreeList - |
| 244 | - PyMapping_HasKeyString |
| 245 | - PyMapping_HasKey |
| 246 | ========================== ============= ======================= |
| 247 | |
| 248 | |
| 249 | The dict Interface Relative to OrderedDict |
| 250 | ========================================== |
| 251 | |
| 252 | Since OrderedDict subclasses dict, understanding the various methods and |
| 253 | attributes of dict is important for implementing OrderedDict. |
| 254 | |
| 255 | Relevant Type Slots |
| 256 | ------------------- |
| 257 | |
| 258 | ================= ================ =================== ================ |
| 259 | slot attribute object dict |
| 260 | ================= ================ =================== ================ |
| 261 | tp_dealloc - object_dealloc dict_dealloc |
| 262 | tp_repr __repr__ object_repr dict_repr |
| 263 | sq_contains __contains__ - dict_contains |
| 264 | mp_length __len__ - dict_length |
| 265 | mp_subscript __getitem__ - dict_subscript |
| 266 | mp_ass_subscript __setitem__ - dict_ass_sub |
| 267 | __delitem__ |
| 268 | tp_hash __hash__ _Py_HashPointer ..._HashNotImpl |
| 269 | tp_str __str__ object_str - |
| 270 | tp_getattro __getattribute__ ..._GenericGetAttr (repeated) |
| 271 | __getattr__ |
| 272 | tp_setattro __setattr__ ..._GenericSetAttr (disabled) |
| 273 | tp_doc __doc__ (literal) dictionary_doc |
| 274 | tp_traverse - - dict_traverse |
| 275 | tp_clear - - dict_tp_clear |
| 276 | tp_richcompare __eq__ object_richcompare dict_richcompare |
| 277 | __ne__ |
| 278 | tp_weaklistoffset (__weakref__) - - |
| 279 | tp_iter __iter__ - dict_iter |
| 280 | tp_dictoffset (__dict__) - - |
| 281 | tp_init __init__ object_init dict_init |
| 282 | tp_alloc - PyType_GenericAlloc (repeated) |
| 283 | tp_new __new__ object_new dict_new |
| 284 | tp_free - PyObject_Del PyObject_GC_Del |
| 285 | ================= ================ =================== ================ |
| 286 | |
| 287 | Relevant Methods |
| 288 | ---------------- |
| 289 | |
| 290 | ================ =================== =============== |
| 291 | method object dict |
| 292 | ================ =================== =============== |
| 293 | __reduce__ object_reduce - |
| 294 | __sizeof__ object_sizeof dict_sizeof |
| 295 | clear - dict_clear |
| 296 | copy - dict_copy |
| 297 | fromkeys - dict_fromkeys |
| 298 | get - dict_get |
| 299 | items - dictitems_new |
| 300 | keys - dictkeys_new |
| 301 | pop - dict_pop |
| 302 | popitem - dict_popitem |
| 303 | setdefault - dict_setdefault |
| 304 | update - dict_update |
| 305 | values - dictvalues_new |
| 306 | ================ =================== =============== |
| 307 | |
| 308 | |
| 309 | Pure Python OrderedDict |
| 310 | ======================= |
| 311 | |
| 312 | As already noted, compatibility with the pure Python OrderedDict |
| 313 | implementation is a key goal of this C implementation. To further that |
| 314 | goal, here's a summary of how OrderedDict-specific methods are implemented |
| 315 | in collections/__init__.py. Also provided is an indication of which |
| 316 | methods directly mutate or iterate the object, as well as any relationship |
| 317 | with the underlying linked-list. |
| 318 | |
| 319 | ============= ============== == ================ === === ==== |
| 320 | method impl used ll uses inq mut iter |
| 321 | ============= ============== == ================ === === ==== |
| 322 | __contains__ dict - - X |
| 323 | __delitem__ OrderedDict Y dict.__delitem__ X |
| 324 | __eq__ OrderedDict N OrderedDict ~ |
| 325 | dict.__eq__ |
| 326 | __iter__ |
| 327 | __getitem__ dict - - X |
| 328 | __iter__ OrderedDict Y - X |
| 329 | __init__ OrderedDict N update |
| 330 | __len__ dict - - X |
| 331 | __ne__ MutableMapping - __eq__ ~ |
| 332 | __reduce__ OrderedDict N OrderedDict ~ |
| 333 | __iter__ |
| 334 | __getitem__ |
| 335 | __repr__ OrderedDict N __class__ ~ |
| 336 | items |
| 337 | __reversed__ OrderedDict Y - X |
| 338 | __setitem__ OrderedDict Y __contains__ X |
| 339 | dict.__setitem__ |
| 340 | __sizeof__ OrderedDict Y __len__ ~ |
| 341 | __dict__ |
| 342 | clear OrderedDict Y dict.clear X |
| 343 | copy OrderedDict N __class__ |
| 344 | __init__ |
| 345 | fromkeys OrderedDict N __setitem__ |
| 346 | get dict - - ~ |
| 347 | items MutableMapping - ItemsView X |
| 348 | keys MutableMapping - KeysView X |
| 349 | move_to_end OrderedDict Y - X |
| 350 | pop OrderedDict N __contains__ X |
| 351 | __getitem__ |
| 352 | __delitem__ |
| 353 | popitem OrderedDict Y dict.pop X |
| 354 | setdefault OrderedDict N __contains__ ~ |
| 355 | __getitem__ |
| 356 | __setitem__ |
| 357 | update MutableMapping - __setitem__ ~ |
| 358 | values MutableMapping - ValuesView X |
| 359 | ============= ============== == ================ === === ==== |
| 360 | |
| 361 | __reversed__ and move_to_end are both exclusive to OrderedDict. |
| 362 | |
| 363 | |
| 364 | C OrderedDict Implementation |
| 365 | ============================ |
| 366 | |
| 367 | ================= ================ |
| 368 | slot impl |
| 369 | ================= ================ |
| 370 | tp_dealloc odict_dealloc |
| 371 | tp_repr odict_repr |
| 372 | mp_ass_subscript odict_ass_sub |
| 373 | tp_doc odict_doc |
| 374 | tp_traverse odict_traverse |
| 375 | tp_clear odict_tp_clear |
| 376 | tp_richcompare odict_richcompare |
| 377 | tp_weaklistoffset (offset) |
| 378 | tp_iter odict_iter |
| 379 | tp_dictoffset (offset) |
| 380 | tp_init odict_init |
| 381 | tp_alloc (repeated) |
| 382 | tp_new odict_new |
| 383 | ================= ================ |
| 384 | |
| 385 | ================= ================ |
| 386 | method impl |
| 387 | ================= ================ |
| 388 | __reduce__ odict_reduce |
| 389 | __sizeof__ odict_sizeof |
| 390 | clear odict_clear |
| 391 | copy odict_copy |
| 392 | fromkeys odict_fromkeys |
| 393 | items odictitems_new |
| 394 | keys odictkeys_new |
| 395 | pop odict_pop |
| 396 | popitem odict_popitem |
| 397 | setdefault odict_setdefault |
| 398 | update odict_update |
| 399 | values odictvalues_new |
| 400 | ================= ================ |
| 401 | |
| 402 | Inherited unchanged from object/dict: |
| 403 | |
| 404 | ================ ========================== |
| 405 | method type field |
| 406 | ================ ========================== |
| 407 | - tp_free |
| 408 | __contains__ tp_as_sequence.sq_contains |
| 409 | __getattr__ tp_getattro |
| 410 | __getattribute__ tp_getattro |
| 411 | __getitem__ tp_as_mapping.mp_subscript |
| 412 | __hash__ tp_hash |
| 413 | __len__ tp_as_mapping.mp_length |
| 414 | __setattr__ tp_setattro |
| 415 | __str__ tp_str |
| 416 | get - |
| 417 | ================ ========================== |
| 418 | |
| 419 | |
| 420 | Other Challenges |
| 421 | ================ |
| 422 | |
| 423 | Preserving Ordering During Iteration |
| 424 | ------------------------------------ |
| 425 | During iteration through an OrderedDict, it is possible that items could |
| 426 | get added, removed, or reordered. For a linked-list implementation, as |
| 427 | with some other implementations, that situation may lead to undefined |
| 428 | behavior. The documentation for dict mentions this in the `iter()` section |
| 429 | of http://docs.python.org/3.4/library/stdtypes.html#dictionary-view-objects. |
| 430 | In this implementation we follow dict's lead (as does the pure Python |
| 431 | implementation) for __iter__(), keys(), values(), and items(). |
| 432 | |
| 433 | For internal iteration (using _odict_FOREACH or not), there is still the |
| 434 | risk that not all nodes that we expect to be seen in the loop actually get |
| 435 | seen. Thus, we are careful in each of those places to ensure that they |
| 436 | are. This comes, of course, at a small price at each location. The |
| 437 | solutions are much the same as those detailed in the `Situation that |
| 438 | Endangers Consistency` section above. |
| 439 | |
| 440 | |
| 441 | Potential Optimizations |
| 442 | ======================= |
| 443 | |
| 444 | * Allocate the nodes as a block via od_fast_nodes instead of individually. |
| 445 | - Set node->key to NULL to indicate the node is not-in-use. |
| 446 | - Add _odict_EXISTS()? |
| 447 | - How to maintain consistency across resizes? Existing node pointers |
| 448 | would be invalidate after a resize, which is particularly problematic |
| 449 | for the iterators. |
| 450 | * Use a more stream-lined implementation of update() and, likely indirectly, |
| 451 | __init__(). |
| 452 | |
| 453 | */ |
| 454 | |
| 455 | /* TODO |
| 456 | |
| 457 | sooner: |
| 458 | - reentrancy (make sure everything is at a thread-safe state when calling |
| 459 | into Python). I've already checked this multiple times, but want to |
| 460 | make one more pass. |
| 461 | - add unit tests for reentrancy? |
| 462 | |
| 463 | later: |
| 464 | - make the dict views support the full set API (the pure Python impl does) |
| 465 | - implement a fuller MutableMapping API in C? |
| 466 | - move the MutableMapping implementation to abstract.c? |
| 467 | - optimize mutablemapping_update |
| 468 | - use PyObject_MALLOC (small object allocator) for odict nodes? |
| 469 | - support subclasses better (e.g. in odict_richcompare) |
| 470 | |
| 471 | */ |
| 472 | |
| 473 | #include "Python.h" |
| 474 | #include "structmember.h" |
| 475 | #include "dict-common.h" |
| 476 | #include <stddef.h> |
| 477 | |
| 478 | |
| 479 | typedef struct _odictnode _ODictNode; |
| 480 | |
| 481 | /* PyODictObject */ |
| 482 | struct _odictobject { |
Eric Snow | 4fabf02 | 2015-06-04 00:09:56 -0600 | [diff] [blame] | 483 | PyDictObject od_dict; /* the underlying dict */ |
| 484 | _ODictNode *od_first; /* first node in the linked list, if any */ |
| 485 | _ODictNode *od_last; /* last node in the linked list, if any */ |
| 486 | /* od_size and od_fast_nodes are managed by _odict_resize() */ |
| 487 | Py_ssize_t od_size; /* hash table that mirrors the dict table */ |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 488 | _ODictNode **od_fast_nodes; /* managed by _odict_resize() */ |
Eric Snow | 4fabf02 | 2015-06-04 00:09:56 -0600 | [diff] [blame] | 489 | size_t od_state; /* incremented whenever the LL changes */ |
| 490 | PyObject *od_inst_dict; /* OrderedDict().__dict__ */ |
| 491 | PyObject *od_weakreflist; /* holds weakrefs to the odict */ |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 492 | }; |
| 493 | |
| 494 | |
| 495 | /* ---------------------------------------------- |
| 496 | * odict keys (a simple doubly-linked list) |
| 497 | */ |
| 498 | |
| 499 | struct _odictnode { |
| 500 | PyObject *key; |
Eric Snow | 4c72918 | 2015-06-03 10:50:37 -0600 | [diff] [blame] | 501 | Py_hash_t hash; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 502 | _ODictNode *next; |
| 503 | _ODictNode *prev; |
| 504 | }; |
| 505 | |
| 506 | #define _odictnode_KEY(node) \ |
| 507 | (node->key) |
Eric Snow | 4c72918 | 2015-06-03 10:50:37 -0600 | [diff] [blame] | 508 | #define _odictnode_HASH(node) \ |
| 509 | (node->hash) |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 510 | /* borrowed reference */ |
| 511 | #define _odictnode_VALUE(node, od) \ |
Eric Snow | a762af7 | 2015-06-01 22:59:08 -0600 | [diff] [blame] | 512 | PyODict_GetItemWithError((PyObject *)od, _odictnode_KEY(node)) |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 513 | /* If needed we could also have _odictnode_HASH. */ |
| 514 | #define _odictnode_PREV(node) (node->prev) |
| 515 | #define _odictnode_NEXT(node) (node->next) |
| 516 | |
| 517 | #define _odict_FIRST(od) (((PyODictObject *)od)->od_first) |
| 518 | #define _odict_LAST(od) (((PyODictObject *)od)->od_last) |
| 519 | #define _odict_EMPTY(od) (_odict_FIRST(od) == NULL) |
| 520 | #define _odict_FOREACH(od, node) \ |
| 521 | for (node = _odict_FIRST(od); node != NULL; node = _odictnode_NEXT(node)) |
| 522 | |
| 523 | |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 524 | static void |
| 525 | _odict_free_fast_nodes(PyODictObject *od) { |
| 526 | if (od->od_fast_nodes) { |
| 527 | PyMem_FREE(od->od_fast_nodes); |
| 528 | } |
| 529 | } |
| 530 | |
Eric Snow | 4c72918 | 2015-06-03 10:50:37 -0600 | [diff] [blame] | 531 | /* Return the index into the hash table, regardless of a valid node. */ |
| 532 | static Py_ssize_t |
| 533 | _odict_get_index_hash(PyODictObject *od, PyObject *key, Py_hash_t hash) |
| 534 | { |
| 535 | PyObject **value_addr = NULL; |
| 536 | PyDictKeyEntry *ep; |
| 537 | PyDictKeysObject *keys = ((PyDictObject *)od)->ma_keys; |
| 538 | |
| 539 | ep = (keys->dk_lookup)((PyDictObject *)od, key, hash, &value_addr); |
| 540 | if (ep == NULL) |
| 541 | return -1; |
| 542 | /* We use pointer arithmetic to get the entry's index into the table. */ |
| 543 | return ep - keys->dk_entries; |
| 544 | } |
| 545 | |
| 546 | /* Replace od->od_fast_nodes with a new table matching the size of dict's. */ |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 547 | static int |
| 548 | _odict_resize(PyODictObject *od) { |
Eric Snow | 4c72918 | 2015-06-03 10:50:37 -0600 | [diff] [blame] | 549 | Py_ssize_t size, i; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 550 | _ODictNode **fast_nodes, *node; |
| 551 | |
| 552 | /* Initialize a new "fast nodes" table. */ |
| 553 | size = ((PyDictObject *)od)->ma_keys->dk_size; |
| 554 | fast_nodes = PyMem_NEW(_ODictNode *, size); |
| 555 | if (fast_nodes == NULL) { |
| 556 | PyErr_NoMemory(); |
| 557 | return -1; |
| 558 | } |
| 559 | for (i = 0; i < size; i++) |
| 560 | fast_nodes[i] = NULL; |
| 561 | |
| 562 | /* Copy the current nodes into the table. */ |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 563 | _odict_FOREACH(od, node) { |
Eric Snow | 4c72918 | 2015-06-03 10:50:37 -0600 | [diff] [blame] | 564 | i = _odict_get_index_hash(od, _odictnode_KEY(node), |
| 565 | _odictnode_HASH(node)); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 566 | if (i < 0) { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 567 | PyMem_FREE(fast_nodes); |
| 568 | return -1; |
| 569 | } |
| 570 | fast_nodes[i] = node; |
| 571 | } |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 572 | |
| 573 | /* Replace the old fast nodes table. */ |
| 574 | _odict_free_fast_nodes(od); |
| 575 | od->od_fast_nodes = fast_nodes; |
Eric Snow | 4c72918 | 2015-06-03 10:50:37 -0600 | [diff] [blame] | 576 | od->od_size = size; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | /* Return the index into the hash table, regardless of a valid node. */ |
| 581 | static Py_ssize_t |
| 582 | _odict_get_index(PyODictObject *od, PyObject *key) |
| 583 | { |
| 584 | Py_hash_t hash; |
Eric Snow | 4c72918 | 2015-06-03 10:50:37 -0600 | [diff] [blame] | 585 | PyDictKeysObject *keys; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 586 | |
| 587 | assert(key != NULL); |
Eric Snow | 4c72918 | 2015-06-03 10:50:37 -0600 | [diff] [blame] | 588 | hash = PyObject_Hash(key); |
| 589 | if (hash == -1) |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 590 | return -1; |
Eric Snow | 4c72918 | 2015-06-03 10:50:37 -0600 | [diff] [blame] | 591 | keys = ((PyDictObject *)od)->ma_keys; |
| 592 | |
| 593 | /* Ensure od_fast_nodes and dk_entries are in sync. */ |
| 594 | if (keys->dk_size != od->od_size) { |
| 595 | int resize_res = _odict_resize(od); |
| 596 | if (resize_res < 0) |
| 597 | return -1; |
| 598 | } |
| 599 | |
| 600 | return _odict_get_index_hash(od, key, hash); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | static int |
| 604 | _odict_initialize(PyODictObject *od) |
| 605 | { |
Eric Snow | 4fabf02 | 2015-06-04 00:09:56 -0600 | [diff] [blame] | 606 | od->od_state = 0; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 607 | _odict_FIRST(od) = NULL; |
| 608 | _odict_LAST(od) = NULL; |
| 609 | return _odict_resize((PyODictObject *)od); |
| 610 | } |
| 611 | |
| 612 | /* Returns NULL if there was some error or the key was not found. */ |
| 613 | static _ODictNode * |
| 614 | _odict_find_node(PyODictObject *od, PyObject *key) |
| 615 | { |
| 616 | Py_ssize_t index; |
| 617 | |
| 618 | if (_odict_EMPTY(od)) |
| 619 | return NULL; |
| 620 | index = _odict_get_index(od, key); |
| 621 | if (index < 0) |
| 622 | return NULL; |
| 623 | return od->od_fast_nodes[index]; |
| 624 | } |
| 625 | |
| 626 | static void |
| 627 | _odict_add_head(PyODictObject *od, _ODictNode *node) |
| 628 | { |
| 629 | if (_odict_FIRST(od) == NULL) { |
| 630 | _odictnode_PREV(node) = NULL; |
| 631 | _odictnode_NEXT(node) = NULL; |
| 632 | _odict_FIRST(od) = node; |
| 633 | _odict_LAST(od) = node; |
| 634 | } |
| 635 | else { |
| 636 | _odictnode_PREV(node) = NULL; |
| 637 | _odictnode_NEXT(node) = _odict_FIRST(od); |
| 638 | _odict_FIRST(od) = node; |
| 639 | _odictnode_PREV(_odict_FIRST(od)) = node; |
| 640 | } |
Eric Snow | 4fabf02 | 2015-06-04 00:09:56 -0600 | [diff] [blame] | 641 | od->od_state++; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | static void |
| 645 | _odict_add_tail(PyODictObject *od, _ODictNode *node) |
| 646 | { |
| 647 | if (_odict_LAST(od) == NULL) { |
| 648 | _odictnode_PREV(node) = NULL; |
| 649 | _odictnode_NEXT(node) = NULL; |
| 650 | _odict_FIRST(od) = node; |
| 651 | _odict_LAST(od) = node; |
| 652 | } |
| 653 | else { |
| 654 | _odictnode_PREV(node) = _odict_LAST(od); |
| 655 | _odictnode_NEXT(node) = NULL; |
| 656 | _odictnode_NEXT(_odict_LAST(od)) = node; |
| 657 | _odict_LAST(od) = node; |
| 658 | } |
Eric Snow | 4fabf02 | 2015-06-04 00:09:56 -0600 | [diff] [blame] | 659 | od->od_state++; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /* adds the node to the end of the list */ |
| 663 | static int |
| 664 | _odict_add_new_node(PyODictObject *od, PyObject *key) |
| 665 | { |
Eric Snow | 4c72918 | 2015-06-03 10:50:37 -0600 | [diff] [blame] | 666 | Py_hash_t hash; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 667 | Py_ssize_t i; |
| 668 | _ODictNode *node; |
| 669 | |
| 670 | Py_INCREF(key); |
Eric Snow | 4c72918 | 2015-06-03 10:50:37 -0600 | [diff] [blame] | 671 | hash = PyObject_Hash(key); |
| 672 | if (hash == -1) |
| 673 | return -1; |
| 674 | |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 675 | i = _odict_get_index(od, key); |
| 676 | if (i < 0) { |
| 677 | if (!PyErr_Occurred()) |
| 678 | PyErr_SetObject(PyExc_KeyError, key); |
| 679 | Py_DECREF(key); |
| 680 | return -1; |
| 681 | } |
| 682 | else if (od->od_fast_nodes[i] != NULL) { |
| 683 | /* We already have a node for the key so there's no need to add one. */ |
| 684 | Py_DECREF(key); |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | /* must not be added yet */ |
| 689 | node = (_ODictNode *)PyMem_MALLOC(sizeof(_ODictNode)); |
| 690 | if (node == NULL) { |
| 691 | Py_DECREF(key); |
| 692 | PyErr_NoMemory(); |
| 693 | return -1; |
| 694 | } |
| 695 | |
| 696 | _odictnode_KEY(node) = key; |
Eric Snow | 4c72918 | 2015-06-03 10:50:37 -0600 | [diff] [blame] | 697 | _odictnode_HASH(node) = hash; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 698 | _odict_add_tail(od, node); |
| 699 | od->od_fast_nodes[i] = node; |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | /* Putting the decref after the free causes problems. */ |
| 704 | #define _odictnode_DEALLOC(node) \ |
| 705 | do { \ |
| 706 | Py_DECREF(_odictnode_KEY(node)); \ |
| 707 | PyMem_FREE((void *)node); \ |
| 708 | } while (0) |
| 709 | |
| 710 | /* Repeated calls on the same node are no-ops. */ |
| 711 | static void |
| 712 | _odict_remove_node(PyODictObject *od, _ODictNode *node) |
| 713 | { |
| 714 | if (_odict_FIRST(od) == node) |
| 715 | _odict_FIRST(od) = _odictnode_NEXT(node); |
| 716 | else if (_odictnode_PREV(node) != NULL) |
| 717 | _odictnode_NEXT(_odictnode_PREV(node)) = _odictnode_NEXT(node); |
| 718 | |
| 719 | if (_odict_LAST(od) == node) |
| 720 | _odict_LAST(od) = _odictnode_PREV(node); |
| 721 | else if (_odictnode_NEXT(node) != NULL) |
| 722 | _odictnode_PREV(_odictnode_NEXT(node)) = _odictnode_PREV(node); |
| 723 | |
| 724 | _odictnode_PREV(node) = NULL; |
| 725 | _odictnode_NEXT(node) = NULL; |
Eric Snow | 4fabf02 | 2015-06-04 00:09:56 -0600 | [diff] [blame] | 726 | od->od_state++; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | static _ODictNode * |
| 730 | _odict_pop_node(PyODictObject *od, _ODictNode *node, PyObject *key) |
| 731 | { |
| 732 | if (node == NULL) { |
| 733 | node = _odict_find_node(od, key); |
| 734 | if (node == NULL) |
| 735 | return NULL; |
| 736 | } |
| 737 | _odict_remove_node(od, node); |
| 738 | return node; |
| 739 | } |
| 740 | |
| 741 | /* If someone calls PyDict_DelItem() directly on an OrderedDict, we'll |
| 742 | get all sorts of problems here. In PyODict_DelItem we make sure to |
| 743 | call _odict_clear_node first. |
| 744 | |
| 745 | This matters in the case of colliding keys. Suppose we add 3 keys: |
| 746 | [A, B, C], where the hash of C collides with A and the next possible |
| 747 | index in the hash table is occupied by B. If we remove B then for C |
| 748 | the dict's looknode func will give us the old index of B instead of |
| 749 | the index we got before deleting B. However, the node for C in |
| 750 | od_fast_nodes is still at the old dict index of C. Thus to be sure |
| 751 | things don't get out of sync, we clear the node in od_fast_nodes |
| 752 | *before* calling PyDict_DelItem. |
| 753 | |
| 754 | The same must be done for any other OrderedDict operations where |
| 755 | we modify od_fast_nodes. |
| 756 | */ |
| 757 | static int |
| 758 | _odict_clear_node(PyODictObject *od, _ODictNode *node, PyObject *key) |
| 759 | { |
| 760 | Py_ssize_t i; |
| 761 | |
| 762 | assert(key != NULL); |
| 763 | if (_odict_EMPTY(od)) { |
| 764 | /* Let later code decide if this is a KeyError. */ |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | i = _odict_get_index(od, key); |
| 769 | if (i < 0) |
| 770 | return PyErr_Occurred() ? -1 : 0; |
| 771 | |
| 772 | if (node == NULL) |
| 773 | node = od->od_fast_nodes[i]; |
| 774 | assert(node == od->od_fast_nodes[i]); |
| 775 | if (node == NULL) { |
| 776 | /* Let later code decide if this is a KeyError. */ |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | // Now clear the node. |
| 781 | od->od_fast_nodes[i] = NULL; |
| 782 | _odict_remove_node(od, node); |
| 783 | _odictnode_DEALLOC(node); |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | static void |
| 788 | _odict_clear_nodes(PyODictObject *od) |
| 789 | { |
| 790 | _ODictNode *node, *next; |
| 791 | |
| 792 | if (!_odict_EMPTY(od)) { |
| 793 | node = _odict_FIRST(od); |
| 794 | while (node != NULL) { |
| 795 | next = _odictnode_NEXT(node); |
| 796 | _odictnode_DEALLOC(node); |
| 797 | node = next; |
| 798 | } |
| 799 | _odict_FIRST(od) = NULL; |
| 800 | _odict_LAST(od) = NULL; |
| 801 | } |
| 802 | |
| 803 | _odict_free_fast_nodes(od); |
| 804 | od->od_fast_nodes = NULL; |
| 805 | } |
| 806 | |
| 807 | /* There isn't any memory management of nodes past this point. */ |
| 808 | #undef _odictnode_DEALLOC |
| 809 | |
| 810 | static int |
| 811 | _odict_keys_equal(PyODictObject *a, PyODictObject *b) |
| 812 | { |
| 813 | _ODictNode *node_a, *node_b; |
| 814 | |
| 815 | node_a = _odict_FIRST(a); |
| 816 | node_b = _odict_FIRST(b); |
| 817 | while (1) { |
| 818 | if (node_a == NULL && node_b == NULL) |
| 819 | /* success: hit the end of each at the same time */ |
| 820 | return 1; |
| 821 | else if (node_a == NULL || node_b == NULL) |
| 822 | /* unequal length */ |
| 823 | return 0; |
| 824 | else { |
| 825 | int res = PyObject_RichCompareBool( |
| 826 | (PyObject *)_odictnode_KEY(node_a), |
| 827 | (PyObject *)_odictnode_KEY(node_b), |
| 828 | Py_EQ); |
| 829 | if (res < 0) |
| 830 | return res; |
| 831 | else if (res == 0) |
| 832 | return 0; |
| 833 | |
| 834 | /* otherwise it must match, so move on to the next one */ |
| 835 | node_a = _odictnode_NEXT(node_a); |
| 836 | node_b = _odictnode_NEXT(node_b); |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | |
| 842 | /* ---------------------------------------------- |
| 843 | * OrderedDict mapping methods |
| 844 | */ |
| 845 | |
| 846 | /* mp_ass_subscript: __setitem__() and __delitem__() */ |
| 847 | |
| 848 | static int |
| 849 | odict_mp_ass_sub(PyODictObject *od, PyObject *v, PyObject *w) |
| 850 | { |
| 851 | if (w == NULL) |
| 852 | return PyODict_DelItem((PyObject *)od, v); |
| 853 | else |
| 854 | return PyODict_SetItem((PyObject *)od, v, w); |
| 855 | } |
| 856 | |
| 857 | /* tp_as_mapping */ |
| 858 | |
| 859 | static PyMappingMethods odict_as_mapping = { |
| 860 | 0, /*mp_length*/ |
| 861 | 0, /*mp_subscript*/ |
| 862 | (objobjargproc)odict_mp_ass_sub, /*mp_ass_subscript*/ |
| 863 | }; |
| 864 | |
| 865 | |
| 866 | /* ---------------------------------------------- |
| 867 | * OrderedDict methods |
| 868 | */ |
| 869 | |
| 870 | /* __delitem__() */ |
| 871 | |
| 872 | PyDoc_STRVAR(odict_delitem__doc__, "od.__delitem__(y) <==> del od[y]"); |
| 873 | |
| 874 | /* __eq__() */ |
| 875 | |
| 876 | PyDoc_STRVAR(odict_eq__doc__, |
| 877 | "od.__eq__(y) <==> od==y. Comparison to another OD is order-sensitive \n\ |
| 878 | while comparison to a regular mapping is order-insensitive.\n\ |
| 879 | "); |
| 880 | |
| 881 | /* forward */ |
| 882 | static PyObject * odict_richcompare(PyObject *v, PyObject *w, int op); |
| 883 | |
| 884 | static PyObject * |
| 885 | odict_eq(PyObject *a, PyObject *b) |
| 886 | { |
| 887 | return odict_richcompare(a, b, Py_EQ); |
| 888 | } |
| 889 | |
| 890 | /* __init__() */ |
| 891 | |
| 892 | PyDoc_STRVAR(odict_init__doc__, |
| 893 | "Initialize an ordered dictionary. The signature is the same as\n\ |
| 894 | regular dictionaries, but keyword arguments are not recommended because\n\ |
| 895 | their insertion order is arbitrary.\n\ |
| 896 | \n\ |
| 897 | "); |
| 898 | |
| 899 | /* forward */ |
| 900 | static int odict_init(PyObject *self, PyObject *args, PyObject *kwds); |
| 901 | |
| 902 | /* __iter__() */ |
| 903 | |
| 904 | PyDoc_STRVAR(odict_iter__doc__, "od.__iter__() <==> iter(od)"); |
| 905 | |
| 906 | static PyObject * odict_iter(PyODictObject *self); /* forward */ |
| 907 | |
| 908 | /* __ne__() */ |
| 909 | |
| 910 | /* Mapping.__ne__() does not have a docstring. */ |
| 911 | PyDoc_STRVAR(odict_ne__doc__, ""); |
| 912 | |
| 913 | static PyObject * |
| 914 | odict_ne(PyObject *a, PyObject *b) |
| 915 | { |
| 916 | return odict_richcompare(a, b, Py_NE); |
| 917 | } |
| 918 | |
| 919 | /* __repr__() */ |
| 920 | |
| 921 | PyDoc_STRVAR(odict_repr__doc__, "od.__repr__() <==> repr(od)"); |
| 922 | |
| 923 | static PyObject * odict_repr(PyODictObject *self); /* forward */ |
| 924 | |
| 925 | /* __setitem__() */ |
| 926 | |
| 927 | PyDoc_STRVAR(odict_setitem__doc__, "od.__setitem__(i, y) <==> od[i]=y"); |
| 928 | |
| 929 | /* fromkeys() */ |
| 930 | |
| 931 | PyDoc_STRVAR(odict_fromkeys__doc__, |
| 932 | "OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S.\n\ |
| 933 | If not specified, the value defaults to None.\n\ |
| 934 | \n\ |
| 935 | "); |
| 936 | |
| 937 | static PyObject * |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 938 | odict_fromkeys(PyObject *cls, PyObject *args, PyObject *kwargs) |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 939 | { |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 940 | static char *kwlist[] = {"iterable", "value", 0}; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 941 | PyObject *seq; |
| 942 | PyObject *value = Py_None; |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 943 | |
| 944 | /* both borrowed */ |
| 945 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:fromkeys", kwlist, |
| 946 | &seq, &value)) { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 947 | return NULL; |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 948 | } |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 949 | return _PyDict_FromKeys(cls, seq, value); |
| 950 | } |
| 951 | |
| 952 | /* __sizeof__() */ |
| 953 | |
| 954 | /* OrderedDict.__sizeof__() does not have a docstring. */ |
| 955 | PyDoc_STRVAR(odict_sizeof__doc__, ""); |
| 956 | |
| 957 | static PyObject * |
| 958 | odict_sizeof(PyODictObject *od) |
| 959 | { |
| 960 | PyObject *pylong; |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 961 | Py_ssize_t res, temp; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 962 | |
| 963 | pylong = _PyDict_SizeOf((PyDictObject *)od); |
| 964 | if (pylong == NULL) |
| 965 | return NULL; |
| 966 | res = PyLong_AsSsize_t(pylong); |
| 967 | Py_DECREF(pylong); |
| 968 | if (res == -1 && PyErr_Occurred()) |
| 969 | return NULL; |
| 970 | |
| 971 | res += sizeof(PyODictObject) - sizeof(PyDictObject); |
| 972 | |
| 973 | /* instance dict */ |
| 974 | pylong = _PyDict_SizeOf((PyDictObject *)od->od_inst_dict); |
| 975 | if (pylong == NULL) |
| 976 | return NULL; |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 977 | temp = PyLong_AsSsize_t(pylong); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 978 | Py_DECREF(pylong); |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 979 | if (temp == -1 && PyErr_Occurred()) |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 980 | return NULL; |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 981 | res += temp; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 982 | |
| 983 | res += sizeof(_ODictNode) * od->od_size; /* od_fast_nodes */ |
| 984 | if (!_odict_EMPTY(od)) { |
| 985 | res += sizeof(_ODictNode) * PyODict_SIZE(od); /* linked-list */ |
| 986 | } |
| 987 | return PyLong_FromSsize_t(res); |
| 988 | } |
| 989 | |
| 990 | /* __reduce__() */ |
| 991 | |
| 992 | PyDoc_STRVAR(odict_reduce__doc__, "Return state information for pickling"); |
| 993 | |
| 994 | static PyObject * |
| 995 | odict_reduce(register PyODictObject *od) |
| 996 | { |
| 997 | _Py_IDENTIFIER(__dict__); |
| 998 | _Py_IDENTIFIER(__class__); |
| 999 | PyObject *vars = NULL, *ns = NULL, *result = NULL, *cls = NULL; |
| 1000 | PyObject *items_iter = NULL, *items = NULL, *args = NULL; |
| 1001 | |
| 1002 | /* capture any instance state */ |
| 1003 | vars = _PyObject_GetAttrId((PyObject *)od, &PyId___dict__); |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 1004 | if (vars == NULL) |
| 1005 | goto Done; |
| 1006 | else { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1007 | PyObject *empty, *od_vars, *iterator, *key; |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 1008 | int ns_len; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1009 | |
| 1010 | /* od.__dict__ isn't necessarily a dict... */ |
| 1011 | ns = PyObject_CallMethod((PyObject *)vars, "copy", NULL); |
| 1012 | if (ns == NULL) |
| 1013 | goto Done; |
| 1014 | empty = PyODict_New(); |
| 1015 | if (empty == NULL) |
| 1016 | goto Done; |
| 1017 | od_vars = _PyObject_GetAttrId((PyObject *)empty, &PyId___dict__); |
| 1018 | Py_DECREF(empty); |
| 1019 | if (od_vars == NULL) |
| 1020 | goto Done; |
| 1021 | iterator = PyObject_GetIter(od_vars); |
| 1022 | Py_DECREF(od_vars); |
| 1023 | if (iterator == NULL) |
| 1024 | goto Done; |
| 1025 | |
| 1026 | while ( (key = PyIter_Next(iterator)) ) { |
| 1027 | if (PyMapping_HasKey(ns, key) && PyMapping_DelItem(ns, key) != 0) { |
| 1028 | Py_DECREF(iterator); |
| 1029 | Py_DECREF(key); |
| 1030 | goto Done; |
| 1031 | } |
| 1032 | Py_DECREF(key); |
| 1033 | } |
| 1034 | Py_DECREF(iterator); |
| 1035 | if (PyErr_Occurred()) |
| 1036 | goto Done; |
| 1037 | |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 1038 | ns_len = PyObject_Length(ns); |
| 1039 | if (ns_len == -1) |
| 1040 | goto Done; |
| 1041 | if (!ns_len) { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1042 | /* nothing novel to pickle in od.__dict__ */ |
Eric Snow | e3dfa9e | 2015-05-30 12:51:15 -0600 | [diff] [blame] | 1043 | Py_CLEAR(ns); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | /* build the result */ |
| 1048 | cls = _PyObject_GetAttrId((PyObject *)od, &PyId___class__); |
| 1049 | if (cls == NULL) |
| 1050 | goto Done; |
| 1051 | |
| 1052 | args = PyTuple_New(0); |
| 1053 | if (args == NULL) |
| 1054 | goto Done; |
| 1055 | |
| 1056 | items = PyObject_CallMethod((PyObject *)od, "items", NULL); |
| 1057 | if (items == NULL) |
| 1058 | goto Done; |
| 1059 | |
| 1060 | items_iter = PyObject_GetIter(items); |
| 1061 | if (items_iter == NULL) |
| 1062 | goto Done; |
| 1063 | |
| 1064 | result = PyTuple_Pack(5, cls, args, ns ? ns : Py_None, Py_None, items_iter); |
| 1065 | |
| 1066 | Done: |
| 1067 | Py_XDECREF(vars); |
| 1068 | Py_XDECREF(ns); |
| 1069 | Py_XDECREF(cls); |
| 1070 | Py_XDECREF(args); |
| 1071 | Py_XDECREF(items); |
| 1072 | Py_XDECREF(items_iter); |
| 1073 | |
| 1074 | return result; |
| 1075 | } |
| 1076 | |
| 1077 | /* setdefault() */ |
| 1078 | |
| 1079 | PyDoc_STRVAR(odict_setdefault__doc__, |
| 1080 | "od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od"); |
| 1081 | |
| 1082 | /* Skips __missing__() calls. */ |
| 1083 | static PyObject * |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1084 | odict_setdefault(register PyODictObject *od, PyObject *args, PyObject *kwargs) |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1085 | { |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1086 | static char *kwlist[] = {"key", "default", 0}; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1087 | PyObject *key, *result = NULL; |
| 1088 | PyObject *failobj = Py_None; |
| 1089 | |
| 1090 | /* both borrowed */ |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1091 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:setdefault", kwlist, |
| 1092 | &key, &failobj)) { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1093 | return NULL; |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1094 | } |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1095 | |
| 1096 | if (PyODict_CheckExact(od)) { |
Eric Snow | d171975 | 2015-06-01 23:12:13 -0600 | [diff] [blame] | 1097 | result = PyODict_GetItemWithError(od, key); /* borrowed */ |
| 1098 | if (result == NULL) { |
| 1099 | if (PyErr_Occurred()) |
| 1100 | return NULL; |
| 1101 | assert(_odict_find_node(od, key) == NULL); |
| 1102 | if (PyODict_SetItem((PyObject *)od, key, failobj) >= 0) { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1103 | result = failobj; |
| 1104 | Py_INCREF(failobj); |
| 1105 | } |
| 1106 | } |
| 1107 | else { |
Eric Snow | d171975 | 2015-06-01 23:12:13 -0600 | [diff] [blame] | 1108 | Py_INCREF(result); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | else { |
| 1112 | int exists = PySequence_Contains((PyObject *)od, key); |
| 1113 | if (exists < 0) { |
Eric Snow | d171975 | 2015-06-01 23:12:13 -0600 | [diff] [blame] | 1114 | return NULL; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1115 | } |
| 1116 | else if (exists) { |
| 1117 | result = PyObject_GetItem((PyObject *)od, key); |
| 1118 | } |
| 1119 | else if (PyObject_SetItem((PyObject *)od, key, failobj) >= 0) { |
| 1120 | result = failobj; |
| 1121 | Py_INCREF(failobj); |
| 1122 | } |
| 1123 | } |
| 1124 | |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1125 | return result; |
| 1126 | } |
| 1127 | |
| 1128 | /* pop() */ |
| 1129 | |
| 1130 | PyDoc_STRVAR(odict_pop__doc__, |
| 1131 | "od.pop(k[,d]) -> v, remove specified key and return the corresponding\n\ |
| 1132 | value. If key is not found, d is returned if given, otherwise KeyError\n\ |
| 1133 | is raised.\n\ |
| 1134 | \n\ |
| 1135 | "); |
| 1136 | |
| 1137 | /* forward */ |
| 1138 | static PyObject * _odict_popkey(PyObject *, PyObject *, PyObject *); |
| 1139 | |
| 1140 | /* Skips __missing__() calls. */ |
| 1141 | static PyObject * |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1142 | odict_pop(PyObject *od, PyObject *args, PyObject *kwargs) |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1143 | { |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1144 | static char *kwlist[] = {"key", "default", 0}; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1145 | PyObject *key, *failobj = NULL; |
| 1146 | |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1147 | /* borrowed */ |
| 1148 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:pop", kwlist, |
| 1149 | &key, &failobj)) { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1150 | return NULL; |
| 1151 | } |
| 1152 | |
| 1153 | return _odict_popkey(od, key, failobj); |
| 1154 | } |
| 1155 | |
| 1156 | static PyObject * |
| 1157 | _odict_popkey(PyObject *od, PyObject *key, PyObject *failobj) |
| 1158 | { |
| 1159 | _ODictNode *node; |
| 1160 | PyObject *value = NULL; |
| 1161 | |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1162 | /* Pop the node first to avoid a possible dict resize (due to |
| 1163 | eval loop reentrancy) and complications due to hash collision |
| 1164 | resolution. */ |
| 1165 | node = _odict_find_node((PyODictObject *)od, key); |
| 1166 | if (node == NULL) { |
| 1167 | if (PyErr_Occurred()) |
Eric Snow | d171975 | 2015-06-01 23:12:13 -0600 | [diff] [blame] | 1168 | return NULL; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1169 | } |
| 1170 | else { |
| 1171 | int res = _odict_clear_node((PyODictObject *)od, node, key); |
| 1172 | if (res < 0) { |
Eric Snow | d171975 | 2015-06-01 23:12:13 -0600 | [diff] [blame] | 1173 | return NULL; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | /* Now delete the value from the dict. */ |
| 1178 | if (PyODict_CheckExact(od)) { |
| 1179 | if (node != NULL) { |
| 1180 | /* We could do PyDict_GetItem() and PyDict_DelItem() directly... */ |
| 1181 | value = _PyDict_Pop((PyDictObject *)od, key, failobj); |
| 1182 | } |
| 1183 | } |
| 1184 | else { |
| 1185 | int exists = PySequence_Contains(od, key); |
| 1186 | if (exists < 0) |
Eric Snow | d171975 | 2015-06-01 23:12:13 -0600 | [diff] [blame] | 1187 | return NULL; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1188 | if (exists) { |
| 1189 | value = PyObject_GetItem(od, key); |
| 1190 | if (value != NULL) { |
| 1191 | if (PyObject_DelItem(od, key) == -1) { |
Eric Snow | e3dfa9e | 2015-05-30 12:51:15 -0600 | [diff] [blame] | 1192 | Py_CLEAR(value); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | /* Apply the fallback value, if necessary. */ |
| 1199 | if (value == NULL && !PyErr_Occurred()) { |
| 1200 | if (failobj) { |
| 1201 | value = failobj; |
| 1202 | Py_INCREF(failobj); |
| 1203 | } |
| 1204 | else { |
| 1205 | PyErr_SetObject(PyExc_KeyError, key); |
| 1206 | } |
| 1207 | } |
| 1208 | |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1209 | return value; |
| 1210 | } |
| 1211 | |
| 1212 | /* popitem() */ |
| 1213 | |
| 1214 | PyDoc_STRVAR(odict_popitem__doc__, |
| 1215 | "od.popitem() -> (k, v), return and remove a (key, value) pair.\n\ |
| 1216 | Pairs are returned in LIFO order if last is true or FIFO order if false.\n\ |
| 1217 | \n\ |
| 1218 | "); |
| 1219 | |
| 1220 | static PyObject * |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1221 | odict_popitem(PyObject *od, PyObject *args, PyObject *kwargs) |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1222 | { |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1223 | static char *kwlist[] = {"last", 0}; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1224 | PyObject *key, *value, *item = NULL, *last = NULL; |
| 1225 | _ODictNode *node; |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1226 | int pos = -1; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1227 | |
| 1228 | if (_odict_EMPTY((PyODictObject *)od)) { |
| 1229 | PyErr_SetString(PyExc_KeyError, "dictionary is empty"); |
| 1230 | return NULL; |
| 1231 | } |
| 1232 | |
| 1233 | /* pull the item */ |
| 1234 | |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1235 | /* borrowed */ |
| 1236 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:popitem", kwlist, |
| 1237 | &last)) { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1238 | return NULL; |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1239 | } |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1240 | |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1241 | if (last != NULL) { |
| 1242 | int is_true; |
| 1243 | is_true = PyObject_IsTrue(last); |
| 1244 | if (is_true == -1) |
| 1245 | return NULL; |
| 1246 | pos = is_true ? -1 : 0; |
| 1247 | } |
| 1248 | if (pos == 0) |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1249 | node = _odict_FIRST((PyODictObject *)od); |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1250 | else |
| 1251 | node = _odict_LAST((PyODictObject *)od); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1252 | |
| 1253 | key = _odictnode_KEY(node); |
Eric Snow | d171975 | 2015-06-01 23:12:13 -0600 | [diff] [blame] | 1254 | Py_INCREF(key); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1255 | value = _odict_popkey(od, key, NULL); |
| 1256 | if (value == NULL) |
| 1257 | return NULL; |
| 1258 | item = PyTuple_Pack(2, key, value); |
Eric Snow | d171975 | 2015-06-01 23:12:13 -0600 | [diff] [blame] | 1259 | Py_DECREF(key); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1260 | Py_DECREF(value); |
| 1261 | return item; |
| 1262 | } |
| 1263 | |
| 1264 | /* keys() */ |
| 1265 | |
| 1266 | /* MutableMapping.keys() does not have a docstring. */ |
| 1267 | PyDoc_STRVAR(odict_keys__doc__, ""); |
| 1268 | |
| 1269 | static PyObject * odictkeys_new(PyObject *od); /* forward */ |
| 1270 | |
| 1271 | /* values() */ |
| 1272 | |
| 1273 | /* MutableMapping.values() does not have a docstring. */ |
| 1274 | PyDoc_STRVAR(odict_values__doc__, ""); |
| 1275 | |
| 1276 | static PyObject * odictvalues_new(PyObject *od); /* forward */ |
| 1277 | |
| 1278 | /* items() */ |
| 1279 | |
| 1280 | /* MutableMapping.items() does not have a docstring. */ |
| 1281 | PyDoc_STRVAR(odict_items__doc__, ""); |
| 1282 | |
| 1283 | static PyObject * odictitems_new(PyObject *od); /* forward */ |
| 1284 | |
| 1285 | /* update() */ |
| 1286 | |
| 1287 | /* MutableMapping.update() does not have a docstring. */ |
| 1288 | PyDoc_STRVAR(odict_update__doc__, ""); |
| 1289 | |
| 1290 | /* forward */ |
| 1291 | static PyObject * mutablemapping_update(PyObject *, PyObject *, PyObject *); |
| 1292 | |
| 1293 | #define odict_update mutablemapping_update |
| 1294 | |
| 1295 | /* clear() */ |
| 1296 | |
| 1297 | PyDoc_STRVAR(odict_clear__doc__, |
| 1298 | "od.clear() -> None. Remove all items from od."); |
| 1299 | |
| 1300 | static PyObject * |
| 1301 | odict_clear(register PyODictObject *od) |
| 1302 | { |
| 1303 | PyDict_Clear((PyObject *)od); |
| 1304 | _odict_clear_nodes(od); |
| 1305 | _odict_FIRST(od) = NULL; |
| 1306 | _odict_LAST(od) = NULL; |
| 1307 | if (_odict_resize(od) < 0) |
| 1308 | return NULL; |
| 1309 | Py_RETURN_NONE; |
| 1310 | } |
| 1311 | |
| 1312 | /* copy() */ |
| 1313 | |
| 1314 | PyDoc_STRVAR(odict_copy__doc__, "od.copy() -> a shallow copy of od"); |
| 1315 | |
| 1316 | static PyObject * |
| 1317 | odict_copy(register PyODictObject *od) |
| 1318 | { |
| 1319 | _ODictNode *node; |
| 1320 | PyObject *od_copy; |
| 1321 | |
| 1322 | if (PyODict_CheckExact(od)) |
| 1323 | od_copy = PyODict_New(); |
| 1324 | else |
| 1325 | od_copy = PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(od), NULL); |
| 1326 | if (od_copy == NULL) |
| 1327 | return NULL; |
| 1328 | |
| 1329 | if (PyODict_CheckExact(od)) { |
| 1330 | _odict_FOREACH(od, node) { |
Eric Snow | a762af7 | 2015-06-01 22:59:08 -0600 | [diff] [blame] | 1331 | PyObject *key = _odictnode_KEY(node); |
| 1332 | PyObject *value = _odictnode_VALUE(node, od); |
| 1333 | if (value == NULL) { |
| 1334 | if (!PyErr_Occurred()) |
| 1335 | PyErr_SetObject(PyExc_KeyError, key); |
| 1336 | goto fail; |
| 1337 | } |
| 1338 | if (PyODict_SetItem((PyObject *)od_copy, key, value) != 0) |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1339 | goto fail; |
| 1340 | } |
| 1341 | } |
| 1342 | else { |
| 1343 | _odict_FOREACH(od, node) { |
| 1344 | int res; |
| 1345 | PyObject *value = PyObject_GetItem((PyObject *)od, |
| 1346 | _odictnode_KEY(node)); |
| 1347 | if (value == NULL) |
| 1348 | goto fail; |
| 1349 | res = PyObject_SetItem((PyObject *)od_copy, |
| 1350 | _odictnode_KEY(node), value); |
| 1351 | Py_DECREF(value); |
| 1352 | if (res != 0) |
| 1353 | goto fail; |
| 1354 | } |
| 1355 | } |
| 1356 | return od_copy; |
| 1357 | |
| 1358 | fail: |
| 1359 | Py_DECREF(od_copy); |
| 1360 | return NULL; |
| 1361 | } |
| 1362 | |
| 1363 | /* __reversed__() */ |
| 1364 | |
| 1365 | PyDoc_STRVAR(odict_reversed__doc__, "od.__reversed__() <==> reversed(od)"); |
| 1366 | |
| 1367 | #define _odict_ITER_REVERSED 1 |
| 1368 | #define _odict_ITER_KEYS 2 |
| 1369 | #define _odict_ITER_VALUES 4 |
| 1370 | |
| 1371 | /* forward */ |
| 1372 | static PyObject * odictiter_new(PyODictObject *, int); |
| 1373 | |
| 1374 | static PyObject * |
| 1375 | odict_reversed(PyODictObject *od) |
| 1376 | { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1377 | return odictiter_new(od, _odict_ITER_KEYS|_odict_ITER_REVERSED); |
| 1378 | } |
| 1379 | |
| 1380 | /* move_to_end() */ |
| 1381 | |
| 1382 | PyDoc_STRVAR(odict_move_to_end__doc__, |
| 1383 | "Move an existing element to the end (or beginning if last==False).\n\ |
| 1384 | \n\ |
| 1385 | Raises KeyError if the element does not exist.\n\ |
| 1386 | When last=True, acts like a fast version of self[key]=self.pop(key).\n\ |
| 1387 | \n\ |
| 1388 | "); |
| 1389 | |
| 1390 | static PyObject * |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1391 | odict_move_to_end(PyODictObject *od, PyObject *args, PyObject *kwargs) |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1392 | { |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1393 | static char *kwlist[] = {"key", "last", 0}; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1394 | PyObject *key, *last = NULL; |
| 1395 | Py_ssize_t pos = -1; |
| 1396 | |
| 1397 | /* both borrowed */ |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1398 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:move_to_end", kwlist, |
| 1399 | &key, &last)) { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1400 | return NULL; |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1401 | } |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1402 | if (_odict_EMPTY(od)) { |
| 1403 | PyErr_SetObject(PyExc_KeyError, key); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1404 | return NULL; |
| 1405 | } |
| 1406 | if (last != NULL) { |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 1407 | int is_true; |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 1408 | is_true = PyObject_IsTrue(last); |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 1409 | if (is_true == -1) |
| 1410 | return NULL; |
| 1411 | pos = is_true ? -1 : 0; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1412 | } |
| 1413 | if (pos == 0) { |
| 1414 | /* Only move if not already the first one. */ |
| 1415 | PyObject *first_key = _odictnode_KEY(_odict_FIRST(od)); |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 1416 | int not_equal = PyObject_RichCompareBool(key, first_key, Py_NE); |
| 1417 | if (not_equal == -1) |
| 1418 | return NULL; |
| 1419 | if (not_equal) { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1420 | _ODictNode *node = _odict_pop_node(od, NULL, key); |
| 1421 | if (node != NULL) { |
| 1422 | _odict_add_head(od, node); |
| 1423 | } |
| 1424 | else { |
| 1425 | if (!PyErr_Occurred()) |
| 1426 | PyErr_SetObject(PyExc_KeyError, key); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1427 | return NULL; |
| 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | else if (pos == -1) { |
| 1432 | /* Only move if not already the last one. */ |
| 1433 | PyObject *last_key = _odictnode_KEY(_odict_LAST(od)); |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 1434 | int not_equal = PyObject_RichCompareBool(key, last_key, Py_NE); |
| 1435 | if (not_equal == -1) |
| 1436 | return NULL; |
| 1437 | if (not_equal) { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1438 | _ODictNode *node = _odict_pop_node(od, NULL, key); |
| 1439 | if (node != NULL) { |
| 1440 | _odict_add_tail(od, node); |
| 1441 | } |
| 1442 | else { |
| 1443 | if (!PyErr_Occurred()) |
| 1444 | PyErr_SetObject(PyExc_KeyError, key); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1445 | return NULL; |
| 1446 | } |
| 1447 | } |
| 1448 | } |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1449 | Py_RETURN_NONE; |
| 1450 | } |
| 1451 | |
| 1452 | |
| 1453 | /* tp_methods */ |
| 1454 | |
| 1455 | static PyMethodDef odict_methods[] = { |
| 1456 | |
| 1457 | /* explicitly defined so we can align docstrings with |
| 1458 | * collections.OrderedDict */ |
| 1459 | {"__delitem__", (PyCFunction)odict_mp_ass_sub, METH_NOARGS, |
| 1460 | odict_delitem__doc__}, |
| 1461 | {"__eq__", (PyCFunction)odict_eq, METH_NOARGS, |
| 1462 | odict_eq__doc__}, |
| 1463 | {"__init__", (PyCFunction)odict_init, METH_NOARGS, |
| 1464 | odict_init__doc__}, |
| 1465 | {"__iter__", (PyCFunction)odict_iter, METH_NOARGS, |
| 1466 | odict_iter__doc__}, |
| 1467 | {"__ne__", (PyCFunction)odict_ne, METH_NOARGS, |
| 1468 | odict_ne__doc__}, |
| 1469 | {"__repr__", (PyCFunction)odict_repr, METH_NOARGS, |
| 1470 | odict_repr__doc__}, |
| 1471 | {"__setitem__", (PyCFunction)odict_mp_ass_sub, METH_NOARGS, |
| 1472 | odict_setitem__doc__}, |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1473 | {"fromkeys", (PyCFunction)odict_fromkeys, |
| 1474 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, odict_fromkeys__doc__}, |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1475 | |
| 1476 | /* overridden dict methods */ |
| 1477 | {"__sizeof__", (PyCFunction)odict_sizeof, METH_NOARGS, |
| 1478 | odict_sizeof__doc__}, |
| 1479 | {"__reduce__", (PyCFunction)odict_reduce, METH_NOARGS, |
| 1480 | odict_reduce__doc__}, |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1481 | {"setdefault", (PyCFunction)odict_setdefault, |
| 1482 | METH_VARARGS | METH_KEYWORDS, odict_setdefault__doc__}, |
| 1483 | {"pop", (PyCFunction)odict_pop, |
| 1484 | METH_VARARGS | METH_KEYWORDS, odict_pop__doc__}, |
| 1485 | {"popitem", (PyCFunction)odict_popitem, |
| 1486 | METH_VARARGS | METH_KEYWORDS, odict_popitem__doc__}, |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1487 | {"keys", (PyCFunction)odictkeys_new, METH_NOARGS, |
| 1488 | odict_keys__doc__}, |
| 1489 | {"values", (PyCFunction)odictvalues_new, METH_NOARGS, |
| 1490 | odict_values__doc__}, |
| 1491 | {"items", (PyCFunction)odictitems_new, METH_NOARGS, |
| 1492 | odict_items__doc__}, |
| 1493 | {"update", (PyCFunction)odict_update, METH_VARARGS | METH_KEYWORDS, |
| 1494 | odict_update__doc__}, |
| 1495 | {"clear", (PyCFunction)odict_clear, METH_NOARGS, |
| 1496 | odict_clear__doc__}, |
| 1497 | {"copy", (PyCFunction)odict_copy, METH_NOARGS, |
| 1498 | odict_copy__doc__}, |
| 1499 | |
| 1500 | /* new methods */ |
| 1501 | {"__reversed__", (PyCFunction)odict_reversed, METH_NOARGS, |
| 1502 | odict_reversed__doc__}, |
Eric Snow | ac02ef3 | 2015-06-02 20:42:14 -0600 | [diff] [blame] | 1503 | {"move_to_end", (PyCFunction)odict_move_to_end, |
| 1504 | METH_VARARGS | METH_KEYWORDS, odict_move_to_end__doc__}, |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1505 | |
| 1506 | {NULL, NULL} /* sentinel */ |
| 1507 | }; |
| 1508 | |
| 1509 | |
| 1510 | /* ---------------------------------------------- |
| 1511 | * OrderedDict members |
| 1512 | */ |
| 1513 | |
| 1514 | /* tp_members */ |
| 1515 | |
| 1516 | static PyMemberDef odict_members[] = { |
| 1517 | {"__dict__", T_OBJECT, offsetof(PyODictObject, od_inst_dict), READONLY}, |
| 1518 | {0} |
| 1519 | }; |
| 1520 | |
| 1521 | |
| 1522 | /* ---------------------------------------------- |
| 1523 | * OrderedDict type slot methods |
| 1524 | */ |
| 1525 | |
| 1526 | /* tp_dealloc */ |
| 1527 | |
| 1528 | static void |
| 1529 | odict_dealloc(PyODictObject *self) |
| 1530 | { |
| 1531 | PyObject_GC_UnTrack(self); |
| 1532 | Py_TRASHCAN_SAFE_BEGIN(self); |
| 1533 | Py_XDECREF(self->od_inst_dict); |
| 1534 | if (self->od_weakreflist != NULL) |
| 1535 | PyObject_ClearWeakRefs((PyObject *)self); |
| 1536 | |
| 1537 | _odict_clear_nodes(self); |
| 1538 | Py_TRASHCAN_SAFE_END(self); |
| 1539 | |
| 1540 | /* must be last */ |
| 1541 | PyDict_Type.tp_dealloc((PyObject *)self); |
| 1542 | }; |
| 1543 | |
| 1544 | /* tp_repr */ |
| 1545 | |
| 1546 | static PyObject * |
| 1547 | odict_repr(PyODictObject *self) |
| 1548 | { |
| 1549 | int i; |
| 1550 | const char *formatstr; |
| 1551 | _Py_IDENTIFIER(__class__); |
| 1552 | _Py_IDENTIFIER(__name__); |
| 1553 | Py_ssize_t count = -1; |
| 1554 | PyObject *pieces = NULL, *result = NULL, *cls = NULL; |
| 1555 | PyObject *classname = NULL, *format = NULL, *args = NULL; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1556 | |
| 1557 | i = Py_ReprEnter((PyObject *)self); |
| 1558 | if (i != 0) { |
| 1559 | return i > 0 ? PyUnicode_FromString("...") : NULL; |
| 1560 | } |
| 1561 | |
| 1562 | if (PyODict_SIZE(self) == 0) { |
| 1563 | /* "OrderedDict()" */ |
| 1564 | goto Finish; |
| 1565 | } |
| 1566 | |
| 1567 | if (PyODict_CheckExact(self)) { |
Eric Snow | a762af7 | 2015-06-01 22:59:08 -0600 | [diff] [blame] | 1568 | _ODictNode *node; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1569 | pieces = PyList_New(PyODict_SIZE(self)); |
| 1570 | if (pieces == NULL) |
| 1571 | goto Done; |
| 1572 | |
| 1573 | _odict_FOREACH(self, node) { |
Eric Snow | a762af7 | 2015-06-01 22:59:08 -0600 | [diff] [blame] | 1574 | PyObject *pair; |
| 1575 | PyObject *key = _odictnode_KEY(node); |
| 1576 | PyObject *value = _odictnode_VALUE(node, self); |
| 1577 | if (value == NULL) { |
| 1578 | if (!PyErr_Occurred()) |
| 1579 | PyErr_SetObject(PyExc_KeyError, key); |
Eric Snow | db4061c | 2015-06-03 11:09:48 -0600 | [diff] [blame] | 1580 | goto Done; |
Eric Snow | a762af7 | 2015-06-01 22:59:08 -0600 | [diff] [blame] | 1581 | } |
| 1582 | pair = PyTuple_Pack(2, key, value); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1583 | if (pair == NULL) |
| 1584 | goto Done; |
| 1585 | |
| 1586 | PyList_SET_ITEM(pieces, ++count, pair); /* steals reference */ |
| 1587 | } |
| 1588 | } |
| 1589 | else { |
| 1590 | PyObject *items = PyObject_CallMethod((PyObject *)self, "items", NULL); |
| 1591 | if (items == NULL) |
| 1592 | goto Done; |
| 1593 | pieces = PySequence_List(items); |
| 1594 | Py_DECREF(items); |
| 1595 | if(pieces == NULL) |
| 1596 | goto Done; |
| 1597 | } |
| 1598 | |
| 1599 | Finish: |
| 1600 | cls = _PyObject_GetAttrId((PyObject *)self, &PyId___class__); |
| 1601 | if (cls == NULL) |
| 1602 | goto Done; |
| 1603 | classname = _PyObject_GetAttrId(cls, &PyId___name__); |
| 1604 | if (classname == NULL) |
| 1605 | goto Done; |
| 1606 | |
| 1607 | if (pieces == NULL) { |
| 1608 | formatstr = "%s()"; |
| 1609 | args = PyTuple_Pack(1, classname); |
| 1610 | } |
| 1611 | else { |
| 1612 | formatstr = "%s(%r)"; |
| 1613 | args = PyTuple_Pack(2, classname, pieces); |
| 1614 | } |
| 1615 | if (args == NULL) |
| 1616 | goto Done; |
| 1617 | |
| 1618 | format = PyUnicode_InternFromString(formatstr); |
| 1619 | if (format == NULL) |
| 1620 | goto Done; |
| 1621 | |
| 1622 | result = PyUnicode_Format(format, args); |
| 1623 | Done: |
| 1624 | Py_XDECREF(pieces); |
| 1625 | Py_XDECREF(cls); |
| 1626 | Py_XDECREF(classname); |
| 1627 | Py_XDECREF(format); |
| 1628 | Py_XDECREF(args); |
| 1629 | Py_ReprLeave((PyObject *)self); |
| 1630 | return result; |
| 1631 | }; |
| 1632 | |
| 1633 | /* tp_doc */ |
| 1634 | |
| 1635 | PyDoc_STRVAR(odict_doc, |
| 1636 | "Dictionary that remembers insertion order"); |
| 1637 | |
| 1638 | /* tp_traverse */ |
| 1639 | |
| 1640 | static int |
| 1641 | odict_traverse(PyODictObject *od, visitproc visit, void *arg) |
| 1642 | { |
| 1643 | Py_VISIT(od->od_inst_dict); |
| 1644 | Py_VISIT(od->od_weakreflist); |
| 1645 | return PyDict_Type.tp_traverse((PyObject *)od, visit, arg); |
| 1646 | } |
| 1647 | |
| 1648 | /* tp_clear */ |
| 1649 | |
| 1650 | static int |
| 1651 | odict_tp_clear(PyODictObject *od) |
| 1652 | { |
| 1653 | PyObject *res; |
| 1654 | Py_CLEAR(od->od_inst_dict); |
| 1655 | Py_CLEAR(od->od_weakreflist); |
| 1656 | res = odict_clear(od); |
| 1657 | if (res == NULL) |
| 1658 | return -1; |
| 1659 | Py_DECREF(res); |
| 1660 | return 0; |
| 1661 | } |
| 1662 | |
| 1663 | /* tp_richcompare */ |
| 1664 | |
| 1665 | static PyObject * |
| 1666 | odict_richcompare(PyObject *v, PyObject *w, int op) |
| 1667 | { |
| 1668 | if (!PyODict_Check(v) || !PyDict_Check(w)) { |
| 1669 | Py_RETURN_NOTIMPLEMENTED; |
| 1670 | } |
| 1671 | |
| 1672 | if (op == Py_EQ || op == Py_NE) { |
| 1673 | PyObject *res, *cmp; |
| 1674 | int eq; |
| 1675 | |
| 1676 | cmp = PyDict_Type.tp_richcompare(v, w, op); |
| 1677 | if (cmp == NULL) |
| 1678 | return NULL; |
| 1679 | if (!PyODict_Check(w)) |
| 1680 | return cmp; |
| 1681 | if (op == Py_EQ && cmp == Py_False) |
| 1682 | return cmp; |
| 1683 | if (op == Py_NE && cmp == Py_True) |
| 1684 | return cmp; |
| 1685 | Py_DECREF(cmp); |
| 1686 | |
| 1687 | /* Try comparing odict keys. */ |
| 1688 | eq = _odict_keys_equal((PyODictObject *)v, (PyODictObject *)w); |
| 1689 | if (eq < 0) |
| 1690 | return NULL; |
| 1691 | |
| 1692 | res = (eq == (op == Py_EQ)) ? Py_True : Py_False; |
| 1693 | Py_INCREF(res); |
| 1694 | return res; |
| 1695 | } else { |
| 1696 | Py_RETURN_NOTIMPLEMENTED; |
| 1697 | } |
| 1698 | }; |
| 1699 | |
| 1700 | /* tp_iter */ |
| 1701 | |
| 1702 | static PyObject * |
| 1703 | odict_iter(PyODictObject *od) |
| 1704 | { |
| 1705 | return odictiter_new(od, _odict_ITER_KEYS); |
| 1706 | }; |
| 1707 | |
| 1708 | /* tp_init */ |
| 1709 | |
| 1710 | static int |
| 1711 | odict_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1712 | { |
| 1713 | PyObject *res; |
| 1714 | Py_ssize_t len = PyObject_Length(args); |
| 1715 | |
| 1716 | if (len == -1) |
| 1717 | return -1; |
| 1718 | if (len > 1) { |
| 1719 | char *msg = "expected at most 1 arguments, got %d"; |
| 1720 | PyErr_Format(PyExc_TypeError, msg, len); |
| 1721 | return -1; |
| 1722 | } |
| 1723 | |
| 1724 | /* __init__() triggering update() is just the way things are! */ |
| 1725 | res = odict_update(self, args, kwds); |
| 1726 | if (res == NULL) { |
| 1727 | return -1; |
| 1728 | } else { |
| 1729 | Py_DECREF(res); |
| 1730 | return 0; |
| 1731 | } |
| 1732 | }; |
| 1733 | |
| 1734 | /* tp_new */ |
| 1735 | |
| 1736 | static PyObject * |
| 1737 | odict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1738 | { |
| 1739 | PyObject *od = PyDict_Type.tp_new(type, args, kwds); |
| 1740 | if (od != NULL) { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1741 | if (_odict_initialize((PyODictObject *)od) < 0) |
| 1742 | return NULL; |
Eric Snow | e3dfa9e | 2015-05-30 12:51:15 -0600 | [diff] [blame] | 1743 | ((PyODictObject *)od)->od_inst_dict = PyDict_New(); |
| 1744 | ((PyODictObject *)od)->od_weakreflist = NULL; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1745 | } |
| 1746 | return od; |
| 1747 | } |
| 1748 | |
| 1749 | /* PyODict_Type */ |
| 1750 | |
| 1751 | PyTypeObject PyODict_Type = { |
| 1752 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1753 | "collections.OrderedDict", /* tp_name */ |
| 1754 | sizeof(PyODictObject), /* tp_basicsize */ |
| 1755 | 0, /* tp_itemsize */ |
| 1756 | (destructor)odict_dealloc, /* tp_dealloc */ |
| 1757 | 0, /* tp_print */ |
| 1758 | 0, /* tp_getattr */ |
| 1759 | 0, /* tp_setattr */ |
| 1760 | 0, /* tp_reserved */ |
| 1761 | (reprfunc)odict_repr, /* tp_repr */ |
| 1762 | 0, /* tp_as_number */ |
| 1763 | 0, /* tp_as_sequence */ |
| 1764 | &odict_as_mapping, /* tp_as_mapping */ |
| 1765 | 0, /* tp_hash */ |
| 1766 | 0, /* tp_call */ |
| 1767 | 0, /* tp_str */ |
| 1768 | 0, /* tp_getattro */ |
| 1769 | 0, /* tp_setattro */ |
| 1770 | 0, /* tp_as_buffer */ |
| 1771 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 1772 | odict_doc, /* tp_doc */ |
| 1773 | (traverseproc)odict_traverse, /* tp_traverse */ |
| 1774 | (inquiry)odict_tp_clear, /* tp_clear */ |
| 1775 | (richcmpfunc)odict_richcompare, /* tp_richcompare */ |
| 1776 | offsetof(PyODictObject, od_weakreflist), /* tp_weaklistoffset */ |
| 1777 | (getiterfunc)odict_iter, /* tp_iter */ |
| 1778 | 0, /* tp_iternext */ |
| 1779 | odict_methods, /* tp_methods */ |
| 1780 | odict_members, /* tp_members */ |
| 1781 | 0, /* tp_getset */ |
| 1782 | &PyDict_Type, /* tp_base */ |
| 1783 | 0, /* tp_dict */ |
| 1784 | 0, /* tp_descr_get */ |
| 1785 | 0, /* tp_descr_set */ |
| 1786 | offsetof(PyODictObject, od_inst_dict), /* tp_dictoffset */ |
| 1787 | (initproc)odict_init, /* tp_init */ |
| 1788 | PyType_GenericAlloc, /* tp_alloc */ |
| 1789 | (newfunc)odict_new, /* tp_new */ |
| 1790 | 0, /* tp_free */ |
| 1791 | }; |
| 1792 | |
| 1793 | |
| 1794 | /* ---------------------------------------------- |
| 1795 | * the public OrderedDict API |
| 1796 | */ |
| 1797 | |
| 1798 | PyObject * |
| 1799 | PyODict_New(void) { |
| 1800 | return odict_new(&PyODict_Type, NULL, NULL); |
| 1801 | }; |
| 1802 | |
| 1803 | int |
| 1804 | PyODict_SetItem(PyObject *od, PyObject *key, PyObject *value) { |
| 1805 | int res = PyDict_SetItem(od, key, value); |
| 1806 | if (res == 0) { |
| 1807 | res = _odict_add_new_node((PyODictObject *)od, key); |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 1808 | /* XXX Revert setting the value on the dict? */ |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1809 | } |
| 1810 | return res; |
| 1811 | }; |
| 1812 | |
| 1813 | int |
| 1814 | PyODict_DelItem(PyObject *od, PyObject *key) { |
| 1815 | int res = _odict_clear_node((PyODictObject *)od, NULL, key); |
| 1816 | if (res < 0) |
| 1817 | return -1; |
| 1818 | return PyDict_DelItem(od, key); |
| 1819 | }; |
| 1820 | |
| 1821 | |
| 1822 | /* ------------------------------------------- |
| 1823 | * The OrderedDict views (keys/values/items) |
| 1824 | */ |
| 1825 | |
| 1826 | typedef struct { |
| 1827 | PyObject_HEAD |
| 1828 | int kind; |
| 1829 | PyODictObject *di_odict; |
Eric Snow | b952ab4 | 2015-06-01 23:35:13 -0600 | [diff] [blame] | 1830 | Py_ssize_t di_size; |
Eric Snow | 4fabf02 | 2015-06-04 00:09:56 -0600 | [diff] [blame] | 1831 | size_t di_state; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1832 | PyObject *di_current; |
| 1833 | PyObject *di_result; /* reusable result tuple for iteritems */ |
| 1834 | } odictiterobject; |
| 1835 | |
| 1836 | static void |
| 1837 | odictiter_dealloc(odictiterobject *di) |
| 1838 | { |
| 1839 | _PyObject_GC_UNTRACK(di); |
Eric Snow | a762af7 | 2015-06-01 22:59:08 -0600 | [diff] [blame] | 1840 | Py_XDECREF(di->di_odict); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1841 | Py_XDECREF(di->di_current); |
| 1842 | if (di->kind & (_odict_ITER_KEYS | _odict_ITER_VALUES)) { |
| 1843 | Py_DECREF(di->di_result); |
| 1844 | } |
| 1845 | PyObject_GC_Del(di); |
| 1846 | } |
| 1847 | |
| 1848 | static int |
| 1849 | odictiter_traverse(odictiterobject *di, visitproc visit, void *arg) |
| 1850 | { |
| 1851 | Py_VISIT(di->di_odict); |
| 1852 | Py_VISIT(di->di_current); /* A key could be any type, not just str. */ |
| 1853 | Py_VISIT(di->di_result); |
| 1854 | return 0; |
| 1855 | } |
| 1856 | |
Eric Snow | a762af7 | 2015-06-01 22:59:08 -0600 | [diff] [blame] | 1857 | /* In order to protect against modifications during iteration, we track |
| 1858 | * the current key instead of the current node. */ |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1859 | static PyObject * |
| 1860 | odictiter_nextkey(odictiterobject *di) |
| 1861 | { |
Eric Snow | a762af7 | 2015-06-01 22:59:08 -0600 | [diff] [blame] | 1862 | PyObject *key = NULL; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1863 | _ODictNode *node; |
| 1864 | int reversed = di->kind & _odict_ITER_REVERSED; |
| 1865 | |
Eric Snow | a762af7 | 2015-06-01 22:59:08 -0600 | [diff] [blame] | 1866 | if (di->di_odict == NULL) |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1867 | return NULL; |
Eric Snow | a762af7 | 2015-06-01 22:59:08 -0600 | [diff] [blame] | 1868 | if (di->di_current == NULL) |
| 1869 | goto done; /* We're already done. */ |
| 1870 | |
Eric Snow | b952ab4 | 2015-06-01 23:35:13 -0600 | [diff] [blame] | 1871 | /* Check for unsupported changes. */ |
Eric Snow | 4fabf02 | 2015-06-04 00:09:56 -0600 | [diff] [blame] | 1872 | if (di->di_odict->od_state != di->di_state) { |
| 1873 | PyErr_SetString(PyExc_RuntimeError, |
| 1874 | "OrderedDict mutated during iteration"); |
| 1875 | goto done; |
| 1876 | } |
Eric Snow | b952ab4 | 2015-06-01 23:35:13 -0600 | [diff] [blame] | 1877 | if (di->di_size != PyODict_SIZE(di->di_odict)) { |
| 1878 | PyErr_SetString(PyExc_RuntimeError, |
| 1879 | "OrderedDict changed size during iteration"); |
| 1880 | di->di_size = -1; /* Make this state sticky */ |
| 1881 | return NULL; |
| 1882 | } |
| 1883 | |
Eric Snow | a762af7 | 2015-06-01 22:59:08 -0600 | [diff] [blame] | 1884 | /* Get the key. */ |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1885 | node = _odict_find_node(di->di_odict, di->di_current); |
| 1886 | if (node == NULL) { |
| 1887 | /* Must have been deleted. */ |
Eric Snow | e3dfa9e | 2015-05-30 12:51:15 -0600 | [diff] [blame] | 1888 | Py_CLEAR(di->di_current); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1889 | return NULL; |
| 1890 | } |
| 1891 | key = di->di_current; |
| 1892 | |
| 1893 | /* Advance to the next key. */ |
| 1894 | node = reversed ? _odictnode_PREV(node) : _odictnode_NEXT(node); |
| 1895 | if (node == NULL) { |
| 1896 | /* Reached the end. */ |
| 1897 | di->di_current = NULL; |
| 1898 | } |
| 1899 | else { |
| 1900 | di->di_current = _odictnode_KEY(node); |
| 1901 | Py_INCREF(di->di_current); |
| 1902 | } |
| 1903 | |
| 1904 | return key; |
Eric Snow | a762af7 | 2015-06-01 22:59:08 -0600 | [diff] [blame] | 1905 | |
| 1906 | done: |
| 1907 | Py_CLEAR(di->di_odict); |
| 1908 | return key; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | static PyObject * |
| 1912 | odictiter_iternext(odictiterobject *di) |
| 1913 | { |
| 1914 | PyObject *value; |
| 1915 | PyObject *key = odictiter_nextkey(di); /* new reference */ |
| 1916 | |
| 1917 | if (key == NULL) |
| 1918 | return NULL; |
| 1919 | |
| 1920 | /* Handle the keys case. */ |
| 1921 | if (! (di->kind & _odict_ITER_VALUES)) { |
| 1922 | return key; |
| 1923 | } |
| 1924 | |
| 1925 | /* Handle the items case. */ |
| 1926 | if (di->kind & _odict_ITER_KEYS) { |
| 1927 | PyObject *result = di->di_result; |
| 1928 | |
| 1929 | value = PyODict_GetItem((PyObject *)di->di_odict, key); /* borrowed */ |
Eric Snow | e3dfa9e | 2015-05-30 12:51:15 -0600 | [diff] [blame] | 1930 | if (value == NULL) { |
Eric Snow | a762af7 | 2015-06-01 22:59:08 -0600 | [diff] [blame] | 1931 | if (!PyErr_Occurred()) |
| 1932 | PyErr_SetObject(PyExc_KeyError, key); |
Eric Snow | e3dfa9e | 2015-05-30 12:51:15 -0600 | [diff] [blame] | 1933 | Py_DECREF(key); |
Eric Snow | a762af7 | 2015-06-01 22:59:08 -0600 | [diff] [blame] | 1934 | goto done; |
Eric Snow | e3dfa9e | 2015-05-30 12:51:15 -0600 | [diff] [blame] | 1935 | } |
| 1936 | Py_INCREF(value); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1937 | |
| 1938 | if (result->ob_refcnt == 1) { |
| 1939 | /* not in use so we can reuse it |
| 1940 | * (the common case during iteration) */ |
| 1941 | Py_INCREF(result); |
| 1942 | Py_DECREF(PyTuple_GET_ITEM(result, 0)); /* borrowed */ |
| 1943 | Py_DECREF(PyTuple_GET_ITEM(result, 1)); /* borrowed */ |
| 1944 | } |
| 1945 | else { |
| 1946 | result = PyTuple_New(2); |
Eric Snow | e3dfa9e | 2015-05-30 12:51:15 -0600 | [diff] [blame] | 1947 | if (result == NULL) { |
| 1948 | Py_DECREF(key); |
| 1949 | Py_DECREF(value); |
Eric Snow | a762af7 | 2015-06-01 22:59:08 -0600 | [diff] [blame] | 1950 | goto done; |
Eric Snow | e3dfa9e | 2015-05-30 12:51:15 -0600 | [diff] [blame] | 1951 | } |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1952 | } |
| 1953 | |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1954 | PyTuple_SET_ITEM(result, 0, key); /* steals reference */ |
| 1955 | PyTuple_SET_ITEM(result, 1, value); /* steals reference */ |
| 1956 | |
| 1957 | return result; |
| 1958 | } |
| 1959 | /* Handle the values case. */ |
| 1960 | else { |
| 1961 | value = PyODict_GetItem((PyObject *)di->di_odict, key); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1962 | Py_DECREF(key); |
Eric Snow | a762af7 | 2015-06-01 22:59:08 -0600 | [diff] [blame] | 1963 | if (value == NULL) { |
| 1964 | if (!PyErr_Occurred()) |
| 1965 | PyErr_SetObject(PyExc_KeyError, key); |
| 1966 | goto done; |
| 1967 | } |
| 1968 | Py_INCREF(value); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1969 | return value; |
| 1970 | } |
Eric Snow | a762af7 | 2015-06-01 22:59:08 -0600 | [diff] [blame] | 1971 | |
| 1972 | done: |
| 1973 | Py_CLEAR(di->di_current); |
| 1974 | Py_CLEAR(di->di_odict); |
| 1975 | return NULL; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | /* No need for tp_clear because odictiterobject is not mutable. */ |
| 1979 | |
| 1980 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling"); |
| 1981 | |
| 1982 | static PyObject * |
| 1983 | odictiter_reduce(odictiterobject *di) |
| 1984 | { |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 1985 | PyObject *list, *iter; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1986 | |
| 1987 | list = PyList_New(0); |
| 1988 | if (!list) |
| 1989 | return NULL; |
| 1990 | |
| 1991 | /* iterate the temporary into a list */ |
| 1992 | for(;;) { |
| 1993 | PyObject *element = odictiter_iternext(di); |
| 1994 | if (element) { |
| 1995 | if (PyList_Append(list, element)) { |
| 1996 | Py_DECREF(element); |
| 1997 | Py_DECREF(list); |
| 1998 | return NULL; |
| 1999 | } |
| 2000 | Py_DECREF(element); |
| 2001 | } |
| 2002 | else { |
| 2003 | /* done iterating? */ |
| 2004 | break; |
| 2005 | } |
| 2006 | } |
| 2007 | if (PyErr_Occurred()) { |
| 2008 | Py_DECREF(list); |
| 2009 | return NULL; |
| 2010 | } |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 2011 | iter = _PyObject_GetBuiltin("iter"); |
| 2012 | if (iter == NULL) { |
| 2013 | Py_DECREF(list); |
| 2014 | return NULL; |
| 2015 | } |
| 2016 | return Py_BuildValue("N(N)", iter, list); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | static PyMethodDef odictiter_methods[] = { |
| 2020 | {"__reduce__", (PyCFunction)odictiter_reduce, METH_NOARGS, reduce_doc}, |
| 2021 | {NULL, NULL} /* sentinel */ |
| 2022 | }; |
| 2023 | |
| 2024 | PyTypeObject PyODictIter_Type = { |
| 2025 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2026 | "odict_iterator", /* tp_name */ |
| 2027 | sizeof(odictiterobject), /* tp_basicsize */ |
| 2028 | 0, /* tp_itemsize */ |
| 2029 | /* methods */ |
| 2030 | (destructor)odictiter_dealloc, /* tp_dealloc */ |
| 2031 | 0, /* tp_print */ |
| 2032 | 0, /* tp_getattr */ |
| 2033 | 0, /* tp_setattr */ |
| 2034 | 0, /* tp_reserved */ |
| 2035 | 0, /* tp_repr */ |
| 2036 | 0, /* tp_as_number */ |
| 2037 | 0, /* tp_as_sequence */ |
| 2038 | 0, /* tp_as_mapping */ |
| 2039 | 0, /* tp_hash */ |
| 2040 | 0, /* tp_call */ |
| 2041 | 0, /* tp_str */ |
| 2042 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2043 | 0, /* tp_setattro */ |
| 2044 | 0, /* tp_as_buffer */ |
| 2045 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 2046 | 0, /* tp_doc */ |
| 2047 | (traverseproc)odictiter_traverse, /* tp_traverse */ |
| 2048 | 0, /* tp_clear */ |
| 2049 | 0, /* tp_richcompare */ |
| 2050 | 0, /* tp_weaklistoffset */ |
| 2051 | PyObject_SelfIter, /* tp_iter */ |
| 2052 | (iternextfunc)odictiter_iternext, /* tp_iternext */ |
| 2053 | odictiter_methods, /* tp_methods */ |
| 2054 | 0, |
| 2055 | }; |
| 2056 | |
| 2057 | static PyObject * |
| 2058 | odictiter_new(PyODictObject *od, int kind) |
| 2059 | { |
| 2060 | odictiterobject *di; |
| 2061 | _ODictNode *node; |
| 2062 | int reversed = kind & _odict_ITER_REVERSED; |
| 2063 | |
| 2064 | di = PyObject_GC_New(odictiterobject, &PyODictIter_Type); |
| 2065 | if (di == NULL) |
| 2066 | return NULL; |
| 2067 | |
| 2068 | if (kind & (_odict_ITER_KEYS | _odict_ITER_VALUES)){ |
| 2069 | di->di_result = PyTuple_Pack(2, Py_None, Py_None); |
| 2070 | if (di->di_result == NULL) { |
| 2071 | Py_DECREF(di); |
| 2072 | return NULL; |
| 2073 | } |
| 2074 | } |
| 2075 | else |
| 2076 | di->di_result = NULL; |
| 2077 | |
| 2078 | di->kind = kind; |
| 2079 | node = reversed ? _odict_LAST(od) : _odict_FIRST(od); |
| 2080 | di->di_current = node ? _odictnode_KEY(node) : NULL; |
| 2081 | Py_XINCREF(di->di_current); |
Eric Snow | b952ab4 | 2015-06-01 23:35:13 -0600 | [diff] [blame] | 2082 | di->di_size = PyODict_SIZE(od); |
Eric Snow | 4fabf02 | 2015-06-04 00:09:56 -0600 | [diff] [blame] | 2083 | di->di_state = od->od_state; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 2084 | di->di_odict = od; |
| 2085 | Py_INCREF(od); |
| 2086 | |
| 2087 | _PyObject_GC_TRACK(di); |
| 2088 | return (PyObject *)di; |
| 2089 | } |
| 2090 | |
| 2091 | /* keys() */ |
| 2092 | |
| 2093 | static PyObject * |
| 2094 | odictkeys_iter(_PyDictViewObject *dv) |
| 2095 | { |
| 2096 | if (dv->dv_dict == NULL) { |
| 2097 | Py_RETURN_NONE; |
| 2098 | } |
| 2099 | return odictiter_new((PyODictObject *)dv->dv_dict, |
| 2100 | _odict_ITER_KEYS); |
| 2101 | } |
| 2102 | |
| 2103 | static PyObject * |
| 2104 | odictkeys_reversed(_PyDictViewObject *dv) |
| 2105 | { |
| 2106 | if (dv->dv_dict == NULL) { |
| 2107 | Py_RETURN_NONE; |
| 2108 | } |
| 2109 | return odictiter_new((PyODictObject *)dv->dv_dict, |
| 2110 | _odict_ITER_KEYS|_odict_ITER_REVERSED); |
| 2111 | } |
| 2112 | |
| 2113 | static PyMethodDef odictkeys_methods[] = { |
| 2114 | {"__reversed__", (PyCFunction)odictkeys_reversed, METH_NOARGS, NULL}, |
| 2115 | {NULL, NULL} /* sentinel */ |
| 2116 | }; |
| 2117 | |
| 2118 | PyTypeObject PyODictKeys_Type = { |
| 2119 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2120 | "odict_keys", /* tp_name */ |
| 2121 | 0, /* tp_basicsize */ |
| 2122 | 0, /* tp_itemsize */ |
| 2123 | 0, /* tp_dealloc */ |
| 2124 | 0, /* tp_print */ |
| 2125 | 0, /* tp_getattr */ |
| 2126 | 0, /* tp_setattr */ |
| 2127 | 0, /* tp_reserved */ |
| 2128 | 0, /* tp_repr */ |
| 2129 | 0, /* tp_as_number */ |
| 2130 | 0, /* tp_as_sequence */ |
| 2131 | 0, /* tp_as_mapping */ |
| 2132 | 0, /* tp_hash */ |
| 2133 | 0, /* tp_call */ |
| 2134 | 0, /* tp_str */ |
| 2135 | 0, /* tp_getattro */ |
| 2136 | 0, /* tp_setattro */ |
| 2137 | 0, /* tp_as_buffer */ |
| 2138 | 0, /* tp_flags */ |
| 2139 | 0, /* tp_doc */ |
| 2140 | 0, /* tp_traverse */ |
| 2141 | 0, /* tp_clear */ |
| 2142 | 0, /* tp_richcompare */ |
| 2143 | 0, /* tp_weaklistoffset */ |
| 2144 | (getiterfunc)odictkeys_iter, /* tp_iter */ |
| 2145 | 0, /* tp_iternext */ |
| 2146 | odictkeys_methods, /* tp_methods */ |
| 2147 | 0, /* tp_members */ |
| 2148 | 0, /* tp_getset */ |
| 2149 | &PyDictKeys_Type, /* tp_base */ |
| 2150 | }; |
| 2151 | |
| 2152 | static PyObject * |
| 2153 | odictkeys_new(PyObject *od) |
| 2154 | { |
| 2155 | return _PyDictView_New(od, &PyODictKeys_Type); |
| 2156 | } |
| 2157 | |
| 2158 | /* items() */ |
| 2159 | |
| 2160 | static PyObject * |
| 2161 | odictitems_iter(_PyDictViewObject *dv) |
| 2162 | { |
| 2163 | if (dv->dv_dict == NULL) { |
| 2164 | Py_RETURN_NONE; |
| 2165 | } |
| 2166 | return odictiter_new((PyODictObject *)dv->dv_dict, |
| 2167 | _odict_ITER_KEYS|_odict_ITER_VALUES); |
| 2168 | } |
| 2169 | |
| 2170 | static PyObject * |
| 2171 | odictitems_reversed(_PyDictViewObject *dv) |
| 2172 | { |
| 2173 | if (dv->dv_dict == NULL) { |
| 2174 | Py_RETURN_NONE; |
| 2175 | } |
| 2176 | return odictiter_new((PyODictObject *)dv->dv_dict, |
| 2177 | _odict_ITER_KEYS|_odict_ITER_VALUES|_odict_ITER_REVERSED); |
| 2178 | } |
| 2179 | |
| 2180 | static PyMethodDef odictitems_methods[] = { |
| 2181 | {"__reversed__", (PyCFunction)odictitems_reversed, METH_NOARGS, NULL}, |
| 2182 | {NULL, NULL} /* sentinel */ |
| 2183 | }; |
| 2184 | |
| 2185 | PyTypeObject PyODictItems_Type = { |
| 2186 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2187 | "odict_items", /* tp_name */ |
| 2188 | 0, /* tp_basicsize */ |
| 2189 | 0, /* tp_itemsize */ |
| 2190 | 0, /* tp_dealloc */ |
| 2191 | 0, /* tp_print */ |
| 2192 | 0, /* tp_getattr */ |
| 2193 | 0, /* tp_setattr */ |
| 2194 | 0, /* tp_reserved */ |
| 2195 | 0, /* tp_repr */ |
| 2196 | 0, /* tp_as_number */ |
| 2197 | 0, /* tp_as_sequence */ |
| 2198 | 0, /* tp_as_mapping */ |
| 2199 | 0, /* tp_hash */ |
| 2200 | 0, /* tp_call */ |
| 2201 | 0, /* tp_str */ |
| 2202 | 0, /* tp_getattro */ |
| 2203 | 0, /* tp_setattro */ |
| 2204 | 0, /* tp_as_buffer */ |
| 2205 | 0, /* tp_flags */ |
| 2206 | 0, /* tp_doc */ |
| 2207 | 0, /* tp_traverse */ |
| 2208 | 0, /* tp_clear */ |
| 2209 | 0, /* tp_richcompare */ |
| 2210 | 0, /* tp_weaklistoffset */ |
| 2211 | (getiterfunc)odictitems_iter, /* tp_iter */ |
| 2212 | 0, /* tp_iternext */ |
| 2213 | odictitems_methods, /* tp_methods */ |
| 2214 | 0, /* tp_members */ |
| 2215 | 0, /* tp_getset */ |
| 2216 | &PyDictItems_Type, /* tp_base */ |
| 2217 | }; |
| 2218 | |
| 2219 | static PyObject * |
| 2220 | odictitems_new(PyObject *od) |
| 2221 | { |
| 2222 | return _PyDictView_New(od, &PyODictItems_Type); |
| 2223 | } |
| 2224 | |
| 2225 | /* values() */ |
| 2226 | |
| 2227 | static PyObject * |
| 2228 | odictvalues_iter(_PyDictViewObject *dv) |
| 2229 | { |
| 2230 | if (dv->dv_dict == NULL) { |
| 2231 | Py_RETURN_NONE; |
| 2232 | } |
| 2233 | return odictiter_new((PyODictObject *)dv->dv_dict, |
| 2234 | _odict_ITER_VALUES); |
| 2235 | } |
| 2236 | |
| 2237 | static PyObject * |
| 2238 | odictvalues_reversed(_PyDictViewObject *dv) |
| 2239 | { |
| 2240 | if (dv->dv_dict == NULL) { |
| 2241 | Py_RETURN_NONE; |
| 2242 | } |
| 2243 | return odictiter_new((PyODictObject *)dv->dv_dict, |
| 2244 | _odict_ITER_VALUES|_odict_ITER_REVERSED); |
| 2245 | } |
| 2246 | |
| 2247 | static PyMethodDef odictvalues_methods[] = { |
| 2248 | {"__reversed__", (PyCFunction)odictvalues_reversed, METH_NOARGS, NULL}, |
| 2249 | {NULL, NULL} /* sentinel */ |
| 2250 | }; |
| 2251 | |
| 2252 | PyTypeObject PyODictValues_Type = { |
| 2253 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2254 | "odict_values", /* tp_name */ |
| 2255 | 0, /* tp_basicsize */ |
| 2256 | 0, /* tp_itemsize */ |
| 2257 | 0, /* tp_dealloc */ |
| 2258 | 0, /* tp_print */ |
| 2259 | 0, /* tp_getattr */ |
| 2260 | 0, /* tp_setattr */ |
| 2261 | 0, /* tp_reserved */ |
| 2262 | 0, /* tp_repr */ |
| 2263 | 0, /* tp_as_number */ |
| 2264 | 0, /* tp_as_sequence */ |
| 2265 | 0, /* tp_as_mapping */ |
| 2266 | 0, /* tp_hash */ |
| 2267 | 0, /* tp_call */ |
| 2268 | 0, /* tp_str */ |
| 2269 | 0, /* tp_getattro */ |
| 2270 | 0, /* tp_setattro */ |
| 2271 | 0, /* tp_as_buffer */ |
| 2272 | 0, /* tp_flags */ |
| 2273 | 0, /* tp_doc */ |
| 2274 | 0, /* tp_traverse */ |
| 2275 | 0, /* tp_clear */ |
| 2276 | 0, /* tp_richcompare */ |
| 2277 | 0, /* tp_weaklistoffset */ |
| 2278 | (getiterfunc)odictvalues_iter, /* tp_iter */ |
| 2279 | 0, /* tp_iternext */ |
| 2280 | odictvalues_methods, /* tp_methods */ |
| 2281 | 0, /* tp_members */ |
| 2282 | 0, /* tp_getset */ |
| 2283 | &PyDictValues_Type, /* tp_base */ |
| 2284 | }; |
| 2285 | |
| 2286 | static PyObject * |
| 2287 | odictvalues_new(PyObject *od) |
| 2288 | { |
| 2289 | return _PyDictView_New(od, &PyODictValues_Type); |
| 2290 | } |
| 2291 | |
| 2292 | |
| 2293 | /* ---------------------------------------------- |
| 2294 | MutableMappping implementations |
| 2295 | |
| 2296 | Mapping: |
| 2297 | |
| 2298 | ============ =========== |
| 2299 | method uses |
| 2300 | ============ =========== |
| 2301 | __contains__ __getitem__ |
| 2302 | __eq__ items |
| 2303 | __getitem__ + |
| 2304 | __iter__ + |
| 2305 | __len__ + |
| 2306 | __ne__ __eq__ |
| 2307 | get __getitem__ |
| 2308 | items ItemsView |
| 2309 | keys KeysView |
| 2310 | values ValuesView |
| 2311 | ============ =========== |
| 2312 | |
| 2313 | ItemsView uses __len__, __iter__, and __getitem__. |
| 2314 | KeysView uses __len__, __iter__, and __contains__. |
| 2315 | ValuesView uses __len__, __iter__, and __getitem__. |
| 2316 | |
| 2317 | MutableMapping: |
| 2318 | |
| 2319 | ============ =========== |
| 2320 | method uses |
| 2321 | ============ =========== |
| 2322 | __delitem__ + |
| 2323 | __setitem__ + |
| 2324 | clear popitem |
| 2325 | pop __getitem__ |
| 2326 | __delitem__ |
| 2327 | popitem __iter__ |
| 2328 | _getitem__ |
| 2329 | __delitem__ |
| 2330 | setdefault __getitem__ |
| 2331 | __setitem__ |
| 2332 | update __setitem__ |
| 2333 | ============ =========== |
| 2334 | */ |
| 2335 | |
| 2336 | static int |
| 2337 | mutablemapping_add_pairs(PyObject *self, PyObject *pairs) |
| 2338 | { |
| 2339 | PyObject *pair, *iterator, *unexpected; |
| 2340 | int res = 0; |
| 2341 | |
| 2342 | iterator = PyObject_GetIter(pairs); |
| 2343 | if (iterator == NULL) |
| 2344 | return -1; |
| 2345 | PyErr_Clear(); |
| 2346 | |
| 2347 | while ((pair = PyIter_Next(iterator)) != NULL) { |
| 2348 | /* could be more efficient (see UNPACK_SEQUENCE in ceval.c) */ |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 2349 | PyObject *key = NULL, *value = NULL; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 2350 | PyObject *pair_iterator = PyObject_GetIter(pair); |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 2351 | if (pair_iterator == NULL) |
| 2352 | goto Done; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 2353 | |
| 2354 | key = PyIter_Next(pair_iterator); |
| 2355 | if (key == NULL) { |
| 2356 | if (!PyErr_Occurred()) |
| 2357 | PyErr_SetString(PyExc_ValueError, |
| 2358 | "need more than 0 values to unpack"); |
| 2359 | goto Done; |
| 2360 | } |
| 2361 | |
| 2362 | value = PyIter_Next(pair_iterator); |
| 2363 | if (value == NULL) { |
| 2364 | if (!PyErr_Occurred()) |
| 2365 | PyErr_SetString(PyExc_ValueError, |
| 2366 | "need more than 1 value to unpack"); |
| 2367 | goto Done; |
| 2368 | } |
| 2369 | |
| 2370 | unexpected = PyIter_Next(pair_iterator); |
| 2371 | if (unexpected != NULL) { |
| 2372 | Py_DECREF(unexpected); |
| 2373 | PyErr_SetString(PyExc_ValueError, |
| 2374 | "too many values to unpack (expected 2)"); |
| 2375 | goto Done; |
| 2376 | } |
| 2377 | else if (PyErr_Occurred()) |
| 2378 | goto Done; |
| 2379 | |
| 2380 | res = PyObject_SetItem(self, key, value); |
| 2381 | |
| 2382 | Done: |
| 2383 | Py_DECREF(pair); |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 2384 | Py_XDECREF(pair_iterator); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 2385 | Py_XDECREF(key); |
| 2386 | Py_XDECREF(value); |
| 2387 | if (PyErr_Occurred()) |
| 2388 | break; |
| 2389 | } |
| 2390 | Py_DECREF(iterator); |
| 2391 | |
| 2392 | if (res < 0 || PyErr_Occurred() != NULL) |
| 2393 | return -1; |
| 2394 | else |
| 2395 | return 0; |
| 2396 | } |
| 2397 | |
| 2398 | static PyObject * |
| 2399 | mutablemapping_update(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2400 | { |
| 2401 | int res = 0; |
| 2402 | Py_ssize_t len = (args != NULL) ? PyObject_Size(args) : 0; |
| 2403 | |
| 2404 | /* first handle args, if any */ |
Eric Snow | c5e5960 | 2015-05-30 11:28:56 -0600 | [diff] [blame] | 2405 | if (len < 0) /* PyObject_Size raised an exception. */ |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 2406 | return NULL; |
Benjamin Peterson | 99e96f2 | 2015-06-06 23:20:32 -0500 | [diff] [blame] | 2407 | |
| 2408 | if (len > 1) { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 2409 | char *msg = "update() takes at most 1 positional argument (%d given)"; |
| 2410 | PyErr_Format(PyExc_TypeError, msg, len); |
| 2411 | return NULL; |
| 2412 | } |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 2413 | |
Benjamin Peterson | 0718de9 | 2015-06-07 00:00:42 -0500 | [diff] [blame] | 2414 | if (len == 1) { |
| 2415 | PyObject *other = PyTuple_GET_ITEM(args, 0); /* borrowed reference */ |
| 2416 | if (other == NULL) |
| 2417 | return NULL; |
| 2418 | Py_INCREF(other); |
| 2419 | if (PyObject_HasAttrString(other, "items")) { /* never fails */ |
| 2420 | PyObject *items = PyMapping_Items(other); |
| 2421 | Py_DECREF(other); |
| 2422 | if (items == NULL) |
| 2423 | return NULL; |
| 2424 | res = mutablemapping_add_pairs(self, items); |
| 2425 | Py_DECREF(items); |
| 2426 | if (res == -1) |
| 2427 | return NULL; |
| 2428 | } |
| 2429 | else if (PyObject_HasAttrString(other, "keys")) { /* never fails */ |
| 2430 | PyObject *keys, *iterator, *key; |
| 2431 | keys = PyObject_CallMethod(other, "keys", NULL); |
| 2432 | if (keys == NULL) { |
| 2433 | Py_DECREF(other); |
| 2434 | return NULL; |
| 2435 | } |
| 2436 | iterator = PyObject_GetIter(keys); |
| 2437 | Py_DECREF(keys); |
| 2438 | if (iterator == NULL) { |
| 2439 | Py_DECREF(other); |
| 2440 | return NULL; |
| 2441 | } |
| 2442 | while (res == 0 && (key = PyIter_Next(iterator))) { |
| 2443 | PyObject *value = PyObject_GetItem(other, key); |
| 2444 | if (value != NULL) { |
| 2445 | res = PyObject_SetItem(self, key, value); |
| 2446 | Py_DECREF(value); |
| 2447 | } |
| 2448 | else { |
| 2449 | res = -1; |
| 2450 | } |
| 2451 | Py_DECREF(key); |
| 2452 | } |
| 2453 | Py_DECREF(other); |
| 2454 | Py_DECREF(iterator); |
| 2455 | if (res != 0 || PyErr_Occurred()) |
| 2456 | return NULL; |
| 2457 | } |
| 2458 | else { |
| 2459 | res = mutablemapping_add_pairs(self, other); |
| 2460 | Py_DECREF(other); |
| 2461 | if (res != 0) |
| 2462 | return NULL; |
| 2463 | } |
| 2464 | } |
| 2465 | |
| 2466 | /* now handle kwargs */ |
| 2467 | len = (kwargs != NULL) ? PyObject_Size(kwargs) : 0; |
| 2468 | if (len < 0) /* PyObject_Size raised an exception. */ |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 2469 | return NULL; |
Benjamin Peterson | 0718de9 | 2015-06-07 00:00:42 -0500 | [diff] [blame] | 2470 | if (len > 0) { |
| 2471 | PyObject *items; |
| 2472 | if (!PyMapping_Check(kwargs)) { |
| 2473 | PyErr_SetString(PyExc_TypeError, "expected mapping for kwargs"); |
| 2474 | return NULL; |
| 2475 | } |
| 2476 | items = PyMapping_Items(kwargs); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 2477 | if (items == NULL) |
| 2478 | return NULL; |
| 2479 | res = mutablemapping_add_pairs(self, items); |
| 2480 | Py_DECREF(items); |
| 2481 | if (res == -1) |
| 2482 | return NULL; |
| 2483 | } |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 2484 | |
| 2485 | Py_RETURN_NONE; |
| 2486 | } |