123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458 |
- /*--------------------------------------------------------------------
- * Licensed to PSF under a Contributor Agreement.
- * See https://www.python.org/psf/license for licensing details.
- *
- * _elementtree - C accelerator for xml.etree.ElementTree
- * Copyright (c) 1999-2009 by Secret Labs AB. All rights reserved.
- * Copyright (c) 1999-2009 by Fredrik Lundh.
- *
- * info@pythonware.com
- * http://www.pythonware.com
- *--------------------------------------------------------------------
- */
- #define PY_SSIZE_T_CLEAN
- #include "Python.h"
- #include "structmember.h" // PyMemberDef
- #include "expat.h"
- #include "pyexpat.h"
- /* -------------------------------------------------------------------- */
- /* configuration */
- /* An element can hold this many children without extra memory
- allocations. */
- #define STATIC_CHILDREN 4
- /* For best performance, chose a value so that 80-90% of all nodes
- have no more than the given number of children. Set this to zero
- to minimize the size of the element structure itself (this only
- helps if you have lots of leaf nodes with attributes). */
- /* Also note that pymalloc always allocates blocks in multiples of
- eight bytes. For the current C version of ElementTree, this means
- that the number of children should be an even number, at least on
- 32-bit platforms. */
- /* -------------------------------------------------------------------- */
- /* compiler tweaks */
- #if defined(_MSC_VER)
- #define LOCAL(type) static __inline type __fastcall
- #else
- #define LOCAL(type) static type
- #endif
- /* macros used to store 'join' flags in string object pointers. note
- that all use of text and tail as object pointers must be wrapped in
- JOIN_OBJ. see comments in the ElementObject definition for more
- info. */
- #define JOIN_GET(p) ((uintptr_t) (p) & 1)
- #define JOIN_SET(p, flag) ((void*) ((uintptr_t) (JOIN_OBJ(p)) | (flag)))
- #define JOIN_OBJ(p) ((PyObject*) ((uintptr_t) (p) & ~(uintptr_t)1))
- /* Py_SETREF for a PyObject* that uses a join flag. */
- Py_LOCAL_INLINE(void)
- _set_joined_ptr(PyObject **p, PyObject *new_joined_ptr)
- {
- PyObject *tmp = JOIN_OBJ(*p);
- *p = new_joined_ptr;
- Py_DECREF(tmp);
- }
- /* Py_CLEAR for a PyObject* that uses a join flag. Pass the pointer by
- * reference since this function sets it to NULL.
- */
- static void _clear_joined_ptr(PyObject **p)
- {
- if (*p) {
- _set_joined_ptr(p, NULL);
- }
- }
- /* Per-module state; PEP 3121 */
- typedef struct {
- PyObject *parseerror_obj;
- PyObject *deepcopy_obj;
- PyObject *elementpath_obj;
- PyObject *comment_factory;
- PyObject *pi_factory;
- /* Interned strings */
- PyObject *str_text;
- PyObject *str_tail;
- PyObject *str_append;
- PyObject *str_find;
- PyObject *str_findtext;
- PyObject *str_findall;
- PyObject *str_iterfind;
- PyObject *str_doctype;
- /* Types defined by this extension */
- PyTypeObject *Element_Type;
- PyTypeObject *ElementIter_Type;
- PyTypeObject *TreeBuilder_Type;
- PyTypeObject *XMLParser_Type;
- PyObject *expat_capsule;
- struct PyExpat_CAPI *expat_capi;
- } elementtreestate;
- static struct PyModuleDef elementtreemodule;
- /* Given a module object (assumed to be _elementtree), get its per-module
- * state.
- */
- static inline elementtreestate*
- get_elementtree_state(PyObject *module)
- {
- void *state = PyModule_GetState(module);
- assert(state != NULL);
- return (elementtreestate *)state;
- }
- static inline elementtreestate *
- get_elementtree_state_by_cls(PyTypeObject *cls)
- {
- void *state = PyType_GetModuleState(cls);
- assert(state != NULL);
- return (elementtreestate *)state;
- }
- static inline elementtreestate *
- get_elementtree_state_by_type(PyTypeObject *tp)
- {
- PyObject *mod = PyType_GetModuleByDef(tp, &elementtreemodule);
- assert(mod != NULL);
- return get_elementtree_state(mod);
- }
- static int
- elementtree_clear(PyObject *m)
- {
- elementtreestate *st = get_elementtree_state(m);
- Py_CLEAR(st->parseerror_obj);
- Py_CLEAR(st->deepcopy_obj);
- Py_CLEAR(st->elementpath_obj);
- Py_CLEAR(st->comment_factory);
- Py_CLEAR(st->pi_factory);
- // Interned strings
- Py_CLEAR(st->str_append);
- Py_CLEAR(st->str_find);
- Py_CLEAR(st->str_findall);
- Py_CLEAR(st->str_findtext);
- Py_CLEAR(st->str_iterfind);
- Py_CLEAR(st->str_tail);
- Py_CLEAR(st->str_text);
- Py_CLEAR(st->str_doctype);
- // Heap types
- Py_CLEAR(st->Element_Type);
- Py_CLEAR(st->ElementIter_Type);
- Py_CLEAR(st->TreeBuilder_Type);
- Py_CLEAR(st->XMLParser_Type);
- Py_CLEAR(st->expat_capsule);
- st->expat_capi = NULL;
- return 0;
- }
- static int
- elementtree_traverse(PyObject *m, visitproc visit, void *arg)
- {
- elementtreestate *st = get_elementtree_state(m);
- Py_VISIT(st->parseerror_obj);
- Py_VISIT(st->deepcopy_obj);
- Py_VISIT(st->elementpath_obj);
- Py_VISIT(st->comment_factory);
- Py_VISIT(st->pi_factory);
- // Heap types
- Py_VISIT(st->Element_Type);
- Py_VISIT(st->ElementIter_Type);
- Py_VISIT(st->TreeBuilder_Type);
- Py_VISIT(st->XMLParser_Type);
- Py_VISIT(st->expat_capsule);
- return 0;
- }
- static void
- elementtree_free(void *m)
- {
- elementtree_clear((PyObject *)m);
- }
- /* helpers */
- LOCAL(PyObject*)
- list_join(PyObject* list)
- {
- /* join list elements */
- PyObject* joiner;
- PyObject* result;
- joiner = PyUnicode_FromStringAndSize("", 0);
- if (!joiner)
- return NULL;
- result = PyUnicode_Join(joiner, list);
- Py_DECREF(joiner);
- return result;
- }
- /* Is the given object an empty dictionary?
- */
- static int
- is_empty_dict(PyObject *obj)
- {
- return PyDict_CheckExact(obj) && PyDict_GET_SIZE(obj) == 0;
- }
- /* -------------------------------------------------------------------- */
- /* the Element type */
- typedef struct {
- /* attributes (a dictionary object), or NULL if no attributes */
- PyObject* attrib;
- /* child elements */
- Py_ssize_t length; /* actual number of items */
- Py_ssize_t allocated; /* allocated items */
- /* this either points to _children or to a malloced buffer */
- PyObject* *children;
- PyObject* _children[STATIC_CHILDREN];
- } ElementObjectExtra;
- typedef struct {
- PyObject_HEAD
- /* element tag (a string). */
- PyObject* tag;
- /* text before first child. note that this is a tagged pointer;
- use JOIN_OBJ to get the object pointer. the join flag is used
- to distinguish lists created by the tree builder from lists
- assigned to the attribute by application code; the former
- should be joined before being returned to the user, the latter
- should be left intact. */
- PyObject* text;
- /* text after this element, in parent. note that this is a tagged
- pointer; use JOIN_OBJ to get the object pointer. */
- PyObject* tail;
- ElementObjectExtra* extra;
- PyObject *weakreflist; /* For tp_weaklistoffset */
- } ElementObject;
- #define Element_CheckExact(st, op) Py_IS_TYPE(op, (st)->Element_Type)
- #define Element_Check(st, op) PyObject_TypeCheck(op, (st)->Element_Type)
- /* -------------------------------------------------------------------- */
- /* Element constructors and destructor */
- LOCAL(int)
- create_extra(ElementObject* self, PyObject* attrib)
- {
- self->extra = PyObject_Malloc(sizeof(ElementObjectExtra));
- if (!self->extra) {
- PyErr_NoMemory();
- return -1;
- }
- self->extra->attrib = Py_XNewRef(attrib);
- self->extra->length = 0;
- self->extra->allocated = STATIC_CHILDREN;
- self->extra->children = self->extra->_children;
- return 0;
- }
- LOCAL(void)
- dealloc_extra(ElementObjectExtra *extra)
- {
- Py_ssize_t i;
- if (!extra)
- return;
- Py_XDECREF(extra->attrib);
- for (i = 0; i < extra->length; i++)
- Py_DECREF(extra->children[i]);
- if (extra->children != extra->_children)
- PyObject_Free(extra->children);
- PyObject_Free(extra);
- }
- LOCAL(void)
- clear_extra(ElementObject* self)
- {
- ElementObjectExtra *myextra;
- if (!self->extra)
- return;
- /* Avoid DECREFs calling into this code again (cycles, etc.)
- */
- myextra = self->extra;
- self->extra = NULL;
- dealloc_extra(myextra);
- }
- /* Convenience internal function to create new Element objects with the given
- * tag and attributes.
- */
- LOCAL(PyObject*)
- create_new_element(elementtreestate *st, PyObject *tag, PyObject *attrib)
- {
- ElementObject* self;
- self = PyObject_GC_New(ElementObject, st->Element_Type);
- if (self == NULL)
- return NULL;
- self->extra = NULL;
- self->tag = Py_NewRef(tag);
- self->text = Py_NewRef(Py_None);
- self->tail = Py_NewRef(Py_None);
- self->weakreflist = NULL;
- PyObject_GC_Track(self);
- if (attrib != NULL && !is_empty_dict(attrib)) {
- if (create_extra(self, attrib) < 0) {
- Py_DECREF(self);
- return NULL;
- }
- }
- return (PyObject*) self;
- }
- static PyObject *
- element_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
- {
- ElementObject *e = (ElementObject *)type->tp_alloc(type, 0);
- if (e != NULL) {
- e->tag = Py_NewRef(Py_None);
- e->text = Py_NewRef(Py_None);
- e->tail = Py_NewRef(Py_None);
- e->extra = NULL;
- e->weakreflist = NULL;
- }
- return (PyObject *)e;
- }
- /* Helper function for extracting the attrib dictionary from a keywords dict.
- * This is required by some constructors/functions in this module that can
- * either accept attrib as a keyword argument or all attributes splashed
- * directly into *kwds.
- *
- * Return a dictionary with the content of kwds merged into the content of
- * attrib. If there is no attrib keyword, return a copy of kwds.
- */
- static PyObject*
- get_attrib_from_keywords(PyObject *kwds)
- {
- PyObject *attrib_str = PyUnicode_FromString("attrib");
- if (attrib_str == NULL) {
- return NULL;
- }
- PyObject *attrib = PyDict_GetItemWithError(kwds, attrib_str);
- if (attrib) {
- /* If attrib was found in kwds, copy its value and remove it from
- * kwds
- */
- if (!PyDict_Check(attrib)) {
- Py_DECREF(attrib_str);
- PyErr_Format(PyExc_TypeError, "attrib must be dict, not %.100s",
- Py_TYPE(attrib)->tp_name);
- return NULL;
- }
- attrib = PyDict_Copy(attrib);
- if (attrib && PyDict_DelItem(kwds, attrib_str) < 0) {
- Py_SETREF(attrib, NULL);
- }
- }
- else if (!PyErr_Occurred()) {
- attrib = PyDict_New();
- }
- Py_DECREF(attrib_str);
- if (attrib != NULL && PyDict_Update(attrib, kwds) < 0) {
- Py_DECREF(attrib);
- return NULL;
- }
- return attrib;
- }
- /*[clinic input]
- module _elementtree
- class _elementtree.Element "ElementObject *" "clinic_state()->Element_Type"
- class _elementtree.TreeBuilder "TreeBuilderObject *" "clinic_state()->TreeBuilder_Type"
- class _elementtree.XMLParser "XMLParserObject *" "clinic_state()->XMLParser_Type"
- [clinic start generated code]*/
- /*[clinic end generated code: output=da39a3ee5e6b4b0d input=6c83ea832d2b0ef1]*/
- static int
- element_init(PyObject *self, PyObject *args, PyObject *kwds)
- {
- PyObject *tag;
- PyObject *attrib = NULL;
- ElementObject *self_elem;
- if (!PyArg_ParseTuple(args, "O|O!:Element", &tag, &PyDict_Type, &attrib))
- return -1;
- if (attrib) {
- /* attrib passed as positional arg */
- attrib = PyDict_Copy(attrib);
- if (!attrib)
- return -1;
- if (kwds) {
- if (PyDict_Update(attrib, kwds) < 0) {
- Py_DECREF(attrib);
- return -1;
- }
- }
- } else if (kwds) {
- /* have keywords args */
- attrib = get_attrib_from_keywords(kwds);
- if (!attrib)
- return -1;
- }
- self_elem = (ElementObject *)self;
- if (attrib != NULL && !is_empty_dict(attrib)) {
- if (create_extra(self_elem, attrib) < 0) {
- Py_DECREF(attrib);
- return -1;
- }
- }
- /* We own a reference to attrib here and it's no longer needed. */
- Py_XDECREF(attrib);
- /* Replace the objects already pointed to by tag, text and tail. */
- Py_XSETREF(self_elem->tag, Py_NewRef(tag));
- _set_joined_ptr(&self_elem->text, Py_NewRef(Py_None));
- _set_joined_ptr(&self_elem->tail, Py_NewRef(Py_None));
- return 0;
- }
- LOCAL(int)
- element_resize(ElementObject* self, Py_ssize_t extra)
- {
- Py_ssize_t size;
- PyObject* *children;
- assert(extra >= 0);
- /* make sure self->children can hold the given number of extra
- elements. set an exception and return -1 if allocation failed */
- if (!self->extra) {
- if (create_extra(self, NULL) < 0)
- return -1;
- }
- size = self->extra->length + extra; /* never overflows */
- if (size > self->extra->allocated) {
- /* use Python 2.4's list growth strategy */
- size = (size >> 3) + (size < 9 ? 3 : 6) + size;
- /* Coverity CID #182 size_error: Allocating 1 bytes to pointer "children"
- * which needs at least 4 bytes.
- * Although it's a false alarm always assume at least one child to
- * be safe.
- */
- size = size ? size : 1;
- if ((size_t)size > PY_SSIZE_T_MAX/sizeof(PyObject*))
- goto nomemory;
- if (self->extra->children != self->extra->_children) {
- /* Coverity CID #182 size_error: Allocating 1 bytes to pointer
- * "children", which needs at least 4 bytes. Although it's a
- * false alarm always assume at least one child to be safe.
- */
- children = PyObject_Realloc(self->extra->children,
- size * sizeof(PyObject*));
- if (!children)
- goto nomemory;
- } else {
- children = PyObject_Malloc(size * sizeof(PyObject*));
- if (!children)
- goto nomemory;
- /* copy existing children from static area to malloc buffer */
- memcpy(children, self->extra->children,
- self->extra->length * sizeof(PyObject*));
- }
- self->extra->children = children;
- self->extra->allocated = size;
- }
- return 0;
- nomemory:
- PyErr_NoMemory();
- return -1;
- }
- LOCAL(void)
- raise_type_error(PyObject *element)
- {
- PyErr_Format(PyExc_TypeError,
- "expected an Element, not \"%.200s\"",
- Py_TYPE(element)->tp_name);
- }
- LOCAL(int)
- element_add_subelement(elementtreestate *st, ElementObject *self,
- PyObject *element)
- {
- /* add a child element to a parent */
- if (!Element_Check(st, element)) {
- raise_type_error(element);
- return -1;
- }
- if (element_resize(self, 1) < 0)
- return -1;
- self->extra->children[self->extra->length] = Py_NewRef(element);
- self->extra->length++;
- return 0;
- }
- LOCAL(PyObject*)
- element_get_attrib(ElementObject* self)
- {
- /* return borrowed reference to attrib dictionary */
- /* note: this function assumes that the extra section exists */
- PyObject* res = self->extra->attrib;
- if (!res) {
- /* create missing dictionary */
- res = self->extra->attrib = PyDict_New();
- }
- return res;
- }
- LOCAL(PyObject*)
- element_get_text(ElementObject* self)
- {
- /* return borrowed reference to text attribute */
- PyObject *res = self->text;
- if (JOIN_GET(res)) {
- res = JOIN_OBJ(res);
- if (PyList_CheckExact(res)) {
- PyObject *tmp = list_join(res);
- if (!tmp)
- return NULL;
- self->text = tmp;
- Py_SETREF(res, tmp);
- }
- }
- return res;
- }
- LOCAL(PyObject*)
- element_get_tail(ElementObject* self)
- {
- /* return borrowed reference to text attribute */
- PyObject *res = self->tail;
- if (JOIN_GET(res)) {
- res = JOIN_OBJ(res);
- if (PyList_CheckExact(res)) {
- PyObject *tmp = list_join(res);
- if (!tmp)
- return NULL;
- self->tail = tmp;
- Py_SETREF(res, tmp);
- }
- }
- return res;
- }
- static PyObject*
- subelement(PyObject *self, PyObject *args, PyObject *kwds)
- {
- PyObject* elem;
- elementtreestate *st = get_elementtree_state(self);
- ElementObject* parent;
- PyObject* tag;
- PyObject* attrib = NULL;
- if (!PyArg_ParseTuple(args, "O!O|O!:SubElement",
- st->Element_Type, &parent, &tag,
- &PyDict_Type, &attrib)) {
- return NULL;
- }
- if (attrib) {
- /* attrib passed as positional arg */
- attrib = PyDict_Copy(attrib);
- if (!attrib)
- return NULL;
- if (kwds != NULL && PyDict_Update(attrib, kwds) < 0) {
- Py_DECREF(attrib);
- return NULL;
- }
- } else if (kwds) {
- /* have keyword args */
- attrib = get_attrib_from_keywords(kwds);
- if (!attrib)
- return NULL;
- } else {
- /* no attrib arg, no kwds, so no attribute */
- }
- elem = create_new_element(st, tag, attrib);
- Py_XDECREF(attrib);
- if (elem == NULL)
- return NULL;
- if (element_add_subelement(st, parent, elem) < 0) {
- Py_DECREF(elem);
- return NULL;
- }
- return elem;
- }
- static int
- element_gc_traverse(ElementObject *self, visitproc visit, void *arg)
- {
- Py_VISIT(Py_TYPE(self));
- Py_VISIT(self->tag);
- Py_VISIT(JOIN_OBJ(self->text));
- Py_VISIT(JOIN_OBJ(self->tail));
- if (self->extra) {
- Py_ssize_t i;
- Py_VISIT(self->extra->attrib);
- for (i = 0; i < self->extra->length; ++i)
- Py_VISIT(self->extra->children[i]);
- }
- return 0;
- }
- static int
- element_gc_clear(ElementObject *self)
- {
- Py_CLEAR(self->tag);
- _clear_joined_ptr(&self->text);
- _clear_joined_ptr(&self->tail);
- /* After dropping all references from extra, it's no longer valid anyway,
- * so fully deallocate it.
- */
- clear_extra(self);
- return 0;
- }
- static void
- element_dealloc(ElementObject* self)
- {
- PyTypeObject *tp = Py_TYPE(self);
- /* bpo-31095: UnTrack is needed before calling any callbacks */
- PyObject_GC_UnTrack(self);
- Py_TRASHCAN_BEGIN(self, element_dealloc)
- if (self->weakreflist != NULL)
- PyObject_ClearWeakRefs((PyObject *) self);
- /* element_gc_clear clears all references and deallocates extra
- */
- element_gc_clear(self);
- tp->tp_free((PyObject *)self);
- Py_DECREF(tp);
- Py_TRASHCAN_END
- }
- /* -------------------------------------------------------------------- */
- /*[clinic input]
- _elementtree.Element.append
- cls: defining_class
- subelement: object(subclass_of='clinic_state()->Element_Type')
- /
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element_append_impl(ElementObject *self, PyTypeObject *cls,
- PyObject *subelement)
- /*[clinic end generated code: output=d00923711ea317fc input=8baf92679f9717b8]*/
- {
- elementtreestate *st = get_elementtree_state_by_cls(cls);
- if (element_add_subelement(st, self, subelement) < 0)
- return NULL;
- Py_RETURN_NONE;
- }
- /*[clinic input]
- _elementtree.Element.clear
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element_clear_impl(ElementObject *self)
- /*[clinic end generated code: output=8bcd7a51f94cfff6 input=3c719ff94bf45dd6]*/
- {
- clear_extra(self);
- _set_joined_ptr(&self->text, Py_NewRef(Py_None));
- _set_joined_ptr(&self->tail, Py_NewRef(Py_None));
- Py_RETURN_NONE;
- }
- /*[clinic input]
- _elementtree.Element.__copy__
- cls: defining_class
- /
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element___copy___impl(ElementObject *self, PyTypeObject *cls)
- /*[clinic end generated code: output=da22894421ff2b36 input=91edb92d9f441213]*/
- {
- Py_ssize_t i;
- ElementObject* element;
- elementtreestate *st = get_elementtree_state_by_cls(cls);
- element = (ElementObject*) create_new_element(
- st, self->tag, self->extra ? self->extra->attrib : NULL);
- if (!element)
- return NULL;
- Py_INCREF(JOIN_OBJ(self->text));
- _set_joined_ptr(&element->text, self->text);
- Py_INCREF(JOIN_OBJ(self->tail));
- _set_joined_ptr(&element->tail, self->tail);
- assert(!element->extra || !element->extra->length);
- if (self->extra) {
- if (element_resize(element, self->extra->length) < 0) {
- Py_DECREF(element);
- return NULL;
- }
- for (i = 0; i < self->extra->length; i++) {
- element->extra->children[i] = Py_NewRef(self->extra->children[i]);
- }
- assert(!element->extra->length);
- element->extra->length = self->extra->length;
- }
- return (PyObject*) element;
- }
- /* Helper for a deep copy. */
- LOCAL(PyObject *) deepcopy(elementtreestate *, PyObject *, PyObject *);
- /*[clinic input]
- _elementtree.Element.__deepcopy__
- memo: object(subclass_of="&PyDict_Type")
- /
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element___deepcopy___impl(ElementObject *self, PyObject *memo)
- /*[clinic end generated code: output=eefc3df50465b642 input=a2d40348c0aade10]*/
- {
- Py_ssize_t i;
- ElementObject* element;
- PyObject* tag;
- PyObject* attrib;
- PyObject* text;
- PyObject* tail;
- PyObject* id;
- PyTypeObject *tp = Py_TYPE(self);
- elementtreestate *st = get_elementtree_state_by_type(tp);
- tag = deepcopy(st, self->tag, memo);
- if (!tag)
- return NULL;
- if (self->extra && self->extra->attrib) {
- attrib = deepcopy(st, self->extra->attrib, memo);
- if (!attrib) {
- Py_DECREF(tag);
- return NULL;
- }
- } else {
- attrib = NULL;
- }
- element = (ElementObject*) create_new_element(st, tag, attrib);
- Py_DECREF(tag);
- Py_XDECREF(attrib);
- if (!element)
- return NULL;
- text = deepcopy(st, JOIN_OBJ(self->text), memo);
- if (!text)
- goto error;
- _set_joined_ptr(&element->text, JOIN_SET(text, JOIN_GET(self->text)));
- tail = deepcopy(st, JOIN_OBJ(self->tail), memo);
- if (!tail)
- goto error;
- _set_joined_ptr(&element->tail, JOIN_SET(tail, JOIN_GET(self->tail)));
- assert(!element->extra || !element->extra->length);
- if (self->extra) {
- if (element_resize(element, self->extra->length) < 0)
- goto error;
- for (i = 0; i < self->extra->length; i++) {
- PyObject* child = deepcopy(st, self->extra->children[i], memo);
- if (!child || !Element_Check(st, child)) {
- if (child) {
- raise_type_error(child);
- Py_DECREF(child);
- }
- element->extra->length = i;
- goto error;
- }
- element->extra->children[i] = child;
- }
- assert(!element->extra->length);
- element->extra->length = self->extra->length;
- }
- /* add object to memo dictionary (so deepcopy won't visit it again) */
- id = PyLong_FromSsize_t((uintptr_t) self);
- if (!id)
- goto error;
- i = PyDict_SetItem(memo, id, (PyObject*) element);
- Py_DECREF(id);
- if (i < 0)
- goto error;
- return (PyObject*) element;
- error:
- Py_DECREF(element);
- return NULL;
- }
- LOCAL(PyObject *)
- deepcopy(elementtreestate *st, PyObject *object, PyObject *memo)
- {
- /* do a deep copy of the given object */
- PyObject *stack[2];
- /* Fast paths */
- if (object == Py_None || PyUnicode_CheckExact(object)) {
- return Py_NewRef(object);
- }
- if (Py_REFCNT(object) == 1) {
- if (PyDict_CheckExact(object)) {
- PyObject *key, *value;
- Py_ssize_t pos = 0;
- int simple = 1;
- while (PyDict_Next(object, &pos, &key, &value)) {
- if (!PyUnicode_CheckExact(key) || !PyUnicode_CheckExact(value)) {
- simple = 0;
- break;
- }
- }
- if (simple)
- return PyDict_Copy(object);
- /* Fall through to general case */
- }
- else if (Element_CheckExact(st, object)) {
- return _elementtree_Element___deepcopy___impl(
- (ElementObject *)object, memo);
- }
- }
- /* General case */
- if (!st->deepcopy_obj) {
- PyErr_SetString(PyExc_RuntimeError,
- "deepcopy helper not found");
- return NULL;
- }
- stack[0] = object;
- stack[1] = memo;
- return _PyObject_FastCall(st->deepcopy_obj, stack, 2);
- }
- /*[clinic input]
- _elementtree.Element.__sizeof__ -> size_t
- [clinic start generated code]*/
- static size_t
- _elementtree_Element___sizeof___impl(ElementObject *self)
- /*[clinic end generated code: output=baae4e7ae9fe04ec input=54e298c501f3e0d0]*/
- {
- size_t result = _PyObject_SIZE(Py_TYPE(self));
- if (self->extra) {
- result += sizeof(ElementObjectExtra);
- if (self->extra->children != self->extra->_children) {
- result += (size_t)self->extra->allocated * sizeof(PyObject*);
- }
- }
- return result;
- }
- /* dict keys for getstate/setstate. */
- #define PICKLED_TAG "tag"
- #define PICKLED_CHILDREN "_children"
- #define PICKLED_ATTRIB "attrib"
- #define PICKLED_TAIL "tail"
- #define PICKLED_TEXT "text"
- /* __getstate__ returns a fabricated instance dict as in the pure-Python
- * Element implementation, for interoperability/interchangeability. This
- * makes the pure-Python implementation details an API, but (a) there aren't
- * any unnecessary structures there; and (b) it buys compatibility with 3.2
- * pickles. See issue #16076.
- */
- /*[clinic input]
- _elementtree.Element.__getstate__
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element___getstate___impl(ElementObject *self)
- /*[clinic end generated code: output=37279aeeb6bb5b04 input=f0d16d7ec2f7adc1]*/
- {
- Py_ssize_t i;
- PyObject *children, *attrib;
- /* Build a list of children. */
- children = PyList_New(self->extra ? self->extra->length : 0);
- if (!children)
- return NULL;
- for (i = 0; i < PyList_GET_SIZE(children); i++) {
- PyObject *child = Py_NewRef(self->extra->children[i]);
- PyList_SET_ITEM(children, i, child);
- }
- if (self->extra && self->extra->attrib) {
- attrib = Py_NewRef(self->extra->attrib);
- }
- else {
- attrib = PyDict_New();
- if (!attrib) {
- Py_DECREF(children);
- return NULL;
- }
- }
- return Py_BuildValue("{sOsNsNsOsO}",
- PICKLED_TAG, self->tag,
- PICKLED_CHILDREN, children,
- PICKLED_ATTRIB, attrib,
- PICKLED_TEXT, JOIN_OBJ(self->text),
- PICKLED_TAIL, JOIN_OBJ(self->tail));
- }
- static PyObject *
- element_setstate_from_attributes(elementtreestate *st,
- ElementObject *self,
- PyObject *tag,
- PyObject *attrib,
- PyObject *text,
- PyObject *tail,
- PyObject *children)
- {
- Py_ssize_t i, nchildren;
- ElementObjectExtra *oldextra = NULL;
- if (!tag) {
- PyErr_SetString(PyExc_TypeError, "tag may not be NULL");
- return NULL;
- }
- Py_XSETREF(self->tag, Py_NewRef(tag));
- text = text ? JOIN_SET(text, PyList_CheckExact(text)) : Py_None;
- Py_INCREF(JOIN_OBJ(text));
- _set_joined_ptr(&self->text, text);
- tail = tail ? JOIN_SET(tail, PyList_CheckExact(tail)) : Py_None;
- Py_INCREF(JOIN_OBJ(tail));
- _set_joined_ptr(&self->tail, tail);
- /* Handle ATTRIB and CHILDREN. */
- if (!children && !attrib) {
- Py_RETURN_NONE;
- }
- /* Compute 'nchildren'. */
- if (children) {
- if (!PyList_Check(children)) {
- PyErr_SetString(PyExc_TypeError, "'_children' is not a list");
- return NULL;
- }
- nchildren = PyList_GET_SIZE(children);
- /* (Re-)allocate 'extra'.
- Avoid DECREFs calling into this code again (cycles, etc.)
- */
- oldextra = self->extra;
- self->extra = NULL;
- if (element_resize(self, nchildren)) {
- assert(!self->extra || !self->extra->length);
- clear_extra(self);
- self->extra = oldextra;
- return NULL;
- }
- assert(self->extra);
- assert(self->extra->allocated >= nchildren);
- if (oldextra) {
- assert(self->extra->attrib == NULL);
- self->extra->attrib = oldextra->attrib;
- oldextra->attrib = NULL;
- }
- /* Copy children */
- for (i = 0; i < nchildren; i++) {
- PyObject *child = PyList_GET_ITEM(children, i);
- if (!Element_Check(st, child)) {
- raise_type_error(child);
- self->extra->length = i;
- dealloc_extra(oldextra);
- return NULL;
- }
- self->extra->children[i] = Py_NewRef(child);
- }
- assert(!self->extra->length);
- self->extra->length = nchildren;
- }
- else {
- if (element_resize(self, 0)) {
- return NULL;
- }
- }
- /* Stash attrib. */
- Py_XSETREF(self->extra->attrib, Py_XNewRef(attrib));
- dealloc_extra(oldextra);
- Py_RETURN_NONE;
- }
- /* __setstate__ for Element instance from the Python implementation.
- * 'state' should be the instance dict.
- */
- static PyObject *
- element_setstate_from_Python(elementtreestate *st, ElementObject *self,
- PyObject *state)
- {
- static char *kwlist[] = {PICKLED_TAG, PICKLED_ATTRIB, PICKLED_TEXT,
- PICKLED_TAIL, PICKLED_CHILDREN, 0};
- PyObject *args;
- PyObject *tag, *attrib, *text, *tail, *children;
- PyObject *retval;
- tag = attrib = text = tail = children = NULL;
- args = PyTuple_New(0);
- if (!args)
- return NULL;
- if (PyArg_ParseTupleAndKeywords(args, state, "|$OOOOO", kwlist, &tag,
- &attrib, &text, &tail, &children))
- retval = element_setstate_from_attributes(st, self, tag, attrib, text,
- tail, children);
- else
- retval = NULL;
- Py_DECREF(args);
- return retval;
- }
- /*[clinic input]
- _elementtree.Element.__setstate__
- cls: defining_class
- state: object
- /
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element___setstate___impl(ElementObject *self,
- PyTypeObject *cls, PyObject *state)
- /*[clinic end generated code: output=598bfb5730f71509 input=13830488d35d51f7]*/
- {
- if (!PyDict_CheckExact(state)) {
- PyErr_Format(PyExc_TypeError,
- "Don't know how to unpickle \"%.200R\" as an Element",
- state);
- return NULL;
- }
- else {
- elementtreestate *st = get_elementtree_state_by_cls(cls);
- return element_setstate_from_Python(st, self, state);
- }
- }
- LOCAL(int)
- checkpath(PyObject* tag)
- {
- Py_ssize_t i;
- int check = 1;
- /* check if a tag contains an xpath character */
- #define PATHCHAR(ch) \
- (ch == '/' || ch == '*' || ch == '[' || ch == '@' || ch == '.')
- if (PyUnicode_Check(tag)) {
- const Py_ssize_t len = PyUnicode_GET_LENGTH(tag);
- const void *data = PyUnicode_DATA(tag);
- int kind = PyUnicode_KIND(tag);
- if (len >= 3 && PyUnicode_READ(kind, data, 0) == '{' && (
- PyUnicode_READ(kind, data, 1) == '}' || (
- PyUnicode_READ(kind, data, 1) == '*' &&
- PyUnicode_READ(kind, data, 2) == '}'))) {
- /* wildcard: '{}tag' or '{*}tag' */
- return 1;
- }
- for (i = 0; i < len; i++) {
- Py_UCS4 ch = PyUnicode_READ(kind, data, i);
- if (ch == '{')
- check = 0;
- else if (ch == '}')
- check = 1;
- else if (check && PATHCHAR(ch))
- return 1;
- }
- return 0;
- }
- if (PyBytes_Check(tag)) {
- const char *p = PyBytes_AS_STRING(tag);
- const Py_ssize_t len = PyBytes_GET_SIZE(tag);
- if (len >= 3 && p[0] == '{' && (
- p[1] == '}' || (p[1] == '*' && p[2] == '}'))) {
- /* wildcard: '{}tag' or '{*}tag' */
- return 1;
- }
- for (i = 0; i < len; i++) {
- if (p[i] == '{')
- check = 0;
- else if (p[i] == '}')
- check = 1;
- else if (check && PATHCHAR(p[i]))
- return 1;
- }
- return 0;
- }
- return 1; /* unknown type; might be path expression */
- }
- /*[clinic input]
- _elementtree.Element.extend
- cls: defining_class
- elements: object
- /
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element_extend_impl(ElementObject *self, PyTypeObject *cls,
- PyObject *elements)
- /*[clinic end generated code: output=3e86d37fac542216 input=6479b1b5379d09ae]*/
- {
- PyObject* seq;
- Py_ssize_t i;
- seq = PySequence_Fast(elements, "");
- if (!seq) {
- PyErr_Format(
- PyExc_TypeError,
- "expected sequence, not \"%.200s\"", Py_TYPE(elements)->tp_name
- );
- return NULL;
- }
- elementtreestate *st = get_elementtree_state_by_cls(cls);
- for (i = 0; i < PySequence_Fast_GET_SIZE(seq); i++) {
- PyObject* element = Py_NewRef(PySequence_Fast_GET_ITEM(seq, i));
- if (element_add_subelement(st, self, element) < 0) {
- Py_DECREF(seq);
- Py_DECREF(element);
- return NULL;
- }
- Py_DECREF(element);
- }
- Py_DECREF(seq);
- Py_RETURN_NONE;
- }
- /*[clinic input]
- _elementtree.Element.find
- cls: defining_class
- /
- path: object
- namespaces: object = None
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element_find_impl(ElementObject *self, PyTypeObject *cls,
- PyObject *path, PyObject *namespaces)
- /*[clinic end generated code: output=18f77d393c9fef1b input=94df8a83f956acc6]*/
- {
- Py_ssize_t i;
- elementtreestate *st = get_elementtree_state_by_cls(cls);
- if (checkpath(path) || namespaces != Py_None) {
- return PyObject_CallMethodObjArgs(
- st->elementpath_obj, st->str_find, self, path, namespaces, NULL
- );
- }
- if (!self->extra)
- Py_RETURN_NONE;
- for (i = 0; i < self->extra->length; i++) {
- PyObject* item = self->extra->children[i];
- int rc;
- assert(Element_Check(st, item));
- Py_INCREF(item);
- rc = PyObject_RichCompareBool(((ElementObject*)item)->tag, path, Py_EQ);
- if (rc > 0)
- return item;
- Py_DECREF(item);
- if (rc < 0)
- return NULL;
- }
- Py_RETURN_NONE;
- }
- /*[clinic input]
- _elementtree.Element.findtext
- cls: defining_class
- /
- path: object
- default: object = None
- namespaces: object = None
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element_findtext_impl(ElementObject *self, PyTypeObject *cls,
- PyObject *path, PyObject *default_value,
- PyObject *namespaces)
- /*[clinic end generated code: output=6af7a2d96aac32cb input=32f252099f62a3d2]*/
- {
- Py_ssize_t i;
- elementtreestate *st = get_elementtree_state_by_cls(cls);
- if (checkpath(path) || namespaces != Py_None)
- return PyObject_CallMethodObjArgs(
- st->elementpath_obj, st->str_findtext,
- self, path, default_value, namespaces, NULL
- );
- if (!self->extra) {
- return Py_NewRef(default_value);
- }
- for (i = 0; i < self->extra->length; i++) {
- PyObject *item = self->extra->children[i];
- int rc;
- assert(Element_Check(st, item));
- Py_INCREF(item);
- rc = PyObject_RichCompareBool(((ElementObject*)item)->tag, path, Py_EQ);
- if (rc > 0) {
- PyObject* text = element_get_text((ElementObject*)item);
- if (text == Py_None) {
- Py_DECREF(item);
- return PyUnicode_New(0, 0);
- }
- Py_XINCREF(text);
- Py_DECREF(item);
- return text;
- }
- Py_DECREF(item);
- if (rc < 0)
- return NULL;
- }
- return Py_NewRef(default_value);
- }
- /*[clinic input]
- _elementtree.Element.findall
- cls: defining_class
- /
- path: object
- namespaces: object = None
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element_findall_impl(ElementObject *self, PyTypeObject *cls,
- PyObject *path, PyObject *namespaces)
- /*[clinic end generated code: output=65e39a1208f3b59e input=7aa0db45673fc9a5]*/
- {
- Py_ssize_t i;
- PyObject* out;
- elementtreestate *st = get_elementtree_state_by_cls(cls);
- if (checkpath(path) || namespaces != Py_None) {
- return PyObject_CallMethodObjArgs(
- st->elementpath_obj, st->str_findall, self, path, namespaces, NULL
- );
- }
- out = PyList_New(0);
- if (!out)
- return NULL;
- if (!self->extra)
- return out;
- for (i = 0; i < self->extra->length; i++) {
- PyObject* item = self->extra->children[i];
- int rc;
- assert(Element_Check(st, item));
- Py_INCREF(item);
- rc = PyObject_RichCompareBool(((ElementObject*)item)->tag, path, Py_EQ);
- if (rc != 0 && (rc < 0 || PyList_Append(out, item) < 0)) {
- Py_DECREF(item);
- Py_DECREF(out);
- return NULL;
- }
- Py_DECREF(item);
- }
- return out;
- }
- /*[clinic input]
- _elementtree.Element.iterfind
- cls: defining_class
- /
- path: object
- namespaces: object = None
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element_iterfind_impl(ElementObject *self, PyTypeObject *cls,
- PyObject *path, PyObject *namespaces)
- /*[clinic end generated code: output=be5c3f697a14e676 input=88766875a5c9a88b]*/
- {
- PyObject* tag = path;
- elementtreestate *st = get_elementtree_state_by_cls(cls);
- return PyObject_CallMethodObjArgs(
- st->elementpath_obj, st->str_iterfind, self, tag, namespaces, NULL);
- }
- /*[clinic input]
- _elementtree.Element.get
- key: object
- default: object = None
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element_get_impl(ElementObject *self, PyObject *key,
- PyObject *default_value)
- /*[clinic end generated code: output=523c614142595d75 input=ee153bbf8cdb246e]*/
- {
- if (self->extra && self->extra->attrib) {
- PyObject *attrib = Py_NewRef(self->extra->attrib);
- PyObject *value = Py_XNewRef(PyDict_GetItemWithError(attrib, key));
- Py_DECREF(attrib);
- if (value != NULL || PyErr_Occurred()) {
- return value;
- }
- }
- return Py_NewRef(default_value);
- }
- static PyObject *
- create_elementiter(elementtreestate *st, ElementObject *self, PyObject *tag,
- int gettext);
- /*[clinic input]
- _elementtree.Element.iter
- cls: defining_class
- /
- tag: object = None
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element_iter_impl(ElementObject *self, PyTypeObject *cls,
- PyObject *tag)
- /*[clinic end generated code: output=bff29dc5d4566c68 input=f6944c48d3f84c58]*/
- {
- if (PyUnicode_Check(tag)) {
- if (PyUnicode_READY(tag) < 0)
- return NULL;
- if (PyUnicode_GET_LENGTH(tag) == 1 && PyUnicode_READ_CHAR(tag, 0) == '*')
- tag = Py_None;
- }
- else if (PyBytes_Check(tag)) {
- if (PyBytes_GET_SIZE(tag) == 1 && *PyBytes_AS_STRING(tag) == '*')
- tag = Py_None;
- }
- elementtreestate *st = get_elementtree_state_by_cls(cls);
- return create_elementiter(st, self, tag, 0);
- }
- /*[clinic input]
- _elementtree.Element.itertext
- cls: defining_class
- /
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element_itertext_impl(ElementObject *self, PyTypeObject *cls)
- /*[clinic end generated code: output=fdeb2a3bca0ae063 input=a1ef1f0fc872a586]*/
- {
- elementtreestate *st = get_elementtree_state_by_cls(cls);
- return create_elementiter(st, self, Py_None, 1);
- }
- static PyObject*
- element_getitem(PyObject* self_, Py_ssize_t index)
- {
- ElementObject* self = (ElementObject*) self_;
- if (!self->extra || index < 0 || index >= self->extra->length) {
- PyErr_SetString(
- PyExc_IndexError,
- "child index out of range"
- );
- return NULL;
- }
- return Py_NewRef(self->extra->children[index]);
- }
- static int
- element_bool(PyObject* self_)
- {
- ElementObject* self = (ElementObject*) self_;
- if (PyErr_WarnEx(PyExc_DeprecationWarning,
- "Testing an element's truth value will raise an exception "
- "in future versions. Use specific 'len(elem)' or "
- "'elem is not None' test instead.",
- 1) < 0) {
- return -1;
- };
- if (self->extra ? self->extra->length : 0) {
- return 1;
- }
- return 0;
- }
- /*[clinic input]
- _elementtree.Element.insert
- index: Py_ssize_t
- subelement: object(subclass_of='clinic_state()->Element_Type')
- /
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element_insert_impl(ElementObject *self, Py_ssize_t index,
- PyObject *subelement)
- /*[clinic end generated code: output=990adfef4d424c0b input=9530f4905aa401ca]*/
- {
- Py_ssize_t i;
- if (!self->extra) {
- if (create_extra(self, NULL) < 0)
- return NULL;
- }
- if (index < 0) {
- index += self->extra->length;
- if (index < 0)
- index = 0;
- }
- if (index > self->extra->length)
- index = self->extra->length;
- if (element_resize(self, 1) < 0)
- return NULL;
- for (i = self->extra->length; i > index; i--)
- self->extra->children[i] = self->extra->children[i-1];
- self->extra->children[index] = Py_NewRef(subelement);
- self->extra->length++;
- Py_RETURN_NONE;
- }
- /*[clinic input]
- _elementtree.Element.items
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element_items_impl(ElementObject *self)
- /*[clinic end generated code: output=6db2c778ce3f5a4d input=adbe09aaea474447]*/
- {
- if (!self->extra || !self->extra->attrib)
- return PyList_New(0);
- return PyDict_Items(self->extra->attrib);
- }
- /*[clinic input]
- _elementtree.Element.keys
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element_keys_impl(ElementObject *self)
- /*[clinic end generated code: output=bc5bfabbf20eeb3c input=f02caf5b496b5b0b]*/
- {
- if (!self->extra || !self->extra->attrib)
- return PyList_New(0);
- return PyDict_Keys(self->extra->attrib);
- }
- static Py_ssize_t
- element_length(ElementObject* self)
- {
- if (!self->extra)
- return 0;
- return self->extra->length;
- }
- /*[clinic input]
- _elementtree.Element.makeelement
- cls: defining_class
- tag: object
- attrib: object(subclass_of='&PyDict_Type')
- /
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element_makeelement_impl(ElementObject *self, PyTypeObject *cls,
- PyObject *tag, PyObject *attrib)
- /*[clinic end generated code: output=d50bb17a47077d47 input=589829dab92f26e8]*/
- {
- PyObject* elem;
- attrib = PyDict_Copy(attrib);
- if (!attrib)
- return NULL;
- elementtreestate *st = get_elementtree_state_by_cls(cls);
- elem = create_new_element(st, tag, attrib);
- Py_DECREF(attrib);
- return elem;
- }
- /*[clinic input]
- _elementtree.Element.remove
- subelement: object(subclass_of='clinic_state()->Element_Type')
- /
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element_remove_impl(ElementObject *self, PyObject *subelement)
- /*[clinic end generated code: output=38fe6c07d6d87d1f input=6133e1d05597d5ee]*/
- {
- Py_ssize_t i;
- int rc;
- PyObject *found;
- if (!self->extra) {
- /* element has no children, so raise exception */
- PyErr_SetString(
- PyExc_ValueError,
- "list.remove(x): x not in list"
- );
- return NULL;
- }
- for (i = 0; i < self->extra->length; i++) {
- if (self->extra->children[i] == subelement)
- break;
- rc = PyObject_RichCompareBool(self->extra->children[i], subelement, Py_EQ);
- if (rc > 0)
- break;
- if (rc < 0)
- return NULL;
- }
- if (i >= self->extra->length) {
- /* subelement is not in children, so raise exception */
- PyErr_SetString(
- PyExc_ValueError,
- "list.remove(x): x not in list"
- );
- return NULL;
- }
- found = self->extra->children[i];
- self->extra->length--;
- for (; i < self->extra->length; i++)
- self->extra->children[i] = self->extra->children[i+1];
- Py_DECREF(found);
- Py_RETURN_NONE;
- }
- static PyObject*
- element_repr(ElementObject* self)
- {
- int status;
- if (self->tag == NULL)
- return PyUnicode_FromFormat("<Element at %p>", self);
- status = Py_ReprEnter((PyObject *)self);
- if (status == 0) {
- PyObject *res;
- res = PyUnicode_FromFormat("<Element %R at %p>", self->tag, self);
- Py_ReprLeave((PyObject *)self);
- return res;
- }
- if (status > 0)
- PyErr_Format(PyExc_RuntimeError,
- "reentrant call inside %s.__repr__",
- Py_TYPE(self)->tp_name);
- return NULL;
- }
- /*[clinic input]
- _elementtree.Element.set
- key: object
- value: object
- /
- [clinic start generated code]*/
- static PyObject *
- _elementtree_Element_set_impl(ElementObject *self, PyObject *key,
- PyObject *value)
- /*[clinic end generated code: output=fb938806be3c5656 input=1efe90f7d82b3fe9]*/
- {
- PyObject* attrib;
- if (!self->extra) {
- if (create_extra(self, NULL) < 0)
- return NULL;
- }
- attrib = element_get_attrib(self);
- if (!attrib)
- return NULL;
- if (PyDict_SetItem(attrib, key, value) < 0)
- return NULL;
- Py_RETURN_NONE;
- }
- static int
- element_setitem(PyObject* self_, Py_ssize_t index, PyObject* item)
- {
- ElementObject* self = (ElementObject*) self_;
- Py_ssize_t i;
- PyObject* old;
- if (!self->extra || index < 0 || index >= self->extra->length) {
- PyErr_SetString(
- PyExc_IndexError,
- "child assignment index out of range");
- return -1;
- }
- old = self->extra->children[index];
- if (item) {
- PyTypeObject *tp = Py_TYPE(self);
- elementtreestate *st = get_elementtree_state_by_type(tp);
- if (!Element_Check(st, item)) {
- raise_type_error(item);
- return -1;
- }
- self->extra->children[index] = Py_NewRef(item);
- } else {
- self->extra->length--;
- for (i = index; i < self->extra->length; i++)
- self->extra->children[i] = self->extra->children[i+1];
- }
- Py_DECREF(old);
- return 0;
- }
- static PyObject*
- element_subscr(PyObject* self_, PyObject* item)
- {
- ElementObject* self = (ElementObject*) self_;
- if (PyIndex_Check(item)) {
- Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
- if (i == -1 && PyErr_Occurred()) {
- return NULL;
- }
- if (i < 0 && self->extra)
- i += self->extra->length;
- return element_getitem(self_, i);
- }
- else if (PySlice_Check(item)) {
- Py_ssize_t start, stop, step, slicelen, i;
- size_t cur;
- PyObject* list;
- if (!self->extra)
- return PyList_New(0);
- if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
- return NULL;
- }
- slicelen = PySlice_AdjustIndices(self->extra->length, &start, &stop,
- step);
- if (slicelen <= 0)
- return PyList_New(0);
- else {
- list = PyList_New(slicelen);
- if (!list)
- return NULL;
- for (cur = start, i = 0; i < slicelen;
- cur += step, i++) {
- PyObject* item = Py_NewRef(self->extra->children[cur]);
- PyList_SET_ITEM(list, i, item);
- }
- return list;
- }
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "element indices must be integers");
- return NULL;
- }
- }
- static int
- element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value)
- {
- ElementObject* self = (ElementObject*) self_;
- if (PyIndex_Check(item)) {
- Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
- if (i == -1 && PyErr_Occurred()) {
- return -1;
- }
- if (i < 0 && self->extra)
- i += self->extra->length;
- return element_setitem(self_, i, value);
- }
- else if (PySlice_Check(item)) {
- Py_ssize_t start, stop, step, slicelen, newlen, i;
- size_t cur;
- PyObject* recycle = NULL;
- PyObject* seq;
- if (!self->extra) {
- if (create_extra(self, NULL) < 0)
- return -1;
- }
- if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
- return -1;
- }
- slicelen = PySlice_AdjustIndices(self->extra->length, &start, &stop,
- step);
- if (value == NULL) {
- /* Delete slice */
- size_t cur;
- Py_ssize_t i;
- if (slicelen <= 0)
- return 0;
- /* Since we're deleting, the direction of the range doesn't matter,
- * so for simplicity make it always ascending.
- */
- if (step < 0) {
- stop = start + 1;
- start = stop + step * (slicelen - 1) - 1;
- step = -step;
- }
- assert((size_t)slicelen <= SIZE_MAX / sizeof(PyObject *));
- /* recycle is a list that will contain all the children
- * scheduled for removal.
- */
- if (!(recycle = PyList_New(slicelen))) {
- return -1;
- }
- /* This loop walks over all the children that have to be deleted,
- * with cur pointing at them. num_moved is the amount of children
- * until the next deleted child that have to be "shifted down" to
- * occupy the deleted's places.
- * Note that in the ith iteration, shifting is done i+i places down
- * because i children were already removed.
- */
- for (cur = start, i = 0; cur < (size_t)stop; cur += step, ++i) {
- /* Compute how many children have to be moved, clipping at the
- * list end.
- */
- Py_ssize_t num_moved = step - 1;
- if (cur + step >= (size_t)self->extra->length) {
- num_moved = self->extra->length - cur - 1;
- }
- PyList_SET_ITEM(recycle, i, self->extra->children[cur]);
- memmove(
- self->extra->children + cur - i,
- self->extra->children + cur + 1,
- num_moved * sizeof(PyObject *));
- }
- /* Leftover "tail" after the last removed child */
- cur = start + (size_t)slicelen * step;
- if (cur < (size_t)self->extra->length) {
- memmove(
- self->extra->children + cur - slicelen,
- self->extra->children + cur,
- (self->extra->length - cur) * sizeof(PyObject *));
- }
- self->extra->length -= slicelen;
- /* Discard the recycle list with all the deleted sub-elements */
- Py_DECREF(recycle);
- return 0;
- }
- /* A new slice is actually being assigned */
- seq = PySequence_Fast(value, "");
- if (!seq) {
- PyErr_Format(
- PyExc_TypeError,
- "expected sequence, not \"%.200s\"", Py_TYPE(value)->tp_name
- );
- return -1;
- }
- newlen = PySequence_Fast_GET_SIZE(seq);
- if (step != 1 && newlen != slicelen)
- {
- Py_DECREF(seq);
- PyErr_Format(PyExc_ValueError,
- "attempt to assign sequence of size %zd "
- "to extended slice of size %zd",
- newlen, slicelen
- );
- return -1;
- }
- /* Resize before creating the recycle bin, to prevent refleaks. */
- if (newlen > slicelen) {
- if (element_resize(self, newlen - slicelen) < 0) {
- Py_DECREF(seq);
- return -1;
- }
- }
- PyTypeObject *tp = Py_TYPE(self);
- elementtreestate *st = get_elementtree_state_by_type(tp);
- for (i = 0; i < newlen; i++) {
- PyObject *element = PySequence_Fast_GET_ITEM(seq, i);
- if (!Element_Check(st, element)) {
- raise_type_error(element);
- Py_DECREF(seq);
- return -1;
- }
- }
- if (slicelen > 0) {
- /* to avoid recursive calls to this method (via decref), move
- old items to the recycle bin here, and get rid of them when
- we're done modifying the element */
- recycle = PyList_New(slicelen);
- if (!recycle) {
- Py_DECREF(seq);
- return -1;
- }
- for (cur = start, i = 0; i < slicelen;
- cur += step, i++)
- PyList_SET_ITEM(recycle, i, self->extra->children[cur]);
- }
- if (newlen < slicelen) {
- /* delete slice */
- for (i = stop; i < self->extra->length; i++)
- self->extra->children[i + newlen - slicelen] = self->extra->children[i];
- } else if (newlen > slicelen) {
- /* insert slice */
- for (i = self->extra->length-1; i >= stop; i--)
- self->extra->children[i + newlen - slicelen] = self->extra->children[i];
- }
- /* replace the slice */
- for (cur = start, i = 0; i < newlen;
- cur += step, i++) {
- PyObject* element = PySequence_Fast_GET_ITEM(seq, i);
- self->extra->children[cur] = Py_NewRef(element);
- }
- self->extra->length += newlen - slicelen;
- Py_DECREF(seq);
- /* discard the recycle bin, and everything in it */
- Py_XDECREF(recycle);
- return 0;
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "element indices must be integers");
- return -1;
- }
- }
- static PyObject*
- element_tag_getter(ElementObject *self, void *closure)
- {
- PyObject *res = self->tag;
- return Py_NewRef(res);
- }
- static PyObject*
- element_text_getter(ElementObject *self, void *closure)
- {
- PyObject *res = element_get_text(self);
- return Py_XNewRef(res);
- }
- static PyObject*
- element_tail_getter(ElementObject *self, void *closure)
- {
- PyObject *res = element_get_tail(self);
- return Py_XNewRef(res);
- }
- static PyObject*
- element_attrib_getter(ElementObject *self, void *closure)
- {
- PyObject *res;
- if (!self->extra) {
- if (create_extra(self, NULL) < 0)
- return NULL;
- }
- res = element_get_attrib(self);
- return Py_XNewRef(res);
- }
- /* macro for setter validation */
- #define _VALIDATE_ATTR_VALUE(V) \
- if ((V) == NULL) { \
- PyErr_SetString( \
- PyExc_AttributeError, \
- "can't delete element attribute"); \
- return -1; \
- }
- static int
- element_tag_setter(ElementObject *self, PyObject *value, void *closure)
- {
- _VALIDATE_ATTR_VALUE(value);
- Py_SETREF(self->tag, Py_NewRef(value));
- return 0;
- }
- static int
- element_text_setter(ElementObject *self, PyObject *value, void *closure)
- {
- _VALIDATE_ATTR_VALUE(value);
- _set_joined_ptr(&self->text, Py_NewRef(value));
- return 0;
- }
- static int
- element_tail_setter(ElementObject *self, PyObject *value, void *closure)
- {
- _VALIDATE_ATTR_VALUE(value);
- _set_joined_ptr(&self->tail, Py_NewRef(value));
- return 0;
- }
- static int
- element_attrib_setter(ElementObject *self, PyObject *value, void *closure)
- {
- _VALIDATE_ATTR_VALUE(value);
- if (!PyDict_Check(value)) {
- PyErr_Format(PyExc_TypeError,
- "attrib must be dict, not %.200s",
- Py_TYPE(value)->tp_name);
- return -1;
- }
- if (!self->extra) {
- if (create_extra(self, NULL) < 0)
- return -1;
- }
- Py_XSETREF(self->extra->attrib, Py_NewRef(value));
- return 0;
- }
- /******************************* Element iterator ****************************/
- /* ElementIterObject represents the iteration state over an XML element in
- * pre-order traversal. To keep track of which sub-element should be returned
- * next, a stack of parents is maintained. This is a standard stack-based
- * iterative pre-order traversal of a tree.
- * The stack is managed using a continuous array.
- * Each stack item contains the saved parent to which we should return after
- * the current one is exhausted, and the next child to examine in that parent.
- */
- typedef struct ParentLocator_t {
- ElementObject *parent;
- Py_ssize_t child_index;
- } ParentLocator;
- typedef struct {
- PyObject_HEAD
- ParentLocator *parent_stack;
- Py_ssize_t parent_stack_used;
- Py_ssize_t parent_stack_size;
- ElementObject *root_element;
- PyObject *sought_tag;
- int gettext;
- } ElementIterObject;
- static void
- elementiter_dealloc(ElementIterObject *it)
- {
- PyTypeObject *tp = Py_TYPE(it);
- Py_ssize_t i = it->parent_stack_used;
- it->parent_stack_used = 0;
- /* bpo-31095: UnTrack is needed before calling any callbacks */
- PyObject_GC_UnTrack(it);
- while (i--)
- Py_XDECREF(it->parent_stack[i].parent);
- PyMem_Free(it->parent_stack);
- Py_XDECREF(it->sought_tag);
- Py_XDECREF(it->root_element);
- tp->tp_free(it);
- Py_DECREF(tp);
- }
- static int
- elementiter_traverse(ElementIterObject *it, visitproc visit, void *arg)
- {
- Py_ssize_t i = it->parent_stack_used;
- while (i--)
- Py_VISIT(it->parent_stack[i].parent);
- Py_VISIT(it->root_element);
- Py_VISIT(it->sought_tag);
- Py_VISIT(Py_TYPE(it));
- return 0;
- }
- /* Helper function for elementiter_next. Add a new parent to the parent stack.
- */
- static int
- parent_stack_push_new(ElementIterObject *it, ElementObject *parent)
- {
- ParentLocator *item;
- if (it->parent_stack_used >= it->parent_stack_size) {
- Py_ssize_t new_size = it->parent_stack_size * 2; /* never overflow */
- ParentLocator *parent_stack = it->parent_stack;
- PyMem_Resize(parent_stack, ParentLocator, new_size);
- if (parent_stack == NULL)
- return -1;
- it->parent_stack = parent_stack;
- it->parent_stack_size = new_size;
- }
- item = it->parent_stack + it->parent_stack_used++;
- item->parent = (ElementObject*)Py_NewRef(parent);
- item->child_index = 0;
- return 0;
- }
- static PyObject *
- elementiter_next(ElementIterObject *it)
- {
- /* Sub-element iterator.
- *
- * A short note on gettext: this function serves both the iter() and
- * itertext() methods to avoid code duplication. However, there are a few
- * small differences in the way these iterations work. Namely:
- * - itertext() only yields text from nodes that have it, and continues
- * iterating when a node doesn't have text (so it doesn't return any
- * node like iter())
- * - itertext() also has to handle tail, after finishing with all the
- * children of a node.
- */
- int rc;
- ElementObject *elem;
- PyObject *text;
- while (1) {
- /* Handle the case reached in the beginning and end of iteration, where
- * the parent stack is empty. If root_element is NULL and we're here, the
- * iterator is exhausted.
- */
- if (!it->parent_stack_used) {
- if (!it->root_element) {
- PyErr_SetNone(PyExc_StopIteration);
- return NULL;
- }
- elem = it->root_element; /* steals a reference */
- it->root_element = NULL;
- }
- else {
- /* See if there are children left to traverse in the current parent. If
- * yes, visit the next child. If not, pop the stack and try again.
- */
- ParentLocator *item = &it->parent_stack[it->parent_stack_used - 1];
- Py_ssize_t child_index = item->child_index;
- ElementObjectExtra *extra;
- elem = item->parent;
- extra = elem->extra;
- if (!extra || child_index >= extra->length) {
- it->parent_stack_used--;
- /* Note that extra condition on it->parent_stack_used here;
- * this is because itertext() is supposed to only return *inner*
- * text, not text following the element it began iteration with.
- */
- if (it->gettext && it->parent_stack_used) {
- text = element_get_tail(elem);
- goto gettext;
- }
- Py_DECREF(elem);
- continue;
- }
- #ifndef NDEBUG
- PyTypeObject *tp = Py_TYPE(it);
- elementtreestate *st = get_elementtree_state_by_type(tp);
- assert(Element_Check(st, extra->children[child_index]));
- #endif
- elem = (ElementObject *)Py_NewRef(extra->children[child_index]);
- item->child_index++;
- }
- if (parent_stack_push_new(it, elem) < 0) {
- Py_DECREF(elem);
- PyErr_NoMemory();
- return NULL;
- }
- if (it->gettext) {
- text = element_get_text(elem);
- goto gettext;
- }
- if (it->sought_tag == Py_None)
- return (PyObject *)elem;
- rc = PyObject_RichCompareBool(elem->tag, it->sought_tag, Py_EQ);
- if (rc > 0)
- return (PyObject *)elem;
- Py_DECREF(elem);
- if (rc < 0)
- return NULL;
- continue;
- gettext:
- if (!text) {
- Py_DECREF(elem);
- return NULL;
- }
- if (text == Py_None) {
- Py_DECREF(elem);
- }
- else {
- Py_INCREF(text);
- Py_DECREF(elem);
- rc = PyObject_IsTrue(text);
- if (rc > 0)
- return text;
- Py_DECREF(text);
- if (rc < 0)
- return NULL;
- }
- }
- return NULL;
- }
- static PyType_Slot elementiter_slots[] = {
- {Py_tp_dealloc, elementiter_dealloc},
- {Py_tp_traverse, elementiter_traverse},
- {Py_tp_iter, PyObject_SelfIter},
- {Py_tp_iternext, elementiter_next},
- {0, NULL},
- };
- static PyType_Spec elementiter_spec = {
- /* Using the module's name since the pure-Python implementation does not
- have such a type. */
- .name = "_elementtree._element_iterator",
- .basicsize = sizeof(ElementIterObject),
- .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION),
- .slots = elementiter_slots,
- };
- #define INIT_PARENT_STACK_SIZE 8
- static PyObject *
- create_elementiter(elementtreestate *st, ElementObject *self, PyObject *tag,
- int gettext)
- {
- ElementIterObject *it;
- it = PyObject_GC_New(ElementIterObject, st->ElementIter_Type);
- if (!it)
- return NULL;
- it->sought_tag = Py_NewRef(tag);
- it->gettext = gettext;
- it->root_element = (ElementObject*)Py_NewRef(self);
- it->parent_stack = PyMem_New(ParentLocator, INIT_PARENT_STACK_SIZE);
- if (it->parent_stack == NULL) {
- Py_DECREF(it);
- PyErr_NoMemory();
- return NULL;
- }
- it->parent_stack_used = 0;
- it->parent_stack_size = INIT_PARENT_STACK_SIZE;
- PyObject_GC_Track(it);
- return (PyObject *)it;
- }
- /* ==================================================================== */
- /* the tree builder type */
- typedef struct {
- PyObject_HEAD
- PyObject *root; /* root node (first created node) */
- PyObject *this; /* current node */
- PyObject *last; /* most recently created node */
- PyObject *last_for_tail; /* most recently created node that takes a tail */
- PyObject *data; /* data collector (string or list), or NULL */
- PyObject *stack; /* element stack */
- Py_ssize_t index; /* current stack size (0 means empty) */
- PyObject *element_factory;
- PyObject *comment_factory;
- PyObject *pi_factory;
- /* element tracing */
- PyObject *events_append; /* the append method of the list of events, or NULL */
- PyObject *start_event_obj; /* event objects (NULL to ignore) */
- PyObject *end_event_obj;
- PyObject *start_ns_event_obj;
- PyObject *end_ns_event_obj;
- PyObject *comment_event_obj;
- PyObject *pi_event_obj;
- char insert_comments;
- char insert_pis;
- elementtreestate *state;
- } TreeBuilderObject;
- #define TreeBuilder_CheckExact(st, op) Py_IS_TYPE((op), (st)->TreeBuilder_Type)
- /* -------------------------------------------------------------------- */
- /* constructor and destructor */
- static PyObject *
- treebuilder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
- {
- TreeBuilderObject *t = (TreeBuilderObject *)type->tp_alloc(type, 0);
- if (t != NULL) {
- t->root = NULL;
- t->this = Py_NewRef(Py_None);
- t->last = Py_NewRef(Py_None);
- t->data = NULL;
- t->element_factory = NULL;
- t->comment_factory = NULL;
- t->pi_factory = NULL;
- t->stack = PyList_New(20);
- if (!t->stack) {
- Py_DECREF(t->this);
- Py_DECREF(t->last);
- Py_DECREF((PyObject *) t);
- return NULL;
- }
- t->index = 0;
- t->events_append = NULL;
- t->start_event_obj = t->end_event_obj = NULL;
- t->start_ns_event_obj = t->end_ns_event_obj = NULL;
- t->comment_event_obj = t->pi_event_obj = NULL;
- t->insert_comments = t->insert_pis = 0;
- t->state = get_elementtree_state_by_type(type);
- }
- return (PyObject *)t;
- }
- /*[clinic input]
- _elementtree.TreeBuilder.__init__
- element_factory: object = None
- *
- comment_factory: object = None
- pi_factory: object = None
- insert_comments: bool = False
- insert_pis: bool = False
- [clinic start generated code]*/
- static int
- _elementtree_TreeBuilder___init___impl(TreeBuilderObject *self,
- PyObject *element_factory,
- PyObject *comment_factory,
- PyObject *pi_factory,
- int insert_comments, int insert_pis)
- /*[clinic end generated code: output=8571d4dcadfdf952 input=ae98a94df20b5cc3]*/
- {
- if (element_factory != Py_None) {
- Py_XSETREF(self->element_factory, Py_NewRef(element_factory));
- } else {
- Py_CLEAR(self->element_factory);
- }
- if (comment_factory == Py_None) {
- elementtreestate *st = self->state;
- comment_factory = st->comment_factory;
- }
- if (comment_factory) {
- Py_XSETREF(self->comment_factory, Py_NewRef(comment_factory));
- self->insert_comments = insert_comments;
- } else {
- Py_CLEAR(self->comment_factory);
- self->insert_comments = 0;
- }
- if (pi_factory == Py_None) {
- elementtreestate *st = self->state;
- pi_factory = st->pi_factory;
- }
- if (pi_factory) {
- Py_XSETREF(self->pi_factory, Py_NewRef(pi_factory));
- self->insert_pis = insert_pis;
- } else {
- Py_CLEAR(self->pi_factory);
- self->insert_pis = 0;
- }
- return 0;
- }
- static int
- treebuilder_gc_traverse(TreeBuilderObject *self, visitproc visit, void *arg)
- {
- Py_VISIT(Py_TYPE(self));
- Py_VISIT(self->pi_event_obj);
- Py_VISIT(self->comment_event_obj);
- Py_VISIT(self->end_ns_event_obj);
- Py_VISIT(self->start_ns_event_obj);
- Py_VISIT(self->end_event_obj);
- Py_VISIT(self->start_event_obj);
- Py_VISIT(self->events_append);
- Py_VISIT(self->root);
- Py_VISIT(self->this);
- Py_VISIT(self->last);
- Py_VISIT(self->last_for_tail);
- Py_VISIT(self->data);
- Py_VISIT(self->stack);
- Py_VISIT(self->pi_factory);
- Py_VISIT(self->comment_factory);
- Py_VISIT(self->element_factory);
- return 0;
- }
- static int
- treebuilder_gc_clear(TreeBuilderObject *self)
- {
- Py_CLEAR(self->pi_event_obj);
- Py_CLEAR(self->comment_event_obj);
- Py_CLEAR(self->end_ns_event_obj);
- Py_CLEAR(self->start_ns_event_obj);
- Py_CLEAR(self->end_event_obj);
- Py_CLEAR(self->start_event_obj);
- Py_CLEAR(self->events_append);
- Py_CLEAR(self->stack);
- Py_CLEAR(self->data);
- Py_CLEAR(self->last);
- Py_CLEAR(self->last_for_tail);
- Py_CLEAR(self->this);
- Py_CLEAR(self->pi_factory);
- Py_CLEAR(self->comment_factory);
- Py_CLEAR(self->element_factory);
- Py_CLEAR(self->root);
- return 0;
- }
- static void
- treebuilder_dealloc(TreeBuilderObject *self)
- {
- PyTypeObject *tp = Py_TYPE(self);
- PyObject_GC_UnTrack(self);
- treebuilder_gc_clear(self);
- tp->tp_free(self);
- Py_DECREF(tp);
- }
- /* -------------------------------------------------------------------- */
- /* helpers for handling of arbitrary element-like objects */
- /*[clinic input]
- _elementtree._set_factories
- comment_factory: object
- pi_factory: object
- /
- Change the factories used to create comments and processing instructions.
- For internal use only.
- [clinic start generated code]*/
- static PyObject *
- _elementtree__set_factories_impl(PyObject *module, PyObject *comment_factory,
- PyObject *pi_factory)
- /*[clinic end generated code: output=813b408adee26535 input=99d17627aea7fb3b]*/
- {
- elementtreestate *st = get_elementtree_state(module);
- PyObject *old;
- if (!PyCallable_Check(comment_factory) && comment_factory != Py_None) {
- PyErr_Format(PyExc_TypeError, "Comment factory must be callable, not %.100s",
- Py_TYPE(comment_factory)->tp_name);
- return NULL;
- }
- if (!PyCallable_Check(pi_factory) && pi_factory != Py_None) {
- PyErr_Format(PyExc_TypeError, "PI factory must be callable, not %.100s",
- Py_TYPE(pi_factory)->tp_name);
- return NULL;
- }
- old = PyTuple_Pack(2,
- st->comment_factory ? st->comment_factory : Py_None,
- st->pi_factory ? st->pi_factory : Py_None);
- if (comment_factory == Py_None) {
- Py_CLEAR(st->comment_factory);
- } else {
- Py_XSETREF(st->comment_factory, Py_NewRef(comment_factory));
- }
- if (pi_factory == Py_None) {
- Py_CLEAR(st->pi_factory);
- } else {
- Py_XSETREF(st->pi_factory, Py_NewRef(pi_factory));
- }
- return old;
- }
- static int
- treebuilder_extend_element_text_or_tail(elementtreestate *st, PyObject *element,
- PyObject **data, PyObject **dest,
- PyObject *name)
- {
- /* Fast paths for the "almost always" cases. */
- if (Element_CheckExact(st, element)) {
- PyObject *dest_obj = JOIN_OBJ(*dest);
- if (dest_obj == Py_None) {
- *dest = JOIN_SET(*data, PyList_CheckExact(*data));
- *data = NULL;
- Py_DECREF(dest_obj);
- return 0;
- }
- else if (JOIN_GET(*dest)) {
- if (PyList_SetSlice(dest_obj, PY_SSIZE_T_MAX, PY_SSIZE_T_MAX, *data) < 0) {
- return -1;
- }
- Py_CLEAR(*data);
- return 0;
- }
- }
- /* Fallback for the non-Element / non-trivial cases. */
- {
- int r;
- PyObject* joined;
- PyObject* previous = PyObject_GetAttr(element, name);
- if (!previous)
- return -1;
- joined = list_join(*data);
- if (!joined) {
- Py_DECREF(previous);
- return -1;
- }
- if (previous != Py_None) {
- PyObject *tmp = PyNumber_Add(previous, joined);
- Py_DECREF(joined);
- Py_DECREF(previous);
- if (!tmp)
- return -1;
- joined = tmp;
- } else {
- Py_DECREF(previous);
- }
- r = PyObject_SetAttr(element, name, joined);
- Py_DECREF(joined);
- if (r < 0)
- return -1;
- Py_CLEAR(*data);
- return 0;
- }
- }
- LOCAL(int)
- treebuilder_flush_data(TreeBuilderObject* self)
- {
- if (!self->data) {
- return 0;
- }
- elementtreestate *st = self->state;
- if (!self->last_for_tail) {
- PyObject *element = self->last;
- return treebuilder_extend_element_text_or_tail(
- st, element, &self->data,
- &((ElementObject *) element)->text, st->str_text);
- }
- else {
- PyObject *element = self->last_for_tail;
- return treebuilder_extend_element_text_or_tail(
- st, element, &self->data,
- &((ElementObject *) element)->tail, st->str_tail);
- }
- }
- static int
- treebuilder_add_subelement(elementtreestate *st, PyObject *element,
- PyObject *child)
- {
- if (Element_CheckExact(st, element)) {
- ElementObject *elem = (ElementObject *) element;
- return element_add_subelement(st, elem, child);
- }
- else {
- PyObject *res;
- res = PyObject_CallMethodOneArg(element, st->str_append, child);
- if (res == NULL)
- return -1;
- Py_DECREF(res);
- return 0;
- }
- }
- LOCAL(int)
- treebuilder_append_event(TreeBuilderObject *self, PyObject *action,
- PyObject *node)
- {
- if (action != NULL) {
- PyObject *res;
- PyObject *event = PyTuple_Pack(2, action, node);
- if (event == NULL)
- return -1;
- res = PyObject_CallOneArg(self->events_append, event);
- Py_DECREF(event);
- if (res == NULL)
- return -1;
- Py_DECREF(res);
- }
- return 0;
- }
- /* -------------------------------------------------------------------- */
- /* handlers */
- LOCAL(PyObject*)
- treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
- PyObject* attrib)
- {
- PyObject* node;
- PyObject* this;
- elementtreestate *st = self->state;
- if (treebuilder_flush_data(self) < 0) {
- return NULL;
- }
- if (!self->element_factory) {
- node = create_new_element(st, tag, attrib);
- }
- else if (attrib == NULL) {
- attrib = PyDict_New();
- if (!attrib)
- return NULL;
- node = PyObject_CallFunctionObjArgs(self->element_factory,
- tag, attrib, NULL);
- Py_DECREF(attrib);
- }
- else {
- node = PyObject_CallFunctionObjArgs(self->element_factory,
- tag, attrib, NULL);
- }
- if (!node) {
- return NULL;
- }
- this = self->this;
- Py_CLEAR(self->last_for_tail);
- if (this != Py_None) {
- if (treebuilder_add_subelement(st, this, node) < 0) {
- goto error;
- }
- } else {
- if (self->root) {
- PyErr_SetString(
- st->parseerror_obj,
- "multiple elements on top level"
- );
- goto error;
- }
- self->root = Py_NewRef(node);
- }
- if (self->index < PyList_GET_SIZE(self->stack)) {
- if (PyList_SetItem(self->stack, self->index, this) < 0)
- goto error;
- Py_INCREF(this);
- } else {
- if (PyList_Append(self->stack, this) < 0)
- goto error;
- }
- self->index++;
- Py_SETREF(self->this, Py_NewRef(node));
- Py_SETREF(self->last, Py_NewRef(node));
- if (treebuilder_append_event(self, self->start_event_obj, node) < 0)
- goto error;
- return node;
- error:
- Py_DECREF(node);
- return NULL;
- }
- LOCAL(PyObject*)
- treebuilder_handle_data(TreeBuilderObject* self, PyObject* data)
- {
- if (!self->data) {
- if (self->last == Py_None) {
- /* ignore calls to data before the first call to start */
- Py_RETURN_NONE;
- }
- /* store the first item as is */
- self->data = Py_NewRef(data);
- } else {
- /* more than one item; use a list to collect items */
- if (PyBytes_CheckExact(self->data) && Py_REFCNT(self->data) == 1 &&
- PyBytes_CheckExact(data) && PyBytes_GET_SIZE(data) == 1) {
- /* XXX this code path unused in Python 3? */
- /* expat often generates single character data sections; handle
- the most common case by resizing the existing string... */
- Py_ssize_t size = PyBytes_GET_SIZE(self->data);
- if (_PyBytes_Resize(&self->data, size + 1) < 0)
- return NULL;
- PyBytes_AS_STRING(self->data)[size] = PyBytes_AS_STRING(data)[0];
- } else if (PyList_CheckExact(self->data)) {
- if (PyList_Append(self->data, data) < 0)
- return NULL;
- } else {
- PyObject* list = PyList_New(2);
- if (!list)
- return NULL;
- PyList_SET_ITEM(list, 0, Py_NewRef(self->data));
- PyList_SET_ITEM(list, 1, Py_NewRef(data));
- Py_SETREF(self->data, list);
- }
- }
- Py_RETURN_NONE;
- }
- LOCAL(PyObject*)
- treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag)
- {
- PyObject* item;
- if (treebuilder_flush_data(self) < 0) {
- return NULL;
- }
- if (self->index == 0) {
- PyErr_SetString(
- PyExc_IndexError,
- "pop from empty stack"
- );
- return NULL;
- }
- item = self->last;
- self->last = Py_NewRef(self->this);
- Py_XSETREF(self->last_for_tail, self->last);
- self->index--;
- self->this = Py_NewRef(PyList_GET_ITEM(self->stack, self->index));
- Py_DECREF(item);
- if (treebuilder_append_event(self, self->end_event_obj, self->last) < 0)
- return NULL;
- return Py_NewRef(self->last);
- }
- LOCAL(PyObject*)
- treebuilder_handle_comment(TreeBuilderObject* self, PyObject* text)
- {
- PyObject* comment;
- PyObject* this;
- if (treebuilder_flush_data(self) < 0) {
- return NULL;
- }
- if (self->comment_factory) {
- comment = PyObject_CallOneArg(self->comment_factory, text);
- if (!comment)
- return NULL;
- this = self->this;
- if (self->insert_comments && this != Py_None) {
- if (treebuilder_add_subelement(self->state, this, comment) < 0) {
- goto error;
- }
- Py_XSETREF(self->last_for_tail, Py_NewRef(comment));
- }
- } else {
- comment = Py_NewRef(text);
- }
- if (self->events_append && self->comment_event_obj) {
- if (treebuilder_append_event(self, self->comment_event_obj, comment) < 0)
- goto error;
- }
- return comment;
- error:
- Py_DECREF(comment);
- return NULL;
- }
- LOCAL(PyObject*)
- treebuilder_handle_pi(TreeBuilderObject* self, PyObject* target, PyObject* text)
- {
- PyObject* pi;
- PyObject* this;
- PyObject* stack[2] = {target, text};
- if (treebuilder_flush_data(self) < 0) {
- return NULL;
- }
- if (self->pi_factory) {
- pi = _PyObject_FastCall(self->pi_factory, stack, 2);
- if (!pi) {
- return NULL;
- }
- this = self->this;
- if (self->insert_pis && this != Py_None) {
- if (treebuilder_add_subelement(self->state, this, pi) < 0) {
- goto error;
- }
- Py_XSETREF(self->last_for_tail, Py_NewRef(pi));
- }
- } else {
- pi = PyTuple_Pack(2, target, text);
- if (!pi) {
- return NULL;
- }
- }
- if (self->events_append && self->pi_event_obj) {
- if (treebuilder_append_event(self, self->pi_event_obj, pi) < 0)
- goto error;
- }
- return pi;
- error:
- Py_DECREF(pi);
- return NULL;
- }
- LOCAL(PyObject*)
- treebuilder_handle_start_ns(TreeBuilderObject* self, PyObject* prefix, PyObject* uri)
- {
- PyObject* parcel;
- if (self->events_append && self->start_ns_event_obj) {
- parcel = PyTuple_Pack(2, prefix, uri);
- if (!parcel) {
- return NULL;
- }
- if (treebuilder_append_event(self, self->start_ns_event_obj, parcel) < 0) {
- Py_DECREF(parcel);
- return NULL;
- }
- Py_DECREF(parcel);
- }
- Py_RETURN_NONE;
- }
- LOCAL(PyObject*)
- treebuilder_handle_end_ns(TreeBuilderObject* self, PyObject* prefix)
- {
- if (self->events_append && self->end_ns_event_obj) {
- if (treebuilder_append_event(self, self->end_ns_event_obj, prefix) < 0) {
- return NULL;
- }
- }
- Py_RETURN_NONE;
- }
- /* -------------------------------------------------------------------- */
- /* methods (in alphabetical order) */
- /*[clinic input]
- _elementtree.TreeBuilder.data
- data: object
- /
- [clinic start generated code]*/
- static PyObject *
- _elementtree_TreeBuilder_data(TreeBuilderObject *self, PyObject *data)
- /*[clinic end generated code: output=69144c7100795bb2 input=a0540c532b284d29]*/
- {
- return treebuilder_handle_data(self, data);
- }
- /*[clinic input]
- _elementtree.TreeBuilder.end
- tag: object
- /
- [clinic start generated code]*/
- static PyObject *
- _elementtree_TreeBuilder_end(TreeBuilderObject *self, PyObject *tag)
- /*[clinic end generated code: output=9a98727cc691cd9d input=22dc3674236f5745]*/
- {
- return treebuilder_handle_end(self, tag);
- }
- /*[clinic input]
- _elementtree.TreeBuilder.comment
- text: object
- /
- [clinic start generated code]*/
- static PyObject *
- _elementtree_TreeBuilder_comment(TreeBuilderObject *self, PyObject *text)
- /*[clinic end generated code: output=22835be41deeaa27 input=47e7ebc48ed01dfa]*/
- {
- return treebuilder_handle_comment(self, text);
- }
- /*[clinic input]
- _elementtree.TreeBuilder.pi
- target: object
- text: object = None
- /
- [clinic start generated code]*/
- static PyObject *
- _elementtree_TreeBuilder_pi_impl(TreeBuilderObject *self, PyObject *target,
- PyObject *text)
- /*[clinic end generated code: output=21eb95ec9d04d1d9 input=349342bd79c35570]*/
- {
- return treebuilder_handle_pi(self, target, text);
- }
- LOCAL(PyObject*)
- treebuilder_done(TreeBuilderObject* self)
- {
- PyObject* res;
- /* FIXME: check stack size? */
- if (self->root)
- res = self->root;
- else
- res = Py_None;
- return Py_NewRef(res);
- }
- /*[clinic input]
- _elementtree.TreeBuilder.close
- [clinic start generated code]*/
- static PyObject *
- _elementtree_TreeBuilder_close_impl(TreeBuilderObject *self)
- /*[clinic end generated code: output=b441fee3202f61ee input=f7c9c65dc718de14]*/
- {
- return treebuilder_done(self);
- }
- /*[clinic input]
- _elementtree.TreeBuilder.start
- tag: object
- attrs: object(subclass_of='&PyDict_Type')
- /
- [clinic start generated code]*/
- static PyObject *
- _elementtree_TreeBuilder_start_impl(TreeBuilderObject *self, PyObject *tag,
- PyObject *attrs)
- /*[clinic end generated code: output=e7e9dc2861349411 input=7288e9e38e63b2b6]*/
- {
- return treebuilder_handle_start(self, tag, attrs);
- }
- /* ==================================================================== */
- /* the expat interface */
- #define EXPAT(st, func) ((st)->expat_capi->func)
- static XML_Memory_Handling_Suite ExpatMemoryHandler = {
- PyObject_Malloc, PyObject_Realloc, PyObject_Free};
- typedef struct {
- PyObject_HEAD
- XML_Parser parser;
- PyObject *target;
- PyObject *entity;
- PyObject *names;
- PyObject *handle_start_ns;
- PyObject *handle_end_ns;
- PyObject *handle_start;
- PyObject *handle_data;
- PyObject *handle_end;
- PyObject *handle_comment;
- PyObject *handle_pi;
- PyObject *handle_doctype;
- PyObject *handle_close;
- elementtreestate *state;
- PyObject *elementtree_module;
- } XMLParserObject;
- /* helpers */
- LOCAL(PyObject*)
- makeuniversal(XMLParserObject* self, const char* string)
- {
- /* convert a UTF-8 tag/attribute name from the expat parser
- to a universal name string */
- Py_ssize_t size = (Py_ssize_t) strlen(string);
- PyObject* key;
- PyObject* value;
- /* look the 'raw' name up in the names dictionary */
- key = PyBytes_FromStringAndSize(string, size);
- if (!key)
- return NULL;
- value = Py_XNewRef(PyDict_GetItemWithError(self->names, key));
- if (value == NULL && !PyErr_Occurred()) {
- /* new name. convert to universal name, and decode as
- necessary */
- PyObject* tag;
- char* p;
- Py_ssize_t i;
- /* look for namespace separator */
- for (i = 0; i < size; i++)
- if (string[i] == '}')
- break;
- if (i != size) {
- /* convert to universal name */
- tag = PyBytes_FromStringAndSize(NULL, size+1);
- if (tag == NULL) {
- Py_DECREF(key);
- return NULL;
- }
- p = PyBytes_AS_STRING(tag);
- p[0] = '{';
- memcpy(p+1, string, size);
- size++;
- } else {
- /* plain name; use key as tag */
- tag = Py_NewRef(key);
- }
- /* decode universal name */
- p = PyBytes_AS_STRING(tag);
- value = PyUnicode_DecodeUTF8(p, size, "strict");
- Py_DECREF(tag);
- if (!value) {
- Py_DECREF(key);
- return NULL;
- }
- /* add to names dictionary */
- if (PyDict_SetItem(self->names, key, value) < 0) {
- Py_DECREF(key);
- Py_DECREF(value);
- return NULL;
- }
- }
- Py_DECREF(key);
- return value;
- }
- /* Set the ParseError exception with the given parameters.
- * If message is not NULL, it's used as the error string. Otherwise, the
- * message string is the default for the given error_code.
- */
- static void
- expat_set_error(elementtreestate *st, enum XML_Error error_code,
- Py_ssize_t line, Py_ssize_t column, const char *message)
- {
- PyObject *errmsg, *error, *position, *code;
- errmsg = PyUnicode_FromFormat("%s: line %zd, column %zd",
- message ? message : EXPAT(st, ErrorString)(error_code),
- line, column);
- if (errmsg == NULL)
- return;
- error = PyObject_CallOneArg(st->parseerror_obj, errmsg);
- Py_DECREF(errmsg);
- if (!error)
- return;
- /* Add code and position attributes */
- code = PyLong_FromLong((long)error_code);
- if (!code) {
- Py_DECREF(error);
- return;
- }
- if (PyObject_SetAttrString(error, "code", code) == -1) {
- Py_DECREF(error);
- Py_DECREF(code);
- return;
- }
- Py_DECREF(code);
- position = Py_BuildValue("(nn)", line, column);
- if (!position) {
- Py_DECREF(error);
- return;
- }
- if (PyObject_SetAttrString(error, "position", position) == -1) {
- Py_DECREF(error);
- Py_DECREF(position);
- return;
- }
- Py_DECREF(position);
- PyErr_SetObject(st->parseerror_obj, error);
- Py_DECREF(error);
- }
- /* -------------------------------------------------------------------- */
- /* handlers */
- static void
- expat_default_handler(XMLParserObject* self, const XML_Char* data_in,
- int data_len)
- {
- PyObject* key;
- PyObject* value;
- PyObject* res;
- if (data_len < 2 || data_in[0] != '&')
- return;
- if (PyErr_Occurred())
- return;
- key = PyUnicode_DecodeUTF8(data_in + 1, data_len - 2, "strict");
- if (!key)
- return;
- value = PyDict_GetItemWithError(self->entity, key);
- elementtreestate *st = self->state;
- if (value) {
- if (TreeBuilder_CheckExact(st, self->target))
- res = treebuilder_handle_data(
- (TreeBuilderObject*) self->target, value
- );
- else if (self->handle_data)
- res = PyObject_CallOneArg(self->handle_data, value);
- else
- res = NULL;
- Py_XDECREF(res);
- } else if (!PyErr_Occurred()) {
- /* Report the first error, not the last */
- char message[128] = "undefined entity ";
- strncat(message, data_in, data_len < 100?data_len:100);
- expat_set_error(
- st,
- XML_ERROR_UNDEFINED_ENTITY,
- EXPAT(st, GetErrorLineNumber)(self->parser),
- EXPAT(st, GetErrorColumnNumber)(self->parser),
- message
- );
- }
- Py_DECREF(key);
- }
- static void
- expat_start_handler(XMLParserObject* self, const XML_Char* tag_in,
- const XML_Char **attrib_in)
- {
- PyObject* res;
- PyObject* tag;
- PyObject* attrib;
- int ok;
- if (PyErr_Occurred())
- return;
- /* tag name */
- tag = makeuniversal(self, tag_in);
- if (!tag)
- return; /* parser will look for errors */
- /* attributes */
- if (attrib_in[0]) {
- attrib = PyDict_New();
- if (!attrib) {
- Py_DECREF(tag);
- return;
- }
- while (attrib_in[0] && attrib_in[1]) {
- PyObject* key = makeuniversal(self, attrib_in[0]);
- if (key == NULL) {
- Py_DECREF(attrib);
- Py_DECREF(tag);
- return;
- }
- PyObject* value = PyUnicode_DecodeUTF8(attrib_in[1], strlen(attrib_in[1]), "strict");
- if (value == NULL) {
- Py_DECREF(key);
- Py_DECREF(attrib);
- Py_DECREF(tag);
- return;
- }
- ok = PyDict_SetItem(attrib, key, value);
- Py_DECREF(value);
- Py_DECREF(key);
- if (ok < 0) {
- Py_DECREF(attrib);
- Py_DECREF(tag);
- return;
- }
- attrib_in += 2;
- }
- } else {
- attrib = NULL;
- }
- elementtreestate *st = self->state;
- if (TreeBuilder_CheckExact(st, self->target)) {
- /* shortcut */
- res = treebuilder_handle_start((TreeBuilderObject*) self->target,
- tag, attrib);
- }
- else if (self->handle_start) {
- if (attrib == NULL) {
- attrib = PyDict_New();
- if (!attrib) {
- Py_DECREF(tag);
- return;
- }
- }
- res = PyObject_CallFunctionObjArgs(self->handle_start,
- tag, attrib, NULL);
- } else
- res = NULL;
- Py_DECREF(tag);
- Py_XDECREF(attrib);
- Py_XDECREF(res);
- }
- static void
- expat_data_handler(XMLParserObject* self, const XML_Char* data_in,
- int data_len)
- {
- PyObject* data;
- PyObject* res;
- if (PyErr_Occurred())
- return;
- data = PyUnicode_DecodeUTF8(data_in, data_len, "strict");
- if (!data)
- return; /* parser will look for errors */
- elementtreestate *st = self->state;
- if (TreeBuilder_CheckExact(st, self->target))
- /* shortcut */
- res = treebuilder_handle_data((TreeBuilderObject*) self->target, data);
- else if (self->handle_data)
- res = PyObject_CallOneArg(self->handle_data, data);
- else
- res = NULL;
- Py_DECREF(data);
- Py_XDECREF(res);
- }
- static void
- expat_end_handler(XMLParserObject* self, const XML_Char* tag_in)
- {
- PyObject* tag;
- PyObject* res = NULL;
- if (PyErr_Occurred())
- return;
- elementtreestate *st = self->state;
- if (TreeBuilder_CheckExact(st, self->target))
- /* shortcut */
- /* the standard tree builder doesn't look at the end tag */
- res = treebuilder_handle_end(
- (TreeBuilderObject*) self->target, Py_None
- );
- else if (self->handle_end) {
- tag = makeuniversal(self, tag_in);
- if (tag) {
- res = PyObject_CallOneArg(self->handle_end, tag);
- Py_DECREF(tag);
- }
- }
- Py_XDECREF(res);
- }
- static void
- expat_start_ns_handler(XMLParserObject* self, const XML_Char* prefix_in,
- const XML_Char *uri_in)
- {
- PyObject* res = NULL;
- PyObject* uri;
- PyObject* prefix;
- PyObject* stack[2];
- if (PyErr_Occurred())
- return;
- if (!uri_in)
- uri_in = "";
- if (!prefix_in)
- prefix_in = "";
- elementtreestate *st = self->state;
- if (TreeBuilder_CheckExact(st, self->target)) {
- /* shortcut - TreeBuilder does not actually implement .start_ns() */
- TreeBuilderObject *target = (TreeBuilderObject*) self->target;
- if (target->events_append && target->start_ns_event_obj) {
- prefix = PyUnicode_DecodeUTF8(prefix_in, strlen(prefix_in), "strict");
- if (!prefix)
- return;
- uri = PyUnicode_DecodeUTF8(uri_in, strlen(uri_in), "strict");
- if (!uri) {
- Py_DECREF(prefix);
- return;
- }
- res = treebuilder_handle_start_ns(target, prefix, uri);
- Py_DECREF(uri);
- Py_DECREF(prefix);
- }
- } else if (self->handle_start_ns) {
- prefix = PyUnicode_DecodeUTF8(prefix_in, strlen(prefix_in), "strict");
- if (!prefix)
- return;
- uri = PyUnicode_DecodeUTF8(uri_in, strlen(uri_in), "strict");
- if (!uri) {
- Py_DECREF(prefix);
- return;
- }
- stack[0] = prefix;
- stack[1] = uri;
- res = _PyObject_FastCall(self->handle_start_ns, stack, 2);
- Py_DECREF(uri);
- Py_DECREF(prefix);
- }
- Py_XDECREF(res);
- }
- static void
- expat_end_ns_handler(XMLParserObject* self, const XML_Char* prefix_in)
- {
- PyObject *res = NULL;
- PyObject* prefix;
- if (PyErr_Occurred())
- return;
- if (!prefix_in)
- prefix_in = "";
- elementtreestate *st = self->state;
- if (TreeBuilder_CheckExact(st, self->target)) {
- /* shortcut - TreeBuilder does not actually implement .end_ns() */
- TreeBuilderObject *target = (TreeBuilderObject*) self->target;
- if (target->events_append && target->end_ns_event_obj) {
- res = treebuilder_handle_end_ns(target, Py_None);
- }
- } else if (self->handle_end_ns) {
- prefix = PyUnicode_DecodeUTF8(prefix_in, strlen(prefix_in), "strict");
- if (!prefix)
- return;
- res = PyObject_CallOneArg(self->handle_end_ns, prefix);
- Py_DECREF(prefix);
- }
- Py_XDECREF(res);
- }
- static void
- expat_comment_handler(XMLParserObject* self, const XML_Char* comment_in)
- {
- PyObject* comment;
- PyObject* res;
- if (PyErr_Occurred())
- return;
- elementtreestate *st = self->state;
- if (TreeBuilder_CheckExact(st, self->target)) {
- /* shortcut */
- TreeBuilderObject *target = (TreeBuilderObject*) self->target;
- comment = PyUnicode_DecodeUTF8(comment_in, strlen(comment_in), "strict");
- if (!comment)
- return; /* parser will look for errors */
- res = treebuilder_handle_comment(target, comment);
- Py_XDECREF(res);
- Py_DECREF(comment);
- } else if (self->handle_comment) {
- comment = PyUnicode_DecodeUTF8(comment_in, strlen(comment_in), "strict");
- if (!comment)
- return;
- res = PyObject_CallOneArg(self->handle_comment, comment);
- Py_XDECREF(res);
- Py_DECREF(comment);
- }
- }
- static void
- expat_start_doctype_handler(XMLParserObject *self,
- const XML_Char *doctype_name,
- const XML_Char *sysid,
- const XML_Char *pubid,
- int has_internal_subset)
- {
- PyObject *doctype_name_obj, *sysid_obj, *pubid_obj;
- PyObject *res;
- if (PyErr_Occurred())
- return;
- doctype_name_obj = makeuniversal(self, doctype_name);
- if (!doctype_name_obj)
- return;
- if (sysid) {
- sysid_obj = makeuniversal(self, sysid);
- if (!sysid_obj) {
- Py_DECREF(doctype_name_obj);
- return;
- }
- } else {
- sysid_obj = Py_NewRef(Py_None);
- }
- if (pubid) {
- pubid_obj = makeuniversal(self, pubid);
- if (!pubid_obj) {
- Py_DECREF(doctype_name_obj);
- Py_DECREF(sysid_obj);
- return;
- }
- } else {
- pubid_obj = Py_NewRef(Py_None);
- }
- elementtreestate *st = self->state;
- /* If the target has a handler for doctype, call it. */
- if (self->handle_doctype) {
- res = PyObject_CallFunctionObjArgs(self->handle_doctype,
- doctype_name_obj, pubid_obj,
- sysid_obj, NULL);
- Py_XDECREF(res);
- }
- else if (_PyObject_LookupAttr((PyObject *)self, st->str_doctype, &res) > 0) {
- (void)PyErr_WarnEx(PyExc_RuntimeWarning,
- "The doctype() method of XMLParser is ignored. "
- "Define doctype() method on the TreeBuilder target.",
- 1);
- Py_DECREF(res);
- }
- Py_DECREF(doctype_name_obj);
- Py_DECREF(pubid_obj);
- Py_DECREF(sysid_obj);
- }
- static void
- expat_pi_handler(XMLParserObject* self, const XML_Char* target_in,
- const XML_Char* data_in)
- {
- PyObject* pi_target;
- PyObject* data;
- PyObject* res;
- PyObject* stack[2];
- if (PyErr_Occurred())
- return;
- elementtreestate *st = self->state;
- if (TreeBuilder_CheckExact(st, self->target)) {
- /* shortcut */
- TreeBuilderObject *target = (TreeBuilderObject*) self->target;
- if ((target->events_append && target->pi_event_obj) || target->insert_pis) {
- pi_target = PyUnicode_DecodeUTF8(target_in, strlen(target_in), "strict");
- if (!pi_target)
- goto error;
- data = PyUnicode_DecodeUTF8(data_in, strlen(data_in), "strict");
- if (!data)
- goto error;
- res = treebuilder_handle_pi(target, pi_target, data);
- Py_XDECREF(res);
- Py_DECREF(data);
- Py_DECREF(pi_target);
- }
- } else if (self->handle_pi) {
- pi_target = PyUnicode_DecodeUTF8(target_in, strlen(target_in), "strict");
- if (!pi_target)
- goto error;
- data = PyUnicode_DecodeUTF8(data_in, strlen(data_in), "strict");
- if (!data)
- goto error;
- stack[0] = pi_target;
- stack[1] = data;
- res = _PyObject_FastCall(self->handle_pi, stack, 2);
- Py_XDECREF(res);
- Py_DECREF(data);
- Py_DECREF(pi_target);
- }
- return;
- error:
- Py_XDECREF(pi_target);
- return;
- }
- /* -------------------------------------------------------------------- */
- static PyObject *
- xmlparser_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
- {
- XMLParserObject *self = (XMLParserObject *)type->tp_alloc(type, 0);
- if (self) {
- self->parser = NULL;
- self->target = self->entity = self->names = NULL;
- self->handle_start_ns = self->handle_end_ns = NULL;
- self->handle_start = self->handle_data = self->handle_end = NULL;
- self->handle_comment = self->handle_pi = self->handle_close = NULL;
- self->handle_doctype = NULL;
- self->elementtree_module = PyType_GetModuleByDef(type, &elementtreemodule);
- assert(self->elementtree_module != NULL);
- Py_INCREF(self->elementtree_module);
- // See gh-111784 for explanation why is reference to module needed here.
- self->state = get_elementtree_state(self->elementtree_module);
- }
- return (PyObject *)self;
- }
- static int
- ignore_attribute_error(PyObject *value)
- {
- if (value == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
- return -1;
- }
- PyErr_Clear();
- }
- return 0;
- }
- /*[clinic input]
- _elementtree.XMLParser.__init__
- *
- target: object = None
- encoding: str(accept={str, NoneType}) = None
- [clinic start generated code]*/
- static int
- _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *target,
- const char *encoding)
- /*[clinic end generated code: output=3ae45ec6cdf344e4 input=7e716dd6e4f3e439]*/
- {
- self->entity = PyDict_New();
- if (!self->entity)
- return -1;
- self->names = PyDict_New();
- if (!self->names) {
- Py_CLEAR(self->entity);
- return -1;
- }
- elementtreestate *st = self->state;
- self->parser = EXPAT(st, ParserCreate_MM)(encoding, &ExpatMemoryHandler, "}");
- if (!self->parser) {
- Py_CLEAR(self->entity);
- Py_CLEAR(self->names);
- PyErr_NoMemory();
- return -1;
- }
- /* expat < 2.1.0 has no XML_SetHashSalt() */
- if (EXPAT(st, SetHashSalt) != NULL) {
- EXPAT(st, SetHashSalt)(self->parser,
- (unsigned long)_Py_HashSecret.expat.hashsalt);
- }
- if (target != Py_None) {
- Py_INCREF(target);
- } else {
- target = treebuilder_new(st->TreeBuilder_Type, NULL, NULL);
- if (!target) {
- Py_CLEAR(self->entity);
- Py_CLEAR(self->names);
- return -1;
- }
- }
- self->target = target;
- self->handle_start_ns = PyObject_GetAttrString(target, "start_ns");
- if (ignore_attribute_error(self->handle_start_ns)) {
- return -1;
- }
- self->handle_end_ns = PyObject_GetAttrString(target, "end_ns");
- if (ignore_attribute_error(self->handle_end_ns)) {
- return -1;
- }
- self->handle_start = PyObject_GetAttrString(target, "start");
- if (ignore_attribute_error(self->handle_start)) {
- return -1;
- }
- self->handle_data = PyObject_GetAttrString(target, "data");
- if (ignore_attribute_error(self->handle_data)) {
- return -1;
- }
- self->handle_end = PyObject_GetAttrString(target, "end");
- if (ignore_attribute_error(self->handle_end)) {
- return -1;
- }
- self->handle_comment = PyObject_GetAttrString(target, "comment");
- if (ignore_attribute_error(self->handle_comment)) {
- return -1;
- }
- self->handle_pi = PyObject_GetAttrString(target, "pi");
- if (ignore_attribute_error(self->handle_pi)) {
- return -1;
- }
- self->handle_close = PyObject_GetAttrString(target, "close");
- if (ignore_attribute_error(self->handle_close)) {
- return -1;
- }
- self->handle_doctype = PyObject_GetAttrString(target, "doctype");
- if (ignore_attribute_error(self->handle_doctype)) {
- return -1;
- }
- /* configure parser */
- EXPAT(st, SetUserData)(self->parser, self);
- if (self->handle_start_ns || self->handle_end_ns)
- EXPAT(st, SetNamespaceDeclHandler)(
- self->parser,
- (XML_StartNamespaceDeclHandler) expat_start_ns_handler,
- (XML_EndNamespaceDeclHandler) expat_end_ns_handler
- );
- EXPAT(st, SetElementHandler)(
- self->parser,
- (XML_StartElementHandler) expat_start_handler,
- (XML_EndElementHandler) expat_end_handler
- );
- EXPAT(st, SetDefaultHandlerExpand)(
- self->parser,
- (XML_DefaultHandler) expat_default_handler
- );
- EXPAT(st, SetCharacterDataHandler)(
- self->parser,
- (XML_CharacterDataHandler) expat_data_handler
- );
- if (self->handle_comment)
- EXPAT(st, SetCommentHandler)(
- self->parser,
- (XML_CommentHandler) expat_comment_handler
- );
- if (self->handle_pi)
- EXPAT(st, SetProcessingInstructionHandler)(
- self->parser,
- (XML_ProcessingInstructionHandler) expat_pi_handler
- );
- EXPAT(st, SetStartDoctypeDeclHandler)(
- self->parser,
- (XML_StartDoctypeDeclHandler) expat_start_doctype_handler
- );
- EXPAT(st, SetUnknownEncodingHandler)(
- self->parser,
- EXPAT(st, DefaultUnknownEncodingHandler), NULL
- );
- return 0;
- }
- static int
- xmlparser_gc_traverse(XMLParserObject *self, visitproc visit, void *arg)
- {
- Py_VISIT(Py_TYPE(self));
- Py_VISIT(self->handle_close);
- Py_VISIT(self->handle_pi);
- Py_VISIT(self->handle_comment);
- Py_VISIT(self->handle_end);
- Py_VISIT(self->handle_data);
- Py_VISIT(self->handle_start);
- Py_VISIT(self->handle_start_ns);
- Py_VISIT(self->handle_end_ns);
- Py_VISIT(self->handle_doctype);
- Py_VISIT(self->target);
- Py_VISIT(self->entity);
- Py_VISIT(self->names);
- return 0;
- }
- static int
- xmlparser_gc_clear(XMLParserObject *self)
- {
- elementtreestate *st = self->state;
- if (self->parser != NULL) {
- XML_Parser parser = self->parser;
- self->parser = NULL;
- EXPAT(st, ParserFree)(parser);
- }
- Py_CLEAR(self->elementtree_module);
- Py_CLEAR(self->handle_close);
- Py_CLEAR(self->handle_pi);
- Py_CLEAR(self->handle_comment);
- Py_CLEAR(self->handle_end);
- Py_CLEAR(self->handle_data);
- Py_CLEAR(self->handle_start);
- Py_CLEAR(self->handle_start_ns);
- Py_CLEAR(self->handle_end_ns);
- Py_CLEAR(self->handle_doctype);
- Py_CLEAR(self->target);
- Py_CLEAR(self->entity);
- Py_CLEAR(self->names);
- return 0;
- }
- static void
- xmlparser_dealloc(XMLParserObject* self)
- {
- PyTypeObject *tp = Py_TYPE(self);
- PyObject_GC_UnTrack(self);
- xmlparser_gc_clear(self);
- tp->tp_free(self);
- Py_DECREF(tp);
- }
- Py_LOCAL_INLINE(int)
- _check_xmlparser(XMLParserObject* self)
- {
- if (self->target == NULL) {
- PyErr_SetString(PyExc_ValueError,
- "XMLParser.__init__() wasn't called");
- return 0;
- }
- return 1;
- }
- LOCAL(PyObject*)
- expat_parse(elementtreestate *st, XMLParserObject *self, const char *data,
- int data_len, int final)
- {
- int ok;
- assert(!PyErr_Occurred());
- ok = EXPAT(st, Parse)(self->parser, data, data_len, final);
- if (PyErr_Occurred())
- return NULL;
- if (!ok) {
- expat_set_error(
- st,
- EXPAT(st, GetErrorCode)(self->parser),
- EXPAT(st, GetErrorLineNumber)(self->parser),
- EXPAT(st, GetErrorColumnNumber)(self->parser),
- NULL
- );
- return NULL;
- }
- Py_RETURN_NONE;
- }
- /*[clinic input]
- _elementtree.XMLParser.close
- [clinic start generated code]*/
- static PyObject *
- _elementtree_XMLParser_close_impl(XMLParserObject *self)
- /*[clinic end generated code: output=d68d375dd23bc7fb input=ca7909ca78c3abfe]*/
- {
- /* end feeding data to parser */
- PyObject* res;
- if (!_check_xmlparser(self)) {
- return NULL;
- }
- elementtreestate *st = self->state;
- res = expat_parse(st, self, "", 0, 1);
- if (!res)
- return NULL;
- if (TreeBuilder_CheckExact(st, self->target)) {
- Py_DECREF(res);
- return treebuilder_done((TreeBuilderObject*) self->target);
- }
- else if (self->handle_close) {
- Py_DECREF(res);
- return PyObject_CallNoArgs(self->handle_close);
- }
- else {
- return res;
- }
- }
- /*[clinic input]
- _elementtree.XMLParser.feed
- data: object
- /
- [clinic start generated code]*/
- static PyObject *
- _elementtree_XMLParser_feed(XMLParserObject *self, PyObject *data)
- /*[clinic end generated code: output=e42b6a78eec7446d input=fe231b6b8de3ce1f]*/
- {
- /* feed data to parser */
- if (!_check_xmlparser(self)) {
- return NULL;
- }
- elementtreestate *st = self->state;
- if (PyUnicode_Check(data)) {
- Py_ssize_t data_len;
- const char *data_ptr = PyUnicode_AsUTF8AndSize(data, &data_len);
- if (data_ptr == NULL)
- return NULL;
- if (data_len > INT_MAX) {
- PyErr_SetString(PyExc_OverflowError, "size does not fit in an int");
- return NULL;
- }
- /* Explicitly set UTF-8 encoding. Return code ignored. */
- (void)EXPAT(st, SetEncoding)(self->parser, "utf-8");
- return expat_parse(st, self, data_ptr, (int)data_len, 0);
- }
- else {
- Py_buffer view;
- PyObject *res;
- if (PyObject_GetBuffer(data, &view, PyBUF_SIMPLE) < 0)
- return NULL;
- if (view.len > INT_MAX) {
- PyBuffer_Release(&view);
- PyErr_SetString(PyExc_OverflowError, "size does not fit in an int");
- return NULL;
- }
- res = expat_parse(st, self, view.buf, (int)view.len, 0);
- PyBuffer_Release(&view);
- return res;
- }
- }
- /*[clinic input]
- _elementtree.XMLParser._parse_whole
- file: object
- /
- [clinic start generated code]*/
- static PyObject *
- _elementtree_XMLParser__parse_whole(XMLParserObject *self, PyObject *file)
- /*[clinic end generated code: output=f797197bb818dda3 input=19ecc893b6f3e752]*/
- {
- /* (internal) parse the whole input, until end of stream */
- PyObject* reader;
- PyObject* buffer;
- PyObject* temp;
- PyObject* res;
- if (!_check_xmlparser(self)) {
- return NULL;
- }
- reader = PyObject_GetAttrString(file, "read");
- if (!reader)
- return NULL;
- /* read from open file object */
- elementtreestate *st = self->state;
- for (;;) {
- buffer = PyObject_CallFunction(reader, "i", 64*1024);
- if (!buffer) {
- /* read failed (e.g. due to KeyboardInterrupt) */
- Py_DECREF(reader);
- return NULL;
- }
- if (PyUnicode_CheckExact(buffer)) {
- /* A unicode object is encoded into bytes using UTF-8 */
- if (PyUnicode_GET_LENGTH(buffer) == 0) {
- Py_DECREF(buffer);
- break;
- }
- temp = PyUnicode_AsEncodedString(buffer, "utf-8", "surrogatepass");
- Py_DECREF(buffer);
- if (!temp) {
- /* Propagate exception from PyUnicode_AsEncodedString */
- Py_DECREF(reader);
- return NULL;
- }
- buffer = temp;
- }
- else if (!PyBytes_CheckExact(buffer) || PyBytes_GET_SIZE(buffer) == 0) {
- Py_DECREF(buffer);
- break;
- }
- if (PyBytes_GET_SIZE(buffer) > INT_MAX) {
- Py_DECREF(buffer);
- Py_DECREF(reader);
- PyErr_SetString(PyExc_OverflowError, "size does not fit in an int");
- return NULL;
- }
- res = expat_parse(
- st, self, PyBytes_AS_STRING(buffer), (int)PyBytes_GET_SIZE(buffer),
- 0);
- Py_DECREF(buffer);
- if (!res) {
- Py_DECREF(reader);
- return NULL;
- }
- Py_DECREF(res);
- }
- Py_DECREF(reader);
- res = expat_parse(st, self, "", 0, 1);
- if (res && TreeBuilder_CheckExact(st, self->target)) {
- Py_DECREF(res);
- return treebuilder_done((TreeBuilderObject*) self->target);
- }
- return res;
- }
- /*[clinic input]
- _elementtree.XMLParser._setevents
- events_queue: object
- events_to_report: object = None
- /
- [clinic start generated code]*/
- static PyObject *
- _elementtree_XMLParser__setevents_impl(XMLParserObject *self,
- PyObject *events_queue,
- PyObject *events_to_report)
- /*[clinic end generated code: output=1440092922b13ed1 input=abf90830a1c3b0fc]*/
- {
- /* activate element event reporting */
- Py_ssize_t i;
- TreeBuilderObject *target;
- PyObject *events_append, *events_seq;
- if (!_check_xmlparser(self)) {
- return NULL;
- }
- elementtreestate *st = self->state;
- if (!TreeBuilder_CheckExact(st, self->target)) {
- PyErr_SetString(
- PyExc_TypeError,
- "event handling only supported for ElementTree.TreeBuilder "
- "targets"
- );
- return NULL;
- }
- target = (TreeBuilderObject*) self->target;
- events_append = PyObject_GetAttrString(events_queue, "append");
- if (events_append == NULL)
- return NULL;
- Py_XSETREF(target->events_append, events_append);
- /* clear out existing events */
- Py_CLEAR(target->start_event_obj);
- Py_CLEAR(target->end_event_obj);
- Py_CLEAR(target->start_ns_event_obj);
- Py_CLEAR(target->end_ns_event_obj);
- Py_CLEAR(target->comment_event_obj);
- Py_CLEAR(target->pi_event_obj);
- if (events_to_report == Py_None) {
- /* default is "end" only */
- target->end_event_obj = PyUnicode_FromString("end");
- Py_RETURN_NONE;
- }
- if (!(events_seq = PySequence_Fast(events_to_report,
- "events must be a sequence"))) {
- return NULL;
- }
- for (i = 0; i < PySequence_Fast_GET_SIZE(events_seq); ++i) {
- PyObject *event_name_obj = PySequence_Fast_GET_ITEM(events_seq, i);
- const char *event_name = NULL;
- if (PyUnicode_Check(event_name_obj)) {
- event_name = PyUnicode_AsUTF8(event_name_obj);
- } else if (PyBytes_Check(event_name_obj)) {
- event_name = PyBytes_AS_STRING(event_name_obj);
- }
- if (event_name == NULL) {
- Py_DECREF(events_seq);
- PyErr_Format(PyExc_ValueError, "invalid events sequence");
- return NULL;
- }
- if (strcmp(event_name, "start") == 0) {
- Py_XSETREF(target->start_event_obj, Py_NewRef(event_name_obj));
- } else if (strcmp(event_name, "end") == 0) {
- Py_XSETREF(target->end_event_obj, Py_NewRef(event_name_obj));
- } else if (strcmp(event_name, "start-ns") == 0) {
- Py_XSETREF(target->start_ns_event_obj, Py_NewRef(event_name_obj));
- EXPAT(st, SetNamespaceDeclHandler)(
- self->parser,
- (XML_StartNamespaceDeclHandler) expat_start_ns_handler,
- (XML_EndNamespaceDeclHandler) expat_end_ns_handler
- );
- } else if (strcmp(event_name, "end-ns") == 0) {
- Py_XSETREF(target->end_ns_event_obj, Py_NewRef(event_name_obj));
- EXPAT(st, SetNamespaceDeclHandler)(
- self->parser,
- (XML_StartNamespaceDeclHandler) expat_start_ns_handler,
- (XML_EndNamespaceDeclHandler) expat_end_ns_handler
- );
- } else if (strcmp(event_name, "comment") == 0) {
- Py_XSETREF(target->comment_event_obj, Py_NewRef(event_name_obj));
- EXPAT(st, SetCommentHandler)(
- self->parser,
- (XML_CommentHandler) expat_comment_handler
- );
- } else if (strcmp(event_name, "pi") == 0) {
- Py_XSETREF(target->pi_event_obj, Py_NewRef(event_name_obj));
- EXPAT(st, SetProcessingInstructionHandler)(
- self->parser,
- (XML_ProcessingInstructionHandler) expat_pi_handler
- );
- } else {
- Py_DECREF(events_seq);
- PyErr_Format(PyExc_ValueError, "unknown event '%s'", event_name);
- return NULL;
- }
- }
- Py_DECREF(events_seq);
- Py_RETURN_NONE;
- }
- static PyMemberDef xmlparser_members[] = {
- {"entity", T_OBJECT, offsetof(XMLParserObject, entity), READONLY, NULL},
- {"target", T_OBJECT, offsetof(XMLParserObject, target), READONLY, NULL},
- {NULL}
- };
- static PyObject*
- xmlparser_version_getter(XMLParserObject *self, void *closure)
- {
- return PyUnicode_FromFormat(
- "Expat %d.%d.%d", XML_MAJOR_VERSION,
- XML_MINOR_VERSION, XML_MICRO_VERSION);
- }
- static PyGetSetDef xmlparser_getsetlist[] = {
- {"version", (getter)xmlparser_version_getter, NULL, NULL},
- {NULL},
- };
- #define clinic_state() (get_elementtree_state_by_type(Py_TYPE(self)))
- #include "clinic/_elementtree.c.h"
- #undef clinic_state
- static PyMethodDef element_methods[] = {
- _ELEMENTTREE_ELEMENT_CLEAR_METHODDEF
- _ELEMENTTREE_ELEMENT_GET_METHODDEF
- _ELEMENTTREE_ELEMENT_SET_METHODDEF
- _ELEMENTTREE_ELEMENT_FIND_METHODDEF
- _ELEMENTTREE_ELEMENT_FINDTEXT_METHODDEF
- _ELEMENTTREE_ELEMENT_FINDALL_METHODDEF
- _ELEMENTTREE_ELEMENT_APPEND_METHODDEF
- _ELEMENTTREE_ELEMENT_EXTEND_METHODDEF
- _ELEMENTTREE_ELEMENT_INSERT_METHODDEF
- _ELEMENTTREE_ELEMENT_REMOVE_METHODDEF
- _ELEMENTTREE_ELEMENT_ITER_METHODDEF
- _ELEMENTTREE_ELEMENT_ITERTEXT_METHODDEF
- _ELEMENTTREE_ELEMENT_ITERFIND_METHODDEF
- _ELEMENTTREE_ELEMENT_ITEMS_METHODDEF
- _ELEMENTTREE_ELEMENT_KEYS_METHODDEF
- _ELEMENTTREE_ELEMENT_MAKEELEMENT_METHODDEF
- _ELEMENTTREE_ELEMENT___COPY___METHODDEF
- _ELEMENTTREE_ELEMENT___DEEPCOPY___METHODDEF
- _ELEMENTTREE_ELEMENT___SIZEOF___METHODDEF
- _ELEMENTTREE_ELEMENT___GETSTATE___METHODDEF
- _ELEMENTTREE_ELEMENT___SETSTATE___METHODDEF
- {NULL, NULL}
- };
- static struct PyMemberDef element_members[] = {
- {"__weaklistoffset__", T_PYSSIZET, offsetof(ElementObject, weakreflist), READONLY},
- {NULL},
- };
- static PyGetSetDef element_getsetlist[] = {
- {"tag",
- (getter)element_tag_getter,
- (setter)element_tag_setter,
- "A string identifying what kind of data this element represents"},
- {"text",
- (getter)element_text_getter,
- (setter)element_text_setter,
- "A string of text directly after the start tag, or None"},
- {"tail",
- (getter)element_tail_getter,
- (setter)element_tail_setter,
- "A string of text directly after the end tag, or None"},
- {"attrib",
- (getter)element_attrib_getter,
- (setter)element_attrib_setter,
- "A dictionary containing the element's attributes"},
- {NULL},
- };
- static PyType_Slot element_slots[] = {
- {Py_tp_dealloc, element_dealloc},
- {Py_tp_repr, element_repr},
- {Py_tp_getattro, PyObject_GenericGetAttr},
- {Py_tp_traverse, element_gc_traverse},
- {Py_tp_clear, element_gc_clear},
- {Py_tp_methods, element_methods},
- {Py_tp_members, element_members},
- {Py_tp_getset, element_getsetlist},
- {Py_tp_init, element_init},
- {Py_tp_alloc, PyType_GenericAlloc},
- {Py_tp_new, element_new},
- {Py_sq_length, element_length},
- {Py_sq_item, element_getitem},
- {Py_sq_ass_item, element_setitem},
- {Py_nb_bool, element_bool},
- {Py_mp_length, element_length},
- {Py_mp_subscript, element_subscr},
- {Py_mp_ass_subscript, element_ass_subscr},
- {0, NULL},
- };
- static PyType_Spec element_spec = {
- .name = "xml.etree.ElementTree.Element",
- .basicsize = sizeof(ElementObject),
- .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_IMMUTABLETYPE),
- .slots = element_slots,
- };
- static PyMethodDef treebuilder_methods[] = {
- _ELEMENTTREE_TREEBUILDER_DATA_METHODDEF
- _ELEMENTTREE_TREEBUILDER_START_METHODDEF
- _ELEMENTTREE_TREEBUILDER_END_METHODDEF
- _ELEMENTTREE_TREEBUILDER_COMMENT_METHODDEF
- _ELEMENTTREE_TREEBUILDER_PI_METHODDEF
- _ELEMENTTREE_TREEBUILDER_CLOSE_METHODDEF
- {NULL, NULL}
- };
- static PyType_Slot treebuilder_slots[] = {
- {Py_tp_dealloc, treebuilder_dealloc},
- {Py_tp_traverse, treebuilder_gc_traverse},
- {Py_tp_clear, treebuilder_gc_clear},
- {Py_tp_methods, treebuilder_methods},
- {Py_tp_init, _elementtree_TreeBuilder___init__},
- {Py_tp_alloc, PyType_GenericAlloc},
- {Py_tp_new, treebuilder_new},
- {0, NULL},
- };
- static PyType_Spec treebuilder_spec = {
- .name = "xml.etree.ElementTree.TreeBuilder",
- .basicsize = sizeof(TreeBuilderObject),
- .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE,
- .slots = treebuilder_slots,
- };
- static PyMethodDef xmlparser_methods[] = {
- _ELEMENTTREE_XMLPARSER_FEED_METHODDEF
- _ELEMENTTREE_XMLPARSER_CLOSE_METHODDEF
- _ELEMENTTREE_XMLPARSER__PARSE_WHOLE_METHODDEF
- _ELEMENTTREE_XMLPARSER__SETEVENTS_METHODDEF
- {NULL, NULL}
- };
- static PyType_Slot xmlparser_slots[] = {
- {Py_tp_dealloc, xmlparser_dealloc},
- {Py_tp_traverse, xmlparser_gc_traverse},
- {Py_tp_clear, xmlparser_gc_clear},
- {Py_tp_methods, xmlparser_methods},
- {Py_tp_members, xmlparser_members},
- {Py_tp_getset, xmlparser_getsetlist},
- {Py_tp_init, _elementtree_XMLParser___init__},
- {Py_tp_alloc, PyType_GenericAlloc},
- {Py_tp_new, xmlparser_new},
- {0, NULL},
- };
- static PyType_Spec xmlparser_spec = {
- .name = "xml.etree.ElementTree.XMLParser",
- .basicsize = sizeof(XMLParserObject),
- .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_IMMUTABLETYPE),
- .slots = xmlparser_slots,
- };
- /* ==================================================================== */
- /* python module interface */
- static PyMethodDef _functions[] = {
- {"SubElement", _PyCFunction_CAST(subelement), METH_VARARGS | METH_KEYWORDS},
- _ELEMENTTREE__SET_FACTORIES_METHODDEF
- {NULL, NULL}
- };
- #define CREATE_TYPE(module, type, spec) \
- do { \
- if (type != NULL) { \
- break; \
- } \
- type = (PyTypeObject *)PyType_FromModuleAndSpec(module, spec, NULL); \
- if (type == NULL) { \
- goto error; \
- } \
- } while (0)
- static int
- module_exec(PyObject *m)
- {
- elementtreestate *st = get_elementtree_state(m);
- /* Initialize object types */
- CREATE_TYPE(m, st->ElementIter_Type, &elementiter_spec);
- CREATE_TYPE(m, st->TreeBuilder_Type, &treebuilder_spec);
- CREATE_TYPE(m, st->Element_Type, &element_spec);
- CREATE_TYPE(m, st->XMLParser_Type, &xmlparser_spec);
- st->deepcopy_obj = _PyImport_GetModuleAttrString("copy", "deepcopy");
- if (st->deepcopy_obj == NULL) {
- goto error;
- }
- assert(!PyErr_Occurred());
- if (!(st->elementpath_obj = PyImport_ImportModule("xml.etree.ElementPath")))
- goto error;
- /* link against pyexpat */
- if (!(st->expat_capsule = _PyImport_GetModuleAttrString("pyexpat", "expat_CAPI")))
- goto error;
- if (!(st->expat_capi = PyCapsule_GetPointer(st->expat_capsule, PyExpat_CAPSULE_NAME)))
- goto error;
- if (st->expat_capi) {
- /* check that it's usable */
- if (strcmp(st->expat_capi->magic, PyExpat_CAPI_MAGIC) != 0 ||
- (size_t)st->expat_capi->size < sizeof(struct PyExpat_CAPI) ||
- st->expat_capi->MAJOR_VERSION != XML_MAJOR_VERSION ||
- st->expat_capi->MINOR_VERSION != XML_MINOR_VERSION ||
- st->expat_capi->MICRO_VERSION != XML_MICRO_VERSION) {
- PyErr_SetString(PyExc_ImportError,
- "pyexpat version is incompatible");
- goto error;
- }
- } else {
- goto error;
- }
- st->str_append = PyUnicode_InternFromString("append");
- if (st->str_append == NULL) {
- goto error;
- }
- st->str_find = PyUnicode_InternFromString("find");
- if (st->str_find == NULL) {
- goto error;
- }
- st->str_findall = PyUnicode_InternFromString("findall");
- if (st->str_findall == NULL) {
- goto error;
- }
- st->str_findtext = PyUnicode_InternFromString("findtext");
- if (st->str_findtext == NULL) {
- goto error;
- }
- st->str_iterfind = PyUnicode_InternFromString("iterfind");
- if (st->str_iterfind == NULL) {
- goto error;
- }
- st->str_tail = PyUnicode_InternFromString("tail");
- if (st->str_tail == NULL) {
- goto error;
- }
- st->str_text = PyUnicode_InternFromString("text");
- if (st->str_text == NULL) {
- goto error;
- }
- st->str_doctype = PyUnicode_InternFromString("doctype");
- if (st->str_doctype == NULL) {
- goto error;
- }
- st->parseerror_obj = PyErr_NewException(
- "xml.etree.ElementTree.ParseError", PyExc_SyntaxError, NULL
- );
- if (PyModule_AddObjectRef(m, "ParseError", st->parseerror_obj) < 0) {
- goto error;
- }
- PyTypeObject *types[] = {
- st->Element_Type,
- st->TreeBuilder_Type,
- st->XMLParser_Type
- };
- for (size_t i = 0; i < Py_ARRAY_LENGTH(types); i++) {
- if (PyModule_AddType(m, types[i]) < 0) {
- goto error;
- }
- }
- return 0;
- error:
- return -1;
- }
- static struct PyModuleDef_Slot elementtree_slots[] = {
- {Py_mod_exec, module_exec},
- // XXX gh-103092: fix isolation.
- {Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
- //{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
- {0, NULL},
- };
- static struct PyModuleDef elementtreemodule = {
- .m_base = PyModuleDef_HEAD_INIT,
- .m_name = "_elementtree",
- .m_size = sizeof(elementtreestate),
- .m_methods = _functions,
- .m_slots = elementtree_slots,
- .m_traverse = elementtree_traverse,
- .m_clear = elementtree_clear,
- .m_free = elementtree_free,
- };
- PyMODINIT_FUNC
- PyInit__elementtree(void)
- {
- return PyModuleDef_Init(&elementtreemodule);
- }
|