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