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