12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990 |
- /* New getargs implementation */
- #include "Python.h"
- #include "pycore_tuple.h" // _PyTuple_ITEMS()
- #include "pycore_pylifecycle.h" // _PyArg_Fini
- #include "pycore_pystate.h" // _Py_IsMainInterpreter()
- #include <ctype.h>
- #include <float.h>
- #ifdef __cplusplus
- extern "C" {
- #endif
- int PyArg_Parse(PyObject *, const char *, ...);
- int PyArg_ParseTuple(PyObject *, const char *, ...);
- int PyArg_VaParse(PyObject *, const char *, va_list);
- int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
- const char *, char **, ...);
- int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
- const char *, char **, va_list);
- int _PyArg_ParseTupleAndKeywordsFast(PyObject *, PyObject *,
- struct _PyArg_Parser *, ...);
- int _PyArg_VaParseTupleAndKeywordsFast(PyObject *, PyObject *,
- struct _PyArg_Parser *, va_list);
- #ifdef HAVE_DECLSPEC_DLL
- /* Export functions */
- PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, const char *, ...);
- PyAPI_FUNC(int) _PyArg_ParseStack_SizeT(PyObject *const *args, Py_ssize_t nargs,
- const char *format, ...);
- PyAPI_FUNC(int) _PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs,
- PyObject *kwnames,
- struct _PyArg_Parser *parser, ...);
- PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, const char *, ...);
- PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
- const char *, char **, ...);
- PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...);
- PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, const char *, va_list);
- PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
- const char *, char **, va_list);
- PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *,
- struct _PyArg_Parser *, ...);
- PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *,
- struct _PyArg_Parser *, va_list);
- #endif
- #define FLAG_COMPAT 1
- #define FLAG_SIZE_T 2
- typedef int (*destr_t)(PyObject *, void *);
- /* Keep track of "objects" that have been allocated or initialized and
- which will need to be deallocated or cleaned up somehow if overall
- parsing fails.
- */
- typedef struct {
- void *item;
- destr_t destructor;
- } freelistentry_t;
- typedef struct {
- freelistentry_t *entries;
- int first_available;
- int entries_malloced;
- } freelist_t;
- #define STATIC_FREELIST_ENTRIES 8
- /* Forward */
- static int vgetargs1_impl(PyObject *args, PyObject *const *stack, Py_ssize_t nargs,
- const char *format, va_list *p_va, int flags);
- static int vgetargs1(PyObject *, const char *, va_list *, int);
- static void seterror(Py_ssize_t, const char *, int *, const char *, const char *);
- static const char *convertitem(PyObject *, const char **, va_list *, int, int *,
- char *, size_t, freelist_t *);
- static const char *converttuple(PyObject *, const char **, va_list *, int,
- int *, char *, size_t, int, freelist_t *);
- static const char *convertsimple(PyObject *, const char **, va_list *, int,
- char *, size_t, freelist_t *);
- static Py_ssize_t convertbuffer(PyObject *, const void **p, const char **);
- static int getbuffer(PyObject *, Py_buffer *, const char**);
- static int vgetargskeywords(PyObject *, PyObject *,
- const char *, char **, va_list *, int);
- static int vgetargskeywordsfast(PyObject *, PyObject *,
- struct _PyArg_Parser *, va_list *, int);
- static int vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
- PyObject *keywords, PyObject *kwnames,
- struct _PyArg_Parser *parser,
- va_list *p_va, int flags);
- static const char *skipitem(const char **, va_list *, int);
- int
- PyArg_Parse(PyObject *args, const char *format, ...)
- {
- int retval;
- va_list va;
- va_start(va, format);
- retval = vgetargs1(args, format, &va, FLAG_COMPAT);
- va_end(va);
- return retval;
- }
- PyAPI_FUNC(int)
- _PyArg_Parse_SizeT(PyObject *args, const char *format, ...)
- {
- int retval;
- va_list va;
- va_start(va, format);
- retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
- va_end(va);
- return retval;
- }
- int
- PyArg_ParseTuple(PyObject *args, const char *format, ...)
- {
- int retval;
- va_list va;
- va_start(va, format);
- retval = vgetargs1(args, format, &va, 0);
- va_end(va);
- return retval;
- }
- PyAPI_FUNC(int)
- _PyArg_ParseTuple_SizeT(PyObject *args, const char *format, ...)
- {
- int retval;
- va_list va;
- va_start(va, format);
- retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
- va_end(va);
- return retval;
- }
- int
- _PyArg_ParseStack(PyObject *const *args, Py_ssize_t nargs, const char *format, ...)
- {
- int retval;
- va_list va;
- va_start(va, format);
- retval = vgetargs1_impl(NULL, args, nargs, format, &va, 0);
- va_end(va);
- return retval;
- }
- PyAPI_FUNC(int)
- _PyArg_ParseStack_SizeT(PyObject *const *args, Py_ssize_t nargs, const char *format, ...)
- {
- int retval;
- va_list va;
- va_start(va, format);
- retval = vgetargs1_impl(NULL, args, nargs, format, &va, FLAG_SIZE_T);
- va_end(va);
- return retval;
- }
- int
- PyArg_VaParse(PyObject *args, const char *format, va_list va)
- {
- va_list lva;
- int retval;
- va_copy(lva, va);
- retval = vgetargs1(args, format, &lva, 0);
- va_end(lva);
- return retval;
- }
- PyAPI_FUNC(int)
- _PyArg_VaParse_SizeT(PyObject *args, const char *format, va_list va)
- {
- va_list lva;
- int retval;
- va_copy(lva, va);
- retval = vgetargs1(args, format, &lva, FLAG_SIZE_T);
- va_end(lva);
- return retval;
- }
- /* Handle cleanup of allocated memory in case of exception */
- static int
- cleanup_ptr(PyObject *self, void *ptr)
- {
- void **pptr = (void **)ptr;
- PyMem_Free(*pptr);
- *pptr = NULL;
- return 0;
- }
- static int
- cleanup_buffer(PyObject *self, void *ptr)
- {
- Py_buffer *buf = (Py_buffer *)ptr;
- if (buf) {
- PyBuffer_Release(buf);
- }
- return 0;
- }
- static int
- addcleanup(void *ptr, freelist_t *freelist, destr_t destructor)
- {
- int index;
- index = freelist->first_available;
- freelist->first_available += 1;
- freelist->entries[index].item = ptr;
- freelist->entries[index].destructor = destructor;
- return 0;
- }
- static int
- cleanreturn(int retval, freelist_t *freelist)
- {
- int index;
- if (retval == 0) {
- /* A failure occurred, therefore execute all of the cleanup
- functions.
- */
- for (index = 0; index < freelist->first_available; ++index) {
- freelist->entries[index].destructor(NULL,
- freelist->entries[index].item);
- }
- }
- if (freelist->entries_malloced)
- PyMem_Free(freelist->entries);
- return retval;
- }
- static int
- vgetargs1_impl(PyObject *compat_args, PyObject *const *stack, Py_ssize_t nargs, const char *format,
- va_list *p_va, int flags)
- {
- char msgbuf[256];
- int levels[32];
- const char *fname = NULL;
- const char *message = NULL;
- int min = -1;
- int max = 0;
- int level = 0;
- int endfmt = 0;
- const char *formatsave = format;
- Py_ssize_t i;
- const char *msg;
- int compat = flags & FLAG_COMPAT;
- freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
- freelist_t freelist;
- assert(nargs == 0 || stack != NULL);
- freelist.entries = static_entries;
- freelist.first_available = 0;
- freelist.entries_malloced = 0;
- flags = flags & ~FLAG_COMPAT;
- while (endfmt == 0) {
- int c = *format++;
- switch (c) {
- case '(':
- if (level == 0)
- max++;
- level++;
- if (level >= 30)
- Py_FatalError("too many tuple nesting levels "
- "in argument format string");
- break;
- case ')':
- if (level == 0)
- Py_FatalError("excess ')' in getargs format");
- else
- level--;
- break;
- case '\0':
- endfmt = 1;
- break;
- case ':':
- fname = format;
- endfmt = 1;
- break;
- case ';':
- message = format;
- endfmt = 1;
- break;
- case '|':
- if (level == 0)
- min = max;
- break;
- default:
- if (level == 0) {
- if (Py_ISALPHA(c))
- if (c != 'e') /* skip encoded */
- max++;
- }
- break;
- }
- }
- if (level != 0)
- Py_FatalError(/* '(' */ "missing ')' in getargs format");
- if (min < 0)
- min = max;
- format = formatsave;
- if (max > STATIC_FREELIST_ENTRIES) {
- freelist.entries = PyMem_NEW(freelistentry_t, max);
- if (freelist.entries == NULL) {
- PyErr_NoMemory();
- return 0;
- }
- freelist.entries_malloced = 1;
- }
- if (compat) {
- if (max == 0) {
- if (compat_args == NULL)
- return 1;
- PyErr_Format(PyExc_TypeError,
- "%.200s%s takes no arguments",
- fname==NULL ? "function" : fname,
- fname==NULL ? "" : "()");
- return cleanreturn(0, &freelist);
- }
- else if (min == 1 && max == 1) {
- if (compat_args == NULL) {
- PyErr_Format(PyExc_TypeError,
- "%.200s%s takes at least one argument",
- fname==NULL ? "function" : fname,
- fname==NULL ? "" : "()");
- return cleanreturn(0, &freelist);
- }
- msg = convertitem(compat_args, &format, p_va, flags, levels,
- msgbuf, sizeof(msgbuf), &freelist);
- if (msg == NULL)
- return cleanreturn(1, &freelist);
- seterror(levels[0], msg, levels+1, fname, message);
- return cleanreturn(0, &freelist);
- }
- else {
- PyErr_SetString(PyExc_SystemError,
- "old style getargs format uses new features");
- return cleanreturn(0, &freelist);
- }
- }
- if (nargs < min || max < nargs) {
- if (message == NULL)
- PyErr_Format(PyExc_TypeError,
- "%.150s%s takes %s %d argument%s (%zd given)",
- fname==NULL ? "function" : fname,
- fname==NULL ? "" : "()",
- min==max ? "exactly"
- : nargs < min ? "at least" : "at most",
- nargs < min ? min : max,
- (nargs < min ? min : max) == 1 ? "" : "s",
- nargs);
- else
- PyErr_SetString(PyExc_TypeError, message);
- return cleanreturn(0, &freelist);
- }
- for (i = 0; i < nargs; i++) {
- if (*format == '|')
- format++;
- msg = convertitem(stack[i], &format, p_va,
- flags, levels, msgbuf,
- sizeof(msgbuf), &freelist);
- if (msg) {
- seterror(i+1, msg, levels, fname, message);
- return cleanreturn(0, &freelist);
- }
- }
- if (*format != '\0' && !Py_ISALPHA(*format) &&
- *format != '(' &&
- *format != '|' && *format != ':' && *format != ';') {
- PyErr_Format(PyExc_SystemError,
- "bad format string: %.200s", formatsave);
- return cleanreturn(0, &freelist);
- }
- return cleanreturn(1, &freelist);
- }
- static int
- vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
- {
- PyObject **stack;
- Py_ssize_t nargs;
- if (!(flags & FLAG_COMPAT)) {
- assert(args != NULL);
- if (!PyTuple_Check(args)) {
- PyErr_SetString(PyExc_SystemError,
- "new style getargs format but argument is not a tuple");
- return 0;
- }
- stack = _PyTuple_ITEMS(args);
- nargs = PyTuple_GET_SIZE(args);
- }
- else {
- stack = NULL;
- nargs = 0;
- }
- return vgetargs1_impl(args, stack, nargs, format, p_va, flags);
- }
- static void
- seterror(Py_ssize_t iarg, const char *msg, int *levels, const char *fname,
- const char *message)
- {
- char buf[512];
- int i;
- char *p = buf;
- if (PyErr_Occurred())
- return;
- else if (message == NULL) {
- if (fname != NULL) {
- PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
- p += strlen(p);
- }
- if (iarg != 0) {
- PyOS_snprintf(p, sizeof(buf) - (p - buf),
- "argument %zd", iarg);
- i = 0;
- p += strlen(p);
- while (i < 32 && levels[i] > 0 && (int)(p-buf) < 220) {
- PyOS_snprintf(p, sizeof(buf) - (p - buf),
- ", item %d", levels[i]-1);
- p += strlen(p);
- i++;
- }
- }
- else {
- PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
- p += strlen(p);
- }
- PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
- message = buf;
- }
- if (msg[0] == '(') {
- PyErr_SetString(PyExc_SystemError, message);
- }
- else {
- PyErr_SetString(PyExc_TypeError, message);
- }
- }
- /* Convert a tuple argument.
- On entry, *p_format points to the character _after_ the opening '('.
- On successful exit, *p_format points to the closing ')'.
- If successful:
- *p_format and *p_va are updated,
- *levels and *msgbuf are untouched,
- and NULL is returned.
- If the argument is invalid:
- *p_format is unchanged,
- *p_va is undefined,
- *levels is a 0-terminated list of item numbers,
- *msgbuf contains an error message, whose format is:
- "must be <typename1>, not <typename2>", where:
- <typename1> is the name of the expected type, and
- <typename2> is the name of the actual type,
- and msgbuf is returned.
- */
- static const char *
- converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
- int *levels, char *msgbuf, size_t bufsize, int toplevel,
- freelist_t *freelist)
- {
- int level = 0;
- int n = 0;
- const char *format = *p_format;
- int i;
- Py_ssize_t len;
- for (;;) {
- int c = *format++;
- if (c == '(') {
- if (level == 0)
- n++;
- level++;
- }
- else if (c == ')') {
- if (level == 0)
- break;
- level--;
- }
- else if (c == ':' || c == ';' || c == '\0')
- break;
- else if (level == 0 && Py_ISALPHA(c) && c != 'e')
- n++;
- }
- if (!PySequence_Check(arg) || PyBytes_Check(arg)) {
- levels[0] = 0;
- PyOS_snprintf(msgbuf, bufsize,
- toplevel ? "expected %d arguments, not %.50s" :
- "must be %d-item sequence, not %.50s",
- n,
- arg == Py_None ? "None" : Py_TYPE(arg)->tp_name);
- return msgbuf;
- }
- len = PySequence_Size(arg);
- if (len != n) {
- levels[0] = 0;
- if (toplevel) {
- PyOS_snprintf(msgbuf, bufsize,
- "expected %d argument%s, not %zd",
- n,
- n == 1 ? "" : "s",
- len);
- }
- else {
- PyOS_snprintf(msgbuf, bufsize,
- "must be sequence of length %d, not %zd",
- n, len);
- }
- return msgbuf;
- }
- format = *p_format;
- for (i = 0; i < n; i++) {
- const char *msg;
- PyObject *item;
- item = PySequence_GetItem(arg, i);
- if (item == NULL) {
- PyErr_Clear();
- levels[0] = i+1;
- levels[1] = 0;
- strncpy(msgbuf, "is not retrievable", bufsize);
- return msgbuf;
- }
- msg = convertitem(item, &format, p_va, flags, levels+1,
- msgbuf, bufsize, freelist);
- /* PySequence_GetItem calls tp->sq_item, which INCREFs */
- Py_XDECREF(item);
- if (msg != NULL) {
- levels[0] = i+1;
- return msg;
- }
- }
- *p_format = format;
- return NULL;
- }
- /* Convert a single item. */
- static const char *
- convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
- int *levels, char *msgbuf, size_t bufsize, freelist_t *freelist)
- {
- const char *msg;
- const char *format = *p_format;
- if (*format == '(' /* ')' */) {
- format++;
- msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
- bufsize, 0, freelist);
- if (msg == NULL)
- format++;
- }
- else {
- msg = convertsimple(arg, &format, p_va, flags,
- msgbuf, bufsize, freelist);
- if (msg != NULL)
- levels[0] = 0;
- }
- if (msg == NULL)
- *p_format = format;
- return msg;
- }
- /* Format an error message generated by convertsimple().
- displayname must be UTF-8 encoded.
- */
- void
- _PyArg_BadArgument(const char *fname, const char *displayname,
- const char *expected, PyObject *arg)
- {
- PyErr_Format(PyExc_TypeError,
- "%.200s() %.200s must be %.50s, not %.50s",
- fname, displayname, expected,
- arg == Py_None ? "None" : Py_TYPE(arg)->tp_name);
- }
- static const char *
- converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
- {
- assert(expected != NULL);
- assert(arg != NULL);
- if (expected[0] == '(') {
- PyOS_snprintf(msgbuf, bufsize,
- "%.100s", expected);
- }
- else {
- PyOS_snprintf(msgbuf, bufsize,
- "must be %.50s, not %.50s", expected,
- arg == Py_None ? "None" : Py_TYPE(arg)->tp_name);
- }
- return msgbuf;
- }
- #define CONV_UNICODE "(unicode conversion error)"
- /* Convert a non-tuple argument. Return NULL if conversion went OK,
- or a string with a message describing the failure. The message is
- formatted as "must be <desired type>, not <actual type>".
- When failing, an exception may or may not have been raised.
- Don't call if a tuple is expected.
- When you add new format codes, please don't forget poor skipitem() below.
- */
- static const char *
- convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
- char *msgbuf, size_t bufsize, freelist_t *freelist)
- {
- #define RETURN_ERR_OCCURRED return msgbuf
- /* For # codes */
- #define REQUIRE_PY_SSIZE_T_CLEAN \
- if (!(flags & FLAG_SIZE_T)) { \
- PyErr_SetString(PyExc_SystemError, \
- "PY_SSIZE_T_CLEAN macro must be defined for '#' formats"); \
- RETURN_ERR_OCCURRED; \
- }
- const char *format = *p_format;
- char c = *format++;
- const char *sarg;
- switch (c) {
- case 'b': { /* unsigned byte -- very short int */
- unsigned char *p = va_arg(*p_va, unsigned char *);
- long ival = PyLong_AsLong(arg);
- if (ival == -1 && PyErr_Occurred())
- RETURN_ERR_OCCURRED;
- else if (ival < 0) {
- PyErr_SetString(PyExc_OverflowError,
- "unsigned byte integer is less than minimum");
- RETURN_ERR_OCCURRED;
- }
- else if (ival > UCHAR_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "unsigned byte integer is greater than maximum");
- RETURN_ERR_OCCURRED;
- }
- else
- *p = (unsigned char) ival;
- break;
- }
- case 'B': {/* byte sized bitfield - both signed and unsigned
- values allowed */
- unsigned char *p = va_arg(*p_va, unsigned char *);
- unsigned long ival = PyLong_AsUnsignedLongMask(arg);
- if (ival == (unsigned long)-1 && PyErr_Occurred())
- RETURN_ERR_OCCURRED;
- else
- *p = (unsigned char) ival;
- break;
- }
- case 'h': {/* signed short int */
- short *p = va_arg(*p_va, short *);
- long ival = PyLong_AsLong(arg);
- if (ival == -1 && PyErr_Occurred())
- RETURN_ERR_OCCURRED;
- else if (ival < SHRT_MIN) {
- PyErr_SetString(PyExc_OverflowError,
- "signed short integer is less than minimum");
- RETURN_ERR_OCCURRED;
- }
- else if (ival > SHRT_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "signed short integer is greater than maximum");
- RETURN_ERR_OCCURRED;
- }
- else
- *p = (short) ival;
- break;
- }
- case 'H': { /* short int sized bitfield, both signed and
- unsigned allowed */
- unsigned short *p = va_arg(*p_va, unsigned short *);
- unsigned long ival = PyLong_AsUnsignedLongMask(arg);
- if (ival == (unsigned long)-1 && PyErr_Occurred())
- RETURN_ERR_OCCURRED;
- else
- *p = (unsigned short) ival;
- break;
- }
- case 'i': {/* signed int */
- int *p = va_arg(*p_va, int *);
- long ival = PyLong_AsLong(arg);
- if (ival == -1 && PyErr_Occurred())
- RETURN_ERR_OCCURRED;
- else if (ival > INT_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "signed integer is greater than maximum");
- RETURN_ERR_OCCURRED;
- }
- else if (ival < INT_MIN) {
- PyErr_SetString(PyExc_OverflowError,
- "signed integer is less than minimum");
- RETURN_ERR_OCCURRED;
- }
- else
- *p = ival;
- break;
- }
- case 'I': { /* int sized bitfield, both signed and
- unsigned allowed */
- unsigned int *p = va_arg(*p_va, unsigned int *);
- unsigned long ival = PyLong_AsUnsignedLongMask(arg);
- if (ival == (unsigned long)-1 && PyErr_Occurred())
- RETURN_ERR_OCCURRED;
- else
- *p = (unsigned int) ival;
- break;
- }
- case 'n': /* Py_ssize_t */
- {
- PyObject *iobj;
- Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
- Py_ssize_t ival = -1;
- iobj = _PyNumber_Index(arg);
- if (iobj != NULL) {
- ival = PyLong_AsSsize_t(iobj);
- Py_DECREF(iobj);
- }
- if (ival == -1 && PyErr_Occurred())
- RETURN_ERR_OCCURRED;
- *p = ival;
- break;
- }
- case 'l': {/* long int */
- long *p = va_arg(*p_va, long *);
- long ival = PyLong_AsLong(arg);
- if (ival == -1 && PyErr_Occurred())
- RETURN_ERR_OCCURRED;
- else
- *p = ival;
- break;
- }
- case 'k': { /* long sized bitfield */
- unsigned long *p = va_arg(*p_va, unsigned long *);
- unsigned long ival;
- if (PyLong_Check(arg))
- ival = PyLong_AsUnsignedLongMask(arg);
- else
- return converterr("int", arg, msgbuf, bufsize);
- *p = ival;
- break;
- }
- case 'L': {/* long long */
- long long *p = va_arg( *p_va, long long * );
- long long ival = PyLong_AsLongLong(arg);
- if (ival == (long long)-1 && PyErr_Occurred())
- RETURN_ERR_OCCURRED;
- else
- *p = ival;
- break;
- }
- case 'K': { /* long long sized bitfield */
- unsigned long long *p = va_arg(*p_va, unsigned long long *);
- unsigned long long ival;
- if (PyLong_Check(arg))
- ival = PyLong_AsUnsignedLongLongMask(arg);
- else
- return converterr("int", arg, msgbuf, bufsize);
- *p = ival;
- break;
- }
- case 'f': {/* float */
- float *p = va_arg(*p_va, float *);
- double dval = PyFloat_AsDouble(arg);
- if (dval == -1.0 && PyErr_Occurred())
- RETURN_ERR_OCCURRED;
- else
- *p = (float) dval;
- break;
- }
- case 'd': {/* double */
- double *p = va_arg(*p_va, double *);
- double dval = PyFloat_AsDouble(arg);
- if (dval == -1.0 && PyErr_Occurred())
- RETURN_ERR_OCCURRED;
- else
- *p = dval;
- break;
- }
- case 'D': {/* complex double */
- Py_complex *p = va_arg(*p_va, Py_complex *);
- Py_complex cval;
- cval = PyComplex_AsCComplex(arg);
- if (PyErr_Occurred())
- RETURN_ERR_OCCURRED;
- else
- *p = cval;
- break;
- }
- case 'c': {/* char */
- char *p = va_arg(*p_va, char *);
- if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
- *p = PyBytes_AS_STRING(arg)[0];
- else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1)
- *p = PyByteArray_AS_STRING(arg)[0];
- else
- return converterr("a byte string of length 1", arg, msgbuf, bufsize);
- break;
- }
- case 'C': {/* unicode char */
- int *p = va_arg(*p_va, int *);
- int kind;
- const void *data;
- if (!PyUnicode_Check(arg))
- return converterr("a unicode character", arg, msgbuf, bufsize);
- if (PyUnicode_READY(arg))
- RETURN_ERR_OCCURRED;
- if (PyUnicode_GET_LENGTH(arg) != 1)
- return converterr("a unicode character", arg, msgbuf, bufsize);
- kind = PyUnicode_KIND(arg);
- data = PyUnicode_DATA(arg);
- *p = PyUnicode_READ(kind, data, 0);
- break;
- }
- case 'p': {/* boolean *p*redicate */
- int *p = va_arg(*p_va, int *);
- int val = PyObject_IsTrue(arg);
- if (val > 0)
- *p = 1;
- else if (val == 0)
- *p = 0;
- else
- RETURN_ERR_OCCURRED;
- break;
- }
- /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all
- need to be cleaned up! */
- case 'y': {/* any bytes-like object */
- void **p = (void **)va_arg(*p_va, char **);
- const char *buf;
- Py_ssize_t count;
- if (*format == '*') {
- if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
- return converterr(buf, arg, msgbuf, bufsize);
- format++;
- if (addcleanup(p, freelist, cleanup_buffer)) {
- return converterr(
- "(cleanup problem)",
- arg, msgbuf, bufsize);
- }
- break;
- }
- count = convertbuffer(arg, (const void **)p, &buf);
- if (count < 0)
- return converterr(buf, arg, msgbuf, bufsize);
- if (*format == '#') {
- REQUIRE_PY_SSIZE_T_CLEAN;
- Py_ssize_t *psize = va_arg(*p_va, Py_ssize_t*);
- *psize = count;
- format++;
- } else {
- if (strlen(*p) != (size_t)count) {
- PyErr_SetString(PyExc_ValueError, "embedded null byte");
- RETURN_ERR_OCCURRED;
- }
- }
- break;
- }
- case 's': /* text string or bytes-like object */
- case 'z': /* text string, bytes-like object or None */
- {
- if (*format == '*') {
- /* "s*" or "z*" */
- Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
- if (c == 'z' && arg == Py_None)
- PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
- else if (PyUnicode_Check(arg)) {
- Py_ssize_t len;
- sarg = PyUnicode_AsUTF8AndSize(arg, &len);
- if (sarg == NULL)
- return converterr(CONV_UNICODE,
- arg, msgbuf, bufsize);
- PyBuffer_FillInfo(p, arg, (void *)sarg, len, 1, 0);
- }
- else { /* any bytes-like object */
- const char *buf;
- if (getbuffer(arg, p, &buf) < 0)
- return converterr(buf, arg, msgbuf, bufsize);
- }
- if (addcleanup(p, freelist, cleanup_buffer)) {
- return converterr(
- "(cleanup problem)",
- arg, msgbuf, bufsize);
- }
- format++;
- } else if (*format == '#') { /* a string or read-only bytes-like object */
- /* "s#" or "z#" */
- const void **p = (const void **)va_arg(*p_va, const char **);
- REQUIRE_PY_SSIZE_T_CLEAN;
- Py_ssize_t *psize = va_arg(*p_va, Py_ssize_t*);
- if (c == 'z' && arg == Py_None) {
- *p = NULL;
- *psize = 0;
- }
- else if (PyUnicode_Check(arg)) {
- Py_ssize_t len;
- sarg = PyUnicode_AsUTF8AndSize(arg, &len);
- if (sarg == NULL)
- return converterr(CONV_UNICODE,
- arg, msgbuf, bufsize);
- *p = sarg;
- *psize = len;
- }
- else { /* read-only bytes-like object */
- /* XXX Really? */
- const char *buf;
- Py_ssize_t count = convertbuffer(arg, p, &buf);
- if (count < 0)
- return converterr(buf, arg, msgbuf, bufsize);
- *psize = count;
- }
- format++;
- } else {
- /* "s" or "z" */
- const char **p = va_arg(*p_va, const char **);
- Py_ssize_t len;
- sarg = NULL;
- if (c == 'z' && arg == Py_None)
- *p = NULL;
- else if (PyUnicode_Check(arg)) {
- sarg = PyUnicode_AsUTF8AndSize(arg, &len);
- if (sarg == NULL)
- return converterr(CONV_UNICODE,
- arg, msgbuf, bufsize);
- if (strlen(sarg) != (size_t)len) {
- PyErr_SetString(PyExc_ValueError, "embedded null character");
- RETURN_ERR_OCCURRED;
- }
- *p = sarg;
- }
- else
- return converterr(c == 'z' ? "str or None" : "str",
- arg, msgbuf, bufsize);
- }
- break;
- }
- case 'e': {/* encoded string */
- char **buffer;
- const char *encoding;
- PyObject *s;
- int recode_strings;
- Py_ssize_t size;
- const char *ptr;
- /* Get 'e' parameter: the encoding name */
- encoding = (const char *)va_arg(*p_va, const char *);
- if (encoding == NULL)
- encoding = PyUnicode_GetDefaultEncoding();
- /* Get output buffer parameter:
- 's' (recode all objects via Unicode) or
- 't' (only recode non-string objects)
- */
- if (*format == 's')
- recode_strings = 1;
- else if (*format == 't')
- recode_strings = 0;
- else
- return converterr(
- "(unknown parser marker combination)",
- arg, msgbuf, bufsize);
- buffer = (char **)va_arg(*p_va, char **);
- format++;
- if (buffer == NULL)
- return converterr("(buffer is NULL)",
- arg, msgbuf, bufsize);
- /* Encode object */
- if (!recode_strings &&
- (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
- s = Py_NewRef(arg);
- if (PyBytes_Check(arg)) {
- size = PyBytes_GET_SIZE(s);
- ptr = PyBytes_AS_STRING(s);
- }
- else {
- size = PyByteArray_GET_SIZE(s);
- ptr = PyByteArray_AS_STRING(s);
- }
- }
- else if (PyUnicode_Check(arg)) {
- /* Encode object; use default error handling */
- s = PyUnicode_AsEncodedString(arg,
- encoding,
- NULL);
- if (s == NULL)
- return converterr("(encoding failed)",
- arg, msgbuf, bufsize);
- assert(PyBytes_Check(s));
- size = PyBytes_GET_SIZE(s);
- ptr = PyBytes_AS_STRING(s);
- if (ptr == NULL)
- ptr = "";
- }
- else {
- return converterr(
- recode_strings ? "str" : "str, bytes or bytearray",
- arg, msgbuf, bufsize);
- }
- /* Write output; output is guaranteed to be 0-terminated */
- if (*format == '#') {
- /* Using buffer length parameter '#':
- - if *buffer is NULL, a new buffer of the
- needed size is allocated and the data
- copied into it; *buffer is updated to point
- to the new buffer; the caller is
- responsible for PyMem_Free()ing it after
- usage
- - if *buffer is not NULL, the data is
- copied to *buffer; *buffer_len has to be
- set to the size of the buffer on input;
- buffer overflow is signalled with an error;
- buffer has to provide enough room for the
- encoded string plus the trailing 0-byte
- - in both cases, *buffer_len is updated to
- the size of the buffer /excluding/ the
- trailing 0-byte
- */
- REQUIRE_PY_SSIZE_T_CLEAN;
- Py_ssize_t *psize = va_arg(*p_va, Py_ssize_t*);
- format++;
- if (psize == NULL) {
- Py_DECREF(s);
- return converterr(
- "(buffer_len is NULL)",
- arg, msgbuf, bufsize);
- }
- if (*buffer == NULL) {
- *buffer = PyMem_NEW(char, size + 1);
- if (*buffer == NULL) {
- Py_DECREF(s);
- PyErr_NoMemory();
- RETURN_ERR_OCCURRED;
- }
- if (addcleanup(buffer, freelist, cleanup_ptr)) {
- Py_DECREF(s);
- return converterr(
- "(cleanup problem)",
- arg, msgbuf, bufsize);
- }
- } else {
- if (size + 1 > *psize) {
- Py_DECREF(s);
- PyErr_Format(PyExc_ValueError,
- "encoded string too long "
- "(%zd, maximum length %zd)",
- (Py_ssize_t)size, (Py_ssize_t)(*psize - 1));
- RETURN_ERR_OCCURRED;
- }
- }
- memcpy(*buffer, ptr, size+1);
- *psize = size;
- }
- else {
- /* Using a 0-terminated buffer:
- - the encoded string has to be 0-terminated
- for this variant to work; if it is not, an
- error raised
- - a new buffer of the needed size is
- allocated and the data copied into it;
- *buffer is updated to point to the new
- buffer; the caller is responsible for
- PyMem_Free()ing it after usage
- */
- if ((Py_ssize_t)strlen(ptr) != size) {
- Py_DECREF(s);
- return converterr(
- "encoded string without null bytes",
- arg, msgbuf, bufsize);
- }
- *buffer = PyMem_NEW(char, size + 1);
- if (*buffer == NULL) {
- Py_DECREF(s);
- PyErr_NoMemory();
- RETURN_ERR_OCCURRED;
- }
- if (addcleanup(buffer, freelist, cleanup_ptr)) {
- Py_DECREF(s);
- return converterr("(cleanup problem)",
- arg, msgbuf, bufsize);
- }
- memcpy(*buffer, ptr, size+1);
- }
- Py_DECREF(s);
- break;
- }
- case 'S': { /* PyBytes object */
- PyObject **p = va_arg(*p_va, PyObject **);
- if (PyBytes_Check(arg))
- *p = arg;
- else
- return converterr("bytes", arg, msgbuf, bufsize);
- break;
- }
- case 'Y': { /* PyByteArray object */
- PyObject **p = va_arg(*p_va, PyObject **);
- if (PyByteArray_Check(arg))
- *p = arg;
- else
- return converterr("bytearray", arg, msgbuf, bufsize);
- break;
- }
- case 'U': { /* PyUnicode object */
- PyObject **p = va_arg(*p_va, PyObject **);
- if (PyUnicode_Check(arg)) {
- if (PyUnicode_READY(arg) == -1)
- RETURN_ERR_OCCURRED;
- *p = arg;
- }
- else
- return converterr("str", arg, msgbuf, bufsize);
- break;
- }
- case 'O': { /* object */
- PyTypeObject *type;
- PyObject **p;
- if (*format == '!') {
- type = va_arg(*p_va, PyTypeObject*);
- p = va_arg(*p_va, PyObject **);
- format++;
- if (PyType_IsSubtype(Py_TYPE(arg), type))
- *p = arg;
- else
- return converterr(type->tp_name, arg, msgbuf, bufsize);
- }
- else if (*format == '&') {
- typedef int (*converter)(PyObject *, void *);
- converter convert = va_arg(*p_va, converter);
- void *addr = va_arg(*p_va, void *);
- int res;
- format++;
- if (! (res = (*convert)(arg, addr)))
- return converterr("(unspecified)",
- arg, msgbuf, bufsize);
- if (res == Py_CLEANUP_SUPPORTED &&
- addcleanup(addr, freelist, convert) == -1)
- return converterr("(cleanup problem)",
- arg, msgbuf, bufsize);
- }
- else {
- p = va_arg(*p_va, PyObject **);
- *p = arg;
- }
- break;
- }
- case 'w': { /* "w*": memory buffer, read-write access */
- void **p = va_arg(*p_va, void **);
- if (*format != '*')
- return converterr(
- "(invalid use of 'w' format character)",
- arg, msgbuf, bufsize);
- format++;
- /* Caller is interested in Py_buffer, and the object
- supports it directly. */
- if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
- PyErr_Clear();
- return converterr("read-write bytes-like object",
- arg, msgbuf, bufsize);
- }
- if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) {
- PyBuffer_Release((Py_buffer*)p);
- return converterr("contiguous buffer", arg, msgbuf, bufsize);
- }
- if (addcleanup(p, freelist, cleanup_buffer)) {
- return converterr(
- "(cleanup problem)",
- arg, msgbuf, bufsize);
- }
- break;
- }
- default:
- return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize);
- }
- *p_format = format;
- return NULL;
- #undef REQUIRE_PY_SSIZE_T_CLEAN
- #undef RETURN_ERR_OCCURRED
- }
- static Py_ssize_t
- convertbuffer(PyObject *arg, const void **p, const char **errmsg)
- {
- PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer;
- Py_ssize_t count;
- Py_buffer view;
- *errmsg = NULL;
- *p = NULL;
- if (pb != NULL && pb->bf_releasebuffer != NULL) {
- *errmsg = "read-only bytes-like object";
- return -1;
- }
- if (getbuffer(arg, &view, errmsg) < 0)
- return -1;
- count = view.len;
- *p = view.buf;
- PyBuffer_Release(&view);
- return count;
- }
- static int
- getbuffer(PyObject *arg, Py_buffer *view, const char **errmsg)
- {
- if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) {
- *errmsg = "bytes-like object";
- return -1;
- }
- if (!PyBuffer_IsContiguous(view, 'C')) {
- PyBuffer_Release(view);
- *errmsg = "contiguous buffer";
- return -1;
- }
- return 0;
- }
- /* Support for keyword arguments donated by
- Geoff Philbrick <philbric@delphi.hks.com> */
- /* Return false (0) for error, else true. */
- int
- PyArg_ParseTupleAndKeywords(PyObject *args,
- PyObject *keywords,
- const char *format,
- char **kwlist, ...)
- {
- int retval;
- va_list va;
- if ((args == NULL || !PyTuple_Check(args)) ||
- (keywords != NULL && !PyDict_Check(keywords)) ||
- format == NULL ||
- kwlist == NULL)
- {
- PyErr_BadInternalCall();
- return 0;
- }
- va_start(va, kwlist);
- retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
- va_end(va);
- return retval;
- }
- PyAPI_FUNC(int)
- _PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
- PyObject *keywords,
- const char *format,
- char **kwlist, ...)
- {
- int retval;
- va_list va;
- if ((args == NULL || !PyTuple_Check(args)) ||
- (keywords != NULL && !PyDict_Check(keywords)) ||
- format == NULL ||
- kwlist == NULL)
- {
- PyErr_BadInternalCall();
- return 0;
- }
- va_start(va, kwlist);
- retval = vgetargskeywords(args, keywords, format,
- kwlist, &va, FLAG_SIZE_T);
- va_end(va);
- return retval;
- }
- int
- PyArg_VaParseTupleAndKeywords(PyObject *args,
- PyObject *keywords,
- const char *format,
- char **kwlist, va_list va)
- {
- int retval;
- va_list lva;
- if ((args == NULL || !PyTuple_Check(args)) ||
- (keywords != NULL && !PyDict_Check(keywords)) ||
- format == NULL ||
- kwlist == NULL)
- {
- PyErr_BadInternalCall();
- return 0;
- }
- va_copy(lva, va);
- retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
- va_end(lva);
- return retval;
- }
- PyAPI_FUNC(int)
- _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
- PyObject *keywords,
- const char *format,
- char **kwlist, va_list va)
- {
- int retval;
- va_list lva;
- if ((args == NULL || !PyTuple_Check(args)) ||
- (keywords != NULL && !PyDict_Check(keywords)) ||
- format == NULL ||
- kwlist == NULL)
- {
- PyErr_BadInternalCall();
- return 0;
- }
- va_copy(lva, va);
- retval = vgetargskeywords(args, keywords, format,
- kwlist, &lva, FLAG_SIZE_T);
- va_end(lva);
- return retval;
- }
- PyAPI_FUNC(int)
- _PyArg_ParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
- struct _PyArg_Parser *parser, ...)
- {
- int retval;
- va_list va;
- va_start(va, parser);
- retval = vgetargskeywordsfast(args, keywords, parser, &va, 0);
- va_end(va);
- return retval;
- }
- PyAPI_FUNC(int)
- _PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
- struct _PyArg_Parser *parser, ...)
- {
- int retval;
- va_list va;
- va_start(va, parser);
- retval = vgetargskeywordsfast(args, keywords, parser, &va, FLAG_SIZE_T);
- va_end(va);
- return retval;
- }
- PyAPI_FUNC(int)
- _PyArg_ParseStackAndKeywords(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
- struct _PyArg_Parser *parser, ...)
- {
- int retval;
- va_list va;
- va_start(va, parser);
- retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, 0);
- va_end(va);
- return retval;
- }
- PyAPI_FUNC(int)
- _PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
- struct _PyArg_Parser *parser, ...)
- {
- int retval;
- va_list va;
- va_start(va, parser);
- retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, FLAG_SIZE_T);
- va_end(va);
- return retval;
- }
- PyAPI_FUNC(int)
- _PyArg_VaParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
- struct _PyArg_Parser *parser, va_list va)
- {
- int retval;
- va_list lva;
- va_copy(lva, va);
- retval = vgetargskeywordsfast(args, keywords, parser, &lva, 0);
- va_end(lva);
- return retval;
- }
- PyAPI_FUNC(int)
- _PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
- struct _PyArg_Parser *parser, va_list va)
- {
- int retval;
- va_list lva;
- va_copy(lva, va);
- retval = vgetargskeywordsfast(args, keywords, parser, &lva, FLAG_SIZE_T);
- va_end(lva);
- return retval;
- }
- static void
- error_unexpected_keyword_arg(PyObject *kwargs, PyObject *kwnames, PyObject *kwtuple, const char *fname)
- {
- /* make sure there are no extraneous keyword arguments */
- Py_ssize_t j = 0;
- while (1) {
- PyObject *keyword;
- if (kwargs != NULL) {
- if (!PyDict_Next(kwargs, &j, &keyword, NULL))
- break;
- }
- else {
- if (j >= PyTuple_GET_SIZE(kwnames))
- break;
- keyword = PyTuple_GET_ITEM(kwnames, j);
- j++;
- }
- if (!PyUnicode_Check(keyword)) {
- PyErr_SetString(PyExc_TypeError,
- "keywords must be strings");
- return;
- }
- int match = PySequence_Contains(kwtuple, keyword);
- if (match <= 0) {
- if (!match) {
- PyErr_Format(PyExc_TypeError,
- "'%S' is an invalid keyword "
- "argument for %.200s%s",
- keyword,
- (fname == NULL) ? "this function" : fname,
- (fname == NULL) ? "" : "()");
- }
- return;
- }
- }
- /* Something wrong happened. There are extraneous keyword arguments,
- * but we don't know what. And we don't bother. */
- PyErr_Format(PyExc_TypeError,
- "invalid keyword argument for %.200s%s",
- (fname == NULL) ? "this function" : fname,
- (fname == NULL) ? "" : "()");
- }
- int
- PyArg_ValidateKeywordArguments(PyObject *kwargs)
- {
- if (!PyDict_Check(kwargs)) {
- PyErr_BadInternalCall();
- return 0;
- }
- if (!_PyDict_HasOnlyStringKeys(kwargs)) {
- PyErr_SetString(PyExc_TypeError,
- "keywords must be strings");
- return 0;
- }
- return 1;
- }
- #define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
- static int
- vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
- char **kwlist, va_list *p_va, int flags)
- {
- char msgbuf[512];
- int levels[32];
- const char *fname, *msg, *custom_msg;
- int min = INT_MAX;
- int max = INT_MAX;
- int i, pos, len;
- int skip = 0;
- Py_ssize_t nargs, nkwargs;
- PyObject *current_arg;
- freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
- freelist_t freelist;
- freelist.entries = static_entries;
- freelist.first_available = 0;
- freelist.entries_malloced = 0;
- assert(args != NULL && PyTuple_Check(args));
- assert(kwargs == NULL || PyDict_Check(kwargs));
- assert(format != NULL);
- assert(kwlist != NULL);
- assert(p_va != NULL);
- /* grab the function name or custom error msg first (mutually exclusive) */
- fname = strchr(format, ':');
- if (fname) {
- fname++;
- custom_msg = NULL;
- }
- else {
- custom_msg = strchr(format,';');
- if (custom_msg)
- custom_msg++;
- }
- /* scan kwlist and count the number of positional-only parameters */
- for (pos = 0; kwlist[pos] && !*kwlist[pos]; pos++) {
- }
- /* scan kwlist and get greatest possible nbr of args */
- for (len = pos; kwlist[len]; len++) {
- if (!*kwlist[len]) {
- PyErr_SetString(PyExc_SystemError,
- "Empty keyword parameter name");
- return cleanreturn(0, &freelist);
- }
- }
- if (len > STATIC_FREELIST_ENTRIES) {
- freelist.entries = PyMem_NEW(freelistentry_t, len);
- if (freelist.entries == NULL) {
- PyErr_NoMemory();
- return 0;
- }
- freelist.entries_malloced = 1;
- }
- nargs = PyTuple_GET_SIZE(args);
- nkwargs = (kwargs == NULL) ? 0 : PyDict_GET_SIZE(kwargs);
- if (nargs + nkwargs > len) {
- /* Adding "keyword" (when nargs == 0) prevents producing wrong error
- messages in some special cases (see bpo-31229). */
- PyErr_Format(PyExc_TypeError,
- "%.200s%s takes at most %d %sargument%s (%zd given)",
- (fname == NULL) ? "function" : fname,
- (fname == NULL) ? "" : "()",
- len,
- (nargs == 0) ? "keyword " : "",
- (len == 1) ? "" : "s",
- nargs + nkwargs);
- return cleanreturn(0, &freelist);
- }
- /* convert tuple args and keyword args in same loop, using kwlist to drive process */
- for (i = 0; i < len; i++) {
- if (*format == '|') {
- if (min != INT_MAX) {
- PyErr_SetString(PyExc_SystemError,
- "Invalid format string (| specified twice)");
- return cleanreturn(0, &freelist);
- }
- min = i;
- format++;
- if (max != INT_MAX) {
- PyErr_SetString(PyExc_SystemError,
- "Invalid format string ($ before |)");
- return cleanreturn(0, &freelist);
- }
- }
- if (*format == '$') {
- if (max != INT_MAX) {
- PyErr_SetString(PyExc_SystemError,
- "Invalid format string ($ specified twice)");
- return cleanreturn(0, &freelist);
- }
- max = i;
- format++;
- if (max < pos) {
- PyErr_SetString(PyExc_SystemError,
- "Empty parameter name after $");
- return cleanreturn(0, &freelist);
- }
- if (skip) {
- /* Now we know the minimal and the maximal numbers of
- * positional arguments and can raise an exception with
- * informative message (see below). */
- break;
- }
- if (max < nargs) {
- if (max == 0) {
- PyErr_Format(PyExc_TypeError,
- "%.200s%s takes no positional arguments",
- (fname == NULL) ? "function" : fname,
- (fname == NULL) ? "" : "()");
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "%.200s%s takes %s %d positional argument%s"
- " (%zd given)",
- (fname == NULL) ? "function" : fname,
- (fname == NULL) ? "" : "()",
- (min != INT_MAX) ? "at most" : "exactly",
- max,
- max == 1 ? "" : "s",
- nargs);
- }
- return cleanreturn(0, &freelist);
- }
- }
- if (IS_END_OF_FORMAT(*format)) {
- PyErr_Format(PyExc_SystemError,
- "More keyword list entries (%d) than "
- "format specifiers (%d)", len, i);
- return cleanreturn(0, &freelist);
- }
- if (!skip) {
- if (i < nargs) {
- current_arg = PyTuple_GET_ITEM(args, i);
- }
- else if (nkwargs && i >= pos) {
- current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]);
- if (current_arg) {
- --nkwargs;
- }
- else if (PyErr_Occurred()) {
- return cleanreturn(0, &freelist);
- }
- }
- else {
- current_arg = NULL;
- }
- if (current_arg) {
- msg = convertitem(current_arg, &format, p_va, flags,
- levels, msgbuf, sizeof(msgbuf), &freelist);
- if (msg) {
- seterror(i+1, msg, levels, fname, custom_msg);
- return cleanreturn(0, &freelist);
- }
- continue;
- }
- if (i < min) {
- if (i < pos) {
- assert (min == INT_MAX);
- assert (max == INT_MAX);
- skip = 1;
- /* At that moment we still don't know the minimal and
- * the maximal numbers of positional arguments. Raising
- * an exception is deferred until we encounter | and $
- * or the end of the format. */
- }
- else {
- PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
- "argument '%s' (pos %d)",
- (fname == NULL) ? "function" : fname,
- (fname == NULL) ? "" : "()",
- kwlist[i], i+1);
- return cleanreturn(0, &freelist);
- }
- }
- /* current code reports success when all required args
- * fulfilled and no keyword args left, with no further
- * validation. XXX Maybe skip this in debug build ?
- */
- if (!nkwargs && !skip) {
- return cleanreturn(1, &freelist);
- }
- }
- /* We are into optional args, skip through to any remaining
- * keyword args */
- msg = skipitem(&format, p_va, flags);
- if (msg) {
- PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
- format);
- return cleanreturn(0, &freelist);
- }
- }
- if (skip) {
- PyErr_Format(PyExc_TypeError,
- "%.200s%s takes %s %d positional argument%s"
- " (%zd given)",
- (fname == NULL) ? "function" : fname,
- (fname == NULL) ? "" : "()",
- (Py_MIN(pos, min) < i) ? "at least" : "exactly",
- Py_MIN(pos, min),
- Py_MIN(pos, min) == 1 ? "" : "s",
- nargs);
- return cleanreturn(0, &freelist);
- }
- if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
- PyErr_Format(PyExc_SystemError,
- "more argument specifiers than keyword list entries "
- "(remaining format:'%s')", format);
- return cleanreturn(0, &freelist);
- }
- if (nkwargs > 0) {
- PyObject *key;
- Py_ssize_t j;
- /* make sure there are no arguments given by name and position */
- for (i = pos; i < nargs; i++) {
- current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]);
- if (current_arg) {
- /* arg present in tuple and in dict */
- PyErr_Format(PyExc_TypeError,
- "argument for %.200s%s given by name ('%s') "
- "and position (%d)",
- (fname == NULL) ? "function" : fname,
- (fname == NULL) ? "" : "()",
- kwlist[i], i+1);
- return cleanreturn(0, &freelist);
- }
- else if (PyErr_Occurred()) {
- return cleanreturn(0, &freelist);
- }
- }
- /* make sure there are no extraneous keyword arguments */
- j = 0;
- while (PyDict_Next(kwargs, &j, &key, NULL)) {
- int match = 0;
- if (!PyUnicode_Check(key)) {
- PyErr_SetString(PyExc_TypeError,
- "keywords must be strings");
- return cleanreturn(0, &freelist);
- }
- for (i = pos; i < len; i++) {
- if (_PyUnicode_EqualToASCIIString(key, kwlist[i])) {
- match = 1;
- break;
- }
- }
- if (!match) {
- PyErr_Format(PyExc_TypeError,
- "'%U' is an invalid keyword "
- "argument for %.200s%s",
- key,
- (fname == NULL) ? "this function" : fname,
- (fname == NULL) ? "" : "()");
- return cleanreturn(0, &freelist);
- }
- }
- /* Something wrong happened. There are extraneous keyword arguments,
- * but we don't know what. And we don't bother. */
- PyErr_Format(PyExc_TypeError,
- "invalid keyword argument for %.200s%s",
- (fname == NULL) ? "this function" : fname,
- (fname == NULL) ? "" : "()");
- return cleanreturn(0, &freelist);
- }
- return cleanreturn(1, &freelist);
- }
- static int
- scan_keywords(const char * const *keywords, int *ptotal, int *pposonly)
- {
- /* scan keywords and count the number of positional-only parameters */
- int i;
- for (i = 0; keywords[i] && !*keywords[i]; i++) {
- }
- *pposonly = i;
- /* scan keywords and get greatest possible nbr of args */
- for (; keywords[i]; i++) {
- if (!*keywords[i]) {
- PyErr_SetString(PyExc_SystemError,
- "Empty keyword parameter name");
- return -1;
- }
- }
- *ptotal = i;
- return 0;
- }
- static int
- parse_format(const char *format, int total, int npos,
- const char **pfname, const char **pcustommsg,
- int *pmin, int *pmax)
- {
- /* grab the function name or custom error msg first (mutually exclusive) */
- const char *custommsg;
- const char *fname = strchr(format, ':');
- if (fname) {
- fname++;
- custommsg = NULL;
- }
- else {
- custommsg = strchr(format,';');
- if (custommsg) {
- custommsg++;
- }
- }
- int min = INT_MAX;
- int max = INT_MAX;
- for (int i = 0; i < total; i++) {
- if (*format == '|') {
- if (min != INT_MAX) {
- PyErr_SetString(PyExc_SystemError,
- "Invalid format string (| specified twice)");
- return -1;
- }
- if (max != INT_MAX) {
- PyErr_SetString(PyExc_SystemError,
- "Invalid format string ($ before |)");
- return -1;
- }
- min = i;
- format++;
- }
- if (*format == '$') {
- if (max != INT_MAX) {
- PyErr_SetString(PyExc_SystemError,
- "Invalid format string ($ specified twice)");
- return -1;
- }
- if (i < npos) {
- PyErr_SetString(PyExc_SystemError,
- "Empty parameter name after $");
- return -1;
- }
- max = i;
- format++;
- }
- if (IS_END_OF_FORMAT(*format)) {
- PyErr_Format(PyExc_SystemError,
- "More keyword list entries (%d) than "
- "format specifiers (%d)", total, i);
- return -1;
- }
- const char *msg = skipitem(&format, NULL, 0);
- if (msg) {
- PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
- format);
- return -1;
- }
- }
- min = Py_MIN(min, total);
- max = Py_MIN(max, total);
- if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
- PyErr_Format(PyExc_SystemError,
- "more argument specifiers than keyword list entries "
- "(remaining format:'%s')", format);
- return -1;
- }
- *pfname = fname;
- *pcustommsg = custommsg;
- *pmin = min;
- *pmax = max;
- return 0;
- }
- static PyObject *
- new_kwtuple(const char * const *keywords, int total, int pos)
- {
- int nkw = total - pos;
- PyObject *kwtuple = PyTuple_New(nkw);
- if (kwtuple == NULL) {
- return NULL;
- }
- keywords += pos;
- for (int i = 0; i < nkw; i++) {
- PyObject *str = PyUnicode_FromString(keywords[i]);
- if (str == NULL) {
- Py_DECREF(kwtuple);
- return NULL;
- }
- PyInterpreterState *interp = _PyInterpreterState_GET();
- _PyUnicode_InternImmortal(interp, &str);
- PyTuple_SET_ITEM(kwtuple, i, str);
- }
- return kwtuple;
- }
- static int
- _parser_init(struct _PyArg_Parser *parser)
- {
- const char * const *keywords = parser->keywords;
- assert(keywords != NULL);
- assert(parser->pos == 0 &&
- (parser->format == NULL || parser->fname == NULL) &&
- parser->custom_msg == NULL &&
- parser->min == 0 &&
- parser->max == 0);
- int len, pos;
- if (scan_keywords(keywords, &len, &pos) < 0) {
- return 0;
- }
- const char *fname, *custommsg = NULL;
- int min = 0, max = 0;
- if (parser->format) {
- assert(parser->fname == NULL);
- if (parse_format(parser->format, len, pos,
- &fname, &custommsg, &min, &max) < 0) {
- return 0;
- }
- }
- else {
- assert(parser->fname != NULL);
- fname = parser->fname;
- }
- int owned;
- PyObject *kwtuple = parser->kwtuple;
- if (kwtuple == NULL) {
- /* We may temporarily switch to the main interpreter to avoid
- * creating a tuple that could outlive its owning interpreter. */
- PyThreadState *save_tstate = NULL;
- PyThreadState *temp_tstate = NULL;
- if (!_Py_IsMainInterpreter(PyInterpreterState_Get())) {
- temp_tstate = PyThreadState_New(_PyInterpreterState_Main());
- if (temp_tstate == NULL) {
- return -1;
- }
- save_tstate = PyThreadState_Swap(temp_tstate);
- }
- kwtuple = new_kwtuple(keywords, len, pos);
- if (temp_tstate != NULL) {
- PyThreadState_Clear(temp_tstate);
- (void)PyThreadState_Swap(save_tstate);
- PyThreadState_Delete(temp_tstate);
- }
- if (kwtuple == NULL) {
- return 0;
- }
- owned = 1;
- }
- else {
- owned = 0;
- }
- parser->pos = pos;
- parser->fname = fname;
- parser->custom_msg = custommsg;
- parser->min = min;
- parser->max = max;
- parser->kwtuple = kwtuple;
- parser->initialized = owned ? 1 : -1;
- assert(parser->next == NULL);
- parser->next = _PyRuntime.getargs.static_parsers;
- _PyRuntime.getargs.static_parsers = parser;
- return 1;
- }
- static int
- parser_init(struct _PyArg_Parser *parser)
- {
- // volatile as it can be modified by other threads
- // and should not be optimized or reordered by compiler
- if (*((volatile int *)&parser->initialized)) {
- assert(parser->kwtuple != NULL);
- return 1;
- }
- PyThread_acquire_lock(_PyRuntime.getargs.mutex, WAIT_LOCK);
- // Check again if another thread initialized the parser
- // while we were waiting for the lock.
- if (*((volatile int *)&parser->initialized)) {
- assert(parser->kwtuple != NULL);
- PyThread_release_lock(_PyRuntime.getargs.mutex);
- return 1;
- }
- int ret = _parser_init(parser);
- PyThread_release_lock(_PyRuntime.getargs.mutex);
- return ret;
- }
- static void
- parser_clear(struct _PyArg_Parser *parser)
- {
- if (parser->initialized == 1) {
- Py_CLEAR(parser->kwtuple);
- }
- if (parser->format) {
- parser->fname = NULL;
- }
- else {
- assert(parser->fname != NULL);
- }
- parser->custom_msg = NULL;
- parser->pos = 0;
- parser->min = 0;
- parser->max = 0;
- parser->initialized = 0;
- }
- static PyObject*
- find_keyword(PyObject *kwnames, PyObject *const *kwstack, PyObject *key)
- {
- Py_ssize_t i, nkwargs;
- nkwargs = PyTuple_GET_SIZE(kwnames);
- for (i = 0; i < nkwargs; i++) {
- PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
- /* kwname == key will normally find a match in since keyword keys
- should be interned strings; if not retry below in a new loop. */
- if (kwname == key) {
- return kwstack[i];
- }
- }
- for (i = 0; i < nkwargs; i++) {
- PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
- assert(PyUnicode_Check(kwname));
- if (_PyUnicode_EQ(kwname, key)) {
- return kwstack[i];
- }
- }
- return NULL;
- }
- static int
- vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
- PyObject *kwargs, PyObject *kwnames,
- struct _PyArg_Parser *parser,
- va_list *p_va, int flags)
- {
- PyObject *kwtuple;
- char msgbuf[512];
- int levels[32];
- const char *format;
- const char *msg;
- PyObject *keyword;
- int i, pos, len;
- Py_ssize_t nkwargs;
- PyObject *current_arg;
- freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
- freelist_t freelist;
- PyObject *const *kwstack = NULL;
- freelist.entries = static_entries;
- freelist.first_available = 0;
- freelist.entries_malloced = 0;
- assert(kwargs == NULL || PyDict_Check(kwargs));
- assert(kwargs == NULL || kwnames == NULL);
- assert(p_va != NULL);
- if (parser == NULL) {
- PyErr_BadInternalCall();
- return 0;
- }
- if (kwnames != NULL && !PyTuple_Check(kwnames)) {
- PyErr_BadInternalCall();
- return 0;
- }
- if (!parser_init(parser)) {
- return 0;
- }
- kwtuple = parser->kwtuple;
- pos = parser->pos;
- len = pos + (int)PyTuple_GET_SIZE(kwtuple);
- if (len > STATIC_FREELIST_ENTRIES) {
- freelist.entries = PyMem_NEW(freelistentry_t, len);
- if (freelist.entries == NULL) {
- PyErr_NoMemory();
- return 0;
- }
- freelist.entries_malloced = 1;
- }
- if (kwargs != NULL) {
- nkwargs = PyDict_GET_SIZE(kwargs);
- }
- else if (kwnames != NULL) {
- nkwargs = PyTuple_GET_SIZE(kwnames);
- kwstack = args + nargs;
- }
- else {
- nkwargs = 0;
- }
- if (nargs + nkwargs > len) {
- /* Adding "keyword" (when nargs == 0) prevents producing wrong error
- messages in some special cases (see bpo-31229). */
- PyErr_Format(PyExc_TypeError,
- "%.200s%s takes at most %d %sargument%s (%zd given)",
- (parser->fname == NULL) ? "function" : parser->fname,
- (parser->fname == NULL) ? "" : "()",
- len,
- (nargs == 0) ? "keyword " : "",
- (len == 1) ? "" : "s",
- nargs + nkwargs);
- return cleanreturn(0, &freelist);
- }
- if (parser->max < nargs) {
- if (parser->max == 0) {
- PyErr_Format(PyExc_TypeError,
- "%.200s%s takes no positional arguments",
- (parser->fname == NULL) ? "function" : parser->fname,
- (parser->fname == NULL) ? "" : "()");
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "%.200s%s takes %s %d positional argument%s (%zd given)",
- (parser->fname == NULL) ? "function" : parser->fname,
- (parser->fname == NULL) ? "" : "()",
- (parser->min < parser->max) ? "at most" : "exactly",
- parser->max,
- parser->max == 1 ? "" : "s",
- nargs);
- }
- return cleanreturn(0, &freelist);
- }
- format = parser->format;
- assert(format != NULL || len == 0);
- /* convert tuple args and keyword args in same loop, using kwtuple to drive process */
- for (i = 0; i < len; i++) {
- if (*format == '|') {
- format++;
- }
- if (*format == '$') {
- format++;
- }
- assert(!IS_END_OF_FORMAT(*format));
- if (i < nargs) {
- current_arg = args[i];
- }
- else if (nkwargs && i >= pos) {
- keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
- if (kwargs != NULL) {
- current_arg = PyDict_GetItemWithError(kwargs, keyword);
- if (!current_arg && PyErr_Occurred()) {
- return cleanreturn(0, &freelist);
- }
- }
- else {
- current_arg = find_keyword(kwnames, kwstack, keyword);
- }
- if (current_arg) {
- --nkwargs;
- }
- }
- else {
- current_arg = NULL;
- }
- if (current_arg) {
- msg = convertitem(current_arg, &format, p_va, flags,
- levels, msgbuf, sizeof(msgbuf), &freelist);
- if (msg) {
- seterror(i+1, msg, levels, parser->fname, parser->custom_msg);
- return cleanreturn(0, &freelist);
- }
- continue;
- }
- if (i < parser->min) {
- /* Less arguments than required */
- if (i < pos) {
- Py_ssize_t min = Py_MIN(pos, parser->min);
- PyErr_Format(PyExc_TypeError,
- "%.200s%s takes %s %d positional argument%s"
- " (%zd given)",
- (parser->fname == NULL) ? "function" : parser->fname,
- (parser->fname == NULL) ? "" : "()",
- min < parser->max ? "at least" : "exactly",
- min,
- min == 1 ? "" : "s",
- nargs);
- }
- else {
- keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
- PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
- "argument '%U' (pos %d)",
- (parser->fname == NULL) ? "function" : parser->fname,
- (parser->fname == NULL) ? "" : "()",
- keyword, i+1);
- }
- return cleanreturn(0, &freelist);
- }
- /* current code reports success when all required args
- * fulfilled and no keyword args left, with no further
- * validation. XXX Maybe skip this in debug build ?
- */
- if (!nkwargs) {
- return cleanreturn(1, &freelist);
- }
- /* We are into optional args, skip through to any remaining
- * keyword args */
- msg = skipitem(&format, p_va, flags);
- assert(msg == NULL);
- }
- assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$'));
- if (nkwargs > 0) {
- /* make sure there are no arguments given by name and position */
- for (i = pos; i < nargs; i++) {
- keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
- if (kwargs != NULL) {
- current_arg = PyDict_GetItemWithError(kwargs, keyword);
- if (!current_arg && PyErr_Occurred()) {
- return cleanreturn(0, &freelist);
- }
- }
- else {
- current_arg = find_keyword(kwnames, kwstack, keyword);
- }
- if (current_arg) {
- /* arg present in tuple and in dict */
- PyErr_Format(PyExc_TypeError,
- "argument for %.200s%s given by name ('%U') "
- "and position (%d)",
- (parser->fname == NULL) ? "function" : parser->fname,
- (parser->fname == NULL) ? "" : "()",
- keyword, i+1);
- return cleanreturn(0, &freelist);
- }
- }
- error_unexpected_keyword_arg(kwargs, kwnames, kwtuple, parser->fname);
- return cleanreturn(0, &freelist);
- }
- return cleanreturn(1, &freelist);
- }
- static int
- vgetargskeywordsfast(PyObject *args, PyObject *keywords,
- struct _PyArg_Parser *parser, va_list *p_va, int flags)
- {
- PyObject **stack;
- Py_ssize_t nargs;
- if (args == NULL
- || !PyTuple_Check(args)
- || (keywords != NULL && !PyDict_Check(keywords)))
- {
- PyErr_BadInternalCall();
- return 0;
- }
- stack = _PyTuple_ITEMS(args);
- nargs = PyTuple_GET_SIZE(args);
- return vgetargskeywordsfast_impl(stack, nargs, keywords, NULL,
- parser, p_va, flags);
- }
- #undef _PyArg_UnpackKeywords
- PyObject * const *
- _PyArg_UnpackKeywords(PyObject *const *args, Py_ssize_t nargs,
- PyObject *kwargs, PyObject *kwnames,
- struct _PyArg_Parser *parser,
- int minpos, int maxpos, int minkw,
- PyObject **buf)
- {
- PyObject *kwtuple;
- PyObject *keyword;
- int i, posonly, minposonly, maxargs;
- int reqlimit = minkw ? maxpos + minkw : minpos;
- Py_ssize_t nkwargs;
- PyObject *current_arg;
- PyObject * const *kwstack = NULL;
- assert(kwargs == NULL || PyDict_Check(kwargs));
- assert(kwargs == NULL || kwnames == NULL);
- if (parser == NULL) {
- PyErr_BadInternalCall();
- return NULL;
- }
- if (kwnames != NULL && !PyTuple_Check(kwnames)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- if (args == NULL && nargs == 0) {
- args = buf;
- }
- if (!parser_init(parser)) {
- return NULL;
- }
- kwtuple = parser->kwtuple;
- posonly = parser->pos;
- minposonly = Py_MIN(posonly, minpos);
- maxargs = posonly + (int)PyTuple_GET_SIZE(kwtuple);
- if (kwargs != NULL) {
- nkwargs = PyDict_GET_SIZE(kwargs);
- }
- else if (kwnames != NULL) {
- nkwargs = PyTuple_GET_SIZE(kwnames);
- kwstack = args + nargs;
- }
- else {
- nkwargs = 0;
- }
- if (nkwargs == 0 && minkw == 0 && minpos <= nargs && nargs <= maxpos) {
- /* Fast path. */
- return args;
- }
- if (nargs + nkwargs > maxargs) {
- /* Adding "keyword" (when nargs == 0) prevents producing wrong error
- messages in some special cases (see bpo-31229). */
- PyErr_Format(PyExc_TypeError,
- "%.200s%s takes at most %d %sargument%s (%zd given)",
- (parser->fname == NULL) ? "function" : parser->fname,
- (parser->fname == NULL) ? "" : "()",
- maxargs,
- (nargs == 0) ? "keyword " : "",
- (maxargs == 1) ? "" : "s",
- nargs + nkwargs);
- return NULL;
- }
- if (nargs > maxpos) {
- if (maxpos == 0) {
- PyErr_Format(PyExc_TypeError,
- "%.200s%s takes no positional arguments",
- (parser->fname == NULL) ? "function" : parser->fname,
- (parser->fname == NULL) ? "" : "()");
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "%.200s%s takes %s %d positional argument%s (%zd given)",
- (parser->fname == NULL) ? "function" : parser->fname,
- (parser->fname == NULL) ? "" : "()",
- (minpos < maxpos) ? "at most" : "exactly",
- maxpos,
- (maxpos == 1) ? "" : "s",
- nargs);
- }
- return NULL;
- }
- if (nargs < minposonly) {
- PyErr_Format(PyExc_TypeError,
- "%.200s%s takes %s %d positional argument%s"
- " (%zd given)",
- (parser->fname == NULL) ? "function" : parser->fname,
- (parser->fname == NULL) ? "" : "()",
- minposonly < maxpos ? "at least" : "exactly",
- minposonly,
- minposonly == 1 ? "" : "s",
- nargs);
- return NULL;
- }
- /* copy tuple args */
- for (i = 0; i < nargs; i++) {
- buf[i] = args[i];
- }
- /* copy keyword args using kwtuple to drive process */
- for (i = Py_MAX((int)nargs, posonly); i < maxargs; i++) {
- if (nkwargs) {
- keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
- if (kwargs != NULL) {
- current_arg = PyDict_GetItemWithError(kwargs, keyword);
- if (!current_arg && PyErr_Occurred()) {
- return NULL;
- }
- }
- else {
- current_arg = find_keyword(kwnames, kwstack, keyword);
- }
- }
- else if (i >= reqlimit) {
- break;
- }
- else {
- current_arg = NULL;
- }
- buf[i] = current_arg;
- if (current_arg) {
- --nkwargs;
- }
- else if (i < minpos || (maxpos <= i && i < reqlimit)) {
- /* Less arguments than required */
- keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
- PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
- "argument '%U' (pos %d)",
- (parser->fname == NULL) ? "function" : parser->fname,
- (parser->fname == NULL) ? "" : "()",
- keyword, i+1);
- return NULL;
- }
- }
- if (nkwargs > 0) {
- /* make sure there are no arguments given by name and position */
- for (i = posonly; i < nargs; i++) {
- keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
- if (kwargs != NULL) {
- current_arg = PyDict_GetItemWithError(kwargs, keyword);
- if (!current_arg && PyErr_Occurred()) {
- return NULL;
- }
- }
- else {
- current_arg = find_keyword(kwnames, kwstack, keyword);
- }
- if (current_arg) {
- /* arg present in tuple and in dict */
- PyErr_Format(PyExc_TypeError,
- "argument for %.200s%s given by name ('%U') "
- "and position (%d)",
- (parser->fname == NULL) ? "function" : parser->fname,
- (parser->fname == NULL) ? "" : "()",
- keyword, i+1);
- return NULL;
- }
- }
- error_unexpected_keyword_arg(kwargs, kwnames, kwtuple, parser->fname);
- return NULL;
- }
- return buf;
- }
- PyObject * const *
- _PyArg_UnpackKeywordsWithVararg(PyObject *const *args, Py_ssize_t nargs,
- PyObject *kwargs, PyObject *kwnames,
- struct _PyArg_Parser *parser,
- int minpos, int maxpos, int minkw,
- int vararg, PyObject **buf)
- {
- PyObject *kwtuple;
- PyObject *keyword;
- Py_ssize_t varargssize = 0;
- int i, posonly, minposonly, maxargs;
- int reqlimit = minkw ? maxpos + minkw : minpos;
- Py_ssize_t nkwargs;
- PyObject *current_arg;
- PyObject * const *kwstack = NULL;
- assert(kwargs == NULL || PyDict_Check(kwargs));
- assert(kwargs == NULL || kwnames == NULL);
- if (parser == NULL) {
- PyErr_BadInternalCall();
- return NULL;
- }
- if (kwnames != NULL && !PyTuple_Check(kwnames)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- if (args == NULL && nargs == 0) {
- args = buf;
- }
- if (!parser_init(parser)) {
- return NULL;
- }
- kwtuple = parser->kwtuple;
- posonly = parser->pos;
- minposonly = Py_MIN(posonly, minpos);
- maxargs = posonly + (int)PyTuple_GET_SIZE(kwtuple);
- if (kwargs != NULL) {
- nkwargs = PyDict_GET_SIZE(kwargs);
- }
- else if (kwnames != NULL) {
- nkwargs = PyTuple_GET_SIZE(kwnames);
- kwstack = args + nargs;
- }
- else {
- nkwargs = 0;
- }
- if (nargs < minposonly) {
- PyErr_Format(PyExc_TypeError,
- "%.200s%s takes %s %d positional argument%s"
- " (%zd given)",
- (parser->fname == NULL) ? "function" : parser->fname,
- (parser->fname == NULL) ? "" : "()",
- minposonly < maxpos ? "at least" : "exactly",
- minposonly,
- minposonly == 1 ? "" : "s",
- nargs);
- return NULL;
- }
- /* create varargs tuple */
- varargssize = nargs - maxpos;
- if (varargssize < 0) {
- varargssize = 0;
- }
- buf[vararg] = PyTuple_New(varargssize);
- if (!buf[vararg]) {
- return NULL;
- }
- /* copy tuple args */
- for (i = 0; i < nargs; i++) {
- if (i >= vararg) {
- PyTuple_SET_ITEM(buf[vararg], i - vararg, Py_NewRef(args[i]));
- continue;
- }
- else {
- buf[i] = args[i];
- }
- }
- /* copy keyword args using kwtuple to drive process */
- for (i = Py_MAX((int)nargs, posonly) -
- Py_SAFE_DOWNCAST(varargssize, Py_ssize_t, int); i < maxargs; i++) {
- if (nkwargs) {
- keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
- if (kwargs != NULL) {
- current_arg = PyDict_GetItemWithError(kwargs, keyword);
- if (!current_arg && PyErr_Occurred()) {
- goto exit;
- }
- }
- else {
- current_arg = find_keyword(kwnames, kwstack, keyword);
- }
- }
- else {
- current_arg = NULL;
- }
- /* If an arguments is passed in as a keyword argument,
- * it should be placed before `buf[vararg]`.
- *
- * For example:
- * def f(a, /, b, *args):
- * pass
- * f(1, b=2)
- *
- * This `buf` array should be: [1, 2, NULL].
- * In this case, nargs < vararg.
- *
- * Otherwise, we leave a place at `buf[vararg]` for vararg tuple
- * so the index is `i + 1`. */
- if (i < vararg) {
- buf[i] = current_arg;
- }
- else {
- buf[i + 1] = current_arg;
- }
- if (current_arg) {
- --nkwargs;
- }
- else if (i < minpos || (maxpos <= i && i < reqlimit)) {
- /* Less arguments than required */
- keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
- PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
- "argument '%U' (pos %d)",
- (parser->fname == NULL) ? "function" : parser->fname,
- (parser->fname == NULL) ? "" : "()",
- keyword, i+1);
- goto exit;
- }
- }
- if (nkwargs > 0) {
- error_unexpected_keyword_arg(kwargs, kwnames, kwtuple, parser->fname);
- goto exit;
- }
- return buf;
- exit:
- Py_XDECREF(buf[vararg]);
- return NULL;
- }
- static const char *
- skipitem(const char **p_format, va_list *p_va, int flags)
- {
- const char *format = *p_format;
- char c = *format++;
- switch (c) {
- /*
- * codes that take a single data pointer as an argument
- * (the type of the pointer is irrelevant)
- */
- case 'b': /* byte -- very short int */
- case 'B': /* byte as bitfield */
- case 'h': /* short int */
- case 'H': /* short int as bitfield */
- case 'i': /* int */
- case 'I': /* int sized bitfield */
- case 'l': /* long int */
- case 'k': /* long int sized bitfield */
- case 'L': /* long long */
- case 'K': /* long long sized bitfield */
- case 'n': /* Py_ssize_t */
- case 'f': /* float */
- case 'd': /* double */
- case 'D': /* complex double */
- case 'c': /* char */
- case 'C': /* unicode char */
- case 'p': /* boolean predicate */
- case 'S': /* string object */
- case 'Y': /* string object */
- case 'U': /* unicode string object */
- {
- if (p_va != NULL) {
- (void) va_arg(*p_va, void *);
- }
- break;
- }
- /* string codes */
- case 'e': /* string with encoding */
- {
- if (p_va != NULL) {
- (void) va_arg(*p_va, const char *);
- }
- if (!(*format == 's' || *format == 't'))
- /* after 'e', only 's' and 't' is allowed */
- goto err;
- format++;
- }
- /* fall through */
- case 's': /* string */
- case 'z': /* string or None */
- case 'y': /* bytes */
- case 'w': /* buffer, read-write */
- {
- if (p_va != NULL) {
- (void) va_arg(*p_va, char **);
- }
- if (*format == '#') {
- if (p_va != NULL) {
- if (!(flags & FLAG_SIZE_T)) {
- return "PY_SSIZE_T_CLEAN macro must be defined for '#' formats";
- }
- (void) va_arg(*p_va, Py_ssize_t *);
- }
- format++;
- } else if ((c == 's' || c == 'z' || c == 'y' || c == 'w')
- && *format == '*')
- {
- format++;
- }
- break;
- }
- case 'O': /* object */
- {
- if (*format == '!') {
- format++;
- if (p_va != NULL) {
- (void) va_arg(*p_va, PyTypeObject*);
- (void) va_arg(*p_va, PyObject **);
- }
- }
- else if (*format == '&') {
- typedef int (*converter)(PyObject *, void *);
- if (p_va != NULL) {
- (void) va_arg(*p_va, converter);
- (void) va_arg(*p_va, void *);
- }
- format++;
- }
- else {
- if (p_va != NULL) {
- (void) va_arg(*p_va, PyObject **);
- }
- }
- break;
- }
- case '(': /* bypass tuple, not handled at all previously */
- {
- const char *msg;
- for (;;) {
- if (*format==')')
- break;
- if (IS_END_OF_FORMAT(*format))
- return "Unmatched left paren in format "
- "string";
- msg = skipitem(&format, p_va, flags);
- if (msg)
- return msg;
- }
- format++;
- break;
- }
- case ')':
- return "Unmatched right paren in format string";
- default:
- err:
- return "impossible<bad format char>";
- }
- *p_format = format;
- return NULL;
- }
- #undef _PyArg_CheckPositional
- int
- _PyArg_CheckPositional(const char *name, Py_ssize_t nargs,
- Py_ssize_t min, Py_ssize_t max)
- {
- assert(min >= 0);
- assert(min <= max);
- if (nargs < min) {
- if (name != NULL)
- PyErr_Format(
- PyExc_TypeError,
- "%.200s expected %s%zd argument%s, got %zd",
- name, (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs);
- else
- PyErr_Format(
- PyExc_TypeError,
- "unpacked tuple should have %s%zd element%s,"
- " but has %zd",
- (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs);
- return 0;
- }
- if (nargs == 0) {
- return 1;
- }
- if (nargs > max) {
- if (name != NULL)
- PyErr_Format(
- PyExc_TypeError,
- "%.200s expected %s%zd argument%s, got %zd",
- name, (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs);
- else
- PyErr_Format(
- PyExc_TypeError,
- "unpacked tuple should have %s%zd element%s,"
- " but has %zd",
- (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs);
- return 0;
- }
- return 1;
- }
- static int
- unpack_stack(PyObject *const *args, Py_ssize_t nargs, const char *name,
- Py_ssize_t min, Py_ssize_t max, va_list vargs)
- {
- Py_ssize_t i;
- PyObject **o;
- if (!_PyArg_CheckPositional(name, nargs, min, max)) {
- return 0;
- }
- for (i = 0; i < nargs; i++) {
- o = va_arg(vargs, PyObject **);
- *o = args[i];
- }
- return 1;
- }
- int
- PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
- {
- PyObject **stack;
- Py_ssize_t nargs;
- int retval;
- va_list vargs;
- if (!PyTuple_Check(args)) {
- PyErr_SetString(PyExc_SystemError,
- "PyArg_UnpackTuple() argument list is not a tuple");
- return 0;
- }
- stack = _PyTuple_ITEMS(args);
- nargs = PyTuple_GET_SIZE(args);
- va_start(vargs, max);
- retval = unpack_stack(stack, nargs, name, min, max, vargs);
- va_end(vargs);
- return retval;
- }
- int
- _PyArg_UnpackStack(PyObject *const *args, Py_ssize_t nargs, const char *name,
- Py_ssize_t min, Py_ssize_t max, ...)
- {
- int retval;
- va_list vargs;
- va_start(vargs, max);
- retval = unpack_stack(args, nargs, name, min, max, vargs);
- va_end(vargs);
- return retval;
- }
- #undef _PyArg_NoKeywords
- #undef _PyArg_NoKwnames
- #undef _PyArg_NoPositional
- /* For type constructors that don't take keyword args
- *
- * Sets a TypeError and returns 0 if the args/kwargs is
- * not empty, returns 1 otherwise
- */
- int
- _PyArg_NoKeywords(const char *funcname, PyObject *kwargs)
- {
- if (kwargs == NULL) {
- return 1;
- }
- if (!PyDict_CheckExact(kwargs)) {
- PyErr_BadInternalCall();
- return 0;
- }
- if (PyDict_GET_SIZE(kwargs) == 0) {
- return 1;
- }
- PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
- funcname);
- return 0;
- }
- int
- _PyArg_NoPositional(const char *funcname, PyObject *args)
- {
- if (args == NULL)
- return 1;
- if (!PyTuple_CheckExact(args)) {
- PyErr_BadInternalCall();
- return 0;
- }
- if (PyTuple_GET_SIZE(args) == 0)
- return 1;
- PyErr_Format(PyExc_TypeError, "%.200s() takes no positional arguments",
- funcname);
- return 0;
- }
- int
- _PyArg_NoKwnames(const char *funcname, PyObject *kwnames)
- {
- if (kwnames == NULL) {
- return 1;
- }
- assert(PyTuple_CheckExact(kwnames));
- if (PyTuple_GET_SIZE(kwnames) == 0) {
- return 1;
- }
- PyErr_Format(PyExc_TypeError, "%s() takes no keyword arguments", funcname);
- return 0;
- }
- void
- _PyArg_Fini(void)
- {
- struct _PyArg_Parser *tmp, *s = _PyRuntime.getargs.static_parsers;
- while (s) {
- tmp = s->next;
- s->next = NULL;
- parser_clear(s);
- s = tmp;
- }
- _PyRuntime.getargs.static_parsers = NULL;
- }
- #ifdef __cplusplus
- };
- #endif
|