odictobject.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243
  1. /* Ordered Dictionary object implementation.
  2. This implementation is necessarily explicitly equivalent to the pure Python
  3. OrderedDict class in Lib/collections/__init__.py. The strategy there
  4. involves using a doubly-linked-list to capture the order. We keep to that
  5. strategy, using a lower-level linked-list.
  6. About the Linked-List
  7. =====================
  8. For the linked list we use a basic doubly-linked-list. Using a circularly-
  9. linked-list does have some benefits, but they don't apply so much here
  10. since OrderedDict is focused on the ends of the list (for the most part).
  11. Furthermore, there are some features of generic linked-lists that we simply
  12. don't need for OrderedDict. Thus a simple custom implementation meets our
  13. needs. Alternatives to our simple approach include the QCIRCLE_*
  14. macros from BSD's queue.h, and the linux's list.h.
  15. Getting O(1) Node Lookup
  16. ------------------------
  17. One invariant of Python's OrderedDict is that it preserves time complexity
  18. of dict's methods, particularly the O(1) operations. Simply adding a
  19. linked-list on top of dict is not sufficient here; operations for nodes in
  20. the middle of the linked-list implicitly require finding the node first.
  21. With a simple linked-list like we're using, that is an O(n) operation.
  22. Consequently, methods like __delitem__() would change from O(1) to O(n),
  23. which is unacceptable.
  24. In order to preserve O(1) performance for node removal (finding nodes), we
  25. must do better than just looping through the linked-list. Here are options
  26. we've considered:
  27. 1. use a second dict to map keys to nodes (a la the pure Python version).
  28. 2. keep a simple hash table mirroring the order of dict's, mapping each key
  29. to the corresponding node in the linked-list.
  30. 3. use a version of shared keys (split dict) that allows non-unicode keys.
  31. 4. have the value stored for each key be a (value, node) pair, and adjust
  32. __getitem__(), get(), etc. accordingly.
  33. The approach with the least performance impact (time and space) is #2,
  34. mirroring the key order of dict's dk_entries with an array of node pointers.
  35. While _Py_dict_lookup() does not give us the index into the array,
  36. we make use of pointer arithmetic to get that index. An alternative would
  37. be to refactor _Py_dict_lookup() to provide the index, explicitly exposing
  38. the implementation detail. We could even just use a custom lookup function
  39. for OrderedDict that facilitates our need. However, both approaches are
  40. significantly more complicated than just using pointer arithmetic.
  41. The catch with mirroring the hash table ordering is that we have to keep
  42. the ordering in sync through any dict resizes. However, that order only
  43. matters during node lookup. We can simply defer any potential resizing
  44. until we need to do a lookup.
  45. Linked-List Nodes
  46. -----------------
  47. The current implementation stores a pointer to the associated key only.
  48. One alternative would be to store a pointer to the PyDictKeyEntry instead.
  49. This would save one pointer de-reference per item, which is nice during
  50. calls to values() and items(). However, it adds unnecessary overhead
  51. otherwise, so we stick with just the key.
  52. Linked-List API
  53. ---------------
  54. As noted, the linked-list implemented here does not have all the bells and
  55. whistles. However, we recognize that the implementation may need to
  56. change to accommodate performance improvements or extra functionality. To
  57. that end, we use a simple API to interact with the linked-list. Here's a
  58. summary of the methods/macros:
  59. Node info:
  60. * _odictnode_KEY(node)
  61. * _odictnode_VALUE(od, node)
  62. * _odictnode_PREV(node)
  63. * _odictnode_NEXT(node)
  64. Linked-List info:
  65. * _odict_FIRST(od)
  66. * _odict_LAST(od)
  67. * _odict_EMPTY(od)
  68. * _odict_FOREACH(od, node) - used in place of `for (node=...)`
  69. For adding nodes:
  70. * _odict_add_head(od, node)
  71. * _odict_add_tail(od, node)
  72. * _odict_add_new_node(od, key, hash)
  73. For removing nodes:
  74. * _odict_clear_node(od, node, key, hash)
  75. * _odict_clear_nodes(od, clear_each)
  76. Others:
  77. * _odict_find_node_hash(od, key, hash)
  78. * _odict_find_node(od, key)
  79. * _odict_keys_equal(od1, od2)
  80. And here's a look at how the linked-list relates to the OrderedDict API:
  81. ============ === === ==== ==== ==== === ==== ===== ==== ==== === ==== === ===
  82. method key val prev next mem 1st last empty iter find add rmv clr keq
  83. ============ === === ==== ==== ==== === ==== ===== ==== ==== === ==== === ===
  84. __del__ ~ X
  85. __delitem__ free ~ node
  86. __eq__ ~ X
  87. __iter__ X X
  88. __new__ X X
  89. __reduce__ X ~ X
  90. __repr__ X X X
  91. __reversed__ X X
  92. __setitem__ key
  93. __sizeof__ size X
  94. clear ~ ~ X
  95. copy X X X
  96. items X X X
  97. keys X X
  98. move_to_end X X X ~ h/t key
  99. pop free key
  100. popitem X X free X X node
  101. setdefault ~ ? ~
  102. values X X
  103. ============ === === ==== ==== ==== === ==== ===== ==== ==== === ==== === ===
  104. __delitem__ is the only method that directly relies on finding an arbitrary
  105. node in the linked-list. Everything else is iteration or relates to the
  106. ends of the linked-list.
  107. Situation that Endangers Consistency
  108. ------------------------------------
  109. Using a raw linked-list for OrderedDict exposes a key situation that can
  110. cause problems. If a node is stored in a variable, there is a chance that
  111. the node may have been deallocated before the variable gets used, thus
  112. potentially leading to a segmentation fault. A key place where this shows
  113. up is during iteration through the linked list (via _odict_FOREACH or
  114. otherwise).
  115. A number of solutions are available to resolve this situation:
  116. * defer looking up the node until as late as possible and certainly after
  117. any code that could possibly result in a deletion;
  118. * if the node is needed both before and after a point where the node might
  119. be removed, do a check before using the node at the "after" location to
  120. see if the node is still valid;
  121. * like the last one, but simply pull the node again to ensure it's right;
  122. * keep the key in the variable instead of the node and then look up the
  123. node using the key at the point where the node is needed (this is what
  124. we do for the iterators).
  125. Another related problem, preserving consistent ordering during iteration,
  126. is described below. That one is not exclusive to using linked-lists.
  127. Challenges from Subclassing dict
  128. ================================
  129. OrderedDict subclasses dict, which is an unusual relationship between two
  130. builtin types (other than the base object type). Doing so results in
  131. some complication and deserves further explanation. There are two things
  132. to consider here. First, in what circumstances or with what adjustments
  133. can OrderedDict be used as a drop-in replacement for dict (at the C level)?
  134. Second, how can the OrderedDict implementation leverage the dict
  135. implementation effectively without introducing unnecessary coupling or
  136. inefficiencies?
  137. This second point is reflected here and in the implementation, so the
  138. further focus is on the first point. It is worth noting that for
  139. overridden methods, the dict implementation is deferred to as much as
  140. possible. Furthermore, coupling is limited to as little as is reasonable.
  141. Concrete API Compatibility
  142. --------------------------
  143. Use of the concrete C-API for dict (PyDict_*) with OrderedDict is
  144. problematic. (See http://bugs.python.org/issue10977.) The concrete API
  145. has a number of hard-coded assumptions tied to the dict implementation.
  146. This is, in part, due to performance reasons, which is understandable
  147. given the part dict plays in Python.
  148. Any attempt to replace dict with OrderedDict for any role in the
  149. interpreter (e.g. **kwds) faces a challenge. Such any effort must
  150. recognize that the instances in affected locations currently interact with
  151. the concrete API.
  152. Here are some ways to address this challenge:
  153. 1. Change the relevant usage of the concrete API in CPython and add
  154. PyDict_CheckExact() calls to each of the concrete API functions.
  155. 2. Adjust the relevant concrete API functions to explicitly accommodate
  156. OrderedDict.
  157. 3. As with #1, add the checks, but improve the abstract API with smart fast
  158. paths for dict and OrderedDict, and refactor CPython to use the abstract
  159. API. Improvements to the abstract API would be valuable regardless.
  160. Adding the checks to the concrete API would help make any interpreter
  161. switch to OrderedDict less painful for extension modules. However, this
  162. won't work. The equivalent C API call to `dict.__setitem__(obj, k, v)`
  163. is 'PyDict_SetItem(obj, k, v)`. This illustrates how subclasses in C call
  164. the base class's methods, since there is no equivalent of super() in the
  165. C API. Calling into Python for parent class API would work, but some
  166. extension modules already rely on this feature of the concrete API.
  167. For reference, here is a breakdown of some of the dict concrete API:
  168. ========================== ============= =======================
  169. concrete API uses abstract API
  170. ========================== ============= =======================
  171. PyDict_Check PyMapping_Check
  172. (PyDict_CheckExact) -
  173. (PyDict_New) -
  174. (PyDictProxy_New) -
  175. PyDict_Clear -
  176. PyDict_Contains PySequence_Contains
  177. PyDict_Copy -
  178. PyDict_SetItem PyObject_SetItem
  179. PyDict_SetItemString PyMapping_SetItemString
  180. PyDict_DelItem PyMapping_DelItem
  181. PyDict_DelItemString PyMapping_DelItemString
  182. PyDict_GetItem -
  183. PyDict_GetItemWithError PyObject_GetItem
  184. _PyDict_GetItemIdWithError -
  185. PyDict_GetItemString PyMapping_GetItemString
  186. PyDict_Items PyMapping_Items
  187. PyDict_Keys PyMapping_Keys
  188. PyDict_Values PyMapping_Values
  189. PyDict_Size PyMapping_Size
  190. PyMapping_Length
  191. PyDict_Next PyIter_Next
  192. _PyDict_Next -
  193. PyDict_Merge -
  194. PyDict_Update -
  195. PyDict_MergeFromSeq2 -
  196. PyDict_ClearFreeList -
  197. - PyMapping_HasKeyString
  198. - PyMapping_HasKey
  199. ========================== ============= =======================
  200. The dict Interface Relative to OrderedDict
  201. ==========================================
  202. Since OrderedDict subclasses dict, understanding the various methods and
  203. attributes of dict is important for implementing OrderedDict.
  204. Relevant Type Slots
  205. -------------------
  206. ================= ================ =================== ================
  207. slot attribute object dict
  208. ================= ================ =================== ================
  209. tp_dealloc - object_dealloc dict_dealloc
  210. tp_repr __repr__ object_repr dict_repr
  211. sq_contains __contains__ - dict_contains
  212. mp_length __len__ - dict_length
  213. mp_subscript __getitem__ - dict_subscript
  214. mp_ass_subscript __setitem__ - dict_ass_sub
  215. __delitem__
  216. tp_hash __hash__ _Py_HashPointer ..._HashNotImpl
  217. tp_str __str__ object_str -
  218. tp_getattro __getattribute__ ..._GenericGetAttr (repeated)
  219. __getattr__
  220. tp_setattro __setattr__ ..._GenericSetAttr (disabled)
  221. tp_doc __doc__ (literal) dictionary_doc
  222. tp_traverse - - dict_traverse
  223. tp_clear - - dict_tp_clear
  224. tp_richcompare __eq__ object_richcompare dict_richcompare
  225. __ne__
  226. tp_weaklistoffset (__weakref__) - -
  227. tp_iter __iter__ - dict_iter
  228. tp_dictoffset (__dict__) - -
  229. tp_init __init__ object_init dict_init
  230. tp_alloc - PyType_GenericAlloc (repeated)
  231. tp_new __new__ object_new dict_new
  232. tp_free - PyObject_Del PyObject_GC_Del
  233. ================= ================ =================== ================
  234. Relevant Methods
  235. ----------------
  236. ================ =================== ===============
  237. method object dict
  238. ================ =================== ===============
  239. __reduce__ object_reduce -
  240. __sizeof__ object_sizeof dict_sizeof
  241. clear - dict_clear
  242. copy - dict_copy
  243. fromkeys - dict_fromkeys
  244. get - dict_get
  245. items - dictitems_new
  246. keys - dictkeys_new
  247. pop - dict_pop
  248. popitem - dict_popitem
  249. setdefault - dict_setdefault
  250. update - dict_update
  251. values - dictvalues_new
  252. ================ =================== ===============
  253. Pure Python OrderedDict
  254. =======================
  255. As already noted, compatibility with the pure Python OrderedDict
  256. implementation is a key goal of this C implementation. To further that
  257. goal, here's a summary of how OrderedDict-specific methods are implemented
  258. in collections/__init__.py. Also provided is an indication of which
  259. methods directly mutate or iterate the object, as well as any relationship
  260. with the underlying linked-list.
  261. ============= ============== == ================ === === ====
  262. method impl used ll uses inq mut iter
  263. ============= ============== == ================ === === ====
  264. __contains__ dict - - X
  265. __delitem__ OrderedDict Y dict.__delitem__ X
  266. __eq__ OrderedDict N OrderedDict ~
  267. dict.__eq__
  268. __iter__
  269. __getitem__ dict - - X
  270. __iter__ OrderedDict Y - X
  271. __init__ OrderedDict N update
  272. __len__ dict - - X
  273. __ne__ MutableMapping - __eq__ ~
  274. __reduce__ OrderedDict N OrderedDict ~
  275. __iter__
  276. __getitem__
  277. __repr__ OrderedDict N __class__ ~
  278. items
  279. __reversed__ OrderedDict Y - X
  280. __setitem__ OrderedDict Y __contains__ X
  281. dict.__setitem__
  282. __sizeof__ OrderedDict Y __len__ ~
  283. __dict__
  284. clear OrderedDict Y dict.clear X
  285. copy OrderedDict N __class__
  286. __init__
  287. fromkeys OrderedDict N __setitem__
  288. get dict - - ~
  289. items MutableMapping - ItemsView X
  290. keys MutableMapping - KeysView X
  291. move_to_end OrderedDict Y - X
  292. pop OrderedDict N __contains__ X
  293. __getitem__
  294. __delitem__
  295. popitem OrderedDict Y dict.pop X
  296. setdefault OrderedDict N __contains__ ~
  297. __getitem__
  298. __setitem__
  299. update MutableMapping - __setitem__ ~
  300. values MutableMapping - ValuesView X
  301. ============= ============== == ================ === === ====
  302. __reversed__ and move_to_end are both exclusive to OrderedDict.
  303. C OrderedDict Implementation
  304. ============================
  305. ================= ================
  306. slot impl
  307. ================= ================
  308. tp_dealloc odict_dealloc
  309. tp_repr odict_repr
  310. mp_ass_subscript odict_ass_sub
  311. tp_doc odict_doc
  312. tp_traverse odict_traverse
  313. tp_clear odict_tp_clear
  314. tp_richcompare odict_richcompare
  315. tp_weaklistoffset (offset)
  316. tp_iter odict_iter
  317. tp_dictoffset (offset)
  318. tp_init odict_init
  319. tp_alloc (repeated)
  320. ================= ================
  321. ================= ================
  322. method impl
  323. ================= ================
  324. __reduce__ odict_reduce
  325. __sizeof__ odict_sizeof
  326. clear odict_clear
  327. copy odict_copy
  328. fromkeys odict_fromkeys
  329. items odictitems_new
  330. keys odictkeys_new
  331. pop odict_pop
  332. popitem odict_popitem
  333. setdefault odict_setdefault
  334. update odict_update
  335. values odictvalues_new
  336. ================= ================
  337. Inherited unchanged from object/dict:
  338. ================ ==========================
  339. method type field
  340. ================ ==========================
  341. - tp_free
  342. __contains__ tp_as_sequence.sq_contains
  343. __getattr__ tp_getattro
  344. __getattribute__ tp_getattro
  345. __getitem__ tp_as_mapping.mp_subscript
  346. __hash__ tp_hash
  347. __len__ tp_as_mapping.mp_length
  348. __setattr__ tp_setattro
  349. __str__ tp_str
  350. get -
  351. ================ ==========================
  352. Other Challenges
  353. ================
  354. Preserving Ordering During Iteration
  355. ------------------------------------
  356. During iteration through an OrderedDict, it is possible that items could
  357. get added, removed, or reordered. For a linked-list implementation, as
  358. with some other implementations, that situation may lead to undefined
  359. behavior. The documentation for dict mentions this in the `iter()` section
  360. of http://docs.python.org/3.4/library/stdtypes.html#dictionary-view-objects.
  361. In this implementation we follow dict's lead (as does the pure Python
  362. implementation) for __iter__(), keys(), values(), and items().
  363. For internal iteration (using _odict_FOREACH or not), there is still the
  364. risk that not all nodes that we expect to be seen in the loop actually get
  365. seen. Thus, we are careful in each of those places to ensure that they
  366. are. This comes, of course, at a small price at each location. The
  367. solutions are much the same as those detailed in the `Situation that
  368. Endangers Consistency` section above.
  369. Potential Optimizations
  370. =======================
  371. * Allocate the nodes as a block via od_fast_nodes instead of individually.
  372. - Set node->key to NULL to indicate the node is not-in-use.
  373. - Add _odict_EXISTS()?
  374. - How to maintain consistency across resizes? Existing node pointers
  375. would be invalidated after a resize, which is particularly problematic
  376. for the iterators.
  377. * Use a more stream-lined implementation of update() and, likely indirectly,
  378. __init__().
  379. */
  380. /* TODO
  381. sooner:
  382. - reentrancy (make sure everything is at a thread-safe state when calling
  383. into Python). I've already checked this multiple times, but want to
  384. make one more pass.
  385. - add unit tests for reentrancy?
  386. later:
  387. - make the dict views support the full set API (the pure Python impl does)
  388. - implement a fuller MutableMapping API in C?
  389. - move the MutableMapping implementation to abstract.c?
  390. - optimize mutablemapping_update
  391. - use PyObject_Malloc (small object allocator) for odict nodes?
  392. - support subclasses better (e.g. in odict_richcompare)
  393. */
  394. #include "Python.h"
  395. #include "pycore_call.h" // _PyObject_CallNoArgs()
  396. #include "pycore_object.h" // _PyObject_GC_UNTRACK()
  397. #include "pycore_dict.h" // _Py_dict_lookup()
  398. #include <stddef.h> // offsetof()
  399. #include "clinic/odictobject.c.h"
  400. /*[clinic input]
  401. class OrderedDict "PyODictObject *" "&PyODict_Type"
  402. [clinic start generated code]*/
  403. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ca0641cf6143d4af]*/
  404. typedef struct _odictnode _ODictNode;
  405. /* PyODictObject */
  406. struct _odictobject {
  407. PyDictObject od_dict; /* the underlying dict */
  408. _ODictNode *od_first; /* first node in the linked list, if any */
  409. _ODictNode *od_last; /* last node in the linked list, if any */
  410. /* od_fast_nodes, od_fast_nodes_size and od_resize_sentinel are managed
  411. * by _odict_resize().
  412. * Note that we rely on implementation details of dict for both. */
  413. _ODictNode **od_fast_nodes; /* hash table that mirrors the dict table */
  414. Py_ssize_t od_fast_nodes_size;
  415. void *od_resize_sentinel; /* changes if odict should be resized */
  416. size_t od_state; /* incremented whenever the LL changes */
  417. PyObject *od_inst_dict; /* OrderedDict().__dict__ */
  418. PyObject *od_weakreflist; /* holds weakrefs to the odict */
  419. };
  420. /* ----------------------------------------------
  421. * odict keys (a simple doubly-linked list)
  422. */
  423. struct _odictnode {
  424. PyObject *key;
  425. Py_hash_t hash;
  426. _ODictNode *next;
  427. _ODictNode *prev;
  428. };
  429. #define _odictnode_KEY(node) \
  430. (node->key)
  431. #define _odictnode_HASH(node) \
  432. (node->hash)
  433. /* borrowed reference */
  434. #define _odictnode_VALUE(node, od) \
  435. PyODict_GetItemWithError((PyObject *)od, _odictnode_KEY(node))
  436. #define _odictnode_PREV(node) (node->prev)
  437. #define _odictnode_NEXT(node) (node->next)
  438. #define _odict_FIRST(od) (((PyODictObject *)od)->od_first)
  439. #define _odict_LAST(od) (((PyODictObject *)od)->od_last)
  440. #define _odict_EMPTY(od) (_odict_FIRST(od) == NULL)
  441. #define _odict_FOREACH(od, node) \
  442. for (node = _odict_FIRST(od); node != NULL; node = _odictnode_NEXT(node))
  443. /* Return the index into the hash table, regardless of a valid node. */
  444. static Py_ssize_t
  445. _odict_get_index_raw(PyODictObject *od, PyObject *key, Py_hash_t hash)
  446. {
  447. PyObject *value = NULL;
  448. PyDictKeysObject *keys = ((PyDictObject *)od)->ma_keys;
  449. Py_ssize_t ix;
  450. ix = _Py_dict_lookup((PyDictObject *)od, key, hash, &value);
  451. if (ix == DKIX_EMPTY) {
  452. return keys->dk_nentries; /* index of new entry */
  453. }
  454. if (ix < 0)
  455. return -1;
  456. /* We use pointer arithmetic to get the entry's index into the table. */
  457. return ix;
  458. }
  459. #define ONE ((Py_ssize_t)1)
  460. /* Replace od->od_fast_nodes with a new table matching the size of dict's. */
  461. static int
  462. _odict_resize(PyODictObject *od)
  463. {
  464. Py_ssize_t size, i;
  465. _ODictNode **fast_nodes, *node;
  466. /* Initialize a new "fast nodes" table. */
  467. size = ONE << (((PyDictObject *)od)->ma_keys->dk_log2_size);
  468. fast_nodes = PyMem_NEW(_ODictNode *, size);
  469. if (fast_nodes == NULL) {
  470. PyErr_NoMemory();
  471. return -1;
  472. }
  473. for (i = 0; i < size; i++)
  474. fast_nodes[i] = NULL;
  475. /* Copy the current nodes into the table. */
  476. _odict_FOREACH(od, node) {
  477. i = _odict_get_index_raw(od, _odictnode_KEY(node),
  478. _odictnode_HASH(node));
  479. if (i < 0) {
  480. PyMem_Free(fast_nodes);
  481. return -1;
  482. }
  483. fast_nodes[i] = node;
  484. }
  485. /* Replace the old fast nodes table. */
  486. PyMem_Free(od->od_fast_nodes);
  487. od->od_fast_nodes = fast_nodes;
  488. od->od_fast_nodes_size = size;
  489. od->od_resize_sentinel = ((PyDictObject *)od)->ma_keys;
  490. return 0;
  491. }
  492. /* Return the index into the hash table, regardless of a valid node. */
  493. static Py_ssize_t
  494. _odict_get_index(PyODictObject *od, PyObject *key, Py_hash_t hash)
  495. {
  496. PyDictKeysObject *keys;
  497. assert(key != NULL);
  498. keys = ((PyDictObject *)od)->ma_keys;
  499. /* Ensure od_fast_nodes and dk_entries are in sync. */
  500. if (od->od_resize_sentinel != keys ||
  501. od->od_fast_nodes_size != (ONE << (keys->dk_log2_size))) {
  502. int resize_res = _odict_resize(od);
  503. if (resize_res < 0)
  504. return -1;
  505. }
  506. return _odict_get_index_raw(od, key, hash);
  507. }
  508. /* Returns NULL if there was some error or the key was not found. */
  509. static _ODictNode *
  510. _odict_find_node_hash(PyODictObject *od, PyObject *key, Py_hash_t hash)
  511. {
  512. Py_ssize_t index;
  513. if (_odict_EMPTY(od))
  514. return NULL;
  515. index = _odict_get_index(od, key, hash);
  516. if (index < 0)
  517. return NULL;
  518. assert(od->od_fast_nodes != NULL);
  519. return od->od_fast_nodes[index];
  520. }
  521. static _ODictNode *
  522. _odict_find_node(PyODictObject *od, PyObject *key)
  523. {
  524. Py_ssize_t index;
  525. Py_hash_t hash;
  526. if (_odict_EMPTY(od))
  527. return NULL;
  528. hash = PyObject_Hash(key);
  529. if (hash == -1)
  530. return NULL;
  531. index = _odict_get_index(od, key, hash);
  532. if (index < 0)
  533. return NULL;
  534. assert(od->od_fast_nodes != NULL);
  535. return od->od_fast_nodes[index];
  536. }
  537. static void
  538. _odict_add_head(PyODictObject *od, _ODictNode *node)
  539. {
  540. _odictnode_PREV(node) = NULL;
  541. _odictnode_NEXT(node) = _odict_FIRST(od);
  542. if (_odict_FIRST(od) == NULL)
  543. _odict_LAST(od) = node;
  544. else
  545. _odictnode_PREV(_odict_FIRST(od)) = node;
  546. _odict_FIRST(od) = node;
  547. od->od_state++;
  548. }
  549. static void
  550. _odict_add_tail(PyODictObject *od, _ODictNode *node)
  551. {
  552. _odictnode_PREV(node) = _odict_LAST(od);
  553. _odictnode_NEXT(node) = NULL;
  554. if (_odict_LAST(od) == NULL)
  555. _odict_FIRST(od) = node;
  556. else
  557. _odictnode_NEXT(_odict_LAST(od)) = node;
  558. _odict_LAST(od) = node;
  559. od->od_state++;
  560. }
  561. /* adds the node to the end of the list */
  562. static int
  563. _odict_add_new_node(PyODictObject *od, PyObject *key, Py_hash_t hash)
  564. {
  565. Py_ssize_t i;
  566. _ODictNode *node;
  567. Py_INCREF(key);
  568. i = _odict_get_index(od, key, hash);
  569. if (i < 0) {
  570. if (!PyErr_Occurred())
  571. PyErr_SetObject(PyExc_KeyError, key);
  572. Py_DECREF(key);
  573. return -1;
  574. }
  575. assert(od->od_fast_nodes != NULL);
  576. if (od->od_fast_nodes[i] != NULL) {
  577. /* We already have a node for the key so there's no need to add one. */
  578. Py_DECREF(key);
  579. return 0;
  580. }
  581. /* must not be added yet */
  582. node = (_ODictNode *)PyMem_Malloc(sizeof(_ODictNode));
  583. if (node == NULL) {
  584. Py_DECREF(key);
  585. PyErr_NoMemory();
  586. return -1;
  587. }
  588. _odictnode_KEY(node) = key;
  589. _odictnode_HASH(node) = hash;
  590. _odict_add_tail(od, node);
  591. od->od_fast_nodes[i] = node;
  592. return 0;
  593. }
  594. /* Putting the decref after the free causes problems. */
  595. #define _odictnode_DEALLOC(node) \
  596. do { \
  597. Py_DECREF(_odictnode_KEY(node)); \
  598. PyMem_Free((void *)node); \
  599. } while (0)
  600. /* Repeated calls on the same node are no-ops. */
  601. static void
  602. _odict_remove_node(PyODictObject *od, _ODictNode *node)
  603. {
  604. if (_odict_FIRST(od) == node)
  605. _odict_FIRST(od) = _odictnode_NEXT(node);
  606. else if (_odictnode_PREV(node) != NULL)
  607. _odictnode_NEXT(_odictnode_PREV(node)) = _odictnode_NEXT(node);
  608. if (_odict_LAST(od) == node)
  609. _odict_LAST(od) = _odictnode_PREV(node);
  610. else if (_odictnode_NEXT(node) != NULL)
  611. _odictnode_PREV(_odictnode_NEXT(node)) = _odictnode_PREV(node);
  612. _odictnode_PREV(node) = NULL;
  613. _odictnode_NEXT(node) = NULL;
  614. od->od_state++;
  615. }
  616. /* If someone calls PyDict_DelItem() directly on an OrderedDict, we'll
  617. get all sorts of problems here. In PyODict_DelItem we make sure to
  618. call _odict_clear_node first.
  619. This matters in the case of colliding keys. Suppose we add 3 keys:
  620. [A, B, C], where the hash of C collides with A and the next possible
  621. index in the hash table is occupied by B. If we remove B then for C
  622. the dict's looknode func will give us the old index of B instead of
  623. the index we got before deleting B. However, the node for C in
  624. od_fast_nodes is still at the old dict index of C. Thus to be sure
  625. things don't get out of sync, we clear the node in od_fast_nodes
  626. *before* calling PyDict_DelItem.
  627. The same must be done for any other OrderedDict operations where
  628. we modify od_fast_nodes.
  629. */
  630. static int
  631. _odict_clear_node(PyODictObject *od, _ODictNode *node, PyObject *key,
  632. Py_hash_t hash)
  633. {
  634. Py_ssize_t i;
  635. assert(key != NULL);
  636. if (_odict_EMPTY(od)) {
  637. /* Let later code decide if this is a KeyError. */
  638. return 0;
  639. }
  640. i = _odict_get_index(od, key, hash);
  641. if (i < 0)
  642. return PyErr_Occurred() ? -1 : 0;
  643. assert(od->od_fast_nodes != NULL);
  644. if (node == NULL)
  645. node = od->od_fast_nodes[i];
  646. assert(node == od->od_fast_nodes[i]);
  647. if (node == NULL) {
  648. /* Let later code decide if this is a KeyError. */
  649. return 0;
  650. }
  651. // Now clear the node.
  652. od->od_fast_nodes[i] = NULL;
  653. _odict_remove_node(od, node);
  654. _odictnode_DEALLOC(node);
  655. return 0;
  656. }
  657. static void
  658. _odict_clear_nodes(PyODictObject *od)
  659. {
  660. _ODictNode *node, *next;
  661. PyMem_Free(od->od_fast_nodes);
  662. od->od_fast_nodes = NULL;
  663. od->od_fast_nodes_size = 0;
  664. od->od_resize_sentinel = NULL;
  665. node = _odict_FIRST(od);
  666. _odict_FIRST(od) = NULL;
  667. _odict_LAST(od) = NULL;
  668. while (node != NULL) {
  669. next = _odictnode_NEXT(node);
  670. _odictnode_DEALLOC(node);
  671. node = next;
  672. }
  673. }
  674. /* There isn't any memory management of nodes past this point. */
  675. #undef _odictnode_DEALLOC
  676. static int
  677. _odict_keys_equal(PyODictObject *a, PyODictObject *b)
  678. {
  679. _ODictNode *node_a, *node_b;
  680. node_a = _odict_FIRST(a);
  681. node_b = _odict_FIRST(b);
  682. while (1) {
  683. if (node_a == NULL && node_b == NULL)
  684. /* success: hit the end of each at the same time */
  685. return 1;
  686. else if (node_a == NULL || node_b == NULL)
  687. /* unequal length */
  688. return 0;
  689. else {
  690. int res = PyObject_RichCompareBool(
  691. (PyObject *)_odictnode_KEY(node_a),
  692. (PyObject *)_odictnode_KEY(node_b),
  693. Py_EQ);
  694. if (res < 0)
  695. return res;
  696. else if (res == 0)
  697. return 0;
  698. /* otherwise it must match, so move on to the next one */
  699. node_a = _odictnode_NEXT(node_a);
  700. node_b = _odictnode_NEXT(node_b);
  701. }
  702. }
  703. }
  704. /* ----------------------------------------------
  705. * OrderedDict mapping methods
  706. */
  707. /* mp_ass_subscript: __setitem__() and __delitem__() */
  708. static int
  709. odict_mp_ass_sub(PyODictObject *od, PyObject *v, PyObject *w)
  710. {
  711. if (w == NULL)
  712. return PyODict_DelItem((PyObject *)od, v);
  713. else
  714. return PyODict_SetItem((PyObject *)od, v, w);
  715. }
  716. /* tp_as_mapping */
  717. static PyMappingMethods odict_as_mapping = {
  718. 0, /*mp_length*/
  719. 0, /*mp_subscript*/
  720. (objobjargproc)odict_mp_ass_sub, /*mp_ass_subscript*/
  721. };
  722. /* ----------------------------------------------
  723. * OrderedDict number methods
  724. */
  725. static int mutablemapping_update_arg(PyObject*, PyObject*);
  726. static PyObject *
  727. odict_or(PyObject *left, PyObject *right)
  728. {
  729. PyTypeObject *type;
  730. PyObject *other;
  731. if (PyODict_Check(left)) {
  732. type = Py_TYPE(left);
  733. other = right;
  734. }
  735. else {
  736. type = Py_TYPE(right);
  737. other = left;
  738. }
  739. if (!PyDict_Check(other)) {
  740. Py_RETURN_NOTIMPLEMENTED;
  741. }
  742. PyObject *new = PyObject_CallOneArg((PyObject*)type, left);
  743. if (!new) {
  744. return NULL;
  745. }
  746. if (mutablemapping_update_arg(new, right) < 0) {
  747. Py_DECREF(new);
  748. return NULL;
  749. }
  750. return new;
  751. }
  752. static PyObject *
  753. odict_inplace_or(PyObject *self, PyObject *other)
  754. {
  755. if (mutablemapping_update_arg(self, other) < 0) {
  756. return NULL;
  757. }
  758. return Py_NewRef(self);
  759. }
  760. /* tp_as_number */
  761. static PyNumberMethods odict_as_number = {
  762. .nb_or = odict_or,
  763. .nb_inplace_or = odict_inplace_or,
  764. };
  765. /* ----------------------------------------------
  766. * OrderedDict methods
  767. */
  768. /* fromkeys() */
  769. /*[clinic input]
  770. @classmethod
  771. OrderedDict.fromkeys
  772. iterable as seq: object
  773. value: object = None
  774. Create a new ordered dictionary with keys from iterable and values set to value.
  775. [clinic start generated code]*/
  776. static PyObject *
  777. OrderedDict_fromkeys_impl(PyTypeObject *type, PyObject *seq, PyObject *value)
  778. /*[clinic end generated code: output=c10390d452d78d6d input=1a0476c229c597b3]*/
  779. {
  780. return _PyDict_FromKeys((PyObject *)type, seq, value);
  781. }
  782. /* __sizeof__() */
  783. /* OrderedDict.__sizeof__() does not have a docstring. */
  784. PyDoc_STRVAR(odict_sizeof__doc__, "");
  785. static PyObject *
  786. odict_sizeof(PyODictObject *od, PyObject *Py_UNUSED(ignored))
  787. {
  788. Py_ssize_t res = _PyDict_SizeOf((PyDictObject *)od);
  789. res += sizeof(_ODictNode *) * od->od_fast_nodes_size; /* od_fast_nodes */
  790. if (!_odict_EMPTY(od)) {
  791. res += sizeof(_ODictNode) * PyODict_SIZE(od); /* linked-list */
  792. }
  793. return PyLong_FromSsize_t(res);
  794. }
  795. /* __reduce__() */
  796. PyDoc_STRVAR(odict_reduce__doc__, "Return state information for pickling");
  797. static PyObject *
  798. odict_reduce(register PyODictObject *od, PyObject *Py_UNUSED(ignored))
  799. {
  800. PyObject *state, *result = NULL;
  801. PyObject *items_iter, *items, *args = NULL;
  802. /* capture any instance state */
  803. state = _PyObject_GetState((PyObject *)od);
  804. if (state == NULL)
  805. goto Done;
  806. /* build the result */
  807. args = PyTuple_New(0);
  808. if (args == NULL)
  809. goto Done;
  810. items = PyObject_CallMethodNoArgs((PyObject *)od, &_Py_ID(items));
  811. if (items == NULL)
  812. goto Done;
  813. items_iter = PyObject_GetIter(items);
  814. Py_DECREF(items);
  815. if (items_iter == NULL)
  816. goto Done;
  817. result = PyTuple_Pack(5, Py_TYPE(od), args, state, Py_None, items_iter);
  818. Py_DECREF(items_iter);
  819. Done:
  820. Py_XDECREF(state);
  821. Py_XDECREF(args);
  822. return result;
  823. }
  824. /* setdefault(): Skips __missing__() calls. */
  825. /*[clinic input]
  826. OrderedDict.setdefault
  827. key: object
  828. default: object = None
  829. Insert key with a value of default if key is not in the dictionary.
  830. Return the value for key if key is in the dictionary, else default.
  831. [clinic start generated code]*/
  832. static PyObject *
  833. OrderedDict_setdefault_impl(PyODictObject *self, PyObject *key,
  834. PyObject *default_value)
  835. /*[clinic end generated code: output=97537cb7c28464b6 input=38e098381c1efbc6]*/
  836. {
  837. PyObject *result = NULL;
  838. if (PyODict_CheckExact(self)) {
  839. result = PyODict_GetItemWithError(self, key); /* borrowed */
  840. if (result == NULL) {
  841. if (PyErr_Occurred())
  842. return NULL;
  843. assert(_odict_find_node(self, key) == NULL);
  844. if (PyODict_SetItem((PyObject *)self, key, default_value) >= 0) {
  845. result = Py_NewRef(default_value);
  846. }
  847. }
  848. else {
  849. Py_INCREF(result);
  850. }
  851. }
  852. else {
  853. int exists = PySequence_Contains((PyObject *)self, key);
  854. if (exists < 0) {
  855. return NULL;
  856. }
  857. else if (exists) {
  858. result = PyObject_GetItem((PyObject *)self, key);
  859. }
  860. else if (PyObject_SetItem((PyObject *)self, key, default_value) >= 0) {
  861. result = Py_NewRef(default_value);
  862. }
  863. }
  864. return result;
  865. }
  866. /* pop() */
  867. static PyObject *
  868. _odict_popkey_hash(PyObject *od, PyObject *key, PyObject *failobj,
  869. Py_hash_t hash)
  870. {
  871. PyObject *value = NULL;
  872. _ODictNode *node = _odict_find_node_hash((PyODictObject *)od, key, hash);
  873. if (node != NULL) {
  874. /* Pop the node first to avoid a possible dict resize (due to
  875. eval loop reentrancy) and complications due to hash collision
  876. resolution. */
  877. int res = _odict_clear_node((PyODictObject *)od, node, key, hash);
  878. if (res < 0) {
  879. return NULL;
  880. }
  881. /* Now delete the value from the dict. */
  882. value = _PyDict_Pop_KnownHash(od, key, hash, failobj);
  883. }
  884. else if (value == NULL && !PyErr_Occurred()) {
  885. /* Apply the fallback value, if necessary. */
  886. if (failobj) {
  887. value = Py_NewRef(failobj);
  888. }
  889. else {
  890. PyErr_SetObject(PyExc_KeyError, key);
  891. }
  892. }
  893. return value;
  894. }
  895. /* Skips __missing__() calls. */
  896. /*[clinic input]
  897. OrderedDict.pop
  898. key: object
  899. default: object = NULL
  900. od.pop(key[,default]) -> v, remove specified key and return the corresponding value.
  901. If the key is not found, return the default if given; otherwise,
  902. raise a KeyError.
  903. [clinic start generated code]*/
  904. static PyObject *
  905. OrderedDict_pop_impl(PyODictObject *self, PyObject *key,
  906. PyObject *default_value)
  907. /*[clinic end generated code: output=7a6447d104e7494b input=7efe36601007dff7]*/
  908. {
  909. Py_hash_t hash = PyObject_Hash(key);
  910. if (hash == -1)
  911. return NULL;
  912. return _odict_popkey_hash((PyObject *)self, key, default_value, hash);
  913. }
  914. /* popitem() */
  915. /*[clinic input]
  916. OrderedDict.popitem
  917. last: bool = True
  918. Remove and return a (key, value) pair from the dictionary.
  919. Pairs are returned in LIFO order if last is true or FIFO order if false.
  920. [clinic start generated code]*/
  921. static PyObject *
  922. OrderedDict_popitem_impl(PyODictObject *self, int last)
  923. /*[clinic end generated code: output=98e7d986690d49eb input=d992ac5ee8305e1a]*/
  924. {
  925. PyObject *key, *value, *item = NULL;
  926. _ODictNode *node;
  927. /* pull the item */
  928. if (_odict_EMPTY(self)) {
  929. PyErr_SetString(PyExc_KeyError, "dictionary is empty");
  930. return NULL;
  931. }
  932. node = last ? _odict_LAST(self) : _odict_FIRST(self);
  933. key = Py_NewRef(_odictnode_KEY(node));
  934. value = _odict_popkey_hash((PyObject *)self, key, NULL, _odictnode_HASH(node));
  935. if (value == NULL)
  936. return NULL;
  937. item = PyTuple_Pack(2, key, value);
  938. Py_DECREF(key);
  939. Py_DECREF(value);
  940. return item;
  941. }
  942. /* keys() */
  943. /* MutableMapping.keys() does not have a docstring. */
  944. PyDoc_STRVAR(odict_keys__doc__, "");
  945. static PyObject * odictkeys_new(PyObject *od, PyObject *Py_UNUSED(ignored)); /* forward */
  946. /* values() */
  947. /* MutableMapping.values() does not have a docstring. */
  948. PyDoc_STRVAR(odict_values__doc__, "");
  949. static PyObject * odictvalues_new(PyObject *od, PyObject *Py_UNUSED(ignored)); /* forward */
  950. /* items() */
  951. /* MutableMapping.items() does not have a docstring. */
  952. PyDoc_STRVAR(odict_items__doc__, "");
  953. static PyObject * odictitems_new(PyObject *od, PyObject *Py_UNUSED(ignored)); /* forward */
  954. /* update() */
  955. /* MutableMapping.update() does not have a docstring. */
  956. PyDoc_STRVAR(odict_update__doc__, "");
  957. /* forward */
  958. static PyObject * mutablemapping_update(PyObject *, PyObject *, PyObject *);
  959. #define odict_update mutablemapping_update
  960. /* clear() */
  961. PyDoc_STRVAR(odict_clear__doc__,
  962. "od.clear() -> None. Remove all items from od.");
  963. static PyObject *
  964. odict_clear(register PyODictObject *od, PyObject *Py_UNUSED(ignored))
  965. {
  966. PyDict_Clear((PyObject *)od);
  967. _odict_clear_nodes(od);
  968. Py_RETURN_NONE;
  969. }
  970. /* copy() */
  971. /* forward */
  972. static int _PyODict_SetItem_KnownHash(PyObject *, PyObject *, PyObject *,
  973. Py_hash_t);
  974. PyDoc_STRVAR(odict_copy__doc__, "od.copy() -> a shallow copy of od");
  975. static PyObject *
  976. odict_copy(register PyODictObject *od, PyObject *Py_UNUSED(ignored))
  977. {
  978. _ODictNode *node;
  979. PyObject *od_copy;
  980. if (PyODict_CheckExact(od))
  981. od_copy = PyODict_New();
  982. else
  983. od_copy = _PyObject_CallNoArgs((PyObject *)Py_TYPE(od));
  984. if (od_copy == NULL)
  985. return NULL;
  986. if (PyODict_CheckExact(od)) {
  987. _odict_FOREACH(od, node) {
  988. PyObject *key = _odictnode_KEY(node);
  989. PyObject *value = _odictnode_VALUE(node, od);
  990. if (value == NULL) {
  991. if (!PyErr_Occurred())
  992. PyErr_SetObject(PyExc_KeyError, key);
  993. goto fail;
  994. }
  995. if (_PyODict_SetItem_KnownHash((PyObject *)od_copy, key, value,
  996. _odictnode_HASH(node)) != 0)
  997. goto fail;
  998. }
  999. }
  1000. else {
  1001. _odict_FOREACH(od, node) {
  1002. int res;
  1003. PyObject *value = PyObject_GetItem((PyObject *)od,
  1004. _odictnode_KEY(node));
  1005. if (value == NULL)
  1006. goto fail;
  1007. res = PyObject_SetItem((PyObject *)od_copy,
  1008. _odictnode_KEY(node), value);
  1009. Py_DECREF(value);
  1010. if (res != 0)
  1011. goto fail;
  1012. }
  1013. }
  1014. return od_copy;
  1015. fail:
  1016. Py_DECREF(od_copy);
  1017. return NULL;
  1018. }
  1019. /* __reversed__() */
  1020. PyDoc_STRVAR(odict_reversed__doc__, "od.__reversed__() <==> reversed(od)");
  1021. #define _odict_ITER_REVERSED 1
  1022. #define _odict_ITER_KEYS 2
  1023. #define _odict_ITER_VALUES 4
  1024. #define _odict_ITER_ITEMS (_odict_ITER_KEYS|_odict_ITER_VALUES)
  1025. /* forward */
  1026. static PyObject * odictiter_new(PyODictObject *, int);
  1027. static PyObject *
  1028. odict_reversed(PyODictObject *od, PyObject *Py_UNUSED(ignored))
  1029. {
  1030. return odictiter_new(od, _odict_ITER_KEYS|_odict_ITER_REVERSED);
  1031. }
  1032. /* move_to_end() */
  1033. /*[clinic input]
  1034. OrderedDict.move_to_end
  1035. key: object
  1036. last: bool = True
  1037. Move an existing element to the end (or beginning if last is false).
  1038. Raise KeyError if the element does not exist.
  1039. [clinic start generated code]*/
  1040. static PyObject *
  1041. OrderedDict_move_to_end_impl(PyODictObject *self, PyObject *key, int last)
  1042. /*[clinic end generated code: output=fafa4c5cc9b92f20 input=d6ceff7132a2fcd7]*/
  1043. {
  1044. _ODictNode *node;
  1045. if (_odict_EMPTY(self)) {
  1046. PyErr_SetObject(PyExc_KeyError, key);
  1047. return NULL;
  1048. }
  1049. node = last ? _odict_LAST(self) : _odict_FIRST(self);
  1050. if (key != _odictnode_KEY(node)) {
  1051. node = _odict_find_node(self, key);
  1052. if (node == NULL) {
  1053. if (!PyErr_Occurred())
  1054. PyErr_SetObject(PyExc_KeyError, key);
  1055. return NULL;
  1056. }
  1057. if (last) {
  1058. /* Only move if not already the last one. */
  1059. if (node != _odict_LAST(self)) {
  1060. _odict_remove_node(self, node);
  1061. _odict_add_tail(self, node);
  1062. }
  1063. }
  1064. else {
  1065. /* Only move if not already the first one. */
  1066. if (node != _odict_FIRST(self)) {
  1067. _odict_remove_node(self, node);
  1068. _odict_add_head(self, node);
  1069. }
  1070. }
  1071. }
  1072. Py_RETURN_NONE;
  1073. }
  1074. /* tp_methods */
  1075. static PyMethodDef odict_methods[] = {
  1076. /* overridden dict methods */
  1077. ORDEREDDICT_FROMKEYS_METHODDEF
  1078. {"__sizeof__", (PyCFunction)odict_sizeof, METH_NOARGS,
  1079. odict_sizeof__doc__},
  1080. {"__reduce__", (PyCFunction)odict_reduce, METH_NOARGS,
  1081. odict_reduce__doc__},
  1082. ORDEREDDICT_SETDEFAULT_METHODDEF
  1083. ORDEREDDICT_POP_METHODDEF
  1084. ORDEREDDICT_POPITEM_METHODDEF
  1085. {"keys", odictkeys_new, METH_NOARGS,
  1086. odict_keys__doc__},
  1087. {"values", odictvalues_new, METH_NOARGS,
  1088. odict_values__doc__},
  1089. {"items", odictitems_new, METH_NOARGS,
  1090. odict_items__doc__},
  1091. {"update", _PyCFunction_CAST(odict_update), METH_VARARGS | METH_KEYWORDS,
  1092. odict_update__doc__},
  1093. {"clear", (PyCFunction)odict_clear, METH_NOARGS,
  1094. odict_clear__doc__},
  1095. {"copy", (PyCFunction)odict_copy, METH_NOARGS,
  1096. odict_copy__doc__},
  1097. /* new methods */
  1098. {"__reversed__", (PyCFunction)odict_reversed, METH_NOARGS,
  1099. odict_reversed__doc__},
  1100. ORDEREDDICT_MOVE_TO_END_METHODDEF
  1101. {NULL, NULL} /* sentinel */
  1102. };
  1103. /* ----------------------------------------------
  1104. * OrderedDict members
  1105. */
  1106. /* tp_getset */
  1107. static PyGetSetDef odict_getset[] = {
  1108. {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
  1109. {NULL}
  1110. };
  1111. /* ----------------------------------------------
  1112. * OrderedDict type slot methods
  1113. */
  1114. /* tp_dealloc */
  1115. static void
  1116. odict_dealloc(PyODictObject *self)
  1117. {
  1118. PyObject_GC_UnTrack(self);
  1119. Py_TRASHCAN_BEGIN(self, odict_dealloc)
  1120. Py_XDECREF(self->od_inst_dict);
  1121. if (self->od_weakreflist != NULL)
  1122. PyObject_ClearWeakRefs((PyObject *)self);
  1123. _odict_clear_nodes(self);
  1124. PyDict_Type.tp_dealloc((PyObject *)self);
  1125. Py_TRASHCAN_END
  1126. }
  1127. /* tp_repr */
  1128. static PyObject *
  1129. odict_repr(PyODictObject *self)
  1130. {
  1131. int i;
  1132. PyObject *result = NULL, *dcopy = NULL;
  1133. if (PyODict_SIZE(self) == 0)
  1134. return PyUnicode_FromFormat("%s()", _PyType_Name(Py_TYPE(self)));
  1135. i = Py_ReprEnter((PyObject *)self);
  1136. if (i != 0) {
  1137. return i > 0 ? PyUnicode_FromString("...") : NULL;
  1138. }
  1139. dcopy = PyDict_Copy((PyObject *)self);
  1140. if (dcopy == NULL) {
  1141. goto Done;
  1142. }
  1143. result = PyUnicode_FromFormat("%s(%R)",
  1144. _PyType_Name(Py_TYPE(self)),
  1145. dcopy);
  1146. Py_DECREF(dcopy);
  1147. Done:
  1148. Py_ReprLeave((PyObject *)self);
  1149. return result;
  1150. }
  1151. /* tp_doc */
  1152. PyDoc_STRVAR(odict_doc,
  1153. "Dictionary that remembers insertion order");
  1154. /* tp_traverse */
  1155. static int
  1156. odict_traverse(PyODictObject *od, visitproc visit, void *arg)
  1157. {
  1158. _ODictNode *node;
  1159. Py_VISIT(od->od_inst_dict);
  1160. _odict_FOREACH(od, node) {
  1161. Py_VISIT(_odictnode_KEY(node));
  1162. }
  1163. return PyDict_Type.tp_traverse((PyObject *)od, visit, arg);
  1164. }
  1165. /* tp_clear */
  1166. static int
  1167. odict_tp_clear(PyODictObject *od)
  1168. {
  1169. Py_CLEAR(od->od_inst_dict);
  1170. PyDict_Clear((PyObject *)od);
  1171. _odict_clear_nodes(od);
  1172. return 0;
  1173. }
  1174. /* tp_richcompare */
  1175. static PyObject *
  1176. odict_richcompare(PyObject *v, PyObject *w, int op)
  1177. {
  1178. if (!PyODict_Check(v) || !PyDict_Check(w)) {
  1179. Py_RETURN_NOTIMPLEMENTED;
  1180. }
  1181. if (op == Py_EQ || op == Py_NE) {
  1182. PyObject *res, *cmp;
  1183. int eq;
  1184. cmp = PyDict_Type.tp_richcompare(v, w, op);
  1185. if (cmp == NULL)
  1186. return NULL;
  1187. if (!PyODict_Check(w))
  1188. return cmp;
  1189. if (op == Py_EQ && cmp == Py_False)
  1190. return cmp;
  1191. if (op == Py_NE && cmp == Py_True)
  1192. return cmp;
  1193. Py_DECREF(cmp);
  1194. /* Try comparing odict keys. */
  1195. eq = _odict_keys_equal((PyODictObject *)v, (PyODictObject *)w);
  1196. if (eq < 0)
  1197. return NULL;
  1198. res = (eq == (op == Py_EQ)) ? Py_True : Py_False;
  1199. return Py_NewRef(res);
  1200. } else {
  1201. Py_RETURN_NOTIMPLEMENTED;
  1202. }
  1203. }
  1204. /* tp_iter */
  1205. static PyObject *
  1206. odict_iter(PyODictObject *od)
  1207. {
  1208. return odictiter_new(od, _odict_ITER_KEYS);
  1209. }
  1210. /* tp_init */
  1211. static int
  1212. odict_init(PyObject *self, PyObject *args, PyObject *kwds)
  1213. {
  1214. PyObject *res;
  1215. Py_ssize_t len = PyObject_Length(args);
  1216. if (len == -1)
  1217. return -1;
  1218. if (len > 1) {
  1219. const char *msg = "expected at most 1 arguments, got %zd";
  1220. PyErr_Format(PyExc_TypeError, msg, len);
  1221. return -1;
  1222. }
  1223. /* __init__() triggering update() is just the way things are! */
  1224. res = odict_update(self, args, kwds);
  1225. if (res == NULL) {
  1226. return -1;
  1227. } else {
  1228. Py_DECREF(res);
  1229. return 0;
  1230. }
  1231. }
  1232. /* PyODict_Type */
  1233. PyTypeObject PyODict_Type = {
  1234. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  1235. "collections.OrderedDict", /* tp_name */
  1236. sizeof(PyODictObject), /* tp_basicsize */
  1237. 0, /* tp_itemsize */
  1238. (destructor)odict_dealloc, /* tp_dealloc */
  1239. 0, /* tp_vectorcall_offset */
  1240. 0, /* tp_getattr */
  1241. 0, /* tp_setattr */
  1242. 0, /* tp_as_async */
  1243. (reprfunc)odict_repr, /* tp_repr */
  1244. &odict_as_number, /* tp_as_number */
  1245. 0, /* tp_as_sequence */
  1246. &odict_as_mapping, /* tp_as_mapping */
  1247. 0, /* tp_hash */
  1248. 0, /* tp_call */
  1249. 0, /* tp_str */
  1250. 0, /* tp_getattro */
  1251. 0, /* tp_setattro */
  1252. 0, /* tp_as_buffer */
  1253. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,/* tp_flags */
  1254. odict_doc, /* tp_doc */
  1255. (traverseproc)odict_traverse, /* tp_traverse */
  1256. (inquiry)odict_tp_clear, /* tp_clear */
  1257. (richcmpfunc)odict_richcompare, /* tp_richcompare */
  1258. offsetof(PyODictObject, od_weakreflist), /* tp_weaklistoffset */
  1259. (getiterfunc)odict_iter, /* tp_iter */
  1260. 0, /* tp_iternext */
  1261. odict_methods, /* tp_methods */
  1262. 0, /* tp_members */
  1263. odict_getset, /* tp_getset */
  1264. &PyDict_Type, /* tp_base */
  1265. 0, /* tp_dict */
  1266. 0, /* tp_descr_get */
  1267. 0, /* tp_descr_set */
  1268. offsetof(PyODictObject, od_inst_dict), /* tp_dictoffset */
  1269. (initproc)odict_init, /* tp_init */
  1270. PyType_GenericAlloc, /* tp_alloc */
  1271. 0, /* tp_new */
  1272. 0, /* tp_free */
  1273. };
  1274. /* ----------------------------------------------
  1275. * the public OrderedDict API
  1276. */
  1277. PyObject *
  1278. PyODict_New(void)
  1279. {
  1280. return PyDict_Type.tp_new(&PyODict_Type, NULL, NULL);
  1281. }
  1282. static int
  1283. _PyODict_SetItem_KnownHash(PyObject *od, PyObject *key, PyObject *value,
  1284. Py_hash_t hash)
  1285. {
  1286. int res = _PyDict_SetItem_KnownHash(od, key, value, hash);
  1287. if (res == 0) {
  1288. res = _odict_add_new_node((PyODictObject *)od, key, hash);
  1289. if (res < 0) {
  1290. /* Revert setting the value on the dict */
  1291. PyObject *exc = PyErr_GetRaisedException();
  1292. (void) _PyDict_DelItem_KnownHash(od, key, hash);
  1293. _PyErr_ChainExceptions1(exc);
  1294. }
  1295. }
  1296. return res;
  1297. }
  1298. int
  1299. PyODict_SetItem(PyObject *od, PyObject *key, PyObject *value)
  1300. {
  1301. Py_hash_t hash = PyObject_Hash(key);
  1302. if (hash == -1)
  1303. return -1;
  1304. return _PyODict_SetItem_KnownHash(od, key, value, hash);
  1305. }
  1306. int
  1307. PyODict_DelItem(PyObject *od, PyObject *key)
  1308. {
  1309. int res;
  1310. Py_hash_t hash = PyObject_Hash(key);
  1311. if (hash == -1)
  1312. return -1;
  1313. res = _odict_clear_node((PyODictObject *)od, NULL, key, hash);
  1314. if (res < 0)
  1315. return -1;
  1316. return _PyDict_DelItem_KnownHash(od, key, hash);
  1317. }
  1318. /* -------------------------------------------
  1319. * The OrderedDict views (keys/values/items)
  1320. */
  1321. typedef struct {
  1322. PyObject_HEAD
  1323. int kind;
  1324. PyODictObject *di_odict;
  1325. Py_ssize_t di_size;
  1326. size_t di_state;
  1327. PyObject *di_current;
  1328. PyObject *di_result; /* reusable result tuple for iteritems */
  1329. } odictiterobject;
  1330. static void
  1331. odictiter_dealloc(odictiterobject *di)
  1332. {
  1333. _PyObject_GC_UNTRACK(di);
  1334. Py_XDECREF(di->di_odict);
  1335. Py_XDECREF(di->di_current);
  1336. if ((di->kind & _odict_ITER_ITEMS) == _odict_ITER_ITEMS) {
  1337. Py_DECREF(di->di_result);
  1338. }
  1339. PyObject_GC_Del(di);
  1340. }
  1341. static int
  1342. odictiter_traverse(odictiterobject *di, visitproc visit, void *arg)
  1343. {
  1344. Py_VISIT(di->di_odict);
  1345. Py_VISIT(di->di_current); /* A key could be any type, not just str. */
  1346. Py_VISIT(di->di_result);
  1347. return 0;
  1348. }
  1349. /* In order to protect against modifications during iteration, we track
  1350. * the current key instead of the current node. */
  1351. static PyObject *
  1352. odictiter_nextkey(odictiterobject *di)
  1353. {
  1354. PyObject *key = NULL;
  1355. _ODictNode *node;
  1356. int reversed = di->kind & _odict_ITER_REVERSED;
  1357. if (di->di_odict == NULL)
  1358. return NULL;
  1359. if (di->di_current == NULL)
  1360. goto done; /* We're already done. */
  1361. /* Check for unsupported changes. */
  1362. if (di->di_odict->od_state != di->di_state) {
  1363. PyErr_SetString(PyExc_RuntimeError,
  1364. "OrderedDict mutated during iteration");
  1365. goto done;
  1366. }
  1367. if (di->di_size != PyODict_SIZE(di->di_odict)) {
  1368. PyErr_SetString(PyExc_RuntimeError,
  1369. "OrderedDict changed size during iteration");
  1370. di->di_size = -1; /* Make this state sticky */
  1371. return NULL;
  1372. }
  1373. /* Get the key. */
  1374. node = _odict_find_node(di->di_odict, di->di_current);
  1375. if (node == NULL) {
  1376. if (!PyErr_Occurred())
  1377. PyErr_SetObject(PyExc_KeyError, di->di_current);
  1378. /* Must have been deleted. */
  1379. Py_CLEAR(di->di_current);
  1380. return NULL;
  1381. }
  1382. key = di->di_current;
  1383. /* Advance to the next key. */
  1384. node = reversed ? _odictnode_PREV(node) : _odictnode_NEXT(node);
  1385. if (node == NULL) {
  1386. /* Reached the end. */
  1387. di->di_current = NULL;
  1388. }
  1389. else {
  1390. di->di_current = Py_NewRef(_odictnode_KEY(node));
  1391. }
  1392. return key;
  1393. done:
  1394. Py_CLEAR(di->di_odict);
  1395. return key;
  1396. }
  1397. static PyObject *
  1398. odictiter_iternext(odictiterobject *di)
  1399. {
  1400. PyObject *result, *value;
  1401. PyObject *key = odictiter_nextkey(di); /* new reference */
  1402. if (key == NULL)
  1403. return NULL;
  1404. /* Handle the keys case. */
  1405. if (! (di->kind & _odict_ITER_VALUES)) {
  1406. return key;
  1407. }
  1408. value = PyODict_GetItem((PyObject *)di->di_odict, key); /* borrowed */
  1409. if (value == NULL) {
  1410. if (!PyErr_Occurred())
  1411. PyErr_SetObject(PyExc_KeyError, key);
  1412. Py_DECREF(key);
  1413. goto done;
  1414. }
  1415. Py_INCREF(value);
  1416. /* Handle the values case. */
  1417. if (!(di->kind & _odict_ITER_KEYS)) {
  1418. Py_DECREF(key);
  1419. return value;
  1420. }
  1421. /* Handle the items case. */
  1422. result = di->di_result;
  1423. if (Py_REFCNT(result) == 1) {
  1424. /* not in use so we can reuse it
  1425. * (the common case during iteration) */
  1426. Py_INCREF(result);
  1427. Py_DECREF(PyTuple_GET_ITEM(result, 0)); /* borrowed */
  1428. Py_DECREF(PyTuple_GET_ITEM(result, 1)); /* borrowed */
  1429. // bpo-42536: The GC may have untracked this result tuple. Since we're
  1430. // recycling it, make sure it's tracked again:
  1431. if (!_PyObject_GC_IS_TRACKED(result)) {
  1432. _PyObject_GC_TRACK(result);
  1433. }
  1434. }
  1435. else {
  1436. result = PyTuple_New(2);
  1437. if (result == NULL) {
  1438. Py_DECREF(key);
  1439. Py_DECREF(value);
  1440. goto done;
  1441. }
  1442. }
  1443. PyTuple_SET_ITEM(result, 0, key); /* steals reference */
  1444. PyTuple_SET_ITEM(result, 1, value); /* steals reference */
  1445. return result;
  1446. done:
  1447. Py_CLEAR(di->di_current);
  1448. Py_CLEAR(di->di_odict);
  1449. return NULL;
  1450. }
  1451. /* No need for tp_clear because odictiterobject is not mutable. */
  1452. PyDoc_STRVAR(reduce_doc, "Return state information for pickling");
  1453. static PyObject *
  1454. odictiter_reduce(odictiterobject *di, PyObject *Py_UNUSED(ignored))
  1455. {
  1456. /* copy the iterator state */
  1457. odictiterobject tmp = *di;
  1458. Py_XINCREF(tmp.di_odict);
  1459. Py_XINCREF(tmp.di_current);
  1460. /* iterate the temporary into a list */
  1461. PyObject *list = PySequence_List((PyObject*)&tmp);
  1462. Py_XDECREF(tmp.di_odict);
  1463. Py_XDECREF(tmp.di_current);
  1464. if (list == NULL) {
  1465. return NULL;
  1466. }
  1467. return Py_BuildValue("N(N)", _PyEval_GetBuiltin(&_Py_ID(iter)), list);
  1468. }
  1469. static PyMethodDef odictiter_methods[] = {
  1470. {"__reduce__", (PyCFunction)odictiter_reduce, METH_NOARGS, reduce_doc},
  1471. {NULL, NULL} /* sentinel */
  1472. };
  1473. PyTypeObject PyODictIter_Type = {
  1474. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  1475. "odict_iterator", /* tp_name */
  1476. sizeof(odictiterobject), /* tp_basicsize */
  1477. 0, /* tp_itemsize */
  1478. /* methods */
  1479. (destructor)odictiter_dealloc, /* tp_dealloc */
  1480. 0, /* tp_vectorcall_offset */
  1481. 0, /* tp_getattr */
  1482. 0, /* tp_setattr */
  1483. 0, /* tp_as_async */
  1484. 0, /* tp_repr */
  1485. 0, /* tp_as_number */
  1486. 0, /* tp_as_sequence */
  1487. 0, /* tp_as_mapping */
  1488. 0, /* tp_hash */
  1489. 0, /* tp_call */
  1490. 0, /* tp_str */
  1491. PyObject_GenericGetAttr, /* tp_getattro */
  1492. 0, /* tp_setattro */
  1493. 0, /* tp_as_buffer */
  1494. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
  1495. 0, /* tp_doc */
  1496. (traverseproc)odictiter_traverse, /* tp_traverse */
  1497. 0, /* tp_clear */
  1498. 0, /* tp_richcompare */
  1499. 0, /* tp_weaklistoffset */
  1500. PyObject_SelfIter, /* tp_iter */
  1501. (iternextfunc)odictiter_iternext, /* tp_iternext */
  1502. odictiter_methods, /* tp_methods */
  1503. 0,
  1504. };
  1505. static PyObject *
  1506. odictiter_new(PyODictObject *od, int kind)
  1507. {
  1508. odictiterobject *di;
  1509. _ODictNode *node;
  1510. int reversed = kind & _odict_ITER_REVERSED;
  1511. di = PyObject_GC_New(odictiterobject, &PyODictIter_Type);
  1512. if (di == NULL)
  1513. return NULL;
  1514. if ((kind & _odict_ITER_ITEMS) == _odict_ITER_ITEMS) {
  1515. di->di_result = PyTuple_Pack(2, Py_None, Py_None);
  1516. if (di->di_result == NULL) {
  1517. Py_DECREF(di);
  1518. return NULL;
  1519. }
  1520. }
  1521. else {
  1522. di->di_result = NULL;
  1523. }
  1524. di->kind = kind;
  1525. node = reversed ? _odict_LAST(od) : _odict_FIRST(od);
  1526. di->di_current = node ? Py_NewRef(_odictnode_KEY(node)) : NULL;
  1527. di->di_size = PyODict_SIZE(od);
  1528. di->di_state = od->od_state;
  1529. di->di_odict = (PyODictObject*)Py_NewRef(od);
  1530. _PyObject_GC_TRACK(di);
  1531. return (PyObject *)di;
  1532. }
  1533. /* keys() */
  1534. static PyObject *
  1535. odictkeys_iter(_PyDictViewObject *dv)
  1536. {
  1537. if (dv->dv_dict == NULL) {
  1538. Py_RETURN_NONE;
  1539. }
  1540. return odictiter_new((PyODictObject *)dv->dv_dict,
  1541. _odict_ITER_KEYS);
  1542. }
  1543. static PyObject *
  1544. odictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
  1545. {
  1546. if (dv->dv_dict == NULL) {
  1547. Py_RETURN_NONE;
  1548. }
  1549. return odictiter_new((PyODictObject *)dv->dv_dict,
  1550. _odict_ITER_KEYS|_odict_ITER_REVERSED);
  1551. }
  1552. static PyMethodDef odictkeys_methods[] = {
  1553. {"__reversed__", (PyCFunction)odictkeys_reversed, METH_NOARGS, NULL},
  1554. {NULL, NULL} /* sentinel */
  1555. };
  1556. PyTypeObject PyODictKeys_Type = {
  1557. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  1558. "odict_keys", /* tp_name */
  1559. 0, /* tp_basicsize */
  1560. 0, /* tp_itemsize */
  1561. 0, /* tp_dealloc */
  1562. 0, /* tp_vectorcall_offset */
  1563. 0, /* tp_getattr */
  1564. 0, /* tp_setattr */
  1565. 0, /* tp_as_async */
  1566. 0, /* tp_repr */
  1567. 0, /* tp_as_number */
  1568. 0, /* tp_as_sequence */
  1569. 0, /* tp_as_mapping */
  1570. 0, /* tp_hash */
  1571. 0, /* tp_call */
  1572. 0, /* tp_str */
  1573. 0, /* tp_getattro */
  1574. 0, /* tp_setattro */
  1575. 0, /* tp_as_buffer */
  1576. 0, /* tp_flags */
  1577. 0, /* tp_doc */
  1578. 0, /* tp_traverse */
  1579. 0, /* tp_clear */
  1580. 0, /* tp_richcompare */
  1581. 0, /* tp_weaklistoffset */
  1582. (getiterfunc)odictkeys_iter, /* tp_iter */
  1583. 0, /* tp_iternext */
  1584. odictkeys_methods, /* tp_methods */
  1585. 0, /* tp_members */
  1586. 0, /* tp_getset */
  1587. &PyDictKeys_Type, /* tp_base */
  1588. };
  1589. static PyObject *
  1590. odictkeys_new(PyObject *od, PyObject *Py_UNUSED(ignored))
  1591. {
  1592. return _PyDictView_New(od, &PyODictKeys_Type);
  1593. }
  1594. /* items() */
  1595. static PyObject *
  1596. odictitems_iter(_PyDictViewObject *dv)
  1597. {
  1598. if (dv->dv_dict == NULL) {
  1599. Py_RETURN_NONE;
  1600. }
  1601. return odictiter_new((PyODictObject *)dv->dv_dict,
  1602. _odict_ITER_KEYS|_odict_ITER_VALUES);
  1603. }
  1604. static PyObject *
  1605. odictitems_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
  1606. {
  1607. if (dv->dv_dict == NULL) {
  1608. Py_RETURN_NONE;
  1609. }
  1610. return odictiter_new((PyODictObject *)dv->dv_dict,
  1611. _odict_ITER_KEYS|_odict_ITER_VALUES|_odict_ITER_REVERSED);
  1612. }
  1613. static PyMethodDef odictitems_methods[] = {
  1614. {"__reversed__", (PyCFunction)odictitems_reversed, METH_NOARGS, NULL},
  1615. {NULL, NULL} /* sentinel */
  1616. };
  1617. PyTypeObject PyODictItems_Type = {
  1618. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  1619. "odict_items", /* tp_name */
  1620. 0, /* tp_basicsize */
  1621. 0, /* tp_itemsize */
  1622. 0, /* tp_dealloc */
  1623. 0, /* tp_vectorcall_offset */
  1624. 0, /* tp_getattr */
  1625. 0, /* tp_setattr */
  1626. 0, /* tp_as_async */
  1627. 0, /* tp_repr */
  1628. 0, /* tp_as_number */
  1629. 0, /* tp_as_sequence */
  1630. 0, /* tp_as_mapping */
  1631. 0, /* tp_hash */
  1632. 0, /* tp_call */
  1633. 0, /* tp_str */
  1634. 0, /* tp_getattro */
  1635. 0, /* tp_setattro */
  1636. 0, /* tp_as_buffer */
  1637. 0, /* tp_flags */
  1638. 0, /* tp_doc */
  1639. 0, /* tp_traverse */
  1640. 0, /* tp_clear */
  1641. 0, /* tp_richcompare */
  1642. 0, /* tp_weaklistoffset */
  1643. (getiterfunc)odictitems_iter, /* tp_iter */
  1644. 0, /* tp_iternext */
  1645. odictitems_methods, /* tp_methods */
  1646. 0, /* tp_members */
  1647. 0, /* tp_getset */
  1648. &PyDictItems_Type, /* tp_base */
  1649. };
  1650. static PyObject *
  1651. odictitems_new(PyObject *od, PyObject *Py_UNUSED(ignored))
  1652. {
  1653. return _PyDictView_New(od, &PyODictItems_Type);
  1654. }
  1655. /* values() */
  1656. static PyObject *
  1657. odictvalues_iter(_PyDictViewObject *dv)
  1658. {
  1659. if (dv->dv_dict == NULL) {
  1660. Py_RETURN_NONE;
  1661. }
  1662. return odictiter_new((PyODictObject *)dv->dv_dict,
  1663. _odict_ITER_VALUES);
  1664. }
  1665. static PyObject *
  1666. odictvalues_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
  1667. {
  1668. if (dv->dv_dict == NULL) {
  1669. Py_RETURN_NONE;
  1670. }
  1671. return odictiter_new((PyODictObject *)dv->dv_dict,
  1672. _odict_ITER_VALUES|_odict_ITER_REVERSED);
  1673. }
  1674. static PyMethodDef odictvalues_methods[] = {
  1675. {"__reversed__", (PyCFunction)odictvalues_reversed, METH_NOARGS, NULL},
  1676. {NULL, NULL} /* sentinel */
  1677. };
  1678. PyTypeObject PyODictValues_Type = {
  1679. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  1680. "odict_values", /* tp_name */
  1681. 0, /* tp_basicsize */
  1682. 0, /* tp_itemsize */
  1683. 0, /* tp_dealloc */
  1684. 0, /* tp_vectorcall_offset */
  1685. 0, /* tp_getattr */
  1686. 0, /* tp_setattr */
  1687. 0, /* tp_as_async */
  1688. 0, /* tp_repr */
  1689. 0, /* tp_as_number */
  1690. 0, /* tp_as_sequence */
  1691. 0, /* tp_as_mapping */
  1692. 0, /* tp_hash */
  1693. 0, /* tp_call */
  1694. 0, /* tp_str */
  1695. 0, /* tp_getattro */
  1696. 0, /* tp_setattro */
  1697. 0, /* tp_as_buffer */
  1698. 0, /* tp_flags */
  1699. 0, /* tp_doc */
  1700. 0, /* tp_traverse */
  1701. 0, /* tp_clear */
  1702. 0, /* tp_richcompare */
  1703. 0, /* tp_weaklistoffset */
  1704. (getiterfunc)odictvalues_iter, /* tp_iter */
  1705. 0, /* tp_iternext */
  1706. odictvalues_methods, /* tp_methods */
  1707. 0, /* tp_members */
  1708. 0, /* tp_getset */
  1709. &PyDictValues_Type, /* tp_base */
  1710. };
  1711. static PyObject *
  1712. odictvalues_new(PyObject *od, PyObject *Py_UNUSED(ignored))
  1713. {
  1714. return _PyDictView_New(od, &PyODictValues_Type);
  1715. }
  1716. /* ----------------------------------------------
  1717. MutableMapping implementations
  1718. Mapping:
  1719. ============ ===========
  1720. method uses
  1721. ============ ===========
  1722. __contains__ __getitem__
  1723. __eq__ items
  1724. __getitem__ +
  1725. __iter__ +
  1726. __len__ +
  1727. __ne__ __eq__
  1728. get __getitem__
  1729. items ItemsView
  1730. keys KeysView
  1731. values ValuesView
  1732. ============ ===========
  1733. ItemsView uses __len__, __iter__, and __getitem__.
  1734. KeysView uses __len__, __iter__, and __contains__.
  1735. ValuesView uses __len__, __iter__, and __getitem__.
  1736. MutableMapping:
  1737. ============ ===========
  1738. method uses
  1739. ============ ===========
  1740. __delitem__ +
  1741. __setitem__ +
  1742. clear popitem
  1743. pop __getitem__
  1744. __delitem__
  1745. popitem __iter__
  1746. _getitem__
  1747. __delitem__
  1748. setdefault __getitem__
  1749. __setitem__
  1750. update __setitem__
  1751. ============ ===========
  1752. */
  1753. static int
  1754. mutablemapping_add_pairs(PyObject *self, PyObject *pairs)
  1755. {
  1756. PyObject *pair, *iterator, *unexpected;
  1757. int res = 0;
  1758. iterator = PyObject_GetIter(pairs);
  1759. if (iterator == NULL)
  1760. return -1;
  1761. PyErr_Clear();
  1762. while ((pair = PyIter_Next(iterator)) != NULL) {
  1763. /* could be more efficient (see UNPACK_SEQUENCE in ceval.c) */
  1764. PyObject *key = NULL, *value = NULL;
  1765. PyObject *pair_iterator = PyObject_GetIter(pair);
  1766. if (pair_iterator == NULL)
  1767. goto Done;
  1768. key = PyIter_Next(pair_iterator);
  1769. if (key == NULL) {
  1770. if (!PyErr_Occurred())
  1771. PyErr_SetString(PyExc_ValueError,
  1772. "need more than 0 values to unpack");
  1773. goto Done;
  1774. }
  1775. value = PyIter_Next(pair_iterator);
  1776. if (value == NULL) {
  1777. if (!PyErr_Occurred())
  1778. PyErr_SetString(PyExc_ValueError,
  1779. "need more than 1 value to unpack");
  1780. goto Done;
  1781. }
  1782. unexpected = PyIter_Next(pair_iterator);
  1783. if (unexpected != NULL) {
  1784. Py_DECREF(unexpected);
  1785. PyErr_SetString(PyExc_ValueError,
  1786. "too many values to unpack (expected 2)");
  1787. goto Done;
  1788. }
  1789. else if (PyErr_Occurred())
  1790. goto Done;
  1791. res = PyObject_SetItem(self, key, value);
  1792. Done:
  1793. Py_DECREF(pair);
  1794. Py_XDECREF(pair_iterator);
  1795. Py_XDECREF(key);
  1796. Py_XDECREF(value);
  1797. if (PyErr_Occurred())
  1798. break;
  1799. }
  1800. Py_DECREF(iterator);
  1801. if (res < 0 || PyErr_Occurred() != NULL)
  1802. return -1;
  1803. else
  1804. return 0;
  1805. }
  1806. static int
  1807. mutablemapping_update_arg(PyObject *self, PyObject *arg)
  1808. {
  1809. int res = 0;
  1810. if (PyDict_CheckExact(arg)) {
  1811. PyObject *items = PyDict_Items(arg);
  1812. if (items == NULL) {
  1813. return -1;
  1814. }
  1815. res = mutablemapping_add_pairs(self, items);
  1816. Py_DECREF(items);
  1817. return res;
  1818. }
  1819. PyObject *func;
  1820. if (_PyObject_LookupAttr(arg, &_Py_ID(keys), &func) < 0) {
  1821. return -1;
  1822. }
  1823. if (func != NULL) {
  1824. PyObject *keys = _PyObject_CallNoArgs(func);
  1825. Py_DECREF(func);
  1826. if (keys == NULL) {
  1827. return -1;
  1828. }
  1829. PyObject *iterator = PyObject_GetIter(keys);
  1830. Py_DECREF(keys);
  1831. if (iterator == NULL) {
  1832. return -1;
  1833. }
  1834. PyObject *key;
  1835. while (res == 0 && (key = PyIter_Next(iterator))) {
  1836. PyObject *value = PyObject_GetItem(arg, key);
  1837. if (value != NULL) {
  1838. res = PyObject_SetItem(self, key, value);
  1839. Py_DECREF(value);
  1840. }
  1841. else {
  1842. res = -1;
  1843. }
  1844. Py_DECREF(key);
  1845. }
  1846. Py_DECREF(iterator);
  1847. if (res != 0 || PyErr_Occurred()) {
  1848. return -1;
  1849. }
  1850. return 0;
  1851. }
  1852. if (_PyObject_LookupAttr(arg, &_Py_ID(items), &func) < 0) {
  1853. return -1;
  1854. }
  1855. if (func != NULL) {
  1856. PyObject *items = _PyObject_CallNoArgs(func);
  1857. Py_DECREF(func);
  1858. if (items == NULL) {
  1859. return -1;
  1860. }
  1861. res = mutablemapping_add_pairs(self, items);
  1862. Py_DECREF(items);
  1863. return res;
  1864. }
  1865. res = mutablemapping_add_pairs(self, arg);
  1866. return res;
  1867. }
  1868. static PyObject *
  1869. mutablemapping_update(PyObject *self, PyObject *args, PyObject *kwargs)
  1870. {
  1871. int res;
  1872. /* first handle args, if any */
  1873. assert(args == NULL || PyTuple_Check(args));
  1874. Py_ssize_t len = (args != NULL) ? PyTuple_GET_SIZE(args) : 0;
  1875. if (len > 1) {
  1876. const char *msg = "update() takes at most 1 positional argument (%zd given)";
  1877. PyErr_Format(PyExc_TypeError, msg, len);
  1878. return NULL;
  1879. }
  1880. if (len) {
  1881. PyObject *other = PyTuple_GET_ITEM(args, 0); /* borrowed reference */
  1882. assert(other != NULL);
  1883. Py_INCREF(other);
  1884. res = mutablemapping_update_arg(self, other);
  1885. Py_DECREF(other);
  1886. if (res < 0) {
  1887. return NULL;
  1888. }
  1889. }
  1890. /* now handle kwargs */
  1891. assert(kwargs == NULL || PyDict_Check(kwargs));
  1892. if (kwargs != NULL && PyDict_GET_SIZE(kwargs)) {
  1893. PyObject *items = PyDict_Items(kwargs);
  1894. if (items == NULL)
  1895. return NULL;
  1896. res = mutablemapping_add_pairs(self, items);
  1897. Py_DECREF(items);
  1898. if (res == -1)
  1899. return NULL;
  1900. }
  1901. Py_RETURN_NONE;
  1902. }