_codecsmodule.c.h 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872
  1. /*[clinic input]
  2. preserve
  3. [clinic start generated code]*/
  4. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  5. # include "pycore_gc.h" // PyGC_Head
  6. # include "pycore_runtime.h" // _Py_ID()
  7. #endif
  8. PyDoc_STRVAR(_codecs_register__doc__,
  9. "register($module, search_function, /)\n"
  10. "--\n"
  11. "\n"
  12. "Register a codec search function.\n"
  13. "\n"
  14. "Search functions are expected to take one argument, the encoding name in\n"
  15. "all lower case letters, and either return None, or a tuple of functions\n"
  16. "(encoder, decoder, stream_reader, stream_writer) (or a CodecInfo object).");
  17. #define _CODECS_REGISTER_METHODDEF \
  18. {"register", (PyCFunction)_codecs_register, METH_O, _codecs_register__doc__},
  19. PyDoc_STRVAR(_codecs_unregister__doc__,
  20. "unregister($module, search_function, /)\n"
  21. "--\n"
  22. "\n"
  23. "Unregister a codec search function and clear the registry\'s cache.\n"
  24. "\n"
  25. "If the search function is not registered, do nothing.");
  26. #define _CODECS_UNREGISTER_METHODDEF \
  27. {"unregister", (PyCFunction)_codecs_unregister, METH_O, _codecs_unregister__doc__},
  28. PyDoc_STRVAR(_codecs_lookup__doc__,
  29. "lookup($module, encoding, /)\n"
  30. "--\n"
  31. "\n"
  32. "Looks up a codec tuple in the Python codec registry and returns a CodecInfo object.");
  33. #define _CODECS_LOOKUP_METHODDEF \
  34. {"lookup", (PyCFunction)_codecs_lookup, METH_O, _codecs_lookup__doc__},
  35. static PyObject *
  36. _codecs_lookup_impl(PyObject *module, const char *encoding);
  37. static PyObject *
  38. _codecs_lookup(PyObject *module, PyObject *arg)
  39. {
  40. PyObject *return_value = NULL;
  41. const char *encoding;
  42. if (!PyUnicode_Check(arg)) {
  43. _PyArg_BadArgument("lookup", "argument", "str", arg);
  44. goto exit;
  45. }
  46. Py_ssize_t encoding_length;
  47. encoding = PyUnicode_AsUTF8AndSize(arg, &encoding_length);
  48. if (encoding == NULL) {
  49. goto exit;
  50. }
  51. if (strlen(encoding) != (size_t)encoding_length) {
  52. PyErr_SetString(PyExc_ValueError, "embedded null character");
  53. goto exit;
  54. }
  55. return_value = _codecs_lookup_impl(module, encoding);
  56. exit:
  57. return return_value;
  58. }
  59. PyDoc_STRVAR(_codecs_encode__doc__,
  60. "encode($module, /, obj, encoding=\'utf-8\', errors=\'strict\')\n"
  61. "--\n"
  62. "\n"
  63. "Encodes obj using the codec registered for encoding.\n"
  64. "\n"
  65. "The default encoding is \'utf-8\'. errors may be given to set a\n"
  66. "different error handling scheme. Default is \'strict\' meaning that encoding\n"
  67. "errors raise a ValueError. Other possible values are \'ignore\', \'replace\'\n"
  68. "and \'backslashreplace\' as well as any other name registered with\n"
  69. "codecs.register_error that can handle ValueErrors.");
  70. #define _CODECS_ENCODE_METHODDEF \
  71. {"encode", _PyCFunction_CAST(_codecs_encode), METH_FASTCALL|METH_KEYWORDS, _codecs_encode__doc__},
  72. static PyObject *
  73. _codecs_encode_impl(PyObject *module, PyObject *obj, const char *encoding,
  74. const char *errors);
  75. static PyObject *
  76. _codecs_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  77. {
  78. PyObject *return_value = NULL;
  79. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  80. #define NUM_KEYWORDS 3
  81. static struct {
  82. PyGC_Head _this_is_not_used;
  83. PyObject_VAR_HEAD
  84. PyObject *ob_item[NUM_KEYWORDS];
  85. } _kwtuple = {
  86. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  87. .ob_item = { &_Py_ID(obj), &_Py_ID(encoding), &_Py_ID(errors), },
  88. };
  89. #undef NUM_KEYWORDS
  90. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  91. #else // !Py_BUILD_CORE
  92. # define KWTUPLE NULL
  93. #endif // !Py_BUILD_CORE
  94. static const char * const _keywords[] = {"obj", "encoding", "errors", NULL};
  95. static _PyArg_Parser _parser = {
  96. .keywords = _keywords,
  97. .fname = "encode",
  98. .kwtuple = KWTUPLE,
  99. };
  100. #undef KWTUPLE
  101. PyObject *argsbuf[3];
  102. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
  103. PyObject *obj;
  104. const char *encoding = NULL;
  105. const char *errors = NULL;
  106. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
  107. if (!args) {
  108. goto exit;
  109. }
  110. obj = args[0];
  111. if (!noptargs) {
  112. goto skip_optional_pos;
  113. }
  114. if (args[1]) {
  115. if (!PyUnicode_Check(args[1])) {
  116. _PyArg_BadArgument("encode", "argument 'encoding'", "str", args[1]);
  117. goto exit;
  118. }
  119. Py_ssize_t encoding_length;
  120. encoding = PyUnicode_AsUTF8AndSize(args[1], &encoding_length);
  121. if (encoding == NULL) {
  122. goto exit;
  123. }
  124. if (strlen(encoding) != (size_t)encoding_length) {
  125. PyErr_SetString(PyExc_ValueError, "embedded null character");
  126. goto exit;
  127. }
  128. if (!--noptargs) {
  129. goto skip_optional_pos;
  130. }
  131. }
  132. if (!PyUnicode_Check(args[2])) {
  133. _PyArg_BadArgument("encode", "argument 'errors'", "str", args[2]);
  134. goto exit;
  135. }
  136. Py_ssize_t errors_length;
  137. errors = PyUnicode_AsUTF8AndSize(args[2], &errors_length);
  138. if (errors == NULL) {
  139. goto exit;
  140. }
  141. if (strlen(errors) != (size_t)errors_length) {
  142. PyErr_SetString(PyExc_ValueError, "embedded null character");
  143. goto exit;
  144. }
  145. skip_optional_pos:
  146. return_value = _codecs_encode_impl(module, obj, encoding, errors);
  147. exit:
  148. return return_value;
  149. }
  150. PyDoc_STRVAR(_codecs_decode__doc__,
  151. "decode($module, /, obj, encoding=\'utf-8\', errors=\'strict\')\n"
  152. "--\n"
  153. "\n"
  154. "Decodes obj using the codec registered for encoding.\n"
  155. "\n"
  156. "Default encoding is \'utf-8\'. errors may be given to set a\n"
  157. "different error handling scheme. Default is \'strict\' meaning that encoding\n"
  158. "errors raise a ValueError. Other possible values are \'ignore\', \'replace\'\n"
  159. "and \'backslashreplace\' as well as any other name registered with\n"
  160. "codecs.register_error that can handle ValueErrors.");
  161. #define _CODECS_DECODE_METHODDEF \
  162. {"decode", _PyCFunction_CAST(_codecs_decode), METH_FASTCALL|METH_KEYWORDS, _codecs_decode__doc__},
  163. static PyObject *
  164. _codecs_decode_impl(PyObject *module, PyObject *obj, const char *encoding,
  165. const char *errors);
  166. static PyObject *
  167. _codecs_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  168. {
  169. PyObject *return_value = NULL;
  170. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  171. #define NUM_KEYWORDS 3
  172. static struct {
  173. PyGC_Head _this_is_not_used;
  174. PyObject_VAR_HEAD
  175. PyObject *ob_item[NUM_KEYWORDS];
  176. } _kwtuple = {
  177. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  178. .ob_item = { &_Py_ID(obj), &_Py_ID(encoding), &_Py_ID(errors), },
  179. };
  180. #undef NUM_KEYWORDS
  181. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  182. #else // !Py_BUILD_CORE
  183. # define KWTUPLE NULL
  184. #endif // !Py_BUILD_CORE
  185. static const char * const _keywords[] = {"obj", "encoding", "errors", NULL};
  186. static _PyArg_Parser _parser = {
  187. .keywords = _keywords,
  188. .fname = "decode",
  189. .kwtuple = KWTUPLE,
  190. };
  191. #undef KWTUPLE
  192. PyObject *argsbuf[3];
  193. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
  194. PyObject *obj;
  195. const char *encoding = NULL;
  196. const char *errors = NULL;
  197. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
  198. if (!args) {
  199. goto exit;
  200. }
  201. obj = args[0];
  202. if (!noptargs) {
  203. goto skip_optional_pos;
  204. }
  205. if (args[1]) {
  206. if (!PyUnicode_Check(args[1])) {
  207. _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[1]);
  208. goto exit;
  209. }
  210. Py_ssize_t encoding_length;
  211. encoding = PyUnicode_AsUTF8AndSize(args[1], &encoding_length);
  212. if (encoding == NULL) {
  213. goto exit;
  214. }
  215. if (strlen(encoding) != (size_t)encoding_length) {
  216. PyErr_SetString(PyExc_ValueError, "embedded null character");
  217. goto exit;
  218. }
  219. if (!--noptargs) {
  220. goto skip_optional_pos;
  221. }
  222. }
  223. if (!PyUnicode_Check(args[2])) {
  224. _PyArg_BadArgument("decode", "argument 'errors'", "str", args[2]);
  225. goto exit;
  226. }
  227. Py_ssize_t errors_length;
  228. errors = PyUnicode_AsUTF8AndSize(args[2], &errors_length);
  229. if (errors == NULL) {
  230. goto exit;
  231. }
  232. if (strlen(errors) != (size_t)errors_length) {
  233. PyErr_SetString(PyExc_ValueError, "embedded null character");
  234. goto exit;
  235. }
  236. skip_optional_pos:
  237. return_value = _codecs_decode_impl(module, obj, encoding, errors);
  238. exit:
  239. return return_value;
  240. }
  241. PyDoc_STRVAR(_codecs_escape_decode__doc__,
  242. "escape_decode($module, data, errors=None, /)\n"
  243. "--\n"
  244. "\n");
  245. #define _CODECS_ESCAPE_DECODE_METHODDEF \
  246. {"escape_decode", _PyCFunction_CAST(_codecs_escape_decode), METH_FASTCALL, _codecs_escape_decode__doc__},
  247. static PyObject *
  248. _codecs_escape_decode_impl(PyObject *module, Py_buffer *data,
  249. const char *errors);
  250. static PyObject *
  251. _codecs_escape_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  252. {
  253. PyObject *return_value = NULL;
  254. Py_buffer data = {NULL, NULL};
  255. const char *errors = NULL;
  256. if (!_PyArg_CheckPositional("escape_decode", nargs, 1, 2)) {
  257. goto exit;
  258. }
  259. if (PyUnicode_Check(args[0])) {
  260. Py_ssize_t len;
  261. const char *ptr = PyUnicode_AsUTF8AndSize(args[0], &len);
  262. if (ptr == NULL) {
  263. goto exit;
  264. }
  265. PyBuffer_FillInfo(&data, args[0], (void *)ptr, len, 1, 0);
  266. }
  267. else { /* any bytes-like object */
  268. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  269. goto exit;
  270. }
  271. if (!PyBuffer_IsContiguous(&data, 'C')) {
  272. _PyArg_BadArgument("escape_decode", "argument 1", "contiguous buffer", args[0]);
  273. goto exit;
  274. }
  275. }
  276. if (nargs < 2) {
  277. goto skip_optional;
  278. }
  279. if (args[1] == Py_None) {
  280. errors = NULL;
  281. }
  282. else if (PyUnicode_Check(args[1])) {
  283. Py_ssize_t errors_length;
  284. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  285. if (errors == NULL) {
  286. goto exit;
  287. }
  288. if (strlen(errors) != (size_t)errors_length) {
  289. PyErr_SetString(PyExc_ValueError, "embedded null character");
  290. goto exit;
  291. }
  292. }
  293. else {
  294. _PyArg_BadArgument("escape_decode", "argument 2", "str or None", args[1]);
  295. goto exit;
  296. }
  297. skip_optional:
  298. return_value = _codecs_escape_decode_impl(module, &data, errors);
  299. exit:
  300. /* Cleanup for data */
  301. if (data.obj) {
  302. PyBuffer_Release(&data);
  303. }
  304. return return_value;
  305. }
  306. PyDoc_STRVAR(_codecs_escape_encode__doc__,
  307. "escape_encode($module, data, errors=None, /)\n"
  308. "--\n"
  309. "\n");
  310. #define _CODECS_ESCAPE_ENCODE_METHODDEF \
  311. {"escape_encode", _PyCFunction_CAST(_codecs_escape_encode), METH_FASTCALL, _codecs_escape_encode__doc__},
  312. static PyObject *
  313. _codecs_escape_encode_impl(PyObject *module, PyObject *data,
  314. const char *errors);
  315. static PyObject *
  316. _codecs_escape_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  317. {
  318. PyObject *return_value = NULL;
  319. PyObject *data;
  320. const char *errors = NULL;
  321. if (!_PyArg_CheckPositional("escape_encode", nargs, 1, 2)) {
  322. goto exit;
  323. }
  324. if (!PyBytes_Check(args[0])) {
  325. _PyArg_BadArgument("escape_encode", "argument 1", "bytes", args[0]);
  326. goto exit;
  327. }
  328. data = args[0];
  329. if (nargs < 2) {
  330. goto skip_optional;
  331. }
  332. if (args[1] == Py_None) {
  333. errors = NULL;
  334. }
  335. else if (PyUnicode_Check(args[1])) {
  336. Py_ssize_t errors_length;
  337. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  338. if (errors == NULL) {
  339. goto exit;
  340. }
  341. if (strlen(errors) != (size_t)errors_length) {
  342. PyErr_SetString(PyExc_ValueError, "embedded null character");
  343. goto exit;
  344. }
  345. }
  346. else {
  347. _PyArg_BadArgument("escape_encode", "argument 2", "str or None", args[1]);
  348. goto exit;
  349. }
  350. skip_optional:
  351. return_value = _codecs_escape_encode_impl(module, data, errors);
  352. exit:
  353. return return_value;
  354. }
  355. PyDoc_STRVAR(_codecs_utf_7_decode__doc__,
  356. "utf_7_decode($module, data, errors=None, final=False, /)\n"
  357. "--\n"
  358. "\n");
  359. #define _CODECS_UTF_7_DECODE_METHODDEF \
  360. {"utf_7_decode", _PyCFunction_CAST(_codecs_utf_7_decode), METH_FASTCALL, _codecs_utf_7_decode__doc__},
  361. static PyObject *
  362. _codecs_utf_7_decode_impl(PyObject *module, Py_buffer *data,
  363. const char *errors, int final);
  364. static PyObject *
  365. _codecs_utf_7_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  366. {
  367. PyObject *return_value = NULL;
  368. Py_buffer data = {NULL, NULL};
  369. const char *errors = NULL;
  370. int final = 0;
  371. if (!_PyArg_CheckPositional("utf_7_decode", nargs, 1, 3)) {
  372. goto exit;
  373. }
  374. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  375. goto exit;
  376. }
  377. if (!PyBuffer_IsContiguous(&data, 'C')) {
  378. _PyArg_BadArgument("utf_7_decode", "argument 1", "contiguous buffer", args[0]);
  379. goto exit;
  380. }
  381. if (nargs < 2) {
  382. goto skip_optional;
  383. }
  384. if (args[1] == Py_None) {
  385. errors = NULL;
  386. }
  387. else if (PyUnicode_Check(args[1])) {
  388. Py_ssize_t errors_length;
  389. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  390. if (errors == NULL) {
  391. goto exit;
  392. }
  393. if (strlen(errors) != (size_t)errors_length) {
  394. PyErr_SetString(PyExc_ValueError, "embedded null character");
  395. goto exit;
  396. }
  397. }
  398. else {
  399. _PyArg_BadArgument("utf_7_decode", "argument 2", "str or None", args[1]);
  400. goto exit;
  401. }
  402. if (nargs < 3) {
  403. goto skip_optional;
  404. }
  405. final = PyObject_IsTrue(args[2]);
  406. if (final < 0) {
  407. goto exit;
  408. }
  409. skip_optional:
  410. return_value = _codecs_utf_7_decode_impl(module, &data, errors, final);
  411. exit:
  412. /* Cleanup for data */
  413. if (data.obj) {
  414. PyBuffer_Release(&data);
  415. }
  416. return return_value;
  417. }
  418. PyDoc_STRVAR(_codecs_utf_8_decode__doc__,
  419. "utf_8_decode($module, data, errors=None, final=False, /)\n"
  420. "--\n"
  421. "\n");
  422. #define _CODECS_UTF_8_DECODE_METHODDEF \
  423. {"utf_8_decode", _PyCFunction_CAST(_codecs_utf_8_decode), METH_FASTCALL, _codecs_utf_8_decode__doc__},
  424. static PyObject *
  425. _codecs_utf_8_decode_impl(PyObject *module, Py_buffer *data,
  426. const char *errors, int final);
  427. static PyObject *
  428. _codecs_utf_8_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  429. {
  430. PyObject *return_value = NULL;
  431. Py_buffer data = {NULL, NULL};
  432. const char *errors = NULL;
  433. int final = 0;
  434. if (!_PyArg_CheckPositional("utf_8_decode", nargs, 1, 3)) {
  435. goto exit;
  436. }
  437. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  438. goto exit;
  439. }
  440. if (!PyBuffer_IsContiguous(&data, 'C')) {
  441. _PyArg_BadArgument("utf_8_decode", "argument 1", "contiguous buffer", args[0]);
  442. goto exit;
  443. }
  444. if (nargs < 2) {
  445. goto skip_optional;
  446. }
  447. if (args[1] == Py_None) {
  448. errors = NULL;
  449. }
  450. else if (PyUnicode_Check(args[1])) {
  451. Py_ssize_t errors_length;
  452. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  453. if (errors == NULL) {
  454. goto exit;
  455. }
  456. if (strlen(errors) != (size_t)errors_length) {
  457. PyErr_SetString(PyExc_ValueError, "embedded null character");
  458. goto exit;
  459. }
  460. }
  461. else {
  462. _PyArg_BadArgument("utf_8_decode", "argument 2", "str or None", args[1]);
  463. goto exit;
  464. }
  465. if (nargs < 3) {
  466. goto skip_optional;
  467. }
  468. final = PyObject_IsTrue(args[2]);
  469. if (final < 0) {
  470. goto exit;
  471. }
  472. skip_optional:
  473. return_value = _codecs_utf_8_decode_impl(module, &data, errors, final);
  474. exit:
  475. /* Cleanup for data */
  476. if (data.obj) {
  477. PyBuffer_Release(&data);
  478. }
  479. return return_value;
  480. }
  481. PyDoc_STRVAR(_codecs_utf_16_decode__doc__,
  482. "utf_16_decode($module, data, errors=None, final=False, /)\n"
  483. "--\n"
  484. "\n");
  485. #define _CODECS_UTF_16_DECODE_METHODDEF \
  486. {"utf_16_decode", _PyCFunction_CAST(_codecs_utf_16_decode), METH_FASTCALL, _codecs_utf_16_decode__doc__},
  487. static PyObject *
  488. _codecs_utf_16_decode_impl(PyObject *module, Py_buffer *data,
  489. const char *errors, int final);
  490. static PyObject *
  491. _codecs_utf_16_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  492. {
  493. PyObject *return_value = NULL;
  494. Py_buffer data = {NULL, NULL};
  495. const char *errors = NULL;
  496. int final = 0;
  497. if (!_PyArg_CheckPositional("utf_16_decode", nargs, 1, 3)) {
  498. goto exit;
  499. }
  500. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  501. goto exit;
  502. }
  503. if (!PyBuffer_IsContiguous(&data, 'C')) {
  504. _PyArg_BadArgument("utf_16_decode", "argument 1", "contiguous buffer", args[0]);
  505. goto exit;
  506. }
  507. if (nargs < 2) {
  508. goto skip_optional;
  509. }
  510. if (args[1] == Py_None) {
  511. errors = NULL;
  512. }
  513. else if (PyUnicode_Check(args[1])) {
  514. Py_ssize_t errors_length;
  515. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  516. if (errors == NULL) {
  517. goto exit;
  518. }
  519. if (strlen(errors) != (size_t)errors_length) {
  520. PyErr_SetString(PyExc_ValueError, "embedded null character");
  521. goto exit;
  522. }
  523. }
  524. else {
  525. _PyArg_BadArgument("utf_16_decode", "argument 2", "str or None", args[1]);
  526. goto exit;
  527. }
  528. if (nargs < 3) {
  529. goto skip_optional;
  530. }
  531. final = PyObject_IsTrue(args[2]);
  532. if (final < 0) {
  533. goto exit;
  534. }
  535. skip_optional:
  536. return_value = _codecs_utf_16_decode_impl(module, &data, errors, final);
  537. exit:
  538. /* Cleanup for data */
  539. if (data.obj) {
  540. PyBuffer_Release(&data);
  541. }
  542. return return_value;
  543. }
  544. PyDoc_STRVAR(_codecs_utf_16_le_decode__doc__,
  545. "utf_16_le_decode($module, data, errors=None, final=False, /)\n"
  546. "--\n"
  547. "\n");
  548. #define _CODECS_UTF_16_LE_DECODE_METHODDEF \
  549. {"utf_16_le_decode", _PyCFunction_CAST(_codecs_utf_16_le_decode), METH_FASTCALL, _codecs_utf_16_le_decode__doc__},
  550. static PyObject *
  551. _codecs_utf_16_le_decode_impl(PyObject *module, Py_buffer *data,
  552. const char *errors, int final);
  553. static PyObject *
  554. _codecs_utf_16_le_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  555. {
  556. PyObject *return_value = NULL;
  557. Py_buffer data = {NULL, NULL};
  558. const char *errors = NULL;
  559. int final = 0;
  560. if (!_PyArg_CheckPositional("utf_16_le_decode", nargs, 1, 3)) {
  561. goto exit;
  562. }
  563. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  564. goto exit;
  565. }
  566. if (!PyBuffer_IsContiguous(&data, 'C')) {
  567. _PyArg_BadArgument("utf_16_le_decode", "argument 1", "contiguous buffer", args[0]);
  568. goto exit;
  569. }
  570. if (nargs < 2) {
  571. goto skip_optional;
  572. }
  573. if (args[1] == Py_None) {
  574. errors = NULL;
  575. }
  576. else if (PyUnicode_Check(args[1])) {
  577. Py_ssize_t errors_length;
  578. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  579. if (errors == NULL) {
  580. goto exit;
  581. }
  582. if (strlen(errors) != (size_t)errors_length) {
  583. PyErr_SetString(PyExc_ValueError, "embedded null character");
  584. goto exit;
  585. }
  586. }
  587. else {
  588. _PyArg_BadArgument("utf_16_le_decode", "argument 2", "str or None", args[1]);
  589. goto exit;
  590. }
  591. if (nargs < 3) {
  592. goto skip_optional;
  593. }
  594. final = PyObject_IsTrue(args[2]);
  595. if (final < 0) {
  596. goto exit;
  597. }
  598. skip_optional:
  599. return_value = _codecs_utf_16_le_decode_impl(module, &data, errors, final);
  600. exit:
  601. /* Cleanup for data */
  602. if (data.obj) {
  603. PyBuffer_Release(&data);
  604. }
  605. return return_value;
  606. }
  607. PyDoc_STRVAR(_codecs_utf_16_be_decode__doc__,
  608. "utf_16_be_decode($module, data, errors=None, final=False, /)\n"
  609. "--\n"
  610. "\n");
  611. #define _CODECS_UTF_16_BE_DECODE_METHODDEF \
  612. {"utf_16_be_decode", _PyCFunction_CAST(_codecs_utf_16_be_decode), METH_FASTCALL, _codecs_utf_16_be_decode__doc__},
  613. static PyObject *
  614. _codecs_utf_16_be_decode_impl(PyObject *module, Py_buffer *data,
  615. const char *errors, int final);
  616. static PyObject *
  617. _codecs_utf_16_be_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  618. {
  619. PyObject *return_value = NULL;
  620. Py_buffer data = {NULL, NULL};
  621. const char *errors = NULL;
  622. int final = 0;
  623. if (!_PyArg_CheckPositional("utf_16_be_decode", nargs, 1, 3)) {
  624. goto exit;
  625. }
  626. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  627. goto exit;
  628. }
  629. if (!PyBuffer_IsContiguous(&data, 'C')) {
  630. _PyArg_BadArgument("utf_16_be_decode", "argument 1", "contiguous buffer", args[0]);
  631. goto exit;
  632. }
  633. if (nargs < 2) {
  634. goto skip_optional;
  635. }
  636. if (args[1] == Py_None) {
  637. errors = NULL;
  638. }
  639. else if (PyUnicode_Check(args[1])) {
  640. Py_ssize_t errors_length;
  641. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  642. if (errors == NULL) {
  643. goto exit;
  644. }
  645. if (strlen(errors) != (size_t)errors_length) {
  646. PyErr_SetString(PyExc_ValueError, "embedded null character");
  647. goto exit;
  648. }
  649. }
  650. else {
  651. _PyArg_BadArgument("utf_16_be_decode", "argument 2", "str or None", args[1]);
  652. goto exit;
  653. }
  654. if (nargs < 3) {
  655. goto skip_optional;
  656. }
  657. final = PyObject_IsTrue(args[2]);
  658. if (final < 0) {
  659. goto exit;
  660. }
  661. skip_optional:
  662. return_value = _codecs_utf_16_be_decode_impl(module, &data, errors, final);
  663. exit:
  664. /* Cleanup for data */
  665. if (data.obj) {
  666. PyBuffer_Release(&data);
  667. }
  668. return return_value;
  669. }
  670. PyDoc_STRVAR(_codecs_utf_16_ex_decode__doc__,
  671. "utf_16_ex_decode($module, data, errors=None, byteorder=0, final=False,\n"
  672. " /)\n"
  673. "--\n"
  674. "\n");
  675. #define _CODECS_UTF_16_EX_DECODE_METHODDEF \
  676. {"utf_16_ex_decode", _PyCFunction_CAST(_codecs_utf_16_ex_decode), METH_FASTCALL, _codecs_utf_16_ex_decode__doc__},
  677. static PyObject *
  678. _codecs_utf_16_ex_decode_impl(PyObject *module, Py_buffer *data,
  679. const char *errors, int byteorder, int final);
  680. static PyObject *
  681. _codecs_utf_16_ex_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  682. {
  683. PyObject *return_value = NULL;
  684. Py_buffer data = {NULL, NULL};
  685. const char *errors = NULL;
  686. int byteorder = 0;
  687. int final = 0;
  688. if (!_PyArg_CheckPositional("utf_16_ex_decode", nargs, 1, 4)) {
  689. goto exit;
  690. }
  691. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  692. goto exit;
  693. }
  694. if (!PyBuffer_IsContiguous(&data, 'C')) {
  695. _PyArg_BadArgument("utf_16_ex_decode", "argument 1", "contiguous buffer", args[0]);
  696. goto exit;
  697. }
  698. if (nargs < 2) {
  699. goto skip_optional;
  700. }
  701. if (args[1] == Py_None) {
  702. errors = NULL;
  703. }
  704. else if (PyUnicode_Check(args[1])) {
  705. Py_ssize_t errors_length;
  706. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  707. if (errors == NULL) {
  708. goto exit;
  709. }
  710. if (strlen(errors) != (size_t)errors_length) {
  711. PyErr_SetString(PyExc_ValueError, "embedded null character");
  712. goto exit;
  713. }
  714. }
  715. else {
  716. _PyArg_BadArgument("utf_16_ex_decode", "argument 2", "str or None", args[1]);
  717. goto exit;
  718. }
  719. if (nargs < 3) {
  720. goto skip_optional;
  721. }
  722. byteorder = _PyLong_AsInt(args[2]);
  723. if (byteorder == -1 && PyErr_Occurred()) {
  724. goto exit;
  725. }
  726. if (nargs < 4) {
  727. goto skip_optional;
  728. }
  729. final = PyObject_IsTrue(args[3]);
  730. if (final < 0) {
  731. goto exit;
  732. }
  733. skip_optional:
  734. return_value = _codecs_utf_16_ex_decode_impl(module, &data, errors, byteorder, final);
  735. exit:
  736. /* Cleanup for data */
  737. if (data.obj) {
  738. PyBuffer_Release(&data);
  739. }
  740. return return_value;
  741. }
  742. PyDoc_STRVAR(_codecs_utf_32_decode__doc__,
  743. "utf_32_decode($module, data, errors=None, final=False, /)\n"
  744. "--\n"
  745. "\n");
  746. #define _CODECS_UTF_32_DECODE_METHODDEF \
  747. {"utf_32_decode", _PyCFunction_CAST(_codecs_utf_32_decode), METH_FASTCALL, _codecs_utf_32_decode__doc__},
  748. static PyObject *
  749. _codecs_utf_32_decode_impl(PyObject *module, Py_buffer *data,
  750. const char *errors, int final);
  751. static PyObject *
  752. _codecs_utf_32_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  753. {
  754. PyObject *return_value = NULL;
  755. Py_buffer data = {NULL, NULL};
  756. const char *errors = NULL;
  757. int final = 0;
  758. if (!_PyArg_CheckPositional("utf_32_decode", nargs, 1, 3)) {
  759. goto exit;
  760. }
  761. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  762. goto exit;
  763. }
  764. if (!PyBuffer_IsContiguous(&data, 'C')) {
  765. _PyArg_BadArgument("utf_32_decode", "argument 1", "contiguous buffer", args[0]);
  766. goto exit;
  767. }
  768. if (nargs < 2) {
  769. goto skip_optional;
  770. }
  771. if (args[1] == Py_None) {
  772. errors = NULL;
  773. }
  774. else if (PyUnicode_Check(args[1])) {
  775. Py_ssize_t errors_length;
  776. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  777. if (errors == NULL) {
  778. goto exit;
  779. }
  780. if (strlen(errors) != (size_t)errors_length) {
  781. PyErr_SetString(PyExc_ValueError, "embedded null character");
  782. goto exit;
  783. }
  784. }
  785. else {
  786. _PyArg_BadArgument("utf_32_decode", "argument 2", "str or None", args[1]);
  787. goto exit;
  788. }
  789. if (nargs < 3) {
  790. goto skip_optional;
  791. }
  792. final = PyObject_IsTrue(args[2]);
  793. if (final < 0) {
  794. goto exit;
  795. }
  796. skip_optional:
  797. return_value = _codecs_utf_32_decode_impl(module, &data, errors, final);
  798. exit:
  799. /* Cleanup for data */
  800. if (data.obj) {
  801. PyBuffer_Release(&data);
  802. }
  803. return return_value;
  804. }
  805. PyDoc_STRVAR(_codecs_utf_32_le_decode__doc__,
  806. "utf_32_le_decode($module, data, errors=None, final=False, /)\n"
  807. "--\n"
  808. "\n");
  809. #define _CODECS_UTF_32_LE_DECODE_METHODDEF \
  810. {"utf_32_le_decode", _PyCFunction_CAST(_codecs_utf_32_le_decode), METH_FASTCALL, _codecs_utf_32_le_decode__doc__},
  811. static PyObject *
  812. _codecs_utf_32_le_decode_impl(PyObject *module, Py_buffer *data,
  813. const char *errors, int final);
  814. static PyObject *
  815. _codecs_utf_32_le_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  816. {
  817. PyObject *return_value = NULL;
  818. Py_buffer data = {NULL, NULL};
  819. const char *errors = NULL;
  820. int final = 0;
  821. if (!_PyArg_CheckPositional("utf_32_le_decode", nargs, 1, 3)) {
  822. goto exit;
  823. }
  824. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  825. goto exit;
  826. }
  827. if (!PyBuffer_IsContiguous(&data, 'C')) {
  828. _PyArg_BadArgument("utf_32_le_decode", "argument 1", "contiguous buffer", args[0]);
  829. goto exit;
  830. }
  831. if (nargs < 2) {
  832. goto skip_optional;
  833. }
  834. if (args[1] == Py_None) {
  835. errors = NULL;
  836. }
  837. else if (PyUnicode_Check(args[1])) {
  838. Py_ssize_t errors_length;
  839. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  840. if (errors == NULL) {
  841. goto exit;
  842. }
  843. if (strlen(errors) != (size_t)errors_length) {
  844. PyErr_SetString(PyExc_ValueError, "embedded null character");
  845. goto exit;
  846. }
  847. }
  848. else {
  849. _PyArg_BadArgument("utf_32_le_decode", "argument 2", "str or None", args[1]);
  850. goto exit;
  851. }
  852. if (nargs < 3) {
  853. goto skip_optional;
  854. }
  855. final = PyObject_IsTrue(args[2]);
  856. if (final < 0) {
  857. goto exit;
  858. }
  859. skip_optional:
  860. return_value = _codecs_utf_32_le_decode_impl(module, &data, errors, final);
  861. exit:
  862. /* Cleanup for data */
  863. if (data.obj) {
  864. PyBuffer_Release(&data);
  865. }
  866. return return_value;
  867. }
  868. PyDoc_STRVAR(_codecs_utf_32_be_decode__doc__,
  869. "utf_32_be_decode($module, data, errors=None, final=False, /)\n"
  870. "--\n"
  871. "\n");
  872. #define _CODECS_UTF_32_BE_DECODE_METHODDEF \
  873. {"utf_32_be_decode", _PyCFunction_CAST(_codecs_utf_32_be_decode), METH_FASTCALL, _codecs_utf_32_be_decode__doc__},
  874. static PyObject *
  875. _codecs_utf_32_be_decode_impl(PyObject *module, Py_buffer *data,
  876. const char *errors, int final);
  877. static PyObject *
  878. _codecs_utf_32_be_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  879. {
  880. PyObject *return_value = NULL;
  881. Py_buffer data = {NULL, NULL};
  882. const char *errors = NULL;
  883. int final = 0;
  884. if (!_PyArg_CheckPositional("utf_32_be_decode", nargs, 1, 3)) {
  885. goto exit;
  886. }
  887. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  888. goto exit;
  889. }
  890. if (!PyBuffer_IsContiguous(&data, 'C')) {
  891. _PyArg_BadArgument("utf_32_be_decode", "argument 1", "contiguous buffer", args[0]);
  892. goto exit;
  893. }
  894. if (nargs < 2) {
  895. goto skip_optional;
  896. }
  897. if (args[1] == Py_None) {
  898. errors = NULL;
  899. }
  900. else if (PyUnicode_Check(args[1])) {
  901. Py_ssize_t errors_length;
  902. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  903. if (errors == NULL) {
  904. goto exit;
  905. }
  906. if (strlen(errors) != (size_t)errors_length) {
  907. PyErr_SetString(PyExc_ValueError, "embedded null character");
  908. goto exit;
  909. }
  910. }
  911. else {
  912. _PyArg_BadArgument("utf_32_be_decode", "argument 2", "str or None", args[1]);
  913. goto exit;
  914. }
  915. if (nargs < 3) {
  916. goto skip_optional;
  917. }
  918. final = PyObject_IsTrue(args[2]);
  919. if (final < 0) {
  920. goto exit;
  921. }
  922. skip_optional:
  923. return_value = _codecs_utf_32_be_decode_impl(module, &data, errors, final);
  924. exit:
  925. /* Cleanup for data */
  926. if (data.obj) {
  927. PyBuffer_Release(&data);
  928. }
  929. return return_value;
  930. }
  931. PyDoc_STRVAR(_codecs_utf_32_ex_decode__doc__,
  932. "utf_32_ex_decode($module, data, errors=None, byteorder=0, final=False,\n"
  933. " /)\n"
  934. "--\n"
  935. "\n");
  936. #define _CODECS_UTF_32_EX_DECODE_METHODDEF \
  937. {"utf_32_ex_decode", _PyCFunction_CAST(_codecs_utf_32_ex_decode), METH_FASTCALL, _codecs_utf_32_ex_decode__doc__},
  938. static PyObject *
  939. _codecs_utf_32_ex_decode_impl(PyObject *module, Py_buffer *data,
  940. const char *errors, int byteorder, int final);
  941. static PyObject *
  942. _codecs_utf_32_ex_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  943. {
  944. PyObject *return_value = NULL;
  945. Py_buffer data = {NULL, NULL};
  946. const char *errors = NULL;
  947. int byteorder = 0;
  948. int final = 0;
  949. if (!_PyArg_CheckPositional("utf_32_ex_decode", nargs, 1, 4)) {
  950. goto exit;
  951. }
  952. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  953. goto exit;
  954. }
  955. if (!PyBuffer_IsContiguous(&data, 'C')) {
  956. _PyArg_BadArgument("utf_32_ex_decode", "argument 1", "contiguous buffer", args[0]);
  957. goto exit;
  958. }
  959. if (nargs < 2) {
  960. goto skip_optional;
  961. }
  962. if (args[1] == Py_None) {
  963. errors = NULL;
  964. }
  965. else if (PyUnicode_Check(args[1])) {
  966. Py_ssize_t errors_length;
  967. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  968. if (errors == NULL) {
  969. goto exit;
  970. }
  971. if (strlen(errors) != (size_t)errors_length) {
  972. PyErr_SetString(PyExc_ValueError, "embedded null character");
  973. goto exit;
  974. }
  975. }
  976. else {
  977. _PyArg_BadArgument("utf_32_ex_decode", "argument 2", "str or None", args[1]);
  978. goto exit;
  979. }
  980. if (nargs < 3) {
  981. goto skip_optional;
  982. }
  983. byteorder = _PyLong_AsInt(args[2]);
  984. if (byteorder == -1 && PyErr_Occurred()) {
  985. goto exit;
  986. }
  987. if (nargs < 4) {
  988. goto skip_optional;
  989. }
  990. final = PyObject_IsTrue(args[3]);
  991. if (final < 0) {
  992. goto exit;
  993. }
  994. skip_optional:
  995. return_value = _codecs_utf_32_ex_decode_impl(module, &data, errors, byteorder, final);
  996. exit:
  997. /* Cleanup for data */
  998. if (data.obj) {
  999. PyBuffer_Release(&data);
  1000. }
  1001. return return_value;
  1002. }
  1003. PyDoc_STRVAR(_codecs_unicode_escape_decode__doc__,
  1004. "unicode_escape_decode($module, data, errors=None, final=True, /)\n"
  1005. "--\n"
  1006. "\n");
  1007. #define _CODECS_UNICODE_ESCAPE_DECODE_METHODDEF \
  1008. {"unicode_escape_decode", _PyCFunction_CAST(_codecs_unicode_escape_decode), METH_FASTCALL, _codecs_unicode_escape_decode__doc__},
  1009. static PyObject *
  1010. _codecs_unicode_escape_decode_impl(PyObject *module, Py_buffer *data,
  1011. const char *errors, int final);
  1012. static PyObject *
  1013. _codecs_unicode_escape_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  1014. {
  1015. PyObject *return_value = NULL;
  1016. Py_buffer data = {NULL, NULL};
  1017. const char *errors = NULL;
  1018. int final = 1;
  1019. if (!_PyArg_CheckPositional("unicode_escape_decode", nargs, 1, 3)) {
  1020. goto exit;
  1021. }
  1022. if (PyUnicode_Check(args[0])) {
  1023. Py_ssize_t len;
  1024. const char *ptr = PyUnicode_AsUTF8AndSize(args[0], &len);
  1025. if (ptr == NULL) {
  1026. goto exit;
  1027. }
  1028. PyBuffer_FillInfo(&data, args[0], (void *)ptr, len, 1, 0);
  1029. }
  1030. else { /* any bytes-like object */
  1031. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  1032. goto exit;
  1033. }
  1034. if (!PyBuffer_IsContiguous(&data, 'C')) {
  1035. _PyArg_BadArgument("unicode_escape_decode", "argument 1", "contiguous buffer", args[0]);
  1036. goto exit;
  1037. }
  1038. }
  1039. if (nargs < 2) {
  1040. goto skip_optional;
  1041. }
  1042. if (args[1] == Py_None) {
  1043. errors = NULL;
  1044. }
  1045. else if (PyUnicode_Check(args[1])) {
  1046. Py_ssize_t errors_length;
  1047. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  1048. if (errors == NULL) {
  1049. goto exit;
  1050. }
  1051. if (strlen(errors) != (size_t)errors_length) {
  1052. PyErr_SetString(PyExc_ValueError, "embedded null character");
  1053. goto exit;
  1054. }
  1055. }
  1056. else {
  1057. _PyArg_BadArgument("unicode_escape_decode", "argument 2", "str or None", args[1]);
  1058. goto exit;
  1059. }
  1060. if (nargs < 3) {
  1061. goto skip_optional;
  1062. }
  1063. final = PyObject_IsTrue(args[2]);
  1064. if (final < 0) {
  1065. goto exit;
  1066. }
  1067. skip_optional:
  1068. return_value = _codecs_unicode_escape_decode_impl(module, &data, errors, final);
  1069. exit:
  1070. /* Cleanup for data */
  1071. if (data.obj) {
  1072. PyBuffer_Release(&data);
  1073. }
  1074. return return_value;
  1075. }
  1076. PyDoc_STRVAR(_codecs_raw_unicode_escape_decode__doc__,
  1077. "raw_unicode_escape_decode($module, data, errors=None, final=True, /)\n"
  1078. "--\n"
  1079. "\n");
  1080. #define _CODECS_RAW_UNICODE_ESCAPE_DECODE_METHODDEF \
  1081. {"raw_unicode_escape_decode", _PyCFunction_CAST(_codecs_raw_unicode_escape_decode), METH_FASTCALL, _codecs_raw_unicode_escape_decode__doc__},
  1082. static PyObject *
  1083. _codecs_raw_unicode_escape_decode_impl(PyObject *module, Py_buffer *data,
  1084. const char *errors, int final);
  1085. static PyObject *
  1086. _codecs_raw_unicode_escape_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  1087. {
  1088. PyObject *return_value = NULL;
  1089. Py_buffer data = {NULL, NULL};
  1090. const char *errors = NULL;
  1091. int final = 1;
  1092. if (!_PyArg_CheckPositional("raw_unicode_escape_decode", nargs, 1, 3)) {
  1093. goto exit;
  1094. }
  1095. if (PyUnicode_Check(args[0])) {
  1096. Py_ssize_t len;
  1097. const char *ptr = PyUnicode_AsUTF8AndSize(args[0], &len);
  1098. if (ptr == NULL) {
  1099. goto exit;
  1100. }
  1101. PyBuffer_FillInfo(&data, args[0], (void *)ptr, len, 1, 0);
  1102. }
  1103. else { /* any bytes-like object */
  1104. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  1105. goto exit;
  1106. }
  1107. if (!PyBuffer_IsContiguous(&data, 'C')) {
  1108. _PyArg_BadArgument("raw_unicode_escape_decode", "argument 1", "contiguous buffer", args[0]);
  1109. goto exit;
  1110. }
  1111. }
  1112. if (nargs < 2) {
  1113. goto skip_optional;
  1114. }
  1115. if (args[1] == Py_None) {
  1116. errors = NULL;
  1117. }
  1118. else if (PyUnicode_Check(args[1])) {
  1119. Py_ssize_t errors_length;
  1120. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  1121. if (errors == NULL) {
  1122. goto exit;
  1123. }
  1124. if (strlen(errors) != (size_t)errors_length) {
  1125. PyErr_SetString(PyExc_ValueError, "embedded null character");
  1126. goto exit;
  1127. }
  1128. }
  1129. else {
  1130. _PyArg_BadArgument("raw_unicode_escape_decode", "argument 2", "str or None", args[1]);
  1131. goto exit;
  1132. }
  1133. if (nargs < 3) {
  1134. goto skip_optional;
  1135. }
  1136. final = PyObject_IsTrue(args[2]);
  1137. if (final < 0) {
  1138. goto exit;
  1139. }
  1140. skip_optional:
  1141. return_value = _codecs_raw_unicode_escape_decode_impl(module, &data, errors, final);
  1142. exit:
  1143. /* Cleanup for data */
  1144. if (data.obj) {
  1145. PyBuffer_Release(&data);
  1146. }
  1147. return return_value;
  1148. }
  1149. PyDoc_STRVAR(_codecs_latin_1_decode__doc__,
  1150. "latin_1_decode($module, data, errors=None, /)\n"
  1151. "--\n"
  1152. "\n");
  1153. #define _CODECS_LATIN_1_DECODE_METHODDEF \
  1154. {"latin_1_decode", _PyCFunction_CAST(_codecs_latin_1_decode), METH_FASTCALL, _codecs_latin_1_decode__doc__},
  1155. static PyObject *
  1156. _codecs_latin_1_decode_impl(PyObject *module, Py_buffer *data,
  1157. const char *errors);
  1158. static PyObject *
  1159. _codecs_latin_1_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  1160. {
  1161. PyObject *return_value = NULL;
  1162. Py_buffer data = {NULL, NULL};
  1163. const char *errors = NULL;
  1164. if (!_PyArg_CheckPositional("latin_1_decode", nargs, 1, 2)) {
  1165. goto exit;
  1166. }
  1167. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  1168. goto exit;
  1169. }
  1170. if (!PyBuffer_IsContiguous(&data, 'C')) {
  1171. _PyArg_BadArgument("latin_1_decode", "argument 1", "contiguous buffer", args[0]);
  1172. goto exit;
  1173. }
  1174. if (nargs < 2) {
  1175. goto skip_optional;
  1176. }
  1177. if (args[1] == Py_None) {
  1178. errors = NULL;
  1179. }
  1180. else if (PyUnicode_Check(args[1])) {
  1181. Py_ssize_t errors_length;
  1182. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  1183. if (errors == NULL) {
  1184. goto exit;
  1185. }
  1186. if (strlen(errors) != (size_t)errors_length) {
  1187. PyErr_SetString(PyExc_ValueError, "embedded null character");
  1188. goto exit;
  1189. }
  1190. }
  1191. else {
  1192. _PyArg_BadArgument("latin_1_decode", "argument 2", "str or None", args[1]);
  1193. goto exit;
  1194. }
  1195. skip_optional:
  1196. return_value = _codecs_latin_1_decode_impl(module, &data, errors);
  1197. exit:
  1198. /* Cleanup for data */
  1199. if (data.obj) {
  1200. PyBuffer_Release(&data);
  1201. }
  1202. return return_value;
  1203. }
  1204. PyDoc_STRVAR(_codecs_ascii_decode__doc__,
  1205. "ascii_decode($module, data, errors=None, /)\n"
  1206. "--\n"
  1207. "\n");
  1208. #define _CODECS_ASCII_DECODE_METHODDEF \
  1209. {"ascii_decode", _PyCFunction_CAST(_codecs_ascii_decode), METH_FASTCALL, _codecs_ascii_decode__doc__},
  1210. static PyObject *
  1211. _codecs_ascii_decode_impl(PyObject *module, Py_buffer *data,
  1212. const char *errors);
  1213. static PyObject *
  1214. _codecs_ascii_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  1215. {
  1216. PyObject *return_value = NULL;
  1217. Py_buffer data = {NULL, NULL};
  1218. const char *errors = NULL;
  1219. if (!_PyArg_CheckPositional("ascii_decode", nargs, 1, 2)) {
  1220. goto exit;
  1221. }
  1222. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  1223. goto exit;
  1224. }
  1225. if (!PyBuffer_IsContiguous(&data, 'C')) {
  1226. _PyArg_BadArgument("ascii_decode", "argument 1", "contiguous buffer", args[0]);
  1227. goto exit;
  1228. }
  1229. if (nargs < 2) {
  1230. goto skip_optional;
  1231. }
  1232. if (args[1] == Py_None) {
  1233. errors = NULL;
  1234. }
  1235. else if (PyUnicode_Check(args[1])) {
  1236. Py_ssize_t errors_length;
  1237. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  1238. if (errors == NULL) {
  1239. goto exit;
  1240. }
  1241. if (strlen(errors) != (size_t)errors_length) {
  1242. PyErr_SetString(PyExc_ValueError, "embedded null character");
  1243. goto exit;
  1244. }
  1245. }
  1246. else {
  1247. _PyArg_BadArgument("ascii_decode", "argument 2", "str or None", args[1]);
  1248. goto exit;
  1249. }
  1250. skip_optional:
  1251. return_value = _codecs_ascii_decode_impl(module, &data, errors);
  1252. exit:
  1253. /* Cleanup for data */
  1254. if (data.obj) {
  1255. PyBuffer_Release(&data);
  1256. }
  1257. return return_value;
  1258. }
  1259. PyDoc_STRVAR(_codecs_charmap_decode__doc__,
  1260. "charmap_decode($module, data, errors=None, mapping=None, /)\n"
  1261. "--\n"
  1262. "\n");
  1263. #define _CODECS_CHARMAP_DECODE_METHODDEF \
  1264. {"charmap_decode", _PyCFunction_CAST(_codecs_charmap_decode), METH_FASTCALL, _codecs_charmap_decode__doc__},
  1265. static PyObject *
  1266. _codecs_charmap_decode_impl(PyObject *module, Py_buffer *data,
  1267. const char *errors, PyObject *mapping);
  1268. static PyObject *
  1269. _codecs_charmap_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  1270. {
  1271. PyObject *return_value = NULL;
  1272. Py_buffer data = {NULL, NULL};
  1273. const char *errors = NULL;
  1274. PyObject *mapping = Py_None;
  1275. if (!_PyArg_CheckPositional("charmap_decode", nargs, 1, 3)) {
  1276. goto exit;
  1277. }
  1278. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  1279. goto exit;
  1280. }
  1281. if (!PyBuffer_IsContiguous(&data, 'C')) {
  1282. _PyArg_BadArgument("charmap_decode", "argument 1", "contiguous buffer", args[0]);
  1283. goto exit;
  1284. }
  1285. if (nargs < 2) {
  1286. goto skip_optional;
  1287. }
  1288. if (args[1] == Py_None) {
  1289. errors = NULL;
  1290. }
  1291. else if (PyUnicode_Check(args[1])) {
  1292. Py_ssize_t errors_length;
  1293. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  1294. if (errors == NULL) {
  1295. goto exit;
  1296. }
  1297. if (strlen(errors) != (size_t)errors_length) {
  1298. PyErr_SetString(PyExc_ValueError, "embedded null character");
  1299. goto exit;
  1300. }
  1301. }
  1302. else {
  1303. _PyArg_BadArgument("charmap_decode", "argument 2", "str or None", args[1]);
  1304. goto exit;
  1305. }
  1306. if (nargs < 3) {
  1307. goto skip_optional;
  1308. }
  1309. mapping = args[2];
  1310. skip_optional:
  1311. return_value = _codecs_charmap_decode_impl(module, &data, errors, mapping);
  1312. exit:
  1313. /* Cleanup for data */
  1314. if (data.obj) {
  1315. PyBuffer_Release(&data);
  1316. }
  1317. return return_value;
  1318. }
  1319. #if defined(MS_WINDOWS)
  1320. PyDoc_STRVAR(_codecs_mbcs_decode__doc__,
  1321. "mbcs_decode($module, data, errors=None, final=False, /)\n"
  1322. "--\n"
  1323. "\n");
  1324. #define _CODECS_MBCS_DECODE_METHODDEF \
  1325. {"mbcs_decode", _PyCFunction_CAST(_codecs_mbcs_decode), METH_FASTCALL, _codecs_mbcs_decode__doc__},
  1326. static PyObject *
  1327. _codecs_mbcs_decode_impl(PyObject *module, Py_buffer *data,
  1328. const char *errors, int final);
  1329. static PyObject *
  1330. _codecs_mbcs_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  1331. {
  1332. PyObject *return_value = NULL;
  1333. Py_buffer data = {NULL, NULL};
  1334. const char *errors = NULL;
  1335. int final = 0;
  1336. if (!_PyArg_CheckPositional("mbcs_decode", nargs, 1, 3)) {
  1337. goto exit;
  1338. }
  1339. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  1340. goto exit;
  1341. }
  1342. if (!PyBuffer_IsContiguous(&data, 'C')) {
  1343. _PyArg_BadArgument("mbcs_decode", "argument 1", "contiguous buffer", args[0]);
  1344. goto exit;
  1345. }
  1346. if (nargs < 2) {
  1347. goto skip_optional;
  1348. }
  1349. if (args[1] == Py_None) {
  1350. errors = NULL;
  1351. }
  1352. else if (PyUnicode_Check(args[1])) {
  1353. Py_ssize_t errors_length;
  1354. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  1355. if (errors == NULL) {
  1356. goto exit;
  1357. }
  1358. if (strlen(errors) != (size_t)errors_length) {
  1359. PyErr_SetString(PyExc_ValueError, "embedded null character");
  1360. goto exit;
  1361. }
  1362. }
  1363. else {
  1364. _PyArg_BadArgument("mbcs_decode", "argument 2", "str or None", args[1]);
  1365. goto exit;
  1366. }
  1367. if (nargs < 3) {
  1368. goto skip_optional;
  1369. }
  1370. final = PyObject_IsTrue(args[2]);
  1371. if (final < 0) {
  1372. goto exit;
  1373. }
  1374. skip_optional:
  1375. return_value = _codecs_mbcs_decode_impl(module, &data, errors, final);
  1376. exit:
  1377. /* Cleanup for data */
  1378. if (data.obj) {
  1379. PyBuffer_Release(&data);
  1380. }
  1381. return return_value;
  1382. }
  1383. #endif /* defined(MS_WINDOWS) */
  1384. #if defined(MS_WINDOWS)
  1385. PyDoc_STRVAR(_codecs_oem_decode__doc__,
  1386. "oem_decode($module, data, errors=None, final=False, /)\n"
  1387. "--\n"
  1388. "\n");
  1389. #define _CODECS_OEM_DECODE_METHODDEF \
  1390. {"oem_decode", _PyCFunction_CAST(_codecs_oem_decode), METH_FASTCALL, _codecs_oem_decode__doc__},
  1391. static PyObject *
  1392. _codecs_oem_decode_impl(PyObject *module, Py_buffer *data,
  1393. const char *errors, int final);
  1394. static PyObject *
  1395. _codecs_oem_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  1396. {
  1397. PyObject *return_value = NULL;
  1398. Py_buffer data = {NULL, NULL};
  1399. const char *errors = NULL;
  1400. int final = 0;
  1401. if (!_PyArg_CheckPositional("oem_decode", nargs, 1, 3)) {
  1402. goto exit;
  1403. }
  1404. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  1405. goto exit;
  1406. }
  1407. if (!PyBuffer_IsContiguous(&data, 'C')) {
  1408. _PyArg_BadArgument("oem_decode", "argument 1", "contiguous buffer", args[0]);
  1409. goto exit;
  1410. }
  1411. if (nargs < 2) {
  1412. goto skip_optional;
  1413. }
  1414. if (args[1] == Py_None) {
  1415. errors = NULL;
  1416. }
  1417. else if (PyUnicode_Check(args[1])) {
  1418. Py_ssize_t errors_length;
  1419. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  1420. if (errors == NULL) {
  1421. goto exit;
  1422. }
  1423. if (strlen(errors) != (size_t)errors_length) {
  1424. PyErr_SetString(PyExc_ValueError, "embedded null character");
  1425. goto exit;
  1426. }
  1427. }
  1428. else {
  1429. _PyArg_BadArgument("oem_decode", "argument 2", "str or None", args[1]);
  1430. goto exit;
  1431. }
  1432. if (nargs < 3) {
  1433. goto skip_optional;
  1434. }
  1435. final = PyObject_IsTrue(args[2]);
  1436. if (final < 0) {
  1437. goto exit;
  1438. }
  1439. skip_optional:
  1440. return_value = _codecs_oem_decode_impl(module, &data, errors, final);
  1441. exit:
  1442. /* Cleanup for data */
  1443. if (data.obj) {
  1444. PyBuffer_Release(&data);
  1445. }
  1446. return return_value;
  1447. }
  1448. #endif /* defined(MS_WINDOWS) */
  1449. #if defined(MS_WINDOWS)
  1450. PyDoc_STRVAR(_codecs_code_page_decode__doc__,
  1451. "code_page_decode($module, codepage, data, errors=None, final=False, /)\n"
  1452. "--\n"
  1453. "\n");
  1454. #define _CODECS_CODE_PAGE_DECODE_METHODDEF \
  1455. {"code_page_decode", _PyCFunction_CAST(_codecs_code_page_decode), METH_FASTCALL, _codecs_code_page_decode__doc__},
  1456. static PyObject *
  1457. _codecs_code_page_decode_impl(PyObject *module, int codepage,
  1458. Py_buffer *data, const char *errors, int final);
  1459. static PyObject *
  1460. _codecs_code_page_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  1461. {
  1462. PyObject *return_value = NULL;
  1463. int codepage;
  1464. Py_buffer data = {NULL, NULL};
  1465. const char *errors = NULL;
  1466. int final = 0;
  1467. if (!_PyArg_CheckPositional("code_page_decode", nargs, 2, 4)) {
  1468. goto exit;
  1469. }
  1470. codepage = _PyLong_AsInt(args[0]);
  1471. if (codepage == -1 && PyErr_Occurred()) {
  1472. goto exit;
  1473. }
  1474. if (PyObject_GetBuffer(args[1], &data, PyBUF_SIMPLE) != 0) {
  1475. goto exit;
  1476. }
  1477. if (!PyBuffer_IsContiguous(&data, 'C')) {
  1478. _PyArg_BadArgument("code_page_decode", "argument 2", "contiguous buffer", args[1]);
  1479. goto exit;
  1480. }
  1481. if (nargs < 3) {
  1482. goto skip_optional;
  1483. }
  1484. if (args[2] == Py_None) {
  1485. errors = NULL;
  1486. }
  1487. else if (PyUnicode_Check(args[2])) {
  1488. Py_ssize_t errors_length;
  1489. errors = PyUnicode_AsUTF8AndSize(args[2], &errors_length);
  1490. if (errors == NULL) {
  1491. goto exit;
  1492. }
  1493. if (strlen(errors) != (size_t)errors_length) {
  1494. PyErr_SetString(PyExc_ValueError, "embedded null character");
  1495. goto exit;
  1496. }
  1497. }
  1498. else {
  1499. _PyArg_BadArgument("code_page_decode", "argument 3", "str or None", args[2]);
  1500. goto exit;
  1501. }
  1502. if (nargs < 4) {
  1503. goto skip_optional;
  1504. }
  1505. final = PyObject_IsTrue(args[3]);
  1506. if (final < 0) {
  1507. goto exit;
  1508. }
  1509. skip_optional:
  1510. return_value = _codecs_code_page_decode_impl(module, codepage, &data, errors, final);
  1511. exit:
  1512. /* Cleanup for data */
  1513. if (data.obj) {
  1514. PyBuffer_Release(&data);
  1515. }
  1516. return return_value;
  1517. }
  1518. #endif /* defined(MS_WINDOWS) */
  1519. PyDoc_STRVAR(_codecs_readbuffer_encode__doc__,
  1520. "readbuffer_encode($module, data, errors=None, /)\n"
  1521. "--\n"
  1522. "\n");
  1523. #define _CODECS_READBUFFER_ENCODE_METHODDEF \
  1524. {"readbuffer_encode", _PyCFunction_CAST(_codecs_readbuffer_encode), METH_FASTCALL, _codecs_readbuffer_encode__doc__},
  1525. static PyObject *
  1526. _codecs_readbuffer_encode_impl(PyObject *module, Py_buffer *data,
  1527. const char *errors);
  1528. static PyObject *
  1529. _codecs_readbuffer_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  1530. {
  1531. PyObject *return_value = NULL;
  1532. Py_buffer data = {NULL, NULL};
  1533. const char *errors = NULL;
  1534. if (!_PyArg_CheckPositional("readbuffer_encode", nargs, 1, 2)) {
  1535. goto exit;
  1536. }
  1537. if (PyUnicode_Check(args[0])) {
  1538. Py_ssize_t len;
  1539. const char *ptr = PyUnicode_AsUTF8AndSize(args[0], &len);
  1540. if (ptr == NULL) {
  1541. goto exit;
  1542. }
  1543. PyBuffer_FillInfo(&data, args[0], (void *)ptr, len, 1, 0);
  1544. }
  1545. else { /* any bytes-like object */
  1546. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  1547. goto exit;
  1548. }
  1549. if (!PyBuffer_IsContiguous(&data, 'C')) {
  1550. _PyArg_BadArgument("readbuffer_encode", "argument 1", "contiguous buffer", args[0]);
  1551. goto exit;
  1552. }
  1553. }
  1554. if (nargs < 2) {
  1555. goto skip_optional;
  1556. }
  1557. if (args[1] == Py_None) {
  1558. errors = NULL;
  1559. }
  1560. else if (PyUnicode_Check(args[1])) {
  1561. Py_ssize_t errors_length;
  1562. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  1563. if (errors == NULL) {
  1564. goto exit;
  1565. }
  1566. if (strlen(errors) != (size_t)errors_length) {
  1567. PyErr_SetString(PyExc_ValueError, "embedded null character");
  1568. goto exit;
  1569. }
  1570. }
  1571. else {
  1572. _PyArg_BadArgument("readbuffer_encode", "argument 2", "str or None", args[1]);
  1573. goto exit;
  1574. }
  1575. skip_optional:
  1576. return_value = _codecs_readbuffer_encode_impl(module, &data, errors);
  1577. exit:
  1578. /* Cleanup for data */
  1579. if (data.obj) {
  1580. PyBuffer_Release(&data);
  1581. }
  1582. return return_value;
  1583. }
  1584. PyDoc_STRVAR(_codecs_utf_7_encode__doc__,
  1585. "utf_7_encode($module, str, errors=None, /)\n"
  1586. "--\n"
  1587. "\n");
  1588. #define _CODECS_UTF_7_ENCODE_METHODDEF \
  1589. {"utf_7_encode", _PyCFunction_CAST(_codecs_utf_7_encode), METH_FASTCALL, _codecs_utf_7_encode__doc__},
  1590. static PyObject *
  1591. _codecs_utf_7_encode_impl(PyObject *module, PyObject *str,
  1592. const char *errors);
  1593. static PyObject *
  1594. _codecs_utf_7_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  1595. {
  1596. PyObject *return_value = NULL;
  1597. PyObject *str;
  1598. const char *errors = NULL;
  1599. if (!_PyArg_CheckPositional("utf_7_encode", nargs, 1, 2)) {
  1600. goto exit;
  1601. }
  1602. if (!PyUnicode_Check(args[0])) {
  1603. _PyArg_BadArgument("utf_7_encode", "argument 1", "str", args[0]);
  1604. goto exit;
  1605. }
  1606. if (PyUnicode_READY(args[0]) == -1) {
  1607. goto exit;
  1608. }
  1609. str = args[0];
  1610. if (nargs < 2) {
  1611. goto skip_optional;
  1612. }
  1613. if (args[1] == Py_None) {
  1614. errors = NULL;
  1615. }
  1616. else if (PyUnicode_Check(args[1])) {
  1617. Py_ssize_t errors_length;
  1618. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  1619. if (errors == NULL) {
  1620. goto exit;
  1621. }
  1622. if (strlen(errors) != (size_t)errors_length) {
  1623. PyErr_SetString(PyExc_ValueError, "embedded null character");
  1624. goto exit;
  1625. }
  1626. }
  1627. else {
  1628. _PyArg_BadArgument("utf_7_encode", "argument 2", "str or None", args[1]);
  1629. goto exit;
  1630. }
  1631. skip_optional:
  1632. return_value = _codecs_utf_7_encode_impl(module, str, errors);
  1633. exit:
  1634. return return_value;
  1635. }
  1636. PyDoc_STRVAR(_codecs_utf_8_encode__doc__,
  1637. "utf_8_encode($module, str, errors=None, /)\n"
  1638. "--\n"
  1639. "\n");
  1640. #define _CODECS_UTF_8_ENCODE_METHODDEF \
  1641. {"utf_8_encode", _PyCFunction_CAST(_codecs_utf_8_encode), METH_FASTCALL, _codecs_utf_8_encode__doc__},
  1642. static PyObject *
  1643. _codecs_utf_8_encode_impl(PyObject *module, PyObject *str,
  1644. const char *errors);
  1645. static PyObject *
  1646. _codecs_utf_8_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  1647. {
  1648. PyObject *return_value = NULL;
  1649. PyObject *str;
  1650. const char *errors = NULL;
  1651. if (!_PyArg_CheckPositional("utf_8_encode", nargs, 1, 2)) {
  1652. goto exit;
  1653. }
  1654. if (!PyUnicode_Check(args[0])) {
  1655. _PyArg_BadArgument("utf_8_encode", "argument 1", "str", args[0]);
  1656. goto exit;
  1657. }
  1658. if (PyUnicode_READY(args[0]) == -1) {
  1659. goto exit;
  1660. }
  1661. str = args[0];
  1662. if (nargs < 2) {
  1663. goto skip_optional;
  1664. }
  1665. if (args[1] == Py_None) {
  1666. errors = NULL;
  1667. }
  1668. else if (PyUnicode_Check(args[1])) {
  1669. Py_ssize_t errors_length;
  1670. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  1671. if (errors == NULL) {
  1672. goto exit;
  1673. }
  1674. if (strlen(errors) != (size_t)errors_length) {
  1675. PyErr_SetString(PyExc_ValueError, "embedded null character");
  1676. goto exit;
  1677. }
  1678. }
  1679. else {
  1680. _PyArg_BadArgument("utf_8_encode", "argument 2", "str or None", args[1]);
  1681. goto exit;
  1682. }
  1683. skip_optional:
  1684. return_value = _codecs_utf_8_encode_impl(module, str, errors);
  1685. exit:
  1686. return return_value;
  1687. }
  1688. PyDoc_STRVAR(_codecs_utf_16_encode__doc__,
  1689. "utf_16_encode($module, str, errors=None, byteorder=0, /)\n"
  1690. "--\n"
  1691. "\n");
  1692. #define _CODECS_UTF_16_ENCODE_METHODDEF \
  1693. {"utf_16_encode", _PyCFunction_CAST(_codecs_utf_16_encode), METH_FASTCALL, _codecs_utf_16_encode__doc__},
  1694. static PyObject *
  1695. _codecs_utf_16_encode_impl(PyObject *module, PyObject *str,
  1696. const char *errors, int byteorder);
  1697. static PyObject *
  1698. _codecs_utf_16_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  1699. {
  1700. PyObject *return_value = NULL;
  1701. PyObject *str;
  1702. const char *errors = NULL;
  1703. int byteorder = 0;
  1704. if (!_PyArg_CheckPositional("utf_16_encode", nargs, 1, 3)) {
  1705. goto exit;
  1706. }
  1707. if (!PyUnicode_Check(args[0])) {
  1708. _PyArg_BadArgument("utf_16_encode", "argument 1", "str", args[0]);
  1709. goto exit;
  1710. }
  1711. if (PyUnicode_READY(args[0]) == -1) {
  1712. goto exit;
  1713. }
  1714. str = args[0];
  1715. if (nargs < 2) {
  1716. goto skip_optional;
  1717. }
  1718. if (args[1] == Py_None) {
  1719. errors = NULL;
  1720. }
  1721. else if (PyUnicode_Check(args[1])) {
  1722. Py_ssize_t errors_length;
  1723. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  1724. if (errors == NULL) {
  1725. goto exit;
  1726. }
  1727. if (strlen(errors) != (size_t)errors_length) {
  1728. PyErr_SetString(PyExc_ValueError, "embedded null character");
  1729. goto exit;
  1730. }
  1731. }
  1732. else {
  1733. _PyArg_BadArgument("utf_16_encode", "argument 2", "str or None", args[1]);
  1734. goto exit;
  1735. }
  1736. if (nargs < 3) {
  1737. goto skip_optional;
  1738. }
  1739. byteorder = _PyLong_AsInt(args[2]);
  1740. if (byteorder == -1 && PyErr_Occurred()) {
  1741. goto exit;
  1742. }
  1743. skip_optional:
  1744. return_value = _codecs_utf_16_encode_impl(module, str, errors, byteorder);
  1745. exit:
  1746. return return_value;
  1747. }
  1748. PyDoc_STRVAR(_codecs_utf_16_le_encode__doc__,
  1749. "utf_16_le_encode($module, str, errors=None, /)\n"
  1750. "--\n"
  1751. "\n");
  1752. #define _CODECS_UTF_16_LE_ENCODE_METHODDEF \
  1753. {"utf_16_le_encode", _PyCFunction_CAST(_codecs_utf_16_le_encode), METH_FASTCALL, _codecs_utf_16_le_encode__doc__},
  1754. static PyObject *
  1755. _codecs_utf_16_le_encode_impl(PyObject *module, PyObject *str,
  1756. const char *errors);
  1757. static PyObject *
  1758. _codecs_utf_16_le_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  1759. {
  1760. PyObject *return_value = NULL;
  1761. PyObject *str;
  1762. const char *errors = NULL;
  1763. if (!_PyArg_CheckPositional("utf_16_le_encode", nargs, 1, 2)) {
  1764. goto exit;
  1765. }
  1766. if (!PyUnicode_Check(args[0])) {
  1767. _PyArg_BadArgument("utf_16_le_encode", "argument 1", "str", args[0]);
  1768. goto exit;
  1769. }
  1770. if (PyUnicode_READY(args[0]) == -1) {
  1771. goto exit;
  1772. }
  1773. str = args[0];
  1774. if (nargs < 2) {
  1775. goto skip_optional;
  1776. }
  1777. if (args[1] == Py_None) {
  1778. errors = NULL;
  1779. }
  1780. else if (PyUnicode_Check(args[1])) {
  1781. Py_ssize_t errors_length;
  1782. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  1783. if (errors == NULL) {
  1784. goto exit;
  1785. }
  1786. if (strlen(errors) != (size_t)errors_length) {
  1787. PyErr_SetString(PyExc_ValueError, "embedded null character");
  1788. goto exit;
  1789. }
  1790. }
  1791. else {
  1792. _PyArg_BadArgument("utf_16_le_encode", "argument 2", "str or None", args[1]);
  1793. goto exit;
  1794. }
  1795. skip_optional:
  1796. return_value = _codecs_utf_16_le_encode_impl(module, str, errors);
  1797. exit:
  1798. return return_value;
  1799. }
  1800. PyDoc_STRVAR(_codecs_utf_16_be_encode__doc__,
  1801. "utf_16_be_encode($module, str, errors=None, /)\n"
  1802. "--\n"
  1803. "\n");
  1804. #define _CODECS_UTF_16_BE_ENCODE_METHODDEF \
  1805. {"utf_16_be_encode", _PyCFunction_CAST(_codecs_utf_16_be_encode), METH_FASTCALL, _codecs_utf_16_be_encode__doc__},
  1806. static PyObject *
  1807. _codecs_utf_16_be_encode_impl(PyObject *module, PyObject *str,
  1808. const char *errors);
  1809. static PyObject *
  1810. _codecs_utf_16_be_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  1811. {
  1812. PyObject *return_value = NULL;
  1813. PyObject *str;
  1814. const char *errors = NULL;
  1815. if (!_PyArg_CheckPositional("utf_16_be_encode", nargs, 1, 2)) {
  1816. goto exit;
  1817. }
  1818. if (!PyUnicode_Check(args[0])) {
  1819. _PyArg_BadArgument("utf_16_be_encode", "argument 1", "str", args[0]);
  1820. goto exit;
  1821. }
  1822. if (PyUnicode_READY(args[0]) == -1) {
  1823. goto exit;
  1824. }
  1825. str = args[0];
  1826. if (nargs < 2) {
  1827. goto skip_optional;
  1828. }
  1829. if (args[1] == Py_None) {
  1830. errors = NULL;
  1831. }
  1832. else if (PyUnicode_Check(args[1])) {
  1833. Py_ssize_t errors_length;
  1834. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  1835. if (errors == NULL) {
  1836. goto exit;
  1837. }
  1838. if (strlen(errors) != (size_t)errors_length) {
  1839. PyErr_SetString(PyExc_ValueError, "embedded null character");
  1840. goto exit;
  1841. }
  1842. }
  1843. else {
  1844. _PyArg_BadArgument("utf_16_be_encode", "argument 2", "str or None", args[1]);
  1845. goto exit;
  1846. }
  1847. skip_optional:
  1848. return_value = _codecs_utf_16_be_encode_impl(module, str, errors);
  1849. exit:
  1850. return return_value;
  1851. }
  1852. PyDoc_STRVAR(_codecs_utf_32_encode__doc__,
  1853. "utf_32_encode($module, str, errors=None, byteorder=0, /)\n"
  1854. "--\n"
  1855. "\n");
  1856. #define _CODECS_UTF_32_ENCODE_METHODDEF \
  1857. {"utf_32_encode", _PyCFunction_CAST(_codecs_utf_32_encode), METH_FASTCALL, _codecs_utf_32_encode__doc__},
  1858. static PyObject *
  1859. _codecs_utf_32_encode_impl(PyObject *module, PyObject *str,
  1860. const char *errors, int byteorder);
  1861. static PyObject *
  1862. _codecs_utf_32_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  1863. {
  1864. PyObject *return_value = NULL;
  1865. PyObject *str;
  1866. const char *errors = NULL;
  1867. int byteorder = 0;
  1868. if (!_PyArg_CheckPositional("utf_32_encode", nargs, 1, 3)) {
  1869. goto exit;
  1870. }
  1871. if (!PyUnicode_Check(args[0])) {
  1872. _PyArg_BadArgument("utf_32_encode", "argument 1", "str", args[0]);
  1873. goto exit;
  1874. }
  1875. if (PyUnicode_READY(args[0]) == -1) {
  1876. goto exit;
  1877. }
  1878. str = args[0];
  1879. if (nargs < 2) {
  1880. goto skip_optional;
  1881. }
  1882. if (args[1] == Py_None) {
  1883. errors = NULL;
  1884. }
  1885. else if (PyUnicode_Check(args[1])) {
  1886. Py_ssize_t errors_length;
  1887. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  1888. if (errors == NULL) {
  1889. goto exit;
  1890. }
  1891. if (strlen(errors) != (size_t)errors_length) {
  1892. PyErr_SetString(PyExc_ValueError, "embedded null character");
  1893. goto exit;
  1894. }
  1895. }
  1896. else {
  1897. _PyArg_BadArgument("utf_32_encode", "argument 2", "str or None", args[1]);
  1898. goto exit;
  1899. }
  1900. if (nargs < 3) {
  1901. goto skip_optional;
  1902. }
  1903. byteorder = _PyLong_AsInt(args[2]);
  1904. if (byteorder == -1 && PyErr_Occurred()) {
  1905. goto exit;
  1906. }
  1907. skip_optional:
  1908. return_value = _codecs_utf_32_encode_impl(module, str, errors, byteorder);
  1909. exit:
  1910. return return_value;
  1911. }
  1912. PyDoc_STRVAR(_codecs_utf_32_le_encode__doc__,
  1913. "utf_32_le_encode($module, str, errors=None, /)\n"
  1914. "--\n"
  1915. "\n");
  1916. #define _CODECS_UTF_32_LE_ENCODE_METHODDEF \
  1917. {"utf_32_le_encode", _PyCFunction_CAST(_codecs_utf_32_le_encode), METH_FASTCALL, _codecs_utf_32_le_encode__doc__},
  1918. static PyObject *
  1919. _codecs_utf_32_le_encode_impl(PyObject *module, PyObject *str,
  1920. const char *errors);
  1921. static PyObject *
  1922. _codecs_utf_32_le_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  1923. {
  1924. PyObject *return_value = NULL;
  1925. PyObject *str;
  1926. const char *errors = NULL;
  1927. if (!_PyArg_CheckPositional("utf_32_le_encode", nargs, 1, 2)) {
  1928. goto exit;
  1929. }
  1930. if (!PyUnicode_Check(args[0])) {
  1931. _PyArg_BadArgument("utf_32_le_encode", "argument 1", "str", args[0]);
  1932. goto exit;
  1933. }
  1934. if (PyUnicode_READY(args[0]) == -1) {
  1935. goto exit;
  1936. }
  1937. str = args[0];
  1938. if (nargs < 2) {
  1939. goto skip_optional;
  1940. }
  1941. if (args[1] == Py_None) {
  1942. errors = NULL;
  1943. }
  1944. else if (PyUnicode_Check(args[1])) {
  1945. Py_ssize_t errors_length;
  1946. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  1947. if (errors == NULL) {
  1948. goto exit;
  1949. }
  1950. if (strlen(errors) != (size_t)errors_length) {
  1951. PyErr_SetString(PyExc_ValueError, "embedded null character");
  1952. goto exit;
  1953. }
  1954. }
  1955. else {
  1956. _PyArg_BadArgument("utf_32_le_encode", "argument 2", "str or None", args[1]);
  1957. goto exit;
  1958. }
  1959. skip_optional:
  1960. return_value = _codecs_utf_32_le_encode_impl(module, str, errors);
  1961. exit:
  1962. return return_value;
  1963. }
  1964. PyDoc_STRVAR(_codecs_utf_32_be_encode__doc__,
  1965. "utf_32_be_encode($module, str, errors=None, /)\n"
  1966. "--\n"
  1967. "\n");
  1968. #define _CODECS_UTF_32_BE_ENCODE_METHODDEF \
  1969. {"utf_32_be_encode", _PyCFunction_CAST(_codecs_utf_32_be_encode), METH_FASTCALL, _codecs_utf_32_be_encode__doc__},
  1970. static PyObject *
  1971. _codecs_utf_32_be_encode_impl(PyObject *module, PyObject *str,
  1972. const char *errors);
  1973. static PyObject *
  1974. _codecs_utf_32_be_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  1975. {
  1976. PyObject *return_value = NULL;
  1977. PyObject *str;
  1978. const char *errors = NULL;
  1979. if (!_PyArg_CheckPositional("utf_32_be_encode", nargs, 1, 2)) {
  1980. goto exit;
  1981. }
  1982. if (!PyUnicode_Check(args[0])) {
  1983. _PyArg_BadArgument("utf_32_be_encode", "argument 1", "str", args[0]);
  1984. goto exit;
  1985. }
  1986. if (PyUnicode_READY(args[0]) == -1) {
  1987. goto exit;
  1988. }
  1989. str = args[0];
  1990. if (nargs < 2) {
  1991. goto skip_optional;
  1992. }
  1993. if (args[1] == Py_None) {
  1994. errors = NULL;
  1995. }
  1996. else if (PyUnicode_Check(args[1])) {
  1997. Py_ssize_t errors_length;
  1998. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  1999. if (errors == NULL) {
  2000. goto exit;
  2001. }
  2002. if (strlen(errors) != (size_t)errors_length) {
  2003. PyErr_SetString(PyExc_ValueError, "embedded null character");
  2004. goto exit;
  2005. }
  2006. }
  2007. else {
  2008. _PyArg_BadArgument("utf_32_be_encode", "argument 2", "str or None", args[1]);
  2009. goto exit;
  2010. }
  2011. skip_optional:
  2012. return_value = _codecs_utf_32_be_encode_impl(module, str, errors);
  2013. exit:
  2014. return return_value;
  2015. }
  2016. PyDoc_STRVAR(_codecs_unicode_escape_encode__doc__,
  2017. "unicode_escape_encode($module, str, errors=None, /)\n"
  2018. "--\n"
  2019. "\n");
  2020. #define _CODECS_UNICODE_ESCAPE_ENCODE_METHODDEF \
  2021. {"unicode_escape_encode", _PyCFunction_CAST(_codecs_unicode_escape_encode), METH_FASTCALL, _codecs_unicode_escape_encode__doc__},
  2022. static PyObject *
  2023. _codecs_unicode_escape_encode_impl(PyObject *module, PyObject *str,
  2024. const char *errors);
  2025. static PyObject *
  2026. _codecs_unicode_escape_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  2027. {
  2028. PyObject *return_value = NULL;
  2029. PyObject *str;
  2030. const char *errors = NULL;
  2031. if (!_PyArg_CheckPositional("unicode_escape_encode", nargs, 1, 2)) {
  2032. goto exit;
  2033. }
  2034. if (!PyUnicode_Check(args[0])) {
  2035. _PyArg_BadArgument("unicode_escape_encode", "argument 1", "str", args[0]);
  2036. goto exit;
  2037. }
  2038. if (PyUnicode_READY(args[0]) == -1) {
  2039. goto exit;
  2040. }
  2041. str = args[0];
  2042. if (nargs < 2) {
  2043. goto skip_optional;
  2044. }
  2045. if (args[1] == Py_None) {
  2046. errors = NULL;
  2047. }
  2048. else if (PyUnicode_Check(args[1])) {
  2049. Py_ssize_t errors_length;
  2050. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  2051. if (errors == NULL) {
  2052. goto exit;
  2053. }
  2054. if (strlen(errors) != (size_t)errors_length) {
  2055. PyErr_SetString(PyExc_ValueError, "embedded null character");
  2056. goto exit;
  2057. }
  2058. }
  2059. else {
  2060. _PyArg_BadArgument("unicode_escape_encode", "argument 2", "str or None", args[1]);
  2061. goto exit;
  2062. }
  2063. skip_optional:
  2064. return_value = _codecs_unicode_escape_encode_impl(module, str, errors);
  2065. exit:
  2066. return return_value;
  2067. }
  2068. PyDoc_STRVAR(_codecs_raw_unicode_escape_encode__doc__,
  2069. "raw_unicode_escape_encode($module, str, errors=None, /)\n"
  2070. "--\n"
  2071. "\n");
  2072. #define _CODECS_RAW_UNICODE_ESCAPE_ENCODE_METHODDEF \
  2073. {"raw_unicode_escape_encode", _PyCFunction_CAST(_codecs_raw_unicode_escape_encode), METH_FASTCALL, _codecs_raw_unicode_escape_encode__doc__},
  2074. static PyObject *
  2075. _codecs_raw_unicode_escape_encode_impl(PyObject *module, PyObject *str,
  2076. const char *errors);
  2077. static PyObject *
  2078. _codecs_raw_unicode_escape_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  2079. {
  2080. PyObject *return_value = NULL;
  2081. PyObject *str;
  2082. const char *errors = NULL;
  2083. if (!_PyArg_CheckPositional("raw_unicode_escape_encode", nargs, 1, 2)) {
  2084. goto exit;
  2085. }
  2086. if (!PyUnicode_Check(args[0])) {
  2087. _PyArg_BadArgument("raw_unicode_escape_encode", "argument 1", "str", args[0]);
  2088. goto exit;
  2089. }
  2090. if (PyUnicode_READY(args[0]) == -1) {
  2091. goto exit;
  2092. }
  2093. str = args[0];
  2094. if (nargs < 2) {
  2095. goto skip_optional;
  2096. }
  2097. if (args[1] == Py_None) {
  2098. errors = NULL;
  2099. }
  2100. else if (PyUnicode_Check(args[1])) {
  2101. Py_ssize_t errors_length;
  2102. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  2103. if (errors == NULL) {
  2104. goto exit;
  2105. }
  2106. if (strlen(errors) != (size_t)errors_length) {
  2107. PyErr_SetString(PyExc_ValueError, "embedded null character");
  2108. goto exit;
  2109. }
  2110. }
  2111. else {
  2112. _PyArg_BadArgument("raw_unicode_escape_encode", "argument 2", "str or None", args[1]);
  2113. goto exit;
  2114. }
  2115. skip_optional:
  2116. return_value = _codecs_raw_unicode_escape_encode_impl(module, str, errors);
  2117. exit:
  2118. return return_value;
  2119. }
  2120. PyDoc_STRVAR(_codecs_latin_1_encode__doc__,
  2121. "latin_1_encode($module, str, errors=None, /)\n"
  2122. "--\n"
  2123. "\n");
  2124. #define _CODECS_LATIN_1_ENCODE_METHODDEF \
  2125. {"latin_1_encode", _PyCFunction_CAST(_codecs_latin_1_encode), METH_FASTCALL, _codecs_latin_1_encode__doc__},
  2126. static PyObject *
  2127. _codecs_latin_1_encode_impl(PyObject *module, PyObject *str,
  2128. const char *errors);
  2129. static PyObject *
  2130. _codecs_latin_1_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  2131. {
  2132. PyObject *return_value = NULL;
  2133. PyObject *str;
  2134. const char *errors = NULL;
  2135. if (!_PyArg_CheckPositional("latin_1_encode", nargs, 1, 2)) {
  2136. goto exit;
  2137. }
  2138. if (!PyUnicode_Check(args[0])) {
  2139. _PyArg_BadArgument("latin_1_encode", "argument 1", "str", args[0]);
  2140. goto exit;
  2141. }
  2142. if (PyUnicode_READY(args[0]) == -1) {
  2143. goto exit;
  2144. }
  2145. str = args[0];
  2146. if (nargs < 2) {
  2147. goto skip_optional;
  2148. }
  2149. if (args[1] == Py_None) {
  2150. errors = NULL;
  2151. }
  2152. else if (PyUnicode_Check(args[1])) {
  2153. Py_ssize_t errors_length;
  2154. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  2155. if (errors == NULL) {
  2156. goto exit;
  2157. }
  2158. if (strlen(errors) != (size_t)errors_length) {
  2159. PyErr_SetString(PyExc_ValueError, "embedded null character");
  2160. goto exit;
  2161. }
  2162. }
  2163. else {
  2164. _PyArg_BadArgument("latin_1_encode", "argument 2", "str or None", args[1]);
  2165. goto exit;
  2166. }
  2167. skip_optional:
  2168. return_value = _codecs_latin_1_encode_impl(module, str, errors);
  2169. exit:
  2170. return return_value;
  2171. }
  2172. PyDoc_STRVAR(_codecs_ascii_encode__doc__,
  2173. "ascii_encode($module, str, errors=None, /)\n"
  2174. "--\n"
  2175. "\n");
  2176. #define _CODECS_ASCII_ENCODE_METHODDEF \
  2177. {"ascii_encode", _PyCFunction_CAST(_codecs_ascii_encode), METH_FASTCALL, _codecs_ascii_encode__doc__},
  2178. static PyObject *
  2179. _codecs_ascii_encode_impl(PyObject *module, PyObject *str,
  2180. const char *errors);
  2181. static PyObject *
  2182. _codecs_ascii_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  2183. {
  2184. PyObject *return_value = NULL;
  2185. PyObject *str;
  2186. const char *errors = NULL;
  2187. if (!_PyArg_CheckPositional("ascii_encode", nargs, 1, 2)) {
  2188. goto exit;
  2189. }
  2190. if (!PyUnicode_Check(args[0])) {
  2191. _PyArg_BadArgument("ascii_encode", "argument 1", "str", args[0]);
  2192. goto exit;
  2193. }
  2194. if (PyUnicode_READY(args[0]) == -1) {
  2195. goto exit;
  2196. }
  2197. str = args[0];
  2198. if (nargs < 2) {
  2199. goto skip_optional;
  2200. }
  2201. if (args[1] == Py_None) {
  2202. errors = NULL;
  2203. }
  2204. else if (PyUnicode_Check(args[1])) {
  2205. Py_ssize_t errors_length;
  2206. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  2207. if (errors == NULL) {
  2208. goto exit;
  2209. }
  2210. if (strlen(errors) != (size_t)errors_length) {
  2211. PyErr_SetString(PyExc_ValueError, "embedded null character");
  2212. goto exit;
  2213. }
  2214. }
  2215. else {
  2216. _PyArg_BadArgument("ascii_encode", "argument 2", "str or None", args[1]);
  2217. goto exit;
  2218. }
  2219. skip_optional:
  2220. return_value = _codecs_ascii_encode_impl(module, str, errors);
  2221. exit:
  2222. return return_value;
  2223. }
  2224. PyDoc_STRVAR(_codecs_charmap_encode__doc__,
  2225. "charmap_encode($module, str, errors=None, mapping=None, /)\n"
  2226. "--\n"
  2227. "\n");
  2228. #define _CODECS_CHARMAP_ENCODE_METHODDEF \
  2229. {"charmap_encode", _PyCFunction_CAST(_codecs_charmap_encode), METH_FASTCALL, _codecs_charmap_encode__doc__},
  2230. static PyObject *
  2231. _codecs_charmap_encode_impl(PyObject *module, PyObject *str,
  2232. const char *errors, PyObject *mapping);
  2233. static PyObject *
  2234. _codecs_charmap_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  2235. {
  2236. PyObject *return_value = NULL;
  2237. PyObject *str;
  2238. const char *errors = NULL;
  2239. PyObject *mapping = Py_None;
  2240. if (!_PyArg_CheckPositional("charmap_encode", nargs, 1, 3)) {
  2241. goto exit;
  2242. }
  2243. if (!PyUnicode_Check(args[0])) {
  2244. _PyArg_BadArgument("charmap_encode", "argument 1", "str", args[0]);
  2245. goto exit;
  2246. }
  2247. if (PyUnicode_READY(args[0]) == -1) {
  2248. goto exit;
  2249. }
  2250. str = args[0];
  2251. if (nargs < 2) {
  2252. goto skip_optional;
  2253. }
  2254. if (args[1] == Py_None) {
  2255. errors = NULL;
  2256. }
  2257. else if (PyUnicode_Check(args[1])) {
  2258. Py_ssize_t errors_length;
  2259. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  2260. if (errors == NULL) {
  2261. goto exit;
  2262. }
  2263. if (strlen(errors) != (size_t)errors_length) {
  2264. PyErr_SetString(PyExc_ValueError, "embedded null character");
  2265. goto exit;
  2266. }
  2267. }
  2268. else {
  2269. _PyArg_BadArgument("charmap_encode", "argument 2", "str or None", args[1]);
  2270. goto exit;
  2271. }
  2272. if (nargs < 3) {
  2273. goto skip_optional;
  2274. }
  2275. mapping = args[2];
  2276. skip_optional:
  2277. return_value = _codecs_charmap_encode_impl(module, str, errors, mapping);
  2278. exit:
  2279. return return_value;
  2280. }
  2281. PyDoc_STRVAR(_codecs_charmap_build__doc__,
  2282. "charmap_build($module, map, /)\n"
  2283. "--\n"
  2284. "\n");
  2285. #define _CODECS_CHARMAP_BUILD_METHODDEF \
  2286. {"charmap_build", (PyCFunction)_codecs_charmap_build, METH_O, _codecs_charmap_build__doc__},
  2287. static PyObject *
  2288. _codecs_charmap_build_impl(PyObject *module, PyObject *map);
  2289. static PyObject *
  2290. _codecs_charmap_build(PyObject *module, PyObject *arg)
  2291. {
  2292. PyObject *return_value = NULL;
  2293. PyObject *map;
  2294. if (!PyUnicode_Check(arg)) {
  2295. _PyArg_BadArgument("charmap_build", "argument", "str", arg);
  2296. goto exit;
  2297. }
  2298. if (PyUnicode_READY(arg) == -1) {
  2299. goto exit;
  2300. }
  2301. map = arg;
  2302. return_value = _codecs_charmap_build_impl(module, map);
  2303. exit:
  2304. return return_value;
  2305. }
  2306. #if defined(MS_WINDOWS)
  2307. PyDoc_STRVAR(_codecs_mbcs_encode__doc__,
  2308. "mbcs_encode($module, str, errors=None, /)\n"
  2309. "--\n"
  2310. "\n");
  2311. #define _CODECS_MBCS_ENCODE_METHODDEF \
  2312. {"mbcs_encode", _PyCFunction_CAST(_codecs_mbcs_encode), METH_FASTCALL, _codecs_mbcs_encode__doc__},
  2313. static PyObject *
  2314. _codecs_mbcs_encode_impl(PyObject *module, PyObject *str, const char *errors);
  2315. static PyObject *
  2316. _codecs_mbcs_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  2317. {
  2318. PyObject *return_value = NULL;
  2319. PyObject *str;
  2320. const char *errors = NULL;
  2321. if (!_PyArg_CheckPositional("mbcs_encode", nargs, 1, 2)) {
  2322. goto exit;
  2323. }
  2324. if (!PyUnicode_Check(args[0])) {
  2325. _PyArg_BadArgument("mbcs_encode", "argument 1", "str", args[0]);
  2326. goto exit;
  2327. }
  2328. if (PyUnicode_READY(args[0]) == -1) {
  2329. goto exit;
  2330. }
  2331. str = args[0];
  2332. if (nargs < 2) {
  2333. goto skip_optional;
  2334. }
  2335. if (args[1] == Py_None) {
  2336. errors = NULL;
  2337. }
  2338. else if (PyUnicode_Check(args[1])) {
  2339. Py_ssize_t errors_length;
  2340. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  2341. if (errors == NULL) {
  2342. goto exit;
  2343. }
  2344. if (strlen(errors) != (size_t)errors_length) {
  2345. PyErr_SetString(PyExc_ValueError, "embedded null character");
  2346. goto exit;
  2347. }
  2348. }
  2349. else {
  2350. _PyArg_BadArgument("mbcs_encode", "argument 2", "str or None", args[1]);
  2351. goto exit;
  2352. }
  2353. skip_optional:
  2354. return_value = _codecs_mbcs_encode_impl(module, str, errors);
  2355. exit:
  2356. return return_value;
  2357. }
  2358. #endif /* defined(MS_WINDOWS) */
  2359. #if defined(MS_WINDOWS)
  2360. PyDoc_STRVAR(_codecs_oem_encode__doc__,
  2361. "oem_encode($module, str, errors=None, /)\n"
  2362. "--\n"
  2363. "\n");
  2364. #define _CODECS_OEM_ENCODE_METHODDEF \
  2365. {"oem_encode", _PyCFunction_CAST(_codecs_oem_encode), METH_FASTCALL, _codecs_oem_encode__doc__},
  2366. static PyObject *
  2367. _codecs_oem_encode_impl(PyObject *module, PyObject *str, const char *errors);
  2368. static PyObject *
  2369. _codecs_oem_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  2370. {
  2371. PyObject *return_value = NULL;
  2372. PyObject *str;
  2373. const char *errors = NULL;
  2374. if (!_PyArg_CheckPositional("oem_encode", nargs, 1, 2)) {
  2375. goto exit;
  2376. }
  2377. if (!PyUnicode_Check(args[0])) {
  2378. _PyArg_BadArgument("oem_encode", "argument 1", "str", args[0]);
  2379. goto exit;
  2380. }
  2381. if (PyUnicode_READY(args[0]) == -1) {
  2382. goto exit;
  2383. }
  2384. str = args[0];
  2385. if (nargs < 2) {
  2386. goto skip_optional;
  2387. }
  2388. if (args[1] == Py_None) {
  2389. errors = NULL;
  2390. }
  2391. else if (PyUnicode_Check(args[1])) {
  2392. Py_ssize_t errors_length;
  2393. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  2394. if (errors == NULL) {
  2395. goto exit;
  2396. }
  2397. if (strlen(errors) != (size_t)errors_length) {
  2398. PyErr_SetString(PyExc_ValueError, "embedded null character");
  2399. goto exit;
  2400. }
  2401. }
  2402. else {
  2403. _PyArg_BadArgument("oem_encode", "argument 2", "str or None", args[1]);
  2404. goto exit;
  2405. }
  2406. skip_optional:
  2407. return_value = _codecs_oem_encode_impl(module, str, errors);
  2408. exit:
  2409. return return_value;
  2410. }
  2411. #endif /* defined(MS_WINDOWS) */
  2412. #if defined(MS_WINDOWS)
  2413. PyDoc_STRVAR(_codecs_code_page_encode__doc__,
  2414. "code_page_encode($module, code_page, str, errors=None, /)\n"
  2415. "--\n"
  2416. "\n");
  2417. #define _CODECS_CODE_PAGE_ENCODE_METHODDEF \
  2418. {"code_page_encode", _PyCFunction_CAST(_codecs_code_page_encode), METH_FASTCALL, _codecs_code_page_encode__doc__},
  2419. static PyObject *
  2420. _codecs_code_page_encode_impl(PyObject *module, int code_page, PyObject *str,
  2421. const char *errors);
  2422. static PyObject *
  2423. _codecs_code_page_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  2424. {
  2425. PyObject *return_value = NULL;
  2426. int code_page;
  2427. PyObject *str;
  2428. const char *errors = NULL;
  2429. if (!_PyArg_CheckPositional("code_page_encode", nargs, 2, 3)) {
  2430. goto exit;
  2431. }
  2432. code_page = _PyLong_AsInt(args[0]);
  2433. if (code_page == -1 && PyErr_Occurred()) {
  2434. goto exit;
  2435. }
  2436. if (!PyUnicode_Check(args[1])) {
  2437. _PyArg_BadArgument("code_page_encode", "argument 2", "str", args[1]);
  2438. goto exit;
  2439. }
  2440. if (PyUnicode_READY(args[1]) == -1) {
  2441. goto exit;
  2442. }
  2443. str = args[1];
  2444. if (nargs < 3) {
  2445. goto skip_optional;
  2446. }
  2447. if (args[2] == Py_None) {
  2448. errors = NULL;
  2449. }
  2450. else if (PyUnicode_Check(args[2])) {
  2451. Py_ssize_t errors_length;
  2452. errors = PyUnicode_AsUTF8AndSize(args[2], &errors_length);
  2453. if (errors == NULL) {
  2454. goto exit;
  2455. }
  2456. if (strlen(errors) != (size_t)errors_length) {
  2457. PyErr_SetString(PyExc_ValueError, "embedded null character");
  2458. goto exit;
  2459. }
  2460. }
  2461. else {
  2462. _PyArg_BadArgument("code_page_encode", "argument 3", "str or None", args[2]);
  2463. goto exit;
  2464. }
  2465. skip_optional:
  2466. return_value = _codecs_code_page_encode_impl(module, code_page, str, errors);
  2467. exit:
  2468. return return_value;
  2469. }
  2470. #endif /* defined(MS_WINDOWS) */
  2471. PyDoc_STRVAR(_codecs_register_error__doc__,
  2472. "register_error($module, errors, handler, /)\n"
  2473. "--\n"
  2474. "\n"
  2475. "Register the specified error handler under the name errors.\n"
  2476. "\n"
  2477. "handler must be a callable object, that will be called with an exception\n"
  2478. "instance containing information about the location of the encoding/decoding\n"
  2479. "error and must return a (replacement, new position) tuple.");
  2480. #define _CODECS_REGISTER_ERROR_METHODDEF \
  2481. {"register_error", _PyCFunction_CAST(_codecs_register_error), METH_FASTCALL, _codecs_register_error__doc__},
  2482. static PyObject *
  2483. _codecs_register_error_impl(PyObject *module, const char *errors,
  2484. PyObject *handler);
  2485. static PyObject *
  2486. _codecs_register_error(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  2487. {
  2488. PyObject *return_value = NULL;
  2489. const char *errors;
  2490. PyObject *handler;
  2491. if (!_PyArg_CheckPositional("register_error", nargs, 2, 2)) {
  2492. goto exit;
  2493. }
  2494. if (!PyUnicode_Check(args[0])) {
  2495. _PyArg_BadArgument("register_error", "argument 1", "str", args[0]);
  2496. goto exit;
  2497. }
  2498. Py_ssize_t errors_length;
  2499. errors = PyUnicode_AsUTF8AndSize(args[0], &errors_length);
  2500. if (errors == NULL) {
  2501. goto exit;
  2502. }
  2503. if (strlen(errors) != (size_t)errors_length) {
  2504. PyErr_SetString(PyExc_ValueError, "embedded null character");
  2505. goto exit;
  2506. }
  2507. handler = args[1];
  2508. return_value = _codecs_register_error_impl(module, errors, handler);
  2509. exit:
  2510. return return_value;
  2511. }
  2512. PyDoc_STRVAR(_codecs_lookup_error__doc__,
  2513. "lookup_error($module, name, /)\n"
  2514. "--\n"
  2515. "\n"
  2516. "lookup_error(errors) -> handler\n"
  2517. "\n"
  2518. "Return the error handler for the specified error handling name or raise a\n"
  2519. "LookupError, if no handler exists under this name.");
  2520. #define _CODECS_LOOKUP_ERROR_METHODDEF \
  2521. {"lookup_error", (PyCFunction)_codecs_lookup_error, METH_O, _codecs_lookup_error__doc__},
  2522. static PyObject *
  2523. _codecs_lookup_error_impl(PyObject *module, const char *name);
  2524. static PyObject *
  2525. _codecs_lookup_error(PyObject *module, PyObject *arg)
  2526. {
  2527. PyObject *return_value = NULL;
  2528. const char *name;
  2529. if (!PyUnicode_Check(arg)) {
  2530. _PyArg_BadArgument("lookup_error", "argument", "str", arg);
  2531. goto exit;
  2532. }
  2533. Py_ssize_t name_length;
  2534. name = PyUnicode_AsUTF8AndSize(arg, &name_length);
  2535. if (name == NULL) {
  2536. goto exit;
  2537. }
  2538. if (strlen(name) != (size_t)name_length) {
  2539. PyErr_SetString(PyExc_ValueError, "embedded null character");
  2540. goto exit;
  2541. }
  2542. return_value = _codecs_lookup_error_impl(module, name);
  2543. exit:
  2544. return return_value;
  2545. }
  2546. #ifndef _CODECS_MBCS_DECODE_METHODDEF
  2547. #define _CODECS_MBCS_DECODE_METHODDEF
  2548. #endif /* !defined(_CODECS_MBCS_DECODE_METHODDEF) */
  2549. #ifndef _CODECS_OEM_DECODE_METHODDEF
  2550. #define _CODECS_OEM_DECODE_METHODDEF
  2551. #endif /* !defined(_CODECS_OEM_DECODE_METHODDEF) */
  2552. #ifndef _CODECS_CODE_PAGE_DECODE_METHODDEF
  2553. #define _CODECS_CODE_PAGE_DECODE_METHODDEF
  2554. #endif /* !defined(_CODECS_CODE_PAGE_DECODE_METHODDEF) */
  2555. #ifndef _CODECS_MBCS_ENCODE_METHODDEF
  2556. #define _CODECS_MBCS_ENCODE_METHODDEF
  2557. #endif /* !defined(_CODECS_MBCS_ENCODE_METHODDEF) */
  2558. #ifndef _CODECS_OEM_ENCODE_METHODDEF
  2559. #define _CODECS_OEM_ENCODE_METHODDEF
  2560. #endif /* !defined(_CODECS_OEM_ENCODE_METHODDEF) */
  2561. #ifndef _CODECS_CODE_PAGE_ENCODE_METHODDEF
  2562. #define _CODECS_CODE_PAGE_ENCODE_METHODDEF
  2563. #endif /* !defined(_CODECS_CODE_PAGE_ENCODE_METHODDEF) */
  2564. /*[clinic end generated code: output=603da07cf8dfeb4b input=a9049054013a1b77]*/