blob: fac12f55210e726537cb382fec022b3c3a4ee195 [file] [log] [blame]
Benjamin Peterson4116f362008-05-27 00:36:20 +00001/* bytes object implementation */
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00002
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00003#define PY_SSIZE_T_CLEAN
Christian Heimes2c9c7a52008-05-26 13:42:13 +00004
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00005#include "Python.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01006#include "pycore_pymem.h"
7#include "pycore_pystate.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00008
Gregory P. Smith60d241f2007-10-16 06:31:30 +00009#include "bytes_methods.h"
Gregory P. Smith8cb65692015-04-25 23:22:26 +000010#include "pystrhex.h"
Mark Dickinsonfd24b322008-12-06 15:33:31 +000011#include <stddef.h>
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000012
Martin v. Löwis7252a6e2014-07-27 16:25:09 +020013/*[clinic input]
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +030014class bytes "PyBytesObject *" "&PyBytes_Type"
Martin v. Löwis7252a6e2014-07-27 16:25:09 +020015[clinic start generated code]*/
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +030016/*[clinic end generated code: output=da39a3ee5e6b4b0d input=7a238f965d64892b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +020017
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030018#include "clinic/bytesobject.c.h"
19
Christian Heimes2c9c7a52008-05-26 13:42:13 +000020#ifdef COUNT_ALLOCS
Pablo Galindo49c75a82018-10-28 15:02:17 +000021Py_ssize_t _Py_null_strings, _Py_one_strings;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000022#endif
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000023
Christian Heimes2c9c7a52008-05-26 13:42:13 +000024static PyBytesObject *characters[UCHAR_MAX + 1];
25static PyBytesObject *nullstring;
26
Mark Dickinsonfd24b322008-12-06 15:33:31 +000027/* PyBytesObject_SIZE gives the basic size of a string; any memory allocation
28 for a string of length n should request PyBytesObject_SIZE + n bytes.
29
30 Using PyBytesObject_SIZE instead of sizeof(PyBytesObject) saves
31 3 bytes per string allocation on a typical system.
32*/
33#define PyBytesObject_SIZE (offsetof(PyBytesObject, ob_sval) + 1)
34
Victor Stinner2bf89932015-10-14 11:25:33 +020035/* Forward declaration */
36Py_LOCAL_INLINE(Py_ssize_t) _PyBytesWriter_GetSize(_PyBytesWriter *writer,
37 char *str);
38
Christian Heimes2c9c7a52008-05-26 13:42:13 +000039/*
Christian Heimes2c9c7a52008-05-26 13:42:13 +000040 For PyBytes_FromString(), the parameter `str' points to a null-terminated
41 string containing exactly `size' bytes.
42
Martin Pantera90a4a92016-05-30 04:04:50 +000043 For PyBytes_FromStringAndSize(), the parameter `str' is
Christian Heimes2c9c7a52008-05-26 13:42:13 +000044 either NULL or else points to a string containing at least `size' bytes.
45 For PyBytes_FromStringAndSize(), the string in the `str' parameter does
46 not have to be null-terminated. (Therefore it is safe to construct a
47 substring by calling `PyBytes_FromStringAndSize(origstring, substrlen)'.)
48 If `str' is NULL then PyBytes_FromStringAndSize() will allocate `size+1'
49 bytes (setting the last byte to the null terminating character) and you can
50 fill in the data yourself. If `str' is non-NULL then the resulting
Antoine Pitrouf2c54842010-01-13 08:07:53 +000051 PyBytes object must be treated as immutable and you must not fill in nor
Christian Heimes2c9c7a52008-05-26 13:42:13 +000052 alter the data yourself, since the strings may be shared.
53
54 The PyObject member `op->ob_size', which denotes the number of "extra
55 items" in a variable-size object, will contain the number of bytes
Eli Bendersky1aef6b62011-03-24 22:32:56 +020056 allocated for string data, not counting the null terminating character.
57 It is therefore equal to the `size' parameter (for
Christian Heimes2c9c7a52008-05-26 13:42:13 +000058 PyBytes_FromStringAndSize()) or the length of the string in the `str'
59 parameter (for PyBytes_FromString()).
60*/
Victor Stinnerdb067af2014-05-02 22:31:14 +020061static PyObject *
62_PyBytes_FromSize(Py_ssize_t size, int use_calloc)
Guido van Rossumd624f182006-04-24 13:47:05 +000063{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020064 PyBytesObject *op;
Victor Stinnerdb067af2014-05-02 22:31:14 +020065 assert(size >= 0);
Victor Stinner049e5092014-08-17 22:20:00 +020066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 if (size == 0 && (op = nullstring) != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +000068#ifdef COUNT_ALLOCS
Pablo Galindo49c75a82018-10-28 15:02:17 +000069 _Py_null_strings++;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000070#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 Py_INCREF(op);
72 return (PyObject *)op;
73 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +000074
Victor Stinner049e5092014-08-17 22:20:00 +020075 if ((size_t)size > (size_t)PY_SSIZE_T_MAX - PyBytesObject_SIZE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 PyErr_SetString(PyExc_OverflowError,
77 "byte string is too large");
78 return NULL;
79 }
Neal Norwitz3ce5d922008-08-24 07:08:55 +000080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 /* Inline PyObject_NewVar */
Victor Stinnerdb067af2014-05-02 22:31:14 +020082 if (use_calloc)
83 op = (PyBytesObject *)PyObject_Calloc(1, PyBytesObject_SIZE + size);
84 else
85 op = (PyBytesObject *)PyObject_Malloc(PyBytesObject_SIZE + size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 if (op == NULL)
87 return PyErr_NoMemory();
Victor Stinnerb4435e22018-10-26 14:35:00 +020088 (void)PyObject_INIT_VAR((PyVarObject *)op, &PyBytes_Type, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 op->ob_shash = -1;
Victor Stinnerdb067af2014-05-02 22:31:14 +020090 if (!use_calloc)
91 op->ob_sval[size] = '\0';
92 /* empty byte string singleton */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 if (size == 0) {
94 nullstring = op;
95 Py_INCREF(op);
Victor Stinnerdb067af2014-05-02 22:31:14 +020096 }
97 return (PyObject *) op;
98}
99
100PyObject *
101PyBytes_FromStringAndSize(const char *str, Py_ssize_t size)
102{
103 PyBytesObject *op;
104 if (size < 0) {
105 PyErr_SetString(PyExc_SystemError,
106 "Negative size passed to PyBytes_FromStringAndSize");
107 return NULL;
108 }
109 if (size == 1 && str != NULL &&
110 (op = characters[*str & UCHAR_MAX]) != NULL)
111 {
112#ifdef COUNT_ALLOCS
Pablo Galindo49c75a82018-10-28 15:02:17 +0000113 _Py_one_strings++;
Victor Stinnerdb067af2014-05-02 22:31:14 +0200114#endif
115 Py_INCREF(op);
116 return (PyObject *)op;
117 }
118
119 op = (PyBytesObject *)_PyBytes_FromSize(size, 0);
120 if (op == NULL)
121 return NULL;
122 if (str == NULL)
123 return (PyObject *) op;
124
Christian Heimesf051e432016-09-13 20:22:02 +0200125 memcpy(op->ob_sval, str, size);
Victor Stinnerdb067af2014-05-02 22:31:14 +0200126 /* share short strings */
127 if (size == 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 characters[*str & UCHAR_MAX] = op;
129 Py_INCREF(op);
130 }
131 return (PyObject *) op;
Guido van Rossumd624f182006-04-24 13:47:05 +0000132}
133
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000134PyObject *
135PyBytes_FromString(const char *str)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000136{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200137 size_t size;
138 PyBytesObject *op;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 assert(str != NULL);
141 size = strlen(str);
142 if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) {
143 PyErr_SetString(PyExc_OverflowError,
144 "byte string is too long");
145 return NULL;
146 }
147 if (size == 0 && (op = nullstring) != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000148#ifdef COUNT_ALLOCS
Pablo Galindo49c75a82018-10-28 15:02:17 +0000149 _Py_null_strings++;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000150#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 Py_INCREF(op);
152 return (PyObject *)op;
153 }
154 if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000155#ifdef COUNT_ALLOCS
Pablo Galindo49c75a82018-10-28 15:02:17 +0000156 _Py_one_strings++;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000157#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 Py_INCREF(op);
159 return (PyObject *)op;
160 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 /* Inline PyObject_NewVar */
163 op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size);
164 if (op == NULL)
165 return PyErr_NoMemory();
Victor Stinnerb4435e22018-10-26 14:35:00 +0200166 (void)PyObject_INIT_VAR((PyVarObject *)op, &PyBytes_Type, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 op->ob_shash = -1;
Christian Heimesf051e432016-09-13 20:22:02 +0200168 memcpy(op->ob_sval, str, size+1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 /* share short strings */
170 if (size == 0) {
171 nullstring = op;
172 Py_INCREF(op);
173 } else if (size == 1) {
174 characters[*str & UCHAR_MAX] = op;
175 Py_INCREF(op);
176 }
177 return (PyObject *) op;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000178}
Guido van Rossumebea9be2007-04-09 00:49:13 +0000179
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000180PyObject *
181PyBytes_FromFormatV(const char *format, va_list vargs)
182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 char *s;
Victor Stinner03dab782015-10-14 00:21:35 +0200184 const char *f;
185 const char *p;
186 Py_ssize_t prec;
187 int longflag;
188 int size_tflag;
189 /* Longest 64-bit formatted numbers:
190 - "18446744073709551615\0" (21 bytes)
191 - "-9223372036854775808\0" (21 bytes)
192 Decimal takes the most space (it isn't enough for octal.)
Guido van Rossum343e97f2007-04-09 00:43:24 +0000193
Victor Stinner03dab782015-10-14 00:21:35 +0200194 Longest 64-bit pointer representation:
195 "0xffffffffffffffff\0" (19 bytes). */
196 char buffer[21];
197 _PyBytesWriter writer;
Guido van Rossum343e97f2007-04-09 00:43:24 +0000198
Victor Stinner03dab782015-10-14 00:21:35 +0200199 _PyBytesWriter_Init(&writer);
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000200
Victor Stinner03dab782015-10-14 00:21:35 +0200201 s = _PyBytesWriter_Alloc(&writer, strlen(format));
202 if (s == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 return NULL;
Victor Stinner03dab782015-10-14 00:21:35 +0200204 writer.overallocate = 1;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000205
Victor Stinner03dab782015-10-14 00:21:35 +0200206#define WRITE_BYTES(str) \
207 do { \
208 s = _PyBytesWriter_WriteBytes(&writer, s, (str), strlen(str)); \
209 if (s == NULL) \
210 goto error; \
211 } while (0)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 for (f = format; *f; f++) {
Victor Stinner03dab782015-10-14 00:21:35 +0200214 if (*f != '%') {
215 *s++ = *f;
216 continue;
217 }
218
219 p = f++;
220
221 /* ignore the width (ex: 10 in "%10s") */
222 while (Py_ISDIGIT(*f))
223 f++;
224
225 /* parse the precision (ex: 10 in "%.10s") */
226 prec = 0;
227 if (*f == '.') {
228 f++;
229 for (; Py_ISDIGIT(*f); f++) {
230 prec = (prec * 10) + (*f - '0');
231 }
232 }
233
234 while (*f && *f != '%' && !Py_ISALPHA(*f))
235 f++;
236
237 /* handle the long flag ('l'), but only for %ld and %lu.
238 others can be added when necessary. */
239 longflag = 0;
240 if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) {
241 longflag = 1;
242 ++f;
243 }
244
245 /* handle the size_t flag ('z'). */
246 size_tflag = 0;
247 if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) {
248 size_tflag = 1;
249 ++f;
250 }
251
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700252 /* subtract bytes preallocated for the format string
Victor Stinner03dab782015-10-14 00:21:35 +0200253 (ex: 2 for "%s") */
254 writer.min_size -= (f - p + 1);
255
256 switch (*f) {
257 case 'c':
258 {
259 int c = va_arg(vargs, int);
260 if (c < 0 || c > 255) {
261 PyErr_SetString(PyExc_OverflowError,
262 "PyBytes_FromFormatV(): %c format "
263 "expects an integer in range [0; 255]");
264 goto error;
265 }
266 writer.min_size++;
267 *s++ = (unsigned char)c;
268 break;
269 }
270
271 case 'd':
272 if (longflag)
273 sprintf(buffer, "%ld", va_arg(vargs, long));
274 else if (size_tflag)
275 sprintf(buffer, "%" PY_FORMAT_SIZE_T "d",
276 va_arg(vargs, Py_ssize_t));
277 else
278 sprintf(buffer, "%d", va_arg(vargs, int));
279 assert(strlen(buffer) < sizeof(buffer));
280 WRITE_BYTES(buffer);
281 break;
282
283 case 'u':
284 if (longflag)
285 sprintf(buffer, "%lu",
286 va_arg(vargs, unsigned long));
287 else if (size_tflag)
288 sprintf(buffer, "%" PY_FORMAT_SIZE_T "u",
289 va_arg(vargs, size_t));
290 else
291 sprintf(buffer, "%u",
292 va_arg(vargs, unsigned int));
293 assert(strlen(buffer) < sizeof(buffer));
294 WRITE_BYTES(buffer);
295 break;
296
297 case 'i':
298 sprintf(buffer, "%i", va_arg(vargs, int));
299 assert(strlen(buffer) < sizeof(buffer));
300 WRITE_BYTES(buffer);
301 break;
302
303 case 'x':
304 sprintf(buffer, "%x", va_arg(vargs, int));
305 assert(strlen(buffer) < sizeof(buffer));
306 WRITE_BYTES(buffer);
307 break;
308
309 case 's':
310 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 Py_ssize_t i;
Victor Stinner03dab782015-10-14 00:21:35 +0200312
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200313 p = va_arg(vargs, const char*);
Victor Stinner03dab782015-10-14 00:21:35 +0200314 i = strlen(p);
315 if (prec > 0 && i > prec)
316 i = prec;
317 s = _PyBytesWriter_WriteBytes(&writer, s, p, i);
318 if (s == NULL)
319 goto error;
320 break;
321 }
322
323 case 'p':
324 sprintf(buffer, "%p", va_arg(vargs, void*));
325 assert(strlen(buffer) < sizeof(buffer));
326 /* %p is ill-defined: ensure leading 0x. */
327 if (buffer[1] == 'X')
328 buffer[1] = 'x';
329 else if (buffer[1] != 'x') {
330 memmove(buffer+2, buffer, strlen(buffer)+1);
331 buffer[0] = '0';
332 buffer[1] = 'x';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 }
Victor Stinner03dab782015-10-14 00:21:35 +0200334 WRITE_BYTES(buffer);
335 break;
336
337 case '%':
338 writer.min_size++;
339 *s++ = '%';
340 break;
341
342 default:
343 if (*f == 0) {
344 /* fix min_size if we reached the end of the format string */
345 writer.min_size++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 }
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000347
Victor Stinner03dab782015-10-14 00:21:35 +0200348 /* invalid format string: copy unformatted string and exit */
349 WRITE_BYTES(p);
350 return _PyBytesWriter_Finish(&writer, s);
351 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000353
Victor Stinner03dab782015-10-14 00:21:35 +0200354#undef WRITE_BYTES
355
356 return _PyBytesWriter_Finish(&writer, s);
357
358 error:
359 _PyBytesWriter_Dealloc(&writer);
360 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000361}
362
363PyObject *
364PyBytes_FromFormat(const char *format, ...)
365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 PyObject* ret;
367 va_list vargs;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000368
369#ifdef HAVE_STDARG_PROTOTYPES
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 va_start(vargs, format);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000371#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 va_start(vargs);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000373#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 ret = PyBytes_FromFormatV(format, vargs);
375 va_end(vargs);
376 return ret;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000377}
378
Ethan Furmanb95b5612015-01-23 20:05:18 -0800379/* Helpers for formatstring */
380
381Py_LOCAL_INLINE(PyObject *)
382getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
383{
384 Py_ssize_t argidx = *p_argidx;
385 if (argidx < arglen) {
386 (*p_argidx)++;
387 if (arglen < 0)
388 return args;
389 else
390 return PyTuple_GetItem(args, argidx);
391 }
392 PyErr_SetString(PyExc_TypeError,
393 "not enough arguments for format string");
394 return NULL;
395}
396
397/* Format codes
398 * F_LJUST '-'
399 * F_SIGN '+'
400 * F_BLANK ' '
401 * F_ALT '#'
402 * F_ZERO '0'
403 */
404#define F_LJUST (1<<0)
405#define F_SIGN (1<<1)
406#define F_BLANK (1<<2)
407#define F_ALT (1<<3)
408#define F_ZERO (1<<4)
409
410/* Returns a new reference to a PyBytes object, or NULL on failure. */
411
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200412static char*
413formatfloat(PyObject *v, int flags, int prec, int type,
Victor Stinnerad771582015-10-09 12:38:53 +0200414 PyObject **p_result, _PyBytesWriter *writer, char *str)
Ethan Furmanb95b5612015-01-23 20:05:18 -0800415{
416 char *p;
417 PyObject *result;
418 double x;
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200419 size_t len;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800420
421 x = PyFloat_AsDouble(v);
422 if (x == -1.0 && PyErr_Occurred()) {
423 PyErr_Format(PyExc_TypeError, "float argument required, "
424 "not %.200s", Py_TYPE(v)->tp_name);
425 return NULL;
426 }
427
428 if (prec < 0)
429 prec = 6;
430
431 p = PyOS_double_to_string(x, type, prec,
432 (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL);
433
434 if (p == NULL)
435 return NULL;
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200436
437 len = strlen(p);
438 if (writer != NULL) {
Victor Stinnerad771582015-10-09 12:38:53 +0200439 str = _PyBytesWriter_Prepare(writer, str, len);
440 if (str == NULL)
441 return NULL;
Christian Heimesf051e432016-09-13 20:22:02 +0200442 memcpy(str, p, len);
Victor Stinner71dc3d82016-04-26 12:35:13 +0200443 PyMem_Free(p);
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200444 str += len;
445 return str;
446 }
447
448 result = PyBytes_FromStringAndSize(p, len);
Ethan Furmanb95b5612015-01-23 20:05:18 -0800449 PyMem_Free(p);
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200450 *p_result = result;
Zackery Spytz96c59322018-10-03 00:01:30 -0600451 return result != NULL ? str : NULL;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800452}
453
Serhiy Storchaka2c7b5a92015-03-30 09:19:08 +0300454static PyObject *
455formatlong(PyObject *v, int flags, int prec, int type)
456{
457 PyObject *result, *iobj;
458 if (type == 'i')
459 type = 'd';
460 if (PyLong_Check(v))
461 return _PyUnicode_FormatLong(v, flags & F_ALT, prec, type);
462 if (PyNumber_Check(v)) {
463 /* make sure number is a type of integer for o, x, and X */
464 if (type == 'o' || type == 'x' || type == 'X')
465 iobj = PyNumber_Index(v);
466 else
467 iobj = PyNumber_Long(v);
468 if (iobj == NULL) {
469 if (!PyErr_ExceptionMatches(PyExc_TypeError))
470 return NULL;
471 }
472 else if (!PyLong_Check(iobj))
473 Py_CLEAR(iobj);
474 if (iobj != NULL) {
475 result = _PyUnicode_FormatLong(iobj, flags & F_ALT, prec, type);
476 Py_DECREF(iobj);
477 return result;
478 }
479 }
480 PyErr_Format(PyExc_TypeError,
481 "%%%c format: %s is required, not %.200s", type,
482 (type == 'o' || type == 'x' || type == 'X') ? "an integer"
483 : "a number",
484 Py_TYPE(v)->tp_name);
485 return NULL;
486}
487
488static int
Serhiy Storchakaea5ce5a2015-02-10 23:23:12 +0200489byte_converter(PyObject *arg, char *p)
Ethan Furmanb95b5612015-01-23 20:05:18 -0800490{
Serhiy Storchakadd40fc32016-05-04 22:23:26 +0300491 if (PyBytes_Check(arg) && PyBytes_GET_SIZE(arg) == 1) {
Serhiy Storchakaea5ce5a2015-02-10 23:23:12 +0200492 *p = PyBytes_AS_STRING(arg)[0];
493 return 1;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800494 }
Serhiy Storchakadd40fc32016-05-04 22:23:26 +0300495 else if (PyByteArray_Check(arg) && PyByteArray_GET_SIZE(arg) == 1) {
Serhiy Storchakaea5ce5a2015-02-10 23:23:12 +0200496 *p = PyByteArray_AS_STRING(arg)[0];
497 return 1;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800498 }
499 else {
Serhiy Storchaka2c7b5a92015-03-30 09:19:08 +0300500 PyObject *iobj;
501 long ival;
502 int overflow;
503 /* make sure number is a type of integer */
504 if (PyLong_Check(arg)) {
505 ival = PyLong_AsLongAndOverflow(arg, &overflow);
506 }
507 else {
508 iobj = PyNumber_Index(arg);
509 if (iobj == NULL) {
510 if (!PyErr_ExceptionMatches(PyExc_TypeError))
511 return 0;
512 goto onError;
513 }
514 ival = PyLong_AsLongAndOverflow(iobj, &overflow);
515 Py_DECREF(iobj);
516 }
Serhiy Storchaka41525e32015-04-03 20:53:46 +0300517 if (!overflow && ival == -1 && PyErr_Occurred())
518 goto onError;
519 if (overflow || !(0 <= ival && ival <= 255)) {
520 PyErr_SetString(PyExc_OverflowError,
521 "%c arg not in range(256)");
522 return 0;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800523 }
Serhiy Storchaka41525e32015-04-03 20:53:46 +0300524 *p = (char)ival;
525 return 1;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800526 }
Serhiy Storchaka2c7b5a92015-03-30 09:19:08 +0300527 onError:
Serhiy Storchakaea5ce5a2015-02-10 23:23:12 +0200528 PyErr_SetString(PyExc_TypeError,
529 "%c requires an integer in range(256) or a single byte");
530 return 0;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800531}
532
Xiang Zhang7e2a54c2017-03-14 15:07:15 +0800533static PyObject *_PyBytes_FromBuffer(PyObject *x);
534
Ethan Furmanb95b5612015-01-23 20:05:18 -0800535static PyObject *
Serhiy Storchakaea5ce5a2015-02-10 23:23:12 +0200536format_obj(PyObject *v, const char **pbuf, Py_ssize_t *plen)
Ethan Furmanb95b5612015-01-23 20:05:18 -0800537{
Serhiy Storchakaea5ce5a2015-02-10 23:23:12 +0200538 PyObject *func, *result;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800539 _Py_IDENTIFIER(__bytes__);
Ethan Furmanb95b5612015-01-23 20:05:18 -0800540 /* is it a bytes object? */
541 if (PyBytes_Check(v)) {
Serhiy Storchakaea5ce5a2015-02-10 23:23:12 +0200542 *pbuf = PyBytes_AS_STRING(v);
543 *plen = PyBytes_GET_SIZE(v);
Ethan Furmanb95b5612015-01-23 20:05:18 -0800544 Py_INCREF(v);
Serhiy Storchakaea5ce5a2015-02-10 23:23:12 +0200545 return v;
546 }
547 if (PyByteArray_Check(v)) {
548 *pbuf = PyByteArray_AS_STRING(v);
549 *plen = PyByteArray_GET_SIZE(v);
550 Py_INCREF(v);
551 return v;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800552 }
553 /* does it support __bytes__? */
554 func = _PyObject_LookupSpecial(v, &PyId___bytes__);
555 if (func != NULL) {
Victor Stinnerf17c3de2016-12-06 18:46:19 +0100556 result = _PyObject_CallNoArg(func);
Ethan Furmanb95b5612015-01-23 20:05:18 -0800557 Py_DECREF(func);
558 if (result == NULL)
559 return NULL;
560 if (!PyBytes_Check(result)) {
561 PyErr_Format(PyExc_TypeError,
562 "__bytes__ returned non-bytes (type %.200s)",
563 Py_TYPE(result)->tp_name);
564 Py_DECREF(result);
565 return NULL;
566 }
Serhiy Storchakaea5ce5a2015-02-10 23:23:12 +0200567 *pbuf = PyBytes_AS_STRING(result);
568 *plen = PyBytes_GET_SIZE(result);
Ethan Furmanb95b5612015-01-23 20:05:18 -0800569 return result;
570 }
Xiang Zhang7e2a54c2017-03-14 15:07:15 +0800571 /* does it support buffer protocol? */
572 if (PyObject_CheckBuffer(v)) {
573 /* maybe we can avoid making a copy of the buffer object here? */
574 result = _PyBytes_FromBuffer(v);
575 if (result == NULL)
576 return NULL;
577 *pbuf = PyBytes_AS_STRING(result);
578 *plen = PyBytes_GET_SIZE(result);
579 return result;
580 }
Ethan Furmanb95b5612015-01-23 20:05:18 -0800581 PyErr_Format(PyExc_TypeError,
Xiang Zhang7e2a54c2017-03-14 15:07:15 +0800582 "%%b requires a bytes-like object, "
583 "or an object that implements __bytes__, not '%.100s'",
Ethan Furmanb95b5612015-01-23 20:05:18 -0800584 Py_TYPE(v)->tp_name);
585 return NULL;
586}
587
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200588/* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) */
Ethan Furmanb95b5612015-01-23 20:05:18 -0800589
590PyObject *
Victor Stinner772b2b02015-10-14 09:56:53 +0200591_PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
592 PyObject *args, int use_bytearray)
Ethan Furmanb95b5612015-01-23 20:05:18 -0800593{
Victor Stinner772b2b02015-10-14 09:56:53 +0200594 const char *fmt;
595 char *res;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800596 Py_ssize_t arglen, argidx;
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200597 Py_ssize_t fmtcnt;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800598 int args_owned = 0;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800599 PyObject *dict = NULL;
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200600 _PyBytesWriter writer;
601
Victor Stinner772b2b02015-10-14 09:56:53 +0200602 if (args == NULL) {
Ethan Furmanb95b5612015-01-23 20:05:18 -0800603 PyErr_BadInternalCall();
604 return NULL;
605 }
Victor Stinner772b2b02015-10-14 09:56:53 +0200606 fmt = format;
607 fmtcnt = format_len;
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200608
609 _PyBytesWriter_Init(&writer);
Victor Stinner772b2b02015-10-14 09:56:53 +0200610 writer.use_bytearray = use_bytearray;
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200611
612 res = _PyBytesWriter_Alloc(&writer, fmtcnt);
613 if (res == NULL)
Ethan Furmanb95b5612015-01-23 20:05:18 -0800614 return NULL;
Victor Stinner772b2b02015-10-14 09:56:53 +0200615 if (!use_bytearray)
616 writer.overallocate = 1;
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200617
Ethan Furmanb95b5612015-01-23 20:05:18 -0800618 if (PyTuple_Check(args)) {
619 arglen = PyTuple_GET_SIZE(args);
620 argidx = 0;
621 }
622 else {
623 arglen = -1;
624 argidx = -2;
625 }
626 if (Py_TYPE(args)->tp_as_mapping && Py_TYPE(args)->tp_as_mapping->mp_subscript &&
627 !PyTuple_Check(args) && !PyBytes_Check(args) && !PyUnicode_Check(args) &&
628 !PyByteArray_Check(args)) {
629 dict = args;
630 }
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200631
Ethan Furmanb95b5612015-01-23 20:05:18 -0800632 while (--fmtcnt >= 0) {
633 if (*fmt != '%') {
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200634 Py_ssize_t len;
635 char *pos;
636
Xiang Zhangb76ad512017-03-06 17:17:05 +0800637 pos = (char *)memchr(fmt + 1, '%', fmtcnt);
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200638 if (pos != NULL)
639 len = pos - fmt;
Victor Stinner772b2b02015-10-14 09:56:53 +0200640 else
Xiang Zhangb76ad512017-03-06 17:17:05 +0800641 len = fmtcnt + 1;
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200642 assert(len != 0);
643
Christian Heimesf051e432016-09-13 20:22:02 +0200644 memcpy(res, fmt, len);
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200645 res += len;
646 fmt += len;
647 fmtcnt -= (len - 1);
Ethan Furmanb95b5612015-01-23 20:05:18 -0800648 }
649 else {
650 /* Got a format specifier */
651 int flags = 0;
652 Py_ssize_t width = -1;
653 int prec = -1;
654 int c = '\0';
655 int fill;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800656 PyObject *v = NULL;
657 PyObject *temp = NULL;
Serhiy Storchakaea5ce5a2015-02-10 23:23:12 +0200658 const char *pbuf = NULL;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800659 int sign;
Serhiy Storchakaea5ce5a2015-02-10 23:23:12 +0200660 Py_ssize_t len = 0;
661 char onechar; /* For byte_converter() */
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200662 Py_ssize_t alloc;
663#ifdef Py_DEBUG
664 char *before;
665#endif
Ethan Furmanb95b5612015-01-23 20:05:18 -0800666
Ethan Furmanb95b5612015-01-23 20:05:18 -0800667 fmt++;
Serhiy Storchaka9f8ad3f2017-03-08 05:51:19 +0200668 if (*fmt == '%') {
669 *res++ = '%';
670 fmt++;
671 fmtcnt--;
672 continue;
673 }
Ethan Furmanb95b5612015-01-23 20:05:18 -0800674 if (*fmt == '(') {
Victor Stinner772b2b02015-10-14 09:56:53 +0200675 const char *keystart;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800676 Py_ssize_t keylen;
677 PyObject *key;
678 int pcount = 1;
679
680 if (dict == NULL) {
681 PyErr_SetString(PyExc_TypeError,
682 "format requires a mapping");
683 goto error;
684 }
685 ++fmt;
686 --fmtcnt;
687 keystart = fmt;
688 /* Skip over balanced parentheses */
689 while (pcount > 0 && --fmtcnt >= 0) {
690 if (*fmt == ')')
691 --pcount;
692 else if (*fmt == '(')
693 ++pcount;
694 fmt++;
695 }
696 keylen = fmt - keystart - 1;
697 if (fmtcnt < 0 || pcount > 0) {
698 PyErr_SetString(PyExc_ValueError,
699 "incomplete format key");
700 goto error;
701 }
702 key = PyBytes_FromStringAndSize(keystart,
703 keylen);
704 if (key == NULL)
705 goto error;
706 if (args_owned) {
707 Py_DECREF(args);
708 args_owned = 0;
709 }
710 args = PyObject_GetItem(dict, key);
711 Py_DECREF(key);
712 if (args == NULL) {
713 goto error;
714 }
715 args_owned = 1;
716 arglen = -1;
717 argidx = -2;
718 }
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200719
720 /* Parse flags. Example: "%+i" => flags=F_SIGN. */
Ethan Furmanb95b5612015-01-23 20:05:18 -0800721 while (--fmtcnt >= 0) {
722 switch (c = *fmt++) {
723 case '-': flags |= F_LJUST; continue;
724 case '+': flags |= F_SIGN; continue;
725 case ' ': flags |= F_BLANK; continue;
726 case '#': flags |= F_ALT; continue;
727 case '0': flags |= F_ZERO; continue;
728 }
729 break;
730 }
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200731
732 /* Parse width. Example: "%10s" => width=10 */
Ethan Furmanb95b5612015-01-23 20:05:18 -0800733 if (c == '*') {
734 v = getnextarg(args, arglen, &argidx);
735 if (v == NULL)
736 goto error;
737 if (!PyLong_Check(v)) {
738 PyErr_SetString(PyExc_TypeError,
739 "* wants int");
740 goto error;
741 }
742 width = PyLong_AsSsize_t(v);
743 if (width == -1 && PyErr_Occurred())
744 goto error;
745 if (width < 0) {
746 flags |= F_LJUST;
747 width = -width;
748 }
749 if (--fmtcnt >= 0)
750 c = *fmt++;
751 }
752 else if (c >= 0 && isdigit(c)) {
753 width = c - '0';
754 while (--fmtcnt >= 0) {
755 c = Py_CHARMASK(*fmt++);
756 if (!isdigit(c))
757 break;
758 if (width > (PY_SSIZE_T_MAX - ((int)c - '0')) / 10) {
759 PyErr_SetString(
760 PyExc_ValueError,
761 "width too big");
762 goto error;
763 }
764 width = width*10 + (c - '0');
765 }
766 }
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200767
768 /* Parse precision. Example: "%.3f" => prec=3 */
Ethan Furmanb95b5612015-01-23 20:05:18 -0800769 if (c == '.') {
770 prec = 0;
771 if (--fmtcnt >= 0)
772 c = *fmt++;
773 if (c == '*') {
774 v = getnextarg(args, arglen, &argidx);
775 if (v == NULL)
776 goto error;
777 if (!PyLong_Check(v)) {
778 PyErr_SetString(
779 PyExc_TypeError,
780 "* wants int");
781 goto error;
782 }
Serhiy Storchaka26861b02015-02-16 20:52:17 +0200783 prec = _PyLong_AsInt(v);
Ethan Furmanb95b5612015-01-23 20:05:18 -0800784 if (prec == -1 && PyErr_Occurred())
785 goto error;
786 if (prec < 0)
787 prec = 0;
788 if (--fmtcnt >= 0)
789 c = *fmt++;
790 }
791 else if (c >= 0 && isdigit(c)) {
792 prec = c - '0';
793 while (--fmtcnt >= 0) {
794 c = Py_CHARMASK(*fmt++);
795 if (!isdigit(c))
796 break;
797 if (prec > (INT_MAX - ((int)c - '0')) / 10) {
798 PyErr_SetString(
799 PyExc_ValueError,
800 "prec too big");
801 goto error;
802 }
803 prec = prec*10 + (c - '0');
804 }
805 }
806 } /* prec */
807 if (fmtcnt >= 0) {
808 if (c == 'h' || c == 'l' || c == 'L') {
809 if (--fmtcnt >= 0)
810 c = *fmt++;
811 }
812 }
813 if (fmtcnt < 0) {
814 PyErr_SetString(PyExc_ValueError,
815 "incomplete format");
816 goto error;
817 }
Serhiy Storchaka9f8ad3f2017-03-08 05:51:19 +0200818 v = getnextarg(args, arglen, &argidx);
819 if (v == NULL)
820 goto error;
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200821
Alexey Izbyshevccd99752018-08-23 10:50:52 +0300822 if (fmtcnt == 0) {
823 /* last write: disable writer overallocation */
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200824 writer.overallocate = 0;
825 }
826
Ethan Furmanb95b5612015-01-23 20:05:18 -0800827 sign = 0;
828 fill = ' ';
829 switch (c) {
Ethan Furman62e977f2015-03-11 08:17:00 -0700830 case 'r':
831 // %r is only for 2/3 code; 3 only code should use %a
Ethan Furmanb95b5612015-01-23 20:05:18 -0800832 case 'a':
Serhiy Storchakaea5ce5a2015-02-10 23:23:12 +0200833 temp = PyObject_ASCII(v);
Ethan Furmanb95b5612015-01-23 20:05:18 -0800834 if (temp == NULL)
835 goto error;
Serhiy Storchakaea5ce5a2015-02-10 23:23:12 +0200836 assert(PyUnicode_IS_ASCII(temp));
837 pbuf = (const char *)PyUnicode_1BYTE_DATA(temp);
838 len = PyUnicode_GET_LENGTH(temp);
Ethan Furmanb95b5612015-01-23 20:05:18 -0800839 if (prec >= 0 && len > prec)
840 len = prec;
841 break;
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200842
Ethan Furmanb95b5612015-01-23 20:05:18 -0800843 case 's':
844 // %s is only for 2/3 code; 3 only code should use %b
845 case 'b':
Serhiy Storchakaea5ce5a2015-02-10 23:23:12 +0200846 temp = format_obj(v, &pbuf, &len);
Ethan Furmanb95b5612015-01-23 20:05:18 -0800847 if (temp == NULL)
848 goto error;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800849 if (prec >= 0 && len > prec)
850 len = prec;
851 break;
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200852
Ethan Furmanb95b5612015-01-23 20:05:18 -0800853 case 'i':
854 case 'd':
855 case 'u':
856 case 'o':
857 case 'x':
858 case 'X':
Victor Stinnerbe75b8c2015-10-09 22:43:24 +0200859 if (PyLong_CheckExact(v)
Victor Stinner0cdad1e2015-10-09 22:50:36 +0200860 && width == -1 && prec == -1
861 && !(flags & (F_SIGN | F_BLANK))
862 && c != 'X')
Victor Stinnerbe75b8c2015-10-09 22:43:24 +0200863 {
864 /* Fast path */
865 int alternate = flags & F_ALT;
866 int base;
867
868 switch(c)
869 {
870 default:
Barry Warsawb2e57942017-09-14 18:13:16 -0700871 Py_UNREACHABLE();
Victor Stinnerbe75b8c2015-10-09 22:43:24 +0200872 case 'd':
873 case 'i':
874 case 'u':
875 base = 10;
876 break;
877 case 'o':
878 base = 8;
879 break;
880 case 'x':
881 case 'X':
882 base = 16;
883 break;
884 }
885
886 /* Fast path */
Victor Stinner0cdad1e2015-10-09 22:50:36 +0200887 writer.min_size -= 2; /* size preallocated for "%d" */
Victor Stinnerbe75b8c2015-10-09 22:43:24 +0200888 res = _PyLong_FormatBytesWriter(&writer, res,
889 v, base, alternate);
890 if (res == NULL)
891 goto error;
892 continue;
893 }
894
Serhiy Storchaka2c7b5a92015-03-30 09:19:08 +0300895 temp = formatlong(v, flags, prec, c);
Serhiy Storchakaea5ce5a2015-02-10 23:23:12 +0200896 if (!temp)
897 goto error;
898 assert(PyUnicode_IS_ASCII(temp));
899 pbuf = (const char *)PyUnicode_1BYTE_DATA(temp);
900 len = PyUnicode_GET_LENGTH(temp);
901 sign = 1;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800902 if (flags & F_ZERO)
903 fill = '0';
904 break;
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200905
Ethan Furmanb95b5612015-01-23 20:05:18 -0800906 case 'e':
907 case 'E':
908 case 'f':
909 case 'F':
910 case 'g':
911 case 'G':
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200912 if (width == -1 && prec == -1
913 && !(flags & (F_SIGN | F_BLANK)))
914 {
915 /* Fast path */
Victor Stinner0cdad1e2015-10-09 22:50:36 +0200916 writer.min_size -= 2; /* size preallocated for "%f" */
Victor Stinnerad771582015-10-09 12:38:53 +0200917 res = formatfloat(v, flags, prec, c, NULL, &writer, res);
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200918 if (res == NULL)
919 goto error;
920 continue;
921 }
922
Victor Stinnerad771582015-10-09 12:38:53 +0200923 if (!formatfloat(v, flags, prec, c, &temp, NULL, res))
Ethan Furmanb95b5612015-01-23 20:05:18 -0800924 goto error;
925 pbuf = PyBytes_AS_STRING(temp);
926 len = PyBytes_GET_SIZE(temp);
927 sign = 1;
928 if (flags & F_ZERO)
929 fill = '0';
930 break;
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200931
Ethan Furmanb95b5612015-01-23 20:05:18 -0800932 case 'c':
Serhiy Storchakaea5ce5a2015-02-10 23:23:12 +0200933 pbuf = &onechar;
934 len = byte_converter(v, &onechar);
935 if (!len)
Ethan Furmanb95b5612015-01-23 20:05:18 -0800936 goto error;
Victor Stinner0cdad1e2015-10-09 22:50:36 +0200937 if (width == -1) {
938 /* Fast path */
939 *res++ = onechar;
940 continue;
941 }
Ethan Furmanb95b5612015-01-23 20:05:18 -0800942 break;
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200943
Ethan Furmanb95b5612015-01-23 20:05:18 -0800944 default:
945 PyErr_Format(PyExc_ValueError,
946 "unsupported format character '%c' (0x%x) "
947 "at index %zd",
948 c, c,
Victor Stinner772b2b02015-10-14 09:56:53 +0200949 (Py_ssize_t)(fmt - 1 - format));
Ethan Furmanb95b5612015-01-23 20:05:18 -0800950 goto error;
951 }
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200952
Ethan Furmanb95b5612015-01-23 20:05:18 -0800953 if (sign) {
954 if (*pbuf == '-' || *pbuf == '+') {
955 sign = *pbuf++;
956 len--;
957 }
958 else if (flags & F_SIGN)
959 sign = '+';
960 else if (flags & F_BLANK)
961 sign = ' ';
962 else
963 sign = 0;
964 }
965 if (width < len)
966 width = len;
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200967
968 alloc = width;
969 if (sign != 0 && len == width)
970 alloc++;
Victor Stinner0cdad1e2015-10-09 22:50:36 +0200971 /* 2: size preallocated for %s */
972 if (alloc > 2) {
973 res = _PyBytesWriter_Prepare(&writer, res, alloc - 2);
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200974 if (res == NULL)
975 goto error;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800976 }
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200977#ifdef Py_DEBUG
978 before = res;
979#endif
980
981 /* Write the sign if needed */
Ethan Furmanb95b5612015-01-23 20:05:18 -0800982 if (sign) {
983 if (fill != ' ')
984 *res++ = sign;
Ethan Furmanb95b5612015-01-23 20:05:18 -0800985 if (width > len)
986 width--;
987 }
Victor Stinnerfa7762e2015-10-09 11:48:06 +0200988
989 /* Write the numeric prefix for "x", "X" and "o" formats
990 if the alternate form is used.
991 For example, write "0x" for the "%#x" format. */
Serhiy Storchakab1a16192016-12-17 21:48:03 +0200992 if ((flags & F_ALT) && (c == 'o' || c == 'x' || c == 'X')) {
Ethan Furmanb95b5612015-01-23 20:05:18 -0800993 assert(pbuf[0] == '0');
994 assert(pbuf[1] == c);
995 if (fill != ' ') {
996 *res++ = *pbuf++;
997 *res++ = *pbuf++;
998 }
Ethan Furmanb95b5612015-01-23 20:05:18 -0800999 width -= 2;
1000 if (width < 0)
1001 width = 0;
1002 len -= 2;
1003 }
Victor Stinnerfa7762e2015-10-09 11:48:06 +02001004
1005 /* Pad left with the fill character if needed */
Ethan Furmanb95b5612015-01-23 20:05:18 -08001006 if (width > len && !(flags & F_LJUST)) {
Victor Stinnerfa7762e2015-10-09 11:48:06 +02001007 memset(res, fill, width - len);
1008 res += (width - len);
1009 width = len;
Ethan Furmanb95b5612015-01-23 20:05:18 -08001010 }
Victor Stinnerfa7762e2015-10-09 11:48:06 +02001011
1012 /* If padding with spaces: write sign if needed and/or numeric
1013 prefix if the alternate form is used */
Ethan Furmanb95b5612015-01-23 20:05:18 -08001014 if (fill == ' ') {
1015 if (sign)
1016 *res++ = sign;
Serhiy Storchakab1a16192016-12-17 21:48:03 +02001017 if ((flags & F_ALT) && (c == 'o' || c == 'x' || c == 'X')) {
Ethan Furmanb95b5612015-01-23 20:05:18 -08001018 assert(pbuf[0] == '0');
1019 assert(pbuf[1] == c);
1020 *res++ = *pbuf++;
1021 *res++ = *pbuf++;
1022 }
1023 }
Victor Stinnerfa7762e2015-10-09 11:48:06 +02001024
1025 /* Copy bytes */
Christian Heimesf051e432016-09-13 20:22:02 +02001026 memcpy(res, pbuf, len);
Ethan Furmanb95b5612015-01-23 20:05:18 -08001027 res += len;
Victor Stinnerfa7762e2015-10-09 11:48:06 +02001028
1029 /* Pad right with the fill character if needed */
1030 if (width > len) {
1031 memset(res, ' ', width - len);
1032 res += (width - len);
Ethan Furmanb95b5612015-01-23 20:05:18 -08001033 }
Victor Stinnerfa7762e2015-10-09 11:48:06 +02001034
Serhiy Storchaka9f8ad3f2017-03-08 05:51:19 +02001035 if (dict && (argidx < arglen)) {
Ethan Furmanb95b5612015-01-23 20:05:18 -08001036 PyErr_SetString(PyExc_TypeError,
1037 "not all arguments converted during bytes formatting");
Ethan Furmanb95b5612015-01-23 20:05:18 -08001038 Py_XDECREF(temp);
1039 goto error;
1040 }
Ethan Furmanb95b5612015-01-23 20:05:18 -08001041 Py_XDECREF(temp);
Victor Stinnerfa7762e2015-10-09 11:48:06 +02001042
1043#ifdef Py_DEBUG
1044 /* check that we computed the exact size for this write */
1045 assert((res - before) == alloc);
1046#endif
Ethan Furmanb95b5612015-01-23 20:05:18 -08001047 } /* '%' */
Victor Stinnerfa7762e2015-10-09 11:48:06 +02001048
1049 /* If overallocation was disabled, ensure that it was the last
1050 write. Otherwise, we missed an optimization */
Alexey Izbyshevccd99752018-08-23 10:50:52 +03001051 assert(writer.overallocate || fmtcnt == 0 || use_bytearray);
Ethan Furmanb95b5612015-01-23 20:05:18 -08001052 } /* until end */
Victor Stinnerfa7762e2015-10-09 11:48:06 +02001053
Ethan Furmanb95b5612015-01-23 20:05:18 -08001054 if (argidx < arglen && !dict) {
1055 PyErr_SetString(PyExc_TypeError,
1056 "not all arguments converted during bytes formatting");
1057 goto error;
1058 }
Victor Stinnerfa7762e2015-10-09 11:48:06 +02001059
Ethan Furmanb95b5612015-01-23 20:05:18 -08001060 if (args_owned) {
1061 Py_DECREF(args);
1062 }
Victor Stinnerfa7762e2015-10-09 11:48:06 +02001063 return _PyBytesWriter_Finish(&writer, res);
Ethan Furmanb95b5612015-01-23 20:05:18 -08001064
1065 error:
Victor Stinnerfa7762e2015-10-09 11:48:06 +02001066 _PyBytesWriter_Dealloc(&writer);
Ethan Furmanb95b5612015-01-23 20:05:18 -08001067 if (args_owned) {
1068 Py_DECREF(args);
1069 }
1070 return NULL;
1071}
1072
1073/* =-= */
1074
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001075static void
Benjamin Peterson80688ef2009-04-18 15:17:02 +00001076bytes_dealloc(PyObject *op)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 Py_TYPE(op)->tp_free(op);
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001079}
1080
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001081/* Unescape a backslash-escaped string. If unicode is non-zero,
1082 the string is a u-literal. If recode_encoding is non-zero,
1083 the string is UTF-8 encoded and should be re-encoded in the
1084 specified encoding. */
1085
Victor Stinner2ec80632015-10-14 13:32:13 +02001086static char *
1087_PyBytes_DecodeEscapeRecode(const char **s, const char *end,
1088 const char *errors, const char *recode_encoding,
1089 _PyBytesWriter *writer, char *p)
1090{
1091 PyObject *u, *w;
1092 const char* t;
1093
1094 t = *s;
1095 /* Decode non-ASCII bytes as UTF-8. */
1096 while (t < end && (*t & 0x80))
1097 t++;
1098 u = PyUnicode_DecodeUTF8(*s, t - *s, errors);
1099 if (u == NULL)
1100 return NULL;
1101
1102 /* Recode them in target encoding. */
1103 w = PyUnicode_AsEncodedString(u, recode_encoding, errors);
1104 Py_DECREF(u);
1105 if (w == NULL)
1106 return NULL;
1107 assert(PyBytes_Check(w));
1108
1109 /* Append bytes to output buffer. */
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001110 writer->min_size--; /* subtract 1 preallocated byte */
Victor Stinner2ec80632015-10-14 13:32:13 +02001111 p = _PyBytesWriter_WriteBytes(writer, p,
1112 PyBytes_AS_STRING(w),
1113 PyBytes_GET_SIZE(w));
1114 Py_DECREF(w);
1115 if (p == NULL)
1116 return NULL;
1117
1118 *s = t;
1119 return p;
1120}
1121
Eric V. Smith42454af2016-10-31 09:22:08 -04001122PyObject *_PyBytes_DecodeEscape(const char *s,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 Py_ssize_t len,
1124 const char *errors,
1125 Py_ssize_t unicode,
Eric V. Smith42454af2016-10-31 09:22:08 -04001126 const char *recode_encoding,
1127 const char **first_invalid_escape)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 int c;
Victor Stinner2ec80632015-10-14 13:32:13 +02001130 char *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 const char *end;
Victor Stinner2ec80632015-10-14 13:32:13 +02001132 _PyBytesWriter writer;
1133
1134 _PyBytesWriter_Init(&writer);
1135
1136 p = _PyBytesWriter_Alloc(&writer, len);
1137 if (p == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 return NULL;
Victor Stinner2ec80632015-10-14 13:32:13 +02001139 writer.overallocate = 1;
1140
Eric V. Smith42454af2016-10-31 09:22:08 -04001141 *first_invalid_escape = NULL;
1142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 end = s + len;
1144 while (s < end) {
1145 if (*s != '\\') {
1146 non_esc:
Victor Stinner2ec80632015-10-14 13:32:13 +02001147 if (!(recode_encoding && (*s & 0x80))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 *p++ = *s++;
1149 }
Victor Stinner2ec80632015-10-14 13:32:13 +02001150 else {
1151 /* non-ASCII character and need to recode */
1152 p = _PyBytes_DecodeEscapeRecode(&s, end,
1153 errors, recode_encoding,
1154 &writer, p);
1155 if (p == NULL)
1156 goto failed;
1157 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 continue;
1159 }
Victor Stinner2ec80632015-10-14 13:32:13 +02001160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 s++;
Victor Stinner2ec80632015-10-14 13:32:13 +02001162 if (s == end) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 PyErr_SetString(PyExc_ValueError,
1164 "Trailing \\ in string");
1165 goto failed;
1166 }
Victor Stinner2ec80632015-10-14 13:32:13 +02001167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 switch (*s++) {
1169 /* XXX This assumes ASCII! */
1170 case '\n': break;
1171 case '\\': *p++ = '\\'; break;
1172 case '\'': *p++ = '\''; break;
1173 case '\"': *p++ = '\"'; break;
1174 case 'b': *p++ = '\b'; break;
1175 case 'f': *p++ = '\014'; break; /* FF */
1176 case 't': *p++ = '\t'; break;
1177 case 'n': *p++ = '\n'; break;
1178 case 'r': *p++ = '\r'; break;
1179 case 'v': *p++ = '\013'; break; /* VT */
1180 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
1181 case '0': case '1': case '2': case '3':
1182 case '4': case '5': case '6': case '7':
1183 c = s[-1] - '0';
1184 if (s < end && '0' <= *s && *s <= '7') {
1185 c = (c<<3) + *s++ - '0';
1186 if (s < end && '0' <= *s && *s <= '7')
1187 c = (c<<3) + *s++ - '0';
1188 }
1189 *p++ = c;
1190 break;
1191 case 'x':
Victor Stinner2ec80632015-10-14 13:32:13 +02001192 if (s+1 < end) {
1193 int digit1, digit2;
1194 digit1 = _PyLong_DigitValue[Py_CHARMASK(s[0])];
1195 digit2 = _PyLong_DigitValue[Py_CHARMASK(s[1])];
1196 if (digit1 < 16 && digit2 < 16) {
1197 *p++ = (unsigned char)((digit1 << 4) + digit2);
1198 s += 2;
1199 break;
1200 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 }
Victor Stinner2ec80632015-10-14 13:32:13 +02001202 /* invalid hexadecimal digits */
1203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 if (!errors || strcmp(errors, "strict") == 0) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02001205 PyErr_Format(PyExc_ValueError,
1206 "invalid \\x escape at position %d",
Serhiy Storchaka801d9552013-02-10 17:42:01 +02001207 s - 2 - (end - len));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 goto failed;
1209 }
1210 if (strcmp(errors, "replace") == 0) {
1211 *p++ = '?';
1212 } else if (strcmp(errors, "ignore") == 0)
1213 /* do nothing */;
1214 else {
1215 PyErr_Format(PyExc_ValueError,
1216 "decoding error; unknown "
1217 "error handling code: %.400s",
1218 errors);
1219 goto failed;
1220 }
Serhiy Storchakaace3ad32013-01-25 23:31:43 +02001221 /* skip \x */
1222 if (s < end && Py_ISXDIGIT(s[0]))
1223 s++; /* and a hexdigit */
1224 break;
Victor Stinner2ec80632015-10-14 13:32:13 +02001225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 default:
Eric V. Smith42454af2016-10-31 09:22:08 -04001227 if (*first_invalid_escape == NULL) {
1228 *first_invalid_escape = s-1; /* Back up one char, since we've
1229 already incremented s. */
1230 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 *p++ = '\\';
Eric V. Smith42454af2016-10-31 09:22:08 -04001232 s--;
Ezio Melotti42da6632011-03-15 05:18:48 +02001233 goto non_esc; /* an arbitrary number of unescaped
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 UTF-8 bytes may follow. */
1235 }
1236 }
Victor Stinner2ec80632015-10-14 13:32:13 +02001237
1238 return _PyBytesWriter_Finish(&writer, p);
1239
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001240 failed:
Victor Stinner2ec80632015-10-14 13:32:13 +02001241 _PyBytesWriter_Dealloc(&writer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001243}
1244
Eric V. Smith42454af2016-10-31 09:22:08 -04001245PyObject *PyBytes_DecodeEscape(const char *s,
1246 Py_ssize_t len,
1247 const char *errors,
1248 Py_ssize_t unicode,
1249 const char *recode_encoding)
1250{
1251 const char* first_invalid_escape;
1252 PyObject *result = _PyBytes_DecodeEscape(s, len, errors, unicode,
1253 recode_encoding,
1254 &first_invalid_escape);
1255 if (result == NULL)
1256 return NULL;
1257 if (first_invalid_escape != NULL) {
1258 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1259 "invalid escape sequence '\\%c'",
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03001260 (unsigned char)*first_invalid_escape) < 0) {
Eric V. Smith42454af2016-10-31 09:22:08 -04001261 Py_DECREF(result);
1262 return NULL;
1263 }
1264 }
1265 return result;
1266
1267}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001268/* -------------------------------------------------------------------- */
1269/* object api */
1270
1271Py_ssize_t
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001272PyBytes_Size(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001273{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 if (!PyBytes_Check(op)) {
1275 PyErr_Format(PyExc_TypeError,
1276 "expected bytes, %.200s found", Py_TYPE(op)->tp_name);
1277 return -1;
1278 }
1279 return Py_SIZE(op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001280}
1281
1282char *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001283PyBytes_AsString(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 if (!PyBytes_Check(op)) {
1286 PyErr_Format(PyExc_TypeError,
1287 "expected bytes, %.200s found", Py_TYPE(op)->tp_name);
1288 return NULL;
1289 }
1290 return ((PyBytesObject *)op)->ob_sval;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001291}
1292
1293int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001294PyBytes_AsStringAndSize(PyObject *obj,
1295 char **s,
1296 Py_ssize_t *len)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 if (s == NULL) {
1299 PyErr_BadInternalCall();
1300 return -1;
1301 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 if (!PyBytes_Check(obj)) {
1304 PyErr_Format(PyExc_TypeError,
1305 "expected bytes, %.200s found", Py_TYPE(obj)->tp_name);
1306 return -1;
1307 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 *s = PyBytes_AS_STRING(obj);
1310 if (len != NULL)
1311 *len = PyBytes_GET_SIZE(obj);
1312 else if (strlen(*s) != (size_t)PyBytes_GET_SIZE(obj)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001313 PyErr_SetString(PyExc_ValueError,
1314 "embedded null byte");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 return -1;
1316 }
1317 return 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001318}
Neal Norwitz6968b052007-02-27 19:02:19 +00001319
1320/* -------------------------------------------------------------------- */
1321/* Methods */
1322
Eric Smith0923d1d2009-04-16 20:16:10 +00001323#include "stringlib/stringdefs.h"
Neal Norwitz6968b052007-02-27 19:02:19 +00001324
1325#include "stringlib/fastsearch.h"
1326#include "stringlib/count.h"
1327#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001328#include "stringlib/join.h"
Neal Norwitz6968b052007-02-27 19:02:19 +00001329#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001330#include "stringlib/split.h"
Gregory P. Smith60d241f2007-10-16 06:31:30 +00001331#include "stringlib/ctype.h"
Neal Norwitz6968b052007-02-27 19:02:19 +00001332
Eric Smith0f78bff2009-11-30 01:01:42 +00001333#include "stringlib/transmogrify.h"
Neal Norwitz6968b052007-02-27 19:02:19 +00001334
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001335PyObject *
1336PyBytes_Repr(PyObject *obj, int smartquotes)
Neal Norwitz6968b052007-02-27 19:02:19 +00001337{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001338 PyBytesObject* op = (PyBytesObject*) obj;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001339 Py_ssize_t i, length = Py_SIZE(op);
Benjamin Petersond48bc942014-09-29 19:12:26 -04001340 Py_ssize_t newsize, squotes, dquotes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 PyObject *v;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001342 unsigned char quote, *s, *p;
1343
1344 /* Compute size of output string */
1345 squotes = dquotes = 0;
1346 newsize = 3; /* b'' */
1347 s = (unsigned char*)op->ob_sval;
1348 for (i = 0; i < length; i++) {
Benjamin Peterson42ff1052014-09-29 19:01:18 -04001349 Py_ssize_t incr = 1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001350 switch(s[i]) {
Benjamin Peterson42ff1052014-09-29 19:01:18 -04001351 case '\'': squotes++; break;
1352 case '"': dquotes++; break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001353 case '\\': case '\t': case '\n': case '\r':
Benjamin Peterson42ff1052014-09-29 19:01:18 -04001354 incr = 2; break; /* \C */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001355 default:
1356 if (s[i] < ' ' || s[i] >= 0x7f)
Benjamin Peterson42ff1052014-09-29 19:01:18 -04001357 incr = 4; /* \xHH */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001358 }
Benjamin Peterson42ff1052014-09-29 19:01:18 -04001359 if (newsize > PY_SSIZE_T_MAX - incr)
1360 goto overflow;
1361 newsize += incr;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001362 }
1363 quote = '\'';
1364 if (smartquotes && squotes && !dquotes)
1365 quote = '"';
Benjamin Peterson42ff1052014-09-29 19:01:18 -04001366 if (squotes && quote == '\'') {
1367 if (newsize > PY_SSIZE_T_MAX - squotes)
1368 goto overflow;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001369 newsize += squotes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001371
1372 v = PyUnicode_New(newsize, 127);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 if (v == NULL) {
1374 return NULL;
1375 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001376 p = PyUnicode_1BYTE_DATA(v);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001377
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001378 *p++ = 'b', *p++ = quote;
1379 for (i = 0; i < length; i++) {
1380 unsigned char c = op->ob_sval[i];
1381 if (c == quote || c == '\\')
1382 *p++ = '\\', *p++ = c;
1383 else if (c == '\t')
1384 *p++ = '\\', *p++ = 't';
1385 else if (c == '\n')
1386 *p++ = '\\', *p++ = 'n';
1387 else if (c == '\r')
1388 *p++ = '\\', *p++ = 'r';
1389 else if (c < ' ' || c >= 0x7f) {
1390 *p++ = '\\';
1391 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +02001392 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
1393 *p++ = Py_hexdigits[c & 0xf];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001395 else
1396 *p++ = c;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001398 *p++ = quote;
Victor Stinner8f825062012-04-27 13:55:39 +02001399 assert(_PyUnicode_CheckConsistency(v, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001400 return v;
Benjamin Peterson42ff1052014-09-29 19:01:18 -04001401
1402 overflow:
1403 PyErr_SetString(PyExc_OverflowError,
1404 "bytes object is too large to make repr");
1405 return NULL;
Neal Norwitz6968b052007-02-27 19:02:19 +00001406}
1407
Neal Norwitz6968b052007-02-27 19:02:19 +00001408static PyObject *
Benjamin Peterson80688ef2009-04-18 15:17:02 +00001409bytes_repr(PyObject *op)
Neal Norwitz6968b052007-02-27 19:02:19 +00001410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 return PyBytes_Repr(op, 1);
Neal Norwitz6968b052007-02-27 19:02:19 +00001412}
1413
Neal Norwitz6968b052007-02-27 19:02:19 +00001414static PyObject *
Benjamin Peterson80688ef2009-04-18 15:17:02 +00001415bytes_str(PyObject *op)
Neal Norwitz6968b052007-02-27 19:02:19 +00001416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 if (Py_BytesWarningFlag) {
1418 if (PyErr_WarnEx(PyExc_BytesWarning,
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001419 "str() on a bytes instance", 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 return NULL;
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001421 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 }
1423 return bytes_repr(op);
Neal Norwitz6968b052007-02-27 19:02:19 +00001424}
1425
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001426static Py_ssize_t
Benjamin Peterson80688ef2009-04-18 15:17:02 +00001427bytes_length(PyBytesObject *a)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 return Py_SIZE(a);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001430}
Neal Norwitz6968b052007-02-27 19:02:19 +00001431
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001432/* This is also used by PyBytes_Concat() */
1433static PyObject *
Benjamin Peterson80688ef2009-04-18 15:17:02 +00001434bytes_concat(PyObject *a, PyObject *b)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 Py_buffer va, vb;
1437 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 va.len = -1;
1440 vb.len = -1;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001441 if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
1442 PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
Serhiy Storchaka6b5a9ec2017-03-19 19:47:02 +02001444 Py_TYPE(b)->tp_name, Py_TYPE(a)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 goto done;
1446 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 /* Optimize end cases */
1449 if (va.len == 0 && PyBytes_CheckExact(b)) {
1450 result = b;
1451 Py_INCREF(result);
1452 goto done;
1453 }
1454 if (vb.len == 0 && PyBytes_CheckExact(a)) {
1455 result = a;
1456 Py_INCREF(result);
1457 goto done;
1458 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001459
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +03001460 if (va.len > PY_SSIZE_T_MAX - vb.len) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 PyErr_NoMemory();
1462 goto done;
1463 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001464
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +03001465 result = PyBytes_FromStringAndSize(NULL, va.len + vb.len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 if (result != NULL) {
1467 memcpy(PyBytes_AS_STRING(result), va.buf, va.len);
1468 memcpy(PyBytes_AS_STRING(result) + va.len, vb.buf, vb.len);
1469 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001470
1471 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 if (va.len != -1)
1473 PyBuffer_Release(&va);
1474 if (vb.len != -1)
1475 PyBuffer_Release(&vb);
1476 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001477}
Neal Norwitz6968b052007-02-27 19:02:19 +00001478
1479static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001480bytes_repeat(PyBytesObject *a, Py_ssize_t n)
Neal Norwitz6968b052007-02-27 19:02:19 +00001481{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001482 Py_ssize_t i;
1483 Py_ssize_t j;
1484 Py_ssize_t size;
1485 PyBytesObject *op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 size_t nbytes;
1487 if (n < 0)
1488 n = 0;
1489 /* watch out for overflows: the size can overflow int,
1490 * and the # of bytes needed can overflow size_t
1491 */
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001492 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 PyErr_SetString(PyExc_OverflowError,
1494 "repeated bytes are too long");
1495 return NULL;
1496 }
Mark Dickinsoncf940c72010-08-10 18:35:01 +00001497 size = Py_SIZE(a) * n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 if (size == Py_SIZE(a) && PyBytes_CheckExact(a)) {
1499 Py_INCREF(a);
1500 return (PyObject *)a;
1501 }
1502 nbytes = (size_t)size;
1503 if (nbytes + PyBytesObject_SIZE <= nbytes) {
1504 PyErr_SetString(PyExc_OverflowError,
1505 "repeated bytes are too long");
1506 return NULL;
1507 }
1508 op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + nbytes);
1509 if (op == NULL)
1510 return PyErr_NoMemory();
Victor Stinnerb4435e22018-10-26 14:35:00 +02001511 (void)PyObject_INIT_VAR((PyVarObject *)op, &PyBytes_Type, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 op->ob_shash = -1;
1513 op->ob_sval[size] = '\0';
1514 if (Py_SIZE(a) == 1 && n > 0) {
1515 memset(op->ob_sval, a->ob_sval[0] , n);
1516 return (PyObject *) op;
1517 }
1518 i = 0;
1519 if (i < size) {
Christian Heimesf051e432016-09-13 20:22:02 +02001520 memcpy(op->ob_sval, a->ob_sval, Py_SIZE(a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 i = Py_SIZE(a);
1522 }
1523 while (i < size) {
1524 j = (i <= size-i) ? i : size-i;
Christian Heimesf051e432016-09-13 20:22:02 +02001525 memcpy(op->ob_sval+i, op->ob_sval, j);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 i += j;
1527 }
1528 return (PyObject *) op;
Neal Norwitz6968b052007-02-27 19:02:19 +00001529}
1530
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001531static int
1532bytes_contains(PyObject *self, PyObject *arg)
1533{
1534 return _Py_bytes_contains(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), arg);
1535}
1536
Neal Norwitz6968b052007-02-27 19:02:19 +00001537static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001538bytes_item(PyBytesObject *a, Py_ssize_t i)
Neal Norwitz6968b052007-02-27 19:02:19 +00001539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 if (i < 0 || i >= Py_SIZE(a)) {
1541 PyErr_SetString(PyExc_IndexError, "index out of range");
1542 return NULL;
1543 }
1544 return PyLong_FromLong((unsigned char)a->ob_sval[i]);
Neal Norwitz6968b052007-02-27 19:02:19 +00001545}
1546
Benjamin Peterson621b4302016-09-09 13:54:34 -07001547static int
Victor Stinnerc8bc5372013-11-04 11:08:10 +01001548bytes_compare_eq(PyBytesObject *a, PyBytesObject *b)
1549{
1550 int cmp;
1551 Py_ssize_t len;
1552
1553 len = Py_SIZE(a);
1554 if (Py_SIZE(b) != len)
1555 return 0;
1556
1557 if (a->ob_sval[0] != b->ob_sval[0])
1558 return 0;
1559
1560 cmp = memcmp(a->ob_sval, b->ob_sval, len);
1561 return (cmp == 0);
1562}
1563
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001564static PyObject*
Benjamin Peterson80688ef2009-04-18 15:17:02 +00001565bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
Neal Norwitz6968b052007-02-27 19:02:19 +00001566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 int c;
1568 Py_ssize_t len_a, len_b;
1569 Py_ssize_t min_len;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001570 int rc;
Neal Norwitz6968b052007-02-27 19:02:19 +00001571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 /* Make sure both arguments are strings. */
1573 if (!(PyBytes_Check(a) && PyBytes_Check(b))) {
Serhiy Storchaka1dd49822015-03-20 16:54:57 +02001574 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001575 rc = PyObject_IsInstance((PyObject*)a,
1576 (PyObject*)&PyUnicode_Type);
1577 if (!rc)
1578 rc = PyObject_IsInstance((PyObject*)b,
1579 (PyObject*)&PyUnicode_Type);
1580 if (rc < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 return NULL;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001582 if (rc) {
Serhiy Storchaka1dd49822015-03-20 16:54:57 +02001583 if (PyErr_WarnEx(PyExc_BytesWarning,
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001584 "Comparison between bytes and string", 1))
Serhiy Storchaka1dd49822015-03-20 16:54:57 +02001585 return NULL;
1586 }
Serhiy Storchakaac5569b2015-05-30 17:48:19 +03001587 else {
1588 rc = PyObject_IsInstance((PyObject*)a,
1589 (PyObject*)&PyLong_Type);
1590 if (!rc)
1591 rc = PyObject_IsInstance((PyObject*)b,
1592 (PyObject*)&PyLong_Type);
1593 if (rc < 0)
Serhiy Storchaka1dd49822015-03-20 16:54:57 +02001594 return NULL;
Serhiy Storchakaac5569b2015-05-30 17:48:19 +03001595 if (rc) {
1596 if (PyErr_WarnEx(PyExc_BytesWarning,
1597 "Comparison between bytes and int", 1))
1598 return NULL;
1599 }
Serhiy Storchaka1dd49822015-03-20 16:54:57 +02001600 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 }
stratakise8b19652017-11-02 11:32:54 +01001602 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 }
Victor Stinnerc8bc5372013-11-04 11:08:10 +01001604 else if (a == b) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 switch (op) {
Victor Stinnerfd9e44d2013-11-04 11:23:05 +01001606 case Py_EQ:
1607 case Py_LE:
1608 case Py_GE:
1609 /* a string is equal to itself */
stratakise8b19652017-11-02 11:32:54 +01001610 Py_RETURN_TRUE;
Victor Stinnerc8bc5372013-11-04 11:08:10 +01001611 break;
Victor Stinnerfd9e44d2013-11-04 11:23:05 +01001612 case Py_NE:
1613 case Py_LT:
1614 case Py_GT:
stratakise8b19652017-11-02 11:32:54 +01001615 Py_RETURN_FALSE;
Victor Stinnerc8bc5372013-11-04 11:08:10 +01001616 break;
Victor Stinnerfd9e44d2013-11-04 11:23:05 +01001617 default:
1618 PyErr_BadArgument();
1619 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 }
1621 }
Victor Stinnerc8bc5372013-11-04 11:08:10 +01001622 else if (op == Py_EQ || op == Py_NE) {
1623 int eq = bytes_compare_eq(a, b);
1624 eq ^= (op == Py_NE);
stratakise8b19652017-11-02 11:32:54 +01001625 return PyBool_FromLong(eq);
Victor Stinnerc8bc5372013-11-04 11:08:10 +01001626 }
1627 else {
Victor Stinnerfd9e44d2013-11-04 11:23:05 +01001628 len_a = Py_SIZE(a);
1629 len_b = Py_SIZE(b);
1630 min_len = Py_MIN(len_a, len_b);
Victor Stinnerc8bc5372013-11-04 11:08:10 +01001631 if (min_len > 0) {
1632 c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
Victor Stinnerfd9e44d2013-11-04 11:23:05 +01001633 if (c == 0)
Victor Stinnerc8bc5372013-11-04 11:08:10 +01001634 c = memcmp(a->ob_sval, b->ob_sval, min_len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 }
Victor Stinnerc8bc5372013-11-04 11:08:10 +01001636 else
1637 c = 0;
stratakise8b19652017-11-02 11:32:54 +01001638 if (c != 0)
1639 Py_RETURN_RICHCOMPARE(c, 0, op);
1640 Py_RETURN_RICHCOMPARE(len_a, len_b, op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 }
Neal Norwitz6968b052007-02-27 19:02:19 +00001642}
1643
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001644static Py_hash_t
Benjamin Peterson80688ef2009-04-18 15:17:02 +00001645bytes_hash(PyBytesObject *a)
Neal Norwitz6968b052007-02-27 19:02:19 +00001646{
Antoine Pitrouce4a9da2011-11-21 20:46:33 +01001647 if (a->ob_shash == -1) {
1648 /* Can't fail */
Christian Heimes985ecdc2013-11-20 11:46:18 +01001649 a->ob_shash = _Py_HashBytes(a->ob_sval, Py_SIZE(a));
Antoine Pitrouce4a9da2011-11-21 20:46:33 +01001650 }
1651 return a->ob_shash;
Neal Norwitz6968b052007-02-27 19:02:19 +00001652}
1653
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001654static PyObject*
Benjamin Peterson80688ef2009-04-18 15:17:02 +00001655bytes_subscript(PyBytesObject* self, PyObject* item)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 if (PyIndex_Check(item)) {
1658 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
1659 if (i == -1 && PyErr_Occurred())
1660 return NULL;
1661 if (i < 0)
1662 i += PyBytes_GET_SIZE(self);
1663 if (i < 0 || i >= PyBytes_GET_SIZE(self)) {
1664 PyErr_SetString(PyExc_IndexError,
1665 "index out of range");
1666 return NULL;
1667 }
1668 return PyLong_FromLong((unsigned char)self->ob_sval[i]);
1669 }
1670 else if (PySlice_Check(item)) {
1671 Py_ssize_t start, stop, step, slicelength, cur, i;
1672 char* source_buf;
1673 char* result_buf;
1674 PyObject* result;
Neal Norwitz6968b052007-02-27 19:02:19 +00001675
Serhiy Storchakab879fe82017-04-08 09:53:51 +03001676 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 return NULL;
1678 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03001679 slicelength = PySlice_AdjustIndices(PyBytes_GET_SIZE(self), &start,
1680 &stop, step);
Neal Norwitz6968b052007-02-27 19:02:19 +00001681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 if (slicelength <= 0) {
1683 return PyBytes_FromStringAndSize("", 0);
1684 }
1685 else if (start == 0 && step == 1 &&
1686 slicelength == PyBytes_GET_SIZE(self) &&
1687 PyBytes_CheckExact(self)) {
1688 Py_INCREF(self);
1689 return (PyObject *)self;
1690 }
1691 else if (step == 1) {
1692 return PyBytes_FromStringAndSize(
1693 PyBytes_AS_STRING(self) + start,
1694 slicelength);
1695 }
1696 else {
1697 source_buf = PyBytes_AS_STRING(self);
1698 result = PyBytes_FromStringAndSize(NULL, slicelength);
1699 if (result == NULL)
1700 return NULL;
Neal Norwitz6968b052007-02-27 19:02:19 +00001701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 result_buf = PyBytes_AS_STRING(result);
1703 for (cur = start, i = 0; i < slicelength;
1704 cur += step, i++) {
1705 result_buf[i] = source_buf[cur];
1706 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 return result;
1709 }
1710 }
1711 else {
1712 PyErr_Format(PyExc_TypeError,
Terry Jan Reedyffff1442014-08-02 01:30:37 -04001713 "byte indices must be integers or slices, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 Py_TYPE(item)->tp_name);
1715 return NULL;
1716 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001717}
1718
1719static int
Benjamin Peterson80688ef2009-04-18 15:17:02 +00001720bytes_buffer_getbuffer(PyBytesObject *self, Py_buffer *view, int flags)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 return PyBuffer_FillInfo(view, (PyObject*)self, (void *)self->ob_sval, Py_SIZE(self),
1723 1, flags);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001724}
1725
Benjamin Peterson80688ef2009-04-18 15:17:02 +00001726static PySequenceMethods bytes_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 (lenfunc)bytes_length, /*sq_length*/
1728 (binaryfunc)bytes_concat, /*sq_concat*/
1729 (ssizeargfunc)bytes_repeat, /*sq_repeat*/
1730 (ssizeargfunc)bytes_item, /*sq_item*/
1731 0, /*sq_slice*/
1732 0, /*sq_ass_item*/
1733 0, /*sq_ass_slice*/
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001734 (objobjproc)bytes_contains /*sq_contains*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001735};
1736
Benjamin Peterson80688ef2009-04-18 15:17:02 +00001737static PyMappingMethods bytes_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 (lenfunc)bytes_length,
1739 (binaryfunc)bytes_subscript,
1740 0,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001741};
1742
Benjamin Peterson80688ef2009-04-18 15:17:02 +00001743static PyBufferProcs bytes_as_buffer = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 (getbufferproc)bytes_buffer_getbuffer,
1745 NULL,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001746};
1747
1748
1749#define LEFTSTRIP 0
1750#define RIGHTSTRIP 1
1751#define BOTHSTRIP 2
1752
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001753/*[clinic input]
1754bytes.split
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001755
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001756 sep: object = None
1757 The delimiter according which to split the bytes.
1758 None (the default value) means split on ASCII whitespace characters
1759 (space, tab, return, newline, formfeed, vertical tab).
1760 maxsplit: Py_ssize_t = -1
1761 Maximum number of splits to do.
1762 -1 (the default value) means no limit.
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001763
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001764Return a list of the sections in the bytes, using sep as the delimiter.
1765[clinic start generated code]*/
1766
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001767static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001768bytes_split_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit)
1769/*[clinic end generated code: output=52126b5844c1d8ef input=8b809b39074abbfa]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001770{
1771 Py_ssize_t len = PyBytes_GET_SIZE(self), n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 const char *s = PyBytes_AS_STRING(self), *sub;
1773 Py_buffer vsub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001774 PyObject *list;
Neal Norwitz6968b052007-02-27 19:02:19 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 if (maxsplit < 0)
1777 maxsplit = PY_SSIZE_T_MAX;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001778 if (sep == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001780 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 return NULL;
1782 sub = vsub.buf;
1783 n = vsub.len;
Guido van Rossum8f950672007-09-10 16:53:45 +00001784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 list = stringlib_split((PyObject*) self, s, len, sub, n, maxsplit);
1786 PyBuffer_Release(&vsub);
1787 return list;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001788}
1789
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001790/*[clinic input]
1791bytes.partition
1792
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001793 sep: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001794 /
1795
1796Partition the bytes into three parts using the given separator.
1797
1798This will search for the separator sep in the bytes. If the separator is found,
1799returns a 3-tuple containing the part before the separator, the separator
1800itself, and the part after it.
1801
1802If the separator is not found, returns a 3-tuple containing the original bytes
1803object and two empty bytes objects.
1804[clinic start generated code]*/
1805
Neal Norwitz6968b052007-02-27 19:02:19 +00001806static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001807bytes_partition_impl(PyBytesObject *self, Py_buffer *sep)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001808/*[clinic end generated code: output=f532b392a17ff695 input=61cca95519406099]*/
Neal Norwitz6968b052007-02-27 19:02:19 +00001809{
Neal Norwitz6968b052007-02-27 19:02:19 +00001810 return stringlib_partition(
1811 (PyObject*) self,
1812 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001813 sep->obj, (const char *)sep->buf, sep->len
Neal Norwitz6968b052007-02-27 19:02:19 +00001814 );
1815}
1816
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001817/*[clinic input]
1818bytes.rpartition
1819
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001820 sep: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001821 /
1822
1823Partition the bytes into three parts using the given separator.
1824
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001825This will search for the separator sep in the bytes, starting at the end. If
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001826the separator is found, returns a 3-tuple containing the part before the
1827separator, the separator itself, and the part after it.
1828
1829If the separator is not found, returns a 3-tuple containing two empty bytes
1830objects and the original bytes object.
1831[clinic start generated code]*/
1832
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001833static PyObject *
1834bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001835/*[clinic end generated code: output=191b114cbb028e50 input=d78db010c8cfdbe1]*/
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001836{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 return stringlib_rpartition(
1838 (PyObject*) self,
1839 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001840 sep->obj, (const char *)sep->buf, sep->len
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 );
Neal Norwitz6968b052007-02-27 19:02:19 +00001842}
1843
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001844/*[clinic input]
1845bytes.rsplit = bytes.split
Neal Norwitz6968b052007-02-27 19:02:19 +00001846
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001847Return a list of the sections in the bytes, using sep as the delimiter.
1848
1849Splitting is done starting at the end of the bytes and working to the front.
1850[clinic start generated code]*/
1851
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001852static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001853bytes_rsplit_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit)
1854/*[clinic end generated code: output=ba698d9ea01e1c8f input=0f86c9f28f7d7b7b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001855{
1856 Py_ssize_t len = PyBytes_GET_SIZE(self), n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 const char *s = PyBytes_AS_STRING(self), *sub;
1858 Py_buffer vsub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001859 PyObject *list;
Neal Norwitz6968b052007-02-27 19:02:19 +00001860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 if (maxsplit < 0)
1862 maxsplit = PY_SSIZE_T_MAX;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001863 if (sep == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001865 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 return NULL;
1867 sub = vsub.buf;
1868 n = vsub.len;
Guido van Rossum8f950672007-09-10 16:53:45 +00001869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 list = stringlib_rsplit((PyObject*) self, s, len, sub, n, maxsplit);
1871 PyBuffer_Release(&vsub);
1872 return list;
Neal Norwitz6968b052007-02-27 19:02:19 +00001873}
1874
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001875
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001876/*[clinic input]
1877bytes.join
1878
1879 iterable_of_bytes: object
1880 /
1881
1882Concatenate any number of bytes objects.
1883
1884The bytes whose method is called is inserted in between each pair.
1885
1886The result is returned as a new bytes object.
1887
1888Example: b'.'.join([b'ab', b'pq', b'rs']) -> b'ab.pq.rs'.
1889[clinic start generated code]*/
1890
Neal Norwitz6968b052007-02-27 19:02:19 +00001891static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001892bytes_join(PyBytesObject *self, PyObject *iterable_of_bytes)
1893/*[clinic end generated code: output=a046f379f626f6f8 input=7fe377b95bd549d2]*/
Neal Norwitz6968b052007-02-27 19:02:19 +00001894{
Martin v. Löwis0efea322014-07-27 17:29:17 +02001895 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Neal Norwitz6968b052007-02-27 19:02:19 +00001896}
1897
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001898PyObject *
1899_PyBytes_Join(PyObject *sep, PyObject *x)
1900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 assert(sep != NULL && PyBytes_Check(sep));
1902 assert(x != NULL);
Martin v. Löwis0efea322014-07-27 17:29:17 +02001903 return bytes_join((PyBytesObject*)sep, x);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001904}
1905
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001906static PyObject *
1907bytes_find(PyBytesObject *self, PyObject *args)
1908{
1909 return _Py_bytes_find(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args);
1910}
1911
1912static PyObject *
1913bytes_index(PyBytesObject *self, PyObject *args)
1914{
1915 return _Py_bytes_index(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args);
1916}
1917
1918
1919static PyObject *
1920bytes_rfind(PyBytesObject *self, PyObject *args)
1921{
1922 return _Py_bytes_rfind(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args);
1923}
1924
1925
1926static PyObject *
1927bytes_rindex(PyBytesObject *self, PyObject *args)
1928{
1929 return _Py_bytes_rindex(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args);
1930}
1931
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001932
1933Py_LOCAL_INLINE(PyObject *)
1934do_xstrip(PyBytesObject *self, int striptype, PyObject *sepobj)
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 Py_buffer vsep;
1937 char *s = PyBytes_AS_STRING(self);
1938 Py_ssize_t len = PyBytes_GET_SIZE(self);
1939 char *sep;
1940 Py_ssize_t seplen;
1941 Py_ssize_t i, j;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001942
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001943 if (PyObject_GetBuffer(sepobj, &vsep, PyBUF_SIMPLE) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 return NULL;
1945 sep = vsep.buf;
1946 seplen = vsep.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 i = 0;
1949 if (striptype != RIGHTSTRIP) {
1950 while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) {
1951 i++;
1952 }
1953 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 j = len;
1956 if (striptype != LEFTSTRIP) {
1957 do {
1958 j--;
1959 } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen));
1960 j++;
1961 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 PyBuffer_Release(&vsep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 if (i == 0 && j == len && PyBytes_CheckExact(self)) {
1966 Py_INCREF(self);
1967 return (PyObject*)self;
1968 }
1969 else
1970 return PyBytes_FromStringAndSize(s+i, j-i);
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001971}
1972
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001973
1974Py_LOCAL_INLINE(PyObject *)
1975do_strip(PyBytesObject *self, int striptype)
1976{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 char *s = PyBytes_AS_STRING(self);
1978 Py_ssize_t len = PyBytes_GET_SIZE(self), i, j;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 i = 0;
1981 if (striptype != RIGHTSTRIP) {
David Malcolm96960882010-11-05 17:23:41 +00001982 while (i < len && Py_ISSPACE(s[i])) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 i++;
1984 }
1985 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 j = len;
1988 if (striptype != LEFTSTRIP) {
1989 do {
1990 j--;
David Malcolm96960882010-11-05 17:23:41 +00001991 } while (j >= i && Py_ISSPACE(s[j]));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 j++;
1993 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 if (i == 0 && j == len && PyBytes_CheckExact(self)) {
1996 Py_INCREF(self);
1997 return (PyObject*)self;
1998 }
1999 else
2000 return PyBytes_FromStringAndSize(s+i, j-i);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002001}
2002
2003
2004Py_LOCAL_INLINE(PyObject *)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002005do_argstrip(PyBytesObject *self, int striptype, PyObject *bytes)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002006{
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002007 if (bytes != NULL && bytes != Py_None) {
2008 return do_xstrip(self, striptype, bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 }
2010 return do_strip(self, striptype);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002011}
2012
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002013/*[clinic input]
2014bytes.strip
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002015
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002016 bytes: object = None
2017 /
2018
2019Strip leading and trailing bytes contained in the argument.
2020
2021If the argument is omitted or None, strip leading and trailing ASCII whitespace.
2022[clinic start generated code]*/
2023
Guido van Rossumad7d8d12007-04-13 01:39:34 +00002024static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002025bytes_strip_impl(PyBytesObject *self, PyObject *bytes)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002026/*[clinic end generated code: output=c7c228d3bd104a1b input=8a354640e4e0b3ef]*/
Guido van Rossumad7d8d12007-04-13 01:39:34 +00002027{
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002028 return do_argstrip(self, BOTHSTRIP, bytes);
Guido van Rossumad7d8d12007-04-13 01:39:34 +00002029}
2030
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002031/*[clinic input]
2032bytes.lstrip
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002033
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002034 bytes: object = None
2035 /
2036
2037Strip leading bytes contained in the argument.
2038
2039If the argument is omitted or None, strip leading ASCII whitespace.
2040[clinic start generated code]*/
2041
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002042static PyObject *
2043bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002044/*[clinic end generated code: output=28602e586f524e82 input=9baff4398c3f6857]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002045{
2046 return do_argstrip(self, LEFTSTRIP, bytes);
2047}
2048
2049/*[clinic input]
2050bytes.rstrip
2051
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002052 bytes: object = None
2053 /
2054
2055Strip trailing bytes contained in the argument.
2056
2057If the argument is omitted or None, strip trailing ASCII whitespace.
2058[clinic start generated code]*/
2059
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002060static PyObject *
2061bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002062/*[clinic end generated code: output=547e3815c95447da input=b78af445c727e32b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002063{
2064 return do_argstrip(self, RIGHTSTRIP, bytes);
Guido van Rossumad7d8d12007-04-13 01:39:34 +00002065}
Neal Norwitz6968b052007-02-27 19:02:19 +00002066
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002067
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002068static PyObject *
2069bytes_count(PyBytesObject *self, PyObject *args)
2070{
2071 return _Py_bytes_count(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args);
2072}
2073
2074
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002075/*[clinic input]
2076bytes.translate
2077
Victor Stinner049e5092014-08-17 22:20:00 +02002078 table: object
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002079 Translation table, which must be a bytes object of length 256.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002080 /
Martin Panter1b6c6da2016-08-27 08:35:02 +00002081 delete as deletechars: object(c_default="NULL") = b''
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002082
2083Return a copy with each character mapped by the given translation table.
2084
Martin Panter1b6c6da2016-08-27 08:35:02 +00002085All characters occurring in the optional argument delete are removed.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002086The remaining characters are mapped through the given translation table.
2087[clinic start generated code]*/
2088
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002089static PyObject *
Martin Panter1b6c6da2016-08-27 08:35:02 +00002090bytes_translate_impl(PyBytesObject *self, PyObject *table,
Larry Hastings89964c42015-04-14 18:07:59 -04002091 PyObject *deletechars)
Martin Panter1b6c6da2016-08-27 08:35:02 +00002092/*[clinic end generated code: output=43be3437f1956211 input=0ecdf159f654233c]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002093{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002094 char *input, *output;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02002095 Py_buffer table_view = {NULL, NULL};
2096 Py_buffer del_table_view = {NULL, NULL};
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002097 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002098 Py_ssize_t i, c, changed = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 PyObject *input_obj = (PyObject*)self;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002100 const char *output_start, *del_table_chars=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 Py_ssize_t inlen, tablen, dellen = 0;
2102 PyObject *result;
2103 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002104
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002105 if (PyBytes_Check(table)) {
2106 table_chars = PyBytes_AS_STRING(table);
2107 tablen = PyBytes_GET_SIZE(table);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002109 else if (table == Py_None) {
2110 table_chars = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 tablen = 256;
2112 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02002113 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002114 if (PyObject_GetBuffer(table, &table_view, PyBUF_SIMPLE) != 0)
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02002115 return NULL;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002116 table_chars = table_view.buf;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02002117 tablen = table_view.len;
2118 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 if (tablen != 256) {
2121 PyErr_SetString(PyExc_ValueError,
2122 "translation table must be 256 characters long");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02002123 PyBuffer_Release(&table_view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 return NULL;
2125 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002126
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002127 if (deletechars != NULL) {
2128 if (PyBytes_Check(deletechars)) {
2129 del_table_chars = PyBytes_AS_STRING(deletechars);
2130 dellen = PyBytes_GET_SIZE(deletechars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02002132 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002133 if (PyObject_GetBuffer(deletechars, &del_table_view, PyBUF_SIMPLE) != 0) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02002134 PyBuffer_Release(&table_view);
2135 return NULL;
2136 }
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002137 del_table_chars = del_table_view.buf;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02002138 dellen = del_table_view.len;
2139 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 }
2141 else {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002142 del_table_chars = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 dellen = 0;
2144 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 inlen = PyBytes_GET_SIZE(input_obj);
2147 result = PyBytes_FromStringAndSize((char *)NULL, inlen);
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02002148 if (result == NULL) {
2149 PyBuffer_Release(&del_table_view);
2150 PyBuffer_Release(&table_view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 return NULL;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02002152 }
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002153 output_start = output = PyBytes_AS_STRING(result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 input = PyBytes_AS_STRING(input_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002155
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002156 if (dellen == 0 && table_chars != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 /* If no deletions are required, use faster code */
2158 for (i = inlen; --i >= 0; ) {
2159 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002160 if (Py_CHARMASK((*output++ = table_chars[c])) != c)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 changed = 1;
2162 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02002163 if (!changed && PyBytes_CheckExact(input_obj)) {
2164 Py_INCREF(input_obj);
2165 Py_DECREF(result);
2166 result = input_obj;
2167 }
2168 PyBuffer_Release(&del_table_view);
2169 PyBuffer_Release(&table_view);
2170 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002172
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002173 if (table_chars == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 for (i = 0; i < 256; i++)
2175 trans_table[i] = Py_CHARMASK(i);
2176 } else {
2177 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002178 trans_table[i] = Py_CHARMASK(table_chars[i]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02002180 PyBuffer_Release(&table_view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 for (i = 0; i < dellen; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002183 trans_table[(int) Py_CHARMASK(del_table_chars[i])] = -1;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02002184 PyBuffer_Release(&del_table_view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 for (i = inlen; --i >= 0; ) {
2187 c = Py_CHARMASK(*input++);
2188 if (trans_table[c] != -1)
2189 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
2190 continue;
2191 changed = 1;
2192 }
2193 if (!changed && PyBytes_CheckExact(input_obj)) {
2194 Py_DECREF(result);
2195 Py_INCREF(input_obj);
2196 return input_obj;
2197 }
2198 /* Fix the size of the resulting string */
2199 if (inlen > 0)
2200 _PyBytes_Resize(&result, output - output_start);
2201 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002202}
2203
2204
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002205/*[clinic input]
2206
2207@staticmethod
2208bytes.maketrans
2209
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002210 frm: Py_buffer
2211 to: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002212 /
2213
2214Return a translation table useable for the bytes or bytearray translate method.
2215
2216The returned table will be one where each byte in frm is mapped to the byte at
2217the same position in to.
2218
2219The bytes objects frm and to must be of the same length.
2220[clinic start generated code]*/
2221
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002222static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002223bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002224/*[clinic end generated code: output=a36f6399d4b77f6f input=de7a8fc5632bb8f1]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002225{
2226 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00002227}
2228
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002229
2230/*[clinic input]
2231bytes.replace
2232
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002233 old: Py_buffer
2234 new: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002235 count: Py_ssize_t = -1
2236 Maximum number of occurrences to replace.
2237 -1 (the default value) means replace all occurrences.
2238 /
2239
2240Return a copy with all occurrences of substring old replaced by new.
2241
2242If the optional argument count is given, only the first count occurrences are
2243replaced.
2244[clinic start generated code]*/
2245
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002246static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002247bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new,
Larry Hastings89964c42015-04-14 18:07:59 -04002248 Py_ssize_t count)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002249/*[clinic end generated code: output=994fa588b6b9c104 input=b2fbbf0bf04de8e5]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002250{
Serhiy Storchakafb81d3c2016-05-05 09:26:07 +03002251 return stringlib_replace((PyObject *)self,
2252 (const char *)old->buf, old->len,
2253 (const char *)new->buf, new->len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002254}
2255
2256/** End DALKE **/
2257
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002258
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002259static PyObject *
2260bytes_startswith(PyBytesObject *self, PyObject *args)
2261{
2262 return _Py_bytes_startswith(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args);
2263}
2264
2265static PyObject *
2266bytes_endswith(PyBytesObject *self, PyObject *args)
2267{
2268 return _Py_bytes_endswith(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args);
2269}
2270
2271
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002272/*[clinic input]
2273bytes.decode
2274
2275 encoding: str(c_default="NULL") = 'utf-8'
2276 The encoding with which to decode the bytes.
2277 errors: str(c_default="NULL") = 'strict'
2278 The error handling scheme to use for the handling of decoding errors.
2279 The default is 'strict' meaning that decoding errors raise a
2280 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
2281 as well as any other name registered with codecs.register_error that
2282 can handle UnicodeDecodeErrors.
2283
2284Decode the bytes using the codec registered for encoding.
2285[clinic start generated code]*/
2286
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002287static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002288bytes_decode_impl(PyBytesObject *self, const char *encoding,
Larry Hastings89964c42015-04-14 18:07:59 -04002289 const char *errors)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002290/*[clinic end generated code: output=5649a53dde27b314 input=958174769d2a40ca]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002291{
Martin v. Löwis0efea322014-07-27 17:29:17 +02002292 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Guido van Rossumd624f182006-04-24 13:47:05 +00002293}
2294
Guido van Rossum20188312006-05-05 15:15:40 +00002295
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002296/*[clinic input]
2297bytes.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002298
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002299 keepends: bool(accept={int}) = False
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002300
2301Return a list of the lines in the bytes, breaking at line boundaries.
2302
2303Line breaks are not included in the resulting list unless keepends is given and
2304true.
2305[clinic start generated code]*/
2306
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002307static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002308bytes_splitlines_impl(PyBytesObject *self, int keepends)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002309/*[clinic end generated code: output=3484149a5d880ffb input=a8b32eb01ff5a5ed]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002310{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002311 return stringlib_splitlines(
Antoine Pitroud1188562010-06-09 16:38:55 +00002312 (PyObject*) self, PyBytes_AS_STRING(self),
2313 PyBytes_GET_SIZE(self), keepends
2314 );
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002315}
2316
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002317/*[clinic input]
2318@classmethod
2319bytes.fromhex
2320
2321 string: unicode
2322 /
2323
2324Create a bytes object from a string of hexadecimal numbers.
2325
2326Spaces between two numbers are accepted.
2327Example: bytes.fromhex('B9 01EF') -> b'\\xb9\\x01\\xef'.
2328[clinic start generated code]*/
2329
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002330static PyObject *
Martin v. Löwis0efea322014-07-27 17:29:17 +02002331bytes_fromhex_impl(PyTypeObject *type, PyObject *string)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002332/*[clinic end generated code: output=0973acc63661bb2e input=bf4d1c361670acd3]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002333{
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002334 PyObject *result = _PyBytes_FromHex(string, 0);
2335 if (type != &PyBytes_Type && result != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002336 Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type,
2337 result, NULL));
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002338 }
2339 return result;
Victor Stinner2bf89932015-10-14 11:25:33 +02002340}
2341
2342PyObject*
2343_PyBytes_FromHex(PyObject *string, int use_bytearray)
2344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 char *buf;
Victor Stinner2bf89932015-10-14 11:25:33 +02002346 Py_ssize_t hexlen, invalid_char;
2347 unsigned int top, bot;
2348 Py_UCS1 *str, *end;
2349 _PyBytesWriter writer;
2350
2351 _PyBytesWriter_Init(&writer);
2352 writer.use_bytearray = use_bytearray;
Georg Brandl0b9b9e02007-02-27 08:40:54 +00002353
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002354 assert(PyUnicode_Check(string));
2355 if (PyUnicode_READY(string))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002357 hexlen = PyUnicode_GET_LENGTH(string);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002358
Victor Stinner2bf89932015-10-14 11:25:33 +02002359 if (!PyUnicode_IS_ASCII(string)) {
2360 void *data = PyUnicode_DATA(string);
2361 unsigned int kind = PyUnicode_KIND(string);
2362 Py_ssize_t i;
2363
2364 /* search for the first non-ASCII character */
2365 for (i = 0; i < hexlen; i++) {
2366 if (PyUnicode_READ(kind, data, i) >= 128)
2367 break;
2368 }
2369 invalid_char = i;
2370 goto error;
2371 }
2372
2373 assert(PyUnicode_KIND(string) == PyUnicode_1BYTE_KIND);
2374 str = PyUnicode_1BYTE_DATA(string);
2375
2376 /* This overestimates if there are spaces */
2377 buf = _PyBytesWriter_Alloc(&writer, hexlen / 2);
2378 if (buf == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 return NULL;
Victor Stinner2bf89932015-10-14 11:25:33 +02002380
2381 end = str + hexlen;
2382 while (str < end) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 /* skip over spaces in the input */
Serhiy Storchakadd1da7f2016-12-19 18:51:37 +02002384 if (Py_ISSPACE(*str)) {
Victor Stinner2bf89932015-10-14 11:25:33 +02002385 do {
2386 str++;
Serhiy Storchakadd1da7f2016-12-19 18:51:37 +02002387 } while (Py_ISSPACE(*str));
Victor Stinner2bf89932015-10-14 11:25:33 +02002388 if (str >= end)
2389 break;
2390 }
2391
2392 top = _PyLong_DigitValue[*str];
2393 if (top >= 16) {
2394 invalid_char = str - PyUnicode_1BYTE_DATA(string);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 goto error;
2396 }
Victor Stinner2bf89932015-10-14 11:25:33 +02002397 str++;
2398
2399 bot = _PyLong_DigitValue[*str];
2400 if (bot >= 16) {
2401 invalid_char = str - PyUnicode_1BYTE_DATA(string);
2402 goto error;
2403 }
2404 str++;
2405
2406 *buf++ = (unsigned char)((top << 4) + bot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 }
Victor Stinner2bf89932015-10-14 11:25:33 +02002408
2409 return _PyBytesWriter_Finish(&writer, buf);
Georg Brandl0b9b9e02007-02-27 08:40:54 +00002410
2411 error:
Victor Stinner2bf89932015-10-14 11:25:33 +02002412 PyErr_Format(PyExc_ValueError,
2413 "non-hexadecimal number found in "
2414 "fromhex() arg at position %zd", invalid_char);
2415 _PyBytesWriter_Dealloc(&writer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 return NULL;
Georg Brandl0b9b9e02007-02-27 08:40:54 +00002417}
2418
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002419PyDoc_STRVAR(hex__doc__,
2420"B.hex() -> string\n\
2421\n\
2422Create a string of hexadecimal numbers from a bytes object.\n\
2423Example: b'\\xb9\\x01\\xef'.hex() -> 'b901ef'.");
2424
2425static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302426bytes_hex(PyBytesObject *self, PyObject *Py_UNUSED(ignored))
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002427{
2428 char* argbuf = PyBytes_AS_STRING(self);
2429 Py_ssize_t arglen = PyBytes_GET_SIZE(self);
2430 return _Py_strhex(argbuf, arglen);
2431}
2432
2433static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302434bytes_getnewargs(PyBytesObject *v, PyObject *Py_UNUSED(ignored))
Guido van Rossum0dd32e22007-04-11 05:40:58 +00002435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 return Py_BuildValue("(y#)", v->ob_sval, Py_SIZE(v));
Guido van Rossum0dd32e22007-04-11 05:40:58 +00002437}
2438
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00002439
2440static PyMethodDef
Benjamin Peterson80688ef2009-04-18 15:17:02 +00002441bytes_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 {"__getnewargs__", (PyCFunction)bytes_getnewargs, METH_NOARGS},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302443 {"capitalize", stringlib_capitalize, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 _Py_capitalize__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002445 STRINGLIB_CENTER_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002446 {"count", (PyCFunction)bytes_count, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002447 _Py_count__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002448 BYTES_DECODE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002449 {"endswith", (PyCFunction)bytes_endswith, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002450 _Py_endswith__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002451 STRINGLIB_EXPANDTABS_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002452 {"find", (PyCFunction)bytes_find, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002453 _Py_find__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002454 BYTES_FROMHEX_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002455 {"hex", (PyCFunction)bytes_hex, METH_NOARGS, hex__doc__},
2456 {"index", (PyCFunction)bytes_index, METH_VARARGS, _Py_index__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302457 {"isalnum", stringlib_isalnum, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 _Py_isalnum__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302459 {"isalpha", stringlib_isalpha, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 _Py_isalpha__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302461 {"isascii", stringlib_isascii, METH_NOARGS,
INADA Naokia49ac992018-01-27 14:06:21 +09002462 _Py_isascii__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302463 {"isdigit", stringlib_isdigit, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 _Py_isdigit__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302465 {"islower", stringlib_islower, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 _Py_islower__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302467 {"isspace", stringlib_isspace, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 _Py_isspace__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302469 {"istitle", stringlib_istitle, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 _Py_istitle__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302471 {"isupper", stringlib_isupper, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 _Py_isupper__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002473 BYTES_JOIN_METHODDEF
Tal Einatc929df32018-07-06 13:17:38 +03002474 STRINGLIB_LJUST_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302475 {"lower", stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002476 BYTES_LSTRIP_METHODDEF
2477 BYTES_MAKETRANS_METHODDEF
2478 BYTES_PARTITION_METHODDEF
2479 BYTES_REPLACE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002480 {"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, _Py_rfind__doc__},
2481 {"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, _Py_rindex__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002482 STRINGLIB_RJUST_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002483 BYTES_RPARTITION_METHODDEF
2484 BYTES_RSPLIT_METHODDEF
2485 BYTES_RSTRIP_METHODDEF
2486 BYTES_SPLIT_METHODDEF
2487 BYTES_SPLITLINES_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002488 {"startswith", (PyCFunction)bytes_startswith, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002489 _Py_startswith__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002490 BYTES_STRIP_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302491 {"swapcase", stringlib_swapcase, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 _Py_swapcase__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302493 {"title", stringlib_title, METH_NOARGS, _Py_title__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002494 BYTES_TRANSLATE_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302495 {"upper", stringlib_upper, METH_NOARGS, _Py_upper__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002496 STRINGLIB_ZFILL_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 {NULL, NULL} /* sentinel */
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00002498};
2499
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002500static PyObject *
Serhiy Storchakac9a59e62016-04-15 14:11:10 +03002501bytes_mod(PyObject *self, PyObject *arg)
Ethan Furmanb95b5612015-01-23 20:05:18 -08002502{
Serhiy Storchakac9a59e62016-04-15 14:11:10 +03002503 if (!PyBytes_Check(self)) {
2504 Py_RETURN_NOTIMPLEMENTED;
Victor Stinner772b2b02015-10-14 09:56:53 +02002505 }
Victor Stinner772b2b02015-10-14 09:56:53 +02002506 return _PyBytes_FormatEx(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
Serhiy Storchakac9a59e62016-04-15 14:11:10 +03002507 arg, 0);
Ethan Furmanb95b5612015-01-23 20:05:18 -08002508}
2509
2510static PyNumberMethods bytes_as_number = {
2511 0, /*nb_add*/
2512 0, /*nb_subtract*/
2513 0, /*nb_multiply*/
2514 bytes_mod, /*nb_remainder*/
2515};
2516
2517static PyObject *
Serhiy Storchaka15095802015-11-25 15:47:01 +02002518bytes_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002519
2520static PyObject *
Benjamin Peterson80688ef2009-04-18 15:17:02 +00002521bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 PyObject *x = NULL;
2524 const char *encoding = NULL;
2525 const char *errors = NULL;
2526 PyObject *new = NULL;
Benjamin Peterson5ff3f732012-12-19 15:27:41 -06002527 PyObject *func;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 Py_ssize_t size;
2529 static char *kwlist[] = {"source", "encoding", "errors", 0};
Benjamin Peterson5ff3f732012-12-19 15:27:41 -06002530 _Py_IDENTIFIER(__bytes__);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 if (type != &PyBytes_Type)
Serhiy Storchaka15095802015-11-25 15:47:01 +02002533 return bytes_subtype_new(type, args, kwds);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytes", kwlist, &x,
2535 &encoding, &errors))
2536 return NULL;
2537 if (x == NULL) {
2538 if (encoding != NULL || errors != NULL) {
2539 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03002540 encoding != NULL ?
2541 "encoding without a string argument" :
2542 "errors without a string argument");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 return NULL;
2544 }
Victor Stinnerdb067af2014-05-02 22:31:14 +02002545 return PyBytes_FromStringAndSize(NULL, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002547
Serhiy Storchaka5aac3ed2015-12-20 16:36:34 +02002548 if (encoding != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 /* Encode via the codec registry */
Serhiy Storchaka5aac3ed2015-12-20 16:36:34 +02002550 if (!PyUnicode_Check(x)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka5aac3ed2015-12-20 16:36:34 +02002552 "encoding without a string argument");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002553 return NULL;
2554 }
2555 new = PyUnicode_AsEncodedString(x, encoding, errors);
2556 if (new == NULL)
2557 return NULL;
2558 assert(PyBytes_Check(new));
2559 return new;
2560 }
Benjamin Peterson5ff3f732012-12-19 15:27:41 -06002561
Serhiy Storchaka5aac3ed2015-12-20 16:36:34 +02002562 if (errors != NULL) {
Serhiy Storchaka83cf99d2014-12-02 09:24:06 +02002563 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka5aac3ed2015-12-20 16:36:34 +02002564 PyUnicode_Check(x) ?
2565 "string argument without an encoding" :
2566 "errors without a string argument");
Serhiy Storchaka83cf99d2014-12-02 09:24:06 +02002567 return NULL;
2568 }
2569
Benjamin Peterson5ff3f732012-12-19 15:27:41 -06002570 /* We'd like to call PyObject_Bytes here, but we need to check for an
2571 integer argument before deferring to PyBytes_FromObject, something
2572 PyObject_Bytes doesn't do. */
2573 func = _PyObject_LookupSpecial(x, &PyId___bytes__);
2574 if (func != NULL) {
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002575 new = _PyObject_CallNoArg(func);
Benjamin Peterson5ff3f732012-12-19 15:27:41 -06002576 Py_DECREF(func);
2577 if (new == NULL)
2578 return NULL;
2579 if (!PyBytes_Check(new)) {
2580 PyErr_Format(PyExc_TypeError,
2581 "__bytes__ returned non-bytes (type %.200s)",
2582 Py_TYPE(new)->tp_name);
2583 Py_DECREF(new);
2584 return NULL;
2585 }
2586 return new;
2587 }
2588 else if (PyErr_Occurred())
2589 return NULL;
2590
Serhiy Storchaka5aac3ed2015-12-20 16:36:34 +02002591 if (PyUnicode_Check(x)) {
2592 PyErr_SetString(PyExc_TypeError,
2593 "string argument without an encoding");
2594 return NULL;
2595 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 /* Is it an integer? */
Serhiy Storchakaeb249882016-08-15 09:46:07 +03002597 if (PyIndex_Check(x)) {
2598 size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
2599 if (size == -1 && PyErr_Occurred()) {
Serhiy Storchakae8904212018-10-15 00:02:57 +03002600 if (!PyErr_ExceptionMatches(PyExc_TypeError))
INADA Naokia634e232017-01-06 17:32:01 +09002601 return NULL;
2602 PyErr_Clear(); /* fall through */
Serhiy Storchakaeb249882016-08-15 09:46:07 +03002603 }
INADA Naokia634e232017-01-06 17:32:01 +09002604 else {
2605 if (size < 0) {
2606 PyErr_SetString(PyExc_ValueError, "negative count");
2607 return NULL;
2608 }
2609 new = _PyBytes_FromSize(size, 1);
2610 if (new == NULL)
2611 return NULL;
2612 return new;
Serhiy Storchakaeb249882016-08-15 09:46:07 +03002613 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002615
Benjamin Peterson5ff3f732012-12-19 15:27:41 -06002616 return PyBytes_FromObject(x);
Benjamin Petersonc15a0732008-08-26 16:46:47 +00002617}
2618
Victor Stinnerf2eafa32015-10-14 13:44:29 +02002619static PyObject*
2620_PyBytes_FromBuffer(PyObject *x)
2621{
2622 PyObject *new;
2623 Py_buffer view;
2624
2625 if (PyObject_GetBuffer(x, &view, PyBUF_FULL_RO) < 0)
2626 return NULL;
2627
2628 new = PyBytes_FromStringAndSize(NULL, view.len);
2629 if (!new)
2630 goto fail;
2631 if (PyBuffer_ToContiguous(((PyBytesObject *)new)->ob_sval,
2632 &view, view.len, 'C') < 0)
2633 goto fail;
2634 PyBuffer_Release(&view);
2635 return new;
2636
2637fail:
2638 Py_XDECREF(new);
2639 PyBuffer_Release(&view);
2640 return NULL;
2641}
2642
2643static PyObject*
2644_PyBytes_FromList(PyObject *x)
2645{
Serhiy Storchaka914f9a02018-10-21 15:25:53 +03002646 Py_ssize_t i, size = PyList_GET_SIZE(x);
2647 Py_ssize_t value;
2648 char *str;
2649 PyObject *item;
2650 _PyBytesWriter writer;
2651
2652 _PyBytesWriter_Init(&writer);
2653 str = _PyBytesWriter_Alloc(&writer, size);
2654 if (str == NULL)
2655 return NULL;
2656 writer.overallocate = 1;
2657 size = writer.allocated;
2658
2659 for (i = 0; i < PyList_GET_SIZE(x); i++) {
2660 item = PyList_GET_ITEM(x, i);
2661 Py_INCREF(item);
2662 value = PyNumber_AsSsize_t(item, NULL);
2663 Py_DECREF(item);
2664 if (value == -1 && PyErr_Occurred())
2665 goto error;
2666
2667 if (value < 0 || value >= 256) {
2668 PyErr_SetString(PyExc_ValueError,
2669 "bytes must be in range(0, 256)");
2670 goto error;
2671 }
2672
2673 if (i >= size) {
2674 str = _PyBytesWriter_Resize(&writer, str, size+1);
2675 if (str == NULL)
2676 return NULL;
2677 size = writer.allocated;
2678 }
2679 *str++ = (char) value;
2680 }
2681 return _PyBytesWriter_Finish(&writer, str);
2682
2683 error:
2684 _PyBytesWriter_Dealloc(&writer);
2685 return NULL;
Victor Stinnerf2eafa32015-10-14 13:44:29 +02002686}
2687
2688static PyObject*
2689_PyBytes_FromTuple(PyObject *x)
2690{
Serhiy Storchaka914f9a02018-10-21 15:25:53 +03002691 PyObject *bytes;
2692 Py_ssize_t i, size = PyTuple_GET_SIZE(x);
2693 Py_ssize_t value;
2694 char *str;
2695 PyObject *item;
2696
2697 bytes = PyBytes_FromStringAndSize(NULL, size);
2698 if (bytes == NULL)
2699 return NULL;
2700 str = ((PyBytesObject *)bytes)->ob_sval;
2701
2702 for (i = 0; i < size; i++) {
2703 item = PyTuple_GET_ITEM(x, i);
2704 value = PyNumber_AsSsize_t(item, NULL);
2705 if (value == -1 && PyErr_Occurred())
2706 goto error;
2707
2708 if (value < 0 || value >= 256) {
2709 PyErr_SetString(PyExc_ValueError,
2710 "bytes must be in range(0, 256)");
2711 goto error;
2712 }
2713 *str++ = (char) value;
2714 }
2715 return bytes;
2716
2717 error:
2718 Py_DECREF(bytes);
2719 return NULL;
Victor Stinnerf2eafa32015-10-14 13:44:29 +02002720}
2721
2722static PyObject *
Serhiy Storchaka03f17f82016-04-10 14:44:59 +03002723_PyBytes_FromIterator(PyObject *it, PyObject *x)
Benjamin Petersonc15a0732008-08-26 16:46:47 +00002724{
Victor Stinnerc3d2bc12015-10-14 14:15:49 +02002725 char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 Py_ssize_t i, size;
Victor Stinnerc3d2bc12015-10-14 14:15:49 +02002727 _PyBytesWriter writer;
2728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002729 /* For iterator version, create a string object and resize as needed */
Armin Ronacheraa9a79d2012-10-06 14:03:24 +02002730 size = PyObject_LengthHint(x, 64);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 if (size == -1 && PyErr_Occurred())
2732 return NULL;
Victor Stinnerc3d2bc12015-10-14 14:15:49 +02002733
Serhiy Storchaka03f17f82016-04-10 14:44:59 +03002734 _PyBytesWriter_Init(&writer);
Victor Stinnerc3d2bc12015-10-14 14:15:49 +02002735 str = _PyBytesWriter_Alloc(&writer, size);
2736 if (str == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 return NULL;
Victor Stinnerc3d2bc12015-10-14 14:15:49 +02002738 writer.overallocate = 1;
2739 size = writer.allocated;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 /* Run the iterator to exhaustion */
2742 for (i = 0; ; i++) {
2743 PyObject *item;
2744 Py_ssize_t value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 /* Get the next item */
2747 item = PyIter_Next(it);
2748 if (item == NULL) {
2749 if (PyErr_Occurred())
2750 goto error;
2751 break;
2752 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 /* Interpret it as an int (__index__) */
Serhiy Storchakaf54d7812016-07-06 21:39:44 +03002755 value = PyNumber_AsSsize_t(item, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 Py_DECREF(item);
2757 if (value == -1 && PyErr_Occurred())
2758 goto error;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 /* Range check */
2761 if (value < 0 || value >= 256) {
2762 PyErr_SetString(PyExc_ValueError,
2763 "bytes must be in range(0, 256)");
2764 goto error;
2765 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 /* Append the byte */
2768 if (i >= size) {
Victor Stinnerc3d2bc12015-10-14 14:15:49 +02002769 str = _PyBytesWriter_Resize(&writer, str, size+1);
2770 if (str == NULL)
2771 return NULL;
2772 size = writer.allocated;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 }
Victor Stinnerc3d2bc12015-10-14 14:15:49 +02002774 *str++ = (char) value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 }
Victor Stinnerc3d2bc12015-10-14 14:15:49 +02002776
2777 return _PyBytesWriter_Finish(&writer, str);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002778
2779 error:
Victor Stinnerc3d2bc12015-10-14 14:15:49 +02002780 _PyBytesWriter_Dealloc(&writer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002782}
2783
Victor Stinnerf2eafa32015-10-14 13:44:29 +02002784PyObject *
2785PyBytes_FromObject(PyObject *x)
2786{
Serhiy Storchaka03f17f82016-04-10 14:44:59 +03002787 PyObject *it, *result;
2788
Victor Stinnerf2eafa32015-10-14 13:44:29 +02002789 if (x == NULL) {
2790 PyErr_BadInternalCall();
2791 return NULL;
2792 }
2793
2794 if (PyBytes_CheckExact(x)) {
2795 Py_INCREF(x);
2796 return x;
2797 }
2798
2799 /* Use the modern buffer interface */
2800 if (PyObject_CheckBuffer(x))
2801 return _PyBytes_FromBuffer(x);
2802
2803 if (PyList_CheckExact(x))
2804 return _PyBytes_FromList(x);
2805
2806 if (PyTuple_CheckExact(x))
2807 return _PyBytes_FromTuple(x);
2808
Serhiy Storchaka03f17f82016-04-10 14:44:59 +03002809 if (!PyUnicode_Check(x)) {
2810 it = PyObject_GetIter(x);
2811 if (it != NULL) {
2812 result = _PyBytes_FromIterator(it, x);
2813 Py_DECREF(it);
2814 return result;
2815 }
Serhiy Storchakae8904212018-10-15 00:02:57 +03002816 if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2817 return NULL;
2818 }
Victor Stinnerf2eafa32015-10-14 13:44:29 +02002819 }
2820
Serhiy Storchaka03f17f82016-04-10 14:44:59 +03002821 PyErr_Format(PyExc_TypeError,
2822 "cannot convert '%.200s' object to bytes",
2823 x->ob_type->tp_name);
2824 return NULL;
Victor Stinnerf2eafa32015-10-14 13:44:29 +02002825}
2826
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002827static PyObject *
Serhiy Storchaka15095802015-11-25 15:47:01 +02002828bytes_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 PyObject *tmp, *pnew;
2831 Py_ssize_t n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 assert(PyType_IsSubtype(type, &PyBytes_Type));
2834 tmp = bytes_new(&PyBytes_Type, args, kwds);
2835 if (tmp == NULL)
2836 return NULL;
Serhiy Storchaka15095802015-11-25 15:47:01 +02002837 assert(PyBytes_Check(tmp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 n = PyBytes_GET_SIZE(tmp);
2839 pnew = type->tp_alloc(type, n);
2840 if (pnew != NULL) {
Christian Heimesf051e432016-09-13 20:22:02 +02002841 memcpy(PyBytes_AS_STRING(pnew),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 PyBytes_AS_STRING(tmp), n+1);
2843 ((PyBytesObject *)pnew)->ob_shash =
2844 ((PyBytesObject *)tmp)->ob_shash;
2845 }
2846 Py_DECREF(tmp);
2847 return pnew;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002848}
2849
Benjamin Peterson80688ef2009-04-18 15:17:02 +00002850PyDoc_STRVAR(bytes_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00002851"bytes(iterable_of_ints) -> bytes\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002852bytes(string, encoding[, errors]) -> bytes\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00002853bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002854bytes(int) -> bytes object of size given by the parameter initialized with null bytes\n\
2855bytes() -> empty bytes object\n\
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00002856\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002857Construct an immutable array of bytes from:\n\
Guido van Rossum98297ee2007-11-06 21:34:58 +00002858 - an iterable yielding integers in range(256)\n\
2859 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002860 - any object implementing the buffer API.\n\
2861 - an integer");
Guido van Rossum98297ee2007-11-06 21:34:58 +00002862
Benjamin Peterson80688ef2009-04-18 15:17:02 +00002863static PyObject *bytes_iter(PyObject *seq);
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00002864
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002865PyTypeObject PyBytes_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2867 "bytes",
2868 PyBytesObject_SIZE,
2869 sizeof(char),
2870 bytes_dealloc, /* tp_dealloc */
2871 0, /* tp_print */
2872 0, /* tp_getattr */
2873 0, /* tp_setattr */
2874 0, /* tp_reserved */
2875 (reprfunc)bytes_repr, /* tp_repr */
Ethan Furmanb95b5612015-01-23 20:05:18 -08002876 &bytes_as_number, /* tp_as_number */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 &bytes_as_sequence, /* tp_as_sequence */
2878 &bytes_as_mapping, /* tp_as_mapping */
2879 (hashfunc)bytes_hash, /* tp_hash */
2880 0, /* tp_call */
2881 bytes_str, /* tp_str */
2882 PyObject_GenericGetAttr, /* tp_getattro */
2883 0, /* tp_setattro */
2884 &bytes_as_buffer, /* tp_as_buffer */
2885 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
2886 Py_TPFLAGS_BYTES_SUBCLASS, /* tp_flags */
2887 bytes_doc, /* tp_doc */
2888 0, /* tp_traverse */
2889 0, /* tp_clear */
2890 (richcmpfunc)bytes_richcompare, /* tp_richcompare */
2891 0, /* tp_weaklistoffset */
2892 bytes_iter, /* tp_iter */
2893 0, /* tp_iternext */
2894 bytes_methods, /* tp_methods */
2895 0, /* tp_members */
2896 0, /* tp_getset */
2897 &PyBaseObject_Type, /* tp_base */
2898 0, /* tp_dict */
2899 0, /* tp_descr_get */
2900 0, /* tp_descr_set */
2901 0, /* tp_dictoffset */
2902 0, /* tp_init */
2903 0, /* tp_alloc */
2904 bytes_new, /* tp_new */
2905 PyObject_Del, /* tp_free */
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00002906};
Guido van Rossuma5d2d552007-10-26 17:39:48 +00002907
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002908void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002909PyBytes_Concat(PyObject **pv, PyObject *w)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 assert(pv != NULL);
2912 if (*pv == NULL)
2913 return;
2914 if (w == NULL) {
Serhiy Storchakaf458a032013-02-02 18:45:22 +02002915 Py_CLEAR(*pv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 return;
2917 }
Antoine Pitrou161d6952014-05-01 14:36:20 +02002918
2919 if (Py_REFCNT(*pv) == 1 && PyBytes_CheckExact(*pv)) {
2920 /* Only one reference, so we can resize in place */
Zachary Warebca96942014-05-06 11:42:37 -05002921 Py_ssize_t oldsize;
Antoine Pitrou161d6952014-05-01 14:36:20 +02002922 Py_buffer wb;
Victor Stinner049e5092014-08-17 22:20:00 +02002923
Antoine Pitrou161d6952014-05-01 14:36:20 +02002924 wb.len = -1;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02002925 if (PyObject_GetBuffer(w, &wb, PyBUF_SIMPLE) != 0) {
Antoine Pitrou161d6952014-05-01 14:36:20 +02002926 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
2927 Py_TYPE(w)->tp_name, Py_TYPE(*pv)->tp_name);
2928 Py_CLEAR(*pv);
2929 return;
2930 }
2931
2932 oldsize = PyBytes_GET_SIZE(*pv);
2933 if (oldsize > PY_SSIZE_T_MAX - wb.len) {
2934 PyErr_NoMemory();
2935 goto error;
2936 }
2937 if (_PyBytes_Resize(pv, oldsize + wb.len) < 0)
2938 goto error;
2939
2940 memcpy(PyBytes_AS_STRING(*pv) + oldsize, wb.buf, wb.len);
2941 PyBuffer_Release(&wb);
2942 return;
2943
2944 error:
2945 PyBuffer_Release(&wb);
2946 Py_CLEAR(*pv);
2947 return;
2948 }
2949
2950 else {
2951 /* Multiple references, need to create new object */
2952 PyObject *v;
2953 v = bytes_concat(*pv, w);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03002954 Py_SETREF(*pv, v);
Antoine Pitrou161d6952014-05-01 14:36:20 +02002955 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002956}
2957
2958void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002959PyBytes_ConcatAndDel(PyObject **pv, PyObject *w)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002960{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 PyBytes_Concat(pv, w);
2962 Py_XDECREF(w);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002963}
2964
2965
Ethan Furmanb95b5612015-01-23 20:05:18 -08002966/* The following function breaks the notion that bytes are immutable:
2967 it changes the size of a bytes object. We get away with this only if there
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002968 is only one module referencing the object. You can also think of it
Ethan Furmanb95b5612015-01-23 20:05:18 -08002969 as creating a new bytes object and destroying the old one, only
2970 more efficiently. In any case, don't use this if the bytes object may
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002971 already be known to some other part of the code...
Ethan Furmanb95b5612015-01-23 20:05:18 -08002972 Note that if there's not enough memory to resize the bytes object, the
2973 original bytes object at *pv is deallocated, *pv is set to NULL, an "out of
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002974 memory" exception is set, and -1 is returned. Else (on success) 0 is
2975 returned, and the value in *pv may or may not be the same as on input.
2976 As always, an extra byte is allocated for a trailing \0 byte (newsize
2977 does *not* include that), and a trailing \0 byte is stored.
2978*/
2979
2980int
2981_PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
2982{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002983 PyObject *v;
2984 PyBytesObject *sv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 v = *pv;
Berker Peksag4a72a7b2016-09-16 17:31:06 +03002986 if (!PyBytes_Check(v) || newsize < 0) {
2987 goto error;
2988 }
2989 if (Py_SIZE(v) == newsize) {
2990 /* return early if newsize equals to v->ob_size */
2991 return 0;
2992 }
2993 if (Py_REFCNT(v) != 1) {
2994 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 }
2996 /* XXX UNREF/NEWREF interface should be more symmetrical */
2997 _Py_DEC_REFTOTAL;
2998 _Py_ForgetReference(v);
2999 *pv = (PyObject *)
Serhiy Storchaka20b39b22014-09-28 11:27:24 +03003000 PyObject_REALLOC(v, PyBytesObject_SIZE + newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 if (*pv == NULL) {
3002 PyObject_Del(v);
3003 PyErr_NoMemory();
3004 return -1;
3005 }
3006 _Py_NewReference(*pv);
3007 sv = (PyBytesObject *) *pv;
3008 Py_SIZE(sv) = newsize;
3009 sv->ob_sval[newsize] = '\0';
3010 sv->ob_shash = -1; /* invalidate cached hash value */
3011 return 0;
Berker Peksag4a72a7b2016-09-16 17:31:06 +03003012error:
3013 *pv = 0;
3014 Py_DECREF(v);
3015 PyErr_BadInternalCall();
3016 return -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003017}
3018
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003019void
3020PyBytes_Fini(void)
3021{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 int i;
Serhiy Storchakaf458a032013-02-02 18:45:22 +02003023 for (i = 0; i < UCHAR_MAX + 1; i++)
3024 Py_CLEAR(characters[i]);
3025 Py_CLEAR(nullstring);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003026}
3027
Benjamin Peterson4116f362008-05-27 00:36:20 +00003028/*********************** Bytes Iterator ****************************/
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003029
3030typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 PyObject_HEAD
3032 Py_ssize_t it_index;
3033 PyBytesObject *it_seq; /* Set to NULL when iterator is exhausted */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003034} striterobject;
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003035
3036static void
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003037striter_dealloc(striterobject *it)
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003038{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 _PyObject_GC_UNTRACK(it);
3040 Py_XDECREF(it->it_seq);
3041 PyObject_GC_Del(it);
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003042}
3043
3044static int
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003045striter_traverse(striterobject *it, visitproc visit, void *arg)
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 Py_VISIT(it->it_seq);
3048 return 0;
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003049}
3050
3051static PyObject *
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003052striter_next(striterobject *it)
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 PyBytesObject *seq;
3055 PyObject *item;
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 assert(it != NULL);
3058 seq = it->it_seq;
3059 if (seq == NULL)
3060 return NULL;
3061 assert(PyBytes_Check(seq));
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 if (it->it_index < PyBytes_GET_SIZE(seq)) {
3064 item = PyLong_FromLong(
3065 (unsigned char)seq->ob_sval[it->it_index]);
3066 if (item != NULL)
3067 ++it->it_index;
3068 return item;
3069 }
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003072 Py_DECREF(seq);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 return NULL;
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003074}
3075
3076static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303077striter_len(striterobject *it, PyObject *Py_UNUSED(ignored))
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 Py_ssize_t len = 0;
3080 if (it->it_seq)
3081 len = PyBytes_GET_SIZE(it->it_seq) - it->it_index;
3082 return PyLong_FromSsize_t(len);
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003083}
3084
3085PyDoc_STRVAR(length_hint_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 "Private method returning an estimate of len(list(it)).");
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003087
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003088static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303089striter_reduce(striterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003090{
3091 if (it->it_seq != NULL) {
Antoine Pitroua7013882012-04-05 00:04:20 +02003092 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003093 it->it_seq, it->it_index);
3094 } else {
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02003095 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003096 }
3097}
3098
3099PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3100
3101static PyObject *
3102striter_setstate(striterobject *it, PyObject *state)
3103{
3104 Py_ssize_t index = PyLong_AsSsize_t(state);
3105 if (index == -1 && PyErr_Occurred())
3106 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00003107 if (it->it_seq != NULL) {
3108 if (index < 0)
3109 index = 0;
3110 else if (index > PyBytes_GET_SIZE(it->it_seq))
3111 index = PyBytes_GET_SIZE(it->it_seq); /* iterator exhausted */
3112 it->it_index = index;
3113 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003114 Py_RETURN_NONE;
3115}
3116
3117PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
3118
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003119static PyMethodDef striter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 {"__length_hint__", (PyCFunction)striter_len, METH_NOARGS,
3121 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003122 {"__reduce__", (PyCFunction)striter_reduce, METH_NOARGS,
3123 reduce_doc},
3124 {"__setstate__", (PyCFunction)striter_setstate, METH_O,
3125 setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003126 {NULL, NULL} /* sentinel */
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003127};
3128
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003129PyTypeObject PyBytesIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3131 "bytes_iterator", /* tp_name */
3132 sizeof(striterobject), /* tp_basicsize */
3133 0, /* tp_itemsize */
3134 /* methods */
3135 (destructor)striter_dealloc, /* tp_dealloc */
3136 0, /* tp_print */
3137 0, /* tp_getattr */
3138 0, /* tp_setattr */
3139 0, /* tp_reserved */
3140 0, /* tp_repr */
3141 0, /* tp_as_number */
3142 0, /* tp_as_sequence */
3143 0, /* tp_as_mapping */
3144 0, /* tp_hash */
3145 0, /* tp_call */
3146 0, /* tp_str */
3147 PyObject_GenericGetAttr, /* tp_getattro */
3148 0, /* tp_setattro */
3149 0, /* tp_as_buffer */
3150 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3151 0, /* tp_doc */
3152 (traverseproc)striter_traverse, /* tp_traverse */
3153 0, /* tp_clear */
3154 0, /* tp_richcompare */
3155 0, /* tp_weaklistoffset */
3156 PyObject_SelfIter, /* tp_iter */
3157 (iternextfunc)striter_next, /* tp_iternext */
3158 striter_methods, /* tp_methods */
3159 0,
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003160};
3161
3162static PyObject *
Benjamin Peterson80688ef2009-04-18 15:17:02 +00003163bytes_iter(PyObject *seq)
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003164{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 striterobject *it;
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 if (!PyBytes_Check(seq)) {
3168 PyErr_BadInternalCall();
3169 return NULL;
3170 }
3171 it = PyObject_GC_New(striterobject, &PyBytesIter_Type);
3172 if (it == NULL)
3173 return NULL;
3174 it->it_index = 0;
3175 Py_INCREF(seq);
3176 it->it_seq = (PyBytesObject *)seq;
3177 _PyObject_GC_TRACK(it);
3178 return (PyObject *)it;
Guido van Rossuma5d2d552007-10-26 17:39:48 +00003179}
Victor Stinner00165072015-10-09 01:53:21 +02003180
3181
3182/* _PyBytesWriter API */
3183
3184#ifdef MS_WINDOWS
3185 /* On Windows, overallocate by 50% is the best factor */
3186# define OVERALLOCATE_FACTOR 2
3187#else
3188 /* On Linux, overallocate by 25% is the best factor */
3189# define OVERALLOCATE_FACTOR 4
3190#endif
3191
3192void
3193_PyBytesWriter_Init(_PyBytesWriter *writer)
3194{
Victor Stinner661aacc2015-10-14 09:41:48 +02003195 /* Set all attributes before small_buffer to 0 */
3196 memset(writer, 0, offsetof(_PyBytesWriter, small_buffer));
Victor Stinner00165072015-10-09 01:53:21 +02003197#ifdef Py_DEBUG
Victor Stinnerb3653a32015-10-09 03:38:24 +02003198 memset(writer->small_buffer, 0xCB, sizeof(writer->small_buffer));
Victor Stinner00165072015-10-09 01:53:21 +02003199#endif
3200}
3201
3202void
3203_PyBytesWriter_Dealloc(_PyBytesWriter *writer)
3204{
3205 Py_CLEAR(writer->buffer);
3206}
3207
3208Py_LOCAL_INLINE(char*)
3209_PyBytesWriter_AsString(_PyBytesWriter *writer)
3210{
Victor Stinner661aacc2015-10-14 09:41:48 +02003211 if (writer->use_small_buffer) {
Victor Stinner00165072015-10-09 01:53:21 +02003212 assert(writer->buffer == NULL);
Victor Stinnerb3653a32015-10-09 03:38:24 +02003213 return writer->small_buffer;
Victor Stinner00165072015-10-09 01:53:21 +02003214 }
Victor Stinner661aacc2015-10-14 09:41:48 +02003215 else if (writer->use_bytearray) {
3216 assert(writer->buffer != NULL);
3217 return PyByteArray_AS_STRING(writer->buffer);
3218 }
3219 else {
3220 assert(writer->buffer != NULL);
3221 return PyBytes_AS_STRING(writer->buffer);
3222 }
Victor Stinner00165072015-10-09 01:53:21 +02003223}
3224
3225Py_LOCAL_INLINE(Py_ssize_t)
Victor Stinner2bf89932015-10-14 11:25:33 +02003226_PyBytesWriter_GetSize(_PyBytesWriter *writer, char *str)
Victor Stinner00165072015-10-09 01:53:21 +02003227{
3228 char *start = _PyBytesWriter_AsString(writer);
3229 assert(str != NULL);
3230 assert(str >= start);
Victor Stinnerb3653a32015-10-09 03:38:24 +02003231 assert(str - start <= writer->allocated);
Victor Stinner00165072015-10-09 01:53:21 +02003232 return str - start;
3233}
3234
3235Py_LOCAL_INLINE(void)
3236_PyBytesWriter_CheckConsistency(_PyBytesWriter *writer, char *str)
3237{
3238#ifdef Py_DEBUG
3239 char *start, *end;
3240
Victor Stinner661aacc2015-10-14 09:41:48 +02003241 if (writer->use_small_buffer) {
Victor Stinner00165072015-10-09 01:53:21 +02003242 assert(writer->buffer == NULL);
3243 }
Victor Stinner661aacc2015-10-14 09:41:48 +02003244 else {
3245 assert(writer->buffer != NULL);
3246 if (writer->use_bytearray)
3247 assert(PyByteArray_CheckExact(writer->buffer));
3248 else
3249 assert(PyBytes_CheckExact(writer->buffer));
3250 assert(Py_REFCNT(writer->buffer) == 1);
3251 }
Victor Stinner00165072015-10-09 01:53:21 +02003252
Victor Stinner661aacc2015-10-14 09:41:48 +02003253 if (writer->use_bytearray) {
3254 /* bytearray has its own overallocation algorithm,
3255 writer overallocation must be disabled */
3256 assert(!writer->overallocate);
3257 }
3258
3259 assert(0 <= writer->allocated);
Victor Stinner53926a12015-10-09 12:37:03 +02003260 assert(0 <= writer->min_size && writer->min_size <= writer->allocated);
Victor Stinner00165072015-10-09 01:53:21 +02003261 /* the last byte must always be null */
Victor Stinner661aacc2015-10-14 09:41:48 +02003262 start = _PyBytesWriter_AsString(writer);
Victor Stinner00165072015-10-09 01:53:21 +02003263 assert(start[writer->allocated] == 0);
3264
3265 end = start + writer->allocated;
3266 assert(str != NULL);
3267 assert(start <= str && str <= end);
3268#endif
3269}
3270
Victor Stinnerc29e29b2015-10-12 13:12:54 +02003271void*
Victor Stinnerc5c3ba42015-10-14 13:56:47 +02003272_PyBytesWriter_Resize(_PyBytesWriter *writer, void *str, Py_ssize_t size)
Victor Stinner00165072015-10-09 01:53:21 +02003273{
3274 Py_ssize_t allocated, pos;
3275
3276 _PyBytesWriter_CheckConsistency(writer, str);
Victor Stinnerc5c3ba42015-10-14 13:56:47 +02003277 assert(writer->allocated < size);
Victor Stinner00165072015-10-09 01:53:21 +02003278
Victor Stinnerc5c3ba42015-10-14 13:56:47 +02003279 allocated = size;
Victor Stinner00165072015-10-09 01:53:21 +02003280 if (writer->overallocate
3281 && allocated <= (PY_SSIZE_T_MAX - allocated / OVERALLOCATE_FACTOR)) {
3282 /* overallocate to limit the number of realloc() */
3283 allocated += allocated / OVERALLOCATE_FACTOR;
3284 }
3285
Victor Stinner2bf89932015-10-14 11:25:33 +02003286 pos = _PyBytesWriter_GetSize(writer, str);
Victor Stinnerb3653a32015-10-09 03:38:24 +02003287 if (!writer->use_small_buffer) {
Victor Stinner661aacc2015-10-14 09:41:48 +02003288 if (writer->use_bytearray) {
3289 if (PyByteArray_Resize(writer->buffer, allocated))
3290 goto error;
3291 /* writer->allocated can be smaller than writer->buffer->ob_alloc,
3292 but we cannot use ob_alloc because bytes may need to be moved
3293 to use the whole buffer. bytearray uses an internal optimization
3294 to avoid moving or copying bytes when bytes are removed at the
3295 beginning (ex: del bytearray[:1]). */
3296 }
3297 else {
3298 if (_PyBytes_Resize(&writer->buffer, allocated))
3299 goto error;
Victor Stinner00165072015-10-09 01:53:21 +02003300 }
3301 }
3302 else {
3303 /* convert from stack buffer to bytes object buffer */
3304 assert(writer->buffer == NULL);
3305
Victor Stinner661aacc2015-10-14 09:41:48 +02003306 if (writer->use_bytearray)
3307 writer->buffer = PyByteArray_FromStringAndSize(NULL, allocated);
3308 else
3309 writer->buffer = PyBytes_FromStringAndSize(NULL, allocated);
Victor Stinner00165072015-10-09 01:53:21 +02003310 if (writer->buffer == NULL)
Victor Stinner661aacc2015-10-14 09:41:48 +02003311 goto error;
Victor Stinner00165072015-10-09 01:53:21 +02003312
3313 if (pos != 0) {
Victor Stinner661aacc2015-10-14 09:41:48 +02003314 char *dest;
3315 if (writer->use_bytearray)
3316 dest = PyByteArray_AS_STRING(writer->buffer);
3317 else
3318 dest = PyBytes_AS_STRING(writer->buffer);
Christian Heimesf051e432016-09-13 20:22:02 +02003319 memcpy(dest,
Victor Stinnerb3653a32015-10-09 03:38:24 +02003320 writer->small_buffer,
Victor Stinner00165072015-10-09 01:53:21 +02003321 pos);
3322 }
3323
Victor Stinnerb3653a32015-10-09 03:38:24 +02003324 writer->use_small_buffer = 0;
Victor Stinner00165072015-10-09 01:53:21 +02003325#ifdef Py_DEBUG
Victor Stinnerb3653a32015-10-09 03:38:24 +02003326 memset(writer->small_buffer, 0xDB, sizeof(writer->small_buffer));
Victor Stinner00165072015-10-09 01:53:21 +02003327#endif
Victor Stinner00165072015-10-09 01:53:21 +02003328 }
3329 writer->allocated = allocated;
3330
3331 str = _PyBytesWriter_AsString(writer) + pos;
3332 _PyBytesWriter_CheckConsistency(writer, str);
3333 return str;
Victor Stinner661aacc2015-10-14 09:41:48 +02003334
3335error:
3336 _PyBytesWriter_Dealloc(writer);
3337 return NULL;
Victor Stinner00165072015-10-09 01:53:21 +02003338}
3339
Victor Stinnerc5c3ba42015-10-14 13:56:47 +02003340void*
3341_PyBytesWriter_Prepare(_PyBytesWriter *writer, void *str, Py_ssize_t size)
3342{
3343 Py_ssize_t new_min_size;
3344
3345 _PyBytesWriter_CheckConsistency(writer, str);
3346 assert(size >= 0);
3347
3348 if (size == 0) {
3349 /* nothing to do */
3350 return str;
3351 }
3352
3353 if (writer->min_size > PY_SSIZE_T_MAX - size) {
3354 PyErr_NoMemory();
3355 _PyBytesWriter_Dealloc(writer);
3356 return NULL;
3357 }
3358 new_min_size = writer->min_size + size;
3359
3360 if (new_min_size > writer->allocated)
3361 str = _PyBytesWriter_Resize(writer, str, new_min_size);
3362
3363 writer->min_size = new_min_size;
3364 return str;
3365}
3366
Victor Stinner00165072015-10-09 01:53:21 +02003367/* Allocate the buffer to write size bytes.
3368 Return the pointer to the beginning of buffer data.
3369 Raise an exception and return NULL on error. */
Victor Stinnerc29e29b2015-10-12 13:12:54 +02003370void*
Victor Stinner00165072015-10-09 01:53:21 +02003371_PyBytesWriter_Alloc(_PyBytesWriter *writer, Py_ssize_t size)
3372{
3373 /* ensure that _PyBytesWriter_Alloc() is only called once */
Victor Stinner53926a12015-10-09 12:37:03 +02003374 assert(writer->min_size == 0 && writer->buffer == NULL);
Victor Stinner00165072015-10-09 01:53:21 +02003375 assert(size >= 0);
3376
Victor Stinnerb3653a32015-10-09 03:38:24 +02003377 writer->use_small_buffer = 1;
Victor Stinnerb13b97d2015-10-09 02:52:16 +02003378#ifdef Py_DEBUG
Victor Stinnerb3653a32015-10-09 03:38:24 +02003379 writer->allocated = sizeof(writer->small_buffer) - 1;
Victor Stinnerf6358a72015-10-14 12:02:39 +02003380 /* In debug mode, don't use the full small buffer because it is less
3381 efficient than bytes and bytearray objects to detect buffer underflow
3382 and buffer overflow. Use 10 bytes of the small buffer to test also
3383 code using the smaller buffer in debug mode.
3384
3385 Don't modify the _PyBytesWriter structure (use a shorter small buffer)
3386 in debug mode to also be able to detect stack overflow when running
3387 tests in debug mode. The _PyBytesWriter is large (more than 512 bytes),
3388 if Py_EnterRecursiveCall() is not used in deep C callback, we may hit a
3389 stack overflow. */
3390 writer->allocated = Py_MIN(writer->allocated, 10);
3391 /* _PyBytesWriter_CheckConsistency() requires the last byte to be 0,
3392 to detect buffer overflow */
Victor Stinnerb3653a32015-10-09 03:38:24 +02003393 writer->small_buffer[writer->allocated] = 0;
Victor Stinner00165072015-10-09 01:53:21 +02003394#else
Victor Stinnerb3653a32015-10-09 03:38:24 +02003395 writer->allocated = sizeof(writer->small_buffer);
Victor Stinner00165072015-10-09 01:53:21 +02003396#endif
Victor Stinnerb3653a32015-10-09 03:38:24 +02003397 return _PyBytesWriter_Prepare(writer, writer->small_buffer, size);
Victor Stinner00165072015-10-09 01:53:21 +02003398}
3399
3400PyObject *
Victor Stinnerc29e29b2015-10-12 13:12:54 +02003401_PyBytesWriter_Finish(_PyBytesWriter *writer, void *str)
Victor Stinner00165072015-10-09 01:53:21 +02003402{
Victor Stinner2bf89932015-10-14 11:25:33 +02003403 Py_ssize_t size;
Victor Stinner00165072015-10-09 01:53:21 +02003404 PyObject *result;
3405
3406 _PyBytesWriter_CheckConsistency(writer, str);
3407
Victor Stinner2bf89932015-10-14 11:25:33 +02003408 size = _PyBytesWriter_GetSize(writer, str);
3409 if (size == 0 && !writer->use_bytearray) {
Victor Stinner6c2cdae2015-10-12 13:29:43 +02003410 Py_CLEAR(writer->buffer);
3411 /* Get the empty byte string singleton */
3412 result = PyBytes_FromStringAndSize(NULL, 0);
3413 }
3414 else if (writer->use_small_buffer) {
Victor Stinnere914d412016-04-15 17:52:27 +02003415 if (writer->use_bytearray) {
3416 result = PyByteArray_FromStringAndSize(writer->small_buffer, size);
3417 }
3418 else {
3419 result = PyBytes_FromStringAndSize(writer->small_buffer, size);
3420 }
Victor Stinner6c2cdae2015-10-12 13:29:43 +02003421 }
3422 else {
3423 result = writer->buffer;
3424 writer->buffer = NULL;
3425
Victor Stinner2bf89932015-10-14 11:25:33 +02003426 if (size != writer->allocated) {
Victor Stinner661aacc2015-10-14 09:41:48 +02003427 if (writer->use_bytearray) {
Victor Stinner2bf89932015-10-14 11:25:33 +02003428 if (PyByteArray_Resize(result, size)) {
Victor Stinner661aacc2015-10-14 09:41:48 +02003429 Py_DECREF(result);
3430 return NULL;
3431 }
3432 }
3433 else {
Victor Stinner2bf89932015-10-14 11:25:33 +02003434 if (_PyBytes_Resize(&result, size)) {
Victor Stinner661aacc2015-10-14 09:41:48 +02003435 assert(result == NULL);
3436 return NULL;
3437 }
Victor Stinner00165072015-10-09 01:53:21 +02003438 }
3439 }
Victor Stinner00165072015-10-09 01:53:21 +02003440 }
Victor Stinner00165072015-10-09 01:53:21 +02003441 return result;
3442}
Victor Stinnerce179bf2015-10-09 12:57:22 +02003443
Victor Stinnerc29e29b2015-10-12 13:12:54 +02003444void*
Victor Stinnere9aa5952015-10-12 13:57:47 +02003445_PyBytesWriter_WriteBytes(_PyBytesWriter *writer, void *ptr,
Victor Stinnerc29e29b2015-10-12 13:12:54 +02003446 const void *bytes, Py_ssize_t size)
Victor Stinnerce179bf2015-10-09 12:57:22 +02003447{
Victor Stinnere9aa5952015-10-12 13:57:47 +02003448 char *str = (char *)ptr;
3449
Victor Stinnerce179bf2015-10-09 12:57:22 +02003450 str = _PyBytesWriter_Prepare(writer, str, size);
3451 if (str == NULL)
3452 return NULL;
3453
Christian Heimesf051e432016-09-13 20:22:02 +02003454 memcpy(str, bytes, size);
Victor Stinnerce179bf2015-10-09 12:57:22 +02003455 str += size;
3456
3457 return str;
3458}