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