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