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