blob: c0ccb16bc3d2ebd25e11b796ddf6a1ab462d2834 [file] [log] [blame]
Eric Snow96c6af92015-05-29 22:21:39 -06001/* Ordered Dictionary object implementation.
2
3This implementation is necessarily explicitly equivalent to the pure Python
4OrderedDict class in Lib/collections/__init__.py. The strategy there
5involves using a doubly-linked-list to capture the order. We keep to that
6strategy, using a lower-level linked-list.
7
8About the Linked-List
9=====================
10
11For the linked list we use a basic doubly-linked-list. Using a circularly-
12linked-list does have some benefits, but they don't apply so much here
13since OrderedDict is focused on the ends of the list (for the most part).
14Furthermore, there are some features of generic linked-lists that we simply
15don't need for OrderedDict. Thus a simple custom implementation meets our
16needs. Alternatives to our simple approach include the QCIRCLE_*
17macros from BSD's queue.h, and the linux's list.h.
18
19Getting O(1) Node Lookup
20------------------------
21
22One invariant of Python's OrderedDict is that it preserves time complexity
23of dict's methods, particularly the O(1) operations. Simply adding a
24linked-list on top of dict is not sufficient here; operations for nodes in
25the middle of the linked-list implicitly require finding the node first.
26With a simple linked-list like we're using, that is an O(n) operation.
27Consequently, methods like __delitem__() would change from O(1) to O(n),
28which is unacceptable.
29
30In order to preserve O(1) performance for node removal (finding nodes), we
31must do better than just looping through the linked-list. Here are options
32we've considered:
33
341. use a second dict to map keys to nodes (a la the pure Python version).
352. keep a simple hash table mirroring the order of dict's, mapping each key
36 to the corresponding node in the linked-list.
373. use a version of shared keys (split dict) that allows non-unicode keys.
384. have the value stored for each key be a (value, node) pair, and adjust
39 __getitem__(), get(), etc. accordingly.
40
41The approach with the least performance impact (time and space) is #2,
Benjamin Petersonee178e62016-09-08 11:08:30 -070042mirroring the key order of dict's dk_entries with an array of node pointers.
Eric Snow96c6af92015-05-29 22:21:39 -060043While lookdict() and friends (dk_lookup) don't give us the index into the
44array, we make use of pointer arithmetic to get that index. An alternative
45would be to refactor lookdict() to provide the index, explicitly exposing
46the implementation detail. We could even just use a custom lookup function
47for OrderedDict that facilitates our need. However, both approaches are
48significantly more complicated than just using pointer arithmetic.
49
50The catch with mirroring the hash table ordering is that we have to keep
51the ordering in sync through any dict resizes. However, that order only
52matters during node lookup. We can simply defer any potential resizing
53until we need to do a lookup.
54
55Linked-List Nodes
56-----------------
57
58The current implementation stores a pointer to the associated key only.
59One alternative would be to store a pointer to the PyDictKeyEntry instead.
60This would save one pointer de-reference per item, which is nice during
61calls to values() and items(). However, it adds unnecessary overhead
62otherwise, so we stick with just the key.
63
64Linked-List API
65---------------
66
67As noted, the linked-list implemented here does not have all the bells and
68whistles. However, we recognize that the implementation may need to
69change to accommodate performance improvements or extra functionality. To
Robert Krzyzanowski6f19fc62018-07-06 05:54:26 -050070that end, we use a simple API to interact with the linked-list. Here's a
Eric Snow96c6af92015-05-29 22:21:39 -060071summary of the methods/macros:
72
73Node info:
74
75* _odictnode_KEY(node)
76* _odictnode_VALUE(od, node)
77* _odictnode_PREV(node)
78* _odictnode_NEXT(node)
79
80Linked-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
87For adding nodes:
88
89* _odict_add_head(od, node)
90* _odict_add_tail(od, node)
Serhiy Storchaka19a70e72015-11-13 14:48:36 +020091* _odict_add_new_node(od, key, hash)
Eric Snow96c6af92015-05-29 22:21:39 -060092
93For removing nodes:
94
Serhiy Storchaka19a70e72015-11-13 14:48:36 +020095* _odict_clear_node(od, node, key, hash)
Eric Snow96c6af92015-05-29 22:21:39 -060096* _odict_clear_nodes(od, clear_each)
97
98Others:
99
Serhiy Storchaka19a70e72015-11-13 14:48:36 +0200100* _odict_find_node_hash(od, key, hash)
Eric Snow96c6af92015-05-29 22:21:39 -0600101* _odict_find_node(od, key)
102* _odict_keys_equal(od1, od2)
103
Eric Snow96c6af92015-05-29 22:21:39 -0600104And here's a look at how the linked-list relates to the OrderedDict API:
105
106============ === === ==== ==== ==== === ==== ===== ==== ==== === ==== === ===
107method key val prev next mem 1st last empty iter find add rmv clr keq
108============ === === ==== ==== ==== === ==== ===== ==== ==== === ==== === ===
109__del__ ~ X
110__delitem__ free ~ node
111__eq__ ~ X
112__iter__ X X
113__new__ X X
114__reduce__ X ~ X
115__repr__ X X X
116__reversed__ X X
117__setitem__ key
118__sizeof__ size X
119clear ~ ~ X
120copy X X X
121items X X X
122keys X X
123move_to_end X X X ~ h/t key
124pop free key
125popitem X X free X X node
126setdefault ~ ? ~
127values X X
128============ === === ==== ==== ==== === ==== ===== ==== ==== === ==== === ===
129
130__delitem__ is the only method that directly relies on finding an arbitrary
131node in the linked-list. Everything else is iteration or relates to the
132ends of the linked-list.
133
134Situation that Endangers Consistency
135------------------------------------
136Using a raw linked-list for OrderedDict exposes a key situation that can
137cause problems. If a node is stored in a variable, there is a chance that
138the node may have been deallocated before the variable gets used, thus
139potentially leading to a segmentation fault. A key place where this shows
140up is during iteration through the linked list (via _odict_FOREACH or
141otherwise).
142
143A number of solutions are available to resolve this situation:
144
145* defer looking up the node until as late as possible and certainly after
146 any code that could possibly result in a deletion;
147* if the node is needed both before and after a point where the node might
148 be removed, do a check before using the node at the "after" location to
149 see if the node is still valid;
150* like the last one, but simply pull the node again to ensure it's right;
151* keep the key in the variable instead of the node and then look up the
152 node using the key at the point where the node is needed (this is what
153 we do for the iterators).
154
155Another related problem, preserving consistent ordering during iteration,
156is described below. That one is not exclusive to using linked-lists.
157
158
159Challenges from Subclassing dict
160================================
161
162OrderedDict subclasses dict, which is an unusual relationship between two
163builtin types (other than the base object type). Doing so results in
164some complication and deserves further explanation. There are two things
165to consider here. First, in what circumstances or with what adjustments
166can OrderedDict be used as a drop-in replacement for dict (at the C level)?
167Second, how can the OrderedDict implementation leverage the dict
168implementation effectively without introducing unnecessary coupling or
169inefficiencies?
170
171This second point is reflected here and in the implementation, so the
172further focus is on the first point. It is worth noting that for
173overridden methods, the dict implementation is deferred to as much as
174possible. Furthermore, coupling is limited to as little as is reasonable.
175
176Concrete API Compatibility
177--------------------------
178
179Use of the concrete C-API for dict (PyDict_*) with OrderedDict is
180problematic. (See http://bugs.python.org/issue10977.) The concrete API
181has a number of hard-coded assumptions tied to the dict implementation.
182This is, in part, due to performance reasons, which is understandable
183given the part dict plays in Python.
184
185Any attempt to replace dict with OrderedDict for any role in the
186interpreter (e.g. **kwds) faces a challenge. Such any effort must
187recognize that the instances in affected locations currently interact with
188the concrete API.
189
190Here are some ways to address this challenge:
191
1921. Change the relevant usage of the concrete API in CPython and add
Martin Panter69332c12016-08-04 13:07:31 +0000193 PyDict_CheckExact() calls to each of the concrete API functions.
Eric Snow96c6af92015-05-29 22:21:39 -06001942. Adjust the relevant concrete API functions to explicitly accommodate
195 OrderedDict.
1963. As with #1, add the checks, but improve the abstract API with smart fast
197 paths for dict and OrderedDict, and refactor CPython to use the abstract
198 API. Improvements to the abstract API would be valuable regardless.
199
200Adding the checks to the concrete API would help make any interpreter
201switch to OrderedDict less painful for extension modules. However, this
202won't work. The equivalent C API call to `dict.__setitem__(obj, k, v)`
203is 'PyDict_SetItem(obj, k, v)`. This illustrates how subclasses in C call
204the base class's methods, since there is no equivalent of super() in the
205C API. Calling into Python for parent class API would work, but some
206extension modules already rely on this feature of the concrete API.
207
208For reference, here is a breakdown of some of the dict concrete API:
209
210========================== ============= =======================
211concrete API uses abstract API
212========================== ============= =======================
213PyDict_Check PyMapping_Check
214(PyDict_CheckExact) -
215(PyDict_New) -
216(PyDictProxy_New) -
217PyDict_Clear -
218PyDict_Contains PySequence_Contains
219PyDict_Copy -
220PyDict_SetItem PyObject_SetItem
221PyDict_SetItemString PyMapping_SetItemString
222PyDict_DelItem PyMapping_DelItem
223PyDict_DelItemString PyMapping_DelItemString
224PyDict_GetItem -
225PyDict_GetItemWithError PyObject_GetItem
226_PyDict_GetItemIdWithError -
227PyDict_GetItemString PyMapping_GetItemString
228PyDict_Items PyMapping_Items
229PyDict_Keys PyMapping_Keys
230PyDict_Values PyMapping_Values
231PyDict_Size PyMapping_Size
232 PyMapping_Length
233PyDict_Next PyIter_Next
234_PyDict_Next -
235PyDict_Merge -
236PyDict_Update -
237PyDict_MergeFromSeq2 -
238PyDict_ClearFreeList -
239- PyMapping_HasKeyString
240- PyMapping_HasKey
241========================== ============= =======================
242
243
244The dict Interface Relative to OrderedDict
245==========================================
246
247Since OrderedDict subclasses dict, understanding the various methods and
248attributes of dict is important for implementing OrderedDict.
249
250Relevant Type Slots
251-------------------
252
253================= ================ =================== ================
254slot attribute object dict
255================= ================ =================== ================
256tp_dealloc - object_dealloc dict_dealloc
257tp_repr __repr__ object_repr dict_repr
258sq_contains __contains__ - dict_contains
259mp_length __len__ - dict_length
260mp_subscript __getitem__ - dict_subscript
261mp_ass_subscript __setitem__ - dict_ass_sub
262 __delitem__
263tp_hash __hash__ _Py_HashPointer ..._HashNotImpl
264tp_str __str__ object_str -
265tp_getattro __getattribute__ ..._GenericGetAttr (repeated)
266 __getattr__
267tp_setattro __setattr__ ..._GenericSetAttr (disabled)
268tp_doc __doc__ (literal) dictionary_doc
269tp_traverse - - dict_traverse
270tp_clear - - dict_tp_clear
271tp_richcompare __eq__ object_richcompare dict_richcompare
272 __ne__
273tp_weaklistoffset (__weakref__) - -
274tp_iter __iter__ - dict_iter
275tp_dictoffset (__dict__) - -
276tp_init __init__ object_init dict_init
277tp_alloc - PyType_GenericAlloc (repeated)
278tp_new __new__ object_new dict_new
279tp_free - PyObject_Del PyObject_GC_Del
280================= ================ =================== ================
281
282Relevant Methods
283----------------
284
285================ =================== ===============
286method object dict
287================ =================== ===============
288__reduce__ object_reduce -
289__sizeof__ object_sizeof dict_sizeof
290clear - dict_clear
291copy - dict_copy
292fromkeys - dict_fromkeys
293get - dict_get
294items - dictitems_new
295keys - dictkeys_new
296pop - dict_pop
297popitem - dict_popitem
298setdefault - dict_setdefault
299update - dict_update
300values - dictvalues_new
301================ =================== ===============
302
303
304Pure Python OrderedDict
305=======================
306
307As already noted, compatibility with the pure Python OrderedDict
308implementation is a key goal of this C implementation. To further that
309goal, here's a summary of how OrderedDict-specific methods are implemented
310in collections/__init__.py. Also provided is an indication of which
311methods directly mutate or iterate the object, as well as any relationship
312with the underlying linked-list.
313
314============= ============== == ================ === === ====
315method impl used ll uses inq mut iter
316============= ============== == ================ === === ====
317__contains__ dict - - X
318__delitem__ OrderedDict Y dict.__delitem__ X
319__eq__ OrderedDict N OrderedDict ~
320 dict.__eq__
321 __iter__
322__getitem__ dict - - X
323__iter__ OrderedDict Y - X
324__init__ OrderedDict N update
325__len__ dict - - X
326__ne__ MutableMapping - __eq__ ~
327__reduce__ OrderedDict N OrderedDict ~
328 __iter__
329 __getitem__
330__repr__ OrderedDict N __class__ ~
331 items
332__reversed__ OrderedDict Y - X
333__setitem__ OrderedDict Y __contains__ X
334 dict.__setitem__
335__sizeof__ OrderedDict Y __len__ ~
336 __dict__
337clear OrderedDict Y dict.clear X
338copy OrderedDict N __class__
339 __init__
340fromkeys OrderedDict N __setitem__
341get dict - - ~
342items MutableMapping - ItemsView X
343keys MutableMapping - KeysView X
344move_to_end OrderedDict Y - X
345pop OrderedDict N __contains__ X
346 __getitem__
347 __delitem__
348popitem OrderedDict Y dict.pop X
349setdefault OrderedDict N __contains__ ~
350 __getitem__
351 __setitem__
352update MutableMapping - __setitem__ ~
353values MutableMapping - ValuesView X
354============= ============== == ================ === === ====
355
356__reversed__ and move_to_end are both exclusive to OrderedDict.
357
358
359C OrderedDict Implementation
360============================
361
362================= ================
363slot impl
364================= ================
365tp_dealloc odict_dealloc
366tp_repr odict_repr
367mp_ass_subscript odict_ass_sub
368tp_doc odict_doc
369tp_traverse odict_traverse
370tp_clear odict_tp_clear
371tp_richcompare odict_richcompare
372tp_weaklistoffset (offset)
373tp_iter odict_iter
374tp_dictoffset (offset)
375tp_init odict_init
376tp_alloc (repeated)
Eric Snow96c6af92015-05-29 22:21:39 -0600377================= ================
378
379================= ================
380method impl
381================= ================
382__reduce__ odict_reduce
383__sizeof__ odict_sizeof
384clear odict_clear
385copy odict_copy
386fromkeys odict_fromkeys
387items odictitems_new
388keys odictkeys_new
389pop odict_pop
390popitem odict_popitem
391setdefault odict_setdefault
392update odict_update
393values odictvalues_new
394================= ================
395
396Inherited unchanged from object/dict:
397
398================ ==========================
399method type field
400================ ==========================
401- tp_free
402__contains__ tp_as_sequence.sq_contains
403__getattr__ tp_getattro
404__getattribute__ tp_getattro
405__getitem__ tp_as_mapping.mp_subscript
406__hash__ tp_hash
407__len__ tp_as_mapping.mp_length
408__setattr__ tp_setattro
409__str__ tp_str
410get -
411================ ==========================
412
413
414Other Challenges
415================
416
417Preserving Ordering During Iteration
418------------------------------------
419During iteration through an OrderedDict, it is possible that items could
420get added, removed, or reordered. For a linked-list implementation, as
421with some other implementations, that situation may lead to undefined
422behavior. The documentation for dict mentions this in the `iter()` section
423of http://docs.python.org/3.4/library/stdtypes.html#dictionary-view-objects.
424In this implementation we follow dict's lead (as does the pure Python
425implementation) for __iter__(), keys(), values(), and items().
426
427For internal iteration (using _odict_FOREACH or not), there is still the
428risk that not all nodes that we expect to be seen in the loop actually get
429seen. Thus, we are careful in each of those places to ensure that they
430are. This comes, of course, at a small price at each location. The
431solutions are much the same as those detailed in the `Situation that
432Endangers Consistency` section above.
433
434
435Potential Optimizations
436=======================
437
438* Allocate the nodes as a block via od_fast_nodes instead of individually.
439 - Set node->key to NULL to indicate the node is not-in-use.
440 - Add _odict_EXISTS()?
441 - How to maintain consistency across resizes? Existing node pointers
Robert Krzyzanowski6f19fc62018-07-06 05:54:26 -0500442 would be invalidated after a resize, which is particularly problematic
Eric Snow96c6af92015-05-29 22:21:39 -0600443 for the iterators.
444* Use a more stream-lined implementation of update() and, likely indirectly,
445 __init__().
446
447*/
448
449/* TODO
450
451sooner:
452- reentrancy (make sure everything is at a thread-safe state when calling
453 into Python). I've already checked this multiple times, but want to
454 make one more pass.
455- add unit tests for reentrancy?
456
457later:
458- make the dict views support the full set API (the pure Python impl does)
459- implement a fuller MutableMapping API in C?
460- move the MutableMapping implementation to abstract.c?
461- optimize mutablemapping_update
Victor Stinner32bd68c2020-12-01 10:37:39 +0100462- use PyObject_Malloc (small object allocator) for odict nodes?
Eric Snow96c6af92015-05-29 22:21:39 -0600463- support subclasses better (e.g. in odict_richcompare)
464
465*/
466
467#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +0100468#include "pycore_object.h"
Victor Stinner4a21e572020-04-15 02:35:41 +0200469#include <stddef.h> // offsetof()
Eric Snow96c6af92015-05-29 22:21:39 -0600470#include "dict-common.h"
471#include <stddef.h>
472
Victor Stinnerb05cbac2017-01-17 03:46:13 +0100473#include "clinic/odictobject.c.h"
474
475/*[clinic input]
476class OrderedDict "PyODictObject *" "&PyODict_Type"
477[clinic start generated code]*/
478/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ca0641cf6143d4af]*/
479
Eric Snow96c6af92015-05-29 22:21:39 -0600480
481typedef struct _odictnode _ODictNode;
482
483/* PyODictObject */
484struct _odictobject {
Eric Snow4fabf022015-06-04 00:09:56 -0600485 PyDictObject od_dict; /* the underlying dict */
486 _ODictNode *od_first; /* first node in the linked list, if any */
487 _ODictNode *od_last; /* last node in the linked list, if any */
Serhiy Storchaka97f46db2015-11-06 12:00:03 +0200488 /* od_fast_nodes, od_fast_nodes_size and od_resize_sentinel are managed
489 * by _odict_resize().
Eric Snow8c7f9552015-08-07 17:45:12 -0600490 * Note that we rely on implementation details of dict for both. */
491 _ODictNode **od_fast_nodes; /* hash table that mirrors the dict table */
Serhiy Storchaka97f46db2015-11-06 12:00:03 +0200492 Py_ssize_t od_fast_nodes_size;
493 void *od_resize_sentinel; /* changes if odict should be resized */
Eric Snow8c7f9552015-08-07 17:45:12 -0600494
Eric Snow4fabf022015-06-04 00:09:56 -0600495 size_t od_state; /* incremented whenever the LL changes */
496 PyObject *od_inst_dict; /* OrderedDict().__dict__ */
497 PyObject *od_weakreflist; /* holds weakrefs to the odict */
Eric Snow96c6af92015-05-29 22:21:39 -0600498};
499
500
501/* ----------------------------------------------
502 * odict keys (a simple doubly-linked list)
503 */
504
505struct _odictnode {
506 PyObject *key;
Eric Snow4c729182015-06-03 10:50:37 -0600507 Py_hash_t hash;
Eric Snow96c6af92015-05-29 22:21:39 -0600508 _ODictNode *next;
509 _ODictNode *prev;
510};
511
512#define _odictnode_KEY(node) \
513 (node->key)
Eric Snow4c729182015-06-03 10:50:37 -0600514#define _odictnode_HASH(node) \
515 (node->hash)
Eric Snow96c6af92015-05-29 22:21:39 -0600516/* borrowed reference */
517#define _odictnode_VALUE(node, od) \
Eric Snowa762af72015-06-01 22:59:08 -0600518 PyODict_GetItemWithError((PyObject *)od, _odictnode_KEY(node))
Eric Snow96c6af92015-05-29 22:21:39 -0600519#define _odictnode_PREV(node) (node->prev)
520#define _odictnode_NEXT(node) (node->next)
521
522#define _odict_FIRST(od) (((PyODictObject *)od)->od_first)
523#define _odict_LAST(od) (((PyODictObject *)od)->od_last)
524#define _odict_EMPTY(od) (_odict_FIRST(od) == NULL)
525#define _odict_FOREACH(od, node) \
526 for (node = _odict_FIRST(od); node != NULL; node = _odictnode_NEXT(node))
527
Hai Shi46874c22020-01-30 17:20:25 -0600528_Py_IDENTIFIER(items);
529
Eric Snow4c729182015-06-03 10:50:37 -0600530/* Return the index into the hash table, regardless of a valid node. */
531static Py_ssize_t
Serhiy Storchaka19a70e72015-11-13 14:48:36 +0200532_odict_get_index_raw(PyODictObject *od, PyObject *key, Py_hash_t hash)
Eric Snow4c729182015-06-03 10:50:37 -0600533{
INADA Naokiba609772016-12-07 20:41:42 +0900534 PyObject *value = NULL;
Eric Snow4c729182015-06-03 10:50:37 -0600535 PyDictKeysObject *keys = ((PyDictObject *)od)->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700536 Py_ssize_t ix;
Eric Snow4c729182015-06-03 10:50:37 -0600537
INADA Naoki778928b2017-08-03 23:45:15 +0900538 ix = (keys->dk_lookup)((PyDictObject *)od, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -0700539 if (ix == DKIX_EMPTY) {
540 return keys->dk_nentries; /* index of new entry */
541 }
542 if (ix < 0)
Eric Snow4c729182015-06-03 10:50:37 -0600543 return -1;
544 /* We use pointer arithmetic to get the entry's index into the table. */
Victor Stinner742da042016-09-07 17:40:12 -0700545 return ix;
Eric Snow4c729182015-06-03 10:50:37 -0600546}
547
548/* Replace od->od_fast_nodes with a new table matching the size of dict's. */
Eric Snow96c6af92015-05-29 22:21:39 -0600549static int
Serhiy Storchaka6f17e512018-10-20 02:27:45 +0300550_odict_resize(PyODictObject *od)
551{
Eric Snow4c729182015-06-03 10:50:37 -0600552 Py_ssize_t size, i;
Eric Snow96c6af92015-05-29 22:21:39 -0600553 _ODictNode **fast_nodes, *node;
554
555 /* Initialize a new "fast nodes" table. */
556 size = ((PyDictObject *)od)->ma_keys->dk_size;
557 fast_nodes = PyMem_NEW(_ODictNode *, size);
558 if (fast_nodes == NULL) {
559 PyErr_NoMemory();
560 return -1;
561 }
562 for (i = 0; i < size; i++)
563 fast_nodes[i] = NULL;
564
565 /* Copy the current nodes into the table. */
Eric Snow96c6af92015-05-29 22:21:39 -0600566 _odict_FOREACH(od, node) {
Serhiy Storchaka19a70e72015-11-13 14:48:36 +0200567 i = _odict_get_index_raw(od, _odictnode_KEY(node),
Victor Stinner742da042016-09-07 17:40:12 -0700568 _odictnode_HASH(node));
Eric Snow96c6af92015-05-29 22:21:39 -0600569 if (i < 0) {
Victor Stinner00d7abd2020-12-01 09:56:42 +0100570 PyMem_Free(fast_nodes);
Eric Snow96c6af92015-05-29 22:21:39 -0600571 return -1;
572 }
573 fast_nodes[i] = node;
574 }
Eric Snow96c6af92015-05-29 22:21:39 -0600575
576 /* Replace the old fast nodes table. */
Victor Stinner00d7abd2020-12-01 09:56:42 +0100577 PyMem_Free(od->od_fast_nodes);
Eric Snow96c6af92015-05-29 22:21:39 -0600578 od->od_fast_nodes = fast_nodes;
Serhiy Storchaka97f46db2015-11-06 12:00:03 +0200579 od->od_fast_nodes_size = size;
580 od->od_resize_sentinel = ((PyDictObject *)od)->ma_keys;
Eric Snow96c6af92015-05-29 22:21:39 -0600581 return 0;
582}
583
584/* Return the index into the hash table, regardless of a valid node. */
585static Py_ssize_t
Serhiy Storchaka19a70e72015-11-13 14:48:36 +0200586_odict_get_index(PyODictObject *od, PyObject *key, Py_hash_t hash)
Eric Snow96c6af92015-05-29 22:21:39 -0600587{
Eric Snow4c729182015-06-03 10:50:37 -0600588 PyDictKeysObject *keys;
Eric Snow96c6af92015-05-29 22:21:39 -0600589
590 assert(key != NULL);
Eric Snow4c729182015-06-03 10:50:37 -0600591 keys = ((PyDictObject *)od)->ma_keys;
592
593 /* Ensure od_fast_nodes and dk_entries are in sync. */
Serhiy Storchaka97f46db2015-11-06 12:00:03 +0200594 if (od->od_resize_sentinel != keys ||
595 od->od_fast_nodes_size != keys->dk_size) {
Eric Snow4c729182015-06-03 10:50:37 -0600596 int resize_res = _odict_resize(od);
597 if (resize_res < 0)
598 return -1;
599 }
600
Serhiy Storchaka19a70e72015-11-13 14:48:36 +0200601 return _odict_get_index_raw(od, key, hash);
Eric Snow96c6af92015-05-29 22:21:39 -0600602}
603
Eric Snow96c6af92015-05-29 22:21:39 -0600604/* Returns NULL if there was some error or the key was not found. */
605static _ODictNode *
Serhiy Storchaka19a70e72015-11-13 14:48:36 +0200606_odict_find_node_hash(PyODictObject *od, PyObject *key, Py_hash_t hash)
Eric Snow96c6af92015-05-29 22:21:39 -0600607{
608 Py_ssize_t index;
609
610 if (_odict_EMPTY(od))
611 return NULL;
Serhiy Storchaka19a70e72015-11-13 14:48:36 +0200612 index = _odict_get_index(od, key, hash);
613 if (index < 0)
614 return NULL;
Serhiy Storchaka6f17e512018-10-20 02:27:45 +0300615 assert(od->od_fast_nodes != NULL);
Serhiy Storchaka19a70e72015-11-13 14:48:36 +0200616 return od->od_fast_nodes[index];
617}
618
619static _ODictNode *
620_odict_find_node(PyODictObject *od, PyObject *key)
621{
622 Py_ssize_t index;
623 Py_hash_t hash;
624
625 if (_odict_EMPTY(od))
626 return NULL;
627 hash = PyObject_Hash(key);
628 if (hash == -1)
629 return NULL;
630 index = _odict_get_index(od, key, hash);
Eric Snow96c6af92015-05-29 22:21:39 -0600631 if (index < 0)
632 return NULL;
Serhiy Storchaka6f17e512018-10-20 02:27:45 +0300633 assert(od->od_fast_nodes != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -0600634 return od->od_fast_nodes[index];
635}
636
637static void
638_odict_add_head(PyODictObject *od, _ODictNode *node)
639{
Serhiy Storchaka992ec462015-10-14 19:21:24 +0300640 _odictnode_PREV(node) = NULL;
641 _odictnode_NEXT(node) = _odict_FIRST(od);
642 if (_odict_FIRST(od) == NULL)
Eric Snow96c6af92015-05-29 22:21:39 -0600643 _odict_LAST(od) = node;
Serhiy Storchaka992ec462015-10-14 19:21:24 +0300644 else
Eric Snow96c6af92015-05-29 22:21:39 -0600645 _odictnode_PREV(_odict_FIRST(od)) = node;
Serhiy Storchaka992ec462015-10-14 19:21:24 +0300646 _odict_FIRST(od) = node;
Eric Snow4fabf022015-06-04 00:09:56 -0600647 od->od_state++;
Eric Snow96c6af92015-05-29 22:21:39 -0600648}
649
650static void
651_odict_add_tail(PyODictObject *od, _ODictNode *node)
652{
Serhiy Storchaka992ec462015-10-14 19:21:24 +0300653 _odictnode_PREV(node) = _odict_LAST(od);
654 _odictnode_NEXT(node) = NULL;
655 if (_odict_LAST(od) == NULL)
Eric Snow96c6af92015-05-29 22:21:39 -0600656 _odict_FIRST(od) = node;
Serhiy Storchaka992ec462015-10-14 19:21:24 +0300657 else
Eric Snow96c6af92015-05-29 22:21:39 -0600658 _odictnode_NEXT(_odict_LAST(od)) = node;
Serhiy Storchaka992ec462015-10-14 19:21:24 +0300659 _odict_LAST(od) = node;
Eric Snow4fabf022015-06-04 00:09:56 -0600660 od->od_state++;
Eric Snow96c6af92015-05-29 22:21:39 -0600661}
662
663/* adds the node to the end of the list */
664static int
Serhiy Storchaka19a70e72015-11-13 14:48:36 +0200665_odict_add_new_node(PyODictObject *od, PyObject *key, Py_hash_t hash)
Eric Snow96c6af92015-05-29 22:21:39 -0600666{
667 Py_ssize_t i;
668 _ODictNode *node;
669
Serhiy Storchakad17427b2015-10-20 18:21:48 +0300670 Py_INCREF(key);
Serhiy Storchaka19a70e72015-11-13 14:48:36 +0200671 i = _odict_get_index(od, key, hash);
Eric Snow96c6af92015-05-29 22:21:39 -0600672 if (i < 0) {
673 if (!PyErr_Occurred())
674 PyErr_SetObject(PyExc_KeyError, key);
675 Py_DECREF(key);
676 return -1;
677 }
Serhiy Storchaka6f17e512018-10-20 02:27:45 +0300678 assert(od->od_fast_nodes != NULL);
679 if (od->od_fast_nodes[i] != NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -0600680 /* We already have a node for the key so there's no need to add one. */
681 Py_DECREF(key);
682 return 0;
683 }
684
685 /* must not be added yet */
Victor Stinner00d7abd2020-12-01 09:56:42 +0100686 node = (_ODictNode *)PyMem_Malloc(sizeof(_ODictNode));
Eric Snow96c6af92015-05-29 22:21:39 -0600687 if (node == NULL) {
688 Py_DECREF(key);
689 PyErr_NoMemory();
690 return -1;
691 }
692
693 _odictnode_KEY(node) = key;
Eric Snow4c729182015-06-03 10:50:37 -0600694 _odictnode_HASH(node) = hash;
Eric Snow96c6af92015-05-29 22:21:39 -0600695 _odict_add_tail(od, node);
696 od->od_fast_nodes[i] = node;
697 return 0;
698}
699
700/* Putting the decref after the free causes problems. */
701#define _odictnode_DEALLOC(node) \
702 do { \
703 Py_DECREF(_odictnode_KEY(node)); \
Victor Stinner00d7abd2020-12-01 09:56:42 +0100704 PyMem_Free((void *)node); \
Eric Snow96c6af92015-05-29 22:21:39 -0600705 } while (0)
706
707/* Repeated calls on the same node are no-ops. */
708static void
709_odict_remove_node(PyODictObject *od, _ODictNode *node)
710{
711 if (_odict_FIRST(od) == node)
712 _odict_FIRST(od) = _odictnode_NEXT(node);
713 else if (_odictnode_PREV(node) != NULL)
714 _odictnode_NEXT(_odictnode_PREV(node)) = _odictnode_NEXT(node);
715
716 if (_odict_LAST(od) == node)
717 _odict_LAST(od) = _odictnode_PREV(node);
718 else if (_odictnode_NEXT(node) != NULL)
719 _odictnode_PREV(_odictnode_NEXT(node)) = _odictnode_PREV(node);
720
721 _odictnode_PREV(node) = NULL;
722 _odictnode_NEXT(node) = NULL;
Eric Snow4fabf022015-06-04 00:09:56 -0600723 od->od_state++;
Eric Snow96c6af92015-05-29 22:21:39 -0600724}
725
Eric Snow96c6af92015-05-29 22:21:39 -0600726/* If someone calls PyDict_DelItem() directly on an OrderedDict, we'll
727 get all sorts of problems here. In PyODict_DelItem we make sure to
728 call _odict_clear_node first.
Victor Stinnerca30b022015-09-03 17:50:04 +0200729
Eric Snow96c6af92015-05-29 22:21:39 -0600730 This matters in the case of colliding keys. Suppose we add 3 keys:
731 [A, B, C], where the hash of C collides with A and the next possible
732 index in the hash table is occupied by B. If we remove B then for C
733 the dict's looknode func will give us the old index of B instead of
734 the index we got before deleting B. However, the node for C in
735 od_fast_nodes is still at the old dict index of C. Thus to be sure
736 things don't get out of sync, we clear the node in od_fast_nodes
737 *before* calling PyDict_DelItem.
738
739 The same must be done for any other OrderedDict operations where
740 we modify od_fast_nodes.
741*/
742static int
Serhiy Storchaka19a70e72015-11-13 14:48:36 +0200743_odict_clear_node(PyODictObject *od, _ODictNode *node, PyObject *key,
744 Py_hash_t hash)
Eric Snow96c6af92015-05-29 22:21:39 -0600745{
746 Py_ssize_t i;
747
748 assert(key != NULL);
749 if (_odict_EMPTY(od)) {
750 /* Let later code decide if this is a KeyError. */
751 return 0;
752 }
753
Serhiy Storchaka19a70e72015-11-13 14:48:36 +0200754 i = _odict_get_index(od, key, hash);
Eric Snow96c6af92015-05-29 22:21:39 -0600755 if (i < 0)
756 return PyErr_Occurred() ? -1 : 0;
757
Serhiy Storchaka6f17e512018-10-20 02:27:45 +0300758 assert(od->od_fast_nodes != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -0600759 if (node == NULL)
760 node = od->od_fast_nodes[i];
761 assert(node == od->od_fast_nodes[i]);
762 if (node == NULL) {
763 /* Let later code decide if this is a KeyError. */
764 return 0;
765 }
766
767 // Now clear the node.
768 od->od_fast_nodes[i] = NULL;
769 _odict_remove_node(od, node);
770 _odictnode_DEALLOC(node);
771 return 0;
772}
773
774static void
775_odict_clear_nodes(PyODictObject *od)
776{
777 _ODictNode *node, *next;
778
Victor Stinner00d7abd2020-12-01 09:56:42 +0100779 PyMem_Free(od->od_fast_nodes);
Eric Snow96c6af92015-05-29 22:21:39 -0600780 od->od_fast_nodes = NULL;
Serhiy Storchaka6f17e512018-10-20 02:27:45 +0300781 od->od_fast_nodes_size = 0;
782 od->od_resize_sentinel = NULL;
Serhiy Storchakad205d012016-01-19 14:46:25 +0200783
784 node = _odict_FIRST(od);
785 _odict_FIRST(od) = NULL;
786 _odict_LAST(od) = NULL;
787 while (node != NULL) {
788 next = _odictnode_NEXT(node);
789 _odictnode_DEALLOC(node);
790 node = next;
791 }
Eric Snow96c6af92015-05-29 22:21:39 -0600792}
793
794/* There isn't any memory management of nodes past this point. */
795#undef _odictnode_DEALLOC
796
797static int
798_odict_keys_equal(PyODictObject *a, PyODictObject *b)
799{
800 _ODictNode *node_a, *node_b;
801
802 node_a = _odict_FIRST(a);
803 node_b = _odict_FIRST(b);
804 while (1) {
805 if (node_a == NULL && node_b == NULL)
806 /* success: hit the end of each at the same time */
807 return 1;
808 else if (node_a == NULL || node_b == NULL)
809 /* unequal length */
810 return 0;
811 else {
812 int res = PyObject_RichCompareBool(
813 (PyObject *)_odictnode_KEY(node_a),
814 (PyObject *)_odictnode_KEY(node_b),
815 Py_EQ);
816 if (res < 0)
817 return res;
818 else if (res == 0)
819 return 0;
820
821 /* otherwise it must match, so move on to the next one */
822 node_a = _odictnode_NEXT(node_a);
823 node_b = _odictnode_NEXT(node_b);
824 }
825 }
826}
827
828
829/* ----------------------------------------------
830 * OrderedDict mapping methods
831 */
832
833/* mp_ass_subscript: __setitem__() and __delitem__() */
834
835static int
836odict_mp_ass_sub(PyODictObject *od, PyObject *v, PyObject *w)
837{
838 if (w == NULL)
839 return PyODict_DelItem((PyObject *)od, v);
840 else
841 return PyODict_SetItem((PyObject *)od, v, w);
842}
843
844/* tp_as_mapping */
845
846static PyMappingMethods odict_as_mapping = {
847 0, /*mp_length*/
848 0, /*mp_subscript*/
849 (objobjargproc)odict_mp_ass_sub, /*mp_ass_subscript*/
850};
851
852
853/* ----------------------------------------------
Brandt Bucher6d674a12020-03-13 09:06:04 -0700854 * OrderedDict number methods
855 */
856
857static int mutablemapping_update_arg(PyObject*, PyObject*);
858
859static PyObject *
860odict_or(PyObject *left, PyObject *right)
861{
862 PyTypeObject *type;
863 PyObject *other;
864 if (PyODict_Check(left)) {
865 type = Py_TYPE(left);
866 other = right;
867 }
868 else {
869 type = Py_TYPE(right);
870 other = left;
871 }
872 if (!PyDict_Check(other)) {
873 Py_RETURN_NOTIMPLEMENTED;
874 }
875 PyObject *new = PyObject_CallOneArg((PyObject*)type, left);
876 if (!new) {
877 return NULL;
878 }
879 if (mutablemapping_update_arg(new, right) < 0) {
880 Py_DECREF(new);
881 return NULL;
882 }
883 return new;
884}
885
886static PyObject *
887odict_inplace_or(PyObject *self, PyObject *other)
888{
889 if (mutablemapping_update_arg(self, other) < 0) {
890 return NULL;
891 }
Victor Stinnere5014be2020-04-14 17:52:15 +0200892 Py_INCREF(self);
Brandt Bucher6d674a12020-03-13 09:06:04 -0700893 return self;
894}
895
896/* tp_as_number */
897
898static PyNumberMethods odict_as_number = {
899 .nb_or = odict_or,
900 .nb_inplace_or = odict_inplace_or,
901};
902
903
904/* ----------------------------------------------
Eric Snow96c6af92015-05-29 22:21:39 -0600905 * OrderedDict methods
906 */
907
Eric Snow96c6af92015-05-29 22:21:39 -0600908/* fromkeys() */
909
Victor Stinnerb05cbac2017-01-17 03:46:13 +0100910/*[clinic input]
911@classmethod
912OrderedDict.fromkeys
913
914 iterable as seq: object
915 value: object = None
916
Serhiy Storchaka78d9e582017-01-25 00:30:04 +0200917Create a new ordered dictionary with keys from iterable and values set to value.
Victor Stinnerb05cbac2017-01-17 03:46:13 +0100918[clinic start generated code]*/
Eric Snow96c6af92015-05-29 22:21:39 -0600919
920static PyObject *
Victor Stinnerb05cbac2017-01-17 03:46:13 +0100921OrderedDict_fromkeys_impl(PyTypeObject *type, PyObject *seq, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +0200922/*[clinic end generated code: output=c10390d452d78d6d input=1a0476c229c597b3]*/
Eric Snow96c6af92015-05-29 22:21:39 -0600923{
Victor Stinnerb05cbac2017-01-17 03:46:13 +0100924 return _PyDict_FromKeys((PyObject *)type, seq, value);
Eric Snow96c6af92015-05-29 22:21:39 -0600925}
926
927/* __sizeof__() */
928
929/* OrderedDict.__sizeof__() does not have a docstring. */
930PyDoc_STRVAR(odict_sizeof__doc__, "");
931
932static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530933odict_sizeof(PyODictObject *od, PyObject *Py_UNUSED(ignored))
Eric Snow96c6af92015-05-29 22:21:39 -0600934{
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +0200935 Py_ssize_t res = _PyDict_SizeOf((PyDictObject *)od);
Serhiy Storchaka6f17e512018-10-20 02:27:45 +0300936 res += sizeof(_ODictNode *) * od->od_fast_nodes_size; /* od_fast_nodes */
Eric Snow96c6af92015-05-29 22:21:39 -0600937 if (!_odict_EMPTY(od)) {
938 res += sizeof(_ODictNode) * PyODict_SIZE(od); /* linked-list */
939 }
940 return PyLong_FromSsize_t(res);
941}
942
943/* __reduce__() */
944
945PyDoc_STRVAR(odict_reduce__doc__, "Return state information for pickling");
946
947static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530948odict_reduce(register PyODictObject *od, PyObject *Py_UNUSED(ignored))
Eric Snow96c6af92015-05-29 22:21:39 -0600949{
950 _Py_IDENTIFIER(__dict__);
Serhiy Storchaka4575beb2015-10-22 20:18:24 +0300951 PyObject *dict = NULL, *result = NULL;
Serhiy Storchaka8003baf2015-10-18 09:53:17 +0300952 PyObject *items_iter, *items, *args = NULL;
Eric Snow96c6af92015-05-29 22:21:39 -0600953
954 /* capture any instance state */
Serhiy Storchaka8003baf2015-10-18 09:53:17 +0300955 dict = _PyObject_GetAttrId((PyObject *)od, &PyId___dict__);
956 if (dict == NULL)
Eric Snowc5e59602015-05-30 11:28:56 -0600957 goto Done;
958 else {
Eric Snow96c6af92015-05-29 22:21:39 -0600959 /* od.__dict__ isn't necessarily a dict... */
Serhiy Storchaka8003baf2015-10-18 09:53:17 +0300960 Py_ssize_t dict_len = PyObject_Length(dict);
961 if (dict_len == -1)
Eric Snow96c6af92015-05-29 22:21:39 -0600962 goto Done;
Serhiy Storchaka8003baf2015-10-18 09:53:17 +0300963 if (!dict_len) {
964 /* nothing to pickle in od.__dict__ */
965 Py_CLEAR(dict);
Eric Snow96c6af92015-05-29 22:21:39 -0600966 }
967 }
968
969 /* build the result */
Eric Snow96c6af92015-05-29 22:21:39 -0600970 args = PyTuple_New(0);
971 if (args == NULL)
972 goto Done;
973
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200974 items = _PyObject_CallMethodIdNoArgs((PyObject *)od, &PyId_items);
Eric Snow96c6af92015-05-29 22:21:39 -0600975 if (items == NULL)
976 goto Done;
977
978 items_iter = PyObject_GetIter(items);
Serhiy Storchaka8003baf2015-10-18 09:53:17 +0300979 Py_DECREF(items);
Eric Snow96c6af92015-05-29 22:21:39 -0600980 if (items_iter == NULL)
981 goto Done;
982
Serhiy Storchaka4575beb2015-10-22 20:18:24 +0300983 result = PyTuple_Pack(5, Py_TYPE(od), args, dict ? dict : Py_None, Py_None, items_iter);
Serhiy Storchaka8003baf2015-10-18 09:53:17 +0300984 Py_DECREF(items_iter);
Eric Snow96c6af92015-05-29 22:21:39 -0600985
986Done:
Serhiy Storchaka8003baf2015-10-18 09:53:17 +0300987 Py_XDECREF(dict);
Eric Snow96c6af92015-05-29 22:21:39 -0600988 Py_XDECREF(args);
Eric Snow96c6af92015-05-29 22:21:39 -0600989
990 return result;
991}
992
Victor Stinnerb05cbac2017-01-17 03:46:13 +0100993/* setdefault(): Skips __missing__() calls. */
Eric Snow96c6af92015-05-29 22:21:39 -0600994
Eric Snow96c6af92015-05-29 22:21:39 -0600995
Victor Stinnerb05cbac2017-01-17 03:46:13 +0100996/*[clinic input]
997OrderedDict.setdefault
998
999 key: object
Serhiy Storchakaa70eaf22017-01-19 19:38:13 +02001000 default: object = None
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001001
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02001002Insert key with a value of default if key is not in the dictionary.
1003
1004Return the value for key if key is in the dictionary, else default.
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001005[clinic start generated code]*/
1006
Eric Snow96c6af92015-05-29 22:21:39 -06001007static PyObject *
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001008OrderedDict_setdefault_impl(PyODictObject *self, PyObject *key,
Serhiy Storchakaa70eaf22017-01-19 19:38:13 +02001009 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02001010/*[clinic end generated code: output=97537cb7c28464b6 input=38e098381c1efbc6]*/
Eric Snow96c6af92015-05-29 22:21:39 -06001011{
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001012 PyObject *result = NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06001013
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001014 if (PyODict_CheckExact(self)) {
1015 result = PyODict_GetItemWithError(self, key); /* borrowed */
Eric Snowd1719752015-06-01 23:12:13 -06001016 if (result == NULL) {
1017 if (PyErr_Occurred())
1018 return NULL;
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001019 assert(_odict_find_node(self, key) == NULL);
Serhiy Storchakaa70eaf22017-01-19 19:38:13 +02001020 if (PyODict_SetItem((PyObject *)self, key, default_value) >= 0) {
1021 result = default_value;
1022 Py_INCREF(result);
Eric Snow96c6af92015-05-29 22:21:39 -06001023 }
1024 }
1025 else {
Eric Snowd1719752015-06-01 23:12:13 -06001026 Py_INCREF(result);
Eric Snow96c6af92015-05-29 22:21:39 -06001027 }
1028 }
1029 else {
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001030 int exists = PySequence_Contains((PyObject *)self, key);
Eric Snow96c6af92015-05-29 22:21:39 -06001031 if (exists < 0) {
Eric Snowd1719752015-06-01 23:12:13 -06001032 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06001033 }
1034 else if (exists) {
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001035 result = PyObject_GetItem((PyObject *)self, key);
Eric Snow96c6af92015-05-29 22:21:39 -06001036 }
Serhiy Storchakaa70eaf22017-01-19 19:38:13 +02001037 else if (PyObject_SetItem((PyObject *)self, key, default_value) >= 0) {
1038 result = default_value;
1039 Py_INCREF(result);
Eric Snow96c6af92015-05-29 22:21:39 -06001040 }
1041 }
1042
Eric Snow96c6af92015-05-29 22:21:39 -06001043 return result;
1044}
1045
1046/* pop() */
1047
Eric Snow96c6af92015-05-29 22:21:39 -06001048/* forward */
1049static PyObject * _odict_popkey(PyObject *, PyObject *, PyObject *);
1050
1051/* Skips __missing__() calls. */
Serhiy Storchaka6bf323732020-07-19 09:18:55 +03001052/*[clinic input]
1053OrderedDict.pop
1054
1055 key: object
1056 default: object = NULL
1057
1058od.pop(key[,default]) -> v, remove specified key and return the corresponding value.
1059
1060If the key is not found, return the default if given; otherwise,
1061raise a KeyError.
1062[clinic start generated code]*/
1063
Eric Snow96c6af92015-05-29 22:21:39 -06001064static PyObject *
Serhiy Storchaka6bf323732020-07-19 09:18:55 +03001065OrderedDict_pop_impl(PyODictObject *self, PyObject *key,
1066 PyObject *default_value)
1067/*[clinic end generated code: output=7a6447d104e7494b input=7efe36601007dff7]*/
Eric Snow96c6af92015-05-29 22:21:39 -06001068{
Serhiy Storchaka6bf323732020-07-19 09:18:55 +03001069 return _odict_popkey((PyObject *)self, key, default_value);
Eric Snow96c6af92015-05-29 22:21:39 -06001070}
1071
1072static PyObject *
Serhiy Storchaka19a70e72015-11-13 14:48:36 +02001073_odict_popkey_hash(PyObject *od, PyObject *key, PyObject *failobj,
1074 Py_hash_t hash)
Eric Snow96c6af92015-05-29 22:21:39 -06001075{
1076 _ODictNode *node;
1077 PyObject *value = NULL;
1078
Eric Snow96c6af92015-05-29 22:21:39 -06001079 /* Pop the node first to avoid a possible dict resize (due to
1080 eval loop reentrancy) and complications due to hash collision
1081 resolution. */
Serhiy Storchaka19a70e72015-11-13 14:48:36 +02001082 node = _odict_find_node_hash((PyODictObject *)od, key, hash);
Eric Snow96c6af92015-05-29 22:21:39 -06001083 if (node == NULL) {
1084 if (PyErr_Occurred())
Eric Snowd1719752015-06-01 23:12:13 -06001085 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06001086 }
1087 else {
Serhiy Storchaka19a70e72015-11-13 14:48:36 +02001088 int res = _odict_clear_node((PyODictObject *)od, node, key, hash);
Eric Snow96c6af92015-05-29 22:21:39 -06001089 if (res < 0) {
Eric Snowd1719752015-06-01 23:12:13 -06001090 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06001091 }
1092 }
1093
1094 /* Now delete the value from the dict. */
Serhiy Storchaka04386832016-10-30 17:17:24 +02001095 if (PyODict_CheckExact(od)) {
1096 if (node != NULL) {
1097 value = _PyDict_GetItem_KnownHash(od, key, hash); /* borrowed */
1098 if (value != NULL) {
1099 Py_INCREF(value);
1100 if (_PyDict_DelItem_KnownHash(od, key, hash) < 0) {
1101 Py_DECREF(value);
1102 return NULL;
1103 }
1104 }
1105 }
1106 }
1107 else {
1108 int exists = PySequence_Contains(od, key);
1109 if (exists < 0)
1110 return NULL;
1111 if (exists) {
1112 value = PyObject_GetItem(od, key);
1113 if (value != NULL) {
1114 if (PyObject_DelItem(od, key) == -1) {
1115 Py_CLEAR(value);
1116 }
Eric Snow96c6af92015-05-29 22:21:39 -06001117 }
1118 }
1119 }
1120
1121 /* Apply the fallback value, if necessary. */
1122 if (value == NULL && !PyErr_Occurred()) {
1123 if (failobj) {
1124 value = failobj;
1125 Py_INCREF(failobj);
1126 }
1127 else {
1128 PyErr_SetObject(PyExc_KeyError, key);
1129 }
1130 }
1131
Eric Snow96c6af92015-05-29 22:21:39 -06001132 return value;
1133}
1134
Serhiy Storchaka19a70e72015-11-13 14:48:36 +02001135static PyObject *
1136_odict_popkey(PyObject *od, PyObject *key, PyObject *failobj)
1137{
1138 Py_hash_t hash = PyObject_Hash(key);
1139 if (hash == -1)
1140 return NULL;
1141
1142 return _odict_popkey_hash(od, key, failobj, hash);
1143}
1144
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001145
Eric Snow96c6af92015-05-29 22:21:39 -06001146/* popitem() */
1147
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001148/*[clinic input]
1149OrderedDict.popitem
1150
1151 last: bool = True
1152
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02001153Remove and return a (key, value) pair from the dictionary.
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001154
1155Pairs are returned in LIFO order if last is true or FIFO order if false.
1156[clinic start generated code]*/
Eric Snow96c6af92015-05-29 22:21:39 -06001157
1158static PyObject *
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001159OrderedDict_popitem_impl(PyODictObject *self, int last)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02001160/*[clinic end generated code: output=98e7d986690d49eb input=d992ac5ee8305e1a]*/
Eric Snow96c6af92015-05-29 22:21:39 -06001161{
Serhiy Storchaka8003baf2015-10-18 09:53:17 +03001162 PyObject *key, *value, *item = NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06001163 _ODictNode *node;
Eric Snow96c6af92015-05-29 22:21:39 -06001164
1165 /* pull the item */
1166
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001167 if (_odict_EMPTY(self)) {
Serhiy Storchaka8003baf2015-10-18 09:53:17 +03001168 PyErr_SetString(PyExc_KeyError, "dictionary is empty");
1169 return NULL;
Eric Snowac02ef32015-06-02 20:42:14 -06001170 }
Eric Snow96c6af92015-05-29 22:21:39 -06001171
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001172 node = last ? _odict_LAST(self) : _odict_FIRST(self);
Eric Snow96c6af92015-05-29 22:21:39 -06001173 key = _odictnode_KEY(node);
Eric Snowd1719752015-06-01 23:12:13 -06001174 Py_INCREF(key);
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001175 value = _odict_popkey_hash((PyObject *)self, key, NULL, _odictnode_HASH(node));
Eric Snow96c6af92015-05-29 22:21:39 -06001176 if (value == NULL)
1177 return NULL;
1178 item = PyTuple_Pack(2, key, value);
Eric Snowd1719752015-06-01 23:12:13 -06001179 Py_DECREF(key);
Eric Snow96c6af92015-05-29 22:21:39 -06001180 Py_DECREF(value);
1181 return item;
1182}
1183
1184/* keys() */
1185
1186/* MutableMapping.keys() does not have a docstring. */
1187PyDoc_STRVAR(odict_keys__doc__, "");
1188
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301189static PyObject * odictkeys_new(PyObject *od, PyObject *Py_UNUSED(ignored)); /* forward */
Eric Snow96c6af92015-05-29 22:21:39 -06001190
1191/* values() */
1192
1193/* MutableMapping.values() does not have a docstring. */
1194PyDoc_STRVAR(odict_values__doc__, "");
1195
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301196static PyObject * odictvalues_new(PyObject *od, PyObject *Py_UNUSED(ignored)); /* forward */
Eric Snow96c6af92015-05-29 22:21:39 -06001197
1198/* items() */
1199
1200/* MutableMapping.items() does not have a docstring. */
1201PyDoc_STRVAR(odict_items__doc__, "");
1202
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301203static PyObject * odictitems_new(PyObject *od, PyObject *Py_UNUSED(ignored)); /* forward */
Eric Snow96c6af92015-05-29 22:21:39 -06001204
1205/* update() */
1206
1207/* MutableMapping.update() does not have a docstring. */
1208PyDoc_STRVAR(odict_update__doc__, "");
1209
1210/* forward */
1211static PyObject * mutablemapping_update(PyObject *, PyObject *, PyObject *);
1212
1213#define odict_update mutablemapping_update
1214
1215/* clear() */
1216
1217PyDoc_STRVAR(odict_clear__doc__,
1218 "od.clear() -> None. Remove all items from od.");
1219
1220static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301221odict_clear(register PyODictObject *od, PyObject *Py_UNUSED(ignored))
Eric Snow96c6af92015-05-29 22:21:39 -06001222{
1223 PyDict_Clear((PyObject *)od);
1224 _odict_clear_nodes(od);
Eric Snow96c6af92015-05-29 22:21:39 -06001225 Py_RETURN_NONE;
1226}
1227
1228/* copy() */
1229
Serhiy Storchaka19a70e72015-11-13 14:48:36 +02001230/* forward */
1231static int _PyODict_SetItem_KnownHash(PyObject *, PyObject *, PyObject *,
1232 Py_hash_t);
1233
Eric Snow96c6af92015-05-29 22:21:39 -06001234PyDoc_STRVAR(odict_copy__doc__, "od.copy() -> a shallow copy of od");
1235
1236static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301237odict_copy(register PyODictObject *od, PyObject *Py_UNUSED(ignored))
Eric Snow96c6af92015-05-29 22:21:39 -06001238{
1239 _ODictNode *node;
1240 PyObject *od_copy;
1241
1242 if (PyODict_CheckExact(od))
1243 od_copy = PyODict_New();
1244 else
Victor Stinnerf17c3de2016-12-06 18:46:19 +01001245 od_copy = _PyObject_CallNoArg((PyObject *)Py_TYPE(od));
Eric Snow96c6af92015-05-29 22:21:39 -06001246 if (od_copy == NULL)
1247 return NULL;
1248
1249 if (PyODict_CheckExact(od)) {
1250 _odict_FOREACH(od, node) {
Eric Snowa762af72015-06-01 22:59:08 -06001251 PyObject *key = _odictnode_KEY(node);
1252 PyObject *value = _odictnode_VALUE(node, od);
1253 if (value == NULL) {
1254 if (!PyErr_Occurred())
1255 PyErr_SetObject(PyExc_KeyError, key);
1256 goto fail;
1257 }
Serhiy Storchaka19a70e72015-11-13 14:48:36 +02001258 if (_PyODict_SetItem_KnownHash((PyObject *)od_copy, key, value,
1259 _odictnode_HASH(node)) != 0)
Eric Snow96c6af92015-05-29 22:21:39 -06001260 goto fail;
1261 }
1262 }
1263 else {
1264 _odict_FOREACH(od, node) {
1265 int res;
1266 PyObject *value = PyObject_GetItem((PyObject *)od,
1267 _odictnode_KEY(node));
1268 if (value == NULL)
1269 goto fail;
1270 res = PyObject_SetItem((PyObject *)od_copy,
1271 _odictnode_KEY(node), value);
1272 Py_DECREF(value);
1273 if (res != 0)
1274 goto fail;
1275 }
1276 }
1277 return od_copy;
1278
1279fail:
1280 Py_DECREF(od_copy);
1281 return NULL;
1282}
1283
1284/* __reversed__() */
1285
1286PyDoc_STRVAR(odict_reversed__doc__, "od.__reversed__() <==> reversed(od)");
1287
1288#define _odict_ITER_REVERSED 1
1289#define _odict_ITER_KEYS 2
1290#define _odict_ITER_VALUES 4
Miss Islington (bot)1b372682021-12-29 21:29:03 -08001291#define _odict_ITER_ITEMS (_odict_ITER_KEYS|_odict_ITER_VALUES)
Eric Snow96c6af92015-05-29 22:21:39 -06001292
1293/* forward */
1294static PyObject * odictiter_new(PyODictObject *, int);
1295
1296static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301297odict_reversed(PyODictObject *od, PyObject *Py_UNUSED(ignored))
Eric Snow96c6af92015-05-29 22:21:39 -06001298{
Eric Snow96c6af92015-05-29 22:21:39 -06001299 return odictiter_new(od, _odict_ITER_KEYS|_odict_ITER_REVERSED);
1300}
1301
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001302
Eric Snow96c6af92015-05-29 22:21:39 -06001303/* move_to_end() */
1304
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001305/*[clinic input]
1306OrderedDict.move_to_end
1307
1308 key: object
1309 last: bool = True
1310
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02001311Move an existing element to the end (or beginning if last is false).
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001312
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02001313Raise KeyError if the element does not exist.
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001314[clinic start generated code]*/
Eric Snow96c6af92015-05-29 22:21:39 -06001315
1316static PyObject *
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001317OrderedDict_move_to_end_impl(PyODictObject *self, PyObject *key, int last)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02001318/*[clinic end generated code: output=fafa4c5cc9b92f20 input=d6ceff7132a2fcd7]*/
Eric Snow96c6af92015-05-29 22:21:39 -06001319{
Serhiy Storchaka8003baf2015-10-18 09:53:17 +03001320 _ODictNode *node;
Eric Snow96c6af92015-05-29 22:21:39 -06001321
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001322 if (_odict_EMPTY(self)) {
Eric Snow96c6af92015-05-29 22:21:39 -06001323 PyErr_SetObject(PyExc_KeyError, key);
Eric Snow96c6af92015-05-29 22:21:39 -06001324 return NULL;
1325 }
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001326 node = last ? _odict_LAST(self) : _odict_FIRST(self);
Serhiy Storchaka8003baf2015-10-18 09:53:17 +03001327 if (key != _odictnode_KEY(node)) {
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001328 node = _odict_find_node(self, key);
Serhiy Storchaka8003baf2015-10-18 09:53:17 +03001329 if (node == NULL) {
1330 if (!PyErr_Occurred())
1331 PyErr_SetObject(PyExc_KeyError, key);
Eric Snowc5e59602015-05-30 11:28:56 -06001332 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06001333 }
Serhiy Storchaka8003baf2015-10-18 09:53:17 +03001334 if (last) {
1335 /* Only move if not already the last one. */
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001336 if (node != _odict_LAST(self)) {
1337 _odict_remove_node(self, node);
1338 _odict_add_tail(self, node);
Eric Snow96c6af92015-05-29 22:21:39 -06001339 }
Serhiy Storchaka8003baf2015-10-18 09:53:17 +03001340 }
1341 else {
1342 /* Only move if not already the first one. */
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001343 if (node != _odict_FIRST(self)) {
1344 _odict_remove_node(self, node);
1345 _odict_add_head(self, node);
Eric Snow96c6af92015-05-29 22:21:39 -06001346 }
1347 }
1348 }
Eric Snow96c6af92015-05-29 22:21:39 -06001349 Py_RETURN_NONE;
1350}
1351
1352
1353/* tp_methods */
1354
1355static PyMethodDef odict_methods[] = {
1356
Eric Snow96c6af92015-05-29 22:21:39 -06001357 /* overridden dict methods */
Serhiy Storchaka827d49f2018-04-09 19:14:26 +03001358 ORDEREDDICT_FROMKEYS_METHODDEF
Eric Snow96c6af92015-05-29 22:21:39 -06001359 {"__sizeof__", (PyCFunction)odict_sizeof, METH_NOARGS,
1360 odict_sizeof__doc__},
1361 {"__reduce__", (PyCFunction)odict_reduce, METH_NOARGS,
1362 odict_reduce__doc__},
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001363 ORDEREDDICT_SETDEFAULT_METHODDEF
Serhiy Storchaka6bf323732020-07-19 09:18:55 +03001364 ORDEREDDICT_POP_METHODDEF
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001365 ORDEREDDICT_POPITEM_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301366 {"keys", odictkeys_new, METH_NOARGS,
Eric Snow96c6af92015-05-29 22:21:39 -06001367 odict_keys__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301368 {"values", odictvalues_new, METH_NOARGS,
Eric Snow96c6af92015-05-29 22:21:39 -06001369 odict_values__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301370 {"items", odictitems_new, METH_NOARGS,
Eric Snow96c6af92015-05-29 22:21:39 -06001371 odict_items__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001372 {"update", (PyCFunction)(void(*)(void))odict_update, METH_VARARGS | METH_KEYWORDS,
Eric Snow96c6af92015-05-29 22:21:39 -06001373 odict_update__doc__},
1374 {"clear", (PyCFunction)odict_clear, METH_NOARGS,
1375 odict_clear__doc__},
1376 {"copy", (PyCFunction)odict_copy, METH_NOARGS,
1377 odict_copy__doc__},
1378
1379 /* new methods */
1380 {"__reversed__", (PyCFunction)odict_reversed, METH_NOARGS,
1381 odict_reversed__doc__},
Victor Stinnerb05cbac2017-01-17 03:46:13 +01001382 ORDEREDDICT_MOVE_TO_END_METHODDEF
Eric Snow96c6af92015-05-29 22:21:39 -06001383
1384 {NULL, NULL} /* sentinel */
1385};
1386
1387
1388/* ----------------------------------------------
1389 * OrderedDict members
1390 */
1391
Serhiy Storchakad2962f12016-02-08 16:39:05 +02001392/* tp_getset */
Eric Snow96c6af92015-05-29 22:21:39 -06001393
Serhiy Storchakad2962f12016-02-08 16:39:05 +02001394static PyGetSetDef odict_getset[] = {
1395 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
1396 {NULL}
Eric Snow96c6af92015-05-29 22:21:39 -06001397};
1398
Eric Snow96c6af92015-05-29 22:21:39 -06001399/* ----------------------------------------------
1400 * OrderedDict type slot methods
1401 */
1402
1403/* tp_dealloc */
1404
1405static void
1406odict_dealloc(PyODictObject *self)
1407{
1408 PyObject_GC_UnTrack(self);
Jeroen Demeyer351c6742019-05-10 19:21:11 +02001409 Py_TRASHCAN_BEGIN(self, odict_dealloc)
Serhiy Storchaka14eefe32015-11-01 16:12:34 +02001410
Eric Snow96c6af92015-05-29 22:21:39 -06001411 Py_XDECREF(self->od_inst_dict);
1412 if (self->od_weakreflist != NULL)
1413 PyObject_ClearWeakRefs((PyObject *)self);
1414
1415 _odict_clear_nodes(self);
Eric Snow96c6af92015-05-29 22:21:39 -06001416 PyDict_Type.tp_dealloc((PyObject *)self);
Serhiy Storchaka14eefe32015-11-01 16:12:34 +02001417
Jeroen Demeyer351c6742019-05-10 19:21:11 +02001418 Py_TRASHCAN_END
Victor Stinnere1871952016-06-08 10:18:18 +02001419}
Eric Snow96c6af92015-05-29 22:21:39 -06001420
1421/* tp_repr */
1422
1423static PyObject *
1424odict_repr(PyODictObject *self)
1425{
1426 int i;
Serhiy Storchaka4575beb2015-10-22 20:18:24 +03001427 PyObject *pieces = NULL, *result = NULL;
Serhiy Storchaka4575beb2015-10-22 20:18:24 +03001428
1429 if (PyODict_SIZE(self) == 0)
Serhiy Storchaka4ab46d72017-09-17 21:11:04 +03001430 return PyUnicode_FromFormat("%s()", _PyType_Name(Py_TYPE(self)));
Eric Snow96c6af92015-05-29 22:21:39 -06001431
1432 i = Py_ReprEnter((PyObject *)self);
1433 if (i != 0) {
1434 return i > 0 ? PyUnicode_FromString("...") : NULL;
1435 }
1436
Eric Snow96c6af92015-05-29 22:21:39 -06001437 if (PyODict_CheckExact(self)) {
Serhiy Storchaka710cd342015-11-04 22:33:07 +02001438 Py_ssize_t count = 0;
Eric Snowa762af72015-06-01 22:59:08 -06001439 _ODictNode *node;
Eric Snow96c6af92015-05-29 22:21:39 -06001440 pieces = PyList_New(PyODict_SIZE(self));
1441 if (pieces == NULL)
1442 goto Done;
1443
1444 _odict_FOREACH(self, node) {
Eric Snowa762af72015-06-01 22:59:08 -06001445 PyObject *pair;
1446 PyObject *key = _odictnode_KEY(node);
1447 PyObject *value = _odictnode_VALUE(node, self);
1448 if (value == NULL) {
1449 if (!PyErr_Occurred())
1450 PyErr_SetObject(PyExc_KeyError, key);
Eric Snowdb4061c2015-06-03 11:09:48 -06001451 goto Done;
Eric Snowa762af72015-06-01 22:59:08 -06001452 }
1453 pair = PyTuple_Pack(2, key, value);
Eric Snow96c6af92015-05-29 22:21:39 -06001454 if (pair == NULL)
1455 goto Done;
1456
Serhiy Storchaka710cd342015-11-04 22:33:07 +02001457 if (count < PyList_GET_SIZE(pieces))
1458 PyList_SET_ITEM(pieces, count, pair); /* steals reference */
1459 else {
1460 if (PyList_Append(pieces, pair) < 0) {
1461 Py_DECREF(pair);
1462 goto Done;
1463 }
1464 Py_DECREF(pair);
1465 }
1466 count++;
Eric Snow96c6af92015-05-29 22:21:39 -06001467 }
Victor Stinner60ac6ed2020-02-07 23:18:08 +01001468 if (count < PyList_GET_SIZE(pieces)) {
1469 Py_SET_SIZE(pieces, count);
1470 }
Eric Snow96c6af92015-05-29 22:21:39 -06001471 }
1472 else {
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001473 PyObject *items = _PyObject_CallMethodIdNoArgs((PyObject *)self,
1474 &PyId_items);
Eric Snow96c6af92015-05-29 22:21:39 -06001475 if (items == NULL)
1476 goto Done;
1477 pieces = PySequence_List(items);
1478 Py_DECREF(items);
Serhiy Storchaka8003baf2015-10-18 09:53:17 +03001479 if (pieces == NULL)
Eric Snow96c6af92015-05-29 22:21:39 -06001480 goto Done;
1481 }
1482
Serhiy Storchaka4ab46d72017-09-17 21:11:04 +03001483 result = PyUnicode_FromFormat("%s(%R)",
1484 _PyType_Name(Py_TYPE(self)), pieces);
Eric Snow96c6af92015-05-29 22:21:39 -06001485
Eric Snow96c6af92015-05-29 22:21:39 -06001486Done:
1487 Py_XDECREF(pieces);
Eric Snow96c6af92015-05-29 22:21:39 -06001488 Py_ReprLeave((PyObject *)self);
1489 return result;
Victor Stinnere1871952016-06-08 10:18:18 +02001490}
Eric Snow96c6af92015-05-29 22:21:39 -06001491
1492/* tp_doc */
1493
1494PyDoc_STRVAR(odict_doc,
1495 "Dictionary that remembers insertion order");
1496
1497/* tp_traverse */
1498
1499static int
1500odict_traverse(PyODictObject *od, visitproc visit, void *arg)
1501{
Serhiy Storchakad205d012016-01-19 14:46:25 +02001502 _ODictNode *node;
1503
Eric Snow96c6af92015-05-29 22:21:39 -06001504 Py_VISIT(od->od_inst_dict);
Serhiy Storchakad205d012016-01-19 14:46:25 +02001505 _odict_FOREACH(od, node) {
1506 Py_VISIT(_odictnode_KEY(node));
1507 }
Eric Snow96c6af92015-05-29 22:21:39 -06001508 return PyDict_Type.tp_traverse((PyObject *)od, visit, arg);
1509}
1510
1511/* tp_clear */
1512
1513static int
1514odict_tp_clear(PyODictObject *od)
1515{
Eric Snow96c6af92015-05-29 22:21:39 -06001516 Py_CLEAR(od->od_inst_dict);
Serhiy Storchaka6f17e512018-10-20 02:27:45 +03001517 PyDict_Clear((PyObject *)od);
1518 _odict_clear_nodes(od);
Eric Snow96c6af92015-05-29 22:21:39 -06001519 return 0;
1520}
1521
1522/* tp_richcompare */
1523
1524static PyObject *
1525odict_richcompare(PyObject *v, PyObject *w, int op)
1526{
1527 if (!PyODict_Check(v) || !PyDict_Check(w)) {
1528 Py_RETURN_NOTIMPLEMENTED;
1529 }
1530
1531 if (op == Py_EQ || op == Py_NE) {
1532 PyObject *res, *cmp;
1533 int eq;
1534
1535 cmp = PyDict_Type.tp_richcompare(v, w, op);
1536 if (cmp == NULL)
1537 return NULL;
1538 if (!PyODict_Check(w))
1539 return cmp;
1540 if (op == Py_EQ && cmp == Py_False)
1541 return cmp;
1542 if (op == Py_NE && cmp == Py_True)
1543 return cmp;
1544 Py_DECREF(cmp);
1545
1546 /* Try comparing odict keys. */
1547 eq = _odict_keys_equal((PyODictObject *)v, (PyODictObject *)w);
1548 if (eq < 0)
1549 return NULL;
1550
1551 res = (eq == (op == Py_EQ)) ? Py_True : Py_False;
1552 Py_INCREF(res);
1553 return res;
1554 } else {
1555 Py_RETURN_NOTIMPLEMENTED;
1556 }
Victor Stinnere1871952016-06-08 10:18:18 +02001557}
Eric Snow96c6af92015-05-29 22:21:39 -06001558
1559/* tp_iter */
1560
1561static PyObject *
1562odict_iter(PyODictObject *od)
1563{
1564 return odictiter_new(od, _odict_ITER_KEYS);
Victor Stinnere1871952016-06-08 10:18:18 +02001565}
Eric Snow96c6af92015-05-29 22:21:39 -06001566
1567/* tp_init */
1568
1569static int
1570odict_init(PyObject *self, PyObject *args, PyObject *kwds)
1571{
1572 PyObject *res;
1573 Py_ssize_t len = PyObject_Length(args);
1574
1575 if (len == -1)
1576 return -1;
1577 if (len > 1) {
Serhiy Storchakad53fe5f2019-03-13 22:59:55 +02001578 const char *msg = "expected at most 1 arguments, got %zd";
Eric Snow96c6af92015-05-29 22:21:39 -06001579 PyErr_Format(PyExc_TypeError, msg, len);
1580 return -1;
1581 }
1582
1583 /* __init__() triggering update() is just the way things are! */
1584 res = odict_update(self, args, kwds);
1585 if (res == NULL) {
1586 return -1;
1587 } else {
1588 Py_DECREF(res);
1589 return 0;
1590 }
Victor Stinnere1871952016-06-08 10:18:18 +02001591}
Eric Snow96c6af92015-05-29 22:21:39 -06001592
Eric Snow96c6af92015-05-29 22:21:39 -06001593/* PyODict_Type */
1594
1595PyTypeObject PyODict_Type = {
1596 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1597 "collections.OrderedDict", /* tp_name */
1598 sizeof(PyODictObject), /* tp_basicsize */
1599 0, /* tp_itemsize */
1600 (destructor)odict_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001601 0, /* tp_vectorcall_offset */
Eric Snow96c6af92015-05-29 22:21:39 -06001602 0, /* tp_getattr */
1603 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001604 0, /* tp_as_async */
Eric Snow96c6af92015-05-29 22:21:39 -06001605 (reprfunc)odict_repr, /* tp_repr */
Brandt Bucher6d674a12020-03-13 09:06:04 -07001606 &odict_as_number, /* tp_as_number */
Eric Snow96c6af92015-05-29 22:21:39 -06001607 0, /* tp_as_sequence */
1608 &odict_as_mapping, /* tp_as_mapping */
1609 0, /* tp_hash */
1610 0, /* tp_call */
1611 0, /* tp_str */
1612 0, /* tp_getattro */
1613 0, /* tp_setattro */
1614 0, /* tp_as_buffer */
1615 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1616 odict_doc, /* tp_doc */
1617 (traverseproc)odict_traverse, /* tp_traverse */
1618 (inquiry)odict_tp_clear, /* tp_clear */
1619 (richcmpfunc)odict_richcompare, /* tp_richcompare */
1620 offsetof(PyODictObject, od_weakreflist), /* tp_weaklistoffset */
1621 (getiterfunc)odict_iter, /* tp_iter */
1622 0, /* tp_iternext */
1623 odict_methods, /* tp_methods */
Serhiy Storchakad2962f12016-02-08 16:39:05 +02001624 0, /* tp_members */
1625 odict_getset, /* tp_getset */
Eric Snow96c6af92015-05-29 22:21:39 -06001626 &PyDict_Type, /* tp_base */
1627 0, /* tp_dict */
1628 0, /* tp_descr_get */
1629 0, /* tp_descr_set */
1630 offsetof(PyODictObject, od_inst_dict), /* tp_dictoffset */
1631 (initproc)odict_init, /* tp_init */
1632 PyType_GenericAlloc, /* tp_alloc */
Serhiy Storchaka6f17e512018-10-20 02:27:45 +03001633 0, /* tp_new */
Eric Snow96c6af92015-05-29 22:21:39 -06001634 0, /* tp_free */
1635};
1636
1637
1638/* ----------------------------------------------
1639 * the public OrderedDict API
1640 */
1641
1642PyObject *
Serhiy Storchaka6f17e512018-10-20 02:27:45 +03001643PyODict_New(void)
1644{
1645 return PyDict_Type.tp_new(&PyODict_Type, NULL, NULL);
Victor Stinnere1871952016-06-08 10:18:18 +02001646}
Eric Snow96c6af92015-05-29 22:21:39 -06001647
Serhiy Storchaka19a70e72015-11-13 14:48:36 +02001648static int
1649_PyODict_SetItem_KnownHash(PyObject *od, PyObject *key, PyObject *value,
1650 Py_hash_t hash)
1651{
1652 int res = _PyDict_SetItem_KnownHash(od, key, value, hash);
Eric Snow96c6af92015-05-29 22:21:39 -06001653 if (res == 0) {
Serhiy Storchaka19a70e72015-11-13 14:48:36 +02001654 res = _odict_add_new_node((PyODictObject *)od, key, hash);
Serhiy Storchakad5f353e2015-11-06 11:07:11 +02001655 if (res < 0) {
1656 /* Revert setting the value on the dict */
1657 PyObject *exc, *val, *tb;
1658 PyErr_Fetch(&exc, &val, &tb);
Serhiy Storchaka19a70e72015-11-13 14:48:36 +02001659 (void) _PyDict_DelItem_KnownHash(od, key, hash);
Serhiy Storchakad5f353e2015-11-06 11:07:11 +02001660 _PyErr_ChainExceptions(exc, val, tb);
1661 }
Eric Snow96c6af92015-05-29 22:21:39 -06001662 }
1663 return res;
Victor Stinnere1871952016-06-08 10:18:18 +02001664}
Eric Snow96c6af92015-05-29 22:21:39 -06001665
1666int
Serhiy Storchaka19a70e72015-11-13 14:48:36 +02001667PyODict_SetItem(PyObject *od, PyObject *key, PyObject *value)
1668{
1669 Py_hash_t hash = PyObject_Hash(key);
1670 if (hash == -1)
1671 return -1;
1672 return _PyODict_SetItem_KnownHash(od, key, value, hash);
Victor Stinnere1871952016-06-08 10:18:18 +02001673}
Serhiy Storchaka19a70e72015-11-13 14:48:36 +02001674
1675int
1676PyODict_DelItem(PyObject *od, PyObject *key)
1677{
1678 int res;
1679 Py_hash_t hash = PyObject_Hash(key);
1680 if (hash == -1)
1681 return -1;
1682 res = _odict_clear_node((PyODictObject *)od, NULL, key, hash);
Eric Snow96c6af92015-05-29 22:21:39 -06001683 if (res < 0)
1684 return -1;
Serhiy Storchaka19a70e72015-11-13 14:48:36 +02001685 return _PyDict_DelItem_KnownHash(od, key, hash);
Victor Stinnere1871952016-06-08 10:18:18 +02001686}
Eric Snow96c6af92015-05-29 22:21:39 -06001687
1688
1689/* -------------------------------------------
1690 * The OrderedDict views (keys/values/items)
1691 */
1692
1693typedef struct {
1694 PyObject_HEAD
1695 int kind;
1696 PyODictObject *di_odict;
Eric Snowb952ab42015-06-01 23:35:13 -06001697 Py_ssize_t di_size;
Eric Snow4fabf022015-06-04 00:09:56 -06001698 size_t di_state;
Eric Snow96c6af92015-05-29 22:21:39 -06001699 PyObject *di_current;
1700 PyObject *di_result; /* reusable result tuple for iteritems */
1701} odictiterobject;
1702
1703static void
1704odictiter_dealloc(odictiterobject *di)
1705{
1706 _PyObject_GC_UNTRACK(di);
Eric Snowa762af72015-06-01 22:59:08 -06001707 Py_XDECREF(di->di_odict);
Eric Snow96c6af92015-05-29 22:21:39 -06001708 Py_XDECREF(di->di_current);
Miss Islington (bot)1b372682021-12-29 21:29:03 -08001709 if ((di->kind & _odict_ITER_ITEMS) == _odict_ITER_ITEMS) {
Eric Snow96c6af92015-05-29 22:21:39 -06001710 Py_DECREF(di->di_result);
1711 }
1712 PyObject_GC_Del(di);
1713}
1714
1715static int
1716odictiter_traverse(odictiterobject *di, visitproc visit, void *arg)
1717{
1718 Py_VISIT(di->di_odict);
1719 Py_VISIT(di->di_current); /* A key could be any type, not just str. */
1720 Py_VISIT(di->di_result);
1721 return 0;
1722}
1723
Eric Snowa762af72015-06-01 22:59:08 -06001724/* In order to protect against modifications during iteration, we track
1725 * the current key instead of the current node. */
Eric Snow96c6af92015-05-29 22:21:39 -06001726static PyObject *
1727odictiter_nextkey(odictiterobject *di)
1728{
Eric Snowa762af72015-06-01 22:59:08 -06001729 PyObject *key = NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06001730 _ODictNode *node;
1731 int reversed = di->kind & _odict_ITER_REVERSED;
1732
Eric Snowa762af72015-06-01 22:59:08 -06001733 if (di->di_odict == NULL)
Eric Snow96c6af92015-05-29 22:21:39 -06001734 return NULL;
Eric Snowa762af72015-06-01 22:59:08 -06001735 if (di->di_current == NULL)
1736 goto done; /* We're already done. */
1737
Eric Snowb952ab42015-06-01 23:35:13 -06001738 /* Check for unsupported changes. */
Eric Snow4fabf022015-06-04 00:09:56 -06001739 if (di->di_odict->od_state != di->di_state) {
1740 PyErr_SetString(PyExc_RuntimeError,
1741 "OrderedDict mutated during iteration");
1742 goto done;
1743 }
Eric Snowb952ab42015-06-01 23:35:13 -06001744 if (di->di_size != PyODict_SIZE(di->di_odict)) {
1745 PyErr_SetString(PyExc_RuntimeError,
1746 "OrderedDict changed size during iteration");
1747 di->di_size = -1; /* Make this state sticky */
1748 return NULL;
1749 }
1750
Eric Snowa762af72015-06-01 22:59:08 -06001751 /* Get the key. */
Eric Snow96c6af92015-05-29 22:21:39 -06001752 node = _odict_find_node(di->di_odict, di->di_current);
1753 if (node == NULL) {
Serhiy Storchakab45b7b22015-11-04 22:05:38 +02001754 if (!PyErr_Occurred())
1755 PyErr_SetObject(PyExc_KeyError, di->di_current);
Eric Snow96c6af92015-05-29 22:21:39 -06001756 /* Must have been deleted. */
Eric Snowe3dfa9e2015-05-30 12:51:15 -06001757 Py_CLEAR(di->di_current);
Eric Snow96c6af92015-05-29 22:21:39 -06001758 return NULL;
1759 }
1760 key = di->di_current;
1761
1762 /* Advance to the next key. */
1763 node = reversed ? _odictnode_PREV(node) : _odictnode_NEXT(node);
1764 if (node == NULL) {
1765 /* Reached the end. */
1766 di->di_current = NULL;
1767 }
1768 else {
1769 di->di_current = _odictnode_KEY(node);
1770 Py_INCREF(di->di_current);
1771 }
1772
1773 return key;
Eric Snowa762af72015-06-01 22:59:08 -06001774
1775done:
1776 Py_CLEAR(di->di_odict);
1777 return key;
Eric Snow96c6af92015-05-29 22:21:39 -06001778}
1779
1780static PyObject *
1781odictiter_iternext(odictiterobject *di)
1782{
Serhiy Storchaka9c967612015-11-06 10:39:51 +02001783 PyObject *result, *value;
Eric Snow96c6af92015-05-29 22:21:39 -06001784 PyObject *key = odictiter_nextkey(di); /* new reference */
1785
1786 if (key == NULL)
1787 return NULL;
1788
1789 /* Handle the keys case. */
1790 if (! (di->kind & _odict_ITER_VALUES)) {
1791 return key;
1792 }
1793
Serhiy Storchaka9c967612015-11-06 10:39:51 +02001794 value = PyODict_GetItem((PyObject *)di->di_odict, key); /* borrowed */
1795 if (value == NULL) {
1796 if (!PyErr_Occurred())
1797 PyErr_SetObject(PyExc_KeyError, key);
Eric Snow96c6af92015-05-29 22:21:39 -06001798 Py_DECREF(key);
Serhiy Storchaka9c967612015-11-06 10:39:51 +02001799 goto done;
1800 }
1801 Py_INCREF(value);
1802
1803 /* Handle the values case. */
1804 if (!(di->kind & _odict_ITER_KEYS)) {
1805 Py_DECREF(key);
Eric Snow96c6af92015-05-29 22:21:39 -06001806 return value;
1807 }
Eric Snowa762af72015-06-01 22:59:08 -06001808
Serhiy Storchaka9c967612015-11-06 10:39:51 +02001809 /* Handle the items case. */
1810 result = di->di_result;
1811
1812 if (Py_REFCNT(result) == 1) {
1813 /* not in use so we can reuse it
1814 * (the common case during iteration) */
1815 Py_INCREF(result);
1816 Py_DECREF(PyTuple_GET_ITEM(result, 0)); /* borrowed */
1817 Py_DECREF(PyTuple_GET_ITEM(result, 1)); /* borrowed */
Brandt Bucher226a0122020-12-04 19:45:57 -08001818 // bpo-42536: The GC may have untracked this result tuple. Since we're
1819 // recycling it, make sure it's tracked again:
1820 if (!_PyObject_GC_IS_TRACKED(result)) {
1821 _PyObject_GC_TRACK(result);
1822 }
Serhiy Storchaka9c967612015-11-06 10:39:51 +02001823 }
1824 else {
1825 result = PyTuple_New(2);
1826 if (result == NULL) {
1827 Py_DECREF(key);
1828 Py_DECREF(value);
1829 goto done;
1830 }
1831 }
1832
1833 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
1834 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
1835 return result;
1836
Eric Snowa762af72015-06-01 22:59:08 -06001837done:
1838 Py_CLEAR(di->di_current);
1839 Py_CLEAR(di->di_odict);
1840 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06001841}
1842
1843/* No need for tp_clear because odictiterobject is not mutable. */
1844
1845PyDoc_STRVAR(reduce_doc, "Return state information for pickling");
1846
1847static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02001848odictiter_reduce(odictiterobject *di, PyObject *Py_UNUSED(ignored))
Eric Snow96c6af92015-05-29 22:21:39 -06001849{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001850 _Py_IDENTIFIER(iter);
Sergey Fedoseeva5259fb2018-10-20 10:20:39 +05001851 /* copy the iterator state */
1852 odictiterobject tmp = *di;
1853 Py_XINCREF(tmp.di_odict);
1854 Py_XINCREF(tmp.di_current);
Eric Snow96c6af92015-05-29 22:21:39 -06001855
1856 /* iterate the temporary into a list */
Sergey Fedoseeva5259fb2018-10-20 10:20:39 +05001857 PyObject *list = PySequence_List((PyObject*)&tmp);
1858 Py_XDECREF(tmp.di_odict);
1859 Py_XDECREF(tmp.di_current);
1860 if (list == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001861 return NULL;
1862 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001863 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Eric Snow96c6af92015-05-29 22:21:39 -06001864}
1865
1866static PyMethodDef odictiter_methods[] = {
1867 {"__reduce__", (PyCFunction)odictiter_reduce, METH_NOARGS, reduce_doc},
1868 {NULL, NULL} /* sentinel */
1869};
1870
1871PyTypeObject PyODictIter_Type = {
1872 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1873 "odict_iterator", /* tp_name */
1874 sizeof(odictiterobject), /* tp_basicsize */
1875 0, /* tp_itemsize */
1876 /* methods */
1877 (destructor)odictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001878 0, /* tp_vectorcall_offset */
Eric Snow96c6af92015-05-29 22:21:39 -06001879 0, /* tp_getattr */
1880 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001881 0, /* tp_as_async */
Eric Snow96c6af92015-05-29 22:21:39 -06001882 0, /* tp_repr */
1883 0, /* tp_as_number */
1884 0, /* tp_as_sequence */
1885 0, /* tp_as_mapping */
1886 0, /* tp_hash */
1887 0, /* tp_call */
1888 0, /* tp_str */
1889 PyObject_GenericGetAttr, /* tp_getattro */
1890 0, /* tp_setattro */
1891 0, /* tp_as_buffer */
1892 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1893 0, /* tp_doc */
1894 (traverseproc)odictiter_traverse, /* tp_traverse */
1895 0, /* tp_clear */
1896 0, /* tp_richcompare */
1897 0, /* tp_weaklistoffset */
1898 PyObject_SelfIter, /* tp_iter */
1899 (iternextfunc)odictiter_iternext, /* tp_iternext */
1900 odictiter_methods, /* tp_methods */
1901 0,
1902};
1903
1904static PyObject *
1905odictiter_new(PyODictObject *od, int kind)
1906{
1907 odictiterobject *di;
1908 _ODictNode *node;
1909 int reversed = kind & _odict_ITER_REVERSED;
1910
1911 di = PyObject_GC_New(odictiterobject, &PyODictIter_Type);
1912 if (di == NULL)
1913 return NULL;
1914
Miss Islington (bot)1b372682021-12-29 21:29:03 -08001915 if ((kind & _odict_ITER_ITEMS) == _odict_ITER_ITEMS) {
Eric Snow96c6af92015-05-29 22:21:39 -06001916 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
1917 if (di->di_result == NULL) {
1918 Py_DECREF(di);
1919 return NULL;
1920 }
1921 }
Miss Islington (bot)1b372682021-12-29 21:29:03 -08001922 else {
Eric Snow96c6af92015-05-29 22:21:39 -06001923 di->di_result = NULL;
Miss Islington (bot)1b372682021-12-29 21:29:03 -08001924 }
Eric Snow96c6af92015-05-29 22:21:39 -06001925
1926 di->kind = kind;
1927 node = reversed ? _odict_LAST(od) : _odict_FIRST(od);
1928 di->di_current = node ? _odictnode_KEY(node) : NULL;
1929 Py_XINCREF(di->di_current);
Eric Snowb952ab42015-06-01 23:35:13 -06001930 di->di_size = PyODict_SIZE(od);
Eric Snow4fabf022015-06-04 00:09:56 -06001931 di->di_state = od->od_state;
Eric Snow96c6af92015-05-29 22:21:39 -06001932 di->di_odict = od;
1933 Py_INCREF(od);
1934
1935 _PyObject_GC_TRACK(di);
1936 return (PyObject *)di;
1937}
1938
1939/* keys() */
1940
1941static PyObject *
1942odictkeys_iter(_PyDictViewObject *dv)
1943{
1944 if (dv->dv_dict == NULL) {
1945 Py_RETURN_NONE;
1946 }
1947 return odictiter_new((PyODictObject *)dv->dv_dict,
1948 _odict_ITER_KEYS);
1949}
1950
1951static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301952odictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
Eric Snow96c6af92015-05-29 22:21:39 -06001953{
1954 if (dv->dv_dict == NULL) {
1955 Py_RETURN_NONE;
1956 }
1957 return odictiter_new((PyODictObject *)dv->dv_dict,
1958 _odict_ITER_KEYS|_odict_ITER_REVERSED);
1959}
1960
1961static PyMethodDef odictkeys_methods[] = {
1962 {"__reversed__", (PyCFunction)odictkeys_reversed, METH_NOARGS, NULL},
1963 {NULL, NULL} /* sentinel */
1964};
1965
1966PyTypeObject PyODictKeys_Type = {
1967 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1968 "odict_keys", /* tp_name */
1969 0, /* tp_basicsize */
1970 0, /* tp_itemsize */
1971 0, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001972 0, /* tp_vectorcall_offset */
Eric Snow96c6af92015-05-29 22:21:39 -06001973 0, /* tp_getattr */
1974 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001975 0, /* tp_as_async */
Eric Snow96c6af92015-05-29 22:21:39 -06001976 0, /* tp_repr */
1977 0, /* tp_as_number */
1978 0, /* tp_as_sequence */
1979 0, /* tp_as_mapping */
1980 0, /* tp_hash */
1981 0, /* tp_call */
1982 0, /* tp_str */
1983 0, /* tp_getattro */
1984 0, /* tp_setattro */
1985 0, /* tp_as_buffer */
1986 0, /* tp_flags */
1987 0, /* tp_doc */
1988 0, /* tp_traverse */
1989 0, /* tp_clear */
1990 0, /* tp_richcompare */
1991 0, /* tp_weaklistoffset */
1992 (getiterfunc)odictkeys_iter, /* tp_iter */
1993 0, /* tp_iternext */
1994 odictkeys_methods, /* tp_methods */
1995 0, /* tp_members */
1996 0, /* tp_getset */
1997 &PyDictKeys_Type, /* tp_base */
1998};
1999
2000static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302001odictkeys_new(PyObject *od, PyObject *Py_UNUSED(ignored))
Eric Snow96c6af92015-05-29 22:21:39 -06002002{
2003 return _PyDictView_New(od, &PyODictKeys_Type);
2004}
2005
2006/* items() */
2007
2008static PyObject *
2009odictitems_iter(_PyDictViewObject *dv)
2010{
2011 if (dv->dv_dict == NULL) {
2012 Py_RETURN_NONE;
2013 }
2014 return odictiter_new((PyODictObject *)dv->dv_dict,
2015 _odict_ITER_KEYS|_odict_ITER_VALUES);
2016}
2017
2018static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302019odictitems_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
Eric Snow96c6af92015-05-29 22:21:39 -06002020{
2021 if (dv->dv_dict == NULL) {
2022 Py_RETURN_NONE;
2023 }
2024 return odictiter_new((PyODictObject *)dv->dv_dict,
2025 _odict_ITER_KEYS|_odict_ITER_VALUES|_odict_ITER_REVERSED);
2026}
2027
2028static PyMethodDef odictitems_methods[] = {
2029 {"__reversed__", (PyCFunction)odictitems_reversed, METH_NOARGS, NULL},
2030 {NULL, NULL} /* sentinel */
2031};
2032
2033PyTypeObject PyODictItems_Type = {
2034 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2035 "odict_items", /* tp_name */
2036 0, /* tp_basicsize */
2037 0, /* tp_itemsize */
2038 0, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002039 0, /* tp_vectorcall_offset */
Eric Snow96c6af92015-05-29 22:21:39 -06002040 0, /* tp_getattr */
2041 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002042 0, /* tp_as_async */
Eric Snow96c6af92015-05-29 22:21:39 -06002043 0, /* tp_repr */
2044 0, /* tp_as_number */
2045 0, /* tp_as_sequence */
2046 0, /* tp_as_mapping */
2047 0, /* tp_hash */
2048 0, /* tp_call */
2049 0, /* tp_str */
2050 0, /* tp_getattro */
2051 0, /* tp_setattro */
2052 0, /* tp_as_buffer */
2053 0, /* tp_flags */
2054 0, /* tp_doc */
2055 0, /* tp_traverse */
2056 0, /* tp_clear */
2057 0, /* tp_richcompare */
2058 0, /* tp_weaklistoffset */
2059 (getiterfunc)odictitems_iter, /* tp_iter */
2060 0, /* tp_iternext */
2061 odictitems_methods, /* tp_methods */
2062 0, /* tp_members */
2063 0, /* tp_getset */
2064 &PyDictItems_Type, /* tp_base */
2065};
2066
2067static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302068odictitems_new(PyObject *od, PyObject *Py_UNUSED(ignored))
Eric Snow96c6af92015-05-29 22:21:39 -06002069{
2070 return _PyDictView_New(od, &PyODictItems_Type);
2071}
2072
2073/* values() */
2074
2075static PyObject *
2076odictvalues_iter(_PyDictViewObject *dv)
2077{
2078 if (dv->dv_dict == NULL) {
2079 Py_RETURN_NONE;
2080 }
2081 return odictiter_new((PyODictObject *)dv->dv_dict,
2082 _odict_ITER_VALUES);
2083}
2084
2085static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302086odictvalues_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
Eric Snow96c6af92015-05-29 22:21:39 -06002087{
2088 if (dv->dv_dict == NULL) {
2089 Py_RETURN_NONE;
2090 }
2091 return odictiter_new((PyODictObject *)dv->dv_dict,
2092 _odict_ITER_VALUES|_odict_ITER_REVERSED);
2093}
2094
2095static PyMethodDef odictvalues_methods[] = {
2096 {"__reversed__", (PyCFunction)odictvalues_reversed, METH_NOARGS, NULL},
2097 {NULL, NULL} /* sentinel */
2098};
2099
2100PyTypeObject PyODictValues_Type = {
2101 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2102 "odict_values", /* tp_name */
2103 0, /* tp_basicsize */
2104 0, /* tp_itemsize */
2105 0, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002106 0, /* tp_vectorcall_offset */
Eric Snow96c6af92015-05-29 22:21:39 -06002107 0, /* tp_getattr */
2108 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002109 0, /* tp_as_async */
Eric Snow96c6af92015-05-29 22:21:39 -06002110 0, /* tp_repr */
2111 0, /* tp_as_number */
2112 0, /* tp_as_sequence */
2113 0, /* tp_as_mapping */
2114 0, /* tp_hash */
2115 0, /* tp_call */
2116 0, /* tp_str */
2117 0, /* tp_getattro */
2118 0, /* tp_setattro */
2119 0, /* tp_as_buffer */
2120 0, /* tp_flags */
2121 0, /* tp_doc */
2122 0, /* tp_traverse */
2123 0, /* tp_clear */
2124 0, /* tp_richcompare */
2125 0, /* tp_weaklistoffset */
2126 (getiterfunc)odictvalues_iter, /* tp_iter */
2127 0, /* tp_iternext */
2128 odictvalues_methods, /* tp_methods */
2129 0, /* tp_members */
2130 0, /* tp_getset */
2131 &PyDictValues_Type, /* tp_base */
2132};
2133
2134static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302135odictvalues_new(PyObject *od, PyObject *Py_UNUSED(ignored))
Eric Snow96c6af92015-05-29 22:21:39 -06002136{
2137 return _PyDictView_New(od, &PyODictValues_Type);
2138}
2139
2140
2141/* ----------------------------------------------
Martin Panter536d70e2017-01-14 08:23:08 +00002142 MutableMapping implementations
Eric Snow96c6af92015-05-29 22:21:39 -06002143
2144Mapping:
2145
2146============ ===========
2147method uses
2148============ ===========
2149__contains__ __getitem__
2150__eq__ items
2151__getitem__ +
2152__iter__ +
2153__len__ +
2154__ne__ __eq__
2155get __getitem__
2156items ItemsView
2157keys KeysView
2158values ValuesView
2159============ ===========
2160
2161ItemsView uses __len__, __iter__, and __getitem__.
2162KeysView uses __len__, __iter__, and __contains__.
2163ValuesView uses __len__, __iter__, and __getitem__.
2164
2165MutableMapping:
2166
2167============ ===========
2168method uses
2169============ ===========
2170__delitem__ +
2171__setitem__ +
2172clear popitem
2173pop __getitem__
2174 __delitem__
2175popitem __iter__
2176 _getitem__
2177 __delitem__
2178setdefault __getitem__
2179 __setitem__
2180update __setitem__
2181============ ===========
2182*/
2183
2184static int
2185mutablemapping_add_pairs(PyObject *self, PyObject *pairs)
2186{
2187 PyObject *pair, *iterator, *unexpected;
2188 int res = 0;
2189
2190 iterator = PyObject_GetIter(pairs);
2191 if (iterator == NULL)
2192 return -1;
2193 PyErr_Clear();
2194
2195 while ((pair = PyIter_Next(iterator)) != NULL) {
2196 /* could be more efficient (see UNPACK_SEQUENCE in ceval.c) */
Eric Snowc5e59602015-05-30 11:28:56 -06002197 PyObject *key = NULL, *value = NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002198 PyObject *pair_iterator = PyObject_GetIter(pair);
Eric Snowc5e59602015-05-30 11:28:56 -06002199 if (pair_iterator == NULL)
2200 goto Done;
Eric Snow96c6af92015-05-29 22:21:39 -06002201
2202 key = PyIter_Next(pair_iterator);
2203 if (key == NULL) {
2204 if (!PyErr_Occurred())
2205 PyErr_SetString(PyExc_ValueError,
2206 "need more than 0 values to unpack");
2207 goto Done;
2208 }
2209
2210 value = PyIter_Next(pair_iterator);
2211 if (value == NULL) {
2212 if (!PyErr_Occurred())
2213 PyErr_SetString(PyExc_ValueError,
2214 "need more than 1 value to unpack");
2215 goto Done;
2216 }
2217
2218 unexpected = PyIter_Next(pair_iterator);
2219 if (unexpected != NULL) {
2220 Py_DECREF(unexpected);
2221 PyErr_SetString(PyExc_ValueError,
2222 "too many values to unpack (expected 2)");
2223 goto Done;
2224 }
2225 else if (PyErr_Occurred())
2226 goto Done;
2227
2228 res = PyObject_SetItem(self, key, value);
2229
2230Done:
2231 Py_DECREF(pair);
Eric Snowc5e59602015-05-30 11:28:56 -06002232 Py_XDECREF(pair_iterator);
Eric Snow96c6af92015-05-29 22:21:39 -06002233 Py_XDECREF(key);
2234 Py_XDECREF(value);
2235 if (PyErr_Occurred())
2236 break;
2237 }
2238 Py_DECREF(iterator);
2239
2240 if (res < 0 || PyErr_Occurred() != NULL)
2241 return -1;
2242 else
2243 return 0;
2244}
2245
Brandt Bucher6d674a12020-03-13 09:06:04 -07002246static int
2247mutablemapping_update_arg(PyObject *self, PyObject *arg)
2248{
2249 int res = 0;
2250 if (PyDict_CheckExact(arg)) {
2251 PyObject *items = PyDict_Items(arg);
2252 if (items == NULL) {
2253 return -1;
2254 }
2255 res = mutablemapping_add_pairs(self, items);
2256 Py_DECREF(items);
2257 return res;
2258 }
2259 _Py_IDENTIFIER(keys);
2260 PyObject *func;
2261 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2262 return -1;
2263 }
2264 if (func != NULL) {
2265 PyObject *keys = _PyObject_CallNoArg(func);
2266 Py_DECREF(func);
2267 if (keys == NULL) {
2268 return -1;
2269 }
2270 PyObject *iterator = PyObject_GetIter(keys);
2271 Py_DECREF(keys);
2272 if (iterator == NULL) {
2273 return -1;
2274 }
2275 PyObject *key;
2276 while (res == 0 && (key = PyIter_Next(iterator))) {
2277 PyObject *value = PyObject_GetItem(arg, key);
2278 if (value != NULL) {
2279 res = PyObject_SetItem(self, key, value);
2280 Py_DECREF(value);
2281 }
2282 else {
2283 res = -1;
2284 }
2285 Py_DECREF(key);
2286 }
2287 Py_DECREF(iterator);
2288 if (res != 0 || PyErr_Occurred()) {
2289 return -1;
2290 }
2291 return 0;
2292 }
2293 if (_PyObject_LookupAttrId(arg, &PyId_items, &func) < 0) {
2294 return -1;
2295 }
2296 if (func != NULL) {
2297 PyObject *items = _PyObject_CallNoArg(func);
2298 Py_DECREF(func);
2299 if (items == NULL) {
2300 return -1;
2301 }
2302 res = mutablemapping_add_pairs(self, items);
2303 Py_DECREF(items);
2304 return res;
2305 }
2306 res = mutablemapping_add_pairs(self, arg);
2307 return res;
2308}
2309
Eric Snow96c6af92015-05-29 22:21:39 -06002310static PyObject *
2311mutablemapping_update(PyObject *self, PyObject *args, PyObject *kwargs)
2312{
Brandt Bucher6d674a12020-03-13 09:06:04 -07002313 int res;
Eric Snow96c6af92015-05-29 22:21:39 -06002314 /* first handle args, if any */
Serhiy Storchaka8003baf2015-10-18 09:53:17 +03002315 assert(args == NULL || PyTuple_Check(args));
Brandt Bucher6d674a12020-03-13 09:06:04 -07002316 Py_ssize_t len = (args != NULL) ? PyTuple_GET_SIZE(args) : 0;
Benjamin Peterson99e96f22015-06-06 23:20:32 -05002317 if (len > 1) {
Serhiy Storchakad53fe5f2019-03-13 22:59:55 +02002318 const char *msg = "update() takes at most 1 positional argument (%zd given)";
Eric Snow96c6af92015-05-29 22:21:39 -06002319 PyErr_Format(PyExc_TypeError, msg, len);
2320 return NULL;
2321 }
Eric Snow96c6af92015-05-29 22:21:39 -06002322
Serhiy Storchaka8003baf2015-10-18 09:53:17 +03002323 if (len) {
Benjamin Peterson0718de92015-06-07 00:00:42 -05002324 PyObject *other = PyTuple_GET_ITEM(args, 0); /* borrowed reference */
Serhiy Storchaka8003baf2015-10-18 09:53:17 +03002325 assert(other != NULL);
Benjamin Peterson0718de92015-06-07 00:00:42 -05002326 Py_INCREF(other);
Brandt Bucher6d674a12020-03-13 09:06:04 -07002327 res = mutablemapping_update_arg(self, other);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002328 Py_DECREF(other);
Brandt Bucher6d674a12020-03-13 09:06:04 -07002329 if (res < 0) {
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002330 return NULL;
Brandt Bucher6d674a12020-03-13 09:06:04 -07002331 }
Benjamin Peterson0718de92015-06-07 00:00:42 -05002332 }
2333
2334 /* now handle kwargs */
Serhiy Storchaka8003baf2015-10-18 09:53:17 +03002335 assert(kwargs == NULL || PyDict_Check(kwargs));
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002336 if (kwargs != NULL && PyDict_GET_SIZE(kwargs)) {
Serhiy Storchaka8003baf2015-10-18 09:53:17 +03002337 PyObject *items = PyDict_Items(kwargs);
Eric Snow96c6af92015-05-29 22:21:39 -06002338 if (items == NULL)
2339 return NULL;
2340 res = mutablemapping_add_pairs(self, items);
2341 Py_DECREF(items);
2342 if (res == -1)
2343 return NULL;
2344 }
Eric Snow96c6af92015-05-29 22:21:39 -06002345
2346 Py_RETURN_NONE;
2347}