ordereddict.c 140 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705
  1. /* Ordered Dictionary object implementation using a hash table and a vector of
  2. pointers to the items.
  3. */
  4. /*
  5. This file has been directly derived from and retains many algorithms from
  6. objectdict.c in the Python 2.5.1 source distribution. Its licensing therefore
  7. is governed by the license as distributed with Python 2.5.1 available in the
  8. file LICNESE in the source distribution of ordereddict
  9. Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Python Software
  10. Foundation; All Rights Reserved"
  11. Copyrigh (c) 2007-10-13 onwards: Anthon van der Neut
  12. */
  13. /*
  14. Ordering by key insertion order (KIO) instead of key/val insertion order
  15. (KVIO) is less expensive (as the list of keys does not have to be updated).
  16. */
  17. #include "Python.h"
  18. #include "ordereddict.h"
  19. #if PY_VERSION_HEX < 0x03000000
  20. #define PyUNISTR_Object PyStringObject
  21. #define PyUNISTR_Concat PyString_Concat
  22. #define PyUNISTR_ConcatAndDel PyString_ConcatAndDel
  23. #define PyUNISTR_CheckExact PyString_CheckExact
  24. #define PyUNISTR_FromString PyString_FromString
  25. #define PyUNISTR_FromFormat PyString_FromFormat
  26. #define PyUNISTR_Join _PyString_Join
  27. #define PyUNISTR_Eq _PyString_Eq
  28. #define Py_hash_t long
  29. #define Py_hash_ssize_t Py_ssize_t
  30. #define OB_HASH ob_shash
  31. #else
  32. /* Return 1 if two unicode objects are equal, 0 if not.
  33. * unicode_eq() is called when the hash of two unicode objects is equal.
  34. */
  35. #if PY_VERSION_HEX < 0x03030000
  36. Py_LOCAL_INLINE(int)
  37. unicode_eq(PyObject *aa, PyObject *bb)
  38. {
  39. register PyUnicodeObject *a = (PyUnicodeObject *)aa;
  40. register PyUnicodeObject *b = (PyUnicodeObject *)bb;
  41. if (a->length != b->length)
  42. return 0;
  43. if (a->length == 0)
  44. return 1;
  45. if (a->str[0] != b->str[0])
  46. return 0;
  47. if (a->length == 1)
  48. return 1;
  49. return memcmp(a->str, b->str, a->length * sizeof(Py_UNICODE)) == 0;
  50. }
  51. #else
  52. Py_LOCAL_INLINE(int)
  53. unicode_eq(PyObject *aa, PyObject *bb)
  54. {
  55. register PyUnicodeObject *a = (PyUnicodeObject *)aa;
  56. register PyUnicodeObject *b = (PyUnicodeObject *)bb;
  57. if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) {
  58. assert(0 && "unicode_eq ready fail");
  59. return 0;
  60. }
  61. if (PyUnicode_GET_LENGTH(a) != PyUnicode_GET_LENGTH(b))
  62. return 0;
  63. if (PyUnicode_GET_LENGTH(a) == 0)
  64. return 1;
  65. if (PyUnicode_KIND(a) != PyUnicode_KIND(b))
  66. return 0;
  67. return memcmp(PyUnicode_1BYTE_DATA(a), PyUnicode_1BYTE_DATA(b),
  68. PyUnicode_GET_LENGTH(a) * PyUnicode_KIND(a)) == 0;
  69. }
  70. #endif
  71. #if PY_VERSION_HEX < 0x03030000
  72. #define PyUNISTR_Object PyUnicodeObject
  73. #else
  74. #define PyUNISTR_Object PyASCIIObject
  75. #endif
  76. #define PyUNISTR_Concat PyUnicode_Append
  77. #define PyUNISTR_ConcatAndDel PyUnicode_AppendAndDel
  78. #define PyUNISTR_CheckExact PyUnicode_CheckExact
  79. #define PyUNISTR_FromString PyUnicode_FromString
  80. #define PyUNISTR_FromFormat PyUnicode_FromFormat
  81. #define PyUNISTR_Join PyUnicode_Join
  82. #define PyUNISTR_Eq unicode_eq
  83. #define Py_hash_ssize_t Py_hash_t
  84. #define OB_HASH hash
  85. #endif
  86. #if PY_VERSION_HEX < 0x02050000
  87. #define SPR "%d"
  88. #else
  89. #define SPR "%ld"
  90. #endif
  91. #if PY_VERSION_HEX < 0x02080000
  92. #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
  93. #endif
  94. #ifdef NDEBUG
  95. #undef NDEBUG
  96. #endif
  97. #define DEFERRED_ADDRESS(ADDR) 0
  98. /* Set a key error with the specified argument, wrapping it in a
  99. * tuple automatically so that tuple keys are not unpacked as the
  100. * exception arguments. */
  101. static void
  102. set_key_error(PyObject *arg)
  103. {
  104. PyObject *tup;
  105. tup = PyTuple_Pack(1, arg);
  106. if (!tup)
  107. return; /* caller will expect error to be set anyway */
  108. PyErr_SetObject(PyExc_KeyError, tup);
  109. Py_DECREF(tup);
  110. }
  111. /* Define this out if you don't want conversion statistics on exit. */
  112. #undef SHOW_CONVERSION_COUNTS
  113. /* See large comment block below. This must be >= 1. */
  114. #define PERTURB_SHIFT 5
  115. /*
  116. see object/dictobject.c for subtilities of the base dict implementation
  117. */
  118. /* Object used as dummy key to fill deleted entries */
  119. static PyObject *dummy = NULL; /* Initialized by first call to newPyDictObject() */
  120. #ifdef Py_REF_DEBUG
  121. PyObject *
  122. _PyOrderedDict_Dummy(void)
  123. {
  124. return dummy;
  125. }
  126. #endif
  127. /* relaxed: allow init etc. of ordereddict from dicts if true */
  128. static int ordereddict_relaxed = 0;
  129. /* Key Value Insertion Order: rearrange at end on update if true */
  130. static int ordereddict_kvio = 0;
  131. /* forward declarations */
  132. static PyOrderedDictEntry *
  133. lookdict_string(PyOrderedDictObject *mp, PyObject *key, Py_hash_t hash);
  134. int PyOrderedDict_CopySome(PyObject *a, PyObject *b,
  135. Py_ssize_t start, Py_ssize_t step,
  136. Py_ssize_t count, int override);
  137. #ifdef SHOW_CONVERSION_COUNTS
  138. static long created = 0L;
  139. static long converted = 0L;
  140. static void
  141. show_counts(void)
  142. {
  143. fprintf(stderr, "created %ld string dicts\n", created);
  144. fprintf(stderr, "converted %ld to normal dicts\n", converted);
  145. fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
  146. }
  147. #endif
  148. /* Debug statistic to compare allocations with reuse through the free list */
  149. #undef SHOW_ALLOC_COUNT
  150. #ifdef SHOW_ALLOC_COUNT
  151. static size_t count_alloc = 0;
  152. static size_t count_reuse = 0;
  153. static void
  154. show_alloc(void)
  155. {
  156. fprintf(stderr, "Dict allocations: %" PY_FORMAT_SIZE_T "d\n",
  157. count_alloc);
  158. fprintf(stderr, "Dict reuse through freelist: %" PY_FORMAT_SIZE_T
  159. "d\n", count_reuse);
  160. fprintf(stderr, "%.2f%% reuse rate\n\n",
  161. (100.0*count_reuse/(count_alloc+count_reuse)));
  162. }
  163. #endif
  164. /* Initialization macros.
  165. There are two ways to create a dict: PyOrderedDict_New() is the main C API
  166. function, and the tp_new slot maps to dict_new(). In the latter case we
  167. can save a little time over what PyOrderedDict_New does because it's guaranteed
  168. that the PyOrderedDictObject struct is already zeroed out.
  169. Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
  170. an excellent reason not to).
  171. */
  172. #define INIT_NONZERO_DICT_SLOTS(mp) do { \
  173. (mp)->ma_table = (mp)->ma_smalltable; \
  174. (mp)->od_otablep = (mp)->ma_smallotablep; \
  175. (mp)->ma_mask = PyOrderedDict_MINSIZE - 1; \
  176. } while(0)
  177. #define EMPTY_TO_MINSIZE(mp) do { \
  178. memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
  179. memset((mp)->ma_smallotablep, 0, sizeof((mp)->ma_smallotablep)); \
  180. (mp)->ma_used = (mp)->od_fill = (mp)->od_state = 0; \
  181. INIT_NONZERO_DICT_SLOTS(mp); \
  182. } while(0)
  183. /* (mp)->od_cmp = (mp)->od_key = NULL; \*/
  184. #define INIT_SORT_FUNCS(SD) do { \
  185. SD->sd_cmp = Py_None; Py_INCREF(Py_None); \
  186. SD->sd_key = Py_None; Py_INCREF(Py_None); \
  187. SD->sd_value = Py_None; Py_INCREF(Py_None); \
  188. } while(0)
  189. #define OD_KVIO_BIT (1<<0)
  190. #define OD_RELAXED_BIT (1<<1)
  191. #define OD_REVERSE_BIT (1<<2)
  192. #define KVIO(mp) (mp->od_state & OD_KVIO_BIT)
  193. #define RELAXED(mp) (mp->od_state & OD_RELAXED_BIT)
  194. #define REVERSE(mp) (mp->od_state & OD_REVERSE_BIT)
  195. /* Dictionary reuse scheme to save calls to malloc, free, and memset */
  196. #ifndef PyDict_MAXFREELIST
  197. #define PyDict_MAXFREELIST 80
  198. #endif
  199. static PyOrderedDictObject *free_list[PyDict_MAXFREELIST];
  200. static int numfree = 0;
  201. void
  202. PyOrderedDict_Fini(void)
  203. {
  204. PyOrderedDictObject *op;
  205. while (numfree) {
  206. op = free_list[--numfree];
  207. assert(PyOrderedDict_CheckExact(op));
  208. PyObject_GC_Del(op);
  209. }
  210. }
  211. PyObject *
  212. PyOrderedDict_New(void)
  213. {
  214. register PyOrderedDictObject *mp;
  215. assert(dummy != NULL); /* initialisation in the module init */
  216. #ifdef SHOW_CONVERSION_COUNTS
  217. Py_AtExit(show_counts);
  218. #endif
  219. #ifdef SHOW_ALLOC_COUNT
  220. Py_AtExit(show_alloc);
  221. #endif
  222. if (numfree) {
  223. mp = free_list[--numfree];
  224. assert (mp != NULL);
  225. assert (Py_TYPE(mp) == &PyOrderedDict_Type);
  226. _Py_NewReference((PyObject *)mp);
  227. if (mp->od_fill) {
  228. EMPTY_TO_MINSIZE(mp);
  229. } else {
  230. /* At least set ma_table and ma_mask; these are wrong
  231. if an empty but presized dict is added to freelist */
  232. INIT_NONZERO_DICT_SLOTS(mp);
  233. }
  234. assert (mp->ma_used == 0);
  235. assert (mp->ma_table == mp->ma_smalltable);
  236. assert (mp->od_otablep == mp->ma_smallotablep);
  237. assert (mp->ma_mask == PyOrderedDict_MINSIZE - 1);
  238. #ifdef SHOW_ALLOC_COUNT
  239. count_reuse++;
  240. #endif
  241. } else {
  242. mp = PyObject_GC_New(PyOrderedDictObject, &PyOrderedDict_Type);
  243. if (mp == NULL)
  244. return NULL;
  245. EMPTY_TO_MINSIZE(mp);
  246. #ifdef SHOW_ALLOC_COUNT
  247. count_alloc++;
  248. #endif
  249. }
  250. mp->ma_lookup = lookdict_string;
  251. #ifdef SHOW_CONVERSION_COUNTS
  252. ++created;
  253. #endif
  254. PyObject_GC_Track(mp);
  255. return (PyObject *)mp;
  256. }
  257. PyObject *
  258. PySortedDict_New(void)
  259. {
  260. register PyOrderedDictObject *mp;
  261. register PySortedDictObject *sd;
  262. assert(dummy != NULL);
  263. mp = (PyOrderedDictObject *) PyObject_GC_New(PySortedDictObject, &PySortedDict_Type);
  264. if (mp == NULL)
  265. return NULL;
  266. EMPTY_TO_MINSIZE(mp);
  267. mp->ma_lookup = lookdict_string;
  268. sd = (PySortedDictObject*)mp;
  269. INIT_SORT_FUNCS(sd);
  270. #ifdef SHOW_CONVERSION_COUNTS
  271. ++created;
  272. #endif
  273. PyObject_GC_Track(mp);
  274. return (PyObject *)mp;
  275. }
  276. /*
  277. The basic lookup function used by all operations.
  278. This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
  279. Open addressing is preferred over chaining since the link overhead for
  280. chaining would be substantial (100% with typical malloc overhead).
  281. The initial probe index is computed as hash mod the table size. Subsequent
  282. probe indices are computed as explained earlier.
  283. All arithmetic on hash should ignore overflow.
  284. (The details in this version are due to Tim Peters, building on many past
  285. contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
  286. Christian Tismer).
  287. lookdict() is general-purpose, and may return NULL if (and only if) a
  288. comparison raises an exception (this was new in Python 2.5).
  289. lookdict_string() below is specialized to string keys, comparison of which can
  290. never raise an exception; that function can never return NULL. For both, when
  291. the key isn't found a PyOrderedDictEntry* is returned for which the me_value field is
  292. NULL; this is the slot in the dict at which the key would have been found, and
  293. the caller can (if it wishes) add the <key, value> pair to the returned
  294. PyOrderedDictEntry *.
  295. */
  296. static PyOrderedDictEntry *
  297. lookdict(PyOrderedDictObject *mp, PyObject *key, register Py_hash_t hash)
  298. {
  299. register size_t i;
  300. register size_t perturb;
  301. register PyOrderedDictEntry *freeslot;
  302. register size_t mask = (size_t)mp->ma_mask;
  303. PyOrderedDictEntry *ep0 = mp->ma_table;
  304. register PyOrderedDictEntry *ep;
  305. register int cmp;
  306. PyObject *startkey;
  307. i = (size_t)hash & mask;
  308. ep = &ep0[i];
  309. if (ep->me_key == NULL || ep->me_key == key)
  310. return ep;
  311. if (ep->me_key == dummy)
  312. freeslot = ep;
  313. else {
  314. if (ep->me_hash == hash) {
  315. startkey = ep->me_key;
  316. Py_INCREF(startkey);
  317. cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
  318. Py_DECREF(startkey);
  319. if (cmp < 0)
  320. return NULL;
  321. if (ep0 == mp->ma_table && ep->me_key == startkey) {
  322. if (cmp > 0)
  323. return ep;
  324. } else {
  325. /* The compare did major nasty stuff to the
  326. * dict: start over.
  327. * XXX A clever adversary could prevent this
  328. * XXX from terminating.
  329. */
  330. return lookdict(mp, key, hash);
  331. }
  332. }
  333. freeslot = NULL;
  334. }
  335. /* In the loop, me_key == dummy is by far (factor of 100s) the
  336. least likely outcome, so test for that last. */
  337. for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
  338. i = (i << 2) + i + perturb + 1;
  339. ep = &ep0[i & mask];
  340. if (ep->me_key == NULL)
  341. return freeslot == NULL ? ep : freeslot;
  342. if (ep->me_key == key)
  343. return ep;
  344. if (ep->me_hash == hash && ep->me_key != dummy) {
  345. startkey = ep->me_key;
  346. Py_INCREF(startkey);
  347. cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
  348. Py_DECREF(startkey);
  349. if (cmp < 0)
  350. return NULL;
  351. if (ep0 == mp->ma_table && ep->me_key == startkey) {
  352. if (cmp > 0)
  353. return ep;
  354. } else {
  355. /* The compare did major nasty stuff to the
  356. * dict: start over.
  357. * XXX A clever adversary could prevent this
  358. * XXX from terminating.
  359. */
  360. return lookdict(mp, key, hash);
  361. }
  362. } else if (ep->me_key == dummy && freeslot == NULL)
  363. freeslot = ep;
  364. }
  365. assert(0); /* NOT REACHED */
  366. return 0;
  367. }
  368. /*
  369. * Hacked up version of lookdict which can assume keys are always strings;
  370. * this assumption allows testing for errors during PyObject_RichCompareBool()
  371. * to be dropped; string-string comparisons never raise exceptions. This also
  372. * means we don't need to go through PyObject_RichCompareBool(); we can always
  373. * use PyUNISTR_Eq() directly.
  374. *
  375. * This is valuable because dicts with only string keys are very common.
  376. */
  377. static PyOrderedDictEntry *
  378. lookdict_string(PyOrderedDictObject *mp, PyObject *key, register Py_hash_t hash)
  379. {
  380. register size_t i;
  381. register size_t perturb;
  382. register PyOrderedDictEntry *freeslot;
  383. register size_t mask = (size_t)mp->ma_mask;
  384. PyOrderedDictEntry *ep0 = mp->ma_table;
  385. register PyOrderedDictEntry *ep;
  386. /* Make sure this function doesn't have to handle non-string keys,
  387. including subclasses of str; e.g., one reason to subclass
  388. strings is to override __eq__, and for speed we don't cater to
  389. that here. */
  390. if (!PyUNISTR_CheckExact(key)) {
  391. #ifdef SHOW_CONVERSION_COUNTS
  392. ++converted;
  393. #endif
  394. mp->ma_lookup = lookdict;
  395. return lookdict(mp, key, hash);
  396. }
  397. i = hash & mask;
  398. ep = &ep0[i];
  399. if (ep->me_key == NULL || ep->me_key == key)
  400. return ep;
  401. if (ep->me_key == dummy)
  402. freeslot = ep;
  403. else {
  404. if (ep->me_hash == hash && PyUNISTR_Eq(ep->me_key, key))
  405. return ep;
  406. freeslot = NULL;
  407. }
  408. /* In the loop, me_key == dummy is by far (factor of 100s) the
  409. least likely outcome, so test for that last. */
  410. for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
  411. i = (i << 2) + i + perturb + 1;
  412. ep = &ep0[i & mask];
  413. if (ep->me_key == NULL)
  414. return freeslot == NULL ? ep : freeslot;
  415. if (ep->me_key == key
  416. || (ep->me_hash == hash
  417. && ep->me_key != dummy
  418. && PyUNISTR_Eq(ep->me_key, key)))
  419. return ep;
  420. if (ep->me_key == dummy && freeslot == NULL)
  421. freeslot = ep;
  422. }
  423. assert(0); /* NOT REACHED */
  424. return 0;
  425. }
  426. static int
  427. dump_ordereddict_head(register PyOrderedDictObject *mp)
  428. {
  429. if (mp == NULL) {
  430. printf("ordereddict header printing received NULL");
  431. return -1;
  432. }
  433. if (PySortedDict_CheckExact(mp))
  434. printf("sorteddict");
  435. else
  436. printf("ordereddict");
  437. printf(": fill " SPR ", ", mp->od_fill);
  438. printf("used " SPR ", ", mp->ma_used);
  439. printf("mask " SPR ", ", mp->ma_mask);
  440. printf("mask " SPR ", ", mp->ma_mask);
  441. printf("\nbits: ");
  442. if (KVIO(mp))
  443. printf("kvio ");
  444. if (RELAXED(mp))
  445. printf("relax ");
  446. if (REVERSE(mp))
  447. printf("reverse ");
  448. printf("\n");
  449. return 0;
  450. }
  451. static void
  452. dump_sorteddict_fun(register PySortedDictObject *mp)
  453. {
  454. printf("cmp %p, key %p, value %p\n", mp->sd_cmp, mp->sd_key, mp->sd_value);
  455. }
  456. static void
  457. dump_otablep(register PyOrderedDictObject *mp)
  458. {
  459. Py_ssize_t index;
  460. PyOrderedDictEntry **p;
  461. printf("mp %p\n", mp);
  462. for (index = 0, p = mp->od_otablep; index < mp->ma_used; index++, p++) {
  463. printf("index " SPR " %p %p\n", index, p, *p);
  464. }
  465. }
  466. /*
  467. https://github.com/pbrady/fastcache/issues/32
  468. mentions no tracking with GC_TRACK in extensions
  469. */
  470. /* #if (PY_VERSION_HEX < 0x02070000) */
  471. #if 1
  472. #define MAINTAIN_TRACKING(mp, key, value)
  473. #define _PyDict_MaybeUntrack(x)
  474. #else
  475. #ifdef SHOW_TRACK_COUNT
  476. #define INCREASE_TRACK_COUNT \
  477. (count_tracked++, count_untracked--);
  478. #define DECREASE_TRACK_COUNT \
  479. (count_tracked--, count_untracked++);
  480. #else
  481. #define INCREASE_TRACK_COUNT
  482. #define DECREASE_TRACK_COUNT
  483. #endif
  484. #define MAINTAIN_TRACKING(mp, key, value) \
  485. do { \
  486. if (!_PyObject_GC_IS_TRACKED(mp)) { \
  487. if (_PyObject_GC_MAY_BE_TRACKED(key) || \
  488. _PyObject_GC_MAY_BE_TRACKED(value)) { \
  489. _PyObject_GC_TRACK(mp); \
  490. INCREASE_TRACK_COUNT \
  491. } \
  492. } \
  493. } while(0)
  494. PyAPI_FUNC(void)
  495. _PyOrderedDict_MaybeUntrack(PyObject *op)
  496. {
  497. PyDictObject *mp;
  498. PyObject *value;
  499. Py_ssize_t mask, i;
  500. PyDictEntry *ep;
  501. if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
  502. return;
  503. mp = (PyDictObject *) op;
  504. ep = mp->ma_table;
  505. mask = mp->ma_mask;
  506. for (i = 0; i <= mask; i++) {
  507. if ((value = ep[i].me_value) == NULL)
  508. continue;
  509. if (_PyObject_GC_MAY_BE_TRACKED(value) ||
  510. _PyObject_GC_MAY_BE_TRACKED(ep[i].me_key))
  511. return;
  512. }
  513. DECREASE_TRACK_COUNT
  514. _PyObject_GC_UNTRACK(op);
  515. }
  516. #endif
  517. /*
  518. Internal routine to insert a new item into the table when you have entry object.
  519. Used by insertdict.
  520. */
  521. static int
  522. insertdict_by_entry(register PyOrderedDictObject *mp, PyObject *key, Py_hash_t hash,
  523. PyOrderedDictEntry *ep, PyObject *value, Py_ssize_t index)
  524. {
  525. PyObject *old_value;
  526. Py_ssize_t oindex;
  527. register PyOrderedDictEntry **epp = NULL;
  528. MAINTAIN_TRACKING(mp, key, value);
  529. if (ep->me_value != NULL) { /* updating a value */
  530. old_value = ep->me_value;
  531. ep->me_value = value;
  532. if (index != -1) {
  533. if (index == -2) /* kvio */
  534. index = mp->ma_used-1;
  535. for (oindex = 0, epp = mp->od_otablep; oindex < mp->ma_used;
  536. oindex++, epp++)
  537. if (*epp == ep)
  538. break;
  539. /* epp now points to item and oindex is its index (optimize?) */
  540. /* if index == oindex we don't have to anything */
  541. if (index < oindex) {
  542. epp = mp->od_otablep;
  543. epp += index;
  544. memmove(epp + 1, epp, (oindex - index) * sizeof(PyOrderedDictEntry *));
  545. *epp = ep;
  546. } else if ((index == oindex + 1) && (index == mp->ma_used)) {
  547. /* nothing to do for inserting beyond last with same key */
  548. } else if (index > oindex) {
  549. /*
  550. printf("moving %d %d %p\n", index, oindex, epp);
  551. dump_otablep(mp); */
  552. memmove(epp, epp + 1, (index - oindex) * sizeof(PyOrderedDictEntry *));
  553. mp->od_otablep[index] = ep;
  554. /*
  555. dump_otablep(mp);
  556. */
  557. }
  558. }
  559. Py_DECREF(old_value); /* which **CAN** re-enter */
  560. Py_DECREF(key);
  561. } else { /* new value */
  562. if (ep->me_key == NULL)
  563. mp->od_fill++;
  564. else {
  565. assert(ep->me_key == dummy);
  566. Py_DECREF(dummy);
  567. }
  568. ep->me_key = key;
  569. ep->me_hash = (Py_ssize_t)hash;
  570. ep->me_value = value;
  571. if (index < 0)
  572. mp->od_otablep[mp->ma_used] = ep;
  573. else {
  574. epp = mp->od_otablep;
  575. epp += index;
  576. /* make space */
  577. memmove(epp + 1, epp, (mp->ma_used - index) * sizeof(PyOrderedDictEntry *));
  578. *epp = ep;
  579. }
  580. mp->ma_used++;
  581. }
  582. return 0;
  583. }
  584. /*
  585. Internal routine to insert a new item into the table.
  586. Used both by the internal resize routine and by the public insert routine.
  587. Eats a reference to key and one to value.
  588. Returns -1 if an error occurred, or 0 on success.
  589. */
  590. static int
  591. insertdict(register PyOrderedDictObject *mp, PyObject *key, Py_hash_t hash,
  592. PyObject *value, Py_ssize_t index)
  593. {
  594. register PyOrderedDictEntry *ep;
  595. assert(mp->ma_lookup != NULL);
  596. ep = mp->ma_lookup(mp, key, hash);
  597. if (ep == NULL) {
  598. Py_DECREF(key);
  599. Py_DECREF(value);
  600. return -1;
  601. }
  602. return insertdict_by_entry(mp, key, hash, ep, value, index);
  603. }
  604. /*
  605. Internal routine to insert a new item into the table when you have entry object.
  606. Used by insertdict.
  607. */
  608. static int
  609. insertsorteddict_by_entry(register PyOrderedDictObject *mp, PyObject *key, Py_hash_t hash,
  610. PyOrderedDictEntry *ep, PyObject *value)
  611. {
  612. PyObject *old_value;
  613. Py_ssize_t index = 0, lower, upper;
  614. int res;
  615. register PySortedDictObject *sd = (PySortedDictObject *) mp;
  616. register PyOrderedDictEntry **epp = NULL;
  617. MAINTAIN_TRACKING(mp, key, value);
  618. if (ep->me_value != NULL) { /* updating a value */
  619. old_value = ep->me_value;
  620. ep->me_value = value;
  621. Py_DECREF(old_value); /* which **CAN** re-enter */
  622. Py_DECREF(key);
  623. if (sd->sd_value != Py_None || sd->sd_cmp != Py_None) {
  624. PyErr_SetString(PyExc_NotImplementedError,
  625. "updating a value for a cmp/value sorted dict not implemented"
  626. );
  627. return -1;
  628. }
  629. } else { /* new value */
  630. if (ep->me_key == NULL)
  631. mp->od_fill++;
  632. else {
  633. assert(ep->me_key == dummy);
  634. Py_DECREF(dummy);
  635. }
  636. ep->me_key = key;
  637. ep->me_hash = (Py_ssize_t)hash;
  638. ep->me_value = value;
  639. /* determine epp */
  640. epp = mp->od_otablep;
  641. lower = 0;
  642. upper = mp->ma_used;
  643. if (sd->sd_key != Py_None && sd->sd_key != Py_True) {
  644. PyObject *transkey;
  645. PyObject *chkkey;
  646. transkey = PyObject_CallFunctionObjArgs(sd->sd_key, key, NULL);
  647. if (transkey == NULL)
  648. transkey = key;
  649. while (lower < upper) {
  650. index = (lower+upper) / 2;
  651. chkkey = PyObject_CallFunctionObjArgs(sd->sd_key,(epp[index])->me_key, NULL);
  652. if (chkkey == NULL)
  653. chkkey = (epp[index])->me_key;
  654. res = PyObject_RichCompareBool(chkkey, transkey, Py_GT);
  655. if (res == 0)
  656. lower = index + 1;
  657. else if (res == 1)
  658. upper = index;
  659. else
  660. return -1; /* res was -1 -> error */
  661. }
  662. } else {
  663. while (lower < upper) {
  664. index = (lower+upper) / 2;
  665. res = PyObject_RichCompareBool((epp[index])->me_key, key, Py_GT);
  666. if (res == 0)
  667. lower = index + 1;
  668. else if (res == 1)
  669. upper = index;
  670. else
  671. return -1; /* res was -1 -> error */
  672. }
  673. }
  674. epp += lower;
  675. /* make space */
  676. memmove(epp + 1, epp, (mp->ma_used - lower) * sizeof(PyOrderedDictEntry *));
  677. *epp = ep;
  678. mp->ma_used++;
  679. }
  680. return 0;
  681. }
  682. static int
  683. insertsorteddict(register PyOrderedDictObject *mp, PyObject *key, Py_hash_t hash,
  684. PyObject *value)
  685. {
  686. register PyOrderedDictEntry *ep;
  687. /* printf("insert sorted dict\n"); */
  688. assert(mp->ma_lookup != NULL);
  689. ep = mp->ma_lookup(mp, key, hash);
  690. if (ep == NULL) {
  691. Py_DECREF(key);
  692. Py_DECREF(value);
  693. return -1;
  694. }
  695. return insertsorteddict_by_entry(mp, key, hash, ep, value);
  696. }
  697. /*
  698. Internal routine used by dictresize() to insert an item which is
  699. known to be absent from the dict. This routine also assumes that
  700. the dict contains no deleted entries. Besides the performance benefit,
  701. using insertdict() in dictresize() is dangerous (SF bug #1456209).
  702. Note that no refcounts are changed by this routine; if needed, the caller
  703. is responsible for incref'ing `key` and `value`.
  704. */
  705. static void
  706. insertdict_clean(register PyOrderedDictObject *mp, PyObject *key, Py_hash_t hash,
  707. PyObject *value)
  708. {
  709. register size_t i;
  710. register size_t perturb;
  711. register size_t mask = (size_t)mp->ma_mask;
  712. PyOrderedDictEntry *ep0 = mp->ma_table;
  713. register PyOrderedDictEntry *ep;
  714. MAINTAIN_TRACKING(mp, key, value);
  715. i = hash & mask;
  716. ep = &ep0[i];
  717. for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
  718. i = (i << 2) + i + perturb + 1;
  719. ep = &ep0[i & mask];
  720. }
  721. assert(ep->me_value == NULL);
  722. mp->od_fill++;
  723. ep->me_key = key;
  724. ep->me_hash = (Py_ssize_t)hash;
  725. ep->me_value = value;
  726. mp->od_otablep[mp->ma_used] = ep;
  727. mp->ma_used++;
  728. }
  729. /*
  730. Restructure the table by allocating a new table and reinserting all
  731. items again. When entries have been deleted, the new table may
  732. actually be smaller than the old one.
  733. */
  734. static int
  735. dictresize(PyOrderedDictObject *mp, Py_ssize_t minused)
  736. {
  737. Py_ssize_t newsize;
  738. PyOrderedDictEntry *oldtable, *newtable, *ep, **epp;
  739. PyOrderedDictEntry **oldotablep, **newotablep;
  740. register Py_ssize_t i, j;
  741. int is_oldtable_malloced;
  742. int reusing_smalltable;
  743. PyOrderedDictEntry small_copy[PyOrderedDict_MINSIZE];
  744. PyOrderedDictEntry *small_ocopyp[PyOrderedDict_MINSIZE];
  745. assert(minused >= 0);
  746. /* Find the smallest table size > minused. */
  747. for (newsize = PyOrderedDict_MINSIZE;
  748. newsize <= minused && newsize > 0;
  749. newsize <<= 1)
  750. ;
  751. if (newsize <= 0) {
  752. PyErr_NoMemory();
  753. return -1;
  754. }
  755. /* Get space for a new table. */
  756. oldtable = mp->ma_table;
  757. oldotablep = mp->od_otablep;
  758. assert(oldtable != NULL);
  759. assert(oldotablep != NULL);
  760. is_oldtable_malloced = oldtable != mp->ma_smalltable;
  761. reusing_smalltable = 0;
  762. if (newsize == PyOrderedDict_MINSIZE) {
  763. /* A large table is shrinking, or we can't get any smaller. */
  764. newtable = mp->ma_smalltable;
  765. newotablep = mp->ma_smallotablep;
  766. if (newtable == oldtable) {
  767. if (mp->od_fill == mp->ma_used) {
  768. /* No dummies, so no point doing anything. */
  769. return 0;
  770. }
  771. /* We're not going to resize it, but rebuild the
  772. table anyway to purge old dummy entries.
  773. Subtle: This is *necessary* if fill==size,
  774. as lookdict needs at least one virgin slot to
  775. terminate failing searches. If fill < size, it's
  776. merely desirable, as dummies slow searches. */
  777. assert(mp->od_fill > mp->ma_used);
  778. memcpy(small_copy, oldtable, sizeof(small_copy));
  779. /* Small_ocopyp must point into small_copy */
  780. for (i = 0; i < PyOrderedDict_MINSIZE; i++) {
  781. small_ocopyp[i] = oldotablep[i] ? &small_copy[oldotablep[i]-&oldtable[0]]: NULL;
  782. }
  783. oldtable = small_copy;
  784. reusing_smalltable = 1;
  785. }
  786. } else {
  787. newtable = PyMem_NEW(PyOrderedDictEntry, newsize);
  788. if (newtable == NULL) {
  789. PyErr_NoMemory();
  790. return -1;
  791. }
  792. newotablep = PyMem_NEW(PyOrderedDictEntry*, newsize);
  793. if (newotablep == NULL) {
  794. PyErr_NoMemory();
  795. return -1;
  796. }
  797. }
  798. /* Make the dict empty, using the new table. */
  799. assert(newtable != oldtable);
  800. assert(newotablep != oldotablep);
  801. mp->ma_table = newtable;
  802. mp->od_otablep = newotablep;
  803. mp->ma_mask = newsize - 1;
  804. memset(newtable, 0, sizeof(PyOrderedDictEntry) * newsize);
  805. memcpy(newotablep, oldotablep, sizeof(PyOrderedDictEntry *) * mp->ma_used);
  806. epp = mp->od_otablep;
  807. j = mp->ma_used;
  808. mp->ma_used = 0;
  809. i = mp->od_fill;
  810. mp->od_fill = 0;
  811. /* Copy the data over; this is refcount-neutral for active entries;
  812. dummy entries aren't copied over, of course */
  813. for (epp = reusing_smalltable ? small_ocopyp: mp->od_otablep; j > 0; epp++, j--) {
  814. insertdict_clean(mp, (*epp)->me_key, (long)(*epp)->me_hash,
  815. (*epp)->me_value);
  816. }
  817. for (ep = oldtable; i > 0; ep++) {
  818. if (ep->me_value != NULL) { /* active entry */
  819. --i;
  820. } else if (ep->me_key != NULL) { /* dummy entry */
  821. --i;
  822. assert(ep->me_key == dummy);
  823. Py_DECREF(ep->me_key);
  824. }
  825. /* else key == value == NULL: nothing to do */
  826. }
  827. if (is_oldtable_malloced) {
  828. PyMem_DEL(oldtable);
  829. PyMem_DEL(oldotablep);
  830. }
  831. return 0;
  832. }
  833. /* Create a new dictionary pre-sized to hold an estimated number of elements.
  834. Underestimates are okay because the dictionary will resize as necessary.
  835. Overestimates just mean the dictionary will be more sparse than usual.
  836. */
  837. PyAPI_FUNC(PyObject *)
  838. _PyOrderedDict_NewPresized(Py_ssize_t minused)
  839. {
  840. PyObject *op = PyOrderedDict_New();
  841. if (minused>5 && op != NULL && dictresize((PyOrderedDictObject *)op, minused) == -1) {
  842. Py_DECREF(op);
  843. return NULL;
  844. }
  845. return op;
  846. }
  847. /* Note that, for historical reasons, PyOrderedDict_GetItem() suppresses all errors
  848. * that may occur (originally dicts supported only string keys, and exceptions
  849. * weren't possible). So, while the original intent was that a NULL return
  850. * meant the key wasn't present, in reality it can mean that, or that an error
  851. * (suppressed) occurred while computing the key's hash, or that some error
  852. * (suppressed) occurred when comparing keys in the dict's internal probe
  853. * sequence. A nasty example of the latter is when a Python-coded comparison
  854. * function hits a stack-depth error, which can cause this to return NULL
  855. * even if the key is present.
  856. */
  857. PyObject *
  858. PyOrderedDict_GetItem(PyObject *op, PyObject *key)
  859. {
  860. Py_hash_t hash;
  861. PyOrderedDictObject *mp = (PyOrderedDictObject *)op;
  862. PyOrderedDictEntry *ep;
  863. PyThreadState *tstate;
  864. if (!PyOrderedDict_Check(op))
  865. return NULL;
  866. if (!PyUNISTR_CheckExact(key) ||
  867. (hash = ((PyUNISTR_Object *) key)->OB_HASH) == -1) {
  868. hash = PyObject_Hash(key);
  869. if (hash == -1) {
  870. PyErr_Clear();
  871. return NULL;
  872. }
  873. }
  874. /* We can arrive here with a NULL tstate during initialization: try
  875. running "python -Wi" for an example related to string interning.
  876. Let's just hope that no exception occurs then... This must be
  877. _PyThreadState_Current and not PyThreadState_GET() because in debug
  878. mode, the latter complains if tstate is NULL. */
  879. #if PY_VERSION_HEX < 0x03000000
  880. tstate = _PyThreadState_Current;
  881. #else
  882. tstate = (PyThreadState*)_Py_atomic_load_relaxed(
  883. &_PyThreadState_Current);
  884. #endif
  885. if (tstate != NULL && tstate->curexc_type != NULL) {
  886. /* preserve the existing exception */
  887. PyObject *err_type, *err_value, *err_tb;
  888. PyErr_Fetch(&err_type, &err_value, &err_tb);
  889. ep = (mp->ma_lookup)(mp, key, hash);
  890. /* ignore errors */
  891. PyErr_Restore(err_type, err_value, err_tb);
  892. if (ep == NULL)
  893. return NULL;
  894. } else {
  895. ep = (mp->ma_lookup)(mp, key, hash);
  896. if (ep == NULL) {
  897. PyErr_Clear();
  898. return NULL;
  899. }
  900. }
  901. return ep->me_value;
  902. }
  903. static int
  904. dict_set_item_by_hash_or_entry(register PyObject *op, PyObject *key,
  905. Py_hash_t hash, PyOrderedDictEntry *ep, PyObject *value)
  906. {
  907. register PyOrderedDictObject *mp;
  908. register Py_ssize_t n_used;
  909. mp = (PyOrderedDictObject *)op;
  910. assert(mp->od_fill <= mp->ma_mask); /* at least one empty slot */
  911. n_used = mp->ma_used;
  912. Py_INCREF(value);
  913. Py_INCREF(key);
  914. #if PY_MAJOR_VERSION < 3
  915. if (PySortedDict_Check(op)) {
  916. #else
  917. if (PySortedDict_CheckExact(op)) {
  918. #endif
  919. if (insertsorteddict(mp, key, hash, value) != 0)
  920. return -1;
  921. } else if (insertdict(mp, key, hash, value, KVIO(mp) ? -2: -1) != 0)
  922. return -1;
  923. /* If we added a key, we can safely resize. Otherwise just return!
  924. * If fill >= 2/3 size, adjust size. Normally, this doubles or
  925. * quaduples the size, but it's also possible for the dict to shrink
  926. * (if od_fill is much larger than ma_used, meaning a lot of dict
  927. * keys have been * deleted).
  928. *
  929. * Quadrupling the size improves average dictionary sparseness
  930. * (reducing collisions) at the cost of some memory and iteration
  931. * speed (which loops over every possible entry). It also halves
  932. * the number of expensive resize operations in a growing dictionary.
  933. *
  934. * Very large dictionaries (over 50K items) use doubling instead.
  935. * This may help applications with severe memory constraints.
  936. */
  937. if (!(mp->ma_used > n_used && mp->od_fill*3 >= (mp->ma_mask+1)*2))
  938. return 0;
  939. return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
  940. }
  941. /* CAUTION: PyOrderedDict_SetItem() must guarantee that it won't resize the
  942. * dictionary if it's merely replacing the value for an existing key.
  943. * This means that it's safe to loop over a dictionary with PyOrderedDict_Next()
  944. * and occasionally replace a value -- but you can't insert new keys or
  945. * remove them.
  946. * This does never hold for kvio
  947. */
  948. int
  949. PyOrderedDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
  950. {
  951. register Py_hash_t hash;
  952. if (!PyOrderedDict_Check(op)) {
  953. PyErr_BadInternalCall();
  954. return -1;
  955. }
  956. assert(key);
  957. assert(value);
  958. if (PyUNISTR_CheckExact(key)) {
  959. hash = ((PyUNISTR_Object *)key)->OB_HASH;
  960. if (hash == -1)
  961. hash = PyObject_Hash(key);
  962. } else {
  963. hash = PyObject_Hash(key);
  964. if (hash == -1)
  965. return -1;
  966. }
  967. return dict_set_item_by_hash_or_entry(op, key, hash, NULL, value);
  968. }
  969. int
  970. PyOrderedDict_InsertItem(register PyOrderedDictObject *mp, Py_ssize_t index,
  971. PyObject *key, PyObject *value)
  972. {
  973. register Py_hash_t hash;
  974. register Py_ssize_t n_used;
  975. #if PY_MAJOR_VERSION < 3
  976. if (PySortedDict_Check(mp)) {
  977. #else
  978. if (PySortedDict_CheckExact(mp)) {
  979. #endif
  980. PyErr_SetString(PyExc_TypeError,
  981. "sorteddict does not support insert()");
  982. return -1;
  983. }
  984. if (!PyOrderedDict_Check(mp)) {
  985. PyErr_BadInternalCall();
  986. return -1;
  987. }
  988. assert(key);
  989. assert(value);
  990. if (index < 0)
  991. index += mp->ma_used;
  992. /* test to see if index is in range */
  993. if (index > mp->ma_used)
  994. index = mp->ma_used;
  995. else if (index < 0)
  996. index = 0;
  997. if (PyUNISTR_CheckExact(key)) {
  998. hash = ((PyUNISTR_Object *)key)->OB_HASH;
  999. if (hash == -1)
  1000. hash = PyObject_Hash(key);
  1001. } else {
  1002. hash = PyObject_Hash(key);
  1003. if (hash == -1)
  1004. return -1;
  1005. }
  1006. assert(mp->od_fill <= mp->ma_mask); /* at least one empty slot */
  1007. n_used = mp->ma_used;
  1008. Py_INCREF(value);
  1009. Py_INCREF(key);
  1010. if (insertdict(mp, key, hash, value, index) != 0)
  1011. return -1;
  1012. /* If we added a key, we can safely resize. Otherwise just return!
  1013. * If fill >= 2/3 size, adjust size. Normally, this doubles or
  1014. * quaduples the size, but it's also possible for the dict to shrink
  1015. * (if od_fill is much larger than ma_used, meaning a lot of dict
  1016. * keys have been * deleted).
  1017. *
  1018. * Quadrupling the size improves average dictionary sparseness
  1019. * (reducing collisions) at the cost of some memory and iteration
  1020. * speed (which loops over every possible entry). It also halves
  1021. * the number of expensive resize operations in a growing dictionary.
  1022. *
  1023. * Very large dictionaries (over 50K items) use doubling instead.
  1024. * This may help applications with severe memory constraints.
  1025. */
  1026. if (!(mp->ma_used > n_used && mp->od_fill*3 >= (mp->ma_mask+1)*2))
  1027. return 0;
  1028. return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
  1029. }
  1030. static int
  1031. del_inorder(PyOrderedDictObject *op, PyOrderedDictEntry* ep)
  1032. {
  1033. register Py_ssize_t count = op->ma_used;
  1034. PyOrderedDictEntry **tmp = op->od_otablep;
  1035. while (count--) {
  1036. if (*tmp == ep) {
  1037. memmove(tmp, tmp+1, count * sizeof(PyOrderedDictEntry *));
  1038. return 1;
  1039. }
  1040. tmp++;
  1041. }
  1042. return 0; /* not found */
  1043. }
  1044. int
  1045. PyOrderedDict_DelItem(PyObject *op, PyObject *key)
  1046. {
  1047. register PyOrderedDictObject *mp;
  1048. register Py_hash_t hash;
  1049. register PyOrderedDictEntry *ep;
  1050. PyObject *old_value, *old_key;
  1051. if (!PyOrderedDict_Check(op)) {
  1052. PyErr_BadInternalCall();
  1053. return -1;
  1054. }
  1055. assert(key);
  1056. if (!PyUNISTR_CheckExact(key) ||
  1057. (hash = ((PyUNISTR_Object *) key)->OB_HASH) == -1) {
  1058. hash = PyObject_Hash(key);
  1059. if (hash == -1)
  1060. return -1;
  1061. }
  1062. mp = (PyOrderedDictObject *)op;
  1063. ep = (mp->ma_lookup)(mp, key, hash);
  1064. /* at this point we have to move all the entries beyond the one found
  1065. back on space (this could be optimised by deferring) */
  1066. del_inorder(mp, ep);
  1067. if (ep == NULL)
  1068. return -1;
  1069. if (ep->me_value == NULL) {
  1070. set_key_error(key);
  1071. return -1;
  1072. }
  1073. old_key = ep->me_key;
  1074. assert(ep->me_key);
  1075. Py_INCREF(dummy);
  1076. ep->me_key = dummy;
  1077. old_value = ep->me_value;
  1078. ep->me_value = NULL;
  1079. mp->ma_used--;
  1080. Py_DECREF(old_value);
  1081. Py_DECREF(old_key);
  1082. return 0;
  1083. }
  1084. void
  1085. PyOrderedDict_Clear(PyObject *op)
  1086. {
  1087. PyOrderedDictObject *mp;
  1088. PyOrderedDictEntry *ep, *table, **otablep;
  1089. int table_is_malloced;
  1090. Py_ssize_t fill;
  1091. PyOrderedDictEntry small_copy[PyOrderedDict_MINSIZE];
  1092. #ifdef Py_DEBUG
  1093. Py_ssize_t i, n;
  1094. #endif
  1095. if (!PyOrderedDict_Check(op))
  1096. return;
  1097. mp = (PyOrderedDictObject *)op;
  1098. #ifdef Py_DEBUG
  1099. n = mp->ma_mask + 1;
  1100. i = 0;
  1101. #endif
  1102. table = mp->ma_table;
  1103. otablep = mp->od_otablep;
  1104. assert(table != NULL);
  1105. assert(otablep != NULL);
  1106. table_is_malloced = table != mp->ma_smalltable;
  1107. /* This is delicate. During the process of clearing the dict,
  1108. * decrefs can cause the dict to mutate. To avoid fatal confusion
  1109. * (voice of experience), we have to make the dict empty before
  1110. * clearing the slots, and never refer to anything via mp->xxx while
  1111. * clearing.
  1112. */
  1113. fill = mp->od_fill;
  1114. if (table_is_malloced)
  1115. EMPTY_TO_MINSIZE(mp);
  1116. else if (fill > 0) {
  1117. /* It's a small table with something that needs to be cleared.
  1118. * Afraid the only safe way is to copy the dict entries into
  1119. * another small table first.
  1120. */
  1121. memcpy(small_copy, table, sizeof(small_copy));
  1122. table = small_copy;
  1123. EMPTY_TO_MINSIZE(mp);
  1124. }
  1125. /* else it's a small table that's already empty */
  1126. /* Now we can finally clear things. If C had refcounts, we could
  1127. * assert that the refcount on table is 1 now, i.e. that this function
  1128. * has unique access to it, so decref side-effects can't alter it.
  1129. */
  1130. for (ep = table; fill > 0; ++ep) {
  1131. #ifdef Py_DEBUG
  1132. assert(i < n);
  1133. ++i;
  1134. #endif
  1135. if (ep->me_key) {
  1136. --fill;
  1137. Py_DECREF(ep->me_key);
  1138. Py_XDECREF(ep->me_value);
  1139. }
  1140. #ifdef Py_DEBUG
  1141. else
  1142. assert(ep->me_value == NULL);
  1143. #endif
  1144. }
  1145. if (table_is_malloced) {
  1146. PyMem_DEL(table);
  1147. PyMem_DEL(otablep);
  1148. }
  1149. }
  1150. /*
  1151. * Iterate over a dict. Use like so:
  1152. *
  1153. * Py_ssize_t i;
  1154. * PyObject *key, *value;
  1155. * i = 0; # important! i should not otherwise be changed by you
  1156. * while (PyOrderedDict_Next(yourdict, &i, &key, &value)) {
  1157. * Refer to borrowed references in key and value.
  1158. * }
  1159. *
  1160. * CAUTION: In general, it isn't safe to use PyOrderedDict_Next in a loop that
  1161. * mutates the dict. One exception: it is safe if the loop merely changes
  1162. * the values associated with the keys (but doesn't insert new keys or
  1163. * delete keys), via PyOrderedDict_SetItem().
  1164. */
  1165. int
  1166. PyOrderedDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
  1167. {
  1168. register Py_ssize_t i;
  1169. register PyOrderedDictEntry **epp;
  1170. if (!PyOrderedDict_Check(op) && !PySortedDict_Check(op))
  1171. return 0;
  1172. i = *ppos;
  1173. if (i < 0)
  1174. return 0;
  1175. /* review: not sure why different from 2.5.1 here. */
  1176. if (i >= ((PyOrderedDictObject *)op)->ma_used)
  1177. return 0;
  1178. *ppos = i+1;
  1179. epp = ((PyOrderedDictObject *)op)->od_otablep;
  1180. if (pkey)
  1181. *pkey = epp[i]->me_key;
  1182. if (pvalue)
  1183. *pvalue = epp[i]->me_value;
  1184. return 1;
  1185. }
  1186. /* Internal version of PyOrderedDict_Next that returns a hash value in addition to the key and value.*/
  1187. int
  1188. _PyOrderedDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, Py_hash_t *phash)
  1189. {
  1190. register Py_ssize_t i;
  1191. register Py_ssize_t mask;
  1192. register PyOrderedDictEntry *ep;
  1193. if (!PyOrderedDict_Check(op))
  1194. return 0;
  1195. i = *ppos;
  1196. if (i < 0)
  1197. return 0;
  1198. ep = ((PyOrderedDictObject *)op)->ma_table;
  1199. mask = ((PyOrderedDictObject *)op)->ma_mask;
  1200. while (i <= mask && ep[i].me_value == NULL)
  1201. i++;
  1202. *ppos = i+1;
  1203. if (i > mask)
  1204. return 0;
  1205. *phash = (long)(ep[i].me_hash);
  1206. if (pkey)
  1207. *pkey = ep[i].me_key;
  1208. if (pvalue)
  1209. *pvalue = ep[i].me_value;
  1210. return 1;
  1211. }
  1212. /* Methods */
  1213. static void
  1214. dict_dealloc(register PyOrderedDictObject *mp)
  1215. {
  1216. register PyOrderedDictEntry *ep;
  1217. Py_ssize_t fill = mp->od_fill;
  1218. PyObject_GC_UnTrack(mp);
  1219. Py_TRASHCAN_SAFE_BEGIN(mp)
  1220. for (ep = mp->ma_table; fill > 0; ep++) {
  1221. if (ep->me_key) {
  1222. --fill;
  1223. Py_DECREF(ep->me_key);
  1224. Py_XDECREF(ep->me_value);
  1225. }
  1226. }
  1227. if (mp->ma_table != mp->ma_smalltable) {
  1228. PyMem_DEL(mp->ma_table);
  1229. PyMem_DEL(mp->od_otablep);
  1230. }
  1231. if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyOrderedDict_Type)
  1232. free_list[numfree++] = mp;
  1233. else
  1234. Py_TYPE(mp)->tp_free((PyObject *)mp);
  1235. Py_TRASHCAN_SAFE_END(mp)
  1236. }
  1237. #if PY_MAJOR_VERSION < 3
  1238. static int
  1239. ordereddict_print(register PyOrderedDictObject *mp, register FILE *fp, register int flags)
  1240. {
  1241. register Py_ssize_t i;
  1242. register Py_ssize_t any;
  1243. char *typestr = "ordered";
  1244. int status;
  1245. PyOrderedDictEntry **epp;
  1246. if (PySortedDict_CheckExact(mp))
  1247. typestr = "sorted";
  1248. status = Py_ReprEnter((PyObject*)mp);
  1249. if (status != 0) {
  1250. if (status < 0)
  1251. return status;
  1252. Py_BEGIN_ALLOW_THREADS
  1253. fprintf(fp, "%sdict([...])", typestr);
  1254. Py_END_ALLOW_THREADS
  1255. return 0;
  1256. }
  1257. Py_BEGIN_ALLOW_THREADS
  1258. fprintf(fp, "%sdict([", typestr);
  1259. Py_END_ALLOW_THREADS
  1260. any = 0;
  1261. epp = mp->od_otablep;
  1262. for (i = 0; i < mp->ma_used; i++) {
  1263. PyObject *pvalue = (*epp)->me_value;
  1264. /* Prevent PyObject_Repr from deleting value during
  1265. key format */
  1266. Py_INCREF(pvalue);
  1267. if (any++ > 0)
  1268. Py_BEGIN_ALLOW_THREADS
  1269. fprintf(fp, ", ");
  1270. Py_END_ALLOW_THREADS
  1271. Py_BEGIN_ALLOW_THREADS
  1272. fprintf(fp, "(");
  1273. Py_END_ALLOW_THREADS
  1274. if (PyObject_Print((PyObject *)((*epp)->me_key), fp, 0)!=0) {
  1275. Py_DECREF(pvalue);
  1276. Py_ReprLeave((PyObject*)mp);
  1277. return -1;
  1278. }
  1279. Py_BEGIN_ALLOW_THREADS
  1280. fprintf(fp, ", ");
  1281. Py_END_ALLOW_THREADS
  1282. if (PyObject_Print(pvalue, fp, 0) != 0) {
  1283. Py_DECREF(pvalue);
  1284. Py_ReprLeave((PyObject*)mp);
  1285. return -1;
  1286. }
  1287. Py_DECREF(pvalue);
  1288. Py_BEGIN_ALLOW_THREADS
  1289. fprintf(fp, ")");
  1290. Py_END_ALLOW_THREADS
  1291. epp++;
  1292. }
  1293. Py_BEGIN_ALLOW_THREADS
  1294. fprintf(fp, "])");
  1295. Py_END_ALLOW_THREADS
  1296. Py_ReprLeave((PyObject*)mp);
  1297. return 0;
  1298. }
  1299. #endif
  1300. static PyObject *
  1301. basedict_repr(PyOrderedDictObject *mp, char *typestr)
  1302. {
  1303. Py_ssize_t i;
  1304. PyObject *s, *temp, *comma = NULL, *rightpar = NULL;
  1305. PyObject *pieces = NULL, *result = NULL;
  1306. PyObject *key, *value;
  1307. /* char *typestr = "ordered"; */
  1308. /* if (PySortedDict_CheckExact(mp))*/
  1309. /*
  1310. #if PY_MAJOR_VERSION < 3
  1311. if (PySortedDict_Check(mp))
  1312. #else
  1313. if (PySortedDict_Check(mp))
  1314. #endif
  1315. typestr = "sorted";
  1316. */
  1317. i = Py_ReprEnter((PyObject *)mp);
  1318. if (i != 0) {
  1319. return i > 0 ? PyUNISTR_FromFormat("%sdict([...])", typestr) : NULL;
  1320. }
  1321. if (mp->ma_used == 0) {
  1322. result = PyUNISTR_FromFormat("%sdict([])", typestr);
  1323. goto Done;
  1324. }
  1325. pieces = PyList_New(0);
  1326. if (pieces == NULL)
  1327. goto Done;
  1328. comma = PyUNISTR_FromString(", ");
  1329. if (comma == NULL)
  1330. goto Done;
  1331. rightpar = PyUNISTR_FromString(")");
  1332. if (rightpar == NULL)
  1333. goto Done;
  1334. /* Do repr() on each key+value pair, and insert ": " between them.
  1335. Note that repr may mutate the dict. */
  1336. i = 0;
  1337. while (PyOrderedDict_Next((PyObject *)mp, &i, &key, &value)) {
  1338. int status;
  1339. /* Prevent repr from deleting value during key format. */
  1340. Py_INCREF(value);
  1341. s = PyUNISTR_FromString("(");
  1342. PyUNISTR_ConcatAndDel(&s, PyObject_Repr(key));
  1343. PyUNISTR_Concat(&s, comma);
  1344. PyUNISTR_ConcatAndDel(&s, PyObject_Repr(value));
  1345. Py_DECREF(value);
  1346. PyUNISTR_Concat(&s, rightpar);
  1347. if (s == NULL)
  1348. goto Done;
  1349. status = PyList_Append(pieces, s);
  1350. Py_DECREF(s); /* append created a new ref */
  1351. if (status < 0)
  1352. goto Done;
  1353. }
  1354. /* Add "[]" decorations to the first and last items. */
  1355. assert(PyList_GET_SIZE(pieces) > 0);
  1356. s = PyUNISTR_FromFormat("%sdict([", typestr);
  1357. if (s == NULL)
  1358. goto Done;
  1359. temp = PyList_GET_ITEM(pieces, 0);
  1360. PyUNISTR_ConcatAndDel(&s, temp);
  1361. PyList_SET_ITEM(pieces, 0, s);
  1362. if (s == NULL)
  1363. goto Done;
  1364. s = PyUNISTR_FromString("])");
  1365. if (s == NULL)
  1366. goto Done;
  1367. temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
  1368. PyUNISTR_ConcatAndDel(&temp, s);
  1369. PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
  1370. if (temp == NULL)
  1371. goto Done;
  1372. /* Paste them all together with ", " between. */
  1373. result = PyUNISTR_Join(comma, pieces);
  1374. Done:
  1375. Py_XDECREF(pieces);
  1376. Py_XDECREF(comma);
  1377. Py_XDECREF(rightpar);
  1378. Py_ReprLeave((PyObject *)mp);
  1379. return result;
  1380. }
  1381. static PyObject *
  1382. ordereddict_repr(PyOrderedDictObject *mp)
  1383. {
  1384. return basedict_repr(mp, "ordered");
  1385. }
  1386. static PyObject *
  1387. sorteddict_repr(PySortedDictObject *mp)
  1388. {
  1389. return basedict_repr((PyOrderedDictObject *)mp, "sorted");
  1390. }
  1391. static Py_ssize_t
  1392. dict_length(PyOrderedDictObject *mp)
  1393. {
  1394. return mp->ma_used;
  1395. }
  1396. static PyObject *
  1397. dict_subscript(PyOrderedDictObject *mp, register PyObject *key)
  1398. {
  1399. PyObject *v;
  1400. Py_hash_t hash;
  1401. PyOrderedDictEntry *ep;
  1402. if (PySlice_Check(key)) {
  1403. Py_ssize_t start, stop, step, slicelength;
  1404. PyObject* result;
  1405. if (PySlice_GetIndicesEx(
  1406. #if PY_VERSION_HEX < 0x03000000
  1407. (PySliceObject*)
  1408. #endif
  1409. key, mp->ma_used,
  1410. &start, &stop, &step, &slicelength) < 0) {
  1411. return NULL;
  1412. }
  1413. result = PyOrderedDict_New();
  1414. if (!result) return NULL;
  1415. if (slicelength <= 0) return result;
  1416. if (PyOrderedDict_CopySome(result, (PyObject *) mp, start, step, slicelength, 1) == 0)
  1417. return result;
  1418. Py_DECREF(result);
  1419. return NULL;
  1420. }
  1421. assert(mp->ma_table != NULL);
  1422. if (!PyUNISTR_CheckExact(key) ||
  1423. (hash = ((PyUNISTR_Object *) key)->OB_HASH) == -1) {
  1424. hash = PyObject_Hash(key);
  1425. if (hash == -1)
  1426. return NULL;
  1427. }
  1428. ep = (mp->ma_lookup)(mp, key, hash);
  1429. if (ep == NULL)
  1430. return NULL;
  1431. v = ep->me_value;
  1432. if (v == NULL) {
  1433. if (!PyOrderedDict_CheckExact(mp) && !PySortedDict_CheckExact(mp)) {
  1434. /* Look up __missing__ method if we're a subclass. */
  1435. #if PY_VERSION_HEX < 0x02070000
  1436. PyObject *missing;
  1437. static PyObject *missing_str = NULL;
  1438. if (missing_str == NULL)
  1439. missing_str =
  1440. PyString_InternFromString("__missing__");
  1441. missing = _PyType_Lookup(Py_TYPE(mp), missing_str);
  1442. if (missing != NULL)
  1443. return PyObject_CallFunctionObjArgs(missing,
  1444. (PyObject *)mp, key, NULL);
  1445. #else
  1446. PyObject *missing, *res;
  1447. static PyObject *missing_str = NULL;
  1448. missing = _PyObject_LookupSpecial((PyObject *)mp,
  1449. "__missing__",
  1450. &missing_str);
  1451. if (missing != NULL) {
  1452. res = PyObject_CallFunctionObjArgs(missing,
  1453. key, NULL);
  1454. Py_DECREF(missing);
  1455. return res;
  1456. }
  1457. else if (PyErr_Occurred())
  1458. return NULL;
  1459. #endif
  1460. }
  1461. set_key_error(key);
  1462. return NULL;
  1463. } else
  1464. Py_INCREF(v);
  1465. return v;
  1466. }
  1467. /* a[ilow:ihigh] = v if v != NULL.
  1468. * del a[ilow:ihigh] if v == NULL.
  1469. *
  1470. * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
  1471. * guaranteed the call cannot fail.
  1472. */
  1473. static Py_ssize_t
  1474. dict_ass_slice(PyOrderedDictObject *self, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *value)
  1475. {
  1476. PyObject *recycle_on_stack[8];
  1477. PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
  1478. Py_ssize_t result = -1, i;
  1479. Py_ssize_t num_to_delete = 0, s;
  1480. PyOrderedDictEntry **epp;
  1481. if (PySortedDict_CheckExact(self)) {
  1482. PyErr_Format(PyExc_TypeError,
  1483. "sorteddict does not support slice %s", value ? "assignment" : "deletion");
  1484. return -1;
  1485. }
  1486. if (ilow < 0)
  1487. ilow = 0;
  1488. else if (ilow > self->ma_used)
  1489. ilow = self->ma_used;
  1490. if (ihigh < ilow)
  1491. ihigh = ilow;
  1492. else if (ihigh > self->ma_used)
  1493. ihigh = self->ma_used;
  1494. if (value != NULL) {
  1495. if (PyObject_Length(value) != (ihigh - ilow)) {
  1496. PyErr_SetString(PyExc_ValueError,
  1497. "slice assignment: wrong size"
  1498. );
  1499. return -1;
  1500. }
  1501. if (!PyOrderedDict_CheckExact(value)) {
  1502. PyErr_SetString(PyExc_TypeError,
  1503. "slice assignment: argument must be ordereddict"
  1504. );
  1505. return -1;
  1506. }
  1507. }
  1508. /* for now lazy implementation: first delete then insert */
  1509. #define DELETION_AND_OVERWRITING_SEPERATE 0
  1510. #if DELETION_AND_OVERWRITING_SEPERATE == 1
  1511. if (value == NULL) {
  1512. #endif
  1513. s = (ihigh - ilow) * 2 * sizeof(PyObject *);
  1514. if (s > sizeof(recycle_on_stack)) {
  1515. recycle = (PyObject **)PyMem_MALLOC(s);
  1516. if (recycle == NULL) {
  1517. PyErr_NoMemory();
  1518. goto Error;
  1519. }
  1520. }
  1521. epp = self->od_otablep;
  1522. epp += ilow;
  1523. for (i = ilow; i < ihigh; i++, epp++) {
  1524. /* AvdN: ToDo DECREF key and value */
  1525. recycle[num_to_delete++] = (*epp)->me_key;
  1526. Py_INCREF(dummy);
  1527. (*epp)->me_key = dummy;
  1528. recycle[num_to_delete++] = (*epp)->me_value;
  1529. (*epp)->me_value = NULL;
  1530. }
  1531. epp = self->od_otablep;
  1532. memmove(epp+ilow, epp+ihigh, (self->ma_used - ihigh) * sizeof(PyOrderedDictEntry *));
  1533. self->ma_used -= (ihigh - ilow);
  1534. result = 0;
  1535. #if DELETION_AND_OVERWRITING_SEPERATE == 1
  1536. } else {
  1537. /* assignment first delete slice */
  1538. /* then delete any items whose keys are in itereable that are already in */
  1539. PyErr_SetString(PyExc_NotImplementedError,
  1540. "ordered dictionary does not support slice assignment"
  1541. );
  1542. result = -1;
  1543. }
  1544. #endif
  1545. for (i = num_to_delete - 1; i >= 0; --i)
  1546. Py_XDECREF(recycle[i]);
  1547. #if DELETION_AND_OVERWRITING_SEPERATE != 1
  1548. if (value != NULL) { /* now insert */
  1549. epp = ((PyOrderedDictObject *) value)->od_otablep;
  1550. for (i = ilow; i < ihigh; i++) {
  1551. if(PyOrderedDict_InsertItem(self, i, (*epp)->me_key, (*epp)->me_value) != 0)
  1552. return -1;
  1553. epp++;
  1554. }
  1555. }
  1556. #endif
  1557. Error:
  1558. if (recycle != recycle_on_stack)
  1559. PyMem_FREE(recycle);
  1560. return result;
  1561. }
  1562. static Py_ssize_t
  1563. dict_ass_subscript(PyOrderedDictObject *self, PyObject *item, PyObject *value)
  1564. {
  1565. if (PySlice_Check(item)) {
  1566. Py_ssize_t start, stop, step, slicelength;
  1567. if (PySortedDict_CheckExact(self)) {
  1568. PyErr_Format(PyExc_TypeError,
  1569. "sorteddict does not support slice %s", value ? "assignment" : "deletion");
  1570. return -1;
  1571. }
  1572. if (PySlice_GetIndicesEx(
  1573. #if PY_VERSION_HEX < 0x03000000
  1574. (PySliceObject*)
  1575. #endif
  1576. item, self->ma_used,
  1577. &start, &stop, &step, &slicelength) < 0) {
  1578. return -1;
  1579. }
  1580. /* treat L[slice(a,b)] = v _exactly_ like L[a:b] = v */
  1581. if (step == 1 && ((PySliceObject*)item)->step == Py_None)
  1582. return dict_ass_slice(self, start, stop, value);
  1583. /* do soemthing about step == -1 ? */
  1584. if (slicelength <= 0)
  1585. return 0;
  1586. if (value == NULL) {
  1587. /* delete slice */
  1588. /* printf("Deleting %d %d %d %d %p\n", start, stop, step, slicelength, value);*/
  1589. while (slicelength--) {
  1590. /* ToDo optimize */
  1591. if (step > 0) { /* do it from the back to preserve right indices */
  1592. dict_ass_slice(self, start + slicelength * step,
  1593. start + (slicelength * step) + 1, NULL);
  1594. } else {
  1595. dict_ass_slice(self, start,
  1596. start + 1, NULL);
  1597. start += step;
  1598. }
  1599. }
  1600. return 0;
  1601. } else {
  1602. /* assign slice */
  1603. Py_ssize_t count = slicelength, start2 = start;
  1604. PyOrderedDictEntry **epp;
  1605. /* printf("Assigning %d %d %d %d %d %p\n", start, stop, step, slicelength, PyObject_Length(value), value); */
  1606. if (PyObject_Length(value) != slicelength) {
  1607. PyErr_SetString(PyExc_ValueError,
  1608. "slice assignment: wrong size"
  1609. );
  1610. return -1;
  1611. }
  1612. if (!PyOrderedDict_CheckExact(value)) {
  1613. PyErr_SetString(PyExc_TypeError,
  1614. "slice assignment: argument must be ordereddict"
  1615. );
  1616. return -1;
  1617. }
  1618. while (count--) {
  1619. /* ToDo optimize */
  1620. if (step > 0) { /* do it from the back to preserve right indices */
  1621. dict_ass_slice(self, start2 + count * step,
  1622. start2 + (count * step) + 1, NULL);
  1623. } else {
  1624. dict_ass_slice(self, start2, start2 + 1, NULL);
  1625. start2 += step;
  1626. }
  1627. }
  1628. count = slicelength;
  1629. start2 = start;
  1630. epp = ((PyOrderedDictObject *) value)->od_otablep;
  1631. if (step < 0) {
  1632. epp += slicelength;
  1633. }
  1634. while (count--) {
  1635. /* ToDo optimize */
  1636. if (step > 0) { /* do it from the front */
  1637. if(PyOrderedDict_InsertItem(self, start2, (*epp)->me_key, (*epp)->me_value) != 0)
  1638. return -1;
  1639. start2 += step;
  1640. epp++;
  1641. } else {
  1642. epp--;
  1643. if(PyOrderedDict_InsertItem(self, start2 + count * step, (*epp)->me_key, (*epp)->me_value) != 0)
  1644. return -1;
  1645. }
  1646. }
  1647. return 0;
  1648. }
  1649. }
  1650. if (value == NULL)
  1651. return PyOrderedDict_DelItem((PyObject *)self, item);
  1652. else
  1653. return PyOrderedDict_SetItem((PyObject *)self, item, value);
  1654. }
  1655. static PyMappingMethods dict_as_mapping = {
  1656. (lenfunc)dict_length, /*mp_length*/
  1657. (binaryfunc)dict_subscript, /*mp_subscript*/
  1658. (objobjargproc)dict_ass_subscript, /*mp_ass_subscript*/
  1659. };
  1660. static PyObject *
  1661. dict_keys(register PyOrderedDictObject *mp, PyObject *args, PyObject *kwds)
  1662. {
  1663. register PyObject *v;
  1664. register Py_ssize_t i;
  1665. PyOrderedDictEntry **epp;
  1666. Py_ssize_t n;
  1667. int reverse = 0;
  1668. static char *kwlist[] = {"reverse", 0};
  1669. if (args != NULL)
  1670. if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:keys",
  1671. kwlist, &reverse))
  1672. return NULL;
  1673. again:
  1674. n = mp->ma_used;
  1675. v = PyList_New(n);
  1676. if (v == NULL)
  1677. return NULL;
  1678. if (n != mp->ma_used) {
  1679. /* Durnit. The allocations caused the dict to resize.
  1680. * Just over, this shouldn't normally happen.
  1681. */
  1682. Py_DECREF(v);
  1683. goto again;
  1684. }
  1685. if (reverse) {
  1686. epp = mp->od_otablep + (n-1);
  1687. reverse = -1;
  1688. } else {
  1689. epp = mp->od_otablep;
  1690. reverse = 1;
  1691. }
  1692. for (i = 0; i < n; i++) {
  1693. PyObject *key = (*epp)->me_key;
  1694. Py_INCREF(key);
  1695. PyList_SET_ITEM(v, i, key);
  1696. epp += reverse;
  1697. }
  1698. return v;
  1699. }
  1700. static PyObject *
  1701. dict_values(register PyOrderedDictObject *mp, PyObject *args, PyObject *kwds)
  1702. {
  1703. register PyObject *v;
  1704. register Py_ssize_t i;
  1705. PyOrderedDictEntry **epp;
  1706. Py_ssize_t n;
  1707. int reverse = 0;
  1708. static char *kwlist[] = {"reverse", 0};
  1709. if (args != NULL)
  1710. if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:values",
  1711. kwlist, &reverse))
  1712. return NULL;
  1713. again:
  1714. n = mp->ma_used;
  1715. v = PyList_New(n);
  1716. if (v == NULL)
  1717. return NULL;
  1718. if (n != mp->ma_used) {
  1719. /* Durnit. The allocations caused the dict to resize.
  1720. * Just start over, this shouldn't normally happen.
  1721. */
  1722. Py_DECREF(v);
  1723. goto again;
  1724. }
  1725. if (reverse) {
  1726. epp = mp->od_otablep + (n-1);
  1727. reverse = -1;
  1728. } else {
  1729. epp = mp->od_otablep;
  1730. reverse = 1;
  1731. }
  1732. for (i = 0; i < n; i++) {
  1733. PyObject *value = (*epp)->me_value;
  1734. Py_INCREF(value);
  1735. PyList_SET_ITEM(v, i, value);
  1736. epp += reverse;
  1737. }
  1738. return v;
  1739. }
  1740. static PyObject *
  1741. dict_items(register PyOrderedDictObject *mp, PyObject *args, PyObject *kwds)
  1742. {
  1743. register PyObject *v;
  1744. register Py_ssize_t i, n;
  1745. PyObject *item, *key, *value;
  1746. PyOrderedDictEntry **epp;
  1747. int reverse = 0;
  1748. static char *kwlist[] = {"reverse", 0};
  1749. if (args != NULL)
  1750. if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:items",
  1751. kwlist, &reverse))
  1752. return NULL;
  1753. /* Preallocate the list of tuples, to avoid allocations during
  1754. * the loop over the items, which could trigger GC, which
  1755. * could resize the dict. :-(
  1756. */
  1757. again:
  1758. n = mp->ma_used;
  1759. v = PyList_New(n);
  1760. if (v == NULL)
  1761. return NULL;
  1762. for (i = 0; i < n; i++) {
  1763. item = PyTuple_New(2);
  1764. if (item == NULL) {
  1765. Py_DECREF(v);
  1766. return NULL;
  1767. }
  1768. PyList_SET_ITEM(v, i, item);
  1769. }
  1770. if (n != mp->ma_used) {
  1771. /* Durnit. The allocations caused the dict to resize.
  1772. * Just start over, this shouldn't normally happen.
  1773. */
  1774. Py_DECREF(v);
  1775. goto again;
  1776. }
  1777. /* Nothing we do below makes any function calls. */
  1778. if (reverse) {
  1779. epp = mp->od_otablep + (n-1);
  1780. reverse = -1;
  1781. } else {
  1782. epp = mp->od_otablep;
  1783. reverse = 1;
  1784. }
  1785. for (i = 0; i < n; i++) {
  1786. key = (*epp)->me_key;
  1787. value = (*epp)->me_value;
  1788. item = PyList_GET_ITEM(v, i);
  1789. Py_INCREF(key);
  1790. PyTuple_SET_ITEM(item, 0, key);
  1791. Py_INCREF(value);
  1792. PyTuple_SET_ITEM(item, 1, value);
  1793. epp += reverse;
  1794. }
  1795. return v;
  1796. }
  1797. static PyObject *
  1798. dict_fromkeys(PyObject *cls, PyObject *args)
  1799. {
  1800. PyObject *seq;
  1801. PyObject *value = Py_None;
  1802. PyObject *it; /* iter(seq) */
  1803. PyObject *key;
  1804. PyObject *d;
  1805. int status;
  1806. if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
  1807. return NULL;
  1808. d = PyObject_CallObject(cls, NULL);
  1809. if (d == NULL)
  1810. return NULL;
  1811. if ((PyOrderedDict_CheckExact(d) || PySortedDict_CheckExact(d)) && ((PyDictObject *)d)->ma_used == 0) {
  1812. if (PyAnySet_CheckExact(seq)) {
  1813. PyOrderedDictObject *mp = (PyOrderedDictObject *)d;
  1814. Py_ssize_t pos = 0;
  1815. PyObject *key;
  1816. Py_hash_t hash;
  1817. if (dictresize(mp, PySet_GET_SIZE(seq))) {
  1818. Py_DECREF(d);
  1819. return NULL;
  1820. }
  1821. while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
  1822. Py_INCREF(key);
  1823. Py_INCREF(value);
  1824. if (insertdict(mp, key, hash, value, -1)) {
  1825. Py_DECREF(d);
  1826. return NULL;
  1827. }
  1828. }
  1829. return d;
  1830. }
  1831. }
  1832. it = PyObject_GetIter(seq);
  1833. if (it == NULL) {
  1834. Py_DECREF(d);
  1835. return NULL;
  1836. }
  1837. #ifndef OLD
  1838. if (PyOrderedDict_CheckExact(d) || PySortedDict_CheckExact(d)) {
  1839. while ((key = PyIter_Next(it)) != NULL) {
  1840. status = PyOrderedDict_SetItem(d, key, value);
  1841. Py_DECREF(key);
  1842. if (status < 0)
  1843. goto Fail;
  1844. }
  1845. } else {
  1846. while ((key = PyIter_Next(it)) != NULL) {
  1847. status = PyObject_SetItem(d, key, value);
  1848. Py_DECREF(key);
  1849. if (status < 0)
  1850. goto Fail;
  1851. }
  1852. }
  1853. if (PyErr_Occurred())
  1854. goto Fail;
  1855. #else
  1856. for (;;) {
  1857. key = PyIter_Next(it);
  1858. if (key == NULL) {
  1859. if (PyErr_Occurred())
  1860. goto Fail;
  1861. break;
  1862. }
  1863. status = PyObject_SetItem(d, key, value);
  1864. Py_DECREF(key);
  1865. if (status < 0)
  1866. goto Fail;
  1867. }
  1868. #endif
  1869. Py_DECREF(it);
  1870. return d;
  1871. Fail:
  1872. Py_DECREF(it);
  1873. Py_DECREF(d);
  1874. return NULL;
  1875. }
  1876. /* called by init, update and setitems */
  1877. static int
  1878. dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *args_name)
  1879. {
  1880. PyObject *arg = NULL;
  1881. int result = 0, tmprelax = 0;
  1882. static char *kwlist[] = {"src", "relax", 0};
  1883. if (args != NULL)
  1884. if (!PyArg_ParseTupleAndKeywords(args, kwds, args_name,
  1885. kwlist, &arg, &tmprelax))
  1886. return -1;
  1887. if (arg != NULL) {
  1888. if (PyObject_HasAttrString(arg, "keys"))
  1889. result = PyOrderedDict_Merge(self, arg, 1, tmprelax);
  1890. else
  1891. result = PyOrderedDict_MergeFromSeq2(self, arg, 1);
  1892. }
  1893. /* do not initialise from keywords at all */
  1894. /* if (result == 0 && kwds != NULL)
  1895. result = PyOrderedDict_Merge(self, kwds, 1); */
  1896. return result;
  1897. }
  1898. static PyObject *
  1899. dict_update(PyObject *self, PyObject *args, PyObject *kwds)
  1900. {
  1901. if (dict_update_common(self, args, kwds, "|Oi:update") != -1)
  1902. Py_RETURN_NONE;
  1903. return NULL;
  1904. }
  1905. /* Update unconditionally replaces existing items.
  1906. Merge has a 3rd argument 'override'; if set, it acts like Update,
  1907. otherwise it leaves existing items unchanged.
  1908. PyOrderedDict_{Update,Merge} update/merge from a mapping object.
  1909. PyOrderedDict_MergeFromSeq2 updates/merges from any iterable object
  1910. producing iterable objects of length 2.
  1911. */
  1912. int
  1913. PyOrderedDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
  1914. {
  1915. PyObject *it; /* iter(seq2) */
  1916. Py_ssize_t i; /* index into seq2 of current element */
  1917. PyObject *item; /* seq2[i] */
  1918. PyObject *fast; /* item as a 2-tuple or 2-list */
  1919. assert(d != NULL);
  1920. assert(PyOrderedDict_Check(d));
  1921. assert(seq2 != NULL);
  1922. it = PyObject_GetIter(seq2);
  1923. if (it == NULL)
  1924. return -1;
  1925. for (i = 0; ; ++i) {
  1926. PyObject *key, *value;
  1927. Py_ssize_t n;
  1928. fast = NULL;
  1929. item = PyIter_Next(it);
  1930. if (item == NULL) {
  1931. if (PyErr_Occurred())
  1932. goto Fail;
  1933. break;
  1934. }
  1935. /* Convert item to sequence, and verify length 2. */
  1936. fast = PySequence_Fast(item, "");
  1937. if (fast == NULL) {
  1938. if (PyErr_ExceptionMatches(PyExc_TypeError))
  1939. PyErr_Format(PyExc_TypeError,
  1940. "cannot convert dictionary update "
  1941. "sequence element #%zd to a sequence",
  1942. i);
  1943. goto Fail;
  1944. }
  1945. n = PySequence_Fast_GET_SIZE(fast);
  1946. if (n != 2) {
  1947. PyErr_Format(PyExc_ValueError,
  1948. "dictionary update sequence element #%zd "
  1949. "has length %zd; 2 is required",
  1950. i, n);
  1951. goto Fail;
  1952. }
  1953. /* Update/merge with this (key, value) pair. */
  1954. key = PySequence_Fast_GET_ITEM(fast, 0);
  1955. value = PySequence_Fast_GET_ITEM(fast, 1);
  1956. if (override || PyOrderedDict_GetItem(d, key) == NULL) {
  1957. int status = PyOrderedDict_SetItem(d, key, value);
  1958. if (status < 0)
  1959. goto Fail;
  1960. }
  1961. Py_DECREF(fast);
  1962. Py_DECREF(item);
  1963. }
  1964. i = 0;
  1965. goto Return;
  1966. Fail:
  1967. Py_XDECREF(item);
  1968. Py_XDECREF(fast);
  1969. i = -1;
  1970. Return:
  1971. Py_DECREF(it);
  1972. return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
  1973. }
  1974. int
  1975. PyOrderedDict_Update(PyObject *a, PyObject *b)
  1976. {
  1977. return PyOrderedDict_Merge(a, b, 1, 0);
  1978. }
  1979. int
  1980. PyOrderedDict_Merge(PyObject *a, PyObject *b, int override, int relaxed)
  1981. {
  1982. register PyOrderedDictObject *mp, *other;
  1983. register Py_ssize_t i;
  1984. PyOrderedDictEntry *entry, **epp;
  1985. /* We accept for the argument either a concrete ordered dictionary object,
  1986. * or an abstract "mapping" object. For the former, we can do
  1987. * things quite efficiently. For the latter, we only require that
  1988. * PyMapping_Keys() and PyObject_GetItem() be supported.
  1989. */
  1990. if (a == NULL || !PyOrderedDict_Check(a) || b == NULL) {
  1991. PyErr_BadInternalCall();
  1992. return -1;
  1993. }
  1994. mp = (PyOrderedDictObject*)a;
  1995. /* sorted dicts are always done with individual elements */
  1996. if (!PySortedDict_CheckExact(a) && PyOrderedDict_CheckExact(b)) {
  1997. other = (PyOrderedDictObject *) b;
  1998. if (other == mp || other->ma_used == 0)
  1999. /* a.update(a) or a.update({}); nothing to do */
  2000. return 0;
  2001. if (mp->ma_used == 0)
  2002. /* Since the target dict is empty, PyOrderedDict_GetItem()
  2003. * always returns NULL. Setting override to 1
  2004. * skips the unnecessary test.
  2005. */
  2006. override = 1;
  2007. /* Do one big resize at the start, rather than
  2008. * incrementally resizing as we insert new items. Expect
  2009. * that there will be no (or few) overlapping keys.
  2010. */
  2011. if ((mp->od_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
  2012. if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
  2013. return -1;
  2014. }
  2015. epp = other->od_otablep;
  2016. for (i = 0; i < other->ma_used; i++) {
  2017. entry = *epp++;
  2018. /* entry->me_value is never NULL when following the otablep */
  2019. /*
  2020. if (entry->me_value != NULL &&
  2021. (override ||
  2022. PyOrderedDict_GetItem(a, entry->me_key) == NULL)) {
  2023. */
  2024. if (override || PyOrderedDict_GetItem(a, entry->me_key) == NULL) {
  2025. Py_INCREF(entry->me_key);
  2026. Py_INCREF(entry->me_value);
  2027. if (insertdict(mp, entry->me_key,
  2028. (long)entry->me_hash,
  2029. entry->me_value, -1) != 0)
  2030. return -1;
  2031. }
  2032. }
  2033. } else if (relaxed || RELAXED(mp)) {
  2034. /* Do it the generic, slower way */
  2035. PyObject *keys = PyMapping_Keys(b);
  2036. PyObject *iter;
  2037. PyObject *key, *value;
  2038. int status;
  2039. if (keys == NULL)
  2040. /* Docstring says this is equivalent to E.keys() so
  2041. * if E doesn't have a .keys() method we want
  2042. * AttributeError to percolate up. Might as well
  2043. * do the same for any other error.
  2044. */
  2045. return -1;
  2046. iter = PyObject_GetIter(keys);
  2047. Py_DECREF(keys);
  2048. if (iter == NULL)
  2049. return -1;
  2050. for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
  2051. if (!override && PyDict_GetItem(a, key) != NULL) {
  2052. Py_DECREF(key);
  2053. continue;
  2054. }
  2055. value = PyObject_GetItem(b, key);
  2056. if (value == NULL) {
  2057. Py_DECREF(iter);
  2058. Py_DECREF(key);
  2059. return -1;
  2060. }
  2061. status = PyOrderedDict_SetItem(a, key, value);
  2062. Py_DECREF(key);
  2063. Py_DECREF(value);
  2064. if (status < 0) {
  2065. Py_DECREF(iter);
  2066. return -1;
  2067. }
  2068. }
  2069. Py_DECREF(iter);
  2070. if (PyErr_Occurred())
  2071. /* Iterator completed, via error */
  2072. return -1;
  2073. } else {
  2074. PyErr_SetString(PyExc_TypeError,
  2075. "source has undefined order");
  2076. return -1;
  2077. }
  2078. return 0;
  2079. }
  2080. /*
  2081. assume that the start step and count are all within the
  2082. borders of what b provides
  2083. */
  2084. int
  2085. PyOrderedDict_CopySome(PyObject *a, PyObject *b,
  2086. Py_ssize_t start, Py_ssize_t step,
  2087. Py_ssize_t count, int override)
  2088. {
  2089. register PyOrderedDictObject *mp, *other;
  2090. register Py_ssize_t i;
  2091. PyOrderedDictEntry *entry, **epp;
  2092. /* We accept for the argument either a concrete ordered dictionary object
  2093. */
  2094. if (a == NULL || !PyOrderedDict_Check(a) || b == NULL) {
  2095. PyErr_BadInternalCall();
  2096. return -1;
  2097. }
  2098. mp = (PyOrderedDictObject*)a;
  2099. if (PyOrderedDict_CheckExact(b) || PySortedDict_CheckExact(b)) {
  2100. other = (PyOrderedDictObject*)b;
  2101. if (other == mp || other->ma_used == 0)
  2102. /* a.update(a) or a.update({}); nothing to do */
  2103. return 0;
  2104. if (mp->ma_used == 0)
  2105. /* Since the target dict is empty, PyOrderedDict_GetItem()
  2106. * always returns NULL. Setting override to 1
  2107. * skips the unnecessary test.
  2108. */
  2109. override = 1;
  2110. /* Do one big resize at the start, rather than
  2111. * incrementally resizing as we insert new items. Expect
  2112. * that there will be no (or few) overlapping keys.
  2113. */
  2114. if ((mp->od_fill + count)*3 >= (mp->ma_mask+1)*2) {
  2115. if (dictresize(mp, (mp->ma_used + count)*2) != 0)
  2116. return -1;
  2117. }
  2118. epp = other->od_otablep;
  2119. epp += start;
  2120. for (i = 0; i < count; i++, epp += step) {
  2121. entry = *epp;
  2122. if (override || PyOrderedDict_GetItem(a, entry->me_key) == NULL) {
  2123. Py_INCREF(entry->me_key);
  2124. Py_INCREF(entry->me_value);
  2125. if (insertdict(mp, entry->me_key,
  2126. (long)entry->me_hash,
  2127. entry->me_value, -1) != 0)
  2128. return -1;
  2129. }
  2130. }
  2131. } else {
  2132. PyErr_SetString(PyExc_TypeError,
  2133. "source has undefined order");
  2134. return -1;
  2135. }
  2136. return 0;
  2137. }
  2138. static PyObject *
  2139. dict_copy(register PyOrderedDictObject *mp)
  2140. {
  2141. return PyOrderedDict_Copy((PyObject*)mp);
  2142. }
  2143. PyObject *
  2144. PyOrderedDict_Copy(PyObject *o)
  2145. {
  2146. PyObject *copy;
  2147. if (o == NULL || !PyOrderedDict_Check(o)) {
  2148. PyErr_BadInternalCall();
  2149. return NULL;
  2150. }
  2151. if (PySortedDict_CheckExact(o)) {
  2152. copy = PySortedDict_New();
  2153. if (copy == NULL)
  2154. return NULL;
  2155. ((PySortedDictObject *) copy)->sd_cmp = ((PySortedDictObject *) o)->sd_cmp;
  2156. ((PySortedDictObject *) copy)->sd_key = ((PySortedDictObject *) o)->sd_key;
  2157. ((PySortedDictObject *) copy)->sd_value = ((PySortedDictObject *) o)->sd_value;
  2158. } else {
  2159. copy = PyOrderedDict_New();
  2160. if (copy == NULL)
  2161. return NULL;
  2162. }
  2163. ((PyOrderedDictObject *) copy)->od_state = ((PyOrderedDictObject *) o)->od_state;
  2164. if (PyOrderedDict_Merge(copy, o, 1, 0) == 0)
  2165. return copy;
  2166. Py_DECREF(copy);
  2167. return NULL;
  2168. }
  2169. Py_ssize_t
  2170. PyOrderedDict_Size(PyObject *mp)
  2171. {
  2172. if (mp == NULL || !PyOrderedDict_Check(mp)) {
  2173. PyErr_BadInternalCall();
  2174. return -1;
  2175. }
  2176. return ((PyOrderedDictObject *)mp)->ma_used;
  2177. }
  2178. PyObject *
  2179. PyOrderedDict_Keys(PyObject *mp)
  2180. {
  2181. if (mp == NULL || !PyOrderedDict_Check(mp)) {
  2182. PyErr_BadInternalCall();
  2183. return NULL;
  2184. }
  2185. return dict_keys((PyOrderedDictObject *)mp, NULL, NULL);
  2186. }
  2187. PyObject *
  2188. PyOrderedDict_Values(PyObject *mp)
  2189. {
  2190. if (mp == NULL || !PyOrderedDict_Check(mp)) {
  2191. PyErr_BadInternalCall();
  2192. return NULL;
  2193. }
  2194. return dict_values((PyOrderedDictObject *)mp, NULL, NULL);
  2195. }
  2196. PyObject *
  2197. PyOrderedDict_Items(PyObject *mp)
  2198. {
  2199. if (mp == NULL || !PyOrderedDict_Check(mp)) {
  2200. PyErr_BadInternalCall();
  2201. return NULL;
  2202. }
  2203. return dict_items((PyOrderedDictObject *)mp, NULL, NULL);
  2204. }
  2205. #if PY_VERSION_HEX < 0x03000000
  2206. /* Subroutine which returns the smallest key in a for which b's value
  2207. is different or absent. The value is returned too, through the
  2208. pval argument. Both are NULL if no key in a is found for which b's status
  2209. differs. The refcounts on (and only on) non-NULL *pval and function return
  2210. values must be decremented by the caller (characterize() increments them
  2211. to ensure that mutating comparison and PyOrderedDict_GetItem calls can't delete
  2212. them before the caller is done looking at them). */
  2213. static PyObject *
  2214. characterize(PyOrderedDictObject *a, PyOrderedDictObject *b, PyObject **pval)
  2215. {
  2216. PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
  2217. PyObject *aval = NULL; /* a[akey] */
  2218. Py_ssize_t i;
  2219. int cmp;
  2220. for (i = 0; i <= a->ma_mask; i++) {
  2221. PyObject *thiskey, *thisaval, *thisbval;
  2222. if (a->ma_table[i].me_value == NULL)
  2223. continue;
  2224. thiskey = a->ma_table[i].me_key;
  2225. Py_INCREF(thiskey); /* keep alive across compares */
  2226. if (akey != NULL) {
  2227. cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
  2228. if (cmp < 0) {
  2229. Py_DECREF(thiskey);
  2230. goto Fail;
  2231. }
  2232. if (cmp > 0 ||
  2233. i > a->ma_mask ||
  2234. a->ma_table[i].me_value == NULL) {
  2235. /* Not the *smallest* a key; or maybe it is
  2236. * but the compare shrunk the dict so we can't
  2237. * find its associated value anymore; or
  2238. * maybe it is but the compare deleted the
  2239. * a[thiskey] entry.
  2240. */
  2241. Py_DECREF(thiskey);
  2242. continue;
  2243. }
  2244. }
  2245. /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
  2246. thisaval = a->ma_table[i].me_value;
  2247. assert(thisaval);
  2248. Py_INCREF(thisaval); /* keep alive */
  2249. thisbval = PyOrderedDict_GetItem((PyObject *)b, thiskey);
  2250. if (thisbval == NULL)
  2251. cmp = 0;
  2252. else {
  2253. /* both dicts have thiskey: same values? */
  2254. cmp = PyObject_RichCompareBool(
  2255. thisaval, thisbval, Py_EQ);
  2256. if (cmp < 0) {
  2257. Py_DECREF(thiskey);
  2258. Py_DECREF(thisaval);
  2259. goto Fail;
  2260. }
  2261. }
  2262. if (cmp == 0) {
  2263. /* New winner. */
  2264. Py_XDECREF(akey);
  2265. Py_XDECREF(aval);
  2266. akey = thiskey;
  2267. aval = thisaval;
  2268. } else {
  2269. Py_DECREF(thiskey);
  2270. Py_DECREF(thisaval);
  2271. }
  2272. }
  2273. *pval = aval;
  2274. return akey;
  2275. Fail:
  2276. Py_XDECREF(akey);
  2277. Py_XDECREF(aval);
  2278. *pval = NULL;
  2279. return NULL;
  2280. }
  2281. static int
  2282. dict_compare(PyOrderedDictObject *a, PyOrderedDictObject *b)
  2283. {
  2284. PyObject *adiff, *bdiff, *aval, *bval;
  2285. int res;
  2286. /* Compare lengths first */
  2287. if (a->ma_used < b->ma_used)
  2288. return -1; /* a is shorter */
  2289. else if (a->ma_used > b->ma_used)
  2290. return 1; /* b is shorter */
  2291. /* Same length -- check all keys */
  2292. bdiff = bval = NULL;
  2293. adiff = characterize(a, b, &aval);
  2294. if (adiff == NULL) {
  2295. assert(!aval);
  2296. /* Either an error, or a is a subset with the same length so
  2297. * must be equal.
  2298. */
  2299. res = PyErr_Occurred() ? -1 : 0;
  2300. goto Finished;
  2301. }
  2302. bdiff = characterize(b, a, &bval);
  2303. if (bdiff == NULL && PyErr_Occurred()) {
  2304. assert(!bval);
  2305. res = -1;
  2306. goto Finished;
  2307. }
  2308. res = 0;
  2309. if (bdiff) {
  2310. /* bdiff == NULL "should be" impossible now, but perhaps
  2311. * the last comparison done by the characterize() on a had
  2312. * the side effect of making the dicts equal!
  2313. */
  2314. res = PyObject_Compare(adiff, bdiff);
  2315. }
  2316. if (res == 0 && bval != NULL)
  2317. res = PyObject_Compare(aval, bval);
  2318. Finished:
  2319. Py_XDECREF(adiff);
  2320. Py_XDECREF(bdiff);
  2321. Py_XDECREF(aval);
  2322. Py_XDECREF(bval);
  2323. return res;
  2324. }
  2325. #endif
  2326. /* Return 1 if dicts equal, 0 if not, -1 if error.
  2327. * Gets out as soon as any difference is detected.
  2328. * Uses only Py_EQ comparison.
  2329. */
  2330. static int
  2331. dict_equal(PyOrderedDictObject *a, PyOrderedDictObject *b)
  2332. {
  2333. Py_ssize_t i;
  2334. PyOrderedDictEntry **app, **bpp;
  2335. if (a->ma_used != b->ma_used)
  2336. /* can't be equal if # of entries differ */
  2337. return 0;
  2338. /* Same # of entries -- check all of 'em. Exit early on any diff. */
  2339. for (i = 0, app = a->od_otablep, bpp = b->od_otablep; i < a->ma_used;
  2340. i++, app++, bpp++) {
  2341. int cmp;
  2342. PyObject *aval = (*app)->me_value;
  2343. PyObject *bval = (*bpp)->me_value;
  2344. PyObject *akey = (*app)->me_key;
  2345. PyObject *bkey = (*bpp)->me_key;
  2346. /* temporarily bump aval's refcount to ensure it stays
  2347. alive until we're done with it */
  2348. Py_INCREF(aval);
  2349. Py_INCREF(bval);
  2350. /* ditto for key */
  2351. Py_INCREF(akey);
  2352. Py_INCREF(bkey);
  2353. cmp = PyObject_RichCompareBool(akey, bkey, Py_EQ);
  2354. if (cmp > 0) /* keys compare ok, now do values */
  2355. cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
  2356. Py_DECREF(bkey);
  2357. Py_DECREF(akey);
  2358. Py_DECREF(bval);
  2359. Py_DECREF(aval);
  2360. if (cmp <= 0) /* error or not equal */
  2361. return cmp;
  2362. }
  2363. return 1;
  2364. }
  2365. static PyObject *
  2366. dict_richcompare(PyObject *v, PyObject *w, int op)
  2367. {
  2368. int cmp;
  2369. PyObject *res;
  2370. if (!PyOrderedDict_Check(v) || !PyOrderedDict_Check(w)) {
  2371. res = Py_NotImplemented;
  2372. } else if (op == Py_EQ || op == Py_NE) {
  2373. cmp = dict_equal((PyOrderedDictObject *)v, (PyOrderedDictObject *)w);
  2374. if (cmp < 0)
  2375. return NULL;
  2376. res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
  2377. } else {
  2378. #if PY_VERSION_HEX < 0x03000000
  2379. /* Py3K warning if comparison isn't == or != */
  2380. if (PyErr_WarnPy3k("dict inequality comparisons not supported "
  2381. "in 3.x", 1) < 0) {
  2382. return NULL;
  2383. }
  2384. #endif
  2385. res = Py_NotImplemented;
  2386. }
  2387. Py_INCREF(res);
  2388. return res;
  2389. }
  2390. static PyObject *
  2391. dict_contains(register PyOrderedDictObject *mp, PyObject *key)
  2392. {
  2393. Py_hash_t hash;
  2394. PyOrderedDictEntry *ep;
  2395. if (!PyUNISTR_CheckExact(key) ||
  2396. (hash = ((PyUNISTR_Object *) key)->OB_HASH) == -1) {
  2397. hash = PyObject_Hash(key);
  2398. if (hash == -1)
  2399. return NULL;
  2400. }
  2401. ep = (mp->ma_lookup)(mp, key, hash);
  2402. if (ep == NULL)
  2403. return NULL;
  2404. return PyBool_FromLong(ep->me_value != NULL);
  2405. }
  2406. #if PY_VERSION_HEX < 0x03000000
  2407. static PyObject *
  2408. dict_has_key(register PyOrderedDictObject *mp, PyObject *key)
  2409. {
  2410. if (Py_Py3kWarningFlag &&
  2411. PyErr_Warn(PyExc_DeprecationWarning,
  2412. "dict.has_key() not supported in 3.x") < 0)
  2413. return NULL;
  2414. return dict_contains(mp, key);
  2415. }
  2416. #endif
  2417. static PyObject *
  2418. dict_get(register PyOrderedDictObject *mp, PyObject *args)
  2419. {
  2420. PyObject *key;
  2421. PyObject *failobj = Py_None;
  2422. PyObject *val = NULL;
  2423. Py_hash_t hash;
  2424. PyOrderedDictEntry *ep;
  2425. if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
  2426. return NULL;
  2427. if (!PyUNISTR_CheckExact(key) ||
  2428. (hash = ((PyUNISTR_Object *) key)->OB_HASH) == -1) {
  2429. hash = PyObject_Hash(key);
  2430. if (hash == -1)
  2431. return NULL;
  2432. }
  2433. ep = (mp->ma_lookup)(mp, key, hash);
  2434. if (ep == NULL)
  2435. return NULL;
  2436. val = ep->me_value;
  2437. if (val == NULL)
  2438. val = failobj;
  2439. Py_INCREF(val);
  2440. return val;
  2441. }
  2442. static PyObject *
  2443. dict_setdefault(register PyOrderedDictObject *mp, PyObject *args)
  2444. {
  2445. PyObject *key;
  2446. PyObject *failobj = Py_None;
  2447. PyObject *val = NULL;
  2448. Py_hash_t hash;
  2449. PyOrderedDictEntry *ep;
  2450. if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
  2451. return NULL;
  2452. if (!PyUNISTR_CheckExact(key) ||
  2453. (hash = ((PyUNISTR_Object *) key)->OB_HASH) == -1) {
  2454. hash = PyObject_Hash(key);
  2455. if (hash == -1)
  2456. return NULL;
  2457. }
  2458. ep = (mp->ma_lookup)(mp, key, hash);
  2459. if (ep == NULL)
  2460. return NULL;
  2461. val = ep->me_value;
  2462. if (val == NULL) {
  2463. if (dict_set_item_by_hash_or_entry((PyObject*)mp, key, hash, ep,
  2464. failobj) == 0)
  2465. val = failobj;
  2466. }
  2467. Py_XINCREF(val);
  2468. return val;
  2469. }
  2470. static PyObject *
  2471. dict_clear(register PyOrderedDictObject *mp)
  2472. {
  2473. PyOrderedDict_Clear((PyObject *)mp);
  2474. Py_RETURN_NONE;
  2475. }
  2476. static PyObject *
  2477. dict_pop(PyOrderedDictObject *mp, PyObject *args)
  2478. {
  2479. Py_hash_t hash;
  2480. PyOrderedDictEntry *ep;
  2481. PyObject *old_value, *old_key;
  2482. PyObject *key, *deflt = NULL;
  2483. if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
  2484. return NULL;
  2485. if (mp->ma_used == 0) {
  2486. if (deflt) {
  2487. Py_INCREF(deflt);
  2488. return deflt;
  2489. }
  2490. PyErr_SetString(PyExc_KeyError,
  2491. "pop(): dictionary is empty");
  2492. return NULL;
  2493. }
  2494. if (!PyUNISTR_CheckExact(key) ||
  2495. (hash = ((PyUNISTR_Object *) key)->OB_HASH) == -1) {
  2496. hash = PyObject_Hash(key);
  2497. if (hash == -1)
  2498. return NULL;
  2499. }
  2500. ep = (mp->ma_lookup)(mp, key, hash);
  2501. if (ep == NULL)
  2502. return NULL;
  2503. if (ep->me_value == NULL) {
  2504. if (deflt) {
  2505. Py_INCREF(deflt);
  2506. return deflt;
  2507. }
  2508. set_key_error(key);
  2509. return NULL;
  2510. }
  2511. old_key = ep->me_key;
  2512. Py_INCREF(dummy);
  2513. ep->me_key = dummy;
  2514. old_value = ep->me_value;
  2515. ep->me_value = NULL;
  2516. del_inorder(mp, ep);
  2517. mp->ma_used--;
  2518. Py_DECREF(old_key);
  2519. return old_value;
  2520. }
  2521. static PyObject *
  2522. dict_popitem(PyOrderedDictObject *mp, PyObject *args)
  2523. {
  2524. Py_hash_ssize_t i = -1, j;
  2525. PyOrderedDictEntry **epp;
  2526. PyObject *res;
  2527. /* Allocate the result tuple before checking the size. Believe it
  2528. * or not, this allocation could trigger a garbage collection which
  2529. * could empty the dict, so if we checked the size first and that
  2530. * happened, the result would be an infinite loop (searching for an
  2531. * entry that no longer exists). Note that the usual popitem()
  2532. * idiom is "while d: k, v = d.popitem()". so needing to throw the
  2533. * tuple away if the dict *is* empty isn't a significant
  2534. * inefficiency -- possible, but unlikely in practice.
  2535. */
  2536. if (!PyArg_ParseTuple(args, "|n:popitem", &i))
  2537. return NULL;
  2538. res = PyTuple_New(2);
  2539. if (res == NULL)
  2540. return NULL;
  2541. if (mp->ma_used == 0) {
  2542. Py_DECREF(res);
  2543. PyErr_SetString(PyExc_KeyError,
  2544. "popitem(): dictionary is empty");
  2545. return NULL;
  2546. }
  2547. if (i < 0)
  2548. j = mp->ma_used + i;
  2549. else
  2550. j = i;
  2551. if (j < 0 || j >= mp->ma_used) {
  2552. Py_DECREF(res);
  2553. PyErr_SetString(PyExc_KeyError,
  2554. "popitem(): index out of range");
  2555. return NULL;
  2556. }
  2557. epp = mp->od_otablep;
  2558. epp += j;
  2559. PyTuple_SET_ITEM(res, 0, (*epp)->me_key);
  2560. PyTuple_SET_ITEM(res, 1, (*epp)->me_value);
  2561. Py_INCREF(dummy);
  2562. (*epp)->me_key = dummy;
  2563. (*epp)->me_value = NULL;
  2564. mp->ma_used--;
  2565. if (i != -1) { /* for default case -1, we don't have to do anything */
  2566. /* ma_used has already been decremented ! */
  2567. memmove(epp, epp+1, (mp->ma_used - j) * sizeof(PyOrderedDictEntry *));
  2568. }
  2569. return res;
  2570. }
  2571. static int
  2572. dict_traverse(PyObject *op, visitproc visit, void *arg)
  2573. {
  2574. Py_ssize_t i = 0;
  2575. PyObject *pk;
  2576. PyObject *pv;
  2577. while (PyOrderedDict_Next(op, &i, &pk, &pv)) {
  2578. Py_VISIT(pk);
  2579. Py_VISIT(pv);
  2580. }
  2581. return 0;
  2582. }
  2583. static int
  2584. dict_tp_clear(PyObject *op)
  2585. {
  2586. PyOrderedDict_Clear(op);
  2587. return 0;
  2588. }
  2589. #if PY_MAJOR_VERSION < 3
  2590. extern PyTypeObject PyOrderedDictIterKey_Type; /* Forward */
  2591. extern PyTypeObject PyOrderedDictIterValue_Type; /* Forward */
  2592. extern PyTypeObject PyOrderedDictIterItem_Type; /* Forward */
  2593. #endif
  2594. static PyObject *dictiter_new(PyOrderedDictObject *, PyTypeObject *,
  2595. PyObject *args, PyObject *kwds);
  2596. #if PY_MAJOR_VERSION < 3
  2597. static PyObject *
  2598. dict_iterkeys(PyOrderedDictObject *dict, PyObject *args, PyObject *kwds)
  2599. {
  2600. return dictiter_new(dict, &PyOrderedDictIterKey_Type, args, kwds);
  2601. }
  2602. static PyObject *
  2603. dict_itervalues(PyOrderedDictObject *dict, PyObject *args, PyObject *kwds)
  2604. {
  2605. return dictiter_new(dict, &PyOrderedDictIterValue_Type, args, kwds);
  2606. }
  2607. static PyObject *
  2608. dict_iteritems(PyOrderedDictObject *dict, PyObject *args, PyObject *kwds)
  2609. {
  2610. return dictiter_new(dict, &PyOrderedDictIterItem_Type, args, kwds);
  2611. }
  2612. #endif
  2613. static PyObject *
  2614. dict_sizeof(PyDictObject *mp)
  2615. {
  2616. Py_ssize_t res;
  2617. res = sizeof(PyOrderedDictObject);
  2618. if (mp->ma_table != mp->ma_smalltable)
  2619. res = res + (mp->ma_mask + 1) * sizeof(PyOrderedDictEntry);
  2620. #if PY_VERSION_HEX < 0x03000000
  2621. return PyInt_FromSize_t(res);
  2622. #else
  2623. return PyLong_FromSize_t(res);
  2624. #endif
  2625. }
  2626. static PyObject *
  2627. dict_index(register PyOrderedDictObject *mp, PyObject *key)
  2628. {
  2629. Py_hash_t hash;
  2630. PyOrderedDictEntry *ep, **tmp;
  2631. register Py_ssize_t index;
  2632. if (!PyUNISTR_CheckExact(key) ||
  2633. (hash = ((PyUNISTR_Object *) key)->OB_HASH) == -1) {
  2634. hash = PyObject_Hash(key);
  2635. if (hash == -1)
  2636. return NULL;
  2637. }
  2638. ep = (mp->ma_lookup)(mp, key, hash);
  2639. if (ep == NULL || ep->me_value == NULL) {
  2640. PyErr_SetString(PyExc_ValueError,
  2641. "ordereddict.index(x): x not a key in ordereddict"
  2642. );
  2643. return NULL;
  2644. }
  2645. for (index = 0, tmp = mp->od_otablep; index < mp->ma_used; index++, tmp++) {
  2646. if (*tmp == ep) {
  2647. #if PY_VERSION_HEX < 0x03000000
  2648. return PyInt_FromSize_t(index);
  2649. #else
  2650. return PyLong_FromSize_t(index);
  2651. #endif
  2652. }
  2653. }
  2654. return NULL; /* not found */
  2655. }
  2656. static PyObject *
  2657. dict_insert(PyOrderedDictObject *mp, PyObject *args)
  2658. {
  2659. Py_ssize_t i;
  2660. PyObject *key;
  2661. PyObject *val;
  2662. #if PY_VERSION_HEX >= 0x02050000
  2663. if (!PyArg_ParseTuple(args, "nOO:insert", &i, &key, &val))
  2664. #else
  2665. if (!PyArg_ParseTuple(args, "iOO:insert", &i, &key, &val))
  2666. #endif
  2667. return NULL;
  2668. if(PyOrderedDict_InsertItem(mp, i, key, val) != 0)
  2669. return NULL;
  2670. Py_RETURN_NONE;
  2671. }
  2672. static PyObject *
  2673. dict_reverse(register PyOrderedDictObject *mp)
  2674. {
  2675. PyOrderedDictEntry **epps, **eppe, *tmp;
  2676. epps = mp->od_otablep;
  2677. eppe = epps + ((mp->ma_used)-1);
  2678. while (epps < eppe) {
  2679. tmp = *epps;
  2680. *epps++ = *eppe;
  2681. *eppe-- = tmp;
  2682. }
  2683. Py_RETURN_NONE;
  2684. }
  2685. static PyObject *
  2686. dict_setkeys(register PyOrderedDictObject *mp, PyObject *keys)
  2687. {
  2688. PyOrderedDictEntry **newtable, *item;
  2689. Py_ssize_t size = mp->ma_used * sizeof(PyOrderedDictEntry *), i, oldindex;
  2690. PyObject *key = NULL;
  2691. PyObject *it;
  2692. Py_hash_t hash;
  2693. if (PySortedDict_CheckExact(mp)) {
  2694. PyErr_SetString(PyExc_TypeError,
  2695. "sorteddict does not support setkeys() assignment");
  2696. return NULL;
  2697. }
  2698. /* determine length -> ok if ok
  2699. if ok, then we still don't know if all keys will be found
  2700. so we allocate an array of ma_mask+1 size (which is what was used for
  2701. last resize and start filling that.
  2702. On finish, memcopy (so we don't have to worry about where the
  2703. values actually are (allocated or in smallbuffer), and
  2704. delete the tmp stuff,
  2705. if some key cannot be found (or is double) we don't update
  2706. */
  2707. newtable = PyMem_NEW(PyOrderedDictEntry *, size);
  2708. if (newtable == NULL) {
  2709. PyErr_NoMemory();
  2710. return NULL;
  2711. }
  2712. i = PyObject_Length(keys);
  2713. if ((i >=0) && (i != mp->ma_used)) {
  2714. PyErr_Format(PyExc_ValueError,
  2715. "ordereddict setkeys requires sequence of length #%zd; "
  2716. "provided was length %zd",
  2717. mp->ma_used, i);
  2718. return NULL;
  2719. }
  2720. if (i == -1) PyErr_Clear();
  2721. it = PyObject_GetIter(keys);
  2722. if (it == NULL)
  2723. return NULL;
  2724. for (i = 0; ; ++i) {
  2725. key = PyIter_Next(it);
  2726. if (key == NULL) {
  2727. if (PyErr_Occurred()) break;
  2728. if (i != mp->ma_used) {
  2729. PyErr_Format(PyExc_ValueError,
  2730. "ordereddict setkeys requires sequence of length #%zd; "
  2731. "provided was length %zd",
  2732. mp->ma_used, i);
  2733. break;
  2734. }
  2735. memcpy(mp->od_otablep, newtable, size);
  2736. PyMem_DEL(newtable);
  2737. Py_DECREF(it);
  2738. Py_RETURN_NONE;
  2739. }
  2740. if (i >= mp->ma_used) {
  2741. PyErr_Format(PyExc_ValueError,
  2742. "ordereddict setkeys requires sequence of max length #%zd; "
  2743. "a longer sequence was provided",
  2744. mp->ma_used);
  2745. Py_DECREF(it);
  2746. return NULL;
  2747. }
  2748. /* find the item with this key */
  2749. if (!PyUNISTR_CheckExact(key) ||
  2750. (hash = ((PyUNISTR_Object *) key)->OB_HASH) == -1) {
  2751. hash = PyObject_Hash(key);
  2752. if (hash == -1)
  2753. break;
  2754. }
  2755. item = (mp->ma_lookup)(mp, key, hash);
  2756. if (item == NULL || item->me_value == NULL) {
  2757. PyErr_Format(PyExc_KeyError,
  2758. "ordereddict setkeys unknown key at pos " SPR,
  2759. i);
  2760. break;
  2761. }
  2762. /* PyObject_Print((PyObject *)item->me_key, stdout, 0);*/
  2763. /* check if a pointer to this item has been set */
  2764. for (oldindex = 0; oldindex < i; oldindex++) {
  2765. if (newtable[oldindex] == item) {
  2766. PyErr_Format(PyExc_KeyError,
  2767. "ordereddict setkeys same key at pos " SPR "and " SPR,
  2768. oldindex, i);
  2769. break;
  2770. }
  2771. }
  2772. /* insert the pointer to this item */
  2773. newtable[i] = item;
  2774. }
  2775. PyMem_DEL(newtable);
  2776. Py_XDECREF(key);
  2777. Py_DECREF(it);
  2778. return NULL;
  2779. }
  2780. static PyObject *
  2781. dict_setvalues(register PyOrderedDictObject *mp, PyObject *values)
  2782. {
  2783. PyObject *it; /* iter(seq2) */
  2784. Py_ssize_t i; /* index into seq2 of current element */
  2785. PyObject *item = NULL; /* values[i] */
  2786. PyOrderedDictEntry **epp = mp->od_otablep, *tmp;
  2787. assert(mp != NULL);
  2788. assert(PyOrderedDict_Check(mp));
  2789. assert(values != NULL);
  2790. i = PyObject_Length(values);
  2791. /* printf("\nlength %d %d\n", i, mp->ma_used); */
  2792. if ((i >=0) && (i != mp->ma_used)) {
  2793. PyErr_Format(PyExc_ValueError,
  2794. "ordereddict setvalues requires sequence of length #%zd; "
  2795. "provided was length %zd",
  2796. mp->ma_used, i);
  2797. return NULL;
  2798. }
  2799. if (i == -1) PyErr_Clear();
  2800. it = PyObject_GetIter(values);
  2801. if (it == NULL)
  2802. return NULL;
  2803. for (i = 0; ; ++i) {
  2804. item = PyIter_Next(it);
  2805. if (item == NULL) {
  2806. if (PyErr_Occurred()) break;
  2807. if (i != mp->ma_used) {
  2808. PyErr_Format(PyExc_ValueError,
  2809. "ordereddict setvalues requires sequence of length #%zd; "
  2810. "provided was length %zd, ordereddict partially updated",
  2811. mp->ma_used, i);
  2812. break;
  2813. }
  2814. Py_DECREF(it);
  2815. Py_RETURN_NONE;
  2816. }
  2817. if (i >= mp->ma_used) {
  2818. PyErr_Format(PyExc_ValueError,
  2819. "ordereddict setvalues requires sequence of max length #%zd; "
  2820. "a longer sequence was provided, ordereddict fully updated",
  2821. mp->ma_used);
  2822. Py_DECREF(it);
  2823. return NULL;
  2824. }
  2825. tmp = *epp++;
  2826. Py_DECREF(tmp->me_value);
  2827. tmp->me_value = item;
  2828. }
  2829. Py_XDECREF(item);
  2830. Py_DECREF(it);
  2831. return NULL;
  2832. }
  2833. static PyObject *
  2834. dict_setitems(register PyObject *mp, PyObject *args, PyObject *kwds)
  2835. {
  2836. PyOrderedDict_Clear((PyObject *)mp);
  2837. if (dict_update_common(mp, args, kwds, "|Oi:setitems") != -1)
  2838. Py_RETURN_NONE;
  2839. return NULL;
  2840. }
  2841. static PyObject *
  2842. dict_rename(register PyOrderedDictObject *mp, PyObject *args)
  2843. {
  2844. PyObject *oldkey, *newkey;
  2845. PyObject *val = NULL;
  2846. Py_hash_t hash;
  2847. PyOrderedDictEntry *ep, **epp;
  2848. register Py_ssize_t index;
  2849. if (PySortedDict_CheckExact(mp)) {
  2850. PyErr_SetString(PyExc_TypeError,
  2851. "sorteddict does not support rename()");
  2852. return NULL;
  2853. }
  2854. if (!PyArg_UnpackTuple(args, "get", 1, 2, &oldkey, &newkey))
  2855. return NULL;
  2856. if (!PyUNISTR_CheckExact(oldkey) ||
  2857. (hash = ((PyUNISTR_Object *) oldkey)->OB_HASH) == -1) {
  2858. hash = PyObject_Hash(oldkey);
  2859. if (hash == -1)
  2860. return NULL;
  2861. }
  2862. ep = (mp->ma_lookup)(mp, oldkey, hash);
  2863. if (ep == NULL || ep->me_value == NULL)
  2864. return NULL;
  2865. epp = mp->od_otablep;
  2866. for (index = 0; index < mp->ma_used; index++, epp++)
  2867. if (*epp == ep)
  2868. break;
  2869. if (*epp != ep)
  2870. return NULL; /* this is bad! */
  2871. oldkey = ep->me_key; /* now point to key from item */
  2872. val = ep->me_value;
  2873. Py_INCREF(dummy);
  2874. ep->me_key = dummy;
  2875. ep->me_value = NULL;
  2876. memmove(epp, epp+1, (mp->ma_used - index) * sizeof(PyOrderedDictEntry *));
  2877. mp->ma_used--;
  2878. Py_DECREF(oldkey);
  2879. if(PyOrderedDict_InsertItem(mp, index, newkey, val) != 0)
  2880. return NULL;
  2881. Py_DECREF(val);
  2882. Py_RETURN_NONE;
  2883. }
  2884. #if PY_VERSION_HEX < 0x03000000
  2885. #define REDUCE
  2886. /* support for pickling */
  2887. static PyObject *
  2888. dict_reduce(PyOrderedDictObject *self)
  2889. {
  2890. PyObject *result, *it, *dict=NULL;
  2891. it = dictiter_new(self, &PyOrderedDictIterItem_Type, NULL, NULL);
  2892. dict = Py_None;
  2893. Py_INCREF(dict);
  2894. Py_INCREF(dict);
  2895. if (PySortedDict_CheckExact(self)) {
  2896. if (((PySortedDictObject *) self)->sd_cmp == NULL)
  2897. printf("NULL!!!!\n");
  2898. result = Py_BuildValue("O(()OOOi)NNO", self->ob_type,
  2899. ((PySortedDictObject *) self)->sd_cmp,
  2900. ((PySortedDictObject *) self)->sd_key,
  2901. ((PySortedDictObject *) self)->sd_value,
  2902. REVERSE(self), dict, dict, it);
  2903. } else {
  2904. result = Py_BuildValue("O(()ii)NNO", self->ob_type, RELAXED(self), KVIO(self), dict, dict, it);
  2905. }
  2906. return result;
  2907. }
  2908. #endif
  2909. static PyObject *
  2910. ordereddict_getstate(register PyOrderedDictObject *mp)
  2911. {
  2912. #if PY_MAJOR_VERSION >= 3
  2913. return PyLong_FromLong(mp->od_state);
  2914. #else
  2915. return PyInt_FromLong(mp->od_state);
  2916. #endif
  2917. }
  2918. static PyObject *
  2919. ordereddict_dump(register PyOrderedDictObject *mp)
  2920. {
  2921. if (dump_ordereddict_head(mp) != -1)
  2922. dump_otablep(mp);
  2923. if (PySortedDict_CheckExact(mp))
  2924. dump_sorteddict_fun((PySortedDictObject *) mp);
  2925. Py_RETURN_NONE;
  2926. }
  2927. #if PY_VERSION_HEX < 0x03000000
  2928. PyDoc_STRVAR(has_key__doc__,
  2929. "D.has_key(k) -> True if D has a key k, else False");
  2930. #endif
  2931. PyDoc_STRVAR(contains__doc__,
  2932. "D.__contains__(k) -> True if D has a key k, else False");
  2933. #ifdef REDUCE
  2934. PyDoc_STRVAR(reduce__doc__, "Return state information for pickling.");
  2935. #endif
  2936. PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
  2937. PyDoc_STRVAR(sizeof__doc__,
  2938. "D.__sizeof__() -> size of D in memory, in bytes");
  2939. PyDoc_STRVAR(get__doc__,
  2940. "D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
  2941. PyDoc_STRVAR(setdefault_doc__,
  2942. "D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D");
  2943. PyDoc_STRVAR(pop__doc__,
  2944. "D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
  2945. If key is not found, d is returned if given, otherwise KeyError is raised");
  2946. PyDoc_STRVAR(popitem__doc__,
  2947. "D.popitem([index]) -> (k, v), remove and return indexed (key, value) pair as a\n\
  2948. 2-tuple (default is last); but raise KeyError if D is empty.");
  2949. #if PY_VERSION_HEX < 0x03000000
  2950. PyDoc_STRVAR(keys__doc__,
  2951. "D.keys([reverse=False]) -> list of D's keys, optionally reversed");
  2952. PyDoc_STRVAR(items__doc__,
  2953. "D.items() -> list of D's (key, value) pairs, as 2-tuples");
  2954. PyDoc_STRVAR(values__doc__,
  2955. "D.values() -> list of D's values");
  2956. #endif
  2957. PyDoc_STRVAR(update__doc__,
  2958. "D.update([E,] **F) -> None. Update D from dict/iterable E and F.\n"
  2959. "If E present and has a .keys() method, does: for k in E: D[k] = E[k]\n\
  2960. If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
  2961. In either case, this is followed by: for k in F: D[k] = F[k]");
  2962. PyDoc_STRVAR(fromkeys__doc__,
  2963. "dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
  2964. v defaults to None.");
  2965. PyDoc_STRVAR(clear__doc__,
  2966. "D.clear() -> None. Remove all items from D.");
  2967. PyDoc_STRVAR(copy__doc__,
  2968. "D.copy() -> a shallow copy of D");
  2969. #if PY_VERSION_HEX < 0x03000000
  2970. PyDoc_STRVAR(iterkeys__doc__,
  2971. "D.iterkeys([reverse=False]) -> an iterator over the keys of D");
  2972. PyDoc_STRVAR(itervalues__doc__,
  2973. "D.itervalues() -> an iterator over the values of D");
  2974. PyDoc_STRVAR(iteritems__doc__,
  2975. "D.iteritems() -> an iterator over the (key, value) items of D");
  2976. #endif
  2977. PyDoc_STRVAR(index_doc,
  2978. "D.index(key) -> return position of key in ordered dict");
  2979. PyDoc_STRVAR(insert_doc,
  2980. "D.insert(index, key, value) -> add/update (key, value) and insert key at index");
  2981. PyDoc_STRVAR(reverse_doc,
  2982. "D.reverse() -> reverse the order of the keys of D");
  2983. PyDoc_STRVAR(setkeys_doc,
  2984. "D.setkeys(keys) -> set the keys of D (keys must be iterable and a permutation of .keys())");
  2985. PyDoc_STRVAR(setvalues_doc,
  2986. "D.setvalues(values) -> set D values to values (must be iterable)");
  2987. PyDoc_STRVAR(setitems_doc,
  2988. "D.setitems(items) -> clear D and then set items");
  2989. PyDoc_STRVAR(rename_doc,
  2990. "D.rename(oldkey, newkey) -> exchange keys without changing order");
  2991. PyDoc_STRVAR(getstate_doc,
  2992. "D.getstate() -> return the state integer");
  2993. PyDoc_STRVAR(dump_doc,
  2994. "D.dump() -> print internals of an orereddict");
  2995. /* Forward */
  2996. static PyObject *dictkeys_new(PyObject *);
  2997. static PyObject *dictitems_new(PyObject *);
  2998. static PyObject *dictvalues_new(PyObject *);
  2999. #if PY_VERSION_HEX < 0x03000000
  3000. PyDoc_STRVAR(viewkeys__doc__,
  3001. "D.viewkeys() -> a set-like object providing a view on D's keys");
  3002. PyDoc_STRVAR(viewitems__doc__,
  3003. "D.viewitems() -> a set-like object providing a view on D's items");
  3004. PyDoc_STRVAR(viewvalues__doc__,
  3005. "D.viewvalues() -> an object providing a view on D's values");
  3006. #else
  3007. PyDoc_STRVAR(viewkeys__doc__,
  3008. "D.keys() -> a set-like object providing a view on D's keys");
  3009. PyDoc_STRVAR(viewitems__doc__,
  3010. "D.items() -> a set-like object providing a view on D's items");
  3011. PyDoc_STRVAR(viewvalues__doc__,
  3012. "D.values() -> an object providing a view on D's values");
  3013. #endif
  3014. static PyMethodDef ordereddict_methods[] = {
  3015. {
  3016. "__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
  3017. contains__doc__
  3018. },
  3019. {
  3020. "__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
  3021. getitem__doc__
  3022. },
  3023. {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
  3024. sizeof__doc__},
  3025. #ifdef REDUCE
  3026. {"__reduce__", (PyCFunction)dict_reduce, METH_NOARGS, reduce__doc__},
  3027. #endif
  3028. #if PY_VERSION_HEX < 0x03000000
  3029. {
  3030. "has_key", (PyCFunction)dict_has_key, METH_O,
  3031. has_key__doc__
  3032. },
  3033. #endif
  3034. {
  3035. "get", (PyCFunction)dict_get, METH_VARARGS,
  3036. get__doc__
  3037. },
  3038. {
  3039. "setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
  3040. setdefault_doc__
  3041. },
  3042. {
  3043. "pop", (PyCFunction)dict_pop, METH_VARARGS,
  3044. pop__doc__
  3045. },
  3046. {
  3047. "popitem", (PyCFunction)dict_popitem, METH_VARARGS,
  3048. popitem__doc__
  3049. },
  3050. #if PY_VERSION_HEX < 0x03000000
  3051. {
  3052. "keys", (PyCFunction)dict_keys, METH_VARARGS | METH_KEYWORDS,
  3053. keys__doc__
  3054. },
  3055. {
  3056. "items", (PyCFunction)dict_items, METH_VARARGS | METH_KEYWORDS,
  3057. items__doc__
  3058. },
  3059. {
  3060. "values", (PyCFunction)dict_values, METH_VARARGS | METH_KEYWORDS,
  3061. values__doc__
  3062. },
  3063. #if PY_VERSION_HEX >= 0x02070000
  3064. {"viewkeys", (PyCFunction)dictkeys_new, METH_NOARGS,
  3065. viewkeys__doc__},
  3066. {"viewitems", (PyCFunction)dictitems_new, METH_NOARGS,
  3067. viewitems__doc__},
  3068. {"viewvalues", (PyCFunction)dictvalues_new, METH_NOARGS,
  3069. viewvalues__doc__},
  3070. #endif
  3071. #else /* Py3K */
  3072. {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
  3073. viewkeys__doc__},
  3074. {"items", (PyCFunction)dictitems_new, METH_NOARGS,
  3075. viewitems__doc__},
  3076. {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
  3077. viewvalues__doc__},
  3078. #endif
  3079. {
  3080. "update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
  3081. update__doc__
  3082. },
  3083. {
  3084. "fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
  3085. fromkeys__doc__
  3086. },
  3087. {
  3088. "clear", (PyCFunction)dict_clear, METH_NOARGS,
  3089. clear__doc__
  3090. },
  3091. {
  3092. "copy", (PyCFunction)dict_copy, METH_NOARGS,
  3093. copy__doc__
  3094. },
  3095. #if PY_VERSION_HEX < 0x03000000
  3096. {
  3097. "iterkeys", (PyCFunction)dict_iterkeys, METH_VARARGS | METH_KEYWORDS,
  3098. iterkeys__doc__
  3099. },
  3100. {
  3101. "itervalues", (PyCFunction)dict_itervalues, METH_VARARGS | METH_KEYWORDS,
  3102. itervalues__doc__
  3103. },
  3104. {
  3105. "iteritems", (PyCFunction)dict_iteritems, METH_VARARGS | METH_KEYWORDS,
  3106. iteritems__doc__
  3107. },
  3108. #endif
  3109. {"index", (PyCFunction)dict_index, METH_O, index_doc},
  3110. {"insert", (PyCFunction)dict_insert, METH_VARARGS, insert_doc},
  3111. {"reverse", (PyCFunction)dict_reverse, METH_NOARGS, reverse_doc},
  3112. {"setkeys", (PyCFunction)dict_setkeys, METH_O, setkeys_doc},
  3113. {"setvalues", (PyCFunction)dict_setvalues, METH_O, setvalues_doc},
  3114. {"setitems", (PyCFunction)dict_setitems, METH_VARARGS | METH_KEYWORDS, setitems_doc},
  3115. {"rename", (PyCFunction)dict_rename, METH_VARARGS, rename_doc},
  3116. {"getstate", (PyCFunction)ordereddict_getstate, METH_NOARGS, getstate_doc},
  3117. {"dump", (PyCFunction)ordereddict_dump, METH_NOARGS, dump_doc},
  3118. {NULL, NULL} /* sentinel */
  3119. };
  3120. /* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
  3121. int
  3122. PyOrderedDict_Contains(PyObject *op, PyObject *key)
  3123. {
  3124. Py_hash_t hash;
  3125. PyOrderedDictObject *mp = (PyOrderedDictObject *)op;
  3126. PyOrderedDictEntry *ep;
  3127. if (!PyUNISTR_CheckExact(key) ||
  3128. (hash = ((PyUNISTR_Object *) key)->OB_HASH) == -1) {
  3129. hash = PyObject_Hash(key);
  3130. if (hash == -1)
  3131. return -1;
  3132. }
  3133. ep = (mp->ma_lookup)(mp, key, hash);
  3134. return ep == NULL ? -1 : (ep->me_value != NULL);
  3135. }
  3136. /* Internal version of PyOrderedDict_Contains used when the hash value is already known */
  3137. int
  3138. _PyOrderedDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
  3139. {
  3140. PyOrderedDictObject *mp = (PyOrderedDictObject *)op;
  3141. PyOrderedDictEntry *ep;
  3142. ep = (mp->ma_lookup)(mp, key, hash);
  3143. return ep == NULL ? -1 : (ep->me_value != NULL);
  3144. }
  3145. static PyObject *
  3146. PyOderedDict_Slice(PyObject *op, register Py_ssize_t ilow,
  3147. register Py_ssize_t ihigh)
  3148. {
  3149. PyOrderedDictObject *mp = (PyOrderedDictObject *)op;
  3150. PyOrderedDictObject *slice;
  3151. if (mp == NULL || !PyOrderedDict_Check(mp)) {
  3152. PyErr_BadInternalCall();
  3153. return NULL;
  3154. }
  3155. slice = (PyOrderedDictObject *) PyOrderedDict_New();
  3156. if (slice == NULL)
  3157. return NULL;
  3158. /* [:] -> ilow = 0, ihigh MAXINT */
  3159. if (ilow < 0)
  3160. ilow += mp->ma_used;
  3161. if (ihigh < 0)
  3162. ihigh += mp->ma_used;
  3163. if (ilow < 0)
  3164. ilow = 0;
  3165. else if (ilow > mp->ma_used)
  3166. ilow = mp->ma_used;
  3167. if (ihigh < ilow)
  3168. ihigh = ilow;
  3169. else if (ihigh > mp->ma_used)
  3170. ihigh = mp->ma_used;
  3171. if (PyOrderedDict_CopySome((PyObject *) slice,
  3172. op, ilow, 1, (ihigh-ilow), 1) == 0) {
  3173. return (PyObject *) slice;
  3174. }
  3175. Py_DECREF(slice);
  3176. return NULL;
  3177. }
  3178. /* Hack to implement "key in dict" */
  3179. static PySequenceMethods dict_as_sequence = {
  3180. 0, /* sq_length */
  3181. 0, /* sq_concat */
  3182. 0, /* sq_repeat */
  3183. 0, /* sq_item */
  3184. (ssizessizeargfunc)PyOderedDict_Slice, /* sq_slice */
  3185. 0, /* sq_ass_item */
  3186. (ssizessizeobjargproc)dict_ass_slice, /* sq_ass_slice */
  3187. PyOrderedDict_Contains, /* sq_contains */
  3188. 0, /* sq_inplace_concat */
  3189. 0, /* sq_inplace_repeat */
  3190. };
  3191. static PyObject *
  3192. dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  3193. {
  3194. PyObject *self;
  3195. assert(type != NULL && type->tp_alloc != NULL);
  3196. assert(dummy != NULL);
  3197. self = type->tp_alloc(type, 0);
  3198. if (self != NULL) {
  3199. PyOrderedDictObject *d = (PyOrderedDictObject *)self;
  3200. /* It's guaranteed that tp->alloc zeroed out the struct. */
  3201. assert(d->ma_table == NULL && d->od_fill == 0 && d->ma_used == 0);
  3202. INIT_NONZERO_DICT_SLOTS(d);
  3203. d->ma_lookup = lookdict_string;
  3204. /* The object has been implicitly tracked by tp_alloc */
  3205. if (type == &PyOrderedDict_Type)
  3206. _PyObject_GC_UNTRACK(d);
  3207. #ifdef SHOW_CONVERSION_COUNTS
  3208. ++created;
  3209. #endif
  3210. #ifdef SHOW_TRACK_COUNT
  3211. if (_PyObject_GC_IS_TRACKED(d))
  3212. count_tracked++;
  3213. else
  3214. count_untracked++;
  3215. #endif
  3216. }
  3217. return self;
  3218. }
  3219. static PyObject *
  3220. sorteddict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  3221. {
  3222. PyObject *self;
  3223. assert(type != NULL && type->tp_alloc != NULL);
  3224. assert(dummy != NULL);
  3225. self = type->tp_alloc(type, 0);
  3226. if (self != NULL) {
  3227. PyOrderedDictObject *d = (PyOrderedDictObject *)self;
  3228. /* It's guaranteed that tp->alloc zeroed out the struct. */
  3229. assert(d->ma_table == NULL && d->od_fill == 0 && d->ma_used == 0);
  3230. INIT_NONZERO_DICT_SLOTS(d);
  3231. d->ma_lookup = lookdict_string;
  3232. INIT_SORT_FUNCS(((PySortedDictObject *) self));
  3233. if (type == &PySortedDict_Type)
  3234. _PyObject_GC_UNTRACK(d);
  3235. #ifdef SHOW_CONVERSION_COUNTS
  3236. ++created;
  3237. #endif
  3238. #ifdef SHOW_TRACK_COUNT
  3239. if (_PyObject_GC_IS_TRACKED(d))
  3240. count_tracked++;
  3241. else
  3242. count_untracked++;
  3243. #endif
  3244. }
  3245. return self;
  3246. }
  3247. static int
  3248. ordereddict_init(PyObject *self, PyObject *args, PyObject *kwds)
  3249. {
  3250. PyObject *arg = NULL;
  3251. int result = 0, tmprelax = -1, tmpkvio = -1;
  3252. static char *kwlist[] = {"src", "relax", "kvio", 0};
  3253. if (args != NULL) {
  3254. if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oii:ordereddict",
  3255. kwlist, &arg, &tmprelax, &tmpkvio)) {
  3256. return -1;
  3257. }
  3258. }
  3259. if (tmpkvio == -1)
  3260. tmpkvio = ordereddict_kvio;
  3261. if (tmpkvio)
  3262. ((PyOrderedDictObject *)self)->od_state |= OD_KVIO_BIT;
  3263. if (tmprelax == -1)
  3264. tmprelax = ordereddict_relaxed;
  3265. if (tmprelax)
  3266. ((PyOrderedDictObject *)self)->od_state |= OD_RELAXED_BIT;
  3267. if (arg != NULL) {
  3268. if (PyObject_HasAttrString(arg, "keys"))
  3269. result = PyOrderedDict_Merge(self, arg, 1, tmprelax);
  3270. else
  3271. result = PyOrderedDict_MergeFromSeq2(self, arg, 1);
  3272. }
  3273. /* do not initialise from keywords at all */
  3274. return result;
  3275. }
  3276. static int
  3277. sorteddict_init(PyObject *self, PyObject *args, PyObject *kwds)
  3278. {
  3279. PyObject *arg = NULL, *cmpfun = NULL, *keyfun = NULL, *valuefun = NULL;
  3280. int result = 0, reverse = 0;
  3281. static char *kwlist[] = {"src", "cmp", "key", "value", "reverse", 0};
  3282. if (args != NULL)
  3283. if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOi:sorteddict",
  3284. kwlist, &arg, &cmpfun, &keyfun, &valuefun, &reverse))
  3285. return -1;
  3286. if (reverse)
  3287. ((PyOrderedDictObject *)self)->od_state |= OD_REVERSE_BIT;
  3288. /* always relaxed about order of source */
  3289. ((PyOrderedDictObject *)self)->od_state |= OD_RELAXED_BIT;
  3290. if (keyfun != NULL && keyfun != Py_False)
  3291. ((PySortedDictObject *)self)->sd_key = keyfun;
  3292. if (arg != NULL) {
  3293. if (PyObject_HasAttrString(arg, "keys"))
  3294. result = PyOrderedDict_Merge(self, arg, 1, 1);
  3295. else
  3296. result = PyOrderedDict_MergeFromSeq2(self, arg, 1);
  3297. }
  3298. /* do not initialise from keywords at all */
  3299. return result;
  3300. }
  3301. static PyObject *
  3302. dict_iter(PyOrderedDictObject *dict)
  3303. {
  3304. return dictiter_new(dict, &PyOrderedDictIterKey_Type, NULL, NULL);
  3305. }
  3306. PyDoc_STRVAR(ordereddict_doc,
  3307. "ordereddict() -> new empty dictionary.\n"
  3308. "dict(orderddict) -> new dictionary initialized from a mappings object's\n"
  3309. " (key, value) pairs.\n"
  3310. //"dict(iterable) -> new dictionary initialized as if via:\n"
  3311. //" d = {}\n"
  3312. //" for k, v in iterable:\n"
  3313. //" d[k] = v\n"
  3314. //"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
  3315. //" in the keyword argument list. For example: dict(one=1, two=2)"
  3316. );
  3317. PyTypeObject PyOrderedDict_Type = {
  3318. PyVarObject_HEAD_INIT(NULL, 0)
  3319. "_ordereddict.ordereddict",
  3320. sizeof(PyOrderedDictObject),
  3321. 0,
  3322. (destructor)dict_dealloc, /* tp_dealloc */
  3323. #if PY_MAJOR_VERSION < 3
  3324. (printfunc)ordereddict_print, /* tp_print */
  3325. #else
  3326. 0, /* tp_print */
  3327. #endif
  3328. 0, /* tp_getattr */
  3329. 0, /* tp_setattr */
  3330. #if PY_MAJOR_VERSION < 3
  3331. (cmpfunc)dict_compare, /* tp_compare */
  3332. #else
  3333. 0, /* tp_reserved */
  3334. #endif
  3335. (reprfunc)ordereddict_repr, /* tp_repr */
  3336. 0, /* tp_as_number */
  3337. &dict_as_sequence, /* tp_as_sequence */
  3338. &dict_as_mapping, /* tp_as_mapping */
  3339. (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
  3340. 0, /* tp_call */
  3341. 0, /* tp_str */
  3342. PyObject_GenericGetAttr, /* tp_getattro */
  3343. 0, /* tp_setattro */
  3344. 0, /* tp_as_buffer */
  3345. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  3346. Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
  3347. ordereddict_doc, /* tp_doc */
  3348. dict_traverse, /* tp_traverse */
  3349. dict_tp_clear, /* tp_clear */
  3350. dict_richcompare, /* tp_richcompare */
  3351. 0, /* tp_weaklistoffset */
  3352. (getiterfunc)dict_iter, /* tp_iter */
  3353. 0, /* tp_iternext */
  3354. ordereddict_methods, /* tp_methods */
  3355. 0, /* tp_members */
  3356. 0, /* tp_getset */
  3357. DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
  3358. 0, /* tp_dict */
  3359. 0, /* tp_descr_get */
  3360. 0, /* tp_descr_set */
  3361. 0, /* tp_dictoffset */
  3362. ordereddict_init, /* tp_init */
  3363. PyType_GenericAlloc, /* tp_alloc */
  3364. dict_new, /* tp_new */
  3365. PyObject_GC_Del, /* tp_free */
  3366. };
  3367. PyDoc_STRVAR(sorteddict_doc,
  3368. "sorteddict() -> new empty dictionary.\n"
  3369. );
  3370. PyTypeObject PySortedDict_Type = {
  3371. PyVarObject_HEAD_INIT(NULL, 0)
  3372. "_ordereddict.sorteddict",
  3373. sizeof(PySortedDictObject),
  3374. 0,
  3375. (destructor)dict_dealloc, /* tp_dealloc */
  3376. #if PY_MAJOR_VERSION < 3
  3377. (printfunc)ordereddict_print, /* tp_print */
  3378. #else
  3379. 0, /* tp_print */
  3380. #endif
  3381. 0, /* tp_getattr */
  3382. 0, /* tp_setattr */
  3383. #if PY_MAJOR_VERSION < 3
  3384. (cmpfunc)dict_compare, /* tp_compare */
  3385. #else
  3386. 0, /* tp_reserved */
  3387. #endif
  3388. (reprfunc)sorteddict_repr, /* tp_repr */
  3389. 0, /* tp_as_number */
  3390. &dict_as_sequence, /* tp_as_sequence */
  3391. &dict_as_mapping, /* tp_as_mapping */
  3392. (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
  3393. 0, /* tp_call */
  3394. 0, /* tp_str */
  3395. PyObject_GenericGetAttr, /* tp_getattro */
  3396. 0, /* tp_setattro */
  3397. 0, /* tp_as_buffer */
  3398. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  3399. Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
  3400. sorteddict_doc, /* tp_doc */
  3401. dict_traverse, /* tp_traverse */
  3402. dict_tp_clear, /* tp_clear */
  3403. dict_richcompare, /* tp_richcompare */
  3404. 0, /* tp_weaklistoffset */
  3405. (getiterfunc)dict_iter, /* tp_iter */
  3406. 0, /* tp_iternext */
  3407. ordereddict_methods, /* tp_methods */
  3408. 0, /* tp_members */
  3409. 0, /* tp_getset */
  3410. DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
  3411. 0, /* tp_dict */
  3412. 0, /* tp_descr_get */
  3413. 0, /* tp_descr_set */
  3414. 0, /* tp_dictoffset */
  3415. sorteddict_init, /* tp_init */
  3416. PyType_GenericAlloc, /* tp_alloc */
  3417. sorteddict_new, /* tp_new */
  3418. PyObject_GC_Del, /* tp_free */
  3419. };
  3420. /* Dictionary iterator types */
  3421. typedef struct {
  3422. PyObject_HEAD
  3423. PyOrderedDictObject *di_dict; /* Set to NULL when iterator is exhausted */
  3424. Py_ssize_t di_used;
  3425. Py_ssize_t di_pos;
  3426. PyObject* di_result; /* reusable result tuple for iteritems */
  3427. Py_ssize_t len;
  3428. int step;
  3429. } ordereddictiterobject;
  3430. static PyObject *
  3431. dictiter_new(PyOrderedDictObject *dict, PyTypeObject *itertype,
  3432. PyObject *args, PyObject *kwds)
  3433. {
  3434. ordereddictiterobject *di;
  3435. int reverse = 0;
  3436. static char *kwlist[] = {"reverse", 0};
  3437. if (args != NULL)
  3438. if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:keys",
  3439. kwlist, &reverse))
  3440. return NULL;
  3441. /* review: introduce GC_New */
  3442. di = PyObject_GC_New(ordereddictiterobject, itertype);
  3443. if (di == NULL)
  3444. return NULL;
  3445. Py_INCREF(dict);
  3446. di->di_dict = dict;
  3447. di->di_used = dict->ma_used;
  3448. di->len = dict->ma_used;
  3449. if (reverse) {
  3450. di->di_pos = (dict->ma_used) - 1;
  3451. di->step = -1;
  3452. } else {
  3453. di->di_pos = 0;
  3454. di->step = 1;
  3455. }
  3456. if (itertype == &PyOrderedDictIterItem_Type) {
  3457. di->di_result = PyTuple_Pack(2, Py_None, Py_None);
  3458. if (di->di_result == NULL) {
  3459. Py_DECREF(di);
  3460. return NULL;
  3461. }
  3462. } else
  3463. di->di_result = NULL;
  3464. PyObject_GC_Track(di);
  3465. return (PyObject *)di;
  3466. }
  3467. static void
  3468. dictiter_dealloc(ordereddictiterobject *di)
  3469. {
  3470. Py_XDECREF(di->di_dict);
  3471. Py_XDECREF(di->di_result);
  3472. PyObject_GC_Del(di);
  3473. }
  3474. static int
  3475. dictiter_traverse(ordereddictiterobject *di, visitproc visit, void *arg)
  3476. {
  3477. Py_VISIT(di->di_dict);
  3478. Py_VISIT(di->di_result);
  3479. return 0;
  3480. }
  3481. static PyObject *
  3482. dictiter_len(ordereddictiterobject *di)
  3483. {
  3484. Py_ssize_t len = 0;
  3485. if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
  3486. len = di->len;
  3487. #if PY_VERSION_HEX < 0x03000000
  3488. return PyInt_FromSize_t(len);
  3489. #else
  3490. return PyLong_FromSize_t(len);
  3491. #endif
  3492. }
  3493. PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
  3494. static PyMethodDef dictiter_methods[] = {
  3495. {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
  3496. {NULL, NULL} /* sentinel */
  3497. };
  3498. static PyObject *dictiter_iternextkey(ordereddictiterobject *di)
  3499. {
  3500. PyObject *key;
  3501. register Py_ssize_t i;
  3502. register PyOrderedDictEntry **epp;
  3503. PyOrderedDictObject *d = di->di_dict;
  3504. if (d == NULL)
  3505. return NULL;
  3506. assert (PyOrderedDict_Check(d));
  3507. if (di->di_used != d->ma_used) {
  3508. PyErr_SetString(PyExc_RuntimeError,
  3509. "dictionary changed size during iteration");
  3510. di->di_used = -1; /* Make this state sticky */
  3511. return NULL;
  3512. }
  3513. i = di->di_pos;
  3514. if (i < 0)
  3515. goto fail;
  3516. if (i >= d->ma_used)
  3517. goto fail;
  3518. epp = d->od_otablep;
  3519. di->di_pos = i+di->step;
  3520. di->len--; /* len can be calculated */
  3521. key = epp[i]->me_key;
  3522. Py_INCREF(key);
  3523. return key;
  3524. fail:
  3525. Py_DECREF(d);
  3526. di->di_dict = NULL;
  3527. return NULL;
  3528. }
  3529. PyTypeObject PyOrderedDictIterKey_Type = {
  3530. PyVarObject_HEAD_INIT(NULL, 0)
  3531. "_ordereddict.keyiterator", /* tp_name */
  3532. sizeof(ordereddictiterobject), /* tp_basicsize */
  3533. 0, /* tp_itemsize */
  3534. /* methods */
  3535. (destructor)dictiter_dealloc, /* tp_dealloc */
  3536. 0, /* tp_print */
  3537. 0, /* tp_getattr */
  3538. 0, /* tp_setattr */
  3539. 0, /* tp_compare */
  3540. 0, /* tp_repr */
  3541. 0, /* tp_as_number */
  3542. 0, /* tp_as_sequence */
  3543. 0, /* tp_as_mapping */
  3544. 0, /* tp_hash */
  3545. 0, /* tp_call */
  3546. 0, /* tp_str */
  3547. PyObject_GenericGetAttr, /* tp_getattro */
  3548. 0, /* tp_setattro */
  3549. 0, /* tp_as_buffer */
  3550. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
  3551. 0, /* tp_doc */
  3552. (traverseproc)dictiter_traverse, /* tp_traverse */
  3553. 0, /* tp_clear */
  3554. 0, /* tp_richcompare */
  3555. 0, /* tp_weaklistoffset */
  3556. PyObject_SelfIter, /* tp_iter */
  3557. (iternextfunc)dictiter_iternextkey, /* tp_iternext */
  3558. dictiter_methods, /* tp_methods */
  3559. 0,
  3560. };
  3561. static PyObject *dictiter_iternextvalue(ordereddictiterobject *di)
  3562. {
  3563. PyObject *value;
  3564. register Py_ssize_t i;
  3565. register PyOrderedDictEntry **epp;
  3566. PyOrderedDictObject *d = di->di_dict;
  3567. if (d == NULL)
  3568. return NULL;
  3569. assert (PyOrderedDict_Check(d));
  3570. if (di->di_used != d->ma_used) {
  3571. PyErr_SetString(PyExc_RuntimeError,
  3572. "dictionary changed size during iteration");
  3573. di->di_used = -1; /* Make this state sticky */
  3574. return NULL;
  3575. }
  3576. i = di->di_pos;
  3577. if (i < 0 || i >= d->ma_used)
  3578. goto fail;
  3579. epp = d->od_otablep;
  3580. di->di_pos = i+di->step;
  3581. di->len--; /* len can be calculated */
  3582. value = epp[i]->me_value;
  3583. Py_INCREF(value);
  3584. return value;
  3585. fail:
  3586. Py_DECREF(d);
  3587. di->di_dict = NULL;
  3588. return NULL;
  3589. }
  3590. PyTypeObject PyOrderedDictIterValue_Type = {
  3591. PyVarObject_HEAD_INIT(NULL, 0)
  3592. "_ordereddict.valueiterator", /* tp_name */
  3593. sizeof(ordereddictiterobject), /* tp_basicsize */
  3594. 0, /* tp_itemsize */
  3595. /* methods */
  3596. (destructor)dictiter_dealloc, /* tp_dealloc */
  3597. 0, /* tp_print */
  3598. 0, /* tp_getattr */
  3599. 0, /* tp_setattr */
  3600. 0, /* tp_compare */
  3601. 0, /* tp_repr */
  3602. 0, /* tp_as_number */
  3603. 0, /* tp_as_sequence */
  3604. 0, /* tp_as_mapping */
  3605. 0, /* tp_hash */
  3606. 0, /* tp_call */
  3607. 0, /* tp_str */
  3608. PyObject_GenericGetAttr, /* tp_getattro */
  3609. 0, /* tp_setattro */
  3610. 0, /* tp_as_buffer */
  3611. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
  3612. 0, /* tp_doc */
  3613. (traverseproc)dictiter_traverse, /* tp_traverse */
  3614. 0, /* tp_clear */
  3615. 0, /* tp_richcompare */
  3616. 0, /* tp_weaklistoffset */
  3617. PyObject_SelfIter, /* tp_iter */
  3618. (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
  3619. dictiter_methods, /* tp_methods */
  3620. 0,
  3621. };
  3622. static PyObject *dictiter_iternextitem(ordereddictiterobject *di)
  3623. {
  3624. PyObject *key, *value, *result = di->di_result;
  3625. register Py_ssize_t i;
  3626. register PyOrderedDictEntry **epp;
  3627. PyOrderedDictObject *d = di->di_dict;
  3628. if (d == NULL)
  3629. return NULL;
  3630. assert (PyOrderedDict_Check(d));
  3631. if (di->di_used != d->ma_used) {
  3632. PyErr_SetString(PyExc_RuntimeError,
  3633. "dictionary changed size during iteration");
  3634. di->di_used = -1; /* Make this state sticky */
  3635. return NULL;
  3636. }
  3637. i = di->di_pos;
  3638. if (i < 0)
  3639. goto fail;
  3640. /* review: differs in 2.5.6 */
  3641. if (i >= d->ma_used)
  3642. goto fail;
  3643. epp = d->od_otablep;
  3644. di->di_pos = i+di->step;
  3645. if (result->ob_refcnt == 1) {
  3646. Py_INCREF(result);
  3647. Py_DECREF(PyTuple_GET_ITEM(result, 0));
  3648. Py_DECREF(PyTuple_GET_ITEM(result, 1));
  3649. } else {
  3650. result = PyTuple_New(2);
  3651. if (result == NULL)
  3652. return NULL;
  3653. }
  3654. di->len--; /* len can be calculated */
  3655. key = epp[i]->me_key;
  3656. value = epp[i]->me_value;
  3657. Py_INCREF(key);
  3658. Py_INCREF(value);
  3659. PyTuple_SET_ITEM(result, 0, key);
  3660. PyTuple_SET_ITEM(result, 1, value);
  3661. return result;
  3662. fail:
  3663. Py_DECREF(d);
  3664. di->di_dict = NULL;
  3665. return NULL;
  3666. }
  3667. PyTypeObject PyOrderedDictIterItem_Type = {
  3668. PyVarObject_HEAD_INIT(NULL, 0)
  3669. "_ordereddict.itemiterator", /* tp_name */
  3670. sizeof(ordereddictiterobject), /* tp_basicsize */
  3671. 0, /* tp_itemsize */
  3672. /* methods */
  3673. (destructor)dictiter_dealloc, /* tp_dealloc */
  3674. 0, /* tp_print */
  3675. 0, /* tp_getattr */
  3676. 0, /* tp_setattr */
  3677. 0, /* tp_compare */
  3678. 0, /* tp_repr */
  3679. 0, /* tp_as_number */
  3680. 0, /* tp_as_sequence */
  3681. 0, /* tp_as_mapping */
  3682. 0, /* tp_hash */
  3683. 0, /* tp_call */
  3684. 0, /* tp_str */
  3685. PyObject_GenericGetAttr, /* tp_getattro */
  3686. 0, /* tp_setattro */
  3687. 0, /* tp_as_buffer */
  3688. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
  3689. 0, /* tp_doc */
  3690. (traverseproc)dictiter_traverse, /* tp_traverse */
  3691. 0, /* tp_clear */
  3692. 0, /* tp_richcompare */
  3693. 0, /* tp_weaklistoffset */
  3694. PyObject_SelfIter, /* tp_iter */
  3695. (iternextfunc)dictiter_iternextitem, /* tp_iternext */
  3696. dictiter_methods, /* tp_methods */
  3697. 0,
  3698. };
  3699. /*******************************************************************/
  3700. static PyObject *
  3701. getset_relaxed(PyObject *self, PyObject *args)
  3702. {
  3703. int n = -1, oldval = ordereddict_relaxed;
  3704. if (!PyArg_ParseTuple(args, "|i", &n))
  3705. return NULL;
  3706. if (n != -1) {
  3707. ordereddict_relaxed = n;
  3708. }
  3709. return PyBool_FromLong(oldval);
  3710. }
  3711. static PyObject *
  3712. getset_kvio(PyObject *self, PyObject *args)
  3713. {
  3714. int n = -1, oldval = ordereddict_kvio;
  3715. if (!PyArg_ParseTuple(args, "|i", &n))
  3716. return NULL;
  3717. if (n != -1) {
  3718. ordereddict_kvio = n;
  3719. }
  3720. return PyBool_FromLong(oldval);
  3721. }
  3722. static PyMethodDef ordereddict_functions[] = {
  3723. {
  3724. "relax", getset_relaxed, METH_VARARGS,
  3725. "get/set routine for allowing global undeordered dict initialisation"
  3726. },
  3727. {
  3728. "kvio", getset_kvio, METH_VARARGS,
  3729. "get/set routine for allowing global KeyValue Insertion Order initialisation"
  3730. },
  3731. {NULL, NULL} /* sentinel */
  3732. };
  3733. #if PY_VERSION_HEX >= 0x02070000
  3734. /* dictionary views are 2.7+ */
  3735. /***********************************************/
  3736. /* View objects for keys(), items(), values(). */
  3737. /***********************************************/
  3738. /* The instance lay-out is the same for all three; but the type differs. */
  3739. typedef struct {
  3740. PyObject_HEAD
  3741. PyOrderedDictObject *dv_dict;
  3742. } dictviewobject;
  3743. static void
  3744. dictview_dealloc(dictviewobject *dv)
  3745. {
  3746. Py_XDECREF(dv->dv_dict);
  3747. PyObject_GC_Del(dv);
  3748. }
  3749. static int
  3750. dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
  3751. {
  3752. Py_VISIT(dv->dv_dict);
  3753. return 0;
  3754. }
  3755. static Py_ssize_t
  3756. dictview_len(dictviewobject *dv)
  3757. {
  3758. Py_ssize_t len = 0;
  3759. if (dv->dv_dict != NULL)
  3760. len = dv->dv_dict->ma_used;
  3761. return len;
  3762. }
  3763. static PyObject *
  3764. dictview_new(PyObject *dict, PyTypeObject *type)
  3765. {
  3766. dictviewobject *dv;
  3767. if (dict == NULL) {
  3768. PyErr_BadInternalCall();
  3769. return NULL;
  3770. }
  3771. if (!PyDict_Check(dict)) {
  3772. /* XXX Get rid of this restriction later */
  3773. PyErr_Format(PyExc_TypeError,
  3774. "%s() requires a dict argument, not '%s'",
  3775. type->tp_name, dict->ob_type->tp_name);
  3776. return NULL;
  3777. }
  3778. dv = PyObject_GC_New(dictviewobject, type);
  3779. if (dv == NULL)
  3780. return NULL;
  3781. Py_INCREF(dict);
  3782. dv->dv_dict = (PyOrderedDictObject *)dict;
  3783. PyObject_GC_Track(dv);
  3784. return (PyObject *)dv;
  3785. }
  3786. /* TODO(guido): The views objects are not complete:
  3787. * support more set operations
  3788. * support arbitrary mappings?
  3789. - either these should be static or exported in dictobject.h
  3790. - if public then they should probably be in builtins
  3791. */
  3792. /* Return 1 if self is a subset of other, iterating over self;
  3793. 0 if not; -1 if an error occurred. */
  3794. static int
  3795. all_contained_in(PyObject *self, PyObject *other)
  3796. {
  3797. PyObject *iter = PyObject_GetIter(self);
  3798. int ok = 1;
  3799. if (iter == NULL)
  3800. return -1;
  3801. for (;;) {
  3802. PyObject *next = PyIter_Next(iter);
  3803. if (next == NULL) {
  3804. if (PyErr_Occurred())
  3805. ok = -1;
  3806. break;
  3807. }
  3808. ok = PySequence_Contains(other, next);
  3809. Py_DECREF(next);
  3810. if (ok <= 0)
  3811. break;
  3812. }
  3813. Py_DECREF(iter);
  3814. return ok;
  3815. }
  3816. static PyObject *
  3817. dictview_richcompare(PyObject *self, PyObject *other, int op)
  3818. {
  3819. Py_ssize_t len_self, len_other;
  3820. int ok;
  3821. PyObject *result;
  3822. assert(self != NULL);
  3823. assert(PyDictViewSet_Check(self));
  3824. assert(other != NULL);
  3825. if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
  3826. Py_INCREF(Py_NotImplemented);
  3827. return Py_NotImplemented;
  3828. }
  3829. len_self = PyObject_Size(self);
  3830. if (len_self < 0)
  3831. return NULL;
  3832. len_other = PyObject_Size(other);
  3833. if (len_other < 0)
  3834. return NULL;
  3835. ok = 0;
  3836. switch(op) {
  3837. case Py_NE:
  3838. case Py_EQ:
  3839. if (len_self == len_other)
  3840. ok = all_contained_in(self, other);
  3841. if (op == Py_NE && ok >= 0)
  3842. ok = !ok;
  3843. break;
  3844. case Py_LT:
  3845. if (len_self < len_other)
  3846. ok = all_contained_in(self, other);
  3847. break;
  3848. case Py_LE:
  3849. if (len_self <= len_other)
  3850. ok = all_contained_in(self, other);
  3851. break;
  3852. case Py_GT:
  3853. if (len_self > len_other)
  3854. ok = all_contained_in(other, self);
  3855. break;
  3856. case Py_GE:
  3857. if (len_self >= len_other)
  3858. ok = all_contained_in(other, self);
  3859. break;
  3860. }
  3861. if (ok < 0)
  3862. return NULL;
  3863. result = ok ? Py_True : Py_False;
  3864. Py_INCREF(result);
  3865. return result;
  3866. }
  3867. static PyObject *
  3868. dictview_repr(dictviewobject *dv)
  3869. {
  3870. PyObject *seq;
  3871. PyObject *result;
  3872. #if PY_MAJOR_VERSION < 3
  3873. PyObject *seq_str;
  3874. #endif
  3875. seq = PySequence_List((PyObject *)dv);
  3876. if (seq == NULL)
  3877. return NULL;
  3878. #if PY_MAJOR_VERSION < 3
  3879. seq_str = PyObject_Repr(seq);
  3880. if (seq_str == NULL) {
  3881. Py_DECREF(seq);
  3882. return NULL;
  3883. }
  3884. result = PyUNISTR_FromFormat("%s(%s)", Py_TYPE(dv)->tp_name,
  3885. PyString_AS_STRING(seq_str));
  3886. Py_DECREF(seq_str);
  3887. #else
  3888. result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
  3889. #endif
  3890. Py_DECREF(seq);
  3891. return result;
  3892. }
  3893. /*** dict_keys ***/
  3894. static PyObject *
  3895. dictkeys_iter(dictviewobject *dv)
  3896. {
  3897. if (dv->dv_dict == NULL) {
  3898. Py_RETURN_NONE;
  3899. }
  3900. return dictiter_new(dv->dv_dict, &PyOrderedDictIterKey_Type, NULL, NULL);
  3901. }
  3902. static int
  3903. dictkeys_contains(dictviewobject *dv, PyObject *obj)
  3904. {
  3905. if (dv->dv_dict == NULL)
  3906. return 0;
  3907. return PyDict_Contains((PyObject *)dv->dv_dict, obj);
  3908. }
  3909. static PySequenceMethods dictkeys_as_sequence = {
  3910. (lenfunc)dictview_len, /* sq_length */
  3911. 0, /* sq_concat */
  3912. 0, /* sq_repeat */
  3913. 0, /* sq_item */
  3914. 0, /* sq_slice */
  3915. 0, /* sq_ass_item */
  3916. 0, /* sq_ass_slice */
  3917. (objobjproc)dictkeys_contains, /* sq_contains */
  3918. };
  3919. static PyObject*
  3920. dictviews_sub(PyObject* self, PyObject *other)
  3921. {
  3922. PyObject *result = PySet_New(self);
  3923. PyObject *tmp;
  3924. if (result == NULL)
  3925. return NULL;
  3926. tmp = PyObject_CallMethod(result, "difference_update", "O", other);
  3927. if (tmp == NULL) {
  3928. Py_DECREF(result);
  3929. return NULL;
  3930. }
  3931. Py_DECREF(tmp);
  3932. return result;
  3933. }
  3934. static PyObject*
  3935. dictviews_and(PyObject* self, PyObject *other)
  3936. {
  3937. PyObject *result = PySet_New(self);
  3938. PyObject *tmp;
  3939. if (result == NULL)
  3940. return NULL;
  3941. tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
  3942. if (tmp == NULL) {
  3943. Py_DECREF(result);
  3944. return NULL;
  3945. }
  3946. Py_DECREF(tmp);
  3947. return result;
  3948. }
  3949. static PyObject*
  3950. dictviews_or(PyObject* self, PyObject *other)
  3951. {
  3952. PyObject *result = PySet_New(self);
  3953. PyObject *tmp;
  3954. if (result == NULL)
  3955. return NULL;
  3956. tmp = PyObject_CallMethod(result, "update", "O", other);
  3957. if (tmp == NULL) {
  3958. Py_DECREF(result);
  3959. return NULL;
  3960. }
  3961. Py_DECREF(tmp);
  3962. return result;
  3963. }
  3964. static PyObject*
  3965. dictviews_xor(PyObject* self, PyObject *other)
  3966. {
  3967. PyObject *result = PySet_New(self);
  3968. PyObject *tmp;
  3969. if (result == NULL)
  3970. return NULL;
  3971. tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
  3972. other);
  3973. if (tmp == NULL) {
  3974. Py_DECREF(result);
  3975. return NULL;
  3976. }
  3977. Py_DECREF(tmp);
  3978. return result;
  3979. }
  3980. static PyNumberMethods dictviews_as_number = {
  3981. 0, /*nb_add*/
  3982. (binaryfunc)dictviews_sub, /*nb_subtract*/
  3983. 0, /*nb_multiply*/
  3984. #if PY_MAJOR_VERSION < 3
  3985. 0, /*nb_divide*/
  3986. #endif
  3987. 0, /*nb_remainder*/
  3988. 0, /*nb_divmod*/
  3989. 0, /*nb_power*/
  3990. 0, /*nb_negative*/
  3991. 0, /*nb_positive*/
  3992. 0, /*nb_absolute*/
  3993. 0, /*nb_nonzero/nb_bool*/
  3994. 0, /*nb_invert*/
  3995. 0, /*nb_lshift*/
  3996. 0, /*nb_rshift*/
  3997. (binaryfunc)dictviews_and, /*nb_and*/
  3998. (binaryfunc)dictviews_xor, /*nb_xor*/
  3999. (binaryfunc)dictviews_or, /*nb_or*/
  4000. };
  4001. static PyMethodDef dictkeys_methods[] = {
  4002. {NULL, NULL} /* sentinel */
  4003. };
  4004. PyTypeObject PyOrderedDictKeys_Type = {
  4005. PyVarObject_HEAD_INIT(NULL, 0)
  4006. "dict_keys", /* tp_name */
  4007. sizeof(dictviewobject), /* tp_basicsize */
  4008. 0, /* tp_itemsize */
  4009. /* methods */
  4010. (destructor)dictview_dealloc, /* tp_dealloc */
  4011. 0, /* tp_print */
  4012. 0, /* tp_getattr */
  4013. 0, /* tp_setattr */
  4014. 0, /* tp_reserved */
  4015. (reprfunc)dictview_repr, /* tp_repr */
  4016. &dictviews_as_number, /* tp_as_number */
  4017. &dictkeys_as_sequence, /* tp_as_sequence */
  4018. 0, /* tp_as_mapping */
  4019. 0, /* tp_hash */
  4020. 0, /* tp_call */
  4021. 0, /* tp_str */
  4022. PyObject_GenericGetAttr, /* tp_getattro */
  4023. 0, /* tp_setattro */
  4024. 0, /* tp_as_buffer */
  4025. #if PY_MAJOR_VERSION < 3
  4026. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  4027. Py_TPFLAGS_CHECKTYPES, /* tp_flags */
  4028. #else
  4029. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
  4030. #endif
  4031. 0, /* tp_doc */
  4032. (traverseproc)dictview_traverse, /* tp_traverse */
  4033. 0, /* tp_clear */
  4034. dictview_richcompare, /* tp_richcompare */
  4035. 0, /* tp_weaklistoffset */
  4036. (getiterfunc)dictkeys_iter, /* tp_iter */
  4037. 0, /* tp_iternext */
  4038. dictkeys_methods, /* tp_methods */
  4039. 0,
  4040. };
  4041. static PyObject *
  4042. dictkeys_new(PyObject *dict)
  4043. {
  4044. return dictview_new(dict, &PyOrderedDictKeys_Type);
  4045. }
  4046. /*** dict_items ***/
  4047. static PyObject *
  4048. dictitems_iter(dictviewobject *dv)
  4049. {
  4050. if (dv->dv_dict == NULL) {
  4051. Py_RETURN_NONE;
  4052. }
  4053. return dictiter_new(dv->dv_dict, &PyOrderedDictIterItem_Type, NULL, NULL);
  4054. }
  4055. static int
  4056. dictitems_contains(dictviewobject *dv, PyObject *obj)
  4057. {
  4058. PyObject *key, *value, *found;
  4059. if (dv->dv_dict == NULL)
  4060. return 0;
  4061. if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
  4062. return 0;
  4063. key = PyTuple_GET_ITEM(obj, 0);
  4064. value = PyTuple_GET_ITEM(obj, 1);
  4065. found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
  4066. if (found == NULL) {
  4067. if (PyErr_Occurred())
  4068. return -1;
  4069. return 0;
  4070. }
  4071. return PyObject_RichCompareBool(value, found, Py_EQ);
  4072. }
  4073. static PySequenceMethods dictitems_as_sequence = {
  4074. (lenfunc)dictview_len, /* sq_length */
  4075. 0, /* sq_concat */
  4076. 0, /* sq_repeat */
  4077. 0, /* sq_item */
  4078. 0, /* sq_slice */
  4079. 0, /* sq_ass_item */
  4080. 0, /* sq_ass_slice */
  4081. (objobjproc)dictitems_contains, /* sq_contains */
  4082. };
  4083. static PyMethodDef dictitems_methods[] = {
  4084. {NULL, NULL} /* sentinel */
  4085. };
  4086. PyTypeObject PyOrderedDictItems_Type = {
  4087. PyVarObject_HEAD_INIT(NULL, 0)
  4088. "dict_items", /* tp_name */
  4089. sizeof(dictviewobject), /* tp_basicsize */
  4090. 0, /* tp_itemsize */
  4091. /* methods */
  4092. (destructor)dictview_dealloc, /* tp_dealloc */
  4093. 0, /* tp_print */
  4094. 0, /* tp_getattr */
  4095. 0, /* tp_setattr */
  4096. 0, /* tp_reserved */
  4097. (reprfunc)dictview_repr, /* tp_repr */
  4098. &dictviews_as_number, /* tp_as_number */
  4099. &dictitems_as_sequence, /* tp_as_sequence */
  4100. 0, /* tp_as_mapping */
  4101. 0, /* tp_hash */
  4102. 0, /* tp_call */
  4103. 0, /* tp_str */
  4104. PyObject_GenericGetAttr, /* tp_getattro */
  4105. 0, /* tp_setattro */
  4106. 0, /* tp_as_buffer */
  4107. #if PY_MAJOR_VERSION < 3
  4108. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  4109. Py_TPFLAGS_CHECKTYPES, /* tp_flags */
  4110. #else
  4111. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
  4112. #endif
  4113. 0, /* tp_doc */
  4114. (traverseproc)dictview_traverse, /* tp_traverse */
  4115. 0, /* tp_clear */
  4116. dictview_richcompare, /* tp_richcompare */
  4117. 0, /* tp_weaklistoffset */
  4118. (getiterfunc)dictitems_iter, /* tp_iter */
  4119. 0, /* tp_iternext */
  4120. dictitems_methods, /* tp_methods */
  4121. 0,
  4122. };
  4123. static PyObject *
  4124. dictitems_new(PyObject *dict)
  4125. {
  4126. return dictview_new(dict, &PyOrderedDictItems_Type);
  4127. }
  4128. /*** dict_values ***/
  4129. static PyObject *
  4130. dictvalues_iter(dictviewobject *dv)
  4131. {
  4132. if (dv->dv_dict == NULL) {
  4133. Py_RETURN_NONE;
  4134. }
  4135. return dictiter_new(dv->dv_dict, &PyOrderedDictIterValue_Type, NULL, NULL);
  4136. }
  4137. static PySequenceMethods dictvalues_as_sequence = {
  4138. (lenfunc)dictview_len, /* sq_length */
  4139. 0, /* sq_concat */
  4140. 0, /* sq_repeat */
  4141. 0, /* sq_item */
  4142. 0, /* sq_slice */
  4143. 0, /* sq_ass_item */
  4144. 0, /* sq_ass_slice */
  4145. (objobjproc)0, /* sq_contains */
  4146. };
  4147. static PyMethodDef dictvalues_methods[] = {
  4148. {NULL, NULL} /* sentinel */
  4149. };
  4150. PyTypeObject PyOrderedDictValues_Type = {
  4151. PyVarObject_HEAD_INIT(NULL, 0)
  4152. "dict_values", /* tp_name */
  4153. sizeof(dictviewobject), /* tp_basicsize */
  4154. 0, /* tp_itemsize */
  4155. /* methods */
  4156. (destructor)dictview_dealloc, /* tp_dealloc */
  4157. 0, /* tp_print */
  4158. 0, /* tp_getattr */
  4159. 0, /* tp_setattr */
  4160. 0, /* tp_reserved */
  4161. (reprfunc)dictview_repr, /* tp_repr */
  4162. 0, /* tp_as_number */
  4163. &dictvalues_as_sequence, /* tp_as_sequence */
  4164. 0, /* tp_as_mapping */
  4165. 0, /* tp_hash */
  4166. 0, /* tp_call */
  4167. 0, /* tp_str */
  4168. PyObject_GenericGetAttr, /* tp_getattro */
  4169. 0, /* tp_setattro */
  4170. 0, /* tp_as_buffer */
  4171. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
  4172. 0, /* tp_doc */
  4173. (traverseproc)dictview_traverse, /* tp_traverse */
  4174. 0, /* tp_clear */
  4175. 0, /* tp_richcompare */
  4176. 0, /* tp_weaklistoffset */
  4177. (getiterfunc)dictvalues_iter, /* tp_iter */
  4178. 0, /* tp_iternext */
  4179. dictvalues_methods, /* tp_methods */
  4180. 0,
  4181. };
  4182. static PyObject *
  4183. dictvalues_new(PyObject *dict)
  4184. {
  4185. return dictview_new(dict, &PyOrderedDictValues_Type);
  4186. }
  4187. #endif /* PY_VERSION_HEX >= 0x02070000 */
  4188. /************************************************************************/
  4189. #if PY_MAJOR_VERSION >= 3
  4190. static struct PyModuleDef moduledef = {
  4191. PyModuleDef_HEAD_INIT,
  4192. "_ordereddict", /* m_name */
  4193. ordereddict_doc, /* m_doc */
  4194. -1, /* m_size */
  4195. ordereddict_functions, /* m_methods */
  4196. NULL, /* m_reload */
  4197. NULL, /* m_traverse */
  4198. NULL, /* m_clear */
  4199. NULL, /* m_free */
  4200. };
  4201. #endif
  4202. static PyObject *
  4203. ruamel_ordereddict_moduleinit(void)
  4204. {
  4205. PyObject *m;
  4206. /* moved here as we have two primitives and dictobject.c had
  4207. no initialisation function */
  4208. if (dummy == NULL) { /* Auto-initialize dummy */
  4209. dummy = PyUNISTR_FromString("<dummy key>");
  4210. if (dummy == NULL)
  4211. return NULL;
  4212. #ifdef SHOW_CONVERSION_COUNTS
  4213. Py_AtExit(show_counts);
  4214. #endif
  4215. }
  4216. /* Fill in deferred data addresses. This must be done before
  4217. PyType_Ready() is called. Note that PyType_Ready() automatically
  4218. initializes the ob.ob_type field to &PyType_Type if it's NULL,
  4219. so it's not necessary to fill in ob_type first. */
  4220. PyOrderedDict_Type.tp_base = &PyDict_Type;
  4221. PySortedDict_Type.tp_base = &PyOrderedDict_Type;
  4222. if (PyType_Ready(&PyOrderedDict_Type) < 0)
  4223. return NULL;
  4224. if (PyType_Ready(&PySortedDict_Type) < 0)
  4225. return NULL;
  4226. /* AvdN: TODO understand why it is necessary or not (as it seems)
  4227. to PyTypeReady the iterator types
  4228. */
  4229. #if PY_MAJOR_VERSION >= 3
  4230. m = PyModule_Create(&moduledef);
  4231. #else
  4232. m = Py_InitModule3("_ordereddict",
  4233. ordereddict_functions,
  4234. ordereddict_doc
  4235. // , NULL, PYTHON_API_VERSION
  4236. );
  4237. #endif
  4238. if (m == NULL)
  4239. return NULL;
  4240. /* this allows PyVarObject_HEAD_INIT to take NULL as first
  4241. parameter: https://docs.python.org/3.1/extending/windows.html
  4242. */
  4243. if (PyType_Ready(&PyOrderedDict_Type) < 0)
  4244. return NULL;
  4245. Py_INCREF(&PyOrderedDict_Type);
  4246. if (PyModule_AddObject(m, "ordereddict",
  4247. (PyObject *) &PyOrderedDict_Type) < 0)
  4248. Py_INCREF(&PySortedDict_Type);
  4249. if (PyModule_AddObject(m, "sorteddict",
  4250. (PyObject *) &PySortedDict_Type) < 0)
  4251. return NULL;
  4252. return m;
  4253. }
  4254. #if PY_MAJOR_VERSION < 3
  4255. PyMODINIT_FUNC init_ordereddict(void)
  4256. {
  4257. ruamel_ordereddict_moduleinit();
  4258. }
  4259. #else
  4260. PyMODINIT_FUNC PyInit__ordereddict(void)
  4261. {
  4262. return ruamel_ordereddict_moduleinit();
  4263. }
  4264. #endif