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