1 | n/a | /* bytes object implementation */ |
---|
2 | n/a | |
---|
3 | n/a | #define PY_SSIZE_T_CLEAN |
---|
4 | n/a | |
---|
5 | n/a | #include "Python.h" |
---|
6 | n/a | |
---|
7 | n/a | #include "bytes_methods.h" |
---|
8 | n/a | #include "pystrhex.h" |
---|
9 | n/a | #include <stddef.h> |
---|
10 | n/a | |
---|
11 | n/a | /*[clinic input] |
---|
12 | n/a | class bytes "PyBytesObject *" "&PyBytes_Type" |
---|
13 | n/a | [clinic start generated code]*/ |
---|
14 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=7a238f965d64892b]*/ |
---|
15 | n/a | |
---|
16 | n/a | #include "clinic/bytesobject.c.h" |
---|
17 | n/a | |
---|
18 | n/a | #ifdef COUNT_ALLOCS |
---|
19 | n/a | Py_ssize_t null_strings, one_strings; |
---|
20 | n/a | #endif |
---|
21 | n/a | |
---|
22 | n/a | static PyBytesObject *characters[UCHAR_MAX + 1]; |
---|
23 | n/a | static PyBytesObject *nullstring; |
---|
24 | n/a | |
---|
25 | n/a | /* PyBytesObject_SIZE gives the basic size of a string; any memory allocation |
---|
26 | n/a | for a string of length n should request PyBytesObject_SIZE + n bytes. |
---|
27 | n/a | |
---|
28 | n/a | Using PyBytesObject_SIZE instead of sizeof(PyBytesObject) saves |
---|
29 | n/a | 3 bytes per string allocation on a typical system. |
---|
30 | n/a | */ |
---|
31 | n/a | #define PyBytesObject_SIZE (offsetof(PyBytesObject, ob_sval) + 1) |
---|
32 | n/a | |
---|
33 | n/a | /* Forward declaration */ |
---|
34 | n/a | Py_LOCAL_INLINE(Py_ssize_t) _PyBytesWriter_GetSize(_PyBytesWriter *writer, |
---|
35 | n/a | char *str); |
---|
36 | n/a | |
---|
37 | n/a | /* |
---|
38 | n/a | For PyBytes_FromString(), the parameter `str' points to a null-terminated |
---|
39 | n/a | string containing exactly `size' bytes. |
---|
40 | n/a | |
---|
41 | n/a | For PyBytes_FromStringAndSize(), the parameter `str' is |
---|
42 | n/a | either NULL or else points to a string containing at least `size' bytes. |
---|
43 | n/a | For PyBytes_FromStringAndSize(), the string in the `str' parameter does |
---|
44 | n/a | not have to be null-terminated. (Therefore it is safe to construct a |
---|
45 | n/a | substring by calling `PyBytes_FromStringAndSize(origstring, substrlen)'.) |
---|
46 | n/a | If `str' is NULL then PyBytes_FromStringAndSize() will allocate `size+1' |
---|
47 | n/a | bytes (setting the last byte to the null terminating character) and you can |
---|
48 | n/a | fill in the data yourself. If `str' is non-NULL then the resulting |
---|
49 | n/a | PyBytes object must be treated as immutable and you must not fill in nor |
---|
50 | n/a | alter the data yourself, since the strings may be shared. |
---|
51 | n/a | |
---|
52 | n/a | The PyObject member `op->ob_size', which denotes the number of "extra |
---|
53 | n/a | items" in a variable-size object, will contain the number of bytes |
---|
54 | n/a | allocated for string data, not counting the null terminating character. |
---|
55 | n/a | It is therefore equal to the `size' parameter (for |
---|
56 | n/a | PyBytes_FromStringAndSize()) or the length of the string in the `str' |
---|
57 | n/a | parameter (for PyBytes_FromString()). |
---|
58 | n/a | */ |
---|
59 | n/a | static PyObject * |
---|
60 | n/a | _PyBytes_FromSize(Py_ssize_t size, int use_calloc) |
---|
61 | n/a | { |
---|
62 | n/a | PyBytesObject *op; |
---|
63 | n/a | assert(size >= 0); |
---|
64 | n/a | |
---|
65 | n/a | if (size == 0 && (op = nullstring) != NULL) { |
---|
66 | n/a | #ifdef COUNT_ALLOCS |
---|
67 | n/a | null_strings++; |
---|
68 | n/a | #endif |
---|
69 | n/a | Py_INCREF(op); |
---|
70 | n/a | return (PyObject *)op; |
---|
71 | n/a | } |
---|
72 | n/a | |
---|
73 | n/a | if ((size_t)size > (size_t)PY_SSIZE_T_MAX - PyBytesObject_SIZE) { |
---|
74 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
75 | n/a | "byte string is too large"); |
---|
76 | n/a | return NULL; |
---|
77 | n/a | } |
---|
78 | n/a | |
---|
79 | n/a | /* Inline PyObject_NewVar */ |
---|
80 | n/a | if (use_calloc) |
---|
81 | n/a | op = (PyBytesObject *)PyObject_Calloc(1, PyBytesObject_SIZE + size); |
---|
82 | n/a | else |
---|
83 | n/a | op = (PyBytesObject *)PyObject_Malloc(PyBytesObject_SIZE + size); |
---|
84 | n/a | if (op == NULL) |
---|
85 | n/a | return PyErr_NoMemory(); |
---|
86 | n/a | (void)PyObject_INIT_VAR(op, &PyBytes_Type, size); |
---|
87 | n/a | op->ob_shash = -1; |
---|
88 | n/a | if (!use_calloc) |
---|
89 | n/a | op->ob_sval[size] = '\0'; |
---|
90 | n/a | /* empty byte string singleton */ |
---|
91 | n/a | if (size == 0) { |
---|
92 | n/a | nullstring = op; |
---|
93 | n/a | Py_INCREF(op); |
---|
94 | n/a | } |
---|
95 | n/a | return (PyObject *) op; |
---|
96 | n/a | } |
---|
97 | n/a | |
---|
98 | n/a | PyObject * |
---|
99 | n/a | PyBytes_FromStringAndSize(const char *str, Py_ssize_t size) |
---|
100 | n/a | { |
---|
101 | n/a | PyBytesObject *op; |
---|
102 | n/a | if (size < 0) { |
---|
103 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
104 | n/a | "Negative size passed to PyBytes_FromStringAndSize"); |
---|
105 | n/a | return NULL; |
---|
106 | n/a | } |
---|
107 | n/a | if (size == 1 && str != NULL && |
---|
108 | n/a | (op = characters[*str & UCHAR_MAX]) != NULL) |
---|
109 | n/a | { |
---|
110 | n/a | #ifdef COUNT_ALLOCS |
---|
111 | n/a | one_strings++; |
---|
112 | n/a | #endif |
---|
113 | n/a | Py_INCREF(op); |
---|
114 | n/a | return (PyObject *)op; |
---|
115 | n/a | } |
---|
116 | n/a | |
---|
117 | n/a | op = (PyBytesObject *)_PyBytes_FromSize(size, 0); |
---|
118 | n/a | if (op == NULL) |
---|
119 | n/a | return NULL; |
---|
120 | n/a | if (str == NULL) |
---|
121 | n/a | return (PyObject *) op; |
---|
122 | n/a | |
---|
123 | n/a | memcpy(op->ob_sval, str, size); |
---|
124 | n/a | /* share short strings */ |
---|
125 | n/a | if (size == 1) { |
---|
126 | n/a | characters[*str & UCHAR_MAX] = op; |
---|
127 | n/a | Py_INCREF(op); |
---|
128 | n/a | } |
---|
129 | n/a | return (PyObject *) op; |
---|
130 | n/a | } |
---|
131 | n/a | |
---|
132 | n/a | PyObject * |
---|
133 | n/a | PyBytes_FromString(const char *str) |
---|
134 | n/a | { |
---|
135 | n/a | size_t size; |
---|
136 | n/a | PyBytesObject *op; |
---|
137 | n/a | |
---|
138 | n/a | assert(str != NULL); |
---|
139 | n/a | size = strlen(str); |
---|
140 | n/a | if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) { |
---|
141 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
142 | n/a | "byte string is too long"); |
---|
143 | n/a | return NULL; |
---|
144 | n/a | } |
---|
145 | n/a | if (size == 0 && (op = nullstring) != NULL) { |
---|
146 | n/a | #ifdef COUNT_ALLOCS |
---|
147 | n/a | null_strings++; |
---|
148 | n/a | #endif |
---|
149 | n/a | Py_INCREF(op); |
---|
150 | n/a | return (PyObject *)op; |
---|
151 | n/a | } |
---|
152 | n/a | if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { |
---|
153 | n/a | #ifdef COUNT_ALLOCS |
---|
154 | n/a | one_strings++; |
---|
155 | n/a | #endif |
---|
156 | n/a | Py_INCREF(op); |
---|
157 | n/a | return (PyObject *)op; |
---|
158 | n/a | } |
---|
159 | n/a | |
---|
160 | n/a | /* Inline PyObject_NewVar */ |
---|
161 | n/a | op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size); |
---|
162 | n/a | if (op == NULL) |
---|
163 | n/a | return PyErr_NoMemory(); |
---|
164 | n/a | (void)PyObject_INIT_VAR(op, &PyBytes_Type, size); |
---|
165 | n/a | op->ob_shash = -1; |
---|
166 | n/a | memcpy(op->ob_sval, str, size+1); |
---|
167 | n/a | /* share short strings */ |
---|
168 | n/a | if (size == 0) { |
---|
169 | n/a | nullstring = op; |
---|
170 | n/a | Py_INCREF(op); |
---|
171 | n/a | } else if (size == 1) { |
---|
172 | n/a | characters[*str & UCHAR_MAX] = op; |
---|
173 | n/a | Py_INCREF(op); |
---|
174 | n/a | } |
---|
175 | n/a | return (PyObject *) op; |
---|
176 | n/a | } |
---|
177 | n/a | |
---|
178 | n/a | PyObject * |
---|
179 | n/a | PyBytes_FromFormatV(const char *format, va_list vargs) |
---|
180 | n/a | { |
---|
181 | n/a | char *s; |
---|
182 | n/a | const char *f; |
---|
183 | n/a | const char *p; |
---|
184 | n/a | Py_ssize_t prec; |
---|
185 | n/a | int longflag; |
---|
186 | n/a | int size_tflag; |
---|
187 | n/a | /* Longest 64-bit formatted numbers: |
---|
188 | n/a | - "18446744073709551615\0" (21 bytes) |
---|
189 | n/a | - "-9223372036854775808\0" (21 bytes) |
---|
190 | n/a | Decimal takes the most space (it isn't enough for octal.) |
---|
191 | n/a | |
---|
192 | n/a | Longest 64-bit pointer representation: |
---|
193 | n/a | "0xffffffffffffffff\0" (19 bytes). */ |
---|
194 | n/a | char buffer[21]; |
---|
195 | n/a | _PyBytesWriter writer; |
---|
196 | n/a | |
---|
197 | n/a | _PyBytesWriter_Init(&writer); |
---|
198 | n/a | |
---|
199 | n/a | s = _PyBytesWriter_Alloc(&writer, strlen(format)); |
---|
200 | n/a | if (s == NULL) |
---|
201 | n/a | return NULL; |
---|
202 | n/a | writer.overallocate = 1; |
---|
203 | n/a | |
---|
204 | n/a | #define WRITE_BYTES(str) \ |
---|
205 | n/a | do { \ |
---|
206 | n/a | s = _PyBytesWriter_WriteBytes(&writer, s, (str), strlen(str)); \ |
---|
207 | n/a | if (s == NULL) \ |
---|
208 | n/a | goto error; \ |
---|
209 | n/a | } while (0) |
---|
210 | n/a | |
---|
211 | n/a | for (f = format; *f; f++) { |
---|
212 | n/a | if (*f != '%') { |
---|
213 | n/a | *s++ = *f; |
---|
214 | n/a | continue; |
---|
215 | n/a | } |
---|
216 | n/a | |
---|
217 | n/a | p = f++; |
---|
218 | n/a | |
---|
219 | n/a | /* ignore the width (ex: 10 in "%10s") */ |
---|
220 | n/a | while (Py_ISDIGIT(*f)) |
---|
221 | n/a | f++; |
---|
222 | n/a | |
---|
223 | n/a | /* parse the precision (ex: 10 in "%.10s") */ |
---|
224 | n/a | prec = 0; |
---|
225 | n/a | if (*f == '.') { |
---|
226 | n/a | f++; |
---|
227 | n/a | for (; Py_ISDIGIT(*f); f++) { |
---|
228 | n/a | prec = (prec * 10) + (*f - '0'); |
---|
229 | n/a | } |
---|
230 | n/a | } |
---|
231 | n/a | |
---|
232 | n/a | while (*f && *f != '%' && !Py_ISALPHA(*f)) |
---|
233 | n/a | f++; |
---|
234 | n/a | |
---|
235 | n/a | /* handle the long flag ('l'), but only for %ld and %lu. |
---|
236 | n/a | others can be added when necessary. */ |
---|
237 | n/a | longflag = 0; |
---|
238 | n/a | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
---|
239 | n/a | longflag = 1; |
---|
240 | n/a | ++f; |
---|
241 | n/a | } |
---|
242 | n/a | |
---|
243 | n/a | /* handle the size_t flag ('z'). */ |
---|
244 | n/a | size_tflag = 0; |
---|
245 | n/a | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
---|
246 | n/a | size_tflag = 1; |
---|
247 | n/a | ++f; |
---|
248 | n/a | } |
---|
249 | n/a | |
---|
250 | n/a | /* subtract bytes preallocated for the format string |
---|
251 | n/a | (ex: 2 for "%s") */ |
---|
252 | n/a | writer.min_size -= (f - p + 1); |
---|
253 | n/a | |
---|
254 | n/a | switch (*f) { |
---|
255 | n/a | case 'c': |
---|
256 | n/a | { |
---|
257 | n/a | int c = va_arg(vargs, int); |
---|
258 | n/a | if (c < 0 || c > 255) { |
---|
259 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
260 | n/a | "PyBytes_FromFormatV(): %c format " |
---|
261 | n/a | "expects an integer in range [0; 255]"); |
---|
262 | n/a | goto error; |
---|
263 | n/a | } |
---|
264 | n/a | writer.min_size++; |
---|
265 | n/a | *s++ = (unsigned char)c; |
---|
266 | n/a | break; |
---|
267 | n/a | } |
---|
268 | n/a | |
---|
269 | n/a | case 'd': |
---|
270 | n/a | if (longflag) |
---|
271 | n/a | sprintf(buffer, "%ld", va_arg(vargs, long)); |
---|
272 | n/a | else if (size_tflag) |
---|
273 | n/a | sprintf(buffer, "%" PY_FORMAT_SIZE_T "d", |
---|
274 | n/a | va_arg(vargs, Py_ssize_t)); |
---|
275 | n/a | else |
---|
276 | n/a | sprintf(buffer, "%d", va_arg(vargs, int)); |
---|
277 | n/a | assert(strlen(buffer) < sizeof(buffer)); |
---|
278 | n/a | WRITE_BYTES(buffer); |
---|
279 | n/a | break; |
---|
280 | n/a | |
---|
281 | n/a | case 'u': |
---|
282 | n/a | if (longflag) |
---|
283 | n/a | sprintf(buffer, "%lu", |
---|
284 | n/a | va_arg(vargs, unsigned long)); |
---|
285 | n/a | else if (size_tflag) |
---|
286 | n/a | sprintf(buffer, "%" PY_FORMAT_SIZE_T "u", |
---|
287 | n/a | va_arg(vargs, size_t)); |
---|
288 | n/a | else |
---|
289 | n/a | sprintf(buffer, "%u", |
---|
290 | n/a | va_arg(vargs, unsigned int)); |
---|
291 | n/a | assert(strlen(buffer) < sizeof(buffer)); |
---|
292 | n/a | WRITE_BYTES(buffer); |
---|
293 | n/a | break; |
---|
294 | n/a | |
---|
295 | n/a | case 'i': |
---|
296 | n/a | sprintf(buffer, "%i", va_arg(vargs, int)); |
---|
297 | n/a | assert(strlen(buffer) < sizeof(buffer)); |
---|
298 | n/a | WRITE_BYTES(buffer); |
---|
299 | n/a | break; |
---|
300 | n/a | |
---|
301 | n/a | case 'x': |
---|
302 | n/a | sprintf(buffer, "%x", va_arg(vargs, int)); |
---|
303 | n/a | assert(strlen(buffer) < sizeof(buffer)); |
---|
304 | n/a | WRITE_BYTES(buffer); |
---|
305 | n/a | break; |
---|
306 | n/a | |
---|
307 | n/a | case 's': |
---|
308 | n/a | { |
---|
309 | n/a | Py_ssize_t i; |
---|
310 | n/a | |
---|
311 | n/a | p = va_arg(vargs, const char*); |
---|
312 | n/a | i = strlen(p); |
---|
313 | n/a | if (prec > 0 && i > prec) |
---|
314 | n/a | i = prec; |
---|
315 | n/a | s = _PyBytesWriter_WriteBytes(&writer, s, p, i); |
---|
316 | n/a | if (s == NULL) |
---|
317 | n/a | goto error; |
---|
318 | n/a | break; |
---|
319 | n/a | } |
---|
320 | n/a | |
---|
321 | n/a | case 'p': |
---|
322 | n/a | sprintf(buffer, "%p", va_arg(vargs, void*)); |
---|
323 | n/a | assert(strlen(buffer) < sizeof(buffer)); |
---|
324 | n/a | /* %p is ill-defined: ensure leading 0x. */ |
---|
325 | n/a | if (buffer[1] == 'X') |
---|
326 | n/a | buffer[1] = 'x'; |
---|
327 | n/a | else if (buffer[1] != 'x') { |
---|
328 | n/a | memmove(buffer+2, buffer, strlen(buffer)+1); |
---|
329 | n/a | buffer[0] = '0'; |
---|
330 | n/a | buffer[1] = 'x'; |
---|
331 | n/a | } |
---|
332 | n/a | WRITE_BYTES(buffer); |
---|
333 | n/a | break; |
---|
334 | n/a | |
---|
335 | n/a | case '%': |
---|
336 | n/a | writer.min_size++; |
---|
337 | n/a | *s++ = '%'; |
---|
338 | n/a | break; |
---|
339 | n/a | |
---|
340 | n/a | default: |
---|
341 | n/a | if (*f == 0) { |
---|
342 | n/a | /* fix min_size if we reached the end of the format string */ |
---|
343 | n/a | writer.min_size++; |
---|
344 | n/a | } |
---|
345 | n/a | |
---|
346 | n/a | /* invalid format string: copy unformatted string and exit */ |
---|
347 | n/a | WRITE_BYTES(p); |
---|
348 | n/a | return _PyBytesWriter_Finish(&writer, s); |
---|
349 | n/a | } |
---|
350 | n/a | } |
---|
351 | n/a | |
---|
352 | n/a | #undef WRITE_BYTES |
---|
353 | n/a | |
---|
354 | n/a | return _PyBytesWriter_Finish(&writer, s); |
---|
355 | n/a | |
---|
356 | n/a | error: |
---|
357 | n/a | _PyBytesWriter_Dealloc(&writer); |
---|
358 | n/a | return NULL; |
---|
359 | n/a | } |
---|
360 | n/a | |
---|
361 | n/a | PyObject * |
---|
362 | n/a | PyBytes_FromFormat(const char *format, ...) |
---|
363 | n/a | { |
---|
364 | n/a | PyObject* ret; |
---|
365 | n/a | va_list vargs; |
---|
366 | n/a | |
---|
367 | n/a | #ifdef HAVE_STDARG_PROTOTYPES |
---|
368 | n/a | va_start(vargs, format); |
---|
369 | n/a | #else |
---|
370 | n/a | va_start(vargs); |
---|
371 | n/a | #endif |
---|
372 | n/a | ret = PyBytes_FromFormatV(format, vargs); |
---|
373 | n/a | va_end(vargs); |
---|
374 | n/a | return ret; |
---|
375 | n/a | } |
---|
376 | n/a | |
---|
377 | n/a | /* Helpers for formatstring */ |
---|
378 | n/a | |
---|
379 | n/a | Py_LOCAL_INLINE(PyObject *) |
---|
380 | n/a | getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx) |
---|
381 | n/a | { |
---|
382 | n/a | Py_ssize_t argidx = *p_argidx; |
---|
383 | n/a | if (argidx < arglen) { |
---|
384 | n/a | (*p_argidx)++; |
---|
385 | n/a | if (arglen < 0) |
---|
386 | n/a | return args; |
---|
387 | n/a | else |
---|
388 | n/a | return PyTuple_GetItem(args, argidx); |
---|
389 | n/a | } |
---|
390 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
391 | n/a | "not enough arguments for format string"); |
---|
392 | n/a | return NULL; |
---|
393 | n/a | } |
---|
394 | n/a | |
---|
395 | n/a | /* Format codes |
---|
396 | n/a | * F_LJUST '-' |
---|
397 | n/a | * F_SIGN '+' |
---|
398 | n/a | * F_BLANK ' ' |
---|
399 | n/a | * F_ALT '#' |
---|
400 | n/a | * F_ZERO '0' |
---|
401 | n/a | */ |
---|
402 | n/a | #define F_LJUST (1<<0) |
---|
403 | n/a | #define F_SIGN (1<<1) |
---|
404 | n/a | #define F_BLANK (1<<2) |
---|
405 | n/a | #define F_ALT (1<<3) |
---|
406 | n/a | #define F_ZERO (1<<4) |
---|
407 | n/a | |
---|
408 | n/a | /* Returns a new reference to a PyBytes object, or NULL on failure. */ |
---|
409 | n/a | |
---|
410 | n/a | static char* |
---|
411 | n/a | formatfloat(PyObject *v, int flags, int prec, int type, |
---|
412 | n/a | PyObject **p_result, _PyBytesWriter *writer, char *str) |
---|
413 | n/a | { |
---|
414 | n/a | char *p; |
---|
415 | n/a | PyObject *result; |
---|
416 | n/a | double x; |
---|
417 | n/a | size_t len; |
---|
418 | n/a | |
---|
419 | n/a | x = PyFloat_AsDouble(v); |
---|
420 | n/a | if (x == -1.0 && PyErr_Occurred()) { |
---|
421 | n/a | PyErr_Format(PyExc_TypeError, "float argument required, " |
---|
422 | n/a | "not %.200s", Py_TYPE(v)->tp_name); |
---|
423 | n/a | return NULL; |
---|
424 | n/a | } |
---|
425 | n/a | |
---|
426 | n/a | if (prec < 0) |
---|
427 | n/a | prec = 6; |
---|
428 | n/a | |
---|
429 | n/a | p = PyOS_double_to_string(x, type, prec, |
---|
430 | n/a | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
---|
431 | n/a | |
---|
432 | n/a | if (p == NULL) |
---|
433 | n/a | return NULL; |
---|
434 | n/a | |
---|
435 | n/a | len = strlen(p); |
---|
436 | n/a | if (writer != NULL) { |
---|
437 | n/a | str = _PyBytesWriter_Prepare(writer, str, len); |
---|
438 | n/a | if (str == NULL) |
---|
439 | n/a | return NULL; |
---|
440 | n/a | memcpy(str, p, len); |
---|
441 | n/a | PyMem_Free(p); |
---|
442 | n/a | str += len; |
---|
443 | n/a | return str; |
---|
444 | n/a | } |
---|
445 | n/a | |
---|
446 | n/a | result = PyBytes_FromStringAndSize(p, len); |
---|
447 | n/a | PyMem_Free(p); |
---|
448 | n/a | *p_result = result; |
---|
449 | n/a | return str; |
---|
450 | n/a | } |
---|
451 | n/a | |
---|
452 | n/a | static PyObject * |
---|
453 | n/a | formatlong(PyObject *v, int flags, int prec, int type) |
---|
454 | n/a | { |
---|
455 | n/a | PyObject *result, *iobj; |
---|
456 | n/a | if (type == 'i') |
---|
457 | n/a | type = 'd'; |
---|
458 | n/a | if (PyLong_Check(v)) |
---|
459 | n/a | return _PyUnicode_FormatLong(v, flags & F_ALT, prec, type); |
---|
460 | n/a | if (PyNumber_Check(v)) { |
---|
461 | n/a | /* make sure number is a type of integer for o, x, and X */ |
---|
462 | n/a | if (type == 'o' || type == 'x' || type == 'X') |
---|
463 | n/a | iobj = PyNumber_Index(v); |
---|
464 | n/a | else |
---|
465 | n/a | iobj = PyNumber_Long(v); |
---|
466 | n/a | if (iobj == NULL) { |
---|
467 | n/a | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
---|
468 | n/a | return NULL; |
---|
469 | n/a | } |
---|
470 | n/a | else if (!PyLong_Check(iobj)) |
---|
471 | n/a | Py_CLEAR(iobj); |
---|
472 | n/a | if (iobj != NULL) { |
---|
473 | n/a | result = _PyUnicode_FormatLong(iobj, flags & F_ALT, prec, type); |
---|
474 | n/a | Py_DECREF(iobj); |
---|
475 | n/a | return result; |
---|
476 | n/a | } |
---|
477 | n/a | } |
---|
478 | n/a | PyErr_Format(PyExc_TypeError, |
---|
479 | n/a | "%%%c format: %s is required, not %.200s", type, |
---|
480 | n/a | (type == 'o' || type == 'x' || type == 'X') ? "an integer" |
---|
481 | n/a | : "a number", |
---|
482 | n/a | Py_TYPE(v)->tp_name); |
---|
483 | n/a | return NULL; |
---|
484 | n/a | } |
---|
485 | n/a | |
---|
486 | n/a | static int |
---|
487 | n/a | byte_converter(PyObject *arg, char *p) |
---|
488 | n/a | { |
---|
489 | n/a | if (PyBytes_Check(arg) && PyBytes_GET_SIZE(arg) == 1) { |
---|
490 | n/a | *p = PyBytes_AS_STRING(arg)[0]; |
---|
491 | n/a | return 1; |
---|
492 | n/a | } |
---|
493 | n/a | else if (PyByteArray_Check(arg) && PyByteArray_GET_SIZE(arg) == 1) { |
---|
494 | n/a | *p = PyByteArray_AS_STRING(arg)[0]; |
---|
495 | n/a | return 1; |
---|
496 | n/a | } |
---|
497 | n/a | else { |
---|
498 | n/a | PyObject *iobj; |
---|
499 | n/a | long ival; |
---|
500 | n/a | int overflow; |
---|
501 | n/a | /* make sure number is a type of integer */ |
---|
502 | n/a | if (PyLong_Check(arg)) { |
---|
503 | n/a | ival = PyLong_AsLongAndOverflow(arg, &overflow); |
---|
504 | n/a | } |
---|
505 | n/a | else { |
---|
506 | n/a | iobj = PyNumber_Index(arg); |
---|
507 | n/a | if (iobj == NULL) { |
---|
508 | n/a | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
---|
509 | n/a | return 0; |
---|
510 | n/a | goto onError; |
---|
511 | n/a | } |
---|
512 | n/a | ival = PyLong_AsLongAndOverflow(iobj, &overflow); |
---|
513 | n/a | Py_DECREF(iobj); |
---|
514 | n/a | } |
---|
515 | n/a | if (!overflow && ival == -1 && PyErr_Occurred()) |
---|
516 | n/a | goto onError; |
---|
517 | n/a | if (overflow || !(0 <= ival && ival <= 255)) { |
---|
518 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
519 | n/a | "%c arg not in range(256)"); |
---|
520 | n/a | return 0; |
---|
521 | n/a | } |
---|
522 | n/a | *p = (char)ival; |
---|
523 | n/a | return 1; |
---|
524 | n/a | } |
---|
525 | n/a | onError: |
---|
526 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
527 | n/a | "%c requires an integer in range(256) or a single byte"); |
---|
528 | n/a | return 0; |
---|
529 | n/a | } |
---|
530 | n/a | |
---|
531 | n/a | static PyObject * |
---|
532 | n/a | format_obj(PyObject *v, const char **pbuf, Py_ssize_t *plen) |
---|
533 | n/a | { |
---|
534 | n/a | PyObject *func, *result; |
---|
535 | n/a | _Py_IDENTIFIER(__bytes__); |
---|
536 | n/a | /* is it a bytes object? */ |
---|
537 | n/a | if (PyBytes_Check(v)) { |
---|
538 | n/a | *pbuf = PyBytes_AS_STRING(v); |
---|
539 | n/a | *plen = PyBytes_GET_SIZE(v); |
---|
540 | n/a | Py_INCREF(v); |
---|
541 | n/a | return v; |
---|
542 | n/a | } |
---|
543 | n/a | if (PyByteArray_Check(v)) { |
---|
544 | n/a | *pbuf = PyByteArray_AS_STRING(v); |
---|
545 | n/a | *plen = PyByteArray_GET_SIZE(v); |
---|
546 | n/a | Py_INCREF(v); |
---|
547 | n/a | return v; |
---|
548 | n/a | } |
---|
549 | n/a | /* does it support __bytes__? */ |
---|
550 | n/a | func = _PyObject_LookupSpecial(v, &PyId___bytes__); |
---|
551 | n/a | if (func != NULL) { |
---|
552 | n/a | result = _PyObject_CallNoArg(func); |
---|
553 | n/a | Py_DECREF(func); |
---|
554 | n/a | if (result == NULL) |
---|
555 | n/a | return NULL; |
---|
556 | n/a | if (!PyBytes_Check(result)) { |
---|
557 | n/a | PyErr_Format(PyExc_TypeError, |
---|
558 | n/a | "__bytes__ returned non-bytes (type %.200s)", |
---|
559 | n/a | Py_TYPE(result)->tp_name); |
---|
560 | n/a | Py_DECREF(result); |
---|
561 | n/a | return NULL; |
---|
562 | n/a | } |
---|
563 | n/a | *pbuf = PyBytes_AS_STRING(result); |
---|
564 | n/a | *plen = PyBytes_GET_SIZE(result); |
---|
565 | n/a | return result; |
---|
566 | n/a | } |
---|
567 | n/a | PyErr_Format(PyExc_TypeError, |
---|
568 | n/a | "%%b requires bytes, or an object that implements __bytes__, not '%.100s'", |
---|
569 | n/a | Py_TYPE(v)->tp_name); |
---|
570 | n/a | return NULL; |
---|
571 | n/a | } |
---|
572 | n/a | |
---|
573 | n/a | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) */ |
---|
574 | n/a | |
---|
575 | n/a | PyObject * |
---|
576 | n/a | _PyBytes_FormatEx(const char *format, Py_ssize_t format_len, |
---|
577 | n/a | PyObject *args, int use_bytearray) |
---|
578 | n/a | { |
---|
579 | n/a | const char *fmt; |
---|
580 | n/a | char *res; |
---|
581 | n/a | Py_ssize_t arglen, argidx; |
---|
582 | n/a | Py_ssize_t fmtcnt; |
---|
583 | n/a | int args_owned = 0; |
---|
584 | n/a | PyObject *dict = NULL; |
---|
585 | n/a | _PyBytesWriter writer; |
---|
586 | n/a | |
---|
587 | n/a | if (args == NULL) { |
---|
588 | n/a | PyErr_BadInternalCall(); |
---|
589 | n/a | return NULL; |
---|
590 | n/a | } |
---|
591 | n/a | fmt = format; |
---|
592 | n/a | fmtcnt = format_len; |
---|
593 | n/a | |
---|
594 | n/a | _PyBytesWriter_Init(&writer); |
---|
595 | n/a | writer.use_bytearray = use_bytearray; |
---|
596 | n/a | |
---|
597 | n/a | res = _PyBytesWriter_Alloc(&writer, fmtcnt); |
---|
598 | n/a | if (res == NULL) |
---|
599 | n/a | return NULL; |
---|
600 | n/a | if (!use_bytearray) |
---|
601 | n/a | writer.overallocate = 1; |
---|
602 | n/a | |
---|
603 | n/a | if (PyTuple_Check(args)) { |
---|
604 | n/a | arglen = PyTuple_GET_SIZE(args); |
---|
605 | n/a | argidx = 0; |
---|
606 | n/a | } |
---|
607 | n/a | else { |
---|
608 | n/a | arglen = -1; |
---|
609 | n/a | argidx = -2; |
---|
610 | n/a | } |
---|
611 | n/a | if (Py_TYPE(args)->tp_as_mapping && Py_TYPE(args)->tp_as_mapping->mp_subscript && |
---|
612 | n/a | !PyTuple_Check(args) && !PyBytes_Check(args) && !PyUnicode_Check(args) && |
---|
613 | n/a | !PyByteArray_Check(args)) { |
---|
614 | n/a | dict = args; |
---|
615 | n/a | } |
---|
616 | n/a | |
---|
617 | n/a | while (--fmtcnt >= 0) { |
---|
618 | n/a | if (*fmt != '%') { |
---|
619 | n/a | Py_ssize_t len; |
---|
620 | n/a | char *pos; |
---|
621 | n/a | |
---|
622 | n/a | pos = strchr(fmt + 1, '%'); |
---|
623 | n/a | if (pos != NULL) |
---|
624 | n/a | len = pos - fmt; |
---|
625 | n/a | else |
---|
626 | n/a | len = format_len - (fmt - format); |
---|
627 | n/a | assert(len != 0); |
---|
628 | n/a | |
---|
629 | n/a | memcpy(res, fmt, len); |
---|
630 | n/a | res += len; |
---|
631 | n/a | fmt += len; |
---|
632 | n/a | fmtcnt -= (len - 1); |
---|
633 | n/a | } |
---|
634 | n/a | else { |
---|
635 | n/a | /* Got a format specifier */ |
---|
636 | n/a | int flags = 0; |
---|
637 | n/a | Py_ssize_t width = -1; |
---|
638 | n/a | int prec = -1; |
---|
639 | n/a | int c = '\0'; |
---|
640 | n/a | int fill; |
---|
641 | n/a | PyObject *v = NULL; |
---|
642 | n/a | PyObject *temp = NULL; |
---|
643 | n/a | const char *pbuf = NULL; |
---|
644 | n/a | int sign; |
---|
645 | n/a | Py_ssize_t len = 0; |
---|
646 | n/a | char onechar; /* For byte_converter() */ |
---|
647 | n/a | Py_ssize_t alloc; |
---|
648 | n/a | #ifdef Py_DEBUG |
---|
649 | n/a | char *before; |
---|
650 | n/a | #endif |
---|
651 | n/a | |
---|
652 | n/a | fmt++; |
---|
653 | n/a | if (*fmt == '(') { |
---|
654 | n/a | const char *keystart; |
---|
655 | n/a | Py_ssize_t keylen; |
---|
656 | n/a | PyObject *key; |
---|
657 | n/a | int pcount = 1; |
---|
658 | n/a | |
---|
659 | n/a | if (dict == NULL) { |
---|
660 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
661 | n/a | "format requires a mapping"); |
---|
662 | n/a | goto error; |
---|
663 | n/a | } |
---|
664 | n/a | ++fmt; |
---|
665 | n/a | --fmtcnt; |
---|
666 | n/a | keystart = fmt; |
---|
667 | n/a | /* Skip over balanced parentheses */ |
---|
668 | n/a | while (pcount > 0 && --fmtcnt >= 0) { |
---|
669 | n/a | if (*fmt == ')') |
---|
670 | n/a | --pcount; |
---|
671 | n/a | else if (*fmt == '(') |
---|
672 | n/a | ++pcount; |
---|
673 | n/a | fmt++; |
---|
674 | n/a | } |
---|
675 | n/a | keylen = fmt - keystart - 1; |
---|
676 | n/a | if (fmtcnt < 0 || pcount > 0) { |
---|
677 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
678 | n/a | "incomplete format key"); |
---|
679 | n/a | goto error; |
---|
680 | n/a | } |
---|
681 | n/a | key = PyBytes_FromStringAndSize(keystart, |
---|
682 | n/a | keylen); |
---|
683 | n/a | if (key == NULL) |
---|
684 | n/a | goto error; |
---|
685 | n/a | if (args_owned) { |
---|
686 | n/a | Py_DECREF(args); |
---|
687 | n/a | args_owned = 0; |
---|
688 | n/a | } |
---|
689 | n/a | args = PyObject_GetItem(dict, key); |
---|
690 | n/a | Py_DECREF(key); |
---|
691 | n/a | if (args == NULL) { |
---|
692 | n/a | goto error; |
---|
693 | n/a | } |
---|
694 | n/a | args_owned = 1; |
---|
695 | n/a | arglen = -1; |
---|
696 | n/a | argidx = -2; |
---|
697 | n/a | } |
---|
698 | n/a | |
---|
699 | n/a | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
---|
700 | n/a | while (--fmtcnt >= 0) { |
---|
701 | n/a | switch (c = *fmt++) { |
---|
702 | n/a | case '-': flags |= F_LJUST; continue; |
---|
703 | n/a | case '+': flags |= F_SIGN; continue; |
---|
704 | n/a | case ' ': flags |= F_BLANK; continue; |
---|
705 | n/a | case '#': flags |= F_ALT; continue; |
---|
706 | n/a | case '0': flags |= F_ZERO; continue; |
---|
707 | n/a | } |
---|
708 | n/a | break; |
---|
709 | n/a | } |
---|
710 | n/a | |
---|
711 | n/a | /* Parse width. Example: "%10s" => width=10 */ |
---|
712 | n/a | if (c == '*') { |
---|
713 | n/a | v = getnextarg(args, arglen, &argidx); |
---|
714 | n/a | if (v == NULL) |
---|
715 | n/a | goto error; |
---|
716 | n/a | if (!PyLong_Check(v)) { |
---|
717 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
718 | n/a | "* wants int"); |
---|
719 | n/a | goto error; |
---|
720 | n/a | } |
---|
721 | n/a | width = PyLong_AsSsize_t(v); |
---|
722 | n/a | if (width == -1 && PyErr_Occurred()) |
---|
723 | n/a | goto error; |
---|
724 | n/a | if (width < 0) { |
---|
725 | n/a | flags |= F_LJUST; |
---|
726 | n/a | width = -width; |
---|
727 | n/a | } |
---|
728 | n/a | if (--fmtcnt >= 0) |
---|
729 | n/a | c = *fmt++; |
---|
730 | n/a | } |
---|
731 | n/a | else if (c >= 0 && isdigit(c)) { |
---|
732 | n/a | width = c - '0'; |
---|
733 | n/a | while (--fmtcnt >= 0) { |
---|
734 | n/a | c = Py_CHARMASK(*fmt++); |
---|
735 | n/a | if (!isdigit(c)) |
---|
736 | n/a | break; |
---|
737 | n/a | if (width > (PY_SSIZE_T_MAX - ((int)c - '0')) / 10) { |
---|
738 | n/a | PyErr_SetString( |
---|
739 | n/a | PyExc_ValueError, |
---|
740 | n/a | "width too big"); |
---|
741 | n/a | goto error; |
---|
742 | n/a | } |
---|
743 | n/a | width = width*10 + (c - '0'); |
---|
744 | n/a | } |
---|
745 | n/a | } |
---|
746 | n/a | |
---|
747 | n/a | /* Parse precision. Example: "%.3f" => prec=3 */ |
---|
748 | n/a | if (c == '.') { |
---|
749 | n/a | prec = 0; |
---|
750 | n/a | if (--fmtcnt >= 0) |
---|
751 | n/a | c = *fmt++; |
---|
752 | n/a | if (c == '*') { |
---|
753 | n/a | v = getnextarg(args, arglen, &argidx); |
---|
754 | n/a | if (v == NULL) |
---|
755 | n/a | goto error; |
---|
756 | n/a | if (!PyLong_Check(v)) { |
---|
757 | n/a | PyErr_SetString( |
---|
758 | n/a | PyExc_TypeError, |
---|
759 | n/a | "* wants int"); |
---|
760 | n/a | goto error; |
---|
761 | n/a | } |
---|
762 | n/a | prec = _PyLong_AsInt(v); |
---|
763 | n/a | if (prec == -1 && PyErr_Occurred()) |
---|
764 | n/a | goto error; |
---|
765 | n/a | if (prec < 0) |
---|
766 | n/a | prec = 0; |
---|
767 | n/a | if (--fmtcnt >= 0) |
---|
768 | n/a | c = *fmt++; |
---|
769 | n/a | } |
---|
770 | n/a | else if (c >= 0 && isdigit(c)) { |
---|
771 | n/a | prec = c - '0'; |
---|
772 | n/a | while (--fmtcnt >= 0) { |
---|
773 | n/a | c = Py_CHARMASK(*fmt++); |
---|
774 | n/a | if (!isdigit(c)) |
---|
775 | n/a | break; |
---|
776 | n/a | if (prec > (INT_MAX - ((int)c - '0')) / 10) { |
---|
777 | n/a | PyErr_SetString( |
---|
778 | n/a | PyExc_ValueError, |
---|
779 | n/a | "prec too big"); |
---|
780 | n/a | goto error; |
---|
781 | n/a | } |
---|
782 | n/a | prec = prec*10 + (c - '0'); |
---|
783 | n/a | } |
---|
784 | n/a | } |
---|
785 | n/a | } /* prec */ |
---|
786 | n/a | if (fmtcnt >= 0) { |
---|
787 | n/a | if (c == 'h' || c == 'l' || c == 'L') { |
---|
788 | n/a | if (--fmtcnt >= 0) |
---|
789 | n/a | c = *fmt++; |
---|
790 | n/a | } |
---|
791 | n/a | } |
---|
792 | n/a | if (fmtcnt < 0) { |
---|
793 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
794 | n/a | "incomplete format"); |
---|
795 | n/a | goto error; |
---|
796 | n/a | } |
---|
797 | n/a | if (c != '%') { |
---|
798 | n/a | v = getnextarg(args, arglen, &argidx); |
---|
799 | n/a | if (v == NULL) |
---|
800 | n/a | goto error; |
---|
801 | n/a | } |
---|
802 | n/a | |
---|
803 | n/a | if (fmtcnt < 0) { |
---|
804 | n/a | /* last writer: disable writer overallocation */ |
---|
805 | n/a | writer.overallocate = 0; |
---|
806 | n/a | } |
---|
807 | n/a | |
---|
808 | n/a | sign = 0; |
---|
809 | n/a | fill = ' '; |
---|
810 | n/a | switch (c) { |
---|
811 | n/a | case '%': |
---|
812 | n/a | *res++ = '%'; |
---|
813 | n/a | continue; |
---|
814 | n/a | |
---|
815 | n/a | case 'r': |
---|
816 | n/a | // %r is only for 2/3 code; 3 only code should use %a |
---|
817 | n/a | case 'a': |
---|
818 | n/a | temp = PyObject_ASCII(v); |
---|
819 | n/a | if (temp == NULL) |
---|
820 | n/a | goto error; |
---|
821 | n/a | assert(PyUnicode_IS_ASCII(temp)); |
---|
822 | n/a | pbuf = (const char *)PyUnicode_1BYTE_DATA(temp); |
---|
823 | n/a | len = PyUnicode_GET_LENGTH(temp); |
---|
824 | n/a | if (prec >= 0 && len > prec) |
---|
825 | n/a | len = prec; |
---|
826 | n/a | break; |
---|
827 | n/a | |
---|
828 | n/a | case 's': |
---|
829 | n/a | // %s is only for 2/3 code; 3 only code should use %b |
---|
830 | n/a | case 'b': |
---|
831 | n/a | temp = format_obj(v, &pbuf, &len); |
---|
832 | n/a | if (temp == NULL) |
---|
833 | n/a | goto error; |
---|
834 | n/a | if (prec >= 0 && len > prec) |
---|
835 | n/a | len = prec; |
---|
836 | n/a | break; |
---|
837 | n/a | |
---|
838 | n/a | case 'i': |
---|
839 | n/a | case 'd': |
---|
840 | n/a | case 'u': |
---|
841 | n/a | case 'o': |
---|
842 | n/a | case 'x': |
---|
843 | n/a | case 'X': |
---|
844 | n/a | if (PyLong_CheckExact(v) |
---|
845 | n/a | && width == -1 && prec == -1 |
---|
846 | n/a | && !(flags & (F_SIGN | F_BLANK)) |
---|
847 | n/a | && c != 'X') |
---|
848 | n/a | { |
---|
849 | n/a | /* Fast path */ |
---|
850 | n/a | int alternate = flags & F_ALT; |
---|
851 | n/a | int base; |
---|
852 | n/a | |
---|
853 | n/a | switch(c) |
---|
854 | n/a | { |
---|
855 | n/a | default: |
---|
856 | n/a | assert(0 && "'type' not in [diuoxX]"); |
---|
857 | n/a | case 'd': |
---|
858 | n/a | case 'i': |
---|
859 | n/a | case 'u': |
---|
860 | n/a | base = 10; |
---|
861 | n/a | break; |
---|
862 | n/a | case 'o': |
---|
863 | n/a | base = 8; |
---|
864 | n/a | break; |
---|
865 | n/a | case 'x': |
---|
866 | n/a | case 'X': |
---|
867 | n/a | base = 16; |
---|
868 | n/a | break; |
---|
869 | n/a | } |
---|
870 | n/a | |
---|
871 | n/a | /* Fast path */ |
---|
872 | n/a | writer.min_size -= 2; /* size preallocated for "%d" */ |
---|
873 | n/a | res = _PyLong_FormatBytesWriter(&writer, res, |
---|
874 | n/a | v, base, alternate); |
---|
875 | n/a | if (res == NULL) |
---|
876 | n/a | goto error; |
---|
877 | n/a | continue; |
---|
878 | n/a | } |
---|
879 | n/a | |
---|
880 | n/a | temp = formatlong(v, flags, prec, c); |
---|
881 | n/a | if (!temp) |
---|
882 | n/a | goto error; |
---|
883 | n/a | assert(PyUnicode_IS_ASCII(temp)); |
---|
884 | n/a | pbuf = (const char *)PyUnicode_1BYTE_DATA(temp); |
---|
885 | n/a | len = PyUnicode_GET_LENGTH(temp); |
---|
886 | n/a | sign = 1; |
---|
887 | n/a | if (flags & F_ZERO) |
---|
888 | n/a | fill = '0'; |
---|
889 | n/a | break; |
---|
890 | n/a | |
---|
891 | n/a | case 'e': |
---|
892 | n/a | case 'E': |
---|
893 | n/a | case 'f': |
---|
894 | n/a | case 'F': |
---|
895 | n/a | case 'g': |
---|
896 | n/a | case 'G': |
---|
897 | n/a | if (width == -1 && prec == -1 |
---|
898 | n/a | && !(flags & (F_SIGN | F_BLANK))) |
---|
899 | n/a | { |
---|
900 | n/a | /* Fast path */ |
---|
901 | n/a | writer.min_size -= 2; /* size preallocated for "%f" */ |
---|
902 | n/a | res = formatfloat(v, flags, prec, c, NULL, &writer, res); |
---|
903 | n/a | if (res == NULL) |
---|
904 | n/a | goto error; |
---|
905 | n/a | continue; |
---|
906 | n/a | } |
---|
907 | n/a | |
---|
908 | n/a | if (!formatfloat(v, flags, prec, c, &temp, NULL, res)) |
---|
909 | n/a | goto error; |
---|
910 | n/a | pbuf = PyBytes_AS_STRING(temp); |
---|
911 | n/a | len = PyBytes_GET_SIZE(temp); |
---|
912 | n/a | sign = 1; |
---|
913 | n/a | if (flags & F_ZERO) |
---|
914 | n/a | fill = '0'; |
---|
915 | n/a | break; |
---|
916 | n/a | |
---|
917 | n/a | case 'c': |
---|
918 | n/a | pbuf = &onechar; |
---|
919 | n/a | len = byte_converter(v, &onechar); |
---|
920 | n/a | if (!len) |
---|
921 | n/a | goto error; |
---|
922 | n/a | if (width == -1) { |
---|
923 | n/a | /* Fast path */ |
---|
924 | n/a | *res++ = onechar; |
---|
925 | n/a | continue; |
---|
926 | n/a | } |
---|
927 | n/a | break; |
---|
928 | n/a | |
---|
929 | n/a | default: |
---|
930 | n/a | PyErr_Format(PyExc_ValueError, |
---|
931 | n/a | "unsupported format character '%c' (0x%x) " |
---|
932 | n/a | "at index %zd", |
---|
933 | n/a | c, c, |
---|
934 | n/a | (Py_ssize_t)(fmt - 1 - format)); |
---|
935 | n/a | goto error; |
---|
936 | n/a | } |
---|
937 | n/a | |
---|
938 | n/a | if (sign) { |
---|
939 | n/a | if (*pbuf == '-' || *pbuf == '+') { |
---|
940 | n/a | sign = *pbuf++; |
---|
941 | n/a | len--; |
---|
942 | n/a | } |
---|
943 | n/a | else if (flags & F_SIGN) |
---|
944 | n/a | sign = '+'; |
---|
945 | n/a | else if (flags & F_BLANK) |
---|
946 | n/a | sign = ' '; |
---|
947 | n/a | else |
---|
948 | n/a | sign = 0; |
---|
949 | n/a | } |
---|
950 | n/a | if (width < len) |
---|
951 | n/a | width = len; |
---|
952 | n/a | |
---|
953 | n/a | alloc = width; |
---|
954 | n/a | if (sign != 0 && len == width) |
---|
955 | n/a | alloc++; |
---|
956 | n/a | /* 2: size preallocated for %s */ |
---|
957 | n/a | if (alloc > 2) { |
---|
958 | n/a | res = _PyBytesWriter_Prepare(&writer, res, alloc - 2); |
---|
959 | n/a | if (res == NULL) |
---|
960 | n/a | goto error; |
---|
961 | n/a | } |
---|
962 | n/a | #ifdef Py_DEBUG |
---|
963 | n/a | before = res; |
---|
964 | n/a | #endif |
---|
965 | n/a | |
---|
966 | n/a | /* Write the sign if needed */ |
---|
967 | n/a | if (sign) { |
---|
968 | n/a | if (fill != ' ') |
---|
969 | n/a | *res++ = sign; |
---|
970 | n/a | if (width > len) |
---|
971 | n/a | width--; |
---|
972 | n/a | } |
---|
973 | n/a | |
---|
974 | n/a | /* Write the numeric prefix for "x", "X" and "o" formats |
---|
975 | n/a | if the alternate form is used. |
---|
976 | n/a | For example, write "0x" for the "%#x" format. */ |
---|
977 | n/a | if ((flags & F_ALT) && (c == 'o' || c == 'x' || c == 'X')) { |
---|
978 | n/a | assert(pbuf[0] == '0'); |
---|
979 | n/a | assert(pbuf[1] == c); |
---|
980 | n/a | if (fill != ' ') { |
---|
981 | n/a | *res++ = *pbuf++; |
---|
982 | n/a | *res++ = *pbuf++; |
---|
983 | n/a | } |
---|
984 | n/a | width -= 2; |
---|
985 | n/a | if (width < 0) |
---|
986 | n/a | width = 0; |
---|
987 | n/a | len -= 2; |
---|
988 | n/a | } |
---|
989 | n/a | |
---|
990 | n/a | /* Pad left with the fill character if needed */ |
---|
991 | n/a | if (width > len && !(flags & F_LJUST)) { |
---|
992 | n/a | memset(res, fill, width - len); |
---|
993 | n/a | res += (width - len); |
---|
994 | n/a | width = len; |
---|
995 | n/a | } |
---|
996 | n/a | |
---|
997 | n/a | /* If padding with spaces: write sign if needed and/or numeric |
---|
998 | n/a | prefix if the alternate form is used */ |
---|
999 | n/a | if (fill == ' ') { |
---|
1000 | n/a | if (sign) |
---|
1001 | n/a | *res++ = sign; |
---|
1002 | n/a | if ((flags & F_ALT) && (c == 'o' || c == 'x' || c == 'X')) { |
---|
1003 | n/a | assert(pbuf[0] == '0'); |
---|
1004 | n/a | assert(pbuf[1] == c); |
---|
1005 | n/a | *res++ = *pbuf++; |
---|
1006 | n/a | *res++ = *pbuf++; |
---|
1007 | n/a | } |
---|
1008 | n/a | } |
---|
1009 | n/a | |
---|
1010 | n/a | /* Copy bytes */ |
---|
1011 | n/a | memcpy(res, pbuf, len); |
---|
1012 | n/a | res += len; |
---|
1013 | n/a | |
---|
1014 | n/a | /* Pad right with the fill character if needed */ |
---|
1015 | n/a | if (width > len) { |
---|
1016 | n/a | memset(res, ' ', width - len); |
---|
1017 | n/a | res += (width - len); |
---|
1018 | n/a | } |
---|
1019 | n/a | |
---|
1020 | n/a | if (dict && (argidx < arglen) && c != '%') { |
---|
1021 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1022 | n/a | "not all arguments converted during bytes formatting"); |
---|
1023 | n/a | Py_XDECREF(temp); |
---|
1024 | n/a | goto error; |
---|
1025 | n/a | } |
---|
1026 | n/a | Py_XDECREF(temp); |
---|
1027 | n/a | |
---|
1028 | n/a | #ifdef Py_DEBUG |
---|
1029 | n/a | /* check that we computed the exact size for this write */ |
---|
1030 | n/a | assert((res - before) == alloc); |
---|
1031 | n/a | #endif |
---|
1032 | n/a | } /* '%' */ |
---|
1033 | n/a | |
---|
1034 | n/a | /* If overallocation was disabled, ensure that it was the last |
---|
1035 | n/a | write. Otherwise, we missed an optimization */ |
---|
1036 | n/a | assert(writer.overallocate || fmtcnt < 0 || use_bytearray); |
---|
1037 | n/a | } /* until end */ |
---|
1038 | n/a | |
---|
1039 | n/a | if (argidx < arglen && !dict) { |
---|
1040 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1041 | n/a | "not all arguments converted during bytes formatting"); |
---|
1042 | n/a | goto error; |
---|
1043 | n/a | } |
---|
1044 | n/a | |
---|
1045 | n/a | if (args_owned) { |
---|
1046 | n/a | Py_DECREF(args); |
---|
1047 | n/a | } |
---|
1048 | n/a | return _PyBytesWriter_Finish(&writer, res); |
---|
1049 | n/a | |
---|
1050 | n/a | error: |
---|
1051 | n/a | _PyBytesWriter_Dealloc(&writer); |
---|
1052 | n/a | if (args_owned) { |
---|
1053 | n/a | Py_DECREF(args); |
---|
1054 | n/a | } |
---|
1055 | n/a | return NULL; |
---|
1056 | n/a | } |
---|
1057 | n/a | |
---|
1058 | n/a | /* =-= */ |
---|
1059 | n/a | |
---|
1060 | n/a | static void |
---|
1061 | n/a | bytes_dealloc(PyObject *op) |
---|
1062 | n/a | { |
---|
1063 | n/a | Py_TYPE(op)->tp_free(op); |
---|
1064 | n/a | } |
---|
1065 | n/a | |
---|
1066 | n/a | /* Unescape a backslash-escaped string. If unicode is non-zero, |
---|
1067 | n/a | the string is a u-literal. If recode_encoding is non-zero, |
---|
1068 | n/a | the string is UTF-8 encoded and should be re-encoded in the |
---|
1069 | n/a | specified encoding. */ |
---|
1070 | n/a | |
---|
1071 | n/a | static char * |
---|
1072 | n/a | _PyBytes_DecodeEscapeRecode(const char **s, const char *end, |
---|
1073 | n/a | const char *errors, const char *recode_encoding, |
---|
1074 | n/a | _PyBytesWriter *writer, char *p) |
---|
1075 | n/a | { |
---|
1076 | n/a | PyObject *u, *w; |
---|
1077 | n/a | const char* t; |
---|
1078 | n/a | |
---|
1079 | n/a | t = *s; |
---|
1080 | n/a | /* Decode non-ASCII bytes as UTF-8. */ |
---|
1081 | n/a | while (t < end && (*t & 0x80)) |
---|
1082 | n/a | t++; |
---|
1083 | n/a | u = PyUnicode_DecodeUTF8(*s, t - *s, errors); |
---|
1084 | n/a | if (u == NULL) |
---|
1085 | n/a | return NULL; |
---|
1086 | n/a | |
---|
1087 | n/a | /* Recode them in target encoding. */ |
---|
1088 | n/a | w = PyUnicode_AsEncodedString(u, recode_encoding, errors); |
---|
1089 | n/a | Py_DECREF(u); |
---|
1090 | n/a | if (w == NULL) |
---|
1091 | n/a | return NULL; |
---|
1092 | n/a | assert(PyBytes_Check(w)); |
---|
1093 | n/a | |
---|
1094 | n/a | /* Append bytes to output buffer. */ |
---|
1095 | n/a | writer->min_size--; /* subtract 1 preallocated byte */ |
---|
1096 | n/a | p = _PyBytesWriter_WriteBytes(writer, p, |
---|
1097 | n/a | PyBytes_AS_STRING(w), |
---|
1098 | n/a | PyBytes_GET_SIZE(w)); |
---|
1099 | n/a | Py_DECREF(w); |
---|
1100 | n/a | if (p == NULL) |
---|
1101 | n/a | return NULL; |
---|
1102 | n/a | |
---|
1103 | n/a | *s = t; |
---|
1104 | n/a | return p; |
---|
1105 | n/a | } |
---|
1106 | n/a | |
---|
1107 | n/a | PyObject *_PyBytes_DecodeEscape(const char *s, |
---|
1108 | n/a | Py_ssize_t len, |
---|
1109 | n/a | const char *errors, |
---|
1110 | n/a | Py_ssize_t unicode, |
---|
1111 | n/a | const char *recode_encoding, |
---|
1112 | n/a | const char **first_invalid_escape) |
---|
1113 | n/a | { |
---|
1114 | n/a | int c; |
---|
1115 | n/a | char *p; |
---|
1116 | n/a | const char *end; |
---|
1117 | n/a | _PyBytesWriter writer; |
---|
1118 | n/a | |
---|
1119 | n/a | _PyBytesWriter_Init(&writer); |
---|
1120 | n/a | |
---|
1121 | n/a | p = _PyBytesWriter_Alloc(&writer, len); |
---|
1122 | n/a | if (p == NULL) |
---|
1123 | n/a | return NULL; |
---|
1124 | n/a | writer.overallocate = 1; |
---|
1125 | n/a | |
---|
1126 | n/a | *first_invalid_escape = NULL; |
---|
1127 | n/a | |
---|
1128 | n/a | end = s + len; |
---|
1129 | n/a | while (s < end) { |
---|
1130 | n/a | if (*s != '\\') { |
---|
1131 | n/a | non_esc: |
---|
1132 | n/a | if (!(recode_encoding && (*s & 0x80))) { |
---|
1133 | n/a | *p++ = *s++; |
---|
1134 | n/a | } |
---|
1135 | n/a | else { |
---|
1136 | n/a | /* non-ASCII character and need to recode */ |
---|
1137 | n/a | p = _PyBytes_DecodeEscapeRecode(&s, end, |
---|
1138 | n/a | errors, recode_encoding, |
---|
1139 | n/a | &writer, p); |
---|
1140 | n/a | if (p == NULL) |
---|
1141 | n/a | goto failed; |
---|
1142 | n/a | } |
---|
1143 | n/a | continue; |
---|
1144 | n/a | } |
---|
1145 | n/a | |
---|
1146 | n/a | s++; |
---|
1147 | n/a | if (s == end) { |
---|
1148 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
1149 | n/a | "Trailing \\ in string"); |
---|
1150 | n/a | goto failed; |
---|
1151 | n/a | } |
---|
1152 | n/a | |
---|
1153 | n/a | switch (*s++) { |
---|
1154 | n/a | /* XXX This assumes ASCII! */ |
---|
1155 | n/a | case '\n': break; |
---|
1156 | n/a | case '\\': *p++ = '\\'; break; |
---|
1157 | n/a | case '\'': *p++ = '\''; break; |
---|
1158 | n/a | case '\"': *p++ = '\"'; break; |
---|
1159 | n/a | case 'b': *p++ = '\b'; break; |
---|
1160 | n/a | case 'f': *p++ = '\014'; break; /* FF */ |
---|
1161 | n/a | case 't': *p++ = '\t'; break; |
---|
1162 | n/a | case 'n': *p++ = '\n'; break; |
---|
1163 | n/a | case 'r': *p++ = '\r'; break; |
---|
1164 | n/a | case 'v': *p++ = '\013'; break; /* VT */ |
---|
1165 | n/a | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
---|
1166 | n/a | case '0': case '1': case '2': case '3': |
---|
1167 | n/a | case '4': case '5': case '6': case '7': |
---|
1168 | n/a | c = s[-1] - '0'; |
---|
1169 | n/a | if (s < end && '0' <= *s && *s <= '7') { |
---|
1170 | n/a | c = (c<<3) + *s++ - '0'; |
---|
1171 | n/a | if (s < end && '0' <= *s && *s <= '7') |
---|
1172 | n/a | c = (c<<3) + *s++ - '0'; |
---|
1173 | n/a | } |
---|
1174 | n/a | *p++ = c; |
---|
1175 | n/a | break; |
---|
1176 | n/a | case 'x': |
---|
1177 | n/a | if (s+1 < end) { |
---|
1178 | n/a | int digit1, digit2; |
---|
1179 | n/a | digit1 = _PyLong_DigitValue[Py_CHARMASK(s[0])]; |
---|
1180 | n/a | digit2 = _PyLong_DigitValue[Py_CHARMASK(s[1])]; |
---|
1181 | n/a | if (digit1 < 16 && digit2 < 16) { |
---|
1182 | n/a | *p++ = (unsigned char)((digit1 << 4) + digit2); |
---|
1183 | n/a | s += 2; |
---|
1184 | n/a | break; |
---|
1185 | n/a | } |
---|
1186 | n/a | } |
---|
1187 | n/a | /* invalid hexadecimal digits */ |
---|
1188 | n/a | |
---|
1189 | n/a | if (!errors || strcmp(errors, "strict") == 0) { |
---|
1190 | n/a | PyErr_Format(PyExc_ValueError, |
---|
1191 | n/a | "invalid \\x escape at position %d", |
---|
1192 | n/a | s - 2 - (end - len)); |
---|
1193 | n/a | goto failed; |
---|
1194 | n/a | } |
---|
1195 | n/a | if (strcmp(errors, "replace") == 0) { |
---|
1196 | n/a | *p++ = '?'; |
---|
1197 | n/a | } else if (strcmp(errors, "ignore") == 0) |
---|
1198 | n/a | /* do nothing */; |
---|
1199 | n/a | else { |
---|
1200 | n/a | PyErr_Format(PyExc_ValueError, |
---|
1201 | n/a | "decoding error; unknown " |
---|
1202 | n/a | "error handling code: %.400s", |
---|
1203 | n/a | errors); |
---|
1204 | n/a | goto failed; |
---|
1205 | n/a | } |
---|
1206 | n/a | /* skip \x */ |
---|
1207 | n/a | if (s < end && Py_ISXDIGIT(s[0])) |
---|
1208 | n/a | s++; /* and a hexdigit */ |
---|
1209 | n/a | break; |
---|
1210 | n/a | |
---|
1211 | n/a | default: |
---|
1212 | n/a | if (*first_invalid_escape == NULL) { |
---|
1213 | n/a | *first_invalid_escape = s-1; /* Back up one char, since we've |
---|
1214 | n/a | already incremented s. */ |
---|
1215 | n/a | } |
---|
1216 | n/a | *p++ = '\\'; |
---|
1217 | n/a | s--; |
---|
1218 | n/a | goto non_esc; /* an arbitrary number of unescaped |
---|
1219 | n/a | UTF-8 bytes may follow. */ |
---|
1220 | n/a | } |
---|
1221 | n/a | } |
---|
1222 | n/a | |
---|
1223 | n/a | return _PyBytesWriter_Finish(&writer, p); |
---|
1224 | n/a | |
---|
1225 | n/a | failed: |
---|
1226 | n/a | _PyBytesWriter_Dealloc(&writer); |
---|
1227 | n/a | return NULL; |
---|
1228 | n/a | } |
---|
1229 | n/a | |
---|
1230 | n/a | PyObject *PyBytes_DecodeEscape(const char *s, |
---|
1231 | n/a | Py_ssize_t len, |
---|
1232 | n/a | const char *errors, |
---|
1233 | n/a | Py_ssize_t unicode, |
---|
1234 | n/a | const char *recode_encoding) |
---|
1235 | n/a | { |
---|
1236 | n/a | const char* first_invalid_escape; |
---|
1237 | n/a | PyObject *result = _PyBytes_DecodeEscape(s, len, errors, unicode, |
---|
1238 | n/a | recode_encoding, |
---|
1239 | n/a | &first_invalid_escape); |
---|
1240 | n/a | if (result == NULL) |
---|
1241 | n/a | return NULL; |
---|
1242 | n/a | if (first_invalid_escape != NULL) { |
---|
1243 | n/a | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
---|
1244 | n/a | "invalid escape sequence '\\%c'", |
---|
1245 | n/a | *first_invalid_escape) < 0) { |
---|
1246 | n/a | Py_DECREF(result); |
---|
1247 | n/a | return NULL; |
---|
1248 | n/a | } |
---|
1249 | n/a | } |
---|
1250 | n/a | return result; |
---|
1251 | n/a | |
---|
1252 | n/a | } |
---|
1253 | n/a | /* -------------------------------------------------------------------- */ |
---|
1254 | n/a | /* object api */ |
---|
1255 | n/a | |
---|
1256 | n/a | Py_ssize_t |
---|
1257 | n/a | PyBytes_Size(PyObject *op) |
---|
1258 | n/a | { |
---|
1259 | n/a | if (!PyBytes_Check(op)) { |
---|
1260 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1261 | n/a | "expected bytes, %.200s found", Py_TYPE(op)->tp_name); |
---|
1262 | n/a | return -1; |
---|
1263 | n/a | } |
---|
1264 | n/a | return Py_SIZE(op); |
---|
1265 | n/a | } |
---|
1266 | n/a | |
---|
1267 | n/a | char * |
---|
1268 | n/a | PyBytes_AsString(PyObject *op) |
---|
1269 | n/a | { |
---|
1270 | n/a | if (!PyBytes_Check(op)) { |
---|
1271 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1272 | n/a | "expected bytes, %.200s found", Py_TYPE(op)->tp_name); |
---|
1273 | n/a | return NULL; |
---|
1274 | n/a | } |
---|
1275 | n/a | return ((PyBytesObject *)op)->ob_sval; |
---|
1276 | n/a | } |
---|
1277 | n/a | |
---|
1278 | n/a | int |
---|
1279 | n/a | PyBytes_AsStringAndSize(PyObject *obj, |
---|
1280 | n/a | char **s, |
---|
1281 | n/a | Py_ssize_t *len) |
---|
1282 | n/a | { |
---|
1283 | n/a | if (s == NULL) { |
---|
1284 | n/a | PyErr_BadInternalCall(); |
---|
1285 | n/a | return -1; |
---|
1286 | n/a | } |
---|
1287 | n/a | |
---|
1288 | n/a | if (!PyBytes_Check(obj)) { |
---|
1289 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1290 | n/a | "expected bytes, %.200s found", Py_TYPE(obj)->tp_name); |
---|
1291 | n/a | return -1; |
---|
1292 | n/a | } |
---|
1293 | n/a | |
---|
1294 | n/a | *s = PyBytes_AS_STRING(obj); |
---|
1295 | n/a | if (len != NULL) |
---|
1296 | n/a | *len = PyBytes_GET_SIZE(obj); |
---|
1297 | n/a | else if (strlen(*s) != (size_t)PyBytes_GET_SIZE(obj)) { |
---|
1298 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
1299 | n/a | "embedded null byte"); |
---|
1300 | n/a | return -1; |
---|
1301 | n/a | } |
---|
1302 | n/a | return 0; |
---|
1303 | n/a | } |
---|
1304 | n/a | |
---|
1305 | n/a | /* -------------------------------------------------------------------- */ |
---|
1306 | n/a | /* Methods */ |
---|
1307 | n/a | |
---|
1308 | n/a | #include "stringlib/stringdefs.h" |
---|
1309 | n/a | |
---|
1310 | n/a | #include "stringlib/fastsearch.h" |
---|
1311 | n/a | #include "stringlib/count.h" |
---|
1312 | n/a | #include "stringlib/find.h" |
---|
1313 | n/a | #include "stringlib/join.h" |
---|
1314 | n/a | #include "stringlib/partition.h" |
---|
1315 | n/a | #include "stringlib/split.h" |
---|
1316 | n/a | #include "stringlib/ctype.h" |
---|
1317 | n/a | |
---|
1318 | n/a | #include "stringlib/transmogrify.h" |
---|
1319 | n/a | |
---|
1320 | n/a | PyObject * |
---|
1321 | n/a | PyBytes_Repr(PyObject *obj, int smartquotes) |
---|
1322 | n/a | { |
---|
1323 | n/a | PyBytesObject* op = (PyBytesObject*) obj; |
---|
1324 | n/a | Py_ssize_t i, length = Py_SIZE(op); |
---|
1325 | n/a | Py_ssize_t newsize, squotes, dquotes; |
---|
1326 | n/a | PyObject *v; |
---|
1327 | n/a | unsigned char quote, *s, *p; |
---|
1328 | n/a | |
---|
1329 | n/a | /* Compute size of output string */ |
---|
1330 | n/a | squotes = dquotes = 0; |
---|
1331 | n/a | newsize = 3; /* b'' */ |
---|
1332 | n/a | s = (unsigned char*)op->ob_sval; |
---|
1333 | n/a | for (i = 0; i < length; i++) { |
---|
1334 | n/a | Py_ssize_t incr = 1; |
---|
1335 | n/a | switch(s[i]) { |
---|
1336 | n/a | case '\'': squotes++; break; |
---|
1337 | n/a | case '"': dquotes++; break; |
---|
1338 | n/a | case '\\': case '\t': case '\n': case '\r': |
---|
1339 | n/a | incr = 2; break; /* \C */ |
---|
1340 | n/a | default: |
---|
1341 | n/a | if (s[i] < ' ' || s[i] >= 0x7f) |
---|
1342 | n/a | incr = 4; /* \xHH */ |
---|
1343 | n/a | } |
---|
1344 | n/a | if (newsize > PY_SSIZE_T_MAX - incr) |
---|
1345 | n/a | goto overflow; |
---|
1346 | n/a | newsize += incr; |
---|
1347 | n/a | } |
---|
1348 | n/a | quote = '\''; |
---|
1349 | n/a | if (smartquotes && squotes && !dquotes) |
---|
1350 | n/a | quote = '"'; |
---|
1351 | n/a | if (squotes && quote == '\'') { |
---|
1352 | n/a | if (newsize > PY_SSIZE_T_MAX - squotes) |
---|
1353 | n/a | goto overflow; |
---|
1354 | n/a | newsize += squotes; |
---|
1355 | n/a | } |
---|
1356 | n/a | |
---|
1357 | n/a | v = PyUnicode_New(newsize, 127); |
---|
1358 | n/a | if (v == NULL) { |
---|
1359 | n/a | return NULL; |
---|
1360 | n/a | } |
---|
1361 | n/a | p = PyUnicode_1BYTE_DATA(v); |
---|
1362 | n/a | |
---|
1363 | n/a | *p++ = 'b', *p++ = quote; |
---|
1364 | n/a | for (i = 0; i < length; i++) { |
---|
1365 | n/a | unsigned char c = op->ob_sval[i]; |
---|
1366 | n/a | if (c == quote || c == '\\') |
---|
1367 | n/a | *p++ = '\\', *p++ = c; |
---|
1368 | n/a | else if (c == '\t') |
---|
1369 | n/a | *p++ = '\\', *p++ = 't'; |
---|
1370 | n/a | else if (c == '\n') |
---|
1371 | n/a | *p++ = '\\', *p++ = 'n'; |
---|
1372 | n/a | else if (c == '\r') |
---|
1373 | n/a | *p++ = '\\', *p++ = 'r'; |
---|
1374 | n/a | else if (c < ' ' || c >= 0x7f) { |
---|
1375 | n/a | *p++ = '\\'; |
---|
1376 | n/a | *p++ = 'x'; |
---|
1377 | n/a | *p++ = Py_hexdigits[(c & 0xf0) >> 4]; |
---|
1378 | n/a | *p++ = Py_hexdigits[c & 0xf]; |
---|
1379 | n/a | } |
---|
1380 | n/a | else |
---|
1381 | n/a | *p++ = c; |
---|
1382 | n/a | } |
---|
1383 | n/a | *p++ = quote; |
---|
1384 | n/a | assert(_PyUnicode_CheckConsistency(v, 1)); |
---|
1385 | n/a | return v; |
---|
1386 | n/a | |
---|
1387 | n/a | overflow: |
---|
1388 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
1389 | n/a | "bytes object is too large to make repr"); |
---|
1390 | n/a | return NULL; |
---|
1391 | n/a | } |
---|
1392 | n/a | |
---|
1393 | n/a | static PyObject * |
---|
1394 | n/a | bytes_repr(PyObject *op) |
---|
1395 | n/a | { |
---|
1396 | n/a | return PyBytes_Repr(op, 1); |
---|
1397 | n/a | } |
---|
1398 | n/a | |
---|
1399 | n/a | static PyObject * |
---|
1400 | n/a | bytes_str(PyObject *op) |
---|
1401 | n/a | { |
---|
1402 | n/a | if (Py_BytesWarningFlag) { |
---|
1403 | n/a | if (PyErr_WarnEx(PyExc_BytesWarning, |
---|
1404 | n/a | "str() on a bytes instance", 1)) |
---|
1405 | n/a | return NULL; |
---|
1406 | n/a | } |
---|
1407 | n/a | return bytes_repr(op); |
---|
1408 | n/a | } |
---|
1409 | n/a | |
---|
1410 | n/a | static Py_ssize_t |
---|
1411 | n/a | bytes_length(PyBytesObject *a) |
---|
1412 | n/a | { |
---|
1413 | n/a | return Py_SIZE(a); |
---|
1414 | n/a | } |
---|
1415 | n/a | |
---|
1416 | n/a | /* This is also used by PyBytes_Concat() */ |
---|
1417 | n/a | static PyObject * |
---|
1418 | n/a | bytes_concat(PyObject *a, PyObject *b) |
---|
1419 | n/a | { |
---|
1420 | n/a | Py_buffer va, vb; |
---|
1421 | n/a | PyObject *result = NULL; |
---|
1422 | n/a | |
---|
1423 | n/a | va.len = -1; |
---|
1424 | n/a | vb.len = -1; |
---|
1425 | n/a | if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 || |
---|
1426 | n/a | PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) { |
---|
1427 | n/a | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
---|
1428 | n/a | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
---|
1429 | n/a | goto done; |
---|
1430 | n/a | } |
---|
1431 | n/a | |
---|
1432 | n/a | /* Optimize end cases */ |
---|
1433 | n/a | if (va.len == 0 && PyBytes_CheckExact(b)) { |
---|
1434 | n/a | result = b; |
---|
1435 | n/a | Py_INCREF(result); |
---|
1436 | n/a | goto done; |
---|
1437 | n/a | } |
---|
1438 | n/a | if (vb.len == 0 && PyBytes_CheckExact(a)) { |
---|
1439 | n/a | result = a; |
---|
1440 | n/a | Py_INCREF(result); |
---|
1441 | n/a | goto done; |
---|
1442 | n/a | } |
---|
1443 | n/a | |
---|
1444 | n/a | if (va.len > PY_SSIZE_T_MAX - vb.len) { |
---|
1445 | n/a | PyErr_NoMemory(); |
---|
1446 | n/a | goto done; |
---|
1447 | n/a | } |
---|
1448 | n/a | |
---|
1449 | n/a | result = PyBytes_FromStringAndSize(NULL, va.len + vb.len); |
---|
1450 | n/a | if (result != NULL) { |
---|
1451 | n/a | memcpy(PyBytes_AS_STRING(result), va.buf, va.len); |
---|
1452 | n/a | memcpy(PyBytes_AS_STRING(result) + va.len, vb.buf, vb.len); |
---|
1453 | n/a | } |
---|
1454 | n/a | |
---|
1455 | n/a | done: |
---|
1456 | n/a | if (va.len != -1) |
---|
1457 | n/a | PyBuffer_Release(&va); |
---|
1458 | n/a | if (vb.len != -1) |
---|
1459 | n/a | PyBuffer_Release(&vb); |
---|
1460 | n/a | return result; |
---|
1461 | n/a | } |
---|
1462 | n/a | |
---|
1463 | n/a | static PyObject * |
---|
1464 | n/a | bytes_repeat(PyBytesObject *a, Py_ssize_t n) |
---|
1465 | n/a | { |
---|
1466 | n/a | Py_ssize_t i; |
---|
1467 | n/a | Py_ssize_t j; |
---|
1468 | n/a | Py_ssize_t size; |
---|
1469 | n/a | PyBytesObject *op; |
---|
1470 | n/a | size_t nbytes; |
---|
1471 | n/a | if (n < 0) |
---|
1472 | n/a | n = 0; |
---|
1473 | n/a | /* watch out for overflows: the size can overflow int, |
---|
1474 | n/a | * and the # of bytes needed can overflow size_t |
---|
1475 | n/a | */ |
---|
1476 | n/a | if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n) { |
---|
1477 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
1478 | n/a | "repeated bytes are too long"); |
---|
1479 | n/a | return NULL; |
---|
1480 | n/a | } |
---|
1481 | n/a | size = Py_SIZE(a) * n; |
---|
1482 | n/a | if (size == Py_SIZE(a) && PyBytes_CheckExact(a)) { |
---|
1483 | n/a | Py_INCREF(a); |
---|
1484 | n/a | return (PyObject *)a; |
---|
1485 | n/a | } |
---|
1486 | n/a | nbytes = (size_t)size; |
---|
1487 | n/a | if (nbytes + PyBytesObject_SIZE <= nbytes) { |
---|
1488 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
1489 | n/a | "repeated bytes are too long"); |
---|
1490 | n/a | return NULL; |
---|
1491 | n/a | } |
---|
1492 | n/a | op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + nbytes); |
---|
1493 | n/a | if (op == NULL) |
---|
1494 | n/a | return PyErr_NoMemory(); |
---|
1495 | n/a | (void)PyObject_INIT_VAR(op, &PyBytes_Type, size); |
---|
1496 | n/a | op->ob_shash = -1; |
---|
1497 | n/a | op->ob_sval[size] = '\0'; |
---|
1498 | n/a | if (Py_SIZE(a) == 1 && n > 0) { |
---|
1499 | n/a | memset(op->ob_sval, a->ob_sval[0] , n); |
---|
1500 | n/a | return (PyObject *) op; |
---|
1501 | n/a | } |
---|
1502 | n/a | i = 0; |
---|
1503 | n/a | if (i < size) { |
---|
1504 | n/a | memcpy(op->ob_sval, a->ob_sval, Py_SIZE(a)); |
---|
1505 | n/a | i = Py_SIZE(a); |
---|
1506 | n/a | } |
---|
1507 | n/a | while (i < size) { |
---|
1508 | n/a | j = (i <= size-i) ? i : size-i; |
---|
1509 | n/a | memcpy(op->ob_sval+i, op->ob_sval, j); |
---|
1510 | n/a | i += j; |
---|
1511 | n/a | } |
---|
1512 | n/a | return (PyObject *) op; |
---|
1513 | n/a | } |
---|
1514 | n/a | |
---|
1515 | n/a | static int |
---|
1516 | n/a | bytes_contains(PyObject *self, PyObject *arg) |
---|
1517 | n/a | { |
---|
1518 | n/a | return _Py_bytes_contains(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), arg); |
---|
1519 | n/a | } |
---|
1520 | n/a | |
---|
1521 | n/a | static PyObject * |
---|
1522 | n/a | bytes_item(PyBytesObject *a, Py_ssize_t i) |
---|
1523 | n/a | { |
---|
1524 | n/a | if (i < 0 || i >= Py_SIZE(a)) { |
---|
1525 | n/a | PyErr_SetString(PyExc_IndexError, "index out of range"); |
---|
1526 | n/a | return NULL; |
---|
1527 | n/a | } |
---|
1528 | n/a | return PyLong_FromLong((unsigned char)a->ob_sval[i]); |
---|
1529 | n/a | } |
---|
1530 | n/a | |
---|
1531 | n/a | static int |
---|
1532 | n/a | bytes_compare_eq(PyBytesObject *a, PyBytesObject *b) |
---|
1533 | n/a | { |
---|
1534 | n/a | int cmp; |
---|
1535 | n/a | Py_ssize_t len; |
---|
1536 | n/a | |
---|
1537 | n/a | len = Py_SIZE(a); |
---|
1538 | n/a | if (Py_SIZE(b) != len) |
---|
1539 | n/a | return 0; |
---|
1540 | n/a | |
---|
1541 | n/a | if (a->ob_sval[0] != b->ob_sval[0]) |
---|
1542 | n/a | return 0; |
---|
1543 | n/a | |
---|
1544 | n/a | cmp = memcmp(a->ob_sval, b->ob_sval, len); |
---|
1545 | n/a | return (cmp == 0); |
---|
1546 | n/a | } |
---|
1547 | n/a | |
---|
1548 | n/a | static PyObject* |
---|
1549 | n/a | bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op) |
---|
1550 | n/a | { |
---|
1551 | n/a | int c; |
---|
1552 | n/a | Py_ssize_t len_a, len_b; |
---|
1553 | n/a | Py_ssize_t min_len; |
---|
1554 | n/a | PyObject *result; |
---|
1555 | n/a | int rc; |
---|
1556 | n/a | |
---|
1557 | n/a | /* Make sure both arguments are strings. */ |
---|
1558 | n/a | if (!(PyBytes_Check(a) && PyBytes_Check(b))) { |
---|
1559 | n/a | if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) { |
---|
1560 | n/a | rc = PyObject_IsInstance((PyObject*)a, |
---|
1561 | n/a | (PyObject*)&PyUnicode_Type); |
---|
1562 | n/a | if (!rc) |
---|
1563 | n/a | rc = PyObject_IsInstance((PyObject*)b, |
---|
1564 | n/a | (PyObject*)&PyUnicode_Type); |
---|
1565 | n/a | if (rc < 0) |
---|
1566 | n/a | return NULL; |
---|
1567 | n/a | if (rc) { |
---|
1568 | n/a | if (PyErr_WarnEx(PyExc_BytesWarning, |
---|
1569 | n/a | "Comparison between bytes and string", 1)) |
---|
1570 | n/a | return NULL; |
---|
1571 | n/a | } |
---|
1572 | n/a | else { |
---|
1573 | n/a | rc = PyObject_IsInstance((PyObject*)a, |
---|
1574 | n/a | (PyObject*)&PyLong_Type); |
---|
1575 | n/a | if (!rc) |
---|
1576 | n/a | rc = PyObject_IsInstance((PyObject*)b, |
---|
1577 | n/a | (PyObject*)&PyLong_Type); |
---|
1578 | n/a | if (rc < 0) |
---|
1579 | n/a | return NULL; |
---|
1580 | n/a | if (rc) { |
---|
1581 | n/a | if (PyErr_WarnEx(PyExc_BytesWarning, |
---|
1582 | n/a | "Comparison between bytes and int", 1)) |
---|
1583 | n/a | return NULL; |
---|
1584 | n/a | } |
---|
1585 | n/a | } |
---|
1586 | n/a | } |
---|
1587 | n/a | result = Py_NotImplemented; |
---|
1588 | n/a | } |
---|
1589 | n/a | else if (a == b) { |
---|
1590 | n/a | switch (op) { |
---|
1591 | n/a | case Py_EQ: |
---|
1592 | n/a | case Py_LE: |
---|
1593 | n/a | case Py_GE: |
---|
1594 | n/a | /* a string is equal to itself */ |
---|
1595 | n/a | result = Py_True; |
---|
1596 | n/a | break; |
---|
1597 | n/a | case Py_NE: |
---|
1598 | n/a | case Py_LT: |
---|
1599 | n/a | case Py_GT: |
---|
1600 | n/a | result = Py_False; |
---|
1601 | n/a | break; |
---|
1602 | n/a | default: |
---|
1603 | n/a | PyErr_BadArgument(); |
---|
1604 | n/a | return NULL; |
---|
1605 | n/a | } |
---|
1606 | n/a | } |
---|
1607 | n/a | else if (op == Py_EQ || op == Py_NE) { |
---|
1608 | n/a | int eq = bytes_compare_eq(a, b); |
---|
1609 | n/a | eq ^= (op == Py_NE); |
---|
1610 | n/a | result = eq ? Py_True : Py_False; |
---|
1611 | n/a | } |
---|
1612 | n/a | else { |
---|
1613 | n/a | len_a = Py_SIZE(a); |
---|
1614 | n/a | len_b = Py_SIZE(b); |
---|
1615 | n/a | min_len = Py_MIN(len_a, len_b); |
---|
1616 | n/a | if (min_len > 0) { |
---|
1617 | n/a | c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval); |
---|
1618 | n/a | if (c == 0) |
---|
1619 | n/a | c = memcmp(a->ob_sval, b->ob_sval, min_len); |
---|
1620 | n/a | } |
---|
1621 | n/a | else |
---|
1622 | n/a | c = 0; |
---|
1623 | n/a | if (c == 0) |
---|
1624 | n/a | c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0; |
---|
1625 | n/a | switch (op) { |
---|
1626 | n/a | case Py_LT: c = c < 0; break; |
---|
1627 | n/a | case Py_LE: c = c <= 0; break; |
---|
1628 | n/a | case Py_GT: c = c > 0; break; |
---|
1629 | n/a | case Py_GE: c = c >= 0; break; |
---|
1630 | n/a | default: |
---|
1631 | n/a | PyErr_BadArgument(); |
---|
1632 | n/a | return NULL; |
---|
1633 | n/a | } |
---|
1634 | n/a | result = c ? Py_True : Py_False; |
---|
1635 | n/a | } |
---|
1636 | n/a | |
---|
1637 | n/a | Py_INCREF(result); |
---|
1638 | n/a | return result; |
---|
1639 | n/a | } |
---|
1640 | n/a | |
---|
1641 | n/a | static Py_hash_t |
---|
1642 | n/a | bytes_hash(PyBytesObject *a) |
---|
1643 | n/a | { |
---|
1644 | n/a | if (a->ob_shash == -1) { |
---|
1645 | n/a | /* Can't fail */ |
---|
1646 | n/a | a->ob_shash = _Py_HashBytes(a->ob_sval, Py_SIZE(a)); |
---|
1647 | n/a | } |
---|
1648 | n/a | return a->ob_shash; |
---|
1649 | n/a | } |
---|
1650 | n/a | |
---|
1651 | n/a | static PyObject* |
---|
1652 | n/a | bytes_subscript(PyBytesObject* self, PyObject* item) |
---|
1653 | n/a | { |
---|
1654 | n/a | if (PyIndex_Check(item)) { |
---|
1655 | n/a | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
---|
1656 | n/a | if (i == -1 && PyErr_Occurred()) |
---|
1657 | n/a | return NULL; |
---|
1658 | n/a | if (i < 0) |
---|
1659 | n/a | i += PyBytes_GET_SIZE(self); |
---|
1660 | n/a | if (i < 0 || i >= PyBytes_GET_SIZE(self)) { |
---|
1661 | n/a | PyErr_SetString(PyExc_IndexError, |
---|
1662 | n/a | "index out of range"); |
---|
1663 | n/a | return NULL; |
---|
1664 | n/a | } |
---|
1665 | n/a | return PyLong_FromLong((unsigned char)self->ob_sval[i]); |
---|
1666 | n/a | } |
---|
1667 | n/a | else if (PySlice_Check(item)) { |
---|
1668 | n/a | Py_ssize_t start, stop, step, slicelength, cur, i; |
---|
1669 | n/a | char* source_buf; |
---|
1670 | n/a | char* result_buf; |
---|
1671 | n/a | PyObject* result; |
---|
1672 | n/a | |
---|
1673 | n/a | if (PySlice_GetIndicesEx(item, |
---|
1674 | n/a | PyBytes_GET_SIZE(self), |
---|
1675 | n/a | &start, &stop, &step, &slicelength) < 0) { |
---|
1676 | n/a | return NULL; |
---|
1677 | n/a | } |
---|
1678 | n/a | |
---|
1679 | n/a | if (slicelength <= 0) { |
---|
1680 | n/a | return PyBytes_FromStringAndSize("", 0); |
---|
1681 | n/a | } |
---|
1682 | n/a | else if (start == 0 && step == 1 && |
---|
1683 | n/a | slicelength == PyBytes_GET_SIZE(self) && |
---|
1684 | n/a | PyBytes_CheckExact(self)) { |
---|
1685 | n/a | Py_INCREF(self); |
---|
1686 | n/a | return (PyObject *)self; |
---|
1687 | n/a | } |
---|
1688 | n/a | else if (step == 1) { |
---|
1689 | n/a | return PyBytes_FromStringAndSize( |
---|
1690 | n/a | PyBytes_AS_STRING(self) + start, |
---|
1691 | n/a | slicelength); |
---|
1692 | n/a | } |
---|
1693 | n/a | else { |
---|
1694 | n/a | source_buf = PyBytes_AS_STRING(self); |
---|
1695 | n/a | result = PyBytes_FromStringAndSize(NULL, slicelength); |
---|
1696 | n/a | if (result == NULL) |
---|
1697 | n/a | return NULL; |
---|
1698 | n/a | |
---|
1699 | n/a | result_buf = PyBytes_AS_STRING(result); |
---|
1700 | n/a | for (cur = start, i = 0; i < slicelength; |
---|
1701 | n/a | cur += step, i++) { |
---|
1702 | n/a | result_buf[i] = source_buf[cur]; |
---|
1703 | n/a | } |
---|
1704 | n/a | |
---|
1705 | n/a | return result; |
---|
1706 | n/a | } |
---|
1707 | n/a | } |
---|
1708 | n/a | else { |
---|
1709 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1710 | n/a | "byte indices must be integers or slices, not %.200s", |
---|
1711 | n/a | Py_TYPE(item)->tp_name); |
---|
1712 | n/a | return NULL; |
---|
1713 | n/a | } |
---|
1714 | n/a | } |
---|
1715 | n/a | |
---|
1716 | n/a | static int |
---|
1717 | n/a | bytes_buffer_getbuffer(PyBytesObject *self, Py_buffer *view, int flags) |
---|
1718 | n/a | { |
---|
1719 | n/a | return PyBuffer_FillInfo(view, (PyObject*)self, (void *)self->ob_sval, Py_SIZE(self), |
---|
1720 | n/a | 1, flags); |
---|
1721 | n/a | } |
---|
1722 | n/a | |
---|
1723 | n/a | static PySequenceMethods bytes_as_sequence = { |
---|
1724 | n/a | (lenfunc)bytes_length, /*sq_length*/ |
---|
1725 | n/a | (binaryfunc)bytes_concat, /*sq_concat*/ |
---|
1726 | n/a | (ssizeargfunc)bytes_repeat, /*sq_repeat*/ |
---|
1727 | n/a | (ssizeargfunc)bytes_item, /*sq_item*/ |
---|
1728 | n/a | 0, /*sq_slice*/ |
---|
1729 | n/a | 0, /*sq_ass_item*/ |
---|
1730 | n/a | 0, /*sq_ass_slice*/ |
---|
1731 | n/a | (objobjproc)bytes_contains /*sq_contains*/ |
---|
1732 | n/a | }; |
---|
1733 | n/a | |
---|
1734 | n/a | static PyMappingMethods bytes_as_mapping = { |
---|
1735 | n/a | (lenfunc)bytes_length, |
---|
1736 | n/a | (binaryfunc)bytes_subscript, |
---|
1737 | n/a | 0, |
---|
1738 | n/a | }; |
---|
1739 | n/a | |
---|
1740 | n/a | static PyBufferProcs bytes_as_buffer = { |
---|
1741 | n/a | (getbufferproc)bytes_buffer_getbuffer, |
---|
1742 | n/a | NULL, |
---|
1743 | n/a | }; |
---|
1744 | n/a | |
---|
1745 | n/a | |
---|
1746 | n/a | #define LEFTSTRIP 0 |
---|
1747 | n/a | #define RIGHTSTRIP 1 |
---|
1748 | n/a | #define BOTHSTRIP 2 |
---|
1749 | n/a | |
---|
1750 | n/a | /*[clinic input] |
---|
1751 | n/a | bytes.split |
---|
1752 | n/a | |
---|
1753 | n/a | sep: object = None |
---|
1754 | n/a | The delimiter according which to split the bytes. |
---|
1755 | n/a | None (the default value) means split on ASCII whitespace characters |
---|
1756 | n/a | (space, tab, return, newline, formfeed, vertical tab). |
---|
1757 | n/a | maxsplit: Py_ssize_t = -1 |
---|
1758 | n/a | Maximum number of splits to do. |
---|
1759 | n/a | -1 (the default value) means no limit. |
---|
1760 | n/a | |
---|
1761 | n/a | Return a list of the sections in the bytes, using sep as the delimiter. |
---|
1762 | n/a | [clinic start generated code]*/ |
---|
1763 | n/a | |
---|
1764 | n/a | static PyObject * |
---|
1765 | n/a | bytes_split_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit) |
---|
1766 | n/a | /*[clinic end generated code: output=52126b5844c1d8ef input=8b809b39074abbfa]*/ |
---|
1767 | n/a | { |
---|
1768 | n/a | Py_ssize_t len = PyBytes_GET_SIZE(self), n; |
---|
1769 | n/a | const char *s = PyBytes_AS_STRING(self), *sub; |
---|
1770 | n/a | Py_buffer vsub; |
---|
1771 | n/a | PyObject *list; |
---|
1772 | n/a | |
---|
1773 | n/a | if (maxsplit < 0) |
---|
1774 | n/a | maxsplit = PY_SSIZE_T_MAX; |
---|
1775 | n/a | if (sep == Py_None) |
---|
1776 | n/a | return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); |
---|
1777 | n/a | if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) |
---|
1778 | n/a | return NULL; |
---|
1779 | n/a | sub = vsub.buf; |
---|
1780 | n/a | n = vsub.len; |
---|
1781 | n/a | |
---|
1782 | n/a | list = stringlib_split((PyObject*) self, s, len, sub, n, maxsplit); |
---|
1783 | n/a | PyBuffer_Release(&vsub); |
---|
1784 | n/a | return list; |
---|
1785 | n/a | } |
---|
1786 | n/a | |
---|
1787 | n/a | /*[clinic input] |
---|
1788 | n/a | bytes.partition |
---|
1789 | n/a | |
---|
1790 | n/a | sep: Py_buffer |
---|
1791 | n/a | / |
---|
1792 | n/a | |
---|
1793 | n/a | Partition the bytes into three parts using the given separator. |
---|
1794 | n/a | |
---|
1795 | n/a | This will search for the separator sep in the bytes. If the separator is found, |
---|
1796 | n/a | returns a 3-tuple containing the part before the separator, the separator |
---|
1797 | n/a | itself, and the part after it. |
---|
1798 | n/a | |
---|
1799 | n/a | If the separator is not found, returns a 3-tuple containing the original bytes |
---|
1800 | n/a | object and two empty bytes objects. |
---|
1801 | n/a | [clinic start generated code]*/ |
---|
1802 | n/a | |
---|
1803 | n/a | static PyObject * |
---|
1804 | n/a | bytes_partition_impl(PyBytesObject *self, Py_buffer *sep) |
---|
1805 | n/a | /*[clinic end generated code: output=f532b392a17ff695 input=61cca95519406099]*/ |
---|
1806 | n/a | { |
---|
1807 | n/a | return stringlib_partition( |
---|
1808 | n/a | (PyObject*) self, |
---|
1809 | n/a | PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
---|
1810 | n/a | sep->obj, (const char *)sep->buf, sep->len |
---|
1811 | n/a | ); |
---|
1812 | n/a | } |
---|
1813 | n/a | |
---|
1814 | n/a | /*[clinic input] |
---|
1815 | n/a | bytes.rpartition |
---|
1816 | n/a | |
---|
1817 | n/a | sep: Py_buffer |
---|
1818 | n/a | / |
---|
1819 | n/a | |
---|
1820 | n/a | Partition the bytes into three parts using the given separator. |
---|
1821 | n/a | |
---|
1822 | n/a | This will search for the separator sep in the bytes, starting and the end. If |
---|
1823 | n/a | the separator is found, returns a 3-tuple containing the part before the |
---|
1824 | n/a | separator, the separator itself, and the part after it. |
---|
1825 | n/a | |
---|
1826 | n/a | If the separator is not found, returns a 3-tuple containing two empty bytes |
---|
1827 | n/a | objects and the original bytes object. |
---|
1828 | n/a | [clinic start generated code]*/ |
---|
1829 | n/a | |
---|
1830 | n/a | static PyObject * |
---|
1831 | n/a | bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep) |
---|
1832 | n/a | /*[clinic end generated code: output=191b114cbb028e50 input=67f689e63a62d478]*/ |
---|
1833 | n/a | { |
---|
1834 | n/a | return stringlib_rpartition( |
---|
1835 | n/a | (PyObject*) self, |
---|
1836 | n/a | PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
---|
1837 | n/a | sep->obj, (const char *)sep->buf, sep->len |
---|
1838 | n/a | ); |
---|
1839 | n/a | } |
---|
1840 | n/a | |
---|
1841 | n/a | /*[clinic input] |
---|
1842 | n/a | bytes.rsplit = bytes.split |
---|
1843 | n/a | |
---|
1844 | n/a | Return a list of the sections in the bytes, using sep as the delimiter. |
---|
1845 | n/a | |
---|
1846 | n/a | Splitting is done starting at the end of the bytes and working to the front. |
---|
1847 | n/a | [clinic start generated code]*/ |
---|
1848 | n/a | |
---|
1849 | n/a | static PyObject * |
---|
1850 | n/a | bytes_rsplit_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit) |
---|
1851 | n/a | /*[clinic end generated code: output=ba698d9ea01e1c8f input=0f86c9f28f7d7b7b]*/ |
---|
1852 | n/a | { |
---|
1853 | n/a | Py_ssize_t len = PyBytes_GET_SIZE(self), n; |
---|
1854 | n/a | const char *s = PyBytes_AS_STRING(self), *sub; |
---|
1855 | n/a | Py_buffer vsub; |
---|
1856 | n/a | PyObject *list; |
---|
1857 | n/a | |
---|
1858 | n/a | if (maxsplit < 0) |
---|
1859 | n/a | maxsplit = PY_SSIZE_T_MAX; |
---|
1860 | n/a | if (sep == Py_None) |
---|
1861 | n/a | return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); |
---|
1862 | n/a | if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) |
---|
1863 | n/a | return NULL; |
---|
1864 | n/a | sub = vsub.buf; |
---|
1865 | n/a | n = vsub.len; |
---|
1866 | n/a | |
---|
1867 | n/a | list = stringlib_rsplit((PyObject*) self, s, len, sub, n, maxsplit); |
---|
1868 | n/a | PyBuffer_Release(&vsub); |
---|
1869 | n/a | return list; |
---|
1870 | n/a | } |
---|
1871 | n/a | |
---|
1872 | n/a | |
---|
1873 | n/a | /*[clinic input] |
---|
1874 | n/a | bytes.join |
---|
1875 | n/a | |
---|
1876 | n/a | iterable_of_bytes: object |
---|
1877 | n/a | / |
---|
1878 | n/a | |
---|
1879 | n/a | Concatenate any number of bytes objects. |
---|
1880 | n/a | |
---|
1881 | n/a | The bytes whose method is called is inserted in between each pair. |
---|
1882 | n/a | |
---|
1883 | n/a | The result is returned as a new bytes object. |
---|
1884 | n/a | |
---|
1885 | n/a | Example: b'.'.join([b'ab', b'pq', b'rs']) -> b'ab.pq.rs'. |
---|
1886 | n/a | [clinic start generated code]*/ |
---|
1887 | n/a | |
---|
1888 | n/a | static PyObject * |
---|
1889 | n/a | bytes_join(PyBytesObject *self, PyObject *iterable_of_bytes) |
---|
1890 | n/a | /*[clinic end generated code: output=a046f379f626f6f8 input=7fe377b95bd549d2]*/ |
---|
1891 | n/a | { |
---|
1892 | n/a | return stringlib_bytes_join((PyObject*)self, iterable_of_bytes); |
---|
1893 | n/a | } |
---|
1894 | n/a | |
---|
1895 | n/a | PyObject * |
---|
1896 | n/a | _PyBytes_Join(PyObject *sep, PyObject *x) |
---|
1897 | n/a | { |
---|
1898 | n/a | assert(sep != NULL && PyBytes_Check(sep)); |
---|
1899 | n/a | assert(x != NULL); |
---|
1900 | n/a | return bytes_join((PyBytesObject*)sep, x); |
---|
1901 | n/a | } |
---|
1902 | n/a | |
---|
1903 | n/a | static PyObject * |
---|
1904 | n/a | bytes_find(PyBytesObject *self, PyObject *args) |
---|
1905 | n/a | { |
---|
1906 | n/a | return _Py_bytes_find(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args); |
---|
1907 | n/a | } |
---|
1908 | n/a | |
---|
1909 | n/a | static PyObject * |
---|
1910 | n/a | bytes_index(PyBytesObject *self, PyObject *args) |
---|
1911 | n/a | { |
---|
1912 | n/a | return _Py_bytes_index(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args); |
---|
1913 | n/a | } |
---|
1914 | n/a | |
---|
1915 | n/a | |
---|
1916 | n/a | static PyObject * |
---|
1917 | n/a | bytes_rfind(PyBytesObject *self, PyObject *args) |
---|
1918 | n/a | { |
---|
1919 | n/a | return _Py_bytes_rfind(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args); |
---|
1920 | n/a | } |
---|
1921 | n/a | |
---|
1922 | n/a | |
---|
1923 | n/a | static PyObject * |
---|
1924 | n/a | bytes_rindex(PyBytesObject *self, PyObject *args) |
---|
1925 | n/a | { |
---|
1926 | n/a | return _Py_bytes_rindex(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args); |
---|
1927 | n/a | } |
---|
1928 | n/a | |
---|
1929 | n/a | |
---|
1930 | n/a | Py_LOCAL_INLINE(PyObject *) |
---|
1931 | n/a | do_xstrip(PyBytesObject *self, int striptype, PyObject *sepobj) |
---|
1932 | n/a | { |
---|
1933 | n/a | Py_buffer vsep; |
---|
1934 | n/a | char *s = PyBytes_AS_STRING(self); |
---|
1935 | n/a | Py_ssize_t len = PyBytes_GET_SIZE(self); |
---|
1936 | n/a | char *sep; |
---|
1937 | n/a | Py_ssize_t seplen; |
---|
1938 | n/a | Py_ssize_t i, j; |
---|
1939 | n/a | |
---|
1940 | n/a | if (PyObject_GetBuffer(sepobj, &vsep, PyBUF_SIMPLE) != 0) |
---|
1941 | n/a | return NULL; |
---|
1942 | n/a | sep = vsep.buf; |
---|
1943 | n/a | seplen = vsep.len; |
---|
1944 | n/a | |
---|
1945 | n/a | i = 0; |
---|
1946 | n/a | if (striptype != RIGHTSTRIP) { |
---|
1947 | n/a | while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) { |
---|
1948 | n/a | i++; |
---|
1949 | n/a | } |
---|
1950 | n/a | } |
---|
1951 | n/a | |
---|
1952 | n/a | j = len; |
---|
1953 | n/a | if (striptype != LEFTSTRIP) { |
---|
1954 | n/a | do { |
---|
1955 | n/a | j--; |
---|
1956 | n/a | } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen)); |
---|
1957 | n/a | j++; |
---|
1958 | n/a | } |
---|
1959 | n/a | |
---|
1960 | n/a | PyBuffer_Release(&vsep); |
---|
1961 | n/a | |
---|
1962 | n/a | if (i == 0 && j == len && PyBytes_CheckExact(self)) { |
---|
1963 | n/a | Py_INCREF(self); |
---|
1964 | n/a | return (PyObject*)self; |
---|
1965 | n/a | } |
---|
1966 | n/a | else |
---|
1967 | n/a | return PyBytes_FromStringAndSize(s+i, j-i); |
---|
1968 | n/a | } |
---|
1969 | n/a | |
---|
1970 | n/a | |
---|
1971 | n/a | Py_LOCAL_INLINE(PyObject *) |
---|
1972 | n/a | do_strip(PyBytesObject *self, int striptype) |
---|
1973 | n/a | { |
---|
1974 | n/a | char *s = PyBytes_AS_STRING(self); |
---|
1975 | n/a | Py_ssize_t len = PyBytes_GET_SIZE(self), i, j; |
---|
1976 | n/a | |
---|
1977 | n/a | i = 0; |
---|
1978 | n/a | if (striptype != RIGHTSTRIP) { |
---|
1979 | n/a | while (i < len && Py_ISSPACE(s[i])) { |
---|
1980 | n/a | i++; |
---|
1981 | n/a | } |
---|
1982 | n/a | } |
---|
1983 | n/a | |
---|
1984 | n/a | j = len; |
---|
1985 | n/a | if (striptype != LEFTSTRIP) { |
---|
1986 | n/a | do { |
---|
1987 | n/a | j--; |
---|
1988 | n/a | } while (j >= i && Py_ISSPACE(s[j])); |
---|
1989 | n/a | j++; |
---|
1990 | n/a | } |
---|
1991 | n/a | |
---|
1992 | n/a | if (i == 0 && j == len && PyBytes_CheckExact(self)) { |
---|
1993 | n/a | Py_INCREF(self); |
---|
1994 | n/a | return (PyObject*)self; |
---|
1995 | n/a | } |
---|
1996 | n/a | else |
---|
1997 | n/a | return PyBytes_FromStringAndSize(s+i, j-i); |
---|
1998 | n/a | } |
---|
1999 | n/a | |
---|
2000 | n/a | |
---|
2001 | n/a | Py_LOCAL_INLINE(PyObject *) |
---|
2002 | n/a | do_argstrip(PyBytesObject *self, int striptype, PyObject *bytes) |
---|
2003 | n/a | { |
---|
2004 | n/a | if (bytes != NULL && bytes != Py_None) { |
---|
2005 | n/a | return do_xstrip(self, striptype, bytes); |
---|
2006 | n/a | } |
---|
2007 | n/a | return do_strip(self, striptype); |
---|
2008 | n/a | } |
---|
2009 | n/a | |
---|
2010 | n/a | /*[clinic input] |
---|
2011 | n/a | bytes.strip |
---|
2012 | n/a | |
---|
2013 | n/a | bytes: object = None |
---|
2014 | n/a | / |
---|
2015 | n/a | |
---|
2016 | n/a | Strip leading and trailing bytes contained in the argument. |
---|
2017 | n/a | |
---|
2018 | n/a | If the argument is omitted or None, strip leading and trailing ASCII whitespace. |
---|
2019 | n/a | [clinic start generated code]*/ |
---|
2020 | n/a | |
---|
2021 | n/a | static PyObject * |
---|
2022 | n/a | bytes_strip_impl(PyBytesObject *self, PyObject *bytes) |
---|
2023 | n/a | /*[clinic end generated code: output=c7c228d3bd104a1b input=8a354640e4e0b3ef]*/ |
---|
2024 | n/a | { |
---|
2025 | n/a | return do_argstrip(self, BOTHSTRIP, bytes); |
---|
2026 | n/a | } |
---|
2027 | n/a | |
---|
2028 | n/a | /*[clinic input] |
---|
2029 | n/a | bytes.lstrip |
---|
2030 | n/a | |
---|
2031 | n/a | bytes: object = None |
---|
2032 | n/a | / |
---|
2033 | n/a | |
---|
2034 | n/a | Strip leading bytes contained in the argument. |
---|
2035 | n/a | |
---|
2036 | n/a | If the argument is omitted or None, strip leading ASCII whitespace. |
---|
2037 | n/a | [clinic start generated code]*/ |
---|
2038 | n/a | |
---|
2039 | n/a | static PyObject * |
---|
2040 | n/a | bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes) |
---|
2041 | n/a | /*[clinic end generated code: output=28602e586f524e82 input=9baff4398c3f6857]*/ |
---|
2042 | n/a | { |
---|
2043 | n/a | return do_argstrip(self, LEFTSTRIP, bytes); |
---|
2044 | n/a | } |
---|
2045 | n/a | |
---|
2046 | n/a | /*[clinic input] |
---|
2047 | n/a | bytes.rstrip |
---|
2048 | n/a | |
---|
2049 | n/a | bytes: object = None |
---|
2050 | n/a | / |
---|
2051 | n/a | |
---|
2052 | n/a | Strip trailing bytes contained in the argument. |
---|
2053 | n/a | |
---|
2054 | n/a | If the argument is omitted or None, strip trailing ASCII whitespace. |
---|
2055 | n/a | [clinic start generated code]*/ |
---|
2056 | n/a | |
---|
2057 | n/a | static PyObject * |
---|
2058 | n/a | bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes) |
---|
2059 | n/a | /*[clinic end generated code: output=547e3815c95447da input=b78af445c727e32b]*/ |
---|
2060 | n/a | { |
---|
2061 | n/a | return do_argstrip(self, RIGHTSTRIP, bytes); |
---|
2062 | n/a | } |
---|
2063 | n/a | |
---|
2064 | n/a | |
---|
2065 | n/a | static PyObject * |
---|
2066 | n/a | bytes_count(PyBytesObject *self, PyObject *args) |
---|
2067 | n/a | { |
---|
2068 | n/a | return _Py_bytes_count(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args); |
---|
2069 | n/a | } |
---|
2070 | n/a | |
---|
2071 | n/a | |
---|
2072 | n/a | /*[clinic input] |
---|
2073 | n/a | bytes.translate |
---|
2074 | n/a | |
---|
2075 | n/a | table: object |
---|
2076 | n/a | Translation table, which must be a bytes object of length 256. |
---|
2077 | n/a | / |
---|
2078 | n/a | delete as deletechars: object(c_default="NULL") = b'' |
---|
2079 | n/a | |
---|
2080 | n/a | Return a copy with each character mapped by the given translation table. |
---|
2081 | n/a | |
---|
2082 | n/a | All characters occurring in the optional argument delete are removed. |
---|
2083 | n/a | The remaining characters are mapped through the given translation table. |
---|
2084 | n/a | [clinic start generated code]*/ |
---|
2085 | n/a | |
---|
2086 | n/a | static PyObject * |
---|
2087 | n/a | bytes_translate_impl(PyBytesObject *self, PyObject *table, |
---|
2088 | n/a | PyObject *deletechars) |
---|
2089 | n/a | /*[clinic end generated code: output=43be3437f1956211 input=0ecdf159f654233c]*/ |
---|
2090 | n/a | { |
---|
2091 | n/a | char *input, *output; |
---|
2092 | n/a | Py_buffer table_view = {NULL, NULL}; |
---|
2093 | n/a | Py_buffer del_table_view = {NULL, NULL}; |
---|
2094 | n/a | const char *table_chars; |
---|
2095 | n/a | Py_ssize_t i, c, changed = 0; |
---|
2096 | n/a | PyObject *input_obj = (PyObject*)self; |
---|
2097 | n/a | const char *output_start, *del_table_chars=NULL; |
---|
2098 | n/a | Py_ssize_t inlen, tablen, dellen = 0; |
---|
2099 | n/a | PyObject *result; |
---|
2100 | n/a | int trans_table[256]; |
---|
2101 | n/a | |
---|
2102 | n/a | if (PyBytes_Check(table)) { |
---|
2103 | n/a | table_chars = PyBytes_AS_STRING(table); |
---|
2104 | n/a | tablen = PyBytes_GET_SIZE(table); |
---|
2105 | n/a | } |
---|
2106 | n/a | else if (table == Py_None) { |
---|
2107 | n/a | table_chars = NULL; |
---|
2108 | n/a | tablen = 256; |
---|
2109 | n/a | } |
---|
2110 | n/a | else { |
---|
2111 | n/a | if (PyObject_GetBuffer(table, &table_view, PyBUF_SIMPLE) != 0) |
---|
2112 | n/a | return NULL; |
---|
2113 | n/a | table_chars = table_view.buf; |
---|
2114 | n/a | tablen = table_view.len; |
---|
2115 | n/a | } |
---|
2116 | n/a | |
---|
2117 | n/a | if (tablen != 256) { |
---|
2118 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
2119 | n/a | "translation table must be 256 characters long"); |
---|
2120 | n/a | PyBuffer_Release(&table_view); |
---|
2121 | n/a | return NULL; |
---|
2122 | n/a | } |
---|
2123 | n/a | |
---|
2124 | n/a | if (deletechars != NULL) { |
---|
2125 | n/a | if (PyBytes_Check(deletechars)) { |
---|
2126 | n/a | del_table_chars = PyBytes_AS_STRING(deletechars); |
---|
2127 | n/a | dellen = PyBytes_GET_SIZE(deletechars); |
---|
2128 | n/a | } |
---|
2129 | n/a | else { |
---|
2130 | n/a | if (PyObject_GetBuffer(deletechars, &del_table_view, PyBUF_SIMPLE) != 0) { |
---|
2131 | n/a | PyBuffer_Release(&table_view); |
---|
2132 | n/a | return NULL; |
---|
2133 | n/a | } |
---|
2134 | n/a | del_table_chars = del_table_view.buf; |
---|
2135 | n/a | dellen = del_table_view.len; |
---|
2136 | n/a | } |
---|
2137 | n/a | } |
---|
2138 | n/a | else { |
---|
2139 | n/a | del_table_chars = NULL; |
---|
2140 | n/a | dellen = 0; |
---|
2141 | n/a | } |
---|
2142 | n/a | |
---|
2143 | n/a | inlen = PyBytes_GET_SIZE(input_obj); |
---|
2144 | n/a | result = PyBytes_FromStringAndSize((char *)NULL, inlen); |
---|
2145 | n/a | if (result == NULL) { |
---|
2146 | n/a | PyBuffer_Release(&del_table_view); |
---|
2147 | n/a | PyBuffer_Release(&table_view); |
---|
2148 | n/a | return NULL; |
---|
2149 | n/a | } |
---|
2150 | n/a | output_start = output = PyBytes_AS_STRING(result); |
---|
2151 | n/a | input = PyBytes_AS_STRING(input_obj); |
---|
2152 | n/a | |
---|
2153 | n/a | if (dellen == 0 && table_chars != NULL) { |
---|
2154 | n/a | /* If no deletions are required, use faster code */ |
---|
2155 | n/a | for (i = inlen; --i >= 0; ) { |
---|
2156 | n/a | c = Py_CHARMASK(*input++); |
---|
2157 | n/a | if (Py_CHARMASK((*output++ = table_chars[c])) != c) |
---|
2158 | n/a | changed = 1; |
---|
2159 | n/a | } |
---|
2160 | n/a | if (!changed && PyBytes_CheckExact(input_obj)) { |
---|
2161 | n/a | Py_INCREF(input_obj); |
---|
2162 | n/a | Py_DECREF(result); |
---|
2163 | n/a | result = input_obj; |
---|
2164 | n/a | } |
---|
2165 | n/a | PyBuffer_Release(&del_table_view); |
---|
2166 | n/a | PyBuffer_Release(&table_view); |
---|
2167 | n/a | return result; |
---|
2168 | n/a | } |
---|
2169 | n/a | |
---|
2170 | n/a | if (table_chars == NULL) { |
---|
2171 | n/a | for (i = 0; i < 256; i++) |
---|
2172 | n/a | trans_table[i] = Py_CHARMASK(i); |
---|
2173 | n/a | } else { |
---|
2174 | n/a | for (i = 0; i < 256; i++) |
---|
2175 | n/a | trans_table[i] = Py_CHARMASK(table_chars[i]); |
---|
2176 | n/a | } |
---|
2177 | n/a | PyBuffer_Release(&table_view); |
---|
2178 | n/a | |
---|
2179 | n/a | for (i = 0; i < dellen; i++) |
---|
2180 | n/a | trans_table[(int) Py_CHARMASK(del_table_chars[i])] = -1; |
---|
2181 | n/a | PyBuffer_Release(&del_table_view); |
---|
2182 | n/a | |
---|
2183 | n/a | for (i = inlen; --i >= 0; ) { |
---|
2184 | n/a | c = Py_CHARMASK(*input++); |
---|
2185 | n/a | if (trans_table[c] != -1) |
---|
2186 | n/a | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
---|
2187 | n/a | continue; |
---|
2188 | n/a | changed = 1; |
---|
2189 | n/a | } |
---|
2190 | n/a | if (!changed && PyBytes_CheckExact(input_obj)) { |
---|
2191 | n/a | Py_DECREF(result); |
---|
2192 | n/a | Py_INCREF(input_obj); |
---|
2193 | n/a | return input_obj; |
---|
2194 | n/a | } |
---|
2195 | n/a | /* Fix the size of the resulting string */ |
---|
2196 | n/a | if (inlen > 0) |
---|
2197 | n/a | _PyBytes_Resize(&result, output - output_start); |
---|
2198 | n/a | return result; |
---|
2199 | n/a | } |
---|
2200 | n/a | |
---|
2201 | n/a | |
---|
2202 | n/a | /*[clinic input] |
---|
2203 | n/a | |
---|
2204 | n/a | @staticmethod |
---|
2205 | n/a | bytes.maketrans |
---|
2206 | n/a | |
---|
2207 | n/a | frm: Py_buffer |
---|
2208 | n/a | to: Py_buffer |
---|
2209 | n/a | / |
---|
2210 | n/a | |
---|
2211 | n/a | Return a translation table useable for the bytes or bytearray translate method. |
---|
2212 | n/a | |
---|
2213 | n/a | The returned table will be one where each byte in frm is mapped to the byte at |
---|
2214 | n/a | the same position in to. |
---|
2215 | n/a | |
---|
2216 | n/a | The bytes objects frm and to must be of the same length. |
---|
2217 | n/a | [clinic start generated code]*/ |
---|
2218 | n/a | |
---|
2219 | n/a | static PyObject * |
---|
2220 | n/a | bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to) |
---|
2221 | n/a | /*[clinic end generated code: output=a36f6399d4b77f6f input=de7a8fc5632bb8f1]*/ |
---|
2222 | n/a | { |
---|
2223 | n/a | return _Py_bytes_maketrans(frm, to); |
---|
2224 | n/a | } |
---|
2225 | n/a | |
---|
2226 | n/a | |
---|
2227 | n/a | /*[clinic input] |
---|
2228 | n/a | bytes.replace |
---|
2229 | n/a | |
---|
2230 | n/a | old: Py_buffer |
---|
2231 | n/a | new: Py_buffer |
---|
2232 | n/a | count: Py_ssize_t = -1 |
---|
2233 | n/a | Maximum number of occurrences to replace. |
---|
2234 | n/a | -1 (the default value) means replace all occurrences. |
---|
2235 | n/a | / |
---|
2236 | n/a | |
---|
2237 | n/a | Return a copy with all occurrences of substring old replaced by new. |
---|
2238 | n/a | |
---|
2239 | n/a | If the optional argument count is given, only the first count occurrences are |
---|
2240 | n/a | replaced. |
---|
2241 | n/a | [clinic start generated code]*/ |
---|
2242 | n/a | |
---|
2243 | n/a | static PyObject * |
---|
2244 | n/a | bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new, |
---|
2245 | n/a | Py_ssize_t count) |
---|
2246 | n/a | /*[clinic end generated code: output=994fa588b6b9c104 input=b2fbbf0bf04de8e5]*/ |
---|
2247 | n/a | { |
---|
2248 | n/a | return stringlib_replace((PyObject *)self, |
---|
2249 | n/a | (const char *)old->buf, old->len, |
---|
2250 | n/a | (const char *)new->buf, new->len, count); |
---|
2251 | n/a | } |
---|
2252 | n/a | |
---|
2253 | n/a | /** End DALKE **/ |
---|
2254 | n/a | |
---|
2255 | n/a | |
---|
2256 | n/a | static PyObject * |
---|
2257 | n/a | bytes_startswith(PyBytesObject *self, PyObject *args) |
---|
2258 | n/a | { |
---|
2259 | n/a | return _Py_bytes_startswith(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args); |
---|
2260 | n/a | } |
---|
2261 | n/a | |
---|
2262 | n/a | static PyObject * |
---|
2263 | n/a | bytes_endswith(PyBytesObject *self, PyObject *args) |
---|
2264 | n/a | { |
---|
2265 | n/a | return _Py_bytes_endswith(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args); |
---|
2266 | n/a | } |
---|
2267 | n/a | |
---|
2268 | n/a | |
---|
2269 | n/a | /*[clinic input] |
---|
2270 | n/a | bytes.decode |
---|
2271 | n/a | |
---|
2272 | n/a | encoding: str(c_default="NULL") = 'utf-8' |
---|
2273 | n/a | The encoding with which to decode the bytes. |
---|
2274 | n/a | errors: str(c_default="NULL") = 'strict' |
---|
2275 | n/a | The error handling scheme to use for the handling of decoding errors. |
---|
2276 | n/a | The default is 'strict' meaning that decoding errors raise a |
---|
2277 | n/a | UnicodeDecodeError. Other possible values are 'ignore' and 'replace' |
---|
2278 | n/a | as well as any other name registered with codecs.register_error that |
---|
2279 | n/a | can handle UnicodeDecodeErrors. |
---|
2280 | n/a | |
---|
2281 | n/a | Decode the bytes using the codec registered for encoding. |
---|
2282 | n/a | [clinic start generated code]*/ |
---|
2283 | n/a | |
---|
2284 | n/a | static PyObject * |
---|
2285 | n/a | bytes_decode_impl(PyBytesObject *self, const char *encoding, |
---|
2286 | n/a | const char *errors) |
---|
2287 | n/a | /*[clinic end generated code: output=5649a53dde27b314 input=958174769d2a40ca]*/ |
---|
2288 | n/a | { |
---|
2289 | n/a | return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors); |
---|
2290 | n/a | } |
---|
2291 | n/a | |
---|
2292 | n/a | |
---|
2293 | n/a | /*[clinic input] |
---|
2294 | n/a | bytes.splitlines |
---|
2295 | n/a | |
---|
2296 | n/a | keepends: int(c_default="0") = False |
---|
2297 | n/a | |
---|
2298 | n/a | Return a list of the lines in the bytes, breaking at line boundaries. |
---|
2299 | n/a | |
---|
2300 | n/a | Line breaks are not included in the resulting list unless keepends is given and |
---|
2301 | n/a | true. |
---|
2302 | n/a | [clinic start generated code]*/ |
---|
2303 | n/a | |
---|
2304 | n/a | static PyObject * |
---|
2305 | n/a | bytes_splitlines_impl(PyBytesObject *self, int keepends) |
---|
2306 | n/a | /*[clinic end generated code: output=3484149a5d880ffb input=7f4aac67144f9944]*/ |
---|
2307 | n/a | { |
---|
2308 | n/a | return stringlib_splitlines( |
---|
2309 | n/a | (PyObject*) self, PyBytes_AS_STRING(self), |
---|
2310 | n/a | PyBytes_GET_SIZE(self), keepends |
---|
2311 | n/a | ); |
---|
2312 | n/a | } |
---|
2313 | n/a | |
---|
2314 | n/a | /*[clinic input] |
---|
2315 | n/a | @classmethod |
---|
2316 | n/a | bytes.fromhex |
---|
2317 | n/a | |
---|
2318 | n/a | string: unicode |
---|
2319 | n/a | / |
---|
2320 | n/a | |
---|
2321 | n/a | Create a bytes object from a string of hexadecimal numbers. |
---|
2322 | n/a | |
---|
2323 | n/a | Spaces between two numbers are accepted. |
---|
2324 | n/a | Example: bytes.fromhex('B9 01EF') -> b'\\xb9\\x01\\xef'. |
---|
2325 | n/a | [clinic start generated code]*/ |
---|
2326 | n/a | |
---|
2327 | n/a | static PyObject * |
---|
2328 | n/a | bytes_fromhex_impl(PyTypeObject *type, PyObject *string) |
---|
2329 | n/a | /*[clinic end generated code: output=0973acc63661bb2e input=bf4d1c361670acd3]*/ |
---|
2330 | n/a | { |
---|
2331 | n/a | PyObject *result = _PyBytes_FromHex(string, 0); |
---|
2332 | n/a | if (type != &PyBytes_Type && result != NULL) { |
---|
2333 | n/a | Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type, |
---|
2334 | n/a | result, NULL)); |
---|
2335 | n/a | } |
---|
2336 | n/a | return result; |
---|
2337 | n/a | } |
---|
2338 | n/a | |
---|
2339 | n/a | PyObject* |
---|
2340 | n/a | _PyBytes_FromHex(PyObject *string, int use_bytearray) |
---|
2341 | n/a | { |
---|
2342 | n/a | char *buf; |
---|
2343 | n/a | Py_ssize_t hexlen, invalid_char; |
---|
2344 | n/a | unsigned int top, bot; |
---|
2345 | n/a | Py_UCS1 *str, *end; |
---|
2346 | n/a | _PyBytesWriter writer; |
---|
2347 | n/a | |
---|
2348 | n/a | _PyBytesWriter_Init(&writer); |
---|
2349 | n/a | writer.use_bytearray = use_bytearray; |
---|
2350 | n/a | |
---|
2351 | n/a | assert(PyUnicode_Check(string)); |
---|
2352 | n/a | if (PyUnicode_READY(string)) |
---|
2353 | n/a | return NULL; |
---|
2354 | n/a | hexlen = PyUnicode_GET_LENGTH(string); |
---|
2355 | n/a | |
---|
2356 | n/a | if (!PyUnicode_IS_ASCII(string)) { |
---|
2357 | n/a | void *data = PyUnicode_DATA(string); |
---|
2358 | n/a | unsigned int kind = PyUnicode_KIND(string); |
---|
2359 | n/a | Py_ssize_t i; |
---|
2360 | n/a | |
---|
2361 | n/a | /* search for the first non-ASCII character */ |
---|
2362 | n/a | for (i = 0; i < hexlen; i++) { |
---|
2363 | n/a | if (PyUnicode_READ(kind, data, i) >= 128) |
---|
2364 | n/a | break; |
---|
2365 | n/a | } |
---|
2366 | n/a | invalid_char = i; |
---|
2367 | n/a | goto error; |
---|
2368 | n/a | } |
---|
2369 | n/a | |
---|
2370 | n/a | assert(PyUnicode_KIND(string) == PyUnicode_1BYTE_KIND); |
---|
2371 | n/a | str = PyUnicode_1BYTE_DATA(string); |
---|
2372 | n/a | |
---|
2373 | n/a | /* This overestimates if there are spaces */ |
---|
2374 | n/a | buf = _PyBytesWriter_Alloc(&writer, hexlen / 2); |
---|
2375 | n/a | if (buf == NULL) |
---|
2376 | n/a | return NULL; |
---|
2377 | n/a | |
---|
2378 | n/a | end = str + hexlen; |
---|
2379 | n/a | while (str < end) { |
---|
2380 | n/a | /* skip over spaces in the input */ |
---|
2381 | n/a | if (Py_ISSPACE(*str)) { |
---|
2382 | n/a | do { |
---|
2383 | n/a | str++; |
---|
2384 | n/a | } while (Py_ISSPACE(*str)); |
---|
2385 | n/a | if (str >= end) |
---|
2386 | n/a | break; |
---|
2387 | n/a | } |
---|
2388 | n/a | |
---|
2389 | n/a | top = _PyLong_DigitValue[*str]; |
---|
2390 | n/a | if (top >= 16) { |
---|
2391 | n/a | invalid_char = str - PyUnicode_1BYTE_DATA(string); |
---|
2392 | n/a | goto error; |
---|
2393 | n/a | } |
---|
2394 | n/a | str++; |
---|
2395 | n/a | |
---|
2396 | n/a | bot = _PyLong_DigitValue[*str]; |
---|
2397 | n/a | if (bot >= 16) { |
---|
2398 | n/a | invalid_char = str - PyUnicode_1BYTE_DATA(string); |
---|
2399 | n/a | goto error; |
---|
2400 | n/a | } |
---|
2401 | n/a | str++; |
---|
2402 | n/a | |
---|
2403 | n/a | *buf++ = (unsigned char)((top << 4) + bot); |
---|
2404 | n/a | } |
---|
2405 | n/a | |
---|
2406 | n/a | return _PyBytesWriter_Finish(&writer, buf); |
---|
2407 | n/a | |
---|
2408 | n/a | error: |
---|
2409 | n/a | PyErr_Format(PyExc_ValueError, |
---|
2410 | n/a | "non-hexadecimal number found in " |
---|
2411 | n/a | "fromhex() arg at position %zd", invalid_char); |
---|
2412 | n/a | _PyBytesWriter_Dealloc(&writer); |
---|
2413 | n/a | return NULL; |
---|
2414 | n/a | } |
---|
2415 | n/a | |
---|
2416 | n/a | PyDoc_STRVAR(hex__doc__, |
---|
2417 | n/a | "B.hex() -> string\n\ |
---|
2418 | n/a | \n\ |
---|
2419 | n/a | Create a string of hexadecimal numbers from a bytes object.\n\ |
---|
2420 | n/a | Example: b'\\xb9\\x01\\xef'.hex() -> 'b901ef'."); |
---|
2421 | n/a | |
---|
2422 | n/a | static PyObject * |
---|
2423 | n/a | bytes_hex(PyBytesObject *self) |
---|
2424 | n/a | { |
---|
2425 | n/a | char* argbuf = PyBytes_AS_STRING(self); |
---|
2426 | n/a | Py_ssize_t arglen = PyBytes_GET_SIZE(self); |
---|
2427 | n/a | return _Py_strhex(argbuf, arglen); |
---|
2428 | n/a | } |
---|
2429 | n/a | |
---|
2430 | n/a | static PyObject * |
---|
2431 | n/a | bytes_getnewargs(PyBytesObject *v) |
---|
2432 | n/a | { |
---|
2433 | n/a | return Py_BuildValue("(y#)", v->ob_sval, Py_SIZE(v)); |
---|
2434 | n/a | } |
---|
2435 | n/a | |
---|
2436 | n/a | |
---|
2437 | n/a | static PyMethodDef |
---|
2438 | n/a | bytes_methods[] = { |
---|
2439 | n/a | {"__getnewargs__", (PyCFunction)bytes_getnewargs, METH_NOARGS}, |
---|
2440 | n/a | {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, |
---|
2441 | n/a | _Py_capitalize__doc__}, |
---|
2442 | n/a | {"center", (PyCFunction)stringlib_center, METH_VARARGS, |
---|
2443 | n/a | _Py_center__doc__}, |
---|
2444 | n/a | {"count", (PyCFunction)bytes_count, METH_VARARGS, |
---|
2445 | n/a | _Py_count__doc__}, |
---|
2446 | n/a | BYTES_DECODE_METHODDEF |
---|
2447 | n/a | {"endswith", (PyCFunction)bytes_endswith, METH_VARARGS, |
---|
2448 | n/a | _Py_endswith__doc__}, |
---|
2449 | n/a | {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS, |
---|
2450 | n/a | _Py_expandtabs__doc__}, |
---|
2451 | n/a | {"find", (PyCFunction)bytes_find, METH_VARARGS, |
---|
2452 | n/a | _Py_find__doc__}, |
---|
2453 | n/a | BYTES_FROMHEX_METHODDEF |
---|
2454 | n/a | {"hex", (PyCFunction)bytes_hex, METH_NOARGS, hex__doc__}, |
---|
2455 | n/a | {"index", (PyCFunction)bytes_index, METH_VARARGS, _Py_index__doc__}, |
---|
2456 | n/a | {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, |
---|
2457 | n/a | _Py_isalnum__doc__}, |
---|
2458 | n/a | {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, |
---|
2459 | n/a | _Py_isalpha__doc__}, |
---|
2460 | n/a | {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, |
---|
2461 | n/a | _Py_isdigit__doc__}, |
---|
2462 | n/a | {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, |
---|
2463 | n/a | _Py_islower__doc__}, |
---|
2464 | n/a | {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, |
---|
2465 | n/a | _Py_isspace__doc__}, |
---|
2466 | n/a | {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, |
---|
2467 | n/a | _Py_istitle__doc__}, |
---|
2468 | n/a | {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, |
---|
2469 | n/a | _Py_isupper__doc__}, |
---|
2470 | n/a | BYTES_JOIN_METHODDEF |
---|
2471 | n/a | {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, _Py_ljust__doc__}, |
---|
2472 | n/a | {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
---|
2473 | n/a | BYTES_LSTRIP_METHODDEF |
---|
2474 | n/a | BYTES_MAKETRANS_METHODDEF |
---|
2475 | n/a | BYTES_PARTITION_METHODDEF |
---|
2476 | n/a | BYTES_REPLACE_METHODDEF |
---|
2477 | n/a | {"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, _Py_rfind__doc__}, |
---|
2478 | n/a | {"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, _Py_rindex__doc__}, |
---|
2479 | n/a | {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, _Py_rjust__doc__}, |
---|
2480 | n/a | BYTES_RPARTITION_METHODDEF |
---|
2481 | n/a | BYTES_RSPLIT_METHODDEF |
---|
2482 | n/a | BYTES_RSTRIP_METHODDEF |
---|
2483 | n/a | BYTES_SPLIT_METHODDEF |
---|
2484 | n/a | BYTES_SPLITLINES_METHODDEF |
---|
2485 | n/a | {"startswith", (PyCFunction)bytes_startswith, METH_VARARGS, |
---|
2486 | n/a | _Py_startswith__doc__}, |
---|
2487 | n/a | BYTES_STRIP_METHODDEF |
---|
2488 | n/a | {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, |
---|
2489 | n/a | _Py_swapcase__doc__}, |
---|
2490 | n/a | {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, |
---|
2491 | n/a | BYTES_TRANSLATE_METHODDEF |
---|
2492 | n/a | {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
---|
2493 | n/a | {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, _Py_zfill__doc__}, |
---|
2494 | n/a | {NULL, NULL} /* sentinel */ |
---|
2495 | n/a | }; |
---|
2496 | n/a | |
---|
2497 | n/a | static PyObject * |
---|
2498 | n/a | bytes_mod(PyObject *self, PyObject *arg) |
---|
2499 | n/a | { |
---|
2500 | n/a | if (!PyBytes_Check(self)) { |
---|
2501 | n/a | Py_RETURN_NOTIMPLEMENTED; |
---|
2502 | n/a | } |
---|
2503 | n/a | return _PyBytes_FormatEx(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
---|
2504 | n/a | arg, 0); |
---|
2505 | n/a | } |
---|
2506 | n/a | |
---|
2507 | n/a | static PyNumberMethods bytes_as_number = { |
---|
2508 | n/a | 0, /*nb_add*/ |
---|
2509 | n/a | 0, /*nb_subtract*/ |
---|
2510 | n/a | 0, /*nb_multiply*/ |
---|
2511 | n/a | bytes_mod, /*nb_remainder*/ |
---|
2512 | n/a | }; |
---|
2513 | n/a | |
---|
2514 | n/a | static PyObject * |
---|
2515 | n/a | bytes_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
---|
2516 | n/a | |
---|
2517 | n/a | static PyObject * |
---|
2518 | n/a | bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
2519 | n/a | { |
---|
2520 | n/a | PyObject *x = NULL; |
---|
2521 | n/a | const char *encoding = NULL; |
---|
2522 | n/a | const char *errors = NULL; |
---|
2523 | n/a | PyObject *new = NULL; |
---|
2524 | n/a | PyObject *func; |
---|
2525 | n/a | Py_ssize_t size; |
---|
2526 | n/a | static char *kwlist[] = {"source", "encoding", "errors", 0}; |
---|
2527 | n/a | _Py_IDENTIFIER(__bytes__); |
---|
2528 | n/a | |
---|
2529 | n/a | if (type != &PyBytes_Type) |
---|
2530 | n/a | return bytes_subtype_new(type, args, kwds); |
---|
2531 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytes", kwlist, &x, |
---|
2532 | n/a | &encoding, &errors)) |
---|
2533 | n/a | return NULL; |
---|
2534 | n/a | if (x == NULL) { |
---|
2535 | n/a | if (encoding != NULL || errors != NULL) { |
---|
2536 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
2537 | n/a | "encoding or errors without sequence " |
---|
2538 | n/a | "argument"); |
---|
2539 | n/a | return NULL; |
---|
2540 | n/a | } |
---|
2541 | n/a | return PyBytes_FromStringAndSize(NULL, 0); |
---|
2542 | n/a | } |
---|
2543 | n/a | |
---|
2544 | n/a | if (encoding != NULL) { |
---|
2545 | n/a | /* Encode via the codec registry */ |
---|
2546 | n/a | if (!PyUnicode_Check(x)) { |
---|
2547 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
2548 | n/a | "encoding without a string argument"); |
---|
2549 | n/a | return NULL; |
---|
2550 | n/a | } |
---|
2551 | n/a | new = PyUnicode_AsEncodedString(x, encoding, errors); |
---|
2552 | n/a | if (new == NULL) |
---|
2553 | n/a | return NULL; |
---|
2554 | n/a | assert(PyBytes_Check(new)); |
---|
2555 | n/a | return new; |
---|
2556 | n/a | } |
---|
2557 | n/a | |
---|
2558 | n/a | if (errors != NULL) { |
---|
2559 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
2560 | n/a | PyUnicode_Check(x) ? |
---|
2561 | n/a | "string argument without an encoding" : |
---|
2562 | n/a | "errors without a string argument"); |
---|
2563 | n/a | return NULL; |
---|
2564 | n/a | } |
---|
2565 | n/a | |
---|
2566 | n/a | /* We'd like to call PyObject_Bytes here, but we need to check for an |
---|
2567 | n/a | integer argument before deferring to PyBytes_FromObject, something |
---|
2568 | n/a | PyObject_Bytes doesn't do. */ |
---|
2569 | n/a | func = _PyObject_LookupSpecial(x, &PyId___bytes__); |
---|
2570 | n/a | if (func != NULL) { |
---|
2571 | n/a | new = _PyObject_CallNoArg(func); |
---|
2572 | n/a | Py_DECREF(func); |
---|
2573 | n/a | if (new == NULL) |
---|
2574 | n/a | return NULL; |
---|
2575 | n/a | if (!PyBytes_Check(new)) { |
---|
2576 | n/a | PyErr_Format(PyExc_TypeError, |
---|
2577 | n/a | "__bytes__ returned non-bytes (type %.200s)", |
---|
2578 | n/a | Py_TYPE(new)->tp_name); |
---|
2579 | n/a | Py_DECREF(new); |
---|
2580 | n/a | return NULL; |
---|
2581 | n/a | } |
---|
2582 | n/a | return new; |
---|
2583 | n/a | } |
---|
2584 | n/a | else if (PyErr_Occurred()) |
---|
2585 | n/a | return NULL; |
---|
2586 | n/a | |
---|
2587 | n/a | if (PyUnicode_Check(x)) { |
---|
2588 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
2589 | n/a | "string argument without an encoding"); |
---|
2590 | n/a | return NULL; |
---|
2591 | n/a | } |
---|
2592 | n/a | /* Is it an integer? */ |
---|
2593 | n/a | if (PyIndex_Check(x)) { |
---|
2594 | n/a | size = PyNumber_AsSsize_t(x, PyExc_OverflowError); |
---|
2595 | n/a | if (size == -1 && PyErr_Occurred()) { |
---|
2596 | n/a | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
---|
2597 | n/a | return NULL; |
---|
2598 | n/a | PyErr_Clear(); /* fall through */ |
---|
2599 | n/a | } |
---|
2600 | n/a | else { |
---|
2601 | n/a | if (size < 0) { |
---|
2602 | n/a | PyErr_SetString(PyExc_ValueError, "negative count"); |
---|
2603 | n/a | return NULL; |
---|
2604 | n/a | } |
---|
2605 | n/a | new = _PyBytes_FromSize(size, 1); |
---|
2606 | n/a | if (new == NULL) |
---|
2607 | n/a | return NULL; |
---|
2608 | n/a | return new; |
---|
2609 | n/a | } |
---|
2610 | n/a | } |
---|
2611 | n/a | |
---|
2612 | n/a | return PyBytes_FromObject(x); |
---|
2613 | n/a | } |
---|
2614 | n/a | |
---|
2615 | n/a | static PyObject* |
---|
2616 | n/a | _PyBytes_FromBuffer(PyObject *x) |
---|
2617 | n/a | { |
---|
2618 | n/a | PyObject *new; |
---|
2619 | n/a | Py_buffer view; |
---|
2620 | n/a | |
---|
2621 | n/a | if (PyObject_GetBuffer(x, &view, PyBUF_FULL_RO) < 0) |
---|
2622 | n/a | return NULL; |
---|
2623 | n/a | |
---|
2624 | n/a | new = PyBytes_FromStringAndSize(NULL, view.len); |
---|
2625 | n/a | if (!new) |
---|
2626 | n/a | goto fail; |
---|
2627 | n/a | if (PyBuffer_ToContiguous(((PyBytesObject *)new)->ob_sval, |
---|
2628 | n/a | &view, view.len, 'C') < 0) |
---|
2629 | n/a | goto fail; |
---|
2630 | n/a | PyBuffer_Release(&view); |
---|
2631 | n/a | return new; |
---|
2632 | n/a | |
---|
2633 | n/a | fail: |
---|
2634 | n/a | Py_XDECREF(new); |
---|
2635 | n/a | PyBuffer_Release(&view); |
---|
2636 | n/a | return NULL; |
---|
2637 | n/a | } |
---|
2638 | n/a | |
---|
2639 | n/a | #define _PyBytes_FROM_LIST_BODY(x, GET_ITEM) \ |
---|
2640 | n/a | do { \ |
---|
2641 | n/a | PyObject *bytes; \ |
---|
2642 | n/a | Py_ssize_t i; \ |
---|
2643 | n/a | Py_ssize_t value; \ |
---|
2644 | n/a | char *str; \ |
---|
2645 | n/a | PyObject *item; \ |
---|
2646 | n/a | \ |
---|
2647 | n/a | bytes = PyBytes_FromStringAndSize(NULL, Py_SIZE(x)); \ |
---|
2648 | n/a | if (bytes == NULL) \ |
---|
2649 | n/a | return NULL; \ |
---|
2650 | n/a | str = ((PyBytesObject *)bytes)->ob_sval; \ |
---|
2651 | n/a | \ |
---|
2652 | n/a | for (i = 0; i < Py_SIZE(x); i++) { \ |
---|
2653 | n/a | item = GET_ITEM((x), i); \ |
---|
2654 | n/a | value = PyNumber_AsSsize_t(item, NULL); \ |
---|
2655 | n/a | if (value == -1 && PyErr_Occurred()) \ |
---|
2656 | n/a | goto error; \ |
---|
2657 | n/a | \ |
---|
2658 | n/a | if (value < 0 || value >= 256) { \ |
---|
2659 | n/a | PyErr_SetString(PyExc_ValueError, \ |
---|
2660 | n/a | "bytes must be in range(0, 256)"); \ |
---|
2661 | n/a | goto error; \ |
---|
2662 | n/a | } \ |
---|
2663 | n/a | *str++ = (char) value; \ |
---|
2664 | n/a | } \ |
---|
2665 | n/a | return bytes; \ |
---|
2666 | n/a | \ |
---|
2667 | n/a | error: \ |
---|
2668 | n/a | Py_DECREF(bytes); \ |
---|
2669 | n/a | return NULL; \ |
---|
2670 | n/a | } while (0) |
---|
2671 | n/a | |
---|
2672 | n/a | static PyObject* |
---|
2673 | n/a | _PyBytes_FromList(PyObject *x) |
---|
2674 | n/a | { |
---|
2675 | n/a | _PyBytes_FROM_LIST_BODY(x, PyList_GET_ITEM); |
---|
2676 | n/a | } |
---|
2677 | n/a | |
---|
2678 | n/a | static PyObject* |
---|
2679 | n/a | _PyBytes_FromTuple(PyObject *x) |
---|
2680 | n/a | { |
---|
2681 | n/a | _PyBytes_FROM_LIST_BODY(x, PyTuple_GET_ITEM); |
---|
2682 | n/a | } |
---|
2683 | n/a | |
---|
2684 | n/a | static PyObject * |
---|
2685 | n/a | _PyBytes_FromIterator(PyObject *it, PyObject *x) |
---|
2686 | n/a | { |
---|
2687 | n/a | char *str; |
---|
2688 | n/a | Py_ssize_t i, size; |
---|
2689 | n/a | _PyBytesWriter writer; |
---|
2690 | n/a | |
---|
2691 | n/a | /* For iterator version, create a string object and resize as needed */ |
---|
2692 | n/a | size = PyObject_LengthHint(x, 64); |
---|
2693 | n/a | if (size == -1 && PyErr_Occurred()) |
---|
2694 | n/a | return NULL; |
---|
2695 | n/a | |
---|
2696 | n/a | _PyBytesWriter_Init(&writer); |
---|
2697 | n/a | str = _PyBytesWriter_Alloc(&writer, size); |
---|
2698 | n/a | if (str == NULL) |
---|
2699 | n/a | return NULL; |
---|
2700 | n/a | writer.overallocate = 1; |
---|
2701 | n/a | size = writer.allocated; |
---|
2702 | n/a | |
---|
2703 | n/a | /* Run the iterator to exhaustion */ |
---|
2704 | n/a | for (i = 0; ; i++) { |
---|
2705 | n/a | PyObject *item; |
---|
2706 | n/a | Py_ssize_t value; |
---|
2707 | n/a | |
---|
2708 | n/a | /* Get the next item */ |
---|
2709 | n/a | item = PyIter_Next(it); |
---|
2710 | n/a | if (item == NULL) { |
---|
2711 | n/a | if (PyErr_Occurred()) |
---|
2712 | n/a | goto error; |
---|
2713 | n/a | break; |
---|
2714 | n/a | } |
---|
2715 | n/a | |
---|
2716 | n/a | /* Interpret it as an int (__index__) */ |
---|
2717 | n/a | value = PyNumber_AsSsize_t(item, NULL); |
---|
2718 | n/a | Py_DECREF(item); |
---|
2719 | n/a | if (value == -1 && PyErr_Occurred()) |
---|
2720 | n/a | goto error; |
---|
2721 | n/a | |
---|
2722 | n/a | /* Range check */ |
---|
2723 | n/a | if (value < 0 || value >= 256) { |
---|
2724 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
2725 | n/a | "bytes must be in range(0, 256)"); |
---|
2726 | n/a | goto error; |
---|
2727 | n/a | } |
---|
2728 | n/a | |
---|
2729 | n/a | /* Append the byte */ |
---|
2730 | n/a | if (i >= size) { |
---|
2731 | n/a | str = _PyBytesWriter_Resize(&writer, str, size+1); |
---|
2732 | n/a | if (str == NULL) |
---|
2733 | n/a | return NULL; |
---|
2734 | n/a | size = writer.allocated; |
---|
2735 | n/a | } |
---|
2736 | n/a | *str++ = (char) value; |
---|
2737 | n/a | } |
---|
2738 | n/a | |
---|
2739 | n/a | return _PyBytesWriter_Finish(&writer, str); |
---|
2740 | n/a | |
---|
2741 | n/a | error: |
---|
2742 | n/a | _PyBytesWriter_Dealloc(&writer); |
---|
2743 | n/a | return NULL; |
---|
2744 | n/a | } |
---|
2745 | n/a | |
---|
2746 | n/a | PyObject * |
---|
2747 | n/a | PyBytes_FromObject(PyObject *x) |
---|
2748 | n/a | { |
---|
2749 | n/a | PyObject *it, *result; |
---|
2750 | n/a | |
---|
2751 | n/a | if (x == NULL) { |
---|
2752 | n/a | PyErr_BadInternalCall(); |
---|
2753 | n/a | return NULL; |
---|
2754 | n/a | } |
---|
2755 | n/a | |
---|
2756 | n/a | if (PyBytes_CheckExact(x)) { |
---|
2757 | n/a | Py_INCREF(x); |
---|
2758 | n/a | return x; |
---|
2759 | n/a | } |
---|
2760 | n/a | |
---|
2761 | n/a | /* Use the modern buffer interface */ |
---|
2762 | n/a | if (PyObject_CheckBuffer(x)) |
---|
2763 | n/a | return _PyBytes_FromBuffer(x); |
---|
2764 | n/a | |
---|
2765 | n/a | if (PyList_CheckExact(x)) |
---|
2766 | n/a | return _PyBytes_FromList(x); |
---|
2767 | n/a | |
---|
2768 | n/a | if (PyTuple_CheckExact(x)) |
---|
2769 | n/a | return _PyBytes_FromTuple(x); |
---|
2770 | n/a | |
---|
2771 | n/a | if (!PyUnicode_Check(x)) { |
---|
2772 | n/a | it = PyObject_GetIter(x); |
---|
2773 | n/a | if (it != NULL) { |
---|
2774 | n/a | result = _PyBytes_FromIterator(it, x); |
---|
2775 | n/a | Py_DECREF(it); |
---|
2776 | n/a | return result; |
---|
2777 | n/a | } |
---|
2778 | n/a | } |
---|
2779 | n/a | |
---|
2780 | n/a | PyErr_Format(PyExc_TypeError, |
---|
2781 | n/a | "cannot convert '%.200s' object to bytes", |
---|
2782 | n/a | x->ob_type->tp_name); |
---|
2783 | n/a | return NULL; |
---|
2784 | n/a | } |
---|
2785 | n/a | |
---|
2786 | n/a | static PyObject * |
---|
2787 | n/a | bytes_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
2788 | n/a | { |
---|
2789 | n/a | PyObject *tmp, *pnew; |
---|
2790 | n/a | Py_ssize_t n; |
---|
2791 | n/a | |
---|
2792 | n/a | assert(PyType_IsSubtype(type, &PyBytes_Type)); |
---|
2793 | n/a | tmp = bytes_new(&PyBytes_Type, args, kwds); |
---|
2794 | n/a | if (tmp == NULL) |
---|
2795 | n/a | return NULL; |
---|
2796 | n/a | assert(PyBytes_Check(tmp)); |
---|
2797 | n/a | n = PyBytes_GET_SIZE(tmp); |
---|
2798 | n/a | pnew = type->tp_alloc(type, n); |
---|
2799 | n/a | if (pnew != NULL) { |
---|
2800 | n/a | memcpy(PyBytes_AS_STRING(pnew), |
---|
2801 | n/a | PyBytes_AS_STRING(tmp), n+1); |
---|
2802 | n/a | ((PyBytesObject *)pnew)->ob_shash = |
---|
2803 | n/a | ((PyBytesObject *)tmp)->ob_shash; |
---|
2804 | n/a | } |
---|
2805 | n/a | Py_DECREF(tmp); |
---|
2806 | n/a | return pnew; |
---|
2807 | n/a | } |
---|
2808 | n/a | |
---|
2809 | n/a | PyDoc_STRVAR(bytes_doc, |
---|
2810 | n/a | "bytes(iterable_of_ints) -> bytes\n\ |
---|
2811 | n/a | bytes(string, encoding[, errors]) -> bytes\n\ |
---|
2812 | n/a | bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer\n\ |
---|
2813 | n/a | bytes(int) -> bytes object of size given by the parameter initialized with null bytes\n\ |
---|
2814 | n/a | bytes() -> empty bytes object\n\ |
---|
2815 | n/a | \n\ |
---|
2816 | n/a | Construct an immutable array of bytes from:\n\ |
---|
2817 | n/a | - an iterable yielding integers in range(256)\n\ |
---|
2818 | n/a | - a text string encoded using the specified encoding\n\ |
---|
2819 | n/a | - any object implementing the buffer API.\n\ |
---|
2820 | n/a | - an integer"); |
---|
2821 | n/a | |
---|
2822 | n/a | static PyObject *bytes_iter(PyObject *seq); |
---|
2823 | n/a | |
---|
2824 | n/a | PyTypeObject PyBytes_Type = { |
---|
2825 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
2826 | n/a | "bytes", |
---|
2827 | n/a | PyBytesObject_SIZE, |
---|
2828 | n/a | sizeof(char), |
---|
2829 | n/a | bytes_dealloc, /* tp_dealloc */ |
---|
2830 | n/a | 0, /* tp_print */ |
---|
2831 | n/a | 0, /* tp_getattr */ |
---|
2832 | n/a | 0, /* tp_setattr */ |
---|
2833 | n/a | 0, /* tp_reserved */ |
---|
2834 | n/a | (reprfunc)bytes_repr, /* tp_repr */ |
---|
2835 | n/a | &bytes_as_number, /* tp_as_number */ |
---|
2836 | n/a | &bytes_as_sequence, /* tp_as_sequence */ |
---|
2837 | n/a | &bytes_as_mapping, /* tp_as_mapping */ |
---|
2838 | n/a | (hashfunc)bytes_hash, /* tp_hash */ |
---|
2839 | n/a | 0, /* tp_call */ |
---|
2840 | n/a | bytes_str, /* tp_str */ |
---|
2841 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
2842 | n/a | 0, /* tp_setattro */ |
---|
2843 | n/a | &bytes_as_buffer, /* tp_as_buffer */ |
---|
2844 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
---|
2845 | n/a | Py_TPFLAGS_BYTES_SUBCLASS, /* tp_flags */ |
---|
2846 | n/a | bytes_doc, /* tp_doc */ |
---|
2847 | n/a | 0, /* tp_traverse */ |
---|
2848 | n/a | 0, /* tp_clear */ |
---|
2849 | n/a | (richcmpfunc)bytes_richcompare, /* tp_richcompare */ |
---|
2850 | n/a | 0, /* tp_weaklistoffset */ |
---|
2851 | n/a | bytes_iter, /* tp_iter */ |
---|
2852 | n/a | 0, /* tp_iternext */ |
---|
2853 | n/a | bytes_methods, /* tp_methods */ |
---|
2854 | n/a | 0, /* tp_members */ |
---|
2855 | n/a | 0, /* tp_getset */ |
---|
2856 | n/a | &PyBaseObject_Type, /* tp_base */ |
---|
2857 | n/a | 0, /* tp_dict */ |
---|
2858 | n/a | 0, /* tp_descr_get */ |
---|
2859 | n/a | 0, /* tp_descr_set */ |
---|
2860 | n/a | 0, /* tp_dictoffset */ |
---|
2861 | n/a | 0, /* tp_init */ |
---|
2862 | n/a | 0, /* tp_alloc */ |
---|
2863 | n/a | bytes_new, /* tp_new */ |
---|
2864 | n/a | PyObject_Del, /* tp_free */ |
---|
2865 | n/a | }; |
---|
2866 | n/a | |
---|
2867 | n/a | void |
---|
2868 | n/a | PyBytes_Concat(PyObject **pv, PyObject *w) |
---|
2869 | n/a | { |
---|
2870 | n/a | assert(pv != NULL); |
---|
2871 | n/a | if (*pv == NULL) |
---|
2872 | n/a | return; |
---|
2873 | n/a | if (w == NULL) { |
---|
2874 | n/a | Py_CLEAR(*pv); |
---|
2875 | n/a | return; |
---|
2876 | n/a | } |
---|
2877 | n/a | |
---|
2878 | n/a | if (Py_REFCNT(*pv) == 1 && PyBytes_CheckExact(*pv)) { |
---|
2879 | n/a | /* Only one reference, so we can resize in place */ |
---|
2880 | n/a | Py_ssize_t oldsize; |
---|
2881 | n/a | Py_buffer wb; |
---|
2882 | n/a | |
---|
2883 | n/a | wb.len = -1; |
---|
2884 | n/a | if (PyObject_GetBuffer(w, &wb, PyBUF_SIMPLE) != 0) { |
---|
2885 | n/a | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
---|
2886 | n/a | Py_TYPE(w)->tp_name, Py_TYPE(*pv)->tp_name); |
---|
2887 | n/a | Py_CLEAR(*pv); |
---|
2888 | n/a | return; |
---|
2889 | n/a | } |
---|
2890 | n/a | |
---|
2891 | n/a | oldsize = PyBytes_GET_SIZE(*pv); |
---|
2892 | n/a | if (oldsize > PY_SSIZE_T_MAX - wb.len) { |
---|
2893 | n/a | PyErr_NoMemory(); |
---|
2894 | n/a | goto error; |
---|
2895 | n/a | } |
---|
2896 | n/a | if (_PyBytes_Resize(pv, oldsize + wb.len) < 0) |
---|
2897 | n/a | goto error; |
---|
2898 | n/a | |
---|
2899 | n/a | memcpy(PyBytes_AS_STRING(*pv) + oldsize, wb.buf, wb.len); |
---|
2900 | n/a | PyBuffer_Release(&wb); |
---|
2901 | n/a | return; |
---|
2902 | n/a | |
---|
2903 | n/a | error: |
---|
2904 | n/a | PyBuffer_Release(&wb); |
---|
2905 | n/a | Py_CLEAR(*pv); |
---|
2906 | n/a | return; |
---|
2907 | n/a | } |
---|
2908 | n/a | |
---|
2909 | n/a | else { |
---|
2910 | n/a | /* Multiple references, need to create new object */ |
---|
2911 | n/a | PyObject *v; |
---|
2912 | n/a | v = bytes_concat(*pv, w); |
---|
2913 | n/a | Py_SETREF(*pv, v); |
---|
2914 | n/a | } |
---|
2915 | n/a | } |
---|
2916 | n/a | |
---|
2917 | n/a | void |
---|
2918 | n/a | PyBytes_ConcatAndDel(PyObject **pv, PyObject *w) |
---|
2919 | n/a | { |
---|
2920 | n/a | PyBytes_Concat(pv, w); |
---|
2921 | n/a | Py_XDECREF(w); |
---|
2922 | n/a | } |
---|
2923 | n/a | |
---|
2924 | n/a | |
---|
2925 | n/a | /* The following function breaks the notion that bytes are immutable: |
---|
2926 | n/a | it changes the size of a bytes object. We get away with this only if there |
---|
2927 | n/a | is only one module referencing the object. You can also think of it |
---|
2928 | n/a | as creating a new bytes object and destroying the old one, only |
---|
2929 | n/a | more efficiently. In any case, don't use this if the bytes object may |
---|
2930 | n/a | already be known to some other part of the code... |
---|
2931 | n/a | Note that if there's not enough memory to resize the bytes object, the |
---|
2932 | n/a | original bytes object at *pv is deallocated, *pv is set to NULL, an "out of |
---|
2933 | n/a | memory" exception is set, and -1 is returned. Else (on success) 0 is |
---|
2934 | n/a | returned, and the value in *pv may or may not be the same as on input. |
---|
2935 | n/a | As always, an extra byte is allocated for a trailing \0 byte (newsize |
---|
2936 | n/a | does *not* include that), and a trailing \0 byte is stored. |
---|
2937 | n/a | */ |
---|
2938 | n/a | |
---|
2939 | n/a | int |
---|
2940 | n/a | _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize) |
---|
2941 | n/a | { |
---|
2942 | n/a | PyObject *v; |
---|
2943 | n/a | PyBytesObject *sv; |
---|
2944 | n/a | v = *pv; |
---|
2945 | n/a | if (!PyBytes_Check(v) || newsize < 0) { |
---|
2946 | n/a | goto error; |
---|
2947 | n/a | } |
---|
2948 | n/a | if (Py_SIZE(v) == newsize) { |
---|
2949 | n/a | /* return early if newsize equals to v->ob_size */ |
---|
2950 | n/a | return 0; |
---|
2951 | n/a | } |
---|
2952 | n/a | if (Py_REFCNT(v) != 1) { |
---|
2953 | n/a | goto error; |
---|
2954 | n/a | } |
---|
2955 | n/a | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
---|
2956 | n/a | _Py_DEC_REFTOTAL; |
---|
2957 | n/a | _Py_ForgetReference(v); |
---|
2958 | n/a | *pv = (PyObject *) |
---|
2959 | n/a | PyObject_REALLOC(v, PyBytesObject_SIZE + newsize); |
---|
2960 | n/a | if (*pv == NULL) { |
---|
2961 | n/a | PyObject_Del(v); |
---|
2962 | n/a | PyErr_NoMemory(); |
---|
2963 | n/a | return -1; |
---|
2964 | n/a | } |
---|
2965 | n/a | _Py_NewReference(*pv); |
---|
2966 | n/a | sv = (PyBytesObject *) *pv; |
---|
2967 | n/a | Py_SIZE(sv) = newsize; |
---|
2968 | n/a | sv->ob_sval[newsize] = '\0'; |
---|
2969 | n/a | sv->ob_shash = -1; /* invalidate cached hash value */ |
---|
2970 | n/a | return 0; |
---|
2971 | n/a | error: |
---|
2972 | n/a | *pv = 0; |
---|
2973 | n/a | Py_DECREF(v); |
---|
2974 | n/a | PyErr_BadInternalCall(); |
---|
2975 | n/a | return -1; |
---|
2976 | n/a | } |
---|
2977 | n/a | |
---|
2978 | n/a | void |
---|
2979 | n/a | PyBytes_Fini(void) |
---|
2980 | n/a | { |
---|
2981 | n/a | int i; |
---|
2982 | n/a | for (i = 0; i < UCHAR_MAX + 1; i++) |
---|
2983 | n/a | Py_CLEAR(characters[i]); |
---|
2984 | n/a | Py_CLEAR(nullstring); |
---|
2985 | n/a | } |
---|
2986 | n/a | |
---|
2987 | n/a | /*********************** Bytes Iterator ****************************/ |
---|
2988 | n/a | |
---|
2989 | n/a | typedef struct { |
---|
2990 | n/a | PyObject_HEAD |
---|
2991 | n/a | Py_ssize_t it_index; |
---|
2992 | n/a | PyBytesObject *it_seq; /* Set to NULL when iterator is exhausted */ |
---|
2993 | n/a | } striterobject; |
---|
2994 | n/a | |
---|
2995 | n/a | static void |
---|
2996 | n/a | striter_dealloc(striterobject *it) |
---|
2997 | n/a | { |
---|
2998 | n/a | _PyObject_GC_UNTRACK(it); |
---|
2999 | n/a | Py_XDECREF(it->it_seq); |
---|
3000 | n/a | PyObject_GC_Del(it); |
---|
3001 | n/a | } |
---|
3002 | n/a | |
---|
3003 | n/a | static int |
---|
3004 | n/a | striter_traverse(striterobject *it, visitproc visit, void *arg) |
---|
3005 | n/a | { |
---|
3006 | n/a | Py_VISIT(it->it_seq); |
---|
3007 | n/a | return 0; |
---|
3008 | n/a | } |
---|
3009 | n/a | |
---|
3010 | n/a | static PyObject * |
---|
3011 | n/a | striter_next(striterobject *it) |
---|
3012 | n/a | { |
---|
3013 | n/a | PyBytesObject *seq; |
---|
3014 | n/a | PyObject *item; |
---|
3015 | n/a | |
---|
3016 | n/a | assert(it != NULL); |
---|
3017 | n/a | seq = it->it_seq; |
---|
3018 | n/a | if (seq == NULL) |
---|
3019 | n/a | return NULL; |
---|
3020 | n/a | assert(PyBytes_Check(seq)); |
---|
3021 | n/a | |
---|
3022 | n/a | if (it->it_index < PyBytes_GET_SIZE(seq)) { |
---|
3023 | n/a | item = PyLong_FromLong( |
---|
3024 | n/a | (unsigned char)seq->ob_sval[it->it_index]); |
---|
3025 | n/a | if (item != NULL) |
---|
3026 | n/a | ++it->it_index; |
---|
3027 | n/a | return item; |
---|
3028 | n/a | } |
---|
3029 | n/a | |
---|
3030 | n/a | it->it_seq = NULL; |
---|
3031 | n/a | Py_DECREF(seq); |
---|
3032 | n/a | return NULL; |
---|
3033 | n/a | } |
---|
3034 | n/a | |
---|
3035 | n/a | static PyObject * |
---|
3036 | n/a | striter_len(striterobject *it) |
---|
3037 | n/a | { |
---|
3038 | n/a | Py_ssize_t len = 0; |
---|
3039 | n/a | if (it->it_seq) |
---|
3040 | n/a | len = PyBytes_GET_SIZE(it->it_seq) - it->it_index; |
---|
3041 | n/a | return PyLong_FromSsize_t(len); |
---|
3042 | n/a | } |
---|
3043 | n/a | |
---|
3044 | n/a | PyDoc_STRVAR(length_hint_doc, |
---|
3045 | n/a | "Private method returning an estimate of len(list(it))."); |
---|
3046 | n/a | |
---|
3047 | n/a | static PyObject * |
---|
3048 | n/a | striter_reduce(striterobject *it) |
---|
3049 | n/a | { |
---|
3050 | n/a | if (it->it_seq != NULL) { |
---|
3051 | n/a | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
---|
3052 | n/a | it->it_seq, it->it_index); |
---|
3053 | n/a | } else { |
---|
3054 | n/a | return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter")); |
---|
3055 | n/a | } |
---|
3056 | n/a | } |
---|
3057 | n/a | |
---|
3058 | n/a | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
---|
3059 | n/a | |
---|
3060 | n/a | static PyObject * |
---|
3061 | n/a | striter_setstate(striterobject *it, PyObject *state) |
---|
3062 | n/a | { |
---|
3063 | n/a | Py_ssize_t index = PyLong_AsSsize_t(state); |
---|
3064 | n/a | if (index == -1 && PyErr_Occurred()) |
---|
3065 | n/a | return NULL; |
---|
3066 | n/a | if (it->it_seq != NULL) { |
---|
3067 | n/a | if (index < 0) |
---|
3068 | n/a | index = 0; |
---|
3069 | n/a | else if (index > PyBytes_GET_SIZE(it->it_seq)) |
---|
3070 | n/a | index = PyBytes_GET_SIZE(it->it_seq); /* iterator exhausted */ |
---|
3071 | n/a | it->it_index = index; |
---|
3072 | n/a | } |
---|
3073 | n/a | Py_RETURN_NONE; |
---|
3074 | n/a | } |
---|
3075 | n/a | |
---|
3076 | n/a | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
---|
3077 | n/a | |
---|
3078 | n/a | static PyMethodDef striter_methods[] = { |
---|
3079 | n/a | {"__length_hint__", (PyCFunction)striter_len, METH_NOARGS, |
---|
3080 | n/a | length_hint_doc}, |
---|
3081 | n/a | {"__reduce__", (PyCFunction)striter_reduce, METH_NOARGS, |
---|
3082 | n/a | reduce_doc}, |
---|
3083 | n/a | {"__setstate__", (PyCFunction)striter_setstate, METH_O, |
---|
3084 | n/a | setstate_doc}, |
---|
3085 | n/a | {NULL, NULL} /* sentinel */ |
---|
3086 | n/a | }; |
---|
3087 | n/a | |
---|
3088 | n/a | PyTypeObject PyBytesIter_Type = { |
---|
3089 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
3090 | n/a | "bytes_iterator", /* tp_name */ |
---|
3091 | n/a | sizeof(striterobject), /* tp_basicsize */ |
---|
3092 | n/a | 0, /* tp_itemsize */ |
---|
3093 | n/a | /* methods */ |
---|
3094 | n/a | (destructor)striter_dealloc, /* tp_dealloc */ |
---|
3095 | n/a | 0, /* tp_print */ |
---|
3096 | n/a | 0, /* tp_getattr */ |
---|
3097 | n/a | 0, /* tp_setattr */ |
---|
3098 | n/a | 0, /* tp_reserved */ |
---|
3099 | n/a | 0, /* tp_repr */ |
---|
3100 | n/a | 0, /* tp_as_number */ |
---|
3101 | n/a | 0, /* tp_as_sequence */ |
---|
3102 | n/a | 0, /* tp_as_mapping */ |
---|
3103 | n/a | 0, /* tp_hash */ |
---|
3104 | n/a | 0, /* tp_call */ |
---|
3105 | n/a | 0, /* tp_str */ |
---|
3106 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
3107 | n/a | 0, /* tp_setattro */ |
---|
3108 | n/a | 0, /* tp_as_buffer */ |
---|
3109 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
---|
3110 | n/a | 0, /* tp_doc */ |
---|
3111 | n/a | (traverseproc)striter_traverse, /* tp_traverse */ |
---|
3112 | n/a | 0, /* tp_clear */ |
---|
3113 | n/a | 0, /* tp_richcompare */ |
---|
3114 | n/a | 0, /* tp_weaklistoffset */ |
---|
3115 | n/a | PyObject_SelfIter, /* tp_iter */ |
---|
3116 | n/a | (iternextfunc)striter_next, /* tp_iternext */ |
---|
3117 | n/a | striter_methods, /* tp_methods */ |
---|
3118 | n/a | 0, |
---|
3119 | n/a | }; |
---|
3120 | n/a | |
---|
3121 | n/a | static PyObject * |
---|
3122 | n/a | bytes_iter(PyObject *seq) |
---|
3123 | n/a | { |
---|
3124 | n/a | striterobject *it; |
---|
3125 | n/a | |
---|
3126 | n/a | if (!PyBytes_Check(seq)) { |
---|
3127 | n/a | PyErr_BadInternalCall(); |
---|
3128 | n/a | return NULL; |
---|
3129 | n/a | } |
---|
3130 | n/a | it = PyObject_GC_New(striterobject, &PyBytesIter_Type); |
---|
3131 | n/a | if (it == NULL) |
---|
3132 | n/a | return NULL; |
---|
3133 | n/a | it->it_index = 0; |
---|
3134 | n/a | Py_INCREF(seq); |
---|
3135 | n/a | it->it_seq = (PyBytesObject *)seq; |
---|
3136 | n/a | _PyObject_GC_TRACK(it); |
---|
3137 | n/a | return (PyObject *)it; |
---|
3138 | n/a | } |
---|
3139 | n/a | |
---|
3140 | n/a | |
---|
3141 | n/a | /* _PyBytesWriter API */ |
---|
3142 | n/a | |
---|
3143 | n/a | #ifdef MS_WINDOWS |
---|
3144 | n/a | /* On Windows, overallocate by 50% is the best factor */ |
---|
3145 | n/a | # define OVERALLOCATE_FACTOR 2 |
---|
3146 | n/a | #else |
---|
3147 | n/a | /* On Linux, overallocate by 25% is the best factor */ |
---|
3148 | n/a | # define OVERALLOCATE_FACTOR 4 |
---|
3149 | n/a | #endif |
---|
3150 | n/a | |
---|
3151 | n/a | void |
---|
3152 | n/a | _PyBytesWriter_Init(_PyBytesWriter *writer) |
---|
3153 | n/a | { |
---|
3154 | n/a | /* Set all attributes before small_buffer to 0 */ |
---|
3155 | n/a | memset(writer, 0, offsetof(_PyBytesWriter, small_buffer)); |
---|
3156 | n/a | #ifdef Py_DEBUG |
---|
3157 | n/a | memset(writer->small_buffer, 0xCB, sizeof(writer->small_buffer)); |
---|
3158 | n/a | #endif |
---|
3159 | n/a | } |
---|
3160 | n/a | |
---|
3161 | n/a | void |
---|
3162 | n/a | _PyBytesWriter_Dealloc(_PyBytesWriter *writer) |
---|
3163 | n/a | { |
---|
3164 | n/a | Py_CLEAR(writer->buffer); |
---|
3165 | n/a | } |
---|
3166 | n/a | |
---|
3167 | n/a | Py_LOCAL_INLINE(char*) |
---|
3168 | n/a | _PyBytesWriter_AsString(_PyBytesWriter *writer) |
---|
3169 | n/a | { |
---|
3170 | n/a | if (writer->use_small_buffer) { |
---|
3171 | n/a | assert(writer->buffer == NULL); |
---|
3172 | n/a | return writer->small_buffer; |
---|
3173 | n/a | } |
---|
3174 | n/a | else if (writer->use_bytearray) { |
---|
3175 | n/a | assert(writer->buffer != NULL); |
---|
3176 | n/a | return PyByteArray_AS_STRING(writer->buffer); |
---|
3177 | n/a | } |
---|
3178 | n/a | else { |
---|
3179 | n/a | assert(writer->buffer != NULL); |
---|
3180 | n/a | return PyBytes_AS_STRING(writer->buffer); |
---|
3181 | n/a | } |
---|
3182 | n/a | } |
---|
3183 | n/a | |
---|
3184 | n/a | Py_LOCAL_INLINE(Py_ssize_t) |
---|
3185 | n/a | _PyBytesWriter_GetSize(_PyBytesWriter *writer, char *str) |
---|
3186 | n/a | { |
---|
3187 | n/a | char *start = _PyBytesWriter_AsString(writer); |
---|
3188 | n/a | assert(str != NULL); |
---|
3189 | n/a | assert(str >= start); |
---|
3190 | n/a | assert(str - start <= writer->allocated); |
---|
3191 | n/a | return str - start; |
---|
3192 | n/a | } |
---|
3193 | n/a | |
---|
3194 | n/a | Py_LOCAL_INLINE(void) |
---|
3195 | n/a | _PyBytesWriter_CheckConsistency(_PyBytesWriter *writer, char *str) |
---|
3196 | n/a | { |
---|
3197 | n/a | #ifdef Py_DEBUG |
---|
3198 | n/a | char *start, *end; |
---|
3199 | n/a | |
---|
3200 | n/a | if (writer->use_small_buffer) { |
---|
3201 | n/a | assert(writer->buffer == NULL); |
---|
3202 | n/a | } |
---|
3203 | n/a | else { |
---|
3204 | n/a | assert(writer->buffer != NULL); |
---|
3205 | n/a | if (writer->use_bytearray) |
---|
3206 | n/a | assert(PyByteArray_CheckExact(writer->buffer)); |
---|
3207 | n/a | else |
---|
3208 | n/a | assert(PyBytes_CheckExact(writer->buffer)); |
---|
3209 | n/a | assert(Py_REFCNT(writer->buffer) == 1); |
---|
3210 | n/a | } |
---|
3211 | n/a | |
---|
3212 | n/a | if (writer->use_bytearray) { |
---|
3213 | n/a | /* bytearray has its own overallocation algorithm, |
---|
3214 | n/a | writer overallocation must be disabled */ |
---|
3215 | n/a | assert(!writer->overallocate); |
---|
3216 | n/a | } |
---|
3217 | n/a | |
---|
3218 | n/a | assert(0 <= writer->allocated); |
---|
3219 | n/a | assert(0 <= writer->min_size && writer->min_size <= writer->allocated); |
---|
3220 | n/a | /* the last byte must always be null */ |
---|
3221 | n/a | start = _PyBytesWriter_AsString(writer); |
---|
3222 | n/a | assert(start[writer->allocated] == 0); |
---|
3223 | n/a | |
---|
3224 | n/a | end = start + writer->allocated; |
---|
3225 | n/a | assert(str != NULL); |
---|
3226 | n/a | assert(start <= str && str <= end); |
---|
3227 | n/a | #endif |
---|
3228 | n/a | } |
---|
3229 | n/a | |
---|
3230 | n/a | void* |
---|
3231 | n/a | _PyBytesWriter_Resize(_PyBytesWriter *writer, void *str, Py_ssize_t size) |
---|
3232 | n/a | { |
---|
3233 | n/a | Py_ssize_t allocated, pos; |
---|
3234 | n/a | |
---|
3235 | n/a | _PyBytesWriter_CheckConsistency(writer, str); |
---|
3236 | n/a | assert(writer->allocated < size); |
---|
3237 | n/a | |
---|
3238 | n/a | allocated = size; |
---|
3239 | n/a | if (writer->overallocate |
---|
3240 | n/a | && allocated <= (PY_SSIZE_T_MAX - allocated / OVERALLOCATE_FACTOR)) { |
---|
3241 | n/a | /* overallocate to limit the number of realloc() */ |
---|
3242 | n/a | allocated += allocated / OVERALLOCATE_FACTOR; |
---|
3243 | n/a | } |
---|
3244 | n/a | |
---|
3245 | n/a | pos = _PyBytesWriter_GetSize(writer, str); |
---|
3246 | n/a | if (!writer->use_small_buffer) { |
---|
3247 | n/a | if (writer->use_bytearray) { |
---|
3248 | n/a | if (PyByteArray_Resize(writer->buffer, allocated)) |
---|
3249 | n/a | goto error; |
---|
3250 | n/a | /* writer->allocated can be smaller than writer->buffer->ob_alloc, |
---|
3251 | n/a | but we cannot use ob_alloc because bytes may need to be moved |
---|
3252 | n/a | to use the whole buffer. bytearray uses an internal optimization |
---|
3253 | n/a | to avoid moving or copying bytes when bytes are removed at the |
---|
3254 | n/a | beginning (ex: del bytearray[:1]). */ |
---|
3255 | n/a | } |
---|
3256 | n/a | else { |
---|
3257 | n/a | if (_PyBytes_Resize(&writer->buffer, allocated)) |
---|
3258 | n/a | goto error; |
---|
3259 | n/a | } |
---|
3260 | n/a | } |
---|
3261 | n/a | else { |
---|
3262 | n/a | /* convert from stack buffer to bytes object buffer */ |
---|
3263 | n/a | assert(writer->buffer == NULL); |
---|
3264 | n/a | |
---|
3265 | n/a | if (writer->use_bytearray) |
---|
3266 | n/a | writer->buffer = PyByteArray_FromStringAndSize(NULL, allocated); |
---|
3267 | n/a | else |
---|
3268 | n/a | writer->buffer = PyBytes_FromStringAndSize(NULL, allocated); |
---|
3269 | n/a | if (writer->buffer == NULL) |
---|
3270 | n/a | goto error; |
---|
3271 | n/a | |
---|
3272 | n/a | if (pos != 0) { |
---|
3273 | n/a | char *dest; |
---|
3274 | n/a | if (writer->use_bytearray) |
---|
3275 | n/a | dest = PyByteArray_AS_STRING(writer->buffer); |
---|
3276 | n/a | else |
---|
3277 | n/a | dest = PyBytes_AS_STRING(writer->buffer); |
---|
3278 | n/a | memcpy(dest, |
---|
3279 | n/a | writer->small_buffer, |
---|
3280 | n/a | pos); |
---|
3281 | n/a | } |
---|
3282 | n/a | |
---|
3283 | n/a | writer->use_small_buffer = 0; |
---|
3284 | n/a | #ifdef Py_DEBUG |
---|
3285 | n/a | memset(writer->small_buffer, 0xDB, sizeof(writer->small_buffer)); |
---|
3286 | n/a | #endif |
---|
3287 | n/a | } |
---|
3288 | n/a | writer->allocated = allocated; |
---|
3289 | n/a | |
---|
3290 | n/a | str = _PyBytesWriter_AsString(writer) + pos; |
---|
3291 | n/a | _PyBytesWriter_CheckConsistency(writer, str); |
---|
3292 | n/a | return str; |
---|
3293 | n/a | |
---|
3294 | n/a | error: |
---|
3295 | n/a | _PyBytesWriter_Dealloc(writer); |
---|
3296 | n/a | return NULL; |
---|
3297 | n/a | } |
---|
3298 | n/a | |
---|
3299 | n/a | void* |
---|
3300 | n/a | _PyBytesWriter_Prepare(_PyBytesWriter *writer, void *str, Py_ssize_t size) |
---|
3301 | n/a | { |
---|
3302 | n/a | Py_ssize_t new_min_size; |
---|
3303 | n/a | |
---|
3304 | n/a | _PyBytesWriter_CheckConsistency(writer, str); |
---|
3305 | n/a | assert(size >= 0); |
---|
3306 | n/a | |
---|
3307 | n/a | if (size == 0) { |
---|
3308 | n/a | /* nothing to do */ |
---|
3309 | n/a | return str; |
---|
3310 | n/a | } |
---|
3311 | n/a | |
---|
3312 | n/a | if (writer->min_size > PY_SSIZE_T_MAX - size) { |
---|
3313 | n/a | PyErr_NoMemory(); |
---|
3314 | n/a | _PyBytesWriter_Dealloc(writer); |
---|
3315 | n/a | return NULL; |
---|
3316 | n/a | } |
---|
3317 | n/a | new_min_size = writer->min_size + size; |
---|
3318 | n/a | |
---|
3319 | n/a | if (new_min_size > writer->allocated) |
---|
3320 | n/a | str = _PyBytesWriter_Resize(writer, str, new_min_size); |
---|
3321 | n/a | |
---|
3322 | n/a | writer->min_size = new_min_size; |
---|
3323 | n/a | return str; |
---|
3324 | n/a | } |
---|
3325 | n/a | |
---|
3326 | n/a | /* Allocate the buffer to write size bytes. |
---|
3327 | n/a | Return the pointer to the beginning of buffer data. |
---|
3328 | n/a | Raise an exception and return NULL on error. */ |
---|
3329 | n/a | void* |
---|
3330 | n/a | _PyBytesWriter_Alloc(_PyBytesWriter *writer, Py_ssize_t size) |
---|
3331 | n/a | { |
---|
3332 | n/a | /* ensure that _PyBytesWriter_Alloc() is only called once */ |
---|
3333 | n/a | assert(writer->min_size == 0 && writer->buffer == NULL); |
---|
3334 | n/a | assert(size >= 0); |
---|
3335 | n/a | |
---|
3336 | n/a | writer->use_small_buffer = 1; |
---|
3337 | n/a | #ifdef Py_DEBUG |
---|
3338 | n/a | writer->allocated = sizeof(writer->small_buffer) - 1; |
---|
3339 | n/a | /* In debug mode, don't use the full small buffer because it is less |
---|
3340 | n/a | efficient than bytes and bytearray objects to detect buffer underflow |
---|
3341 | n/a | and buffer overflow. Use 10 bytes of the small buffer to test also |
---|
3342 | n/a | code using the smaller buffer in debug mode. |
---|
3343 | n/a | |
---|
3344 | n/a | Don't modify the _PyBytesWriter structure (use a shorter small buffer) |
---|
3345 | n/a | in debug mode to also be able to detect stack overflow when running |
---|
3346 | n/a | tests in debug mode. The _PyBytesWriter is large (more than 512 bytes), |
---|
3347 | n/a | if Py_EnterRecursiveCall() is not used in deep C callback, we may hit a |
---|
3348 | n/a | stack overflow. */ |
---|
3349 | n/a | writer->allocated = Py_MIN(writer->allocated, 10); |
---|
3350 | n/a | /* _PyBytesWriter_CheckConsistency() requires the last byte to be 0, |
---|
3351 | n/a | to detect buffer overflow */ |
---|
3352 | n/a | writer->small_buffer[writer->allocated] = 0; |
---|
3353 | n/a | #else |
---|
3354 | n/a | writer->allocated = sizeof(writer->small_buffer); |
---|
3355 | n/a | #endif |
---|
3356 | n/a | return _PyBytesWriter_Prepare(writer, writer->small_buffer, size); |
---|
3357 | n/a | } |
---|
3358 | n/a | |
---|
3359 | n/a | PyObject * |
---|
3360 | n/a | _PyBytesWriter_Finish(_PyBytesWriter *writer, void *str) |
---|
3361 | n/a | { |
---|
3362 | n/a | Py_ssize_t size; |
---|
3363 | n/a | PyObject *result; |
---|
3364 | n/a | |
---|
3365 | n/a | _PyBytesWriter_CheckConsistency(writer, str); |
---|
3366 | n/a | |
---|
3367 | n/a | size = _PyBytesWriter_GetSize(writer, str); |
---|
3368 | n/a | if (size == 0 && !writer->use_bytearray) { |
---|
3369 | n/a | Py_CLEAR(writer->buffer); |
---|
3370 | n/a | /* Get the empty byte string singleton */ |
---|
3371 | n/a | result = PyBytes_FromStringAndSize(NULL, 0); |
---|
3372 | n/a | } |
---|
3373 | n/a | else if (writer->use_small_buffer) { |
---|
3374 | n/a | if (writer->use_bytearray) { |
---|
3375 | n/a | result = PyByteArray_FromStringAndSize(writer->small_buffer, size); |
---|
3376 | n/a | } |
---|
3377 | n/a | else { |
---|
3378 | n/a | result = PyBytes_FromStringAndSize(writer->small_buffer, size); |
---|
3379 | n/a | } |
---|
3380 | n/a | } |
---|
3381 | n/a | else { |
---|
3382 | n/a | result = writer->buffer; |
---|
3383 | n/a | writer->buffer = NULL; |
---|
3384 | n/a | |
---|
3385 | n/a | if (size != writer->allocated) { |
---|
3386 | n/a | if (writer->use_bytearray) { |
---|
3387 | n/a | if (PyByteArray_Resize(result, size)) { |
---|
3388 | n/a | Py_DECREF(result); |
---|
3389 | n/a | return NULL; |
---|
3390 | n/a | } |
---|
3391 | n/a | } |
---|
3392 | n/a | else { |
---|
3393 | n/a | if (_PyBytes_Resize(&result, size)) { |
---|
3394 | n/a | assert(result == NULL); |
---|
3395 | n/a | return NULL; |
---|
3396 | n/a | } |
---|
3397 | n/a | } |
---|
3398 | n/a | } |
---|
3399 | n/a | } |
---|
3400 | n/a | return result; |
---|
3401 | n/a | } |
---|
3402 | n/a | |
---|
3403 | n/a | void* |
---|
3404 | n/a | _PyBytesWriter_WriteBytes(_PyBytesWriter *writer, void *ptr, |
---|
3405 | n/a | const void *bytes, Py_ssize_t size) |
---|
3406 | n/a | { |
---|
3407 | n/a | char *str = (char *)ptr; |
---|
3408 | n/a | |
---|
3409 | n/a | str = _PyBytesWriter_Prepare(writer, str, size); |
---|
3410 | n/a | if (str == NULL) |
---|
3411 | n/a | return NULL; |
---|
3412 | n/a | |
---|
3413 | n/a | memcpy(str, bytes, size); |
---|
3414 | n/a | str += size; |
---|
3415 | n/a | |
---|
3416 | n/a | return str; |
---|
3417 | n/a | } |
---|