bytesobject.c.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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(bytes___bytes____doc__,
  9. "__bytes__($self, /)\n"
  10. "--\n"
  11. "\n"
  12. "Convert this value to exact type bytes.");
  13. #define BYTES___BYTES___METHODDEF \
  14. {"__bytes__", (PyCFunction)bytes___bytes__, METH_NOARGS, bytes___bytes____doc__},
  15. static PyObject *
  16. bytes___bytes___impl(PyBytesObject *self);
  17. static PyObject *
  18. bytes___bytes__(PyBytesObject *self, PyObject *Py_UNUSED(ignored))
  19. {
  20. return bytes___bytes___impl(self);
  21. }
  22. PyDoc_STRVAR(bytes_split__doc__,
  23. "split($self, /, sep=None, maxsplit=-1)\n"
  24. "--\n"
  25. "\n"
  26. "Return a list of the sections in the bytes, using sep as the delimiter.\n"
  27. "\n"
  28. " sep\n"
  29. " The delimiter according which to split the bytes.\n"
  30. " None (the default value) means split on ASCII whitespace characters\n"
  31. " (space, tab, return, newline, formfeed, vertical tab).\n"
  32. " maxsplit\n"
  33. " Maximum number of splits to do.\n"
  34. " -1 (the default value) means no limit.");
  35. #define BYTES_SPLIT_METHODDEF \
  36. {"split", _PyCFunction_CAST(bytes_split), METH_FASTCALL|METH_KEYWORDS, bytes_split__doc__},
  37. static PyObject *
  38. bytes_split_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit);
  39. static PyObject *
  40. bytes_split(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  41. {
  42. PyObject *return_value = NULL;
  43. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  44. #define NUM_KEYWORDS 2
  45. static struct {
  46. PyGC_Head _this_is_not_used;
  47. PyObject_VAR_HEAD
  48. PyObject *ob_item[NUM_KEYWORDS];
  49. } _kwtuple = {
  50. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  51. .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
  52. };
  53. #undef NUM_KEYWORDS
  54. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  55. #else // !Py_BUILD_CORE
  56. # define KWTUPLE NULL
  57. #endif // !Py_BUILD_CORE
  58. static const char * const _keywords[] = {"sep", "maxsplit", NULL};
  59. static _PyArg_Parser _parser = {
  60. .keywords = _keywords,
  61. .fname = "split",
  62. .kwtuple = KWTUPLE,
  63. };
  64. #undef KWTUPLE
  65. PyObject *argsbuf[2];
  66. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  67. PyObject *sep = Py_None;
  68. Py_ssize_t maxsplit = -1;
  69. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
  70. if (!args) {
  71. goto exit;
  72. }
  73. if (!noptargs) {
  74. goto skip_optional_pos;
  75. }
  76. if (args[0]) {
  77. sep = args[0];
  78. if (!--noptargs) {
  79. goto skip_optional_pos;
  80. }
  81. }
  82. {
  83. Py_ssize_t ival = -1;
  84. PyObject *iobj = _PyNumber_Index(args[1]);
  85. if (iobj != NULL) {
  86. ival = PyLong_AsSsize_t(iobj);
  87. Py_DECREF(iobj);
  88. }
  89. if (ival == -1 && PyErr_Occurred()) {
  90. goto exit;
  91. }
  92. maxsplit = ival;
  93. }
  94. skip_optional_pos:
  95. return_value = bytes_split_impl(self, sep, maxsplit);
  96. exit:
  97. return return_value;
  98. }
  99. PyDoc_STRVAR(bytes_partition__doc__,
  100. "partition($self, sep, /)\n"
  101. "--\n"
  102. "\n"
  103. "Partition the bytes into three parts using the given separator.\n"
  104. "\n"
  105. "This will search for the separator sep in the bytes. If the separator is found,\n"
  106. "returns a 3-tuple containing the part before the separator, the separator\n"
  107. "itself, and the part after it.\n"
  108. "\n"
  109. "If the separator is not found, returns a 3-tuple containing the original bytes\n"
  110. "object and two empty bytes objects.");
  111. #define BYTES_PARTITION_METHODDEF \
  112. {"partition", (PyCFunction)bytes_partition, METH_O, bytes_partition__doc__},
  113. static PyObject *
  114. bytes_partition_impl(PyBytesObject *self, Py_buffer *sep);
  115. static PyObject *
  116. bytes_partition(PyBytesObject *self, PyObject *arg)
  117. {
  118. PyObject *return_value = NULL;
  119. Py_buffer sep = {NULL, NULL};
  120. if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) {
  121. goto exit;
  122. }
  123. if (!PyBuffer_IsContiguous(&sep, 'C')) {
  124. _PyArg_BadArgument("partition", "argument", "contiguous buffer", arg);
  125. goto exit;
  126. }
  127. return_value = bytes_partition_impl(self, &sep);
  128. exit:
  129. /* Cleanup for sep */
  130. if (sep.obj) {
  131. PyBuffer_Release(&sep);
  132. }
  133. return return_value;
  134. }
  135. PyDoc_STRVAR(bytes_rpartition__doc__,
  136. "rpartition($self, sep, /)\n"
  137. "--\n"
  138. "\n"
  139. "Partition the bytes into three parts using the given separator.\n"
  140. "\n"
  141. "This will search for the separator sep in the bytes, starting at the end. If\n"
  142. "the separator is found, returns a 3-tuple containing the part before the\n"
  143. "separator, the separator itself, and the part after it.\n"
  144. "\n"
  145. "If the separator is not found, returns a 3-tuple containing two empty bytes\n"
  146. "objects and the original bytes object.");
  147. #define BYTES_RPARTITION_METHODDEF \
  148. {"rpartition", (PyCFunction)bytes_rpartition, METH_O, bytes_rpartition__doc__},
  149. static PyObject *
  150. bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep);
  151. static PyObject *
  152. bytes_rpartition(PyBytesObject *self, PyObject *arg)
  153. {
  154. PyObject *return_value = NULL;
  155. Py_buffer sep = {NULL, NULL};
  156. if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) {
  157. goto exit;
  158. }
  159. if (!PyBuffer_IsContiguous(&sep, 'C')) {
  160. _PyArg_BadArgument("rpartition", "argument", "contiguous buffer", arg);
  161. goto exit;
  162. }
  163. return_value = bytes_rpartition_impl(self, &sep);
  164. exit:
  165. /* Cleanup for sep */
  166. if (sep.obj) {
  167. PyBuffer_Release(&sep);
  168. }
  169. return return_value;
  170. }
  171. PyDoc_STRVAR(bytes_rsplit__doc__,
  172. "rsplit($self, /, sep=None, maxsplit=-1)\n"
  173. "--\n"
  174. "\n"
  175. "Return a list of the sections in the bytes, using sep as the delimiter.\n"
  176. "\n"
  177. " sep\n"
  178. " The delimiter according which to split the bytes.\n"
  179. " None (the default value) means split on ASCII whitespace characters\n"
  180. " (space, tab, return, newline, formfeed, vertical tab).\n"
  181. " maxsplit\n"
  182. " Maximum number of splits to do.\n"
  183. " -1 (the default value) means no limit.\n"
  184. "\n"
  185. "Splitting is done starting at the end of the bytes and working to the front.");
  186. #define BYTES_RSPLIT_METHODDEF \
  187. {"rsplit", _PyCFunction_CAST(bytes_rsplit), METH_FASTCALL|METH_KEYWORDS, bytes_rsplit__doc__},
  188. static PyObject *
  189. bytes_rsplit_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit);
  190. static PyObject *
  191. bytes_rsplit(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  192. {
  193. PyObject *return_value = NULL;
  194. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  195. #define NUM_KEYWORDS 2
  196. static struct {
  197. PyGC_Head _this_is_not_used;
  198. PyObject_VAR_HEAD
  199. PyObject *ob_item[NUM_KEYWORDS];
  200. } _kwtuple = {
  201. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  202. .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
  203. };
  204. #undef NUM_KEYWORDS
  205. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  206. #else // !Py_BUILD_CORE
  207. # define KWTUPLE NULL
  208. #endif // !Py_BUILD_CORE
  209. static const char * const _keywords[] = {"sep", "maxsplit", NULL};
  210. static _PyArg_Parser _parser = {
  211. .keywords = _keywords,
  212. .fname = "rsplit",
  213. .kwtuple = KWTUPLE,
  214. };
  215. #undef KWTUPLE
  216. PyObject *argsbuf[2];
  217. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  218. PyObject *sep = Py_None;
  219. Py_ssize_t maxsplit = -1;
  220. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
  221. if (!args) {
  222. goto exit;
  223. }
  224. if (!noptargs) {
  225. goto skip_optional_pos;
  226. }
  227. if (args[0]) {
  228. sep = args[0];
  229. if (!--noptargs) {
  230. goto skip_optional_pos;
  231. }
  232. }
  233. {
  234. Py_ssize_t ival = -1;
  235. PyObject *iobj = _PyNumber_Index(args[1]);
  236. if (iobj != NULL) {
  237. ival = PyLong_AsSsize_t(iobj);
  238. Py_DECREF(iobj);
  239. }
  240. if (ival == -1 && PyErr_Occurred()) {
  241. goto exit;
  242. }
  243. maxsplit = ival;
  244. }
  245. skip_optional_pos:
  246. return_value = bytes_rsplit_impl(self, sep, maxsplit);
  247. exit:
  248. return return_value;
  249. }
  250. PyDoc_STRVAR(bytes_join__doc__,
  251. "join($self, iterable_of_bytes, /)\n"
  252. "--\n"
  253. "\n"
  254. "Concatenate any number of bytes objects.\n"
  255. "\n"
  256. "The bytes whose method is called is inserted in between each pair.\n"
  257. "\n"
  258. "The result is returned as a new bytes object.\n"
  259. "\n"
  260. "Example: b\'.\'.join([b\'ab\', b\'pq\', b\'rs\']) -> b\'ab.pq.rs\'.");
  261. #define BYTES_JOIN_METHODDEF \
  262. {"join", (PyCFunction)bytes_join, METH_O, bytes_join__doc__},
  263. PyDoc_STRVAR(bytes_strip__doc__,
  264. "strip($self, bytes=None, /)\n"
  265. "--\n"
  266. "\n"
  267. "Strip leading and trailing bytes contained in the argument.\n"
  268. "\n"
  269. "If the argument is omitted or None, strip leading and trailing ASCII whitespace.");
  270. #define BYTES_STRIP_METHODDEF \
  271. {"strip", _PyCFunction_CAST(bytes_strip), METH_FASTCALL, bytes_strip__doc__},
  272. static PyObject *
  273. bytes_strip_impl(PyBytesObject *self, PyObject *bytes);
  274. static PyObject *
  275. bytes_strip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
  276. {
  277. PyObject *return_value = NULL;
  278. PyObject *bytes = Py_None;
  279. if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
  280. goto exit;
  281. }
  282. if (nargs < 1) {
  283. goto skip_optional;
  284. }
  285. bytes = args[0];
  286. skip_optional:
  287. return_value = bytes_strip_impl(self, bytes);
  288. exit:
  289. return return_value;
  290. }
  291. PyDoc_STRVAR(bytes_lstrip__doc__,
  292. "lstrip($self, bytes=None, /)\n"
  293. "--\n"
  294. "\n"
  295. "Strip leading bytes contained in the argument.\n"
  296. "\n"
  297. "If the argument is omitted or None, strip leading ASCII whitespace.");
  298. #define BYTES_LSTRIP_METHODDEF \
  299. {"lstrip", _PyCFunction_CAST(bytes_lstrip), METH_FASTCALL, bytes_lstrip__doc__},
  300. static PyObject *
  301. bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes);
  302. static PyObject *
  303. bytes_lstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
  304. {
  305. PyObject *return_value = NULL;
  306. PyObject *bytes = Py_None;
  307. if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
  308. goto exit;
  309. }
  310. if (nargs < 1) {
  311. goto skip_optional;
  312. }
  313. bytes = args[0];
  314. skip_optional:
  315. return_value = bytes_lstrip_impl(self, bytes);
  316. exit:
  317. return return_value;
  318. }
  319. PyDoc_STRVAR(bytes_rstrip__doc__,
  320. "rstrip($self, bytes=None, /)\n"
  321. "--\n"
  322. "\n"
  323. "Strip trailing bytes contained in the argument.\n"
  324. "\n"
  325. "If the argument is omitted or None, strip trailing ASCII whitespace.");
  326. #define BYTES_RSTRIP_METHODDEF \
  327. {"rstrip", _PyCFunction_CAST(bytes_rstrip), METH_FASTCALL, bytes_rstrip__doc__},
  328. static PyObject *
  329. bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes);
  330. static PyObject *
  331. bytes_rstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
  332. {
  333. PyObject *return_value = NULL;
  334. PyObject *bytes = Py_None;
  335. if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
  336. goto exit;
  337. }
  338. if (nargs < 1) {
  339. goto skip_optional;
  340. }
  341. bytes = args[0];
  342. skip_optional:
  343. return_value = bytes_rstrip_impl(self, bytes);
  344. exit:
  345. return return_value;
  346. }
  347. PyDoc_STRVAR(bytes_translate__doc__,
  348. "translate($self, table, /, delete=b\'\')\n"
  349. "--\n"
  350. "\n"
  351. "Return a copy with each character mapped by the given translation table.\n"
  352. "\n"
  353. " table\n"
  354. " Translation table, which must be a bytes object of length 256.\n"
  355. "\n"
  356. "All characters occurring in the optional argument delete are removed.\n"
  357. "The remaining characters are mapped through the given translation table.");
  358. #define BYTES_TRANSLATE_METHODDEF \
  359. {"translate", _PyCFunction_CAST(bytes_translate), METH_FASTCALL|METH_KEYWORDS, bytes_translate__doc__},
  360. static PyObject *
  361. bytes_translate_impl(PyBytesObject *self, PyObject *table,
  362. PyObject *deletechars);
  363. static PyObject *
  364. bytes_translate(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  365. {
  366. PyObject *return_value = NULL;
  367. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  368. #define NUM_KEYWORDS 1
  369. static struct {
  370. PyGC_Head _this_is_not_used;
  371. PyObject_VAR_HEAD
  372. PyObject *ob_item[NUM_KEYWORDS];
  373. } _kwtuple = {
  374. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  375. .ob_item = { &_Py_ID(delete), },
  376. };
  377. #undef NUM_KEYWORDS
  378. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  379. #else // !Py_BUILD_CORE
  380. # define KWTUPLE NULL
  381. #endif // !Py_BUILD_CORE
  382. static const char * const _keywords[] = {"", "delete", NULL};
  383. static _PyArg_Parser _parser = {
  384. .keywords = _keywords,
  385. .fname = "translate",
  386. .kwtuple = KWTUPLE,
  387. };
  388. #undef KWTUPLE
  389. PyObject *argsbuf[2];
  390. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
  391. PyObject *table;
  392. PyObject *deletechars = NULL;
  393. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
  394. if (!args) {
  395. goto exit;
  396. }
  397. table = args[0];
  398. if (!noptargs) {
  399. goto skip_optional_pos;
  400. }
  401. deletechars = args[1];
  402. skip_optional_pos:
  403. return_value = bytes_translate_impl(self, table, deletechars);
  404. exit:
  405. return return_value;
  406. }
  407. PyDoc_STRVAR(bytes_maketrans__doc__,
  408. "maketrans(frm, to, /)\n"
  409. "--\n"
  410. "\n"
  411. "Return a translation table useable for the bytes or bytearray translate method.\n"
  412. "\n"
  413. "The returned table will be one where each byte in frm is mapped to the byte at\n"
  414. "the same position in to.\n"
  415. "\n"
  416. "The bytes objects frm and to must be of the same length.");
  417. #define BYTES_MAKETRANS_METHODDEF \
  418. {"maketrans", _PyCFunction_CAST(bytes_maketrans), METH_FASTCALL|METH_STATIC, bytes_maketrans__doc__},
  419. static PyObject *
  420. bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to);
  421. static PyObject *
  422. bytes_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
  423. {
  424. PyObject *return_value = NULL;
  425. Py_buffer frm = {NULL, NULL};
  426. Py_buffer to = {NULL, NULL};
  427. if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
  428. goto exit;
  429. }
  430. if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
  431. goto exit;
  432. }
  433. if (!PyBuffer_IsContiguous(&frm, 'C')) {
  434. _PyArg_BadArgument("maketrans", "argument 1", "contiguous buffer", args[0]);
  435. goto exit;
  436. }
  437. if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
  438. goto exit;
  439. }
  440. if (!PyBuffer_IsContiguous(&to, 'C')) {
  441. _PyArg_BadArgument("maketrans", "argument 2", "contiguous buffer", args[1]);
  442. goto exit;
  443. }
  444. return_value = bytes_maketrans_impl(&frm, &to);
  445. exit:
  446. /* Cleanup for frm */
  447. if (frm.obj) {
  448. PyBuffer_Release(&frm);
  449. }
  450. /* Cleanup for to */
  451. if (to.obj) {
  452. PyBuffer_Release(&to);
  453. }
  454. return return_value;
  455. }
  456. PyDoc_STRVAR(bytes_replace__doc__,
  457. "replace($self, old, new, count=-1, /)\n"
  458. "--\n"
  459. "\n"
  460. "Return a copy with all occurrences of substring old replaced by new.\n"
  461. "\n"
  462. " count\n"
  463. " Maximum number of occurrences to replace.\n"
  464. " -1 (the default value) means replace all occurrences.\n"
  465. "\n"
  466. "If the optional argument count is given, only the first count occurrences are\n"
  467. "replaced.");
  468. #define BYTES_REPLACE_METHODDEF \
  469. {"replace", _PyCFunction_CAST(bytes_replace), METH_FASTCALL, bytes_replace__doc__},
  470. static PyObject *
  471. bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new,
  472. Py_ssize_t count);
  473. static PyObject *
  474. bytes_replace(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
  475. {
  476. PyObject *return_value = NULL;
  477. Py_buffer old = {NULL, NULL};
  478. Py_buffer new = {NULL, NULL};
  479. Py_ssize_t count = -1;
  480. if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
  481. goto exit;
  482. }
  483. if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
  484. goto exit;
  485. }
  486. if (!PyBuffer_IsContiguous(&old, 'C')) {
  487. _PyArg_BadArgument("replace", "argument 1", "contiguous buffer", args[0]);
  488. goto exit;
  489. }
  490. if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
  491. goto exit;
  492. }
  493. if (!PyBuffer_IsContiguous(&new, 'C')) {
  494. _PyArg_BadArgument("replace", "argument 2", "contiguous buffer", args[1]);
  495. goto exit;
  496. }
  497. if (nargs < 3) {
  498. goto skip_optional;
  499. }
  500. {
  501. Py_ssize_t ival = -1;
  502. PyObject *iobj = _PyNumber_Index(args[2]);
  503. if (iobj != NULL) {
  504. ival = PyLong_AsSsize_t(iobj);
  505. Py_DECREF(iobj);
  506. }
  507. if (ival == -1 && PyErr_Occurred()) {
  508. goto exit;
  509. }
  510. count = ival;
  511. }
  512. skip_optional:
  513. return_value = bytes_replace_impl(self, &old, &new, count);
  514. exit:
  515. /* Cleanup for old */
  516. if (old.obj) {
  517. PyBuffer_Release(&old);
  518. }
  519. /* Cleanup for new */
  520. if (new.obj) {
  521. PyBuffer_Release(&new);
  522. }
  523. return return_value;
  524. }
  525. PyDoc_STRVAR(bytes_removeprefix__doc__,
  526. "removeprefix($self, prefix, /)\n"
  527. "--\n"
  528. "\n"
  529. "Return a bytes object with the given prefix string removed if present.\n"
  530. "\n"
  531. "If the bytes starts with the prefix string, return bytes[len(prefix):].\n"
  532. "Otherwise, return a copy of the original bytes.");
  533. #define BYTES_REMOVEPREFIX_METHODDEF \
  534. {"removeprefix", (PyCFunction)bytes_removeprefix, METH_O, bytes_removeprefix__doc__},
  535. static PyObject *
  536. bytes_removeprefix_impl(PyBytesObject *self, Py_buffer *prefix);
  537. static PyObject *
  538. bytes_removeprefix(PyBytesObject *self, PyObject *arg)
  539. {
  540. PyObject *return_value = NULL;
  541. Py_buffer prefix = {NULL, NULL};
  542. if (PyObject_GetBuffer(arg, &prefix, PyBUF_SIMPLE) != 0) {
  543. goto exit;
  544. }
  545. if (!PyBuffer_IsContiguous(&prefix, 'C')) {
  546. _PyArg_BadArgument("removeprefix", "argument", "contiguous buffer", arg);
  547. goto exit;
  548. }
  549. return_value = bytes_removeprefix_impl(self, &prefix);
  550. exit:
  551. /* Cleanup for prefix */
  552. if (prefix.obj) {
  553. PyBuffer_Release(&prefix);
  554. }
  555. return return_value;
  556. }
  557. PyDoc_STRVAR(bytes_removesuffix__doc__,
  558. "removesuffix($self, suffix, /)\n"
  559. "--\n"
  560. "\n"
  561. "Return a bytes object with the given suffix string removed if present.\n"
  562. "\n"
  563. "If the bytes ends with the suffix string and that suffix is not empty,\n"
  564. "return bytes[:-len(prefix)]. Otherwise, return a copy of the original\n"
  565. "bytes.");
  566. #define BYTES_REMOVESUFFIX_METHODDEF \
  567. {"removesuffix", (PyCFunction)bytes_removesuffix, METH_O, bytes_removesuffix__doc__},
  568. static PyObject *
  569. bytes_removesuffix_impl(PyBytesObject *self, Py_buffer *suffix);
  570. static PyObject *
  571. bytes_removesuffix(PyBytesObject *self, PyObject *arg)
  572. {
  573. PyObject *return_value = NULL;
  574. Py_buffer suffix = {NULL, NULL};
  575. if (PyObject_GetBuffer(arg, &suffix, PyBUF_SIMPLE) != 0) {
  576. goto exit;
  577. }
  578. if (!PyBuffer_IsContiguous(&suffix, 'C')) {
  579. _PyArg_BadArgument("removesuffix", "argument", "contiguous buffer", arg);
  580. goto exit;
  581. }
  582. return_value = bytes_removesuffix_impl(self, &suffix);
  583. exit:
  584. /* Cleanup for suffix */
  585. if (suffix.obj) {
  586. PyBuffer_Release(&suffix);
  587. }
  588. return return_value;
  589. }
  590. PyDoc_STRVAR(bytes_decode__doc__,
  591. "decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
  592. "--\n"
  593. "\n"
  594. "Decode the bytes using the codec registered for encoding.\n"
  595. "\n"
  596. " encoding\n"
  597. " The encoding with which to decode the bytes.\n"
  598. " errors\n"
  599. " The error handling scheme to use for the handling of decoding errors.\n"
  600. " The default is \'strict\' meaning that decoding errors raise a\n"
  601. " UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
  602. " as well as any other name registered with codecs.register_error that\n"
  603. " can handle UnicodeDecodeErrors.");
  604. #define BYTES_DECODE_METHODDEF \
  605. {"decode", _PyCFunction_CAST(bytes_decode), METH_FASTCALL|METH_KEYWORDS, bytes_decode__doc__},
  606. static PyObject *
  607. bytes_decode_impl(PyBytesObject *self, const char *encoding,
  608. const char *errors);
  609. static PyObject *
  610. bytes_decode(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  611. {
  612. PyObject *return_value = NULL;
  613. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  614. #define NUM_KEYWORDS 2
  615. static struct {
  616. PyGC_Head _this_is_not_used;
  617. PyObject_VAR_HEAD
  618. PyObject *ob_item[NUM_KEYWORDS];
  619. } _kwtuple = {
  620. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  621. .ob_item = { &_Py_ID(encoding), &_Py_ID(errors), },
  622. };
  623. #undef NUM_KEYWORDS
  624. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  625. #else // !Py_BUILD_CORE
  626. # define KWTUPLE NULL
  627. #endif // !Py_BUILD_CORE
  628. static const char * const _keywords[] = {"encoding", "errors", NULL};
  629. static _PyArg_Parser _parser = {
  630. .keywords = _keywords,
  631. .fname = "decode",
  632. .kwtuple = KWTUPLE,
  633. };
  634. #undef KWTUPLE
  635. PyObject *argsbuf[2];
  636. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  637. const char *encoding = NULL;
  638. const char *errors = NULL;
  639. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
  640. if (!args) {
  641. goto exit;
  642. }
  643. if (!noptargs) {
  644. goto skip_optional_pos;
  645. }
  646. if (args[0]) {
  647. if (!PyUnicode_Check(args[0])) {
  648. _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
  649. goto exit;
  650. }
  651. Py_ssize_t encoding_length;
  652. encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
  653. if (encoding == NULL) {
  654. goto exit;
  655. }
  656. if (strlen(encoding) != (size_t)encoding_length) {
  657. PyErr_SetString(PyExc_ValueError, "embedded null character");
  658. goto exit;
  659. }
  660. if (!--noptargs) {
  661. goto skip_optional_pos;
  662. }
  663. }
  664. if (!PyUnicode_Check(args[1])) {
  665. _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
  666. goto exit;
  667. }
  668. Py_ssize_t errors_length;
  669. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  670. if (errors == NULL) {
  671. goto exit;
  672. }
  673. if (strlen(errors) != (size_t)errors_length) {
  674. PyErr_SetString(PyExc_ValueError, "embedded null character");
  675. goto exit;
  676. }
  677. skip_optional_pos:
  678. return_value = bytes_decode_impl(self, encoding, errors);
  679. exit:
  680. return return_value;
  681. }
  682. PyDoc_STRVAR(bytes_splitlines__doc__,
  683. "splitlines($self, /, keepends=False)\n"
  684. "--\n"
  685. "\n"
  686. "Return a list of the lines in the bytes, breaking at line boundaries.\n"
  687. "\n"
  688. "Line breaks are not included in the resulting list unless keepends is given and\n"
  689. "true.");
  690. #define BYTES_SPLITLINES_METHODDEF \
  691. {"splitlines", _PyCFunction_CAST(bytes_splitlines), METH_FASTCALL|METH_KEYWORDS, bytes_splitlines__doc__},
  692. static PyObject *
  693. bytes_splitlines_impl(PyBytesObject *self, int keepends);
  694. static PyObject *
  695. bytes_splitlines(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  696. {
  697. PyObject *return_value = NULL;
  698. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  699. #define NUM_KEYWORDS 1
  700. static struct {
  701. PyGC_Head _this_is_not_used;
  702. PyObject_VAR_HEAD
  703. PyObject *ob_item[NUM_KEYWORDS];
  704. } _kwtuple = {
  705. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  706. .ob_item = { &_Py_ID(keepends), },
  707. };
  708. #undef NUM_KEYWORDS
  709. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  710. #else // !Py_BUILD_CORE
  711. # define KWTUPLE NULL
  712. #endif // !Py_BUILD_CORE
  713. static const char * const _keywords[] = {"keepends", NULL};
  714. static _PyArg_Parser _parser = {
  715. .keywords = _keywords,
  716. .fname = "splitlines",
  717. .kwtuple = KWTUPLE,
  718. };
  719. #undef KWTUPLE
  720. PyObject *argsbuf[1];
  721. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  722. int keepends = 0;
  723. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
  724. if (!args) {
  725. goto exit;
  726. }
  727. if (!noptargs) {
  728. goto skip_optional_pos;
  729. }
  730. keepends = PyObject_IsTrue(args[0]);
  731. if (keepends < 0) {
  732. goto exit;
  733. }
  734. skip_optional_pos:
  735. return_value = bytes_splitlines_impl(self, keepends);
  736. exit:
  737. return return_value;
  738. }
  739. PyDoc_STRVAR(bytes_fromhex__doc__,
  740. "fromhex($type, string, /)\n"
  741. "--\n"
  742. "\n"
  743. "Create a bytes object from a string of hexadecimal numbers.\n"
  744. "\n"
  745. "Spaces between two numbers are accepted.\n"
  746. "Example: bytes.fromhex(\'B9 01EF\') -> b\'\\\\xb9\\\\x01\\\\xef\'.");
  747. #define BYTES_FROMHEX_METHODDEF \
  748. {"fromhex", (PyCFunction)bytes_fromhex, METH_O|METH_CLASS, bytes_fromhex__doc__},
  749. static PyObject *
  750. bytes_fromhex_impl(PyTypeObject *type, PyObject *string);
  751. static PyObject *
  752. bytes_fromhex(PyTypeObject *type, PyObject *arg)
  753. {
  754. PyObject *return_value = NULL;
  755. PyObject *string;
  756. if (!PyUnicode_Check(arg)) {
  757. _PyArg_BadArgument("fromhex", "argument", "str", arg);
  758. goto exit;
  759. }
  760. if (PyUnicode_READY(arg) == -1) {
  761. goto exit;
  762. }
  763. string = arg;
  764. return_value = bytes_fromhex_impl(type, string);
  765. exit:
  766. return return_value;
  767. }
  768. PyDoc_STRVAR(bytes_hex__doc__,
  769. "hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
  770. "--\n"
  771. "\n"
  772. "Create a string of hexadecimal numbers from a bytes object.\n"
  773. "\n"
  774. " sep\n"
  775. " An optional single character or byte to separate hex bytes.\n"
  776. " bytes_per_sep\n"
  777. " How many bytes between separators. Positive values count from the\n"
  778. " right, negative values count from the left.\n"
  779. "\n"
  780. "Example:\n"
  781. ">>> value = b\'\\xb9\\x01\\xef\'\n"
  782. ">>> value.hex()\n"
  783. "\'b901ef\'\n"
  784. ">>> value.hex(\':\')\n"
  785. "\'b9:01:ef\'\n"
  786. ">>> value.hex(\':\', 2)\n"
  787. "\'b9:01ef\'\n"
  788. ">>> value.hex(\':\', -2)\n"
  789. "\'b901:ef\'");
  790. #define BYTES_HEX_METHODDEF \
  791. {"hex", _PyCFunction_CAST(bytes_hex), METH_FASTCALL|METH_KEYWORDS, bytes_hex__doc__},
  792. static PyObject *
  793. bytes_hex_impl(PyBytesObject *self, PyObject *sep, int bytes_per_sep);
  794. static PyObject *
  795. bytes_hex(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  796. {
  797. PyObject *return_value = NULL;
  798. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  799. #define NUM_KEYWORDS 2
  800. static struct {
  801. PyGC_Head _this_is_not_used;
  802. PyObject_VAR_HEAD
  803. PyObject *ob_item[NUM_KEYWORDS];
  804. } _kwtuple = {
  805. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  806. .ob_item = { &_Py_ID(sep), &_Py_ID(bytes_per_sep), },
  807. };
  808. #undef NUM_KEYWORDS
  809. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  810. #else // !Py_BUILD_CORE
  811. # define KWTUPLE NULL
  812. #endif // !Py_BUILD_CORE
  813. static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
  814. static _PyArg_Parser _parser = {
  815. .keywords = _keywords,
  816. .fname = "hex",
  817. .kwtuple = KWTUPLE,
  818. };
  819. #undef KWTUPLE
  820. PyObject *argsbuf[2];
  821. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  822. PyObject *sep = NULL;
  823. int bytes_per_sep = 1;
  824. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
  825. if (!args) {
  826. goto exit;
  827. }
  828. if (!noptargs) {
  829. goto skip_optional_pos;
  830. }
  831. if (args[0]) {
  832. sep = args[0];
  833. if (!--noptargs) {
  834. goto skip_optional_pos;
  835. }
  836. }
  837. bytes_per_sep = _PyLong_AsInt(args[1]);
  838. if (bytes_per_sep == -1 && PyErr_Occurred()) {
  839. goto exit;
  840. }
  841. skip_optional_pos:
  842. return_value = bytes_hex_impl(self, sep, bytes_per_sep);
  843. exit:
  844. return return_value;
  845. }
  846. static PyObject *
  847. bytes_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
  848. const char *errors);
  849. static PyObject *
  850. bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
  851. {
  852. PyObject *return_value = NULL;
  853. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  854. #define NUM_KEYWORDS 3
  855. static struct {
  856. PyGC_Head _this_is_not_used;
  857. PyObject_VAR_HEAD
  858. PyObject *ob_item[NUM_KEYWORDS];
  859. } _kwtuple = {
  860. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  861. .ob_item = { &_Py_ID(source), &_Py_ID(encoding), &_Py_ID(errors), },
  862. };
  863. #undef NUM_KEYWORDS
  864. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  865. #else // !Py_BUILD_CORE
  866. # define KWTUPLE NULL
  867. #endif // !Py_BUILD_CORE
  868. static const char * const _keywords[] = {"source", "encoding", "errors", NULL};
  869. static _PyArg_Parser _parser = {
  870. .keywords = _keywords,
  871. .fname = "bytes",
  872. .kwtuple = KWTUPLE,
  873. };
  874. #undef KWTUPLE
  875. PyObject *argsbuf[3];
  876. PyObject * const *fastargs;
  877. Py_ssize_t nargs = PyTuple_GET_SIZE(args);
  878. Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
  879. PyObject *x = NULL;
  880. const char *encoding = NULL;
  881. const char *errors = NULL;
  882. fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 3, 0, argsbuf);
  883. if (!fastargs) {
  884. goto exit;
  885. }
  886. if (!noptargs) {
  887. goto skip_optional_pos;
  888. }
  889. if (fastargs[0]) {
  890. x = fastargs[0];
  891. if (!--noptargs) {
  892. goto skip_optional_pos;
  893. }
  894. }
  895. if (fastargs[1]) {
  896. if (!PyUnicode_Check(fastargs[1])) {
  897. _PyArg_BadArgument("bytes", "argument 'encoding'", "str", fastargs[1]);
  898. goto exit;
  899. }
  900. Py_ssize_t encoding_length;
  901. encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
  902. if (encoding == NULL) {
  903. goto exit;
  904. }
  905. if (strlen(encoding) != (size_t)encoding_length) {
  906. PyErr_SetString(PyExc_ValueError, "embedded null character");
  907. goto exit;
  908. }
  909. if (!--noptargs) {
  910. goto skip_optional_pos;
  911. }
  912. }
  913. if (!PyUnicode_Check(fastargs[2])) {
  914. _PyArg_BadArgument("bytes", "argument 'errors'", "str", fastargs[2]);
  915. goto exit;
  916. }
  917. Py_ssize_t errors_length;
  918. errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length);
  919. if (errors == NULL) {
  920. goto exit;
  921. }
  922. if (strlen(errors) != (size_t)errors_length) {
  923. PyErr_SetString(PyExc_ValueError, "embedded null character");
  924. goto exit;
  925. }
  926. skip_optional_pos:
  927. return_value = bytes_new_impl(type, x, encoding, errors);
  928. exit:
  929. return return_value;
  930. }
  931. /*[clinic end generated code: output=31a9e4af85562612 input=a9049054013a1b77]*/