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 | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 6 | #include "pycore_pymem.h" |
| 7 | #include "pycore_pystate.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 8 | |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 9 | #include "bytes_methods.h" |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 10 | #include "pystrhex.h" |
Mark Dickinson | fd24b32 | 2008-12-06 15:33:31 +0000 | [diff] [blame] | 11 | #include <stddef.h> |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 12 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 13 | /*[clinic input] |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 14 | class bytes "PyBytesObject *" "&PyBytes_Type" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 15 | [clinic start generated code]*/ |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 16 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=7a238f965d64892b]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 17 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 18 | #include "clinic/bytesobject.c.h" |
| 19 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 20 | #ifdef COUNT_ALLOCS |
Pablo Galindo | 49c75a8 | 2018-10-28 15:02:17 +0000 | [diff] [blame] | 21 | Py_ssize_t _Py_null_strings, _Py_one_strings; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 22 | #endif |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 23 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 24 | static PyBytesObject *characters[UCHAR_MAX + 1]; |
| 25 | static PyBytesObject *nullstring; |
| 26 | |
Mark Dickinson | fd24b32 | 2008-12-06 15:33:31 +0000 | [diff] [blame] | 27 | /* PyBytesObject_SIZE gives the basic size of a string; any memory allocation |
| 28 | for a string of length n should request PyBytesObject_SIZE + n bytes. |
| 29 | |
| 30 | Using PyBytesObject_SIZE instead of sizeof(PyBytesObject) saves |
| 31 | 3 bytes per string allocation on a typical system. |
| 32 | */ |
| 33 | #define PyBytesObject_SIZE (offsetof(PyBytesObject, ob_sval) + 1) |
| 34 | |
Victor Stinner | 2bf8993 | 2015-10-14 11:25:33 +0200 | [diff] [blame] | 35 | /* Forward declaration */ |
| 36 | Py_LOCAL_INLINE(Py_ssize_t) _PyBytesWriter_GetSize(_PyBytesWriter *writer, |
| 37 | char *str); |
| 38 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 39 | /* |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 40 | For PyBytes_FromString(), the parameter `str' points to a null-terminated |
| 41 | string containing exactly `size' bytes. |
| 42 | |
Martin Panter | a90a4a9 | 2016-05-30 04:04:50 +0000 | [diff] [blame] | 43 | For PyBytes_FromStringAndSize(), the parameter `str' is |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 44 | either NULL or else points to a string containing at least `size' bytes. |
| 45 | For PyBytes_FromStringAndSize(), the string in the `str' parameter does |
| 46 | not have to be null-terminated. (Therefore it is safe to construct a |
| 47 | substring by calling `PyBytes_FromStringAndSize(origstring, substrlen)'.) |
| 48 | If `str' is NULL then PyBytes_FromStringAndSize() will allocate `size+1' |
| 49 | bytes (setting the last byte to the null terminating character) and you can |
| 50 | fill in the data yourself. If `str' is non-NULL then the resulting |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 51 | PyBytes object must be treated as immutable and you must not fill in nor |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 52 | alter the data yourself, since the strings may be shared. |
| 53 | |
| 54 | The PyObject member `op->ob_size', which denotes the number of "extra |
| 55 | items" in a variable-size object, will contain the number of bytes |
Eli Bendersky | 1aef6b6 | 2011-03-24 22:32:56 +0200 | [diff] [blame] | 56 | allocated for string data, not counting the null terminating character. |
| 57 | It is therefore equal to the `size' parameter (for |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 58 | PyBytes_FromStringAndSize()) or the length of the string in the `str' |
| 59 | parameter (for PyBytes_FromString()). |
| 60 | */ |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 61 | static PyObject * |
| 62 | _PyBytes_FromSize(Py_ssize_t size, int use_calloc) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 63 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 64 | PyBytesObject *op; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 65 | assert(size >= 0); |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 66 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | if (size == 0 && (op = nullstring) != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 68 | #ifdef COUNT_ALLOCS |
Pablo Galindo | 49c75a8 | 2018-10-28 15:02:17 +0000 | [diff] [blame] | 69 | _Py_null_strings++; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 70 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | Py_INCREF(op); |
| 72 | return (PyObject *)op; |
| 73 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 74 | |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 75 | if ((size_t)size > (size_t)PY_SSIZE_T_MAX - PyBytesObject_SIZE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 76 | PyErr_SetString(PyExc_OverflowError, |
| 77 | "byte string is too large"); |
| 78 | return NULL; |
| 79 | } |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 80 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | /* Inline PyObject_NewVar */ |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 82 | if (use_calloc) |
| 83 | op = (PyBytesObject *)PyObject_Calloc(1, PyBytesObject_SIZE + size); |
| 84 | else |
| 85 | op = (PyBytesObject *)PyObject_Malloc(PyBytesObject_SIZE + size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 86 | if (op == NULL) |
| 87 | return PyErr_NoMemory(); |
Victor Stinner | b4435e2 | 2018-10-26 14:35:00 +0200 | [diff] [blame] | 88 | (void)PyObject_INIT_VAR((PyVarObject *)op, &PyBytes_Type, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 89 | op->ob_shash = -1; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 90 | if (!use_calloc) |
| 91 | op->ob_sval[size] = '\0'; |
| 92 | /* empty byte string singleton */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | if (size == 0) { |
| 94 | nullstring = op; |
| 95 | Py_INCREF(op); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 96 | } |
| 97 | return (PyObject *) op; |
| 98 | } |
| 99 | |
| 100 | PyObject * |
| 101 | PyBytes_FromStringAndSize(const char *str, Py_ssize_t size) |
| 102 | { |
| 103 | PyBytesObject *op; |
| 104 | if (size < 0) { |
| 105 | PyErr_SetString(PyExc_SystemError, |
| 106 | "Negative size passed to PyBytes_FromStringAndSize"); |
| 107 | return NULL; |
| 108 | } |
| 109 | if (size == 1 && str != NULL && |
| 110 | (op = characters[*str & UCHAR_MAX]) != NULL) |
| 111 | { |
| 112 | #ifdef COUNT_ALLOCS |
Pablo Galindo | 49c75a8 | 2018-10-28 15:02:17 +0000 | [diff] [blame] | 113 | _Py_one_strings++; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 114 | #endif |
| 115 | Py_INCREF(op); |
| 116 | return (PyObject *)op; |
| 117 | } |
| 118 | |
| 119 | op = (PyBytesObject *)_PyBytes_FromSize(size, 0); |
| 120 | if (op == NULL) |
| 121 | return NULL; |
| 122 | if (str == NULL) |
| 123 | return (PyObject *) op; |
| 124 | |
Christian Heimes | f051e43 | 2016-09-13 20:22:02 +0200 | [diff] [blame] | 125 | memcpy(op->ob_sval, str, size); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 126 | /* share short strings */ |
| 127 | if (size == 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | characters[*str & UCHAR_MAX] = op; |
| 129 | Py_INCREF(op); |
| 130 | } |
| 131 | return (PyObject *) op; |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 134 | PyObject * |
| 135 | PyBytes_FromString(const char *str) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 136 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 137 | size_t size; |
| 138 | PyBytesObject *op; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 140 | assert(str != NULL); |
| 141 | size = strlen(str); |
| 142 | if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) { |
| 143 | PyErr_SetString(PyExc_OverflowError, |
| 144 | "byte string is too long"); |
| 145 | return NULL; |
| 146 | } |
| 147 | if (size == 0 && (op = nullstring) != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 148 | #ifdef COUNT_ALLOCS |
Pablo Galindo | 49c75a8 | 2018-10-28 15:02:17 +0000 | [diff] [blame] | 149 | _Py_null_strings++; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 150 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | Py_INCREF(op); |
| 152 | return (PyObject *)op; |
| 153 | } |
| 154 | if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 155 | #ifdef COUNT_ALLOCS |
Pablo Galindo | 49c75a8 | 2018-10-28 15:02:17 +0000 | [diff] [blame] | 156 | _Py_one_strings++; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 157 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | Py_INCREF(op); |
| 159 | return (PyObject *)op; |
| 160 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | /* Inline PyObject_NewVar */ |
| 163 | op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size); |
| 164 | if (op == NULL) |
| 165 | return PyErr_NoMemory(); |
Victor Stinner | b4435e2 | 2018-10-26 14:35:00 +0200 | [diff] [blame] | 166 | (void)PyObject_INIT_VAR((PyVarObject *)op, &PyBytes_Type, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | op->ob_shash = -1; |
Christian Heimes | f051e43 | 2016-09-13 20:22:02 +0200 | [diff] [blame] | 168 | memcpy(op->ob_sval, str, size+1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | /* share short strings */ |
| 170 | if (size == 0) { |
| 171 | nullstring = op; |
| 172 | Py_INCREF(op); |
| 173 | } else if (size == 1) { |
| 174 | characters[*str & UCHAR_MAX] = op; |
| 175 | Py_INCREF(op); |
| 176 | } |
| 177 | return (PyObject *) op; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 178 | } |
Guido van Rossum | ebea9be | 2007-04-09 00:49:13 +0000 | [diff] [blame] | 179 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 180 | PyObject * |
| 181 | PyBytes_FromFormatV(const char *format, va_list vargs) |
| 182 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 183 | char *s; |
Victor Stinner | 03dab78 | 2015-10-14 00:21:35 +0200 | [diff] [blame] | 184 | const char *f; |
| 185 | const char *p; |
| 186 | Py_ssize_t prec; |
| 187 | int longflag; |
| 188 | int size_tflag; |
| 189 | /* Longest 64-bit formatted numbers: |
| 190 | - "18446744073709551615\0" (21 bytes) |
| 191 | - "-9223372036854775808\0" (21 bytes) |
| 192 | Decimal takes the most space (it isn't enough for octal.) |
Guido van Rossum | 343e97f | 2007-04-09 00:43:24 +0000 | [diff] [blame] | 193 | |
Victor Stinner | 03dab78 | 2015-10-14 00:21:35 +0200 | [diff] [blame] | 194 | Longest 64-bit pointer representation: |
| 195 | "0xffffffffffffffff\0" (19 bytes). */ |
| 196 | char buffer[21]; |
| 197 | _PyBytesWriter writer; |
Guido van Rossum | 343e97f | 2007-04-09 00:43:24 +0000 | [diff] [blame] | 198 | |
Victor Stinner | 03dab78 | 2015-10-14 00:21:35 +0200 | [diff] [blame] | 199 | _PyBytesWriter_Init(&writer); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 200 | |
Victor Stinner | 03dab78 | 2015-10-14 00:21:35 +0200 | [diff] [blame] | 201 | s = _PyBytesWriter_Alloc(&writer, strlen(format)); |
| 202 | if (s == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 203 | return NULL; |
Victor Stinner | 03dab78 | 2015-10-14 00:21:35 +0200 | [diff] [blame] | 204 | writer.overallocate = 1; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 205 | |
Victor Stinner | 03dab78 | 2015-10-14 00:21:35 +0200 | [diff] [blame] | 206 | #define WRITE_BYTES(str) \ |
| 207 | do { \ |
| 208 | s = _PyBytesWriter_WriteBytes(&writer, s, (str), strlen(str)); \ |
| 209 | if (s == NULL) \ |
| 210 | goto error; \ |
| 211 | } while (0) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | for (f = format; *f; f++) { |
Victor Stinner | 03dab78 | 2015-10-14 00:21:35 +0200 | [diff] [blame] | 214 | if (*f != '%') { |
| 215 | *s++ = *f; |
| 216 | continue; |
| 217 | } |
| 218 | |
| 219 | p = f++; |
| 220 | |
| 221 | /* ignore the width (ex: 10 in "%10s") */ |
| 222 | while (Py_ISDIGIT(*f)) |
| 223 | f++; |
| 224 | |
| 225 | /* parse the precision (ex: 10 in "%.10s") */ |
| 226 | prec = 0; |
| 227 | if (*f == '.') { |
| 228 | f++; |
| 229 | for (; Py_ISDIGIT(*f); f++) { |
| 230 | prec = (prec * 10) + (*f - '0'); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | while (*f && *f != '%' && !Py_ISALPHA(*f)) |
| 235 | f++; |
| 236 | |
| 237 | /* handle the long flag ('l'), but only for %ld and %lu. |
| 238 | others can be added when necessary. */ |
| 239 | longflag = 0; |
| 240 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 241 | longflag = 1; |
| 242 | ++f; |
| 243 | } |
| 244 | |
| 245 | /* handle the size_t flag ('z'). */ |
| 246 | size_tflag = 0; |
| 247 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 248 | size_tflag = 1; |
| 249 | ++f; |
| 250 | } |
| 251 | |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 252 | /* subtract bytes preallocated for the format string |
Victor Stinner | 03dab78 | 2015-10-14 00:21:35 +0200 | [diff] [blame] | 253 | (ex: 2 for "%s") */ |
| 254 | writer.min_size -= (f - p + 1); |
| 255 | |
| 256 | switch (*f) { |
| 257 | case 'c': |
| 258 | { |
| 259 | int c = va_arg(vargs, int); |
| 260 | if (c < 0 || c > 255) { |
| 261 | PyErr_SetString(PyExc_OverflowError, |
| 262 | "PyBytes_FromFormatV(): %c format " |
| 263 | "expects an integer in range [0; 255]"); |
| 264 | goto error; |
| 265 | } |
| 266 | writer.min_size++; |
| 267 | *s++ = (unsigned char)c; |
| 268 | break; |
| 269 | } |
| 270 | |
| 271 | case 'd': |
| 272 | if (longflag) |
| 273 | sprintf(buffer, "%ld", va_arg(vargs, long)); |
| 274 | else if (size_tflag) |
| 275 | sprintf(buffer, "%" PY_FORMAT_SIZE_T "d", |
| 276 | va_arg(vargs, Py_ssize_t)); |
| 277 | else |
| 278 | sprintf(buffer, "%d", va_arg(vargs, int)); |
| 279 | assert(strlen(buffer) < sizeof(buffer)); |
| 280 | WRITE_BYTES(buffer); |
| 281 | break; |
| 282 | |
| 283 | case 'u': |
| 284 | if (longflag) |
| 285 | sprintf(buffer, "%lu", |
| 286 | va_arg(vargs, unsigned long)); |
| 287 | else if (size_tflag) |
| 288 | sprintf(buffer, "%" PY_FORMAT_SIZE_T "u", |
| 289 | va_arg(vargs, size_t)); |
| 290 | else |
| 291 | sprintf(buffer, "%u", |
| 292 | va_arg(vargs, unsigned int)); |
| 293 | assert(strlen(buffer) < sizeof(buffer)); |
| 294 | WRITE_BYTES(buffer); |
| 295 | break; |
| 296 | |
| 297 | case 'i': |
| 298 | sprintf(buffer, "%i", va_arg(vargs, int)); |
| 299 | assert(strlen(buffer) < sizeof(buffer)); |
| 300 | WRITE_BYTES(buffer); |
| 301 | break; |
| 302 | |
| 303 | case 'x': |
| 304 | sprintf(buffer, "%x", va_arg(vargs, int)); |
| 305 | assert(strlen(buffer) < sizeof(buffer)); |
| 306 | WRITE_BYTES(buffer); |
| 307 | break; |
| 308 | |
| 309 | case 's': |
| 310 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 311 | Py_ssize_t i; |
Victor Stinner | 03dab78 | 2015-10-14 00:21:35 +0200 | [diff] [blame] | 312 | |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 313 | p = va_arg(vargs, const char*); |
Victor Stinner | 03dab78 | 2015-10-14 00:21:35 +0200 | [diff] [blame] | 314 | i = strlen(p); |
| 315 | if (prec > 0 && i > prec) |
| 316 | i = prec; |
| 317 | s = _PyBytesWriter_WriteBytes(&writer, s, p, i); |
| 318 | if (s == NULL) |
| 319 | goto error; |
| 320 | break; |
| 321 | } |
| 322 | |
| 323 | case 'p': |
| 324 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 325 | assert(strlen(buffer) < sizeof(buffer)); |
| 326 | /* %p is ill-defined: ensure leading 0x. */ |
| 327 | if (buffer[1] == 'X') |
| 328 | buffer[1] = 'x'; |
| 329 | else if (buffer[1] != 'x') { |
| 330 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 331 | buffer[0] = '0'; |
| 332 | buffer[1] = 'x'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | } |
Victor Stinner | 03dab78 | 2015-10-14 00:21:35 +0200 | [diff] [blame] | 334 | WRITE_BYTES(buffer); |
| 335 | break; |
| 336 | |
| 337 | case '%': |
| 338 | writer.min_size++; |
| 339 | *s++ = '%'; |
| 340 | break; |
| 341 | |
| 342 | default: |
| 343 | if (*f == 0) { |
| 344 | /* fix min_size if we reached the end of the format string */ |
| 345 | writer.min_size++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | } |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 347 | |
Victor Stinner | 03dab78 | 2015-10-14 00:21:35 +0200 | [diff] [blame] | 348 | /* invalid format string: copy unformatted string and exit */ |
| 349 | WRITE_BYTES(p); |
| 350 | return _PyBytesWriter_Finish(&writer, s); |
| 351 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 353 | |
Victor Stinner | 03dab78 | 2015-10-14 00:21:35 +0200 | [diff] [blame] | 354 | #undef WRITE_BYTES |
| 355 | |
| 356 | return _PyBytesWriter_Finish(&writer, s); |
| 357 | |
| 358 | error: |
| 359 | _PyBytesWriter_Dealloc(&writer); |
| 360 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | PyObject * |
| 364 | PyBytes_FromFormat(const char *format, ...) |
| 365 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | PyObject* ret; |
| 367 | va_list vargs; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 368 | |
| 369 | #ifdef HAVE_STDARG_PROTOTYPES |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | va_start(vargs, format); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 371 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | va_start(vargs); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 373 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | ret = PyBytes_FromFormatV(format, vargs); |
| 375 | va_end(vargs); |
| 376 | return ret; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 379 | /* Helpers for formatstring */ |
| 380 | |
| 381 | Py_LOCAL_INLINE(PyObject *) |
| 382 | getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx) |
| 383 | { |
| 384 | Py_ssize_t argidx = *p_argidx; |
| 385 | if (argidx < arglen) { |
| 386 | (*p_argidx)++; |
| 387 | if (arglen < 0) |
| 388 | return args; |
| 389 | else |
| 390 | return PyTuple_GetItem(args, argidx); |
| 391 | } |
| 392 | PyErr_SetString(PyExc_TypeError, |
| 393 | "not enough arguments for format string"); |
| 394 | return NULL; |
| 395 | } |
| 396 | |
| 397 | /* Format codes |
| 398 | * F_LJUST '-' |
| 399 | * F_SIGN '+' |
| 400 | * F_BLANK ' ' |
| 401 | * F_ALT '#' |
| 402 | * F_ZERO '0' |
| 403 | */ |
| 404 | #define F_LJUST (1<<0) |
| 405 | #define F_SIGN (1<<1) |
| 406 | #define F_BLANK (1<<2) |
| 407 | #define F_ALT (1<<3) |
| 408 | #define F_ZERO (1<<4) |
| 409 | |
| 410 | /* Returns a new reference to a PyBytes object, or NULL on failure. */ |
| 411 | |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 412 | static char* |
| 413 | formatfloat(PyObject *v, int flags, int prec, int type, |
Victor Stinner | ad77158 | 2015-10-09 12:38:53 +0200 | [diff] [blame] | 414 | PyObject **p_result, _PyBytesWriter *writer, char *str) |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 415 | { |
| 416 | char *p; |
| 417 | PyObject *result; |
| 418 | double x; |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 419 | size_t len; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 420 | |
| 421 | x = PyFloat_AsDouble(v); |
| 422 | if (x == -1.0 && PyErr_Occurred()) { |
| 423 | PyErr_Format(PyExc_TypeError, "float argument required, " |
| 424 | "not %.200s", Py_TYPE(v)->tp_name); |
| 425 | return NULL; |
| 426 | } |
| 427 | |
| 428 | if (prec < 0) |
| 429 | prec = 6; |
| 430 | |
| 431 | p = PyOS_double_to_string(x, type, prec, |
| 432 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
| 433 | |
| 434 | if (p == NULL) |
| 435 | return NULL; |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 436 | |
| 437 | len = strlen(p); |
| 438 | if (writer != NULL) { |
Victor Stinner | ad77158 | 2015-10-09 12:38:53 +0200 | [diff] [blame] | 439 | str = _PyBytesWriter_Prepare(writer, str, len); |
| 440 | if (str == NULL) |
| 441 | return NULL; |
Christian Heimes | f051e43 | 2016-09-13 20:22:02 +0200 | [diff] [blame] | 442 | memcpy(str, p, len); |
Victor Stinner | 71dc3d8 | 2016-04-26 12:35:13 +0200 | [diff] [blame] | 443 | PyMem_Free(p); |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 444 | str += len; |
| 445 | return str; |
| 446 | } |
| 447 | |
| 448 | result = PyBytes_FromStringAndSize(p, len); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 449 | PyMem_Free(p); |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 450 | *p_result = result; |
Zackery Spytz | 96c5932 | 2018-10-03 00:01:30 -0600 | [diff] [blame] | 451 | return result != NULL ? str : NULL; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Serhiy Storchaka | 2c7b5a9 | 2015-03-30 09:19:08 +0300 | [diff] [blame] | 454 | static PyObject * |
| 455 | formatlong(PyObject *v, int flags, int prec, int type) |
| 456 | { |
| 457 | PyObject *result, *iobj; |
| 458 | if (type == 'i') |
| 459 | type = 'd'; |
| 460 | if (PyLong_Check(v)) |
| 461 | return _PyUnicode_FormatLong(v, flags & F_ALT, prec, type); |
| 462 | if (PyNumber_Check(v)) { |
| 463 | /* make sure number is a type of integer for o, x, and X */ |
| 464 | if (type == 'o' || type == 'x' || type == 'X') |
| 465 | iobj = PyNumber_Index(v); |
| 466 | else |
| 467 | iobj = PyNumber_Long(v); |
| 468 | if (iobj == NULL) { |
| 469 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 470 | return NULL; |
| 471 | } |
| 472 | else if (!PyLong_Check(iobj)) |
| 473 | Py_CLEAR(iobj); |
| 474 | if (iobj != NULL) { |
| 475 | result = _PyUnicode_FormatLong(iobj, flags & F_ALT, prec, type); |
| 476 | Py_DECREF(iobj); |
| 477 | return result; |
| 478 | } |
| 479 | } |
| 480 | PyErr_Format(PyExc_TypeError, |
| 481 | "%%%c format: %s is required, not %.200s", type, |
| 482 | (type == 'o' || type == 'x' || type == 'X') ? "an integer" |
| 483 | : "a number", |
| 484 | Py_TYPE(v)->tp_name); |
| 485 | return NULL; |
| 486 | } |
| 487 | |
| 488 | static int |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame] | 489 | byte_converter(PyObject *arg, char *p) |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 490 | { |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 491 | if (PyBytes_Check(arg) && PyBytes_GET_SIZE(arg) == 1) { |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame] | 492 | *p = PyBytes_AS_STRING(arg)[0]; |
| 493 | return 1; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 494 | } |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 495 | else if (PyByteArray_Check(arg) && PyByteArray_GET_SIZE(arg) == 1) { |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame] | 496 | *p = PyByteArray_AS_STRING(arg)[0]; |
| 497 | return 1; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 498 | } |
| 499 | else { |
Serhiy Storchaka | 2c7b5a9 | 2015-03-30 09:19:08 +0300 | [diff] [blame] | 500 | PyObject *iobj; |
| 501 | long ival; |
| 502 | int overflow; |
| 503 | /* make sure number is a type of integer */ |
| 504 | if (PyLong_Check(arg)) { |
| 505 | ival = PyLong_AsLongAndOverflow(arg, &overflow); |
| 506 | } |
| 507 | else { |
| 508 | iobj = PyNumber_Index(arg); |
| 509 | if (iobj == NULL) { |
| 510 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 511 | return 0; |
| 512 | goto onError; |
| 513 | } |
| 514 | ival = PyLong_AsLongAndOverflow(iobj, &overflow); |
| 515 | Py_DECREF(iobj); |
| 516 | } |
Serhiy Storchaka | 41525e3 | 2015-04-03 20:53:46 +0300 | [diff] [blame] | 517 | if (!overflow && ival == -1 && PyErr_Occurred()) |
| 518 | goto onError; |
| 519 | if (overflow || !(0 <= ival && ival <= 255)) { |
| 520 | PyErr_SetString(PyExc_OverflowError, |
| 521 | "%c arg not in range(256)"); |
| 522 | return 0; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 523 | } |
Serhiy Storchaka | 41525e3 | 2015-04-03 20:53:46 +0300 | [diff] [blame] | 524 | *p = (char)ival; |
| 525 | return 1; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 526 | } |
Serhiy Storchaka | 2c7b5a9 | 2015-03-30 09:19:08 +0300 | [diff] [blame] | 527 | onError: |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame] | 528 | PyErr_SetString(PyExc_TypeError, |
| 529 | "%c requires an integer in range(256) or a single byte"); |
| 530 | return 0; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Xiang Zhang | 7e2a54c | 2017-03-14 15:07:15 +0800 | [diff] [blame] | 533 | static PyObject *_PyBytes_FromBuffer(PyObject *x); |
| 534 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 535 | static PyObject * |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame] | 536 | format_obj(PyObject *v, const char **pbuf, Py_ssize_t *plen) |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 537 | { |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame] | 538 | PyObject *func, *result; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 539 | _Py_IDENTIFIER(__bytes__); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 540 | /* is it a bytes object? */ |
| 541 | if (PyBytes_Check(v)) { |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame] | 542 | *pbuf = PyBytes_AS_STRING(v); |
| 543 | *plen = PyBytes_GET_SIZE(v); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 544 | Py_INCREF(v); |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame] | 545 | return v; |
| 546 | } |
| 547 | if (PyByteArray_Check(v)) { |
| 548 | *pbuf = PyByteArray_AS_STRING(v); |
| 549 | *plen = PyByteArray_GET_SIZE(v); |
| 550 | Py_INCREF(v); |
| 551 | return v; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 552 | } |
| 553 | /* does it support __bytes__? */ |
| 554 | func = _PyObject_LookupSpecial(v, &PyId___bytes__); |
| 555 | if (func != NULL) { |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 556 | result = _PyObject_CallNoArg(func); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 557 | Py_DECREF(func); |
| 558 | if (result == NULL) |
| 559 | return NULL; |
| 560 | if (!PyBytes_Check(result)) { |
| 561 | PyErr_Format(PyExc_TypeError, |
| 562 | "__bytes__ returned non-bytes (type %.200s)", |
| 563 | Py_TYPE(result)->tp_name); |
| 564 | Py_DECREF(result); |
| 565 | return NULL; |
| 566 | } |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame] | 567 | *pbuf = PyBytes_AS_STRING(result); |
| 568 | *plen = PyBytes_GET_SIZE(result); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 569 | return result; |
| 570 | } |
Xiang Zhang | 7e2a54c | 2017-03-14 15:07:15 +0800 | [diff] [blame] | 571 | /* does it support buffer protocol? */ |
| 572 | if (PyObject_CheckBuffer(v)) { |
| 573 | /* maybe we can avoid making a copy of the buffer object here? */ |
| 574 | result = _PyBytes_FromBuffer(v); |
| 575 | if (result == NULL) |
| 576 | return NULL; |
| 577 | *pbuf = PyBytes_AS_STRING(result); |
| 578 | *plen = PyBytes_GET_SIZE(result); |
| 579 | return result; |
| 580 | } |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 581 | PyErr_Format(PyExc_TypeError, |
Xiang Zhang | 7e2a54c | 2017-03-14 15:07:15 +0800 | [diff] [blame] | 582 | "%%b requires a bytes-like object, " |
| 583 | "or an object that implements __bytes__, not '%.100s'", |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 584 | Py_TYPE(v)->tp_name); |
| 585 | return NULL; |
| 586 | } |
| 587 | |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 588 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 589 | |
| 590 | PyObject * |
Victor Stinner | 772b2b0 | 2015-10-14 09:56:53 +0200 | [diff] [blame] | 591 | _PyBytes_FormatEx(const char *format, Py_ssize_t format_len, |
| 592 | PyObject *args, int use_bytearray) |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 593 | { |
Victor Stinner | 772b2b0 | 2015-10-14 09:56:53 +0200 | [diff] [blame] | 594 | const char *fmt; |
| 595 | char *res; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 596 | Py_ssize_t arglen, argidx; |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 597 | Py_ssize_t fmtcnt; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 598 | int args_owned = 0; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 599 | PyObject *dict = NULL; |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 600 | _PyBytesWriter writer; |
| 601 | |
Victor Stinner | 772b2b0 | 2015-10-14 09:56:53 +0200 | [diff] [blame] | 602 | if (args == NULL) { |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 603 | PyErr_BadInternalCall(); |
| 604 | return NULL; |
| 605 | } |
Victor Stinner | 772b2b0 | 2015-10-14 09:56:53 +0200 | [diff] [blame] | 606 | fmt = format; |
| 607 | fmtcnt = format_len; |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 608 | |
| 609 | _PyBytesWriter_Init(&writer); |
Victor Stinner | 772b2b0 | 2015-10-14 09:56:53 +0200 | [diff] [blame] | 610 | writer.use_bytearray = use_bytearray; |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 611 | |
| 612 | res = _PyBytesWriter_Alloc(&writer, fmtcnt); |
| 613 | if (res == NULL) |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 614 | return NULL; |
Victor Stinner | 772b2b0 | 2015-10-14 09:56:53 +0200 | [diff] [blame] | 615 | if (!use_bytearray) |
| 616 | writer.overallocate = 1; |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 617 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 618 | if (PyTuple_Check(args)) { |
| 619 | arglen = PyTuple_GET_SIZE(args); |
| 620 | argidx = 0; |
| 621 | } |
| 622 | else { |
| 623 | arglen = -1; |
| 624 | argidx = -2; |
| 625 | } |
| 626 | if (Py_TYPE(args)->tp_as_mapping && Py_TYPE(args)->tp_as_mapping->mp_subscript && |
| 627 | !PyTuple_Check(args) && !PyBytes_Check(args) && !PyUnicode_Check(args) && |
| 628 | !PyByteArray_Check(args)) { |
| 629 | dict = args; |
| 630 | } |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 631 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 632 | while (--fmtcnt >= 0) { |
| 633 | if (*fmt != '%') { |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 634 | Py_ssize_t len; |
| 635 | char *pos; |
| 636 | |
Xiang Zhang | b76ad51 | 2017-03-06 17:17:05 +0800 | [diff] [blame] | 637 | pos = (char *)memchr(fmt + 1, '%', fmtcnt); |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 638 | if (pos != NULL) |
| 639 | len = pos - fmt; |
Victor Stinner | 772b2b0 | 2015-10-14 09:56:53 +0200 | [diff] [blame] | 640 | else |
Xiang Zhang | b76ad51 | 2017-03-06 17:17:05 +0800 | [diff] [blame] | 641 | len = fmtcnt + 1; |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 642 | assert(len != 0); |
| 643 | |
Christian Heimes | f051e43 | 2016-09-13 20:22:02 +0200 | [diff] [blame] | 644 | memcpy(res, fmt, len); |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 645 | res += len; |
| 646 | fmt += len; |
| 647 | fmtcnt -= (len - 1); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 648 | } |
| 649 | else { |
| 650 | /* Got a format specifier */ |
| 651 | int flags = 0; |
| 652 | Py_ssize_t width = -1; |
| 653 | int prec = -1; |
| 654 | int c = '\0'; |
| 655 | int fill; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 656 | PyObject *v = NULL; |
| 657 | PyObject *temp = NULL; |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame] | 658 | const char *pbuf = NULL; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 659 | int sign; |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame] | 660 | Py_ssize_t len = 0; |
| 661 | char onechar; /* For byte_converter() */ |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 662 | Py_ssize_t alloc; |
| 663 | #ifdef Py_DEBUG |
| 664 | char *before; |
| 665 | #endif |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 666 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 667 | fmt++; |
Serhiy Storchaka | 9f8ad3f | 2017-03-08 05:51:19 +0200 | [diff] [blame] | 668 | if (*fmt == '%') { |
| 669 | *res++ = '%'; |
| 670 | fmt++; |
| 671 | fmtcnt--; |
| 672 | continue; |
| 673 | } |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 674 | if (*fmt == '(') { |
Victor Stinner | 772b2b0 | 2015-10-14 09:56:53 +0200 | [diff] [blame] | 675 | const char *keystart; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 676 | Py_ssize_t keylen; |
| 677 | PyObject *key; |
| 678 | int pcount = 1; |
| 679 | |
| 680 | if (dict == NULL) { |
| 681 | PyErr_SetString(PyExc_TypeError, |
| 682 | "format requires a mapping"); |
| 683 | goto error; |
| 684 | } |
| 685 | ++fmt; |
| 686 | --fmtcnt; |
| 687 | keystart = fmt; |
| 688 | /* Skip over balanced parentheses */ |
| 689 | while (pcount > 0 && --fmtcnt >= 0) { |
| 690 | if (*fmt == ')') |
| 691 | --pcount; |
| 692 | else if (*fmt == '(') |
| 693 | ++pcount; |
| 694 | fmt++; |
| 695 | } |
| 696 | keylen = fmt - keystart - 1; |
| 697 | if (fmtcnt < 0 || pcount > 0) { |
| 698 | PyErr_SetString(PyExc_ValueError, |
| 699 | "incomplete format key"); |
| 700 | goto error; |
| 701 | } |
| 702 | key = PyBytes_FromStringAndSize(keystart, |
| 703 | keylen); |
| 704 | if (key == NULL) |
| 705 | goto error; |
| 706 | if (args_owned) { |
| 707 | Py_DECREF(args); |
| 708 | args_owned = 0; |
| 709 | } |
| 710 | args = PyObject_GetItem(dict, key); |
| 711 | Py_DECREF(key); |
| 712 | if (args == NULL) { |
| 713 | goto error; |
| 714 | } |
| 715 | args_owned = 1; |
| 716 | arglen = -1; |
| 717 | argidx = -2; |
| 718 | } |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 719 | |
| 720 | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 721 | while (--fmtcnt >= 0) { |
| 722 | switch (c = *fmt++) { |
| 723 | case '-': flags |= F_LJUST; continue; |
| 724 | case '+': flags |= F_SIGN; continue; |
| 725 | case ' ': flags |= F_BLANK; continue; |
| 726 | case '#': flags |= F_ALT; continue; |
| 727 | case '0': flags |= F_ZERO; continue; |
| 728 | } |
| 729 | break; |
| 730 | } |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 731 | |
| 732 | /* Parse width. Example: "%10s" => width=10 */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 733 | if (c == '*') { |
| 734 | v = getnextarg(args, arglen, &argidx); |
| 735 | if (v == NULL) |
| 736 | goto error; |
| 737 | if (!PyLong_Check(v)) { |
| 738 | PyErr_SetString(PyExc_TypeError, |
| 739 | "* wants int"); |
| 740 | goto error; |
| 741 | } |
| 742 | width = PyLong_AsSsize_t(v); |
| 743 | if (width == -1 && PyErr_Occurred()) |
| 744 | goto error; |
| 745 | if (width < 0) { |
| 746 | flags |= F_LJUST; |
| 747 | width = -width; |
| 748 | } |
| 749 | if (--fmtcnt >= 0) |
| 750 | c = *fmt++; |
| 751 | } |
| 752 | else if (c >= 0 && isdigit(c)) { |
| 753 | width = c - '0'; |
| 754 | while (--fmtcnt >= 0) { |
| 755 | c = Py_CHARMASK(*fmt++); |
| 756 | if (!isdigit(c)) |
| 757 | break; |
| 758 | if (width > (PY_SSIZE_T_MAX - ((int)c - '0')) / 10) { |
| 759 | PyErr_SetString( |
| 760 | PyExc_ValueError, |
| 761 | "width too big"); |
| 762 | goto error; |
| 763 | } |
| 764 | width = width*10 + (c - '0'); |
| 765 | } |
| 766 | } |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 767 | |
| 768 | /* Parse precision. Example: "%.3f" => prec=3 */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 769 | if (c == '.') { |
| 770 | prec = 0; |
| 771 | if (--fmtcnt >= 0) |
| 772 | c = *fmt++; |
| 773 | if (c == '*') { |
| 774 | v = getnextarg(args, arglen, &argidx); |
| 775 | if (v == NULL) |
| 776 | goto error; |
| 777 | if (!PyLong_Check(v)) { |
| 778 | PyErr_SetString( |
| 779 | PyExc_TypeError, |
| 780 | "* wants int"); |
| 781 | goto error; |
| 782 | } |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 783 | prec = _PyLong_AsInt(v); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 784 | if (prec == -1 && PyErr_Occurred()) |
| 785 | goto error; |
| 786 | if (prec < 0) |
| 787 | prec = 0; |
| 788 | if (--fmtcnt >= 0) |
| 789 | c = *fmt++; |
| 790 | } |
| 791 | else if (c >= 0 && isdigit(c)) { |
| 792 | prec = c - '0'; |
| 793 | while (--fmtcnt >= 0) { |
| 794 | c = Py_CHARMASK(*fmt++); |
| 795 | if (!isdigit(c)) |
| 796 | break; |
| 797 | if (prec > (INT_MAX - ((int)c - '0')) / 10) { |
| 798 | PyErr_SetString( |
| 799 | PyExc_ValueError, |
| 800 | "prec too big"); |
| 801 | goto error; |
| 802 | } |
| 803 | prec = prec*10 + (c - '0'); |
| 804 | } |
| 805 | } |
| 806 | } /* prec */ |
| 807 | if (fmtcnt >= 0) { |
| 808 | if (c == 'h' || c == 'l' || c == 'L') { |
| 809 | if (--fmtcnt >= 0) |
| 810 | c = *fmt++; |
| 811 | } |
| 812 | } |
| 813 | if (fmtcnt < 0) { |
| 814 | PyErr_SetString(PyExc_ValueError, |
| 815 | "incomplete format"); |
| 816 | goto error; |
| 817 | } |
Serhiy Storchaka | 9f8ad3f | 2017-03-08 05:51:19 +0200 | [diff] [blame] | 818 | v = getnextarg(args, arglen, &argidx); |
| 819 | if (v == NULL) |
| 820 | goto error; |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 821 | |
Alexey Izbyshev | ccd9975 | 2018-08-23 10:50:52 +0300 | [diff] [blame] | 822 | if (fmtcnt == 0) { |
| 823 | /* last write: disable writer overallocation */ |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 824 | writer.overallocate = 0; |
| 825 | } |
| 826 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 827 | sign = 0; |
| 828 | fill = ' '; |
| 829 | switch (c) { |
Ethan Furman | 62e977f | 2015-03-11 08:17:00 -0700 | [diff] [blame] | 830 | case 'r': |
| 831 | // %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] | 832 | case 'a': |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame] | 833 | temp = PyObject_ASCII(v); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 834 | if (temp == NULL) |
| 835 | goto error; |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame] | 836 | assert(PyUnicode_IS_ASCII(temp)); |
| 837 | pbuf = (const char *)PyUnicode_1BYTE_DATA(temp); |
| 838 | len = PyUnicode_GET_LENGTH(temp); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 839 | if (prec >= 0 && len > prec) |
| 840 | len = prec; |
| 841 | break; |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 842 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 843 | case 's': |
| 844 | // %s is only for 2/3 code; 3 only code should use %b |
| 845 | case 'b': |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame] | 846 | temp = format_obj(v, &pbuf, &len); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 847 | if (temp == NULL) |
| 848 | goto error; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 849 | if (prec >= 0 && len > prec) |
| 850 | len = prec; |
| 851 | break; |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 852 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 853 | case 'i': |
| 854 | case 'd': |
| 855 | case 'u': |
| 856 | case 'o': |
| 857 | case 'x': |
| 858 | case 'X': |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 859 | if (PyLong_CheckExact(v) |
Victor Stinner | 0cdad1e | 2015-10-09 22:50:36 +0200 | [diff] [blame] | 860 | && width == -1 && prec == -1 |
| 861 | && !(flags & (F_SIGN | F_BLANK)) |
| 862 | && c != 'X') |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 863 | { |
| 864 | /* Fast path */ |
| 865 | int alternate = flags & F_ALT; |
| 866 | int base; |
| 867 | |
| 868 | switch(c) |
| 869 | { |
| 870 | default: |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 871 | Py_UNREACHABLE(); |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 872 | case 'd': |
| 873 | case 'i': |
| 874 | case 'u': |
| 875 | base = 10; |
| 876 | break; |
| 877 | case 'o': |
| 878 | base = 8; |
| 879 | break; |
| 880 | case 'x': |
| 881 | case 'X': |
| 882 | base = 16; |
| 883 | break; |
| 884 | } |
| 885 | |
| 886 | /* Fast path */ |
Victor Stinner | 0cdad1e | 2015-10-09 22:50:36 +0200 | [diff] [blame] | 887 | writer.min_size -= 2; /* size preallocated for "%d" */ |
Victor Stinner | be75b8c | 2015-10-09 22:43:24 +0200 | [diff] [blame] | 888 | res = _PyLong_FormatBytesWriter(&writer, res, |
| 889 | v, base, alternate); |
| 890 | if (res == NULL) |
| 891 | goto error; |
| 892 | continue; |
| 893 | } |
| 894 | |
Serhiy Storchaka | 2c7b5a9 | 2015-03-30 09:19:08 +0300 | [diff] [blame] | 895 | temp = formatlong(v, flags, prec, c); |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame] | 896 | if (!temp) |
| 897 | goto error; |
| 898 | assert(PyUnicode_IS_ASCII(temp)); |
| 899 | pbuf = (const char *)PyUnicode_1BYTE_DATA(temp); |
| 900 | len = PyUnicode_GET_LENGTH(temp); |
| 901 | sign = 1; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 902 | if (flags & F_ZERO) |
| 903 | fill = '0'; |
| 904 | break; |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 905 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 906 | case 'e': |
| 907 | case 'E': |
| 908 | case 'f': |
| 909 | case 'F': |
| 910 | case 'g': |
| 911 | case 'G': |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 912 | if (width == -1 && prec == -1 |
| 913 | && !(flags & (F_SIGN | F_BLANK))) |
| 914 | { |
| 915 | /* Fast path */ |
Victor Stinner | 0cdad1e | 2015-10-09 22:50:36 +0200 | [diff] [blame] | 916 | writer.min_size -= 2; /* size preallocated for "%f" */ |
Victor Stinner | ad77158 | 2015-10-09 12:38:53 +0200 | [diff] [blame] | 917 | res = formatfloat(v, flags, prec, c, NULL, &writer, res); |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 918 | if (res == NULL) |
| 919 | goto error; |
| 920 | continue; |
| 921 | } |
| 922 | |
Victor Stinner | ad77158 | 2015-10-09 12:38:53 +0200 | [diff] [blame] | 923 | if (!formatfloat(v, flags, prec, c, &temp, NULL, res)) |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 924 | goto error; |
| 925 | pbuf = PyBytes_AS_STRING(temp); |
| 926 | len = PyBytes_GET_SIZE(temp); |
| 927 | sign = 1; |
| 928 | if (flags & F_ZERO) |
| 929 | fill = '0'; |
| 930 | break; |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 931 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 932 | case 'c': |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame] | 933 | pbuf = &onechar; |
| 934 | len = byte_converter(v, &onechar); |
| 935 | if (!len) |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 936 | goto error; |
Victor Stinner | 0cdad1e | 2015-10-09 22:50:36 +0200 | [diff] [blame] | 937 | if (width == -1) { |
| 938 | /* Fast path */ |
| 939 | *res++ = onechar; |
| 940 | continue; |
| 941 | } |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 942 | break; |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 943 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 944 | default: |
| 945 | PyErr_Format(PyExc_ValueError, |
| 946 | "unsupported format character '%c' (0x%x) " |
| 947 | "at index %zd", |
| 948 | c, c, |
Victor Stinner | 772b2b0 | 2015-10-14 09:56:53 +0200 | [diff] [blame] | 949 | (Py_ssize_t)(fmt - 1 - format)); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 950 | goto error; |
| 951 | } |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 952 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 953 | if (sign) { |
| 954 | if (*pbuf == '-' || *pbuf == '+') { |
| 955 | sign = *pbuf++; |
| 956 | len--; |
| 957 | } |
| 958 | else if (flags & F_SIGN) |
| 959 | sign = '+'; |
| 960 | else if (flags & F_BLANK) |
| 961 | sign = ' '; |
| 962 | else |
| 963 | sign = 0; |
| 964 | } |
| 965 | if (width < len) |
| 966 | width = len; |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 967 | |
| 968 | alloc = width; |
| 969 | if (sign != 0 && len == width) |
| 970 | alloc++; |
Victor Stinner | 0cdad1e | 2015-10-09 22:50:36 +0200 | [diff] [blame] | 971 | /* 2: size preallocated for %s */ |
| 972 | if (alloc > 2) { |
| 973 | res = _PyBytesWriter_Prepare(&writer, res, alloc - 2); |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 974 | if (res == NULL) |
| 975 | goto error; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 976 | } |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 977 | #ifdef Py_DEBUG |
| 978 | before = res; |
| 979 | #endif |
| 980 | |
| 981 | /* Write the sign if needed */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 982 | if (sign) { |
| 983 | if (fill != ' ') |
| 984 | *res++ = sign; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 985 | if (width > len) |
| 986 | width--; |
| 987 | } |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 988 | |
| 989 | /* Write the numeric prefix for "x", "X" and "o" formats |
| 990 | if the alternate form is used. |
| 991 | For example, write "0x" for the "%#x" format. */ |
Serhiy Storchaka | b1a1619 | 2016-12-17 21:48:03 +0200 | [diff] [blame] | 992 | if ((flags & F_ALT) && (c == 'o' || c == 'x' || c == 'X')) { |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 993 | assert(pbuf[0] == '0'); |
| 994 | assert(pbuf[1] == c); |
| 995 | if (fill != ' ') { |
| 996 | *res++ = *pbuf++; |
| 997 | *res++ = *pbuf++; |
| 998 | } |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 999 | width -= 2; |
| 1000 | if (width < 0) |
| 1001 | width = 0; |
| 1002 | len -= 2; |
| 1003 | } |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 1004 | |
| 1005 | /* Pad left with the fill character if needed */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 1006 | if (width > len && !(flags & F_LJUST)) { |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 1007 | memset(res, fill, width - len); |
| 1008 | res += (width - len); |
| 1009 | width = len; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 1010 | } |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 1011 | |
| 1012 | /* If padding with spaces: write sign if needed and/or numeric |
| 1013 | prefix if the alternate form is used */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 1014 | if (fill == ' ') { |
| 1015 | if (sign) |
| 1016 | *res++ = sign; |
Serhiy Storchaka | b1a1619 | 2016-12-17 21:48:03 +0200 | [diff] [blame] | 1017 | if ((flags & F_ALT) && (c == 'o' || c == 'x' || c == 'X')) { |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 1018 | assert(pbuf[0] == '0'); |
| 1019 | assert(pbuf[1] == c); |
| 1020 | *res++ = *pbuf++; |
| 1021 | *res++ = *pbuf++; |
| 1022 | } |
| 1023 | } |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 1024 | |
| 1025 | /* Copy bytes */ |
Christian Heimes | f051e43 | 2016-09-13 20:22:02 +0200 | [diff] [blame] | 1026 | memcpy(res, pbuf, len); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 1027 | res += len; |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 1028 | |
| 1029 | /* Pad right with the fill character if needed */ |
| 1030 | if (width > len) { |
| 1031 | memset(res, ' ', width - len); |
| 1032 | res += (width - len); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 1033 | } |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 1034 | |
Serhiy Storchaka | 9f8ad3f | 2017-03-08 05:51:19 +0200 | [diff] [blame] | 1035 | if (dict && (argidx < arglen)) { |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 1036 | PyErr_SetString(PyExc_TypeError, |
| 1037 | "not all arguments converted during bytes formatting"); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 1038 | Py_XDECREF(temp); |
| 1039 | goto error; |
| 1040 | } |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 1041 | Py_XDECREF(temp); |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 1042 | |
| 1043 | #ifdef Py_DEBUG |
| 1044 | /* check that we computed the exact size for this write */ |
| 1045 | assert((res - before) == alloc); |
| 1046 | #endif |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 1047 | } /* '%' */ |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 1048 | |
| 1049 | /* If overallocation was disabled, ensure that it was the last |
| 1050 | write. Otherwise, we missed an optimization */ |
Alexey Izbyshev | ccd9975 | 2018-08-23 10:50:52 +0300 | [diff] [blame] | 1051 | assert(writer.overallocate || fmtcnt == 0 || use_bytearray); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 1052 | } /* until end */ |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 1053 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 1054 | if (argidx < arglen && !dict) { |
| 1055 | PyErr_SetString(PyExc_TypeError, |
| 1056 | "not all arguments converted during bytes formatting"); |
| 1057 | goto error; |
| 1058 | } |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 1059 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 1060 | if (args_owned) { |
| 1061 | Py_DECREF(args); |
| 1062 | } |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 1063 | return _PyBytesWriter_Finish(&writer, res); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 1064 | |
| 1065 | error: |
Victor Stinner | fa7762e | 2015-10-09 11:48:06 +0200 | [diff] [blame] | 1066 | _PyBytesWriter_Dealloc(&writer); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 1067 | if (args_owned) { |
| 1068 | Py_DECREF(args); |
| 1069 | } |
| 1070 | return NULL; |
| 1071 | } |
| 1072 | |
| 1073 | /* =-= */ |
| 1074 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 1075 | static void |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1076 | bytes_dealloc(PyObject *op) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 1077 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1078 | Py_TYPE(op)->tp_free(op); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1081 | /* Unescape a backslash-escaped string. If unicode is non-zero, |
| 1082 | the string is a u-literal. If recode_encoding is non-zero, |
| 1083 | the string is UTF-8 encoded and should be re-encoded in the |
| 1084 | specified encoding. */ |
| 1085 | |
Victor Stinner | 2ec8063 | 2015-10-14 13:32:13 +0200 | [diff] [blame] | 1086 | static char * |
| 1087 | _PyBytes_DecodeEscapeRecode(const char **s, const char *end, |
| 1088 | const char *errors, const char *recode_encoding, |
| 1089 | _PyBytesWriter *writer, char *p) |
| 1090 | { |
| 1091 | PyObject *u, *w; |
| 1092 | const char* t; |
| 1093 | |
| 1094 | t = *s; |
| 1095 | /* Decode non-ASCII bytes as UTF-8. */ |
| 1096 | while (t < end && (*t & 0x80)) |
| 1097 | t++; |
| 1098 | u = PyUnicode_DecodeUTF8(*s, t - *s, errors); |
| 1099 | if (u == NULL) |
| 1100 | return NULL; |
| 1101 | |
| 1102 | /* Recode them in target encoding. */ |
| 1103 | w = PyUnicode_AsEncodedString(u, recode_encoding, errors); |
| 1104 | Py_DECREF(u); |
| 1105 | if (w == NULL) |
| 1106 | return NULL; |
| 1107 | assert(PyBytes_Check(w)); |
| 1108 | |
| 1109 | /* Append bytes to output buffer. */ |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1110 | writer->min_size--; /* subtract 1 preallocated byte */ |
Victor Stinner | 2ec8063 | 2015-10-14 13:32:13 +0200 | [diff] [blame] | 1111 | p = _PyBytesWriter_WriteBytes(writer, p, |
| 1112 | PyBytes_AS_STRING(w), |
| 1113 | PyBytes_GET_SIZE(w)); |
| 1114 | Py_DECREF(w); |
| 1115 | if (p == NULL) |
| 1116 | return NULL; |
| 1117 | |
| 1118 | *s = t; |
| 1119 | return p; |
| 1120 | } |
| 1121 | |
Eric V. Smith | 42454af | 2016-10-31 09:22:08 -0400 | [diff] [blame] | 1122 | PyObject *_PyBytes_DecodeEscape(const char *s, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | Py_ssize_t len, |
| 1124 | const char *errors, |
| 1125 | Py_ssize_t unicode, |
Eric V. Smith | 42454af | 2016-10-31 09:22:08 -0400 | [diff] [blame] | 1126 | const char *recode_encoding, |
| 1127 | const char **first_invalid_escape) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1128 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1129 | int c; |
Victor Stinner | 2ec8063 | 2015-10-14 13:32:13 +0200 | [diff] [blame] | 1130 | char *p; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1131 | const char *end; |
Victor Stinner | 2ec8063 | 2015-10-14 13:32:13 +0200 | [diff] [blame] | 1132 | _PyBytesWriter writer; |
| 1133 | |
| 1134 | _PyBytesWriter_Init(&writer); |
| 1135 | |
| 1136 | p = _PyBytesWriter_Alloc(&writer, len); |
| 1137 | if (p == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1138 | return NULL; |
Victor Stinner | 2ec8063 | 2015-10-14 13:32:13 +0200 | [diff] [blame] | 1139 | writer.overallocate = 1; |
| 1140 | |
Eric V. Smith | 42454af | 2016-10-31 09:22:08 -0400 | [diff] [blame] | 1141 | *first_invalid_escape = NULL; |
| 1142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1143 | end = s + len; |
| 1144 | while (s < end) { |
| 1145 | if (*s != '\\') { |
| 1146 | non_esc: |
Victor Stinner | 2ec8063 | 2015-10-14 13:32:13 +0200 | [diff] [blame] | 1147 | if (!(recode_encoding && (*s & 0x80))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 | *p++ = *s++; |
| 1149 | } |
Victor Stinner | 2ec8063 | 2015-10-14 13:32:13 +0200 | [diff] [blame] | 1150 | else { |
| 1151 | /* non-ASCII character and need to recode */ |
| 1152 | p = _PyBytes_DecodeEscapeRecode(&s, end, |
| 1153 | errors, recode_encoding, |
| 1154 | &writer, p); |
| 1155 | if (p == NULL) |
| 1156 | goto failed; |
| 1157 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1158 | continue; |
| 1159 | } |
Victor Stinner | 2ec8063 | 2015-10-14 13:32:13 +0200 | [diff] [blame] | 1160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | s++; |
Victor Stinner | 2ec8063 | 2015-10-14 13:32:13 +0200 | [diff] [blame] | 1162 | if (s == end) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1163 | PyErr_SetString(PyExc_ValueError, |
| 1164 | "Trailing \\ in string"); |
| 1165 | goto failed; |
| 1166 | } |
Victor Stinner | 2ec8063 | 2015-10-14 13:32:13 +0200 | [diff] [blame] | 1167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1168 | switch (*s++) { |
| 1169 | /* XXX This assumes ASCII! */ |
| 1170 | case '\n': break; |
| 1171 | case '\\': *p++ = '\\'; break; |
| 1172 | case '\'': *p++ = '\''; break; |
| 1173 | case '\"': *p++ = '\"'; break; |
| 1174 | case 'b': *p++ = '\b'; break; |
| 1175 | case 'f': *p++ = '\014'; break; /* FF */ |
| 1176 | case 't': *p++ = '\t'; break; |
| 1177 | case 'n': *p++ = '\n'; break; |
| 1178 | case 'r': *p++ = '\r'; break; |
| 1179 | case 'v': *p++ = '\013'; break; /* VT */ |
| 1180 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 1181 | case '0': case '1': case '2': case '3': |
| 1182 | case '4': case '5': case '6': case '7': |
| 1183 | c = s[-1] - '0'; |
| 1184 | if (s < end && '0' <= *s && *s <= '7') { |
| 1185 | c = (c<<3) + *s++ - '0'; |
| 1186 | if (s < end && '0' <= *s && *s <= '7') |
| 1187 | c = (c<<3) + *s++ - '0'; |
| 1188 | } |
| 1189 | *p++ = c; |
| 1190 | break; |
| 1191 | case 'x': |
Victor Stinner | 2ec8063 | 2015-10-14 13:32:13 +0200 | [diff] [blame] | 1192 | if (s+1 < end) { |
| 1193 | int digit1, digit2; |
| 1194 | digit1 = _PyLong_DigitValue[Py_CHARMASK(s[0])]; |
| 1195 | digit2 = _PyLong_DigitValue[Py_CHARMASK(s[1])]; |
| 1196 | if (digit1 < 16 && digit2 < 16) { |
| 1197 | *p++ = (unsigned char)((digit1 << 4) + digit2); |
| 1198 | s += 2; |
| 1199 | break; |
| 1200 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1201 | } |
Victor Stinner | 2ec8063 | 2015-10-14 13:32:13 +0200 | [diff] [blame] | 1202 | /* invalid hexadecimal digits */ |
| 1203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1204 | if (!errors || strcmp(errors, "strict") == 0) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 1205 | PyErr_Format(PyExc_ValueError, |
| 1206 | "invalid \\x escape at position %d", |
Serhiy Storchaka | 801d955 | 2013-02-10 17:42:01 +0200 | [diff] [blame] | 1207 | s - 2 - (end - len)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1208 | goto failed; |
| 1209 | } |
| 1210 | if (strcmp(errors, "replace") == 0) { |
| 1211 | *p++ = '?'; |
| 1212 | } else if (strcmp(errors, "ignore") == 0) |
| 1213 | /* do nothing */; |
| 1214 | else { |
| 1215 | PyErr_Format(PyExc_ValueError, |
| 1216 | "decoding error; unknown " |
| 1217 | "error handling code: %.400s", |
| 1218 | errors); |
| 1219 | goto failed; |
| 1220 | } |
Serhiy Storchaka | ace3ad3 | 2013-01-25 23:31:43 +0200 | [diff] [blame] | 1221 | /* skip \x */ |
| 1222 | if (s < end && Py_ISXDIGIT(s[0])) |
| 1223 | s++; /* and a hexdigit */ |
| 1224 | break; |
Victor Stinner | 2ec8063 | 2015-10-14 13:32:13 +0200 | [diff] [blame] | 1225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1226 | default: |
Eric V. Smith | 42454af | 2016-10-31 09:22:08 -0400 | [diff] [blame] | 1227 | if (*first_invalid_escape == NULL) { |
| 1228 | *first_invalid_escape = s-1; /* Back up one char, since we've |
| 1229 | already incremented s. */ |
| 1230 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1231 | *p++ = '\\'; |
Eric V. Smith | 42454af | 2016-10-31 09:22:08 -0400 | [diff] [blame] | 1232 | s--; |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 1233 | goto non_esc; /* an arbitrary number of unescaped |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1234 | UTF-8 bytes may follow. */ |
| 1235 | } |
| 1236 | } |
Victor Stinner | 2ec8063 | 2015-10-14 13:32:13 +0200 | [diff] [blame] | 1237 | |
| 1238 | return _PyBytesWriter_Finish(&writer, p); |
| 1239 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1240 | failed: |
Victor Stinner | 2ec8063 | 2015-10-14 13:32:13 +0200 | [diff] [blame] | 1241 | _PyBytesWriter_Dealloc(&writer); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1242 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Eric V. Smith | 42454af | 2016-10-31 09:22:08 -0400 | [diff] [blame] | 1245 | PyObject *PyBytes_DecodeEscape(const char *s, |
| 1246 | Py_ssize_t len, |
| 1247 | const char *errors, |
| 1248 | Py_ssize_t unicode, |
| 1249 | const char *recode_encoding) |
| 1250 | { |
| 1251 | const char* first_invalid_escape; |
| 1252 | PyObject *result = _PyBytes_DecodeEscape(s, len, errors, unicode, |
| 1253 | recode_encoding, |
| 1254 | &first_invalid_escape); |
| 1255 | if (result == NULL) |
| 1256 | return NULL; |
| 1257 | if (first_invalid_escape != NULL) { |
| 1258 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 1259 | "invalid escape sequence '\\%c'", |
Serhiy Storchaka | 56cb465 | 2017-10-20 17:08:15 +0300 | [diff] [blame] | 1260 | (unsigned char)*first_invalid_escape) < 0) { |
Eric V. Smith | 42454af | 2016-10-31 09:22:08 -0400 | [diff] [blame] | 1261 | Py_DECREF(result); |
| 1262 | return NULL; |
| 1263 | } |
| 1264 | } |
| 1265 | return result; |
| 1266 | |
| 1267 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1268 | /* -------------------------------------------------------------------- */ |
| 1269 | /* object api */ |
| 1270 | |
| 1271 | Py_ssize_t |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1272 | PyBytes_Size(PyObject *op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1273 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1274 | if (!PyBytes_Check(op)) { |
| 1275 | PyErr_Format(PyExc_TypeError, |
| 1276 | "expected bytes, %.200s found", Py_TYPE(op)->tp_name); |
| 1277 | return -1; |
| 1278 | } |
| 1279 | return Py_SIZE(op); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | char * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1283 | PyBytes_AsString(PyObject *op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1284 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1285 | if (!PyBytes_Check(op)) { |
| 1286 | PyErr_Format(PyExc_TypeError, |
| 1287 | "expected bytes, %.200s found", Py_TYPE(op)->tp_name); |
| 1288 | return NULL; |
| 1289 | } |
| 1290 | return ((PyBytesObject *)op)->ob_sval; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | int |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1294 | PyBytes_AsStringAndSize(PyObject *obj, |
| 1295 | char **s, |
| 1296 | Py_ssize_t *len) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1297 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1298 | if (s == NULL) { |
| 1299 | PyErr_BadInternalCall(); |
| 1300 | return -1; |
| 1301 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1303 | if (!PyBytes_Check(obj)) { |
| 1304 | PyErr_Format(PyExc_TypeError, |
| 1305 | "expected bytes, %.200s found", Py_TYPE(obj)->tp_name); |
| 1306 | return -1; |
| 1307 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1309 | *s = PyBytes_AS_STRING(obj); |
| 1310 | if (len != NULL) |
| 1311 | *len = PyBytes_GET_SIZE(obj); |
| 1312 | else if (strlen(*s) != (size_t)PyBytes_GET_SIZE(obj)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1313 | PyErr_SetString(PyExc_ValueError, |
| 1314 | "embedded null byte"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1315 | return -1; |
| 1316 | } |
| 1317 | return 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1318 | } |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1319 | |
| 1320 | /* -------------------------------------------------------------------- */ |
| 1321 | /* Methods */ |
| 1322 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1323 | #include "stringlib/stringdefs.h" |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1324 | |
| 1325 | #include "stringlib/fastsearch.h" |
| 1326 | #include "stringlib/count.h" |
| 1327 | #include "stringlib/find.h" |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 1328 | #include "stringlib/join.h" |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1329 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1330 | #include "stringlib/split.h" |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 1331 | #include "stringlib/ctype.h" |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1332 | |
Eric Smith | 0f78bff | 2009-11-30 01:01:42 +0000 | [diff] [blame] | 1333 | #include "stringlib/transmogrify.h" |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1334 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1335 | PyObject * |
| 1336 | PyBytes_Repr(PyObject *obj, int smartquotes) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1337 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1338 | PyBytesObject* op = (PyBytesObject*) obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1339 | Py_ssize_t i, length = Py_SIZE(op); |
Benjamin Peterson | d48bc94 | 2014-09-29 19:12:26 -0400 | [diff] [blame] | 1340 | Py_ssize_t newsize, squotes, dquotes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1342 | unsigned char quote, *s, *p; |
| 1343 | |
| 1344 | /* Compute size of output string */ |
| 1345 | squotes = dquotes = 0; |
| 1346 | newsize = 3; /* b'' */ |
| 1347 | s = (unsigned char*)op->ob_sval; |
| 1348 | for (i = 0; i < length; i++) { |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 1349 | Py_ssize_t incr = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1350 | switch(s[i]) { |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 1351 | case '\'': squotes++; break; |
| 1352 | case '"': dquotes++; break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1353 | case '\\': case '\t': case '\n': case '\r': |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 1354 | incr = 2; break; /* \C */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1355 | default: |
| 1356 | if (s[i] < ' ' || s[i] >= 0x7f) |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 1357 | incr = 4; /* \xHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1358 | } |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 1359 | if (newsize > PY_SSIZE_T_MAX - incr) |
| 1360 | goto overflow; |
| 1361 | newsize += incr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1362 | } |
| 1363 | quote = '\''; |
| 1364 | if (smartquotes && squotes && !dquotes) |
| 1365 | quote = '"'; |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 1366 | if (squotes && quote == '\'') { |
| 1367 | if (newsize > PY_SSIZE_T_MAX - squotes) |
| 1368 | goto overflow; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1369 | newsize += squotes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1370 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1371 | |
| 1372 | v = PyUnicode_New(newsize, 127); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1373 | if (v == NULL) { |
| 1374 | return NULL; |
| 1375 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1376 | p = PyUnicode_1BYTE_DATA(v); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1377 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1378 | *p++ = 'b', *p++ = quote; |
| 1379 | for (i = 0; i < length; i++) { |
| 1380 | unsigned char c = op->ob_sval[i]; |
| 1381 | if (c == quote || c == '\\') |
| 1382 | *p++ = '\\', *p++ = c; |
| 1383 | else if (c == '\t') |
| 1384 | *p++ = '\\', *p++ = 't'; |
| 1385 | else if (c == '\n') |
| 1386 | *p++ = '\\', *p++ = 'n'; |
| 1387 | else if (c == '\r') |
| 1388 | *p++ = '\\', *p++ = 'r'; |
| 1389 | else if (c < ' ' || c >= 0x7f) { |
| 1390 | *p++ = '\\'; |
| 1391 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 1392 | *p++ = Py_hexdigits[(c & 0xf0) >> 4]; |
| 1393 | *p++ = Py_hexdigits[c & 0xf]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1394 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1395 | else |
| 1396 | *p++ = c; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1397 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1398 | *p++ = quote; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1399 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1400 | return v; |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 1401 | |
| 1402 | overflow: |
| 1403 | PyErr_SetString(PyExc_OverflowError, |
| 1404 | "bytes object is too large to make repr"); |
| 1405 | return NULL; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1408 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1409 | bytes_repr(PyObject *op) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1410 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1411 | return PyBytes_Repr(op, 1); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1414 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1415 | bytes_str(PyObject *op) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1416 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1417 | if (Py_BytesWarningFlag) { |
| 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 | b4435e2 | 2018-10-26 14:35:00 +0200 | [diff] [blame] | 1511 | (void)PyObject_INIT_VAR((PyVarObject *)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))) { |
Serhiy Storchaka | 1dd4982 | 2015-03-20 16:54:57 +0200 | [diff] [blame] | 1574 | if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) { |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 1575 | rc = PyObject_IsInstance((PyObject*)a, |
| 1576 | (PyObject*)&PyUnicode_Type); |
| 1577 | if (!rc) |
| 1578 | rc = PyObject_IsInstance((PyObject*)b, |
| 1579 | (PyObject*)&PyUnicode_Type); |
| 1580 | if (rc < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1581 | return NULL; |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 1582 | if (rc) { |
Serhiy Storchaka | 1dd4982 | 2015-03-20 16:54:57 +0200 | [diff] [blame] | 1583 | if (PyErr_WarnEx(PyExc_BytesWarning, |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 1584 | "Comparison between bytes and string", 1)) |
Serhiy Storchaka | 1dd4982 | 2015-03-20 16:54:57 +0200 | [diff] [blame] | 1585 | return NULL; |
| 1586 | } |
Serhiy Storchaka | ac5569b | 2015-05-30 17:48:19 +0300 | [diff] [blame] | 1587 | else { |
| 1588 | rc = PyObject_IsInstance((PyObject*)a, |
| 1589 | (PyObject*)&PyLong_Type); |
| 1590 | if (!rc) |
| 1591 | rc = PyObject_IsInstance((PyObject*)b, |
| 1592 | (PyObject*)&PyLong_Type); |
| 1593 | if (rc < 0) |
Serhiy Storchaka | 1dd4982 | 2015-03-20 16:54:57 +0200 | [diff] [blame] | 1594 | return NULL; |
Serhiy Storchaka | ac5569b | 2015-05-30 17:48:19 +0300 | [diff] [blame] | 1595 | if (rc) { |
| 1596 | if (PyErr_WarnEx(PyExc_BytesWarning, |
| 1597 | "Comparison between bytes and int", 1)) |
| 1598 | return NULL; |
| 1599 | } |
Serhiy Storchaka | 1dd4982 | 2015-03-20 16:54:57 +0200 | [diff] [blame] | 1600 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1601 | } |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 1602 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1603 | } |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 1604 | else if (a == b) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1605 | switch (op) { |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 1606 | case Py_EQ: |
| 1607 | case Py_LE: |
| 1608 | case Py_GE: |
| 1609 | /* a string is equal to itself */ |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 1610 | Py_RETURN_TRUE; |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 1611 | break; |
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 | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 1616 | break; |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 1617 | default: |
| 1618 | PyErr_BadArgument(); |
| 1619 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1620 | } |
| 1621 | } |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 1622 | else if (op == Py_EQ || op == Py_NE) { |
| 1623 | int eq = bytes_compare_eq(a, b); |
| 1624 | eq ^= (op == Py_NE); |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 1625 | return PyBool_FromLong(eq); |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 1626 | } |
| 1627 | else { |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 1628 | len_a = Py_SIZE(a); |
| 1629 | len_b = Py_SIZE(b); |
| 1630 | min_len = Py_MIN(len_a, len_b); |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 1631 | if (min_len > 0) { |
| 1632 | c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval); |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 1633 | if (c == 0) |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 1634 | c = memcmp(a->ob_sval, b->ob_sval, min_len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1635 | } |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 1636 | else |
| 1637 | c = 0; |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 1638 | if (c != 0) |
| 1639 | Py_RETURN_RICHCOMPARE(c, 0, op); |
| 1640 | Py_RETURN_RICHCOMPARE(len_a, len_b, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1641 | } |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1644 | static Py_hash_t |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1645 | bytes_hash(PyBytesObject *a) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1646 | { |
Antoine Pitrou | ce4a9da | 2011-11-21 20:46:33 +0100 | [diff] [blame] | 1647 | if (a->ob_shash == -1) { |
| 1648 | /* Can't fail */ |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 1649 | a->ob_shash = _Py_HashBytes(a->ob_sval, Py_SIZE(a)); |
Antoine Pitrou | ce4a9da | 2011-11-21 20:46:33 +0100 | [diff] [blame] | 1650 | } |
| 1651 | return a->ob_shash; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1654 | static PyObject* |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1655 | bytes_subscript(PyBytesObject* self, PyObject* item) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1656 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1657 | if (PyIndex_Check(item)) { |
| 1658 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 1659 | if (i == -1 && PyErr_Occurred()) |
| 1660 | return NULL; |
| 1661 | if (i < 0) |
| 1662 | i += PyBytes_GET_SIZE(self); |
| 1663 | if (i < 0 || i >= PyBytes_GET_SIZE(self)) { |
| 1664 | PyErr_SetString(PyExc_IndexError, |
| 1665 | "index out of range"); |
| 1666 | return NULL; |
| 1667 | } |
| 1668 | return PyLong_FromLong((unsigned char)self->ob_sval[i]); |
| 1669 | } |
| 1670 | else if (PySlice_Check(item)) { |
| 1671 | Py_ssize_t start, stop, step, slicelength, cur, i; |
| 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 | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 2419 | PyDoc_STRVAR(hex__doc__, |
| 2420 | "B.hex() -> string\n\ |
| 2421 | \n\ |
| 2422 | Create a string of hexadecimal numbers from a bytes object.\n\ |
| 2423 | Example: b'\\xb9\\x01\\xef'.hex() -> 'b901ef'."); |
| 2424 | |
| 2425 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2426 | bytes_hex(PyBytesObject *self, PyObject *Py_UNUSED(ignored)) |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2427 | { |
| 2428 | char* argbuf = PyBytes_AS_STRING(self); |
| 2429 | Py_ssize_t arglen = PyBytes_GET_SIZE(self); |
| 2430 | return _Py_strhex(argbuf, arglen); |
| 2431 | } |
| 2432 | |
| 2433 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2434 | bytes_getnewargs(PyBytesObject *v, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | 0dd32e2 | 2007-04-11 05:40:58 +0000 | [diff] [blame] | 2435 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2436 | return Py_BuildValue("(y#)", v->ob_sval, Py_SIZE(v)); |
Guido van Rossum | 0dd32e2 | 2007-04-11 05:40:58 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 2439 | |
| 2440 | static PyMethodDef |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2441 | bytes_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2442 | {"__getnewargs__", (PyCFunction)bytes_getnewargs, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2443 | {"capitalize", stringlib_capitalize, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2444 | _Py_capitalize__doc__}, |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 2445 | STRINGLIB_CENTER_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2446 | {"count", (PyCFunction)bytes_count, METH_VARARGS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2447 | _Py_count__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2448 | BYTES_DECODE_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2449 | {"endswith", (PyCFunction)bytes_endswith, METH_VARARGS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2450 | _Py_endswith__doc__}, |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 2451 | STRINGLIB_EXPANDTABS_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2452 | {"find", (PyCFunction)bytes_find, METH_VARARGS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2453 | _Py_find__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2454 | BYTES_FROMHEX_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2455 | {"hex", (PyCFunction)bytes_hex, METH_NOARGS, hex__doc__}, |
| 2456 | {"index", (PyCFunction)bytes_index, METH_VARARGS, _Py_index__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2457 | {"isalnum", stringlib_isalnum, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2458 | _Py_isalnum__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2459 | {"isalpha", stringlib_isalpha, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2460 | _Py_isalpha__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2461 | {"isascii", stringlib_isascii, METH_NOARGS, |
INADA Naoki | a49ac99 | 2018-01-27 14:06:21 +0900 | [diff] [blame] | 2462 | _Py_isascii__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2463 | {"isdigit", stringlib_isdigit, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2464 | _Py_isdigit__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2465 | {"islower", stringlib_islower, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2466 | _Py_islower__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2467 | {"isspace", stringlib_isspace, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2468 | _Py_isspace__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2469 | {"istitle", stringlib_istitle, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2470 | _Py_istitle__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2471 | {"isupper", stringlib_isupper, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2472 | _Py_isupper__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2473 | BYTES_JOIN_METHODDEF |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 2474 | STRINGLIB_LJUST_METHODDEF |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2475 | {"lower", stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2476 | BYTES_LSTRIP_METHODDEF |
| 2477 | BYTES_MAKETRANS_METHODDEF |
| 2478 | BYTES_PARTITION_METHODDEF |
| 2479 | BYTES_REPLACE_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2480 | {"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, _Py_rfind__doc__}, |
| 2481 | {"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, _Py_rindex__doc__}, |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 2482 | STRINGLIB_RJUST_METHODDEF |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2483 | BYTES_RPARTITION_METHODDEF |
| 2484 | BYTES_RSPLIT_METHODDEF |
| 2485 | BYTES_RSTRIP_METHODDEF |
| 2486 | BYTES_SPLIT_METHODDEF |
| 2487 | BYTES_SPLITLINES_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2488 | {"startswith", (PyCFunction)bytes_startswith, METH_VARARGS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2489 | _Py_startswith__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2490 | BYTES_STRIP_METHODDEF |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2491 | {"swapcase", stringlib_swapcase, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2492 | _Py_swapcase__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2493 | {"title", stringlib_title, METH_NOARGS, _Py_title__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2494 | BYTES_TRANSLATE_METHODDEF |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2495 | {"upper", stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 2496 | STRINGLIB_ZFILL_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2497 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 2498 | }; |
| 2499 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2500 | static PyObject * |
Serhiy Storchaka | c9a59e6 | 2016-04-15 14:11:10 +0300 | [diff] [blame] | 2501 | bytes_mod(PyObject *self, PyObject *arg) |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 2502 | { |
Serhiy Storchaka | c9a59e6 | 2016-04-15 14:11:10 +0300 | [diff] [blame] | 2503 | if (!PyBytes_Check(self)) { |
| 2504 | Py_RETURN_NOTIMPLEMENTED; |
Victor Stinner | 772b2b0 | 2015-10-14 09:56:53 +0200 | [diff] [blame] | 2505 | } |
Victor Stinner | 772b2b0 | 2015-10-14 09:56:53 +0200 | [diff] [blame] | 2506 | return _PyBytes_FormatEx(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
Serhiy Storchaka | c9a59e6 | 2016-04-15 14:11:10 +0300 | [diff] [blame] | 2507 | arg, 0); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | static PyNumberMethods bytes_as_number = { |
| 2511 | 0, /*nb_add*/ |
| 2512 | 0, /*nb_subtract*/ |
| 2513 | 0, /*nb_multiply*/ |
| 2514 | bytes_mod, /*nb_remainder*/ |
| 2515 | }; |
| 2516 | |
| 2517 | static PyObject * |
Serhiy Storchaka | 1509580 | 2015-11-25 15:47:01 +0200 | [diff] [blame] | 2518 | bytes_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2519 | |
| 2520 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2521 | bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2522 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2523 | PyObject *x = NULL; |
| 2524 | const char *encoding = NULL; |
| 2525 | const char *errors = NULL; |
| 2526 | PyObject *new = NULL; |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 2527 | PyObject *func; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2528 | Py_ssize_t size; |
| 2529 | static char *kwlist[] = {"source", "encoding", "errors", 0}; |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 2530 | _Py_IDENTIFIER(__bytes__); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2531 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2532 | if (type != &PyBytes_Type) |
Serhiy Storchaka | 1509580 | 2015-11-25 15:47:01 +0200 | [diff] [blame] | 2533 | return bytes_subtype_new(type, args, kwds); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2534 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytes", kwlist, &x, |
| 2535 | &encoding, &errors)) |
| 2536 | return NULL; |
| 2537 | if (x == NULL) { |
| 2538 | if (encoding != NULL || errors != NULL) { |
| 2539 | PyErr_SetString(PyExc_TypeError, |
Serhiy Storchaka | 2c2044e | 2018-10-21 15:29:12 +0300 | [diff] [blame] | 2540 | encoding != NULL ? |
| 2541 | "encoding without a string argument" : |
| 2542 | "errors without a string argument"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2543 | return NULL; |
| 2544 | } |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 2545 | return PyBytes_FromStringAndSize(NULL, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2546 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2547 | |
Serhiy Storchaka | 5aac3ed | 2015-12-20 16:36:34 +0200 | [diff] [blame] | 2548 | if (encoding != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2549 | /* Encode via the codec registry */ |
Serhiy Storchaka | 5aac3ed | 2015-12-20 16:36:34 +0200 | [diff] [blame] | 2550 | if (!PyUnicode_Check(x)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2551 | PyErr_SetString(PyExc_TypeError, |
Serhiy Storchaka | 5aac3ed | 2015-12-20 16:36:34 +0200 | [diff] [blame] | 2552 | "encoding without a string argument"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2553 | return NULL; |
| 2554 | } |
| 2555 | new = PyUnicode_AsEncodedString(x, encoding, errors); |
| 2556 | if (new == NULL) |
| 2557 | return NULL; |
| 2558 | assert(PyBytes_Check(new)); |
| 2559 | return new; |
| 2560 | } |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 2561 | |
Serhiy Storchaka | 5aac3ed | 2015-12-20 16:36:34 +0200 | [diff] [blame] | 2562 | if (errors != NULL) { |
Serhiy Storchaka | 83cf99d | 2014-12-02 09:24:06 +0200 | [diff] [blame] | 2563 | PyErr_SetString(PyExc_TypeError, |
Serhiy Storchaka | 5aac3ed | 2015-12-20 16:36:34 +0200 | [diff] [blame] | 2564 | PyUnicode_Check(x) ? |
| 2565 | "string argument without an encoding" : |
| 2566 | "errors without a string argument"); |
Serhiy Storchaka | 83cf99d | 2014-12-02 09:24:06 +0200 | [diff] [blame] | 2567 | return NULL; |
| 2568 | } |
| 2569 | |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 2570 | /* We'd like to call PyObject_Bytes here, but we need to check for an |
| 2571 | integer argument before deferring to PyBytes_FromObject, something |
| 2572 | PyObject_Bytes doesn't do. */ |
| 2573 | func = _PyObject_LookupSpecial(x, &PyId___bytes__); |
| 2574 | if (func != NULL) { |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 2575 | new = _PyObject_CallNoArg(func); |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 2576 | Py_DECREF(func); |
| 2577 | if (new == NULL) |
| 2578 | return NULL; |
| 2579 | if (!PyBytes_Check(new)) { |
| 2580 | PyErr_Format(PyExc_TypeError, |
| 2581 | "__bytes__ returned non-bytes (type %.200s)", |
| 2582 | Py_TYPE(new)->tp_name); |
| 2583 | Py_DECREF(new); |
| 2584 | return NULL; |
| 2585 | } |
| 2586 | return new; |
| 2587 | } |
| 2588 | else if (PyErr_Occurred()) |
| 2589 | return NULL; |
| 2590 | |
Serhiy Storchaka | 5aac3ed | 2015-12-20 16:36:34 +0200 | [diff] [blame] | 2591 | if (PyUnicode_Check(x)) { |
| 2592 | PyErr_SetString(PyExc_TypeError, |
| 2593 | "string argument without an encoding"); |
| 2594 | return NULL; |
| 2595 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2596 | /* Is it an integer? */ |
Serhiy Storchaka | eb24988 | 2016-08-15 09:46:07 +0300 | [diff] [blame] | 2597 | if (PyIndex_Check(x)) { |
| 2598 | size = PyNumber_AsSsize_t(x, PyExc_OverflowError); |
| 2599 | if (size == -1 && PyErr_Occurred()) { |
Serhiy Storchaka | e890421 | 2018-10-15 00:02:57 +0300 | [diff] [blame] | 2600 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
INADA Naoki | a634e23 | 2017-01-06 17:32:01 +0900 | [diff] [blame] | 2601 | return NULL; |
| 2602 | PyErr_Clear(); /* fall through */ |
Serhiy Storchaka | eb24988 | 2016-08-15 09:46:07 +0300 | [diff] [blame] | 2603 | } |
INADA Naoki | a634e23 | 2017-01-06 17:32:01 +0900 | [diff] [blame] | 2604 | else { |
| 2605 | if (size < 0) { |
| 2606 | PyErr_SetString(PyExc_ValueError, "negative count"); |
| 2607 | return NULL; |
| 2608 | } |
| 2609 | new = _PyBytes_FromSize(size, 1); |
| 2610 | if (new == NULL) |
| 2611 | return NULL; |
| 2612 | return new; |
Serhiy Storchaka | eb24988 | 2016-08-15 09:46:07 +0300 | [diff] [blame] | 2613 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2614 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2615 | |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 2616 | return PyBytes_FromObject(x); |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
Victor Stinner | f2eafa3 | 2015-10-14 13:44:29 +0200 | [diff] [blame] | 2619 | static PyObject* |
| 2620 | _PyBytes_FromBuffer(PyObject *x) |
| 2621 | { |
| 2622 | PyObject *new; |
| 2623 | Py_buffer view; |
| 2624 | |
| 2625 | if (PyObject_GetBuffer(x, &view, PyBUF_FULL_RO) < 0) |
| 2626 | return NULL; |
| 2627 | |
| 2628 | new = PyBytes_FromStringAndSize(NULL, view.len); |
| 2629 | if (!new) |
| 2630 | goto fail; |
| 2631 | if (PyBuffer_ToContiguous(((PyBytesObject *)new)->ob_sval, |
| 2632 | &view, view.len, 'C') < 0) |
| 2633 | goto fail; |
| 2634 | PyBuffer_Release(&view); |
| 2635 | return new; |
| 2636 | |
| 2637 | fail: |
| 2638 | Py_XDECREF(new); |
| 2639 | PyBuffer_Release(&view); |
| 2640 | return NULL; |
| 2641 | } |
| 2642 | |
| 2643 | static PyObject* |
| 2644 | _PyBytes_FromList(PyObject *x) |
| 2645 | { |
Serhiy Storchaka | 914f9a0 | 2018-10-21 15:25:53 +0300 | [diff] [blame] | 2646 | Py_ssize_t i, size = PyList_GET_SIZE(x); |
| 2647 | Py_ssize_t value; |
| 2648 | char *str; |
| 2649 | PyObject *item; |
| 2650 | _PyBytesWriter writer; |
| 2651 | |
| 2652 | _PyBytesWriter_Init(&writer); |
| 2653 | str = _PyBytesWriter_Alloc(&writer, size); |
| 2654 | if (str == NULL) |
| 2655 | return NULL; |
| 2656 | writer.overallocate = 1; |
| 2657 | size = writer.allocated; |
| 2658 | |
| 2659 | for (i = 0; i < PyList_GET_SIZE(x); i++) { |
| 2660 | item = PyList_GET_ITEM(x, i); |
| 2661 | Py_INCREF(item); |
| 2662 | value = PyNumber_AsSsize_t(item, NULL); |
| 2663 | Py_DECREF(item); |
| 2664 | if (value == -1 && PyErr_Occurred()) |
| 2665 | goto error; |
| 2666 | |
| 2667 | if (value < 0 || value >= 256) { |
| 2668 | PyErr_SetString(PyExc_ValueError, |
| 2669 | "bytes must be in range(0, 256)"); |
| 2670 | goto error; |
| 2671 | } |
| 2672 | |
| 2673 | if (i >= size) { |
| 2674 | str = _PyBytesWriter_Resize(&writer, str, size+1); |
| 2675 | if (str == NULL) |
| 2676 | return NULL; |
| 2677 | size = writer.allocated; |
| 2678 | } |
| 2679 | *str++ = (char) value; |
| 2680 | } |
| 2681 | return _PyBytesWriter_Finish(&writer, str); |
| 2682 | |
| 2683 | error: |
| 2684 | _PyBytesWriter_Dealloc(&writer); |
| 2685 | return NULL; |
Victor Stinner | f2eafa3 | 2015-10-14 13:44:29 +0200 | [diff] [blame] | 2686 | } |
| 2687 | |
| 2688 | static PyObject* |
| 2689 | _PyBytes_FromTuple(PyObject *x) |
| 2690 | { |
Serhiy Storchaka | 914f9a0 | 2018-10-21 15:25:53 +0300 | [diff] [blame] | 2691 | PyObject *bytes; |
| 2692 | Py_ssize_t i, size = PyTuple_GET_SIZE(x); |
| 2693 | Py_ssize_t value; |
| 2694 | char *str; |
| 2695 | PyObject *item; |
| 2696 | |
| 2697 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 2698 | if (bytes == NULL) |
| 2699 | return NULL; |
| 2700 | str = ((PyBytesObject *)bytes)->ob_sval; |
| 2701 | |
| 2702 | for (i = 0; i < size; i++) { |
| 2703 | item = PyTuple_GET_ITEM(x, i); |
| 2704 | value = PyNumber_AsSsize_t(item, NULL); |
| 2705 | if (value == -1 && PyErr_Occurred()) |
| 2706 | goto error; |
| 2707 | |
| 2708 | if (value < 0 || value >= 256) { |
| 2709 | PyErr_SetString(PyExc_ValueError, |
| 2710 | "bytes must be in range(0, 256)"); |
| 2711 | goto error; |
| 2712 | } |
| 2713 | *str++ = (char) value; |
| 2714 | } |
| 2715 | return bytes; |
| 2716 | |
| 2717 | error: |
| 2718 | Py_DECREF(bytes); |
| 2719 | return NULL; |
Victor Stinner | f2eafa3 | 2015-10-14 13:44:29 +0200 | [diff] [blame] | 2720 | } |
| 2721 | |
| 2722 | static PyObject * |
Serhiy Storchaka | 03f17f8 | 2016-04-10 14:44:59 +0300 | [diff] [blame] | 2723 | _PyBytes_FromIterator(PyObject *it, PyObject *x) |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 2724 | { |
Victor Stinner | c3d2bc1 | 2015-10-14 14:15:49 +0200 | [diff] [blame] | 2725 | char *str; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2726 | Py_ssize_t i, size; |
Victor Stinner | c3d2bc1 | 2015-10-14 14:15:49 +0200 | [diff] [blame] | 2727 | _PyBytesWriter writer; |
| 2728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2729 | /* For iterator version, create a string object and resize as needed */ |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 2730 | size = PyObject_LengthHint(x, 64); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2731 | if (size == -1 && PyErr_Occurred()) |
| 2732 | return NULL; |
Victor Stinner | c3d2bc1 | 2015-10-14 14:15:49 +0200 | [diff] [blame] | 2733 | |
Serhiy Storchaka | 03f17f8 | 2016-04-10 14:44:59 +0300 | [diff] [blame] | 2734 | _PyBytesWriter_Init(&writer); |
Victor Stinner | c3d2bc1 | 2015-10-14 14:15:49 +0200 | [diff] [blame] | 2735 | str = _PyBytesWriter_Alloc(&writer, size); |
| 2736 | if (str == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2737 | return NULL; |
Victor Stinner | c3d2bc1 | 2015-10-14 14:15:49 +0200 | [diff] [blame] | 2738 | writer.overallocate = 1; |
| 2739 | size = writer.allocated; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2741 | /* Run the iterator to exhaustion */ |
| 2742 | for (i = 0; ; i++) { |
| 2743 | PyObject *item; |
| 2744 | Py_ssize_t value; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2745 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2746 | /* Get the next item */ |
| 2747 | item = PyIter_Next(it); |
| 2748 | if (item == NULL) { |
| 2749 | if (PyErr_Occurred()) |
| 2750 | goto error; |
| 2751 | break; |
| 2752 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2754 | /* Interpret it as an int (__index__) */ |
Serhiy Storchaka | f54d781 | 2016-07-06 21:39:44 +0300 | [diff] [blame] | 2755 | value = PyNumber_AsSsize_t(item, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2756 | Py_DECREF(item); |
| 2757 | if (value == -1 && PyErr_Occurred()) |
| 2758 | goto error; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2759 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2760 | /* Range check */ |
| 2761 | if (value < 0 || value >= 256) { |
| 2762 | PyErr_SetString(PyExc_ValueError, |
| 2763 | "bytes must be in range(0, 256)"); |
| 2764 | goto error; |
| 2765 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2766 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2767 | /* Append the byte */ |
| 2768 | if (i >= size) { |
Victor Stinner | c3d2bc1 | 2015-10-14 14:15:49 +0200 | [diff] [blame] | 2769 | str = _PyBytesWriter_Resize(&writer, str, size+1); |
| 2770 | if (str == NULL) |
| 2771 | return NULL; |
| 2772 | size = writer.allocated; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2773 | } |
Victor Stinner | c3d2bc1 | 2015-10-14 14:15:49 +0200 | [diff] [blame] | 2774 | *str++ = (char) value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2775 | } |
Victor Stinner | c3d2bc1 | 2015-10-14 14:15:49 +0200 | [diff] [blame] | 2776 | |
| 2777 | return _PyBytesWriter_Finish(&writer, str); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2778 | |
| 2779 | error: |
Victor Stinner | c3d2bc1 | 2015-10-14 14:15:49 +0200 | [diff] [blame] | 2780 | _PyBytesWriter_Dealloc(&writer); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2781 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
Victor Stinner | f2eafa3 | 2015-10-14 13:44:29 +0200 | [diff] [blame] | 2784 | PyObject * |
| 2785 | PyBytes_FromObject(PyObject *x) |
| 2786 | { |
Serhiy Storchaka | 03f17f8 | 2016-04-10 14:44:59 +0300 | [diff] [blame] | 2787 | PyObject *it, *result; |
| 2788 | |
Victor Stinner | f2eafa3 | 2015-10-14 13:44:29 +0200 | [diff] [blame] | 2789 | if (x == NULL) { |
| 2790 | PyErr_BadInternalCall(); |
| 2791 | return NULL; |
| 2792 | } |
| 2793 | |
| 2794 | if (PyBytes_CheckExact(x)) { |
| 2795 | Py_INCREF(x); |
| 2796 | return x; |
| 2797 | } |
| 2798 | |
| 2799 | /* Use the modern buffer interface */ |
| 2800 | if (PyObject_CheckBuffer(x)) |
| 2801 | return _PyBytes_FromBuffer(x); |
| 2802 | |
| 2803 | if (PyList_CheckExact(x)) |
| 2804 | return _PyBytes_FromList(x); |
| 2805 | |
| 2806 | if (PyTuple_CheckExact(x)) |
| 2807 | return _PyBytes_FromTuple(x); |
| 2808 | |
Serhiy Storchaka | 03f17f8 | 2016-04-10 14:44:59 +0300 | [diff] [blame] | 2809 | if (!PyUnicode_Check(x)) { |
| 2810 | it = PyObject_GetIter(x); |
| 2811 | if (it != NULL) { |
| 2812 | result = _PyBytes_FromIterator(it, x); |
| 2813 | Py_DECREF(it); |
| 2814 | return result; |
| 2815 | } |
Serhiy Storchaka | e890421 | 2018-10-15 00:02:57 +0300 | [diff] [blame] | 2816 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 2817 | return NULL; |
| 2818 | } |
Victor Stinner | f2eafa3 | 2015-10-14 13:44:29 +0200 | [diff] [blame] | 2819 | } |
| 2820 | |
Serhiy Storchaka | 03f17f8 | 2016-04-10 14:44:59 +0300 | [diff] [blame] | 2821 | PyErr_Format(PyExc_TypeError, |
| 2822 | "cannot convert '%.200s' object to bytes", |
| 2823 | x->ob_type->tp_name); |
| 2824 | return NULL; |
Victor Stinner | f2eafa3 | 2015-10-14 13:44:29 +0200 | [diff] [blame] | 2825 | } |
| 2826 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2827 | static PyObject * |
Serhiy Storchaka | 1509580 | 2015-11-25 15:47:01 +0200 | [diff] [blame] | 2828 | bytes_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2829 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | PyObject *tmp, *pnew; |
| 2831 | Py_ssize_t n; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2833 | assert(PyType_IsSubtype(type, &PyBytes_Type)); |
| 2834 | tmp = bytes_new(&PyBytes_Type, args, kwds); |
| 2835 | if (tmp == NULL) |
| 2836 | return NULL; |
Serhiy Storchaka | 1509580 | 2015-11-25 15:47:01 +0200 | [diff] [blame] | 2837 | assert(PyBytes_Check(tmp)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2838 | n = PyBytes_GET_SIZE(tmp); |
| 2839 | pnew = type->tp_alloc(type, n); |
| 2840 | if (pnew != NULL) { |
Christian Heimes | f051e43 | 2016-09-13 20:22:02 +0200 | [diff] [blame] | 2841 | memcpy(PyBytes_AS_STRING(pnew), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2842 | PyBytes_AS_STRING(tmp), n+1); |
| 2843 | ((PyBytesObject *)pnew)->ob_shash = |
| 2844 | ((PyBytesObject *)tmp)->ob_shash; |
| 2845 | } |
| 2846 | Py_DECREF(tmp); |
| 2847 | return pnew; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2850 | PyDoc_STRVAR(bytes_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2851 | "bytes(iterable_of_ints) -> bytes\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2852 | bytes(string, encoding[, errors]) -> bytes\n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2853 | bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2854 | bytes(int) -> bytes object of size given by the parameter initialized with null bytes\n\ |
| 2855 | bytes() -> empty bytes object\n\ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 2856 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2857 | Construct an immutable array of bytes from:\n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2858 | - an iterable yielding integers in range(256)\n\ |
| 2859 | - a text string encoded using the specified encoding\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2860 | - any object implementing the buffer API.\n\ |
| 2861 | - an integer"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2862 | |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2863 | static PyObject *bytes_iter(PyObject *seq); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 2864 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2865 | PyTypeObject PyBytes_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2866 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2867 | "bytes", |
| 2868 | PyBytesObject_SIZE, |
| 2869 | sizeof(char), |
| 2870 | bytes_dealloc, /* tp_dealloc */ |
| 2871 | 0, /* tp_print */ |
| 2872 | 0, /* tp_getattr */ |
| 2873 | 0, /* tp_setattr */ |
| 2874 | 0, /* tp_reserved */ |
| 2875 | (reprfunc)bytes_repr, /* tp_repr */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 2876 | &bytes_as_number, /* tp_as_number */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2877 | &bytes_as_sequence, /* tp_as_sequence */ |
| 2878 | &bytes_as_mapping, /* tp_as_mapping */ |
| 2879 | (hashfunc)bytes_hash, /* tp_hash */ |
| 2880 | 0, /* tp_call */ |
| 2881 | bytes_str, /* tp_str */ |
| 2882 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2883 | 0, /* tp_setattro */ |
| 2884 | &bytes_as_buffer, /* tp_as_buffer */ |
| 2885 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 2886 | Py_TPFLAGS_BYTES_SUBCLASS, /* tp_flags */ |
| 2887 | bytes_doc, /* tp_doc */ |
| 2888 | 0, /* tp_traverse */ |
| 2889 | 0, /* tp_clear */ |
| 2890 | (richcmpfunc)bytes_richcompare, /* tp_richcompare */ |
| 2891 | 0, /* tp_weaklistoffset */ |
| 2892 | bytes_iter, /* tp_iter */ |
| 2893 | 0, /* tp_iternext */ |
| 2894 | bytes_methods, /* tp_methods */ |
| 2895 | 0, /* tp_members */ |
| 2896 | 0, /* tp_getset */ |
| 2897 | &PyBaseObject_Type, /* tp_base */ |
| 2898 | 0, /* tp_dict */ |
| 2899 | 0, /* tp_descr_get */ |
| 2900 | 0, /* tp_descr_set */ |
| 2901 | 0, /* tp_dictoffset */ |
| 2902 | 0, /* tp_init */ |
| 2903 | 0, /* tp_alloc */ |
| 2904 | bytes_new, /* tp_new */ |
| 2905 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 2906 | }; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2907 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2908 | void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2909 | PyBytes_Concat(PyObject **pv, PyObject *w) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2910 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2911 | assert(pv != NULL); |
| 2912 | if (*pv == NULL) |
| 2913 | return; |
| 2914 | if (w == NULL) { |
Serhiy Storchaka | f458a03 | 2013-02-02 18:45:22 +0200 | [diff] [blame] | 2915 | Py_CLEAR(*pv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2916 | return; |
| 2917 | } |
Antoine Pitrou | 161d695 | 2014-05-01 14:36:20 +0200 | [diff] [blame] | 2918 | |
| 2919 | if (Py_REFCNT(*pv) == 1 && PyBytes_CheckExact(*pv)) { |
| 2920 | /* Only one reference, so we can resize in place */ |
Zachary Ware | bca9694 | 2014-05-06 11:42:37 -0500 | [diff] [blame] | 2921 | Py_ssize_t oldsize; |
Antoine Pitrou | 161d695 | 2014-05-01 14:36:20 +0200 | [diff] [blame] | 2922 | Py_buffer wb; |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 2923 | |
Antoine Pitrou | 161d695 | 2014-05-01 14:36:20 +0200 | [diff] [blame] | 2924 | wb.len = -1; |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2925 | if (PyObject_GetBuffer(w, &wb, PyBUF_SIMPLE) != 0) { |
Antoine Pitrou | 161d695 | 2014-05-01 14:36:20 +0200 | [diff] [blame] | 2926 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 2927 | Py_TYPE(w)->tp_name, Py_TYPE(*pv)->tp_name); |
| 2928 | Py_CLEAR(*pv); |
| 2929 | return; |
| 2930 | } |
| 2931 | |
| 2932 | oldsize = PyBytes_GET_SIZE(*pv); |
| 2933 | if (oldsize > PY_SSIZE_T_MAX - wb.len) { |
| 2934 | PyErr_NoMemory(); |
| 2935 | goto error; |
| 2936 | } |
| 2937 | if (_PyBytes_Resize(pv, oldsize + wb.len) < 0) |
| 2938 | goto error; |
| 2939 | |
| 2940 | memcpy(PyBytes_AS_STRING(*pv) + oldsize, wb.buf, wb.len); |
| 2941 | PyBuffer_Release(&wb); |
| 2942 | return; |
| 2943 | |
| 2944 | error: |
| 2945 | PyBuffer_Release(&wb); |
| 2946 | Py_CLEAR(*pv); |
| 2947 | return; |
| 2948 | } |
| 2949 | |
| 2950 | else { |
| 2951 | /* Multiple references, need to create new object */ |
| 2952 | PyObject *v; |
| 2953 | v = bytes_concat(*pv, w); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 2954 | Py_SETREF(*pv, v); |
Antoine Pitrou | 161d695 | 2014-05-01 14:36:20 +0200 | [diff] [blame] | 2955 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2956 | } |
| 2957 | |
| 2958 | void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2959 | PyBytes_ConcatAndDel(PyObject **pv, PyObject *w) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2960 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2961 | PyBytes_Concat(pv, w); |
| 2962 | Py_XDECREF(w); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
| 2965 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 2966 | /* The following function breaks the notion that bytes are immutable: |
| 2967 | 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] | 2968 | 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] | 2969 | as creating a new bytes object and destroying the old one, only |
| 2970 | 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] | 2971 | already be known to some other part of the code... |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 2972 | Note that if there's not enough memory to resize the bytes object, the |
| 2973 | 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] | 2974 | memory" exception is set, and -1 is returned. Else (on success) 0 is |
| 2975 | returned, and the value in *pv may or may not be the same as on input. |
| 2976 | As always, an extra byte is allocated for a trailing \0 byte (newsize |
| 2977 | does *not* include that), and a trailing \0 byte is stored. |
| 2978 | */ |
| 2979 | |
| 2980 | int |
| 2981 | _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize) |
| 2982 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2983 | PyObject *v; |
| 2984 | PyBytesObject *sv; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2985 | v = *pv; |
Berker Peksag | 4a72a7b | 2016-09-16 17:31:06 +0300 | [diff] [blame] | 2986 | if (!PyBytes_Check(v) || newsize < 0) { |
| 2987 | goto error; |
| 2988 | } |
| 2989 | if (Py_SIZE(v) == newsize) { |
| 2990 | /* return early if newsize equals to v->ob_size */ |
| 2991 | return 0; |
| 2992 | } |
| 2993 | if (Py_REFCNT(v) != 1) { |
| 2994 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2995 | } |
| 2996 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 2997 | _Py_DEC_REFTOTAL; |
| 2998 | _Py_ForgetReference(v); |
| 2999 | *pv = (PyObject *) |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 3000 | PyObject_REALLOC(v, PyBytesObject_SIZE + newsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3001 | if (*pv == NULL) { |
| 3002 | PyObject_Del(v); |
| 3003 | PyErr_NoMemory(); |
| 3004 | return -1; |
| 3005 | } |
| 3006 | _Py_NewReference(*pv); |
| 3007 | sv = (PyBytesObject *) *pv; |
| 3008 | Py_SIZE(sv) = newsize; |
| 3009 | sv->ob_sval[newsize] = '\0'; |
| 3010 | sv->ob_shash = -1; /* invalidate cached hash value */ |
| 3011 | return 0; |
Berker Peksag | 4a72a7b | 2016-09-16 17:31:06 +0300 | [diff] [blame] | 3012 | error: |
| 3013 | *pv = 0; |
| 3014 | Py_DECREF(v); |
| 3015 | PyErr_BadInternalCall(); |
| 3016 | return -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3019 | void |
| 3020 | PyBytes_Fini(void) |
| 3021 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3022 | int i; |
Serhiy Storchaka | f458a03 | 2013-02-02 18:45:22 +0200 | [diff] [blame] | 3023 | for (i = 0; i < UCHAR_MAX + 1; i++) |
| 3024 | Py_CLEAR(characters[i]); |
| 3025 | Py_CLEAR(nullstring); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
Benjamin Peterson | 4116f36 | 2008-05-27 00:36:20 +0000 | [diff] [blame] | 3028 | /*********************** Bytes Iterator ****************************/ |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3029 | |
| 3030 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3031 | PyObject_HEAD |
| 3032 | Py_ssize_t it_index; |
| 3033 | PyBytesObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3034 | } striterobject; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3035 | |
| 3036 | static void |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3037 | striter_dealloc(striterobject *it) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3038 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3039 | _PyObject_GC_UNTRACK(it); |
| 3040 | Py_XDECREF(it->it_seq); |
| 3041 | PyObject_GC_Del(it); |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | static int |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3045 | striter_traverse(striterobject *it, visitproc visit, void *arg) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3046 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3047 | Py_VISIT(it->it_seq); |
| 3048 | return 0; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
| 3051 | static PyObject * |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3052 | striter_next(striterobject *it) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3053 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3054 | PyBytesObject *seq; |
| 3055 | PyObject *item; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3056 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3057 | assert(it != NULL); |
| 3058 | seq = it->it_seq; |
| 3059 | if (seq == NULL) |
| 3060 | return NULL; |
| 3061 | assert(PyBytes_Check(seq)); |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3063 | if (it->it_index < PyBytes_GET_SIZE(seq)) { |
| 3064 | item = PyLong_FromLong( |
| 3065 | (unsigned char)seq->ob_sval[it->it_index]); |
| 3066 | if (item != NULL) |
| 3067 | ++it->it_index; |
| 3068 | return item; |
| 3069 | } |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3070 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3071 | it->it_seq = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 3072 | Py_DECREF(seq); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3073 | return NULL; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3074 | } |
| 3075 | |
| 3076 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3077 | striter_len(striterobject *it, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3078 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3079 | Py_ssize_t len = 0; |
| 3080 | if (it->it_seq) |
| 3081 | len = PyBytes_GET_SIZE(it->it_seq) - it->it_index; |
| 3082 | return PyLong_FromSsize_t(len); |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3083 | } |
| 3084 | |
| 3085 | PyDoc_STRVAR(length_hint_doc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3086 | "Private method returning an estimate of len(list(it))."); |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3087 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3088 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3089 | striter_reduce(striterobject *it, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3090 | { |
| 3091 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 3092 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3093 | it->it_seq, it->it_index); |
| 3094 | } else { |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 3095 | return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter")); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3096 | } |
| 3097 | } |
| 3098 | |
| 3099 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 3100 | |
| 3101 | static PyObject * |
| 3102 | striter_setstate(striterobject *it, PyObject *state) |
| 3103 | { |
| 3104 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 3105 | if (index == -1 && PyErr_Occurred()) |
| 3106 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 3107 | if (it->it_seq != NULL) { |
| 3108 | if (index < 0) |
| 3109 | index = 0; |
| 3110 | else if (index > PyBytes_GET_SIZE(it->it_seq)) |
| 3111 | index = PyBytes_GET_SIZE(it->it_seq); /* iterator exhausted */ |
| 3112 | it->it_index = index; |
| 3113 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3114 | Py_RETURN_NONE; |
| 3115 | } |
| 3116 | |
| 3117 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 3118 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3119 | static PyMethodDef striter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3120 | {"__length_hint__", (PyCFunction)striter_len, METH_NOARGS, |
| 3121 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3122 | {"__reduce__", (PyCFunction)striter_reduce, METH_NOARGS, |
| 3123 | reduce_doc}, |
| 3124 | {"__setstate__", (PyCFunction)striter_setstate, METH_O, |
| 3125 | setstate_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3126 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3127 | }; |
| 3128 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3129 | PyTypeObject PyBytesIter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3130 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3131 | "bytes_iterator", /* tp_name */ |
| 3132 | sizeof(striterobject), /* tp_basicsize */ |
| 3133 | 0, /* tp_itemsize */ |
| 3134 | /* methods */ |
| 3135 | (destructor)striter_dealloc, /* tp_dealloc */ |
| 3136 | 0, /* tp_print */ |
| 3137 | 0, /* tp_getattr */ |
| 3138 | 0, /* tp_setattr */ |
| 3139 | 0, /* tp_reserved */ |
| 3140 | 0, /* tp_repr */ |
| 3141 | 0, /* tp_as_number */ |
| 3142 | 0, /* tp_as_sequence */ |
| 3143 | 0, /* tp_as_mapping */ |
| 3144 | 0, /* tp_hash */ |
| 3145 | 0, /* tp_call */ |
| 3146 | 0, /* tp_str */ |
| 3147 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3148 | 0, /* tp_setattro */ |
| 3149 | 0, /* tp_as_buffer */ |
| 3150 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 3151 | 0, /* tp_doc */ |
| 3152 | (traverseproc)striter_traverse, /* tp_traverse */ |
| 3153 | 0, /* tp_clear */ |
| 3154 | 0, /* tp_richcompare */ |
| 3155 | 0, /* tp_weaklistoffset */ |
| 3156 | PyObject_SelfIter, /* tp_iter */ |
| 3157 | (iternextfunc)striter_next, /* tp_iternext */ |
| 3158 | striter_methods, /* tp_methods */ |
| 3159 | 0, |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3160 | }; |
| 3161 | |
| 3162 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 3163 | bytes_iter(PyObject *seq) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3164 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3165 | striterobject *it; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3167 | if (!PyBytes_Check(seq)) { |
| 3168 | PyErr_BadInternalCall(); |
| 3169 | return NULL; |
| 3170 | } |
| 3171 | it = PyObject_GC_New(striterobject, &PyBytesIter_Type); |
| 3172 | if (it == NULL) |
| 3173 | return NULL; |
| 3174 | it->it_index = 0; |
| 3175 | Py_INCREF(seq); |
| 3176 | it->it_seq = (PyBytesObject *)seq; |
| 3177 | _PyObject_GC_TRACK(it); |
| 3178 | return (PyObject *)it; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3179 | } |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3180 | |
| 3181 | |
| 3182 | /* _PyBytesWriter API */ |
| 3183 | |
| 3184 | #ifdef MS_WINDOWS |
| 3185 | /* On Windows, overallocate by 50% is the best factor */ |
| 3186 | # define OVERALLOCATE_FACTOR 2 |
| 3187 | #else |
| 3188 | /* On Linux, overallocate by 25% is the best factor */ |
| 3189 | # define OVERALLOCATE_FACTOR 4 |
| 3190 | #endif |
| 3191 | |
| 3192 | void |
| 3193 | _PyBytesWriter_Init(_PyBytesWriter *writer) |
| 3194 | { |
Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 3195 | /* Set all attributes before small_buffer to 0 */ |
| 3196 | memset(writer, 0, offsetof(_PyBytesWriter, small_buffer)); |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3197 | #ifdef Py_DEBUG |
Victor Stinner | b3653a3 | 2015-10-09 03:38:24 +0200 | [diff] [blame] | 3198 | memset(writer->small_buffer, 0xCB, sizeof(writer->small_buffer)); |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3199 | #endif |
| 3200 | } |
| 3201 | |
| 3202 | void |
| 3203 | _PyBytesWriter_Dealloc(_PyBytesWriter *writer) |
| 3204 | { |
| 3205 | Py_CLEAR(writer->buffer); |
| 3206 | } |
| 3207 | |
| 3208 | Py_LOCAL_INLINE(char*) |
| 3209 | _PyBytesWriter_AsString(_PyBytesWriter *writer) |
| 3210 | { |
Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 3211 | if (writer->use_small_buffer) { |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3212 | assert(writer->buffer == NULL); |
Victor Stinner | b3653a3 | 2015-10-09 03:38:24 +0200 | [diff] [blame] | 3213 | return writer->small_buffer; |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3214 | } |
Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 3215 | else if (writer->use_bytearray) { |
| 3216 | assert(writer->buffer != NULL); |
| 3217 | return PyByteArray_AS_STRING(writer->buffer); |
| 3218 | } |
| 3219 | else { |
| 3220 | assert(writer->buffer != NULL); |
| 3221 | return PyBytes_AS_STRING(writer->buffer); |
| 3222 | } |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3223 | } |
| 3224 | |
| 3225 | Py_LOCAL_INLINE(Py_ssize_t) |
Victor Stinner | 2bf8993 | 2015-10-14 11:25:33 +0200 | [diff] [blame] | 3226 | _PyBytesWriter_GetSize(_PyBytesWriter *writer, char *str) |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3227 | { |
| 3228 | char *start = _PyBytesWriter_AsString(writer); |
| 3229 | assert(str != NULL); |
| 3230 | assert(str >= start); |
Victor Stinner | b3653a3 | 2015-10-09 03:38:24 +0200 | [diff] [blame] | 3231 | assert(str - start <= writer->allocated); |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3232 | return str - start; |
| 3233 | } |
| 3234 | |
| 3235 | Py_LOCAL_INLINE(void) |
| 3236 | _PyBytesWriter_CheckConsistency(_PyBytesWriter *writer, char *str) |
| 3237 | { |
| 3238 | #ifdef Py_DEBUG |
| 3239 | char *start, *end; |
| 3240 | |
Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 3241 | if (writer->use_small_buffer) { |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3242 | assert(writer->buffer == NULL); |
| 3243 | } |
Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 3244 | else { |
| 3245 | assert(writer->buffer != NULL); |
| 3246 | if (writer->use_bytearray) |
| 3247 | assert(PyByteArray_CheckExact(writer->buffer)); |
| 3248 | else |
| 3249 | assert(PyBytes_CheckExact(writer->buffer)); |
| 3250 | assert(Py_REFCNT(writer->buffer) == 1); |
| 3251 | } |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3252 | |
Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 3253 | if (writer->use_bytearray) { |
| 3254 | /* bytearray has its own overallocation algorithm, |
| 3255 | writer overallocation must be disabled */ |
| 3256 | assert(!writer->overallocate); |
| 3257 | } |
| 3258 | |
| 3259 | assert(0 <= writer->allocated); |
Victor Stinner | 53926a1 | 2015-10-09 12:37:03 +0200 | [diff] [blame] | 3260 | assert(0 <= writer->min_size && writer->min_size <= writer->allocated); |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3261 | /* the last byte must always be null */ |
Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 3262 | start = _PyBytesWriter_AsString(writer); |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3263 | assert(start[writer->allocated] == 0); |
| 3264 | |
| 3265 | end = start + writer->allocated; |
| 3266 | assert(str != NULL); |
| 3267 | assert(start <= str && str <= end); |
| 3268 | #endif |
| 3269 | } |
| 3270 | |
Victor Stinner | c29e29b | 2015-10-12 13:12:54 +0200 | [diff] [blame] | 3271 | void* |
Victor Stinner | c5c3ba4 | 2015-10-14 13:56:47 +0200 | [diff] [blame] | 3272 | _PyBytesWriter_Resize(_PyBytesWriter *writer, void *str, Py_ssize_t size) |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3273 | { |
| 3274 | Py_ssize_t allocated, pos; |
| 3275 | |
| 3276 | _PyBytesWriter_CheckConsistency(writer, str); |
Victor Stinner | c5c3ba4 | 2015-10-14 13:56:47 +0200 | [diff] [blame] | 3277 | assert(writer->allocated < size); |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3278 | |
Victor Stinner | c5c3ba4 | 2015-10-14 13:56:47 +0200 | [diff] [blame] | 3279 | allocated = size; |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3280 | if (writer->overallocate |
| 3281 | && allocated <= (PY_SSIZE_T_MAX - allocated / OVERALLOCATE_FACTOR)) { |
| 3282 | /* overallocate to limit the number of realloc() */ |
| 3283 | allocated += allocated / OVERALLOCATE_FACTOR; |
| 3284 | } |
| 3285 | |
Victor Stinner | 2bf8993 | 2015-10-14 11:25:33 +0200 | [diff] [blame] | 3286 | pos = _PyBytesWriter_GetSize(writer, str); |
Victor Stinner | b3653a3 | 2015-10-09 03:38:24 +0200 | [diff] [blame] | 3287 | if (!writer->use_small_buffer) { |
Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 3288 | if (writer->use_bytearray) { |
| 3289 | if (PyByteArray_Resize(writer->buffer, allocated)) |
| 3290 | goto error; |
| 3291 | /* writer->allocated can be smaller than writer->buffer->ob_alloc, |
| 3292 | but we cannot use ob_alloc because bytes may need to be moved |
| 3293 | to use the whole buffer. bytearray uses an internal optimization |
| 3294 | to avoid moving or copying bytes when bytes are removed at the |
| 3295 | beginning (ex: del bytearray[:1]). */ |
| 3296 | } |
| 3297 | else { |
| 3298 | if (_PyBytes_Resize(&writer->buffer, allocated)) |
| 3299 | goto error; |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3300 | } |
| 3301 | } |
| 3302 | else { |
| 3303 | /* convert from stack buffer to bytes object buffer */ |
| 3304 | assert(writer->buffer == NULL); |
| 3305 | |
Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 3306 | if (writer->use_bytearray) |
| 3307 | writer->buffer = PyByteArray_FromStringAndSize(NULL, allocated); |
| 3308 | else |
| 3309 | writer->buffer = PyBytes_FromStringAndSize(NULL, allocated); |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3310 | if (writer->buffer == NULL) |
Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 3311 | goto error; |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3312 | |
| 3313 | if (pos != 0) { |
Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 3314 | char *dest; |
| 3315 | if (writer->use_bytearray) |
| 3316 | dest = PyByteArray_AS_STRING(writer->buffer); |
| 3317 | else |
| 3318 | dest = PyBytes_AS_STRING(writer->buffer); |
Christian Heimes | f051e43 | 2016-09-13 20:22:02 +0200 | [diff] [blame] | 3319 | memcpy(dest, |
Victor Stinner | b3653a3 | 2015-10-09 03:38:24 +0200 | [diff] [blame] | 3320 | writer->small_buffer, |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3321 | pos); |
| 3322 | } |
| 3323 | |
Victor Stinner | b3653a3 | 2015-10-09 03:38:24 +0200 | [diff] [blame] | 3324 | writer->use_small_buffer = 0; |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3325 | #ifdef Py_DEBUG |
Victor Stinner | b3653a3 | 2015-10-09 03:38:24 +0200 | [diff] [blame] | 3326 | memset(writer->small_buffer, 0xDB, sizeof(writer->small_buffer)); |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3327 | #endif |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3328 | } |
| 3329 | writer->allocated = allocated; |
| 3330 | |
| 3331 | str = _PyBytesWriter_AsString(writer) + pos; |
| 3332 | _PyBytesWriter_CheckConsistency(writer, str); |
| 3333 | return str; |
Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 3334 | |
| 3335 | error: |
| 3336 | _PyBytesWriter_Dealloc(writer); |
| 3337 | return NULL; |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3338 | } |
| 3339 | |
Victor Stinner | c5c3ba4 | 2015-10-14 13:56:47 +0200 | [diff] [blame] | 3340 | void* |
| 3341 | _PyBytesWriter_Prepare(_PyBytesWriter *writer, void *str, Py_ssize_t size) |
| 3342 | { |
| 3343 | Py_ssize_t new_min_size; |
| 3344 | |
| 3345 | _PyBytesWriter_CheckConsistency(writer, str); |
| 3346 | assert(size >= 0); |
| 3347 | |
| 3348 | if (size == 0) { |
| 3349 | /* nothing to do */ |
| 3350 | return str; |
| 3351 | } |
| 3352 | |
| 3353 | if (writer->min_size > PY_SSIZE_T_MAX - size) { |
| 3354 | PyErr_NoMemory(); |
| 3355 | _PyBytesWriter_Dealloc(writer); |
| 3356 | return NULL; |
| 3357 | } |
| 3358 | new_min_size = writer->min_size + size; |
| 3359 | |
| 3360 | if (new_min_size > writer->allocated) |
| 3361 | str = _PyBytesWriter_Resize(writer, str, new_min_size); |
| 3362 | |
| 3363 | writer->min_size = new_min_size; |
| 3364 | return str; |
| 3365 | } |
| 3366 | |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3367 | /* Allocate the buffer to write size bytes. |
| 3368 | Return the pointer to the beginning of buffer data. |
| 3369 | Raise an exception and return NULL on error. */ |
Victor Stinner | c29e29b | 2015-10-12 13:12:54 +0200 | [diff] [blame] | 3370 | void* |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3371 | _PyBytesWriter_Alloc(_PyBytesWriter *writer, Py_ssize_t size) |
| 3372 | { |
| 3373 | /* ensure that _PyBytesWriter_Alloc() is only called once */ |
Victor Stinner | 53926a1 | 2015-10-09 12:37:03 +0200 | [diff] [blame] | 3374 | assert(writer->min_size == 0 && writer->buffer == NULL); |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3375 | assert(size >= 0); |
| 3376 | |
Victor Stinner | b3653a3 | 2015-10-09 03:38:24 +0200 | [diff] [blame] | 3377 | writer->use_small_buffer = 1; |
Victor Stinner | b13b97d | 2015-10-09 02:52:16 +0200 | [diff] [blame] | 3378 | #ifdef Py_DEBUG |
Victor Stinner | b3653a3 | 2015-10-09 03:38:24 +0200 | [diff] [blame] | 3379 | writer->allocated = sizeof(writer->small_buffer) - 1; |
Victor Stinner | f6358a7 | 2015-10-14 12:02:39 +0200 | [diff] [blame] | 3380 | /* In debug mode, don't use the full small buffer because it is less |
| 3381 | efficient than bytes and bytearray objects to detect buffer underflow |
| 3382 | and buffer overflow. Use 10 bytes of the small buffer to test also |
| 3383 | code using the smaller buffer in debug mode. |
| 3384 | |
| 3385 | Don't modify the _PyBytesWriter structure (use a shorter small buffer) |
| 3386 | in debug mode to also be able to detect stack overflow when running |
| 3387 | tests in debug mode. The _PyBytesWriter is large (more than 512 bytes), |
| 3388 | if Py_EnterRecursiveCall() is not used in deep C callback, we may hit a |
| 3389 | stack overflow. */ |
| 3390 | writer->allocated = Py_MIN(writer->allocated, 10); |
| 3391 | /* _PyBytesWriter_CheckConsistency() requires the last byte to be 0, |
| 3392 | to detect buffer overflow */ |
Victor Stinner | b3653a3 | 2015-10-09 03:38:24 +0200 | [diff] [blame] | 3393 | writer->small_buffer[writer->allocated] = 0; |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3394 | #else |
Victor Stinner | b3653a3 | 2015-10-09 03:38:24 +0200 | [diff] [blame] | 3395 | writer->allocated = sizeof(writer->small_buffer); |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3396 | #endif |
Victor Stinner | b3653a3 | 2015-10-09 03:38:24 +0200 | [diff] [blame] | 3397 | return _PyBytesWriter_Prepare(writer, writer->small_buffer, size); |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3398 | } |
| 3399 | |
| 3400 | PyObject * |
Victor Stinner | c29e29b | 2015-10-12 13:12:54 +0200 | [diff] [blame] | 3401 | _PyBytesWriter_Finish(_PyBytesWriter *writer, void *str) |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3402 | { |
Victor Stinner | 2bf8993 | 2015-10-14 11:25:33 +0200 | [diff] [blame] | 3403 | Py_ssize_t size; |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3404 | PyObject *result; |
| 3405 | |
| 3406 | _PyBytesWriter_CheckConsistency(writer, str); |
| 3407 | |
Victor Stinner | 2bf8993 | 2015-10-14 11:25:33 +0200 | [diff] [blame] | 3408 | size = _PyBytesWriter_GetSize(writer, str); |
| 3409 | if (size == 0 && !writer->use_bytearray) { |
Victor Stinner | 6c2cdae | 2015-10-12 13:29:43 +0200 | [diff] [blame] | 3410 | Py_CLEAR(writer->buffer); |
| 3411 | /* Get the empty byte string singleton */ |
| 3412 | result = PyBytes_FromStringAndSize(NULL, 0); |
| 3413 | } |
| 3414 | else if (writer->use_small_buffer) { |
Victor Stinner | e914d41 | 2016-04-15 17:52:27 +0200 | [diff] [blame] | 3415 | if (writer->use_bytearray) { |
| 3416 | result = PyByteArray_FromStringAndSize(writer->small_buffer, size); |
| 3417 | } |
| 3418 | else { |
| 3419 | result = PyBytes_FromStringAndSize(writer->small_buffer, size); |
| 3420 | } |
Victor Stinner | 6c2cdae | 2015-10-12 13:29:43 +0200 | [diff] [blame] | 3421 | } |
| 3422 | else { |
| 3423 | result = writer->buffer; |
| 3424 | writer->buffer = NULL; |
| 3425 | |
Victor Stinner | 2bf8993 | 2015-10-14 11:25:33 +0200 | [diff] [blame] | 3426 | if (size != writer->allocated) { |
Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 3427 | if (writer->use_bytearray) { |
Victor Stinner | 2bf8993 | 2015-10-14 11:25:33 +0200 | [diff] [blame] | 3428 | if (PyByteArray_Resize(result, size)) { |
Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 3429 | Py_DECREF(result); |
| 3430 | return NULL; |
| 3431 | } |
| 3432 | } |
| 3433 | else { |
Victor Stinner | 2bf8993 | 2015-10-14 11:25:33 +0200 | [diff] [blame] | 3434 | if (_PyBytes_Resize(&result, size)) { |
Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 3435 | assert(result == NULL); |
| 3436 | return NULL; |
| 3437 | } |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3438 | } |
| 3439 | } |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3440 | } |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 3441 | return result; |
| 3442 | } |
Victor Stinner | ce179bf | 2015-10-09 12:57:22 +0200 | [diff] [blame] | 3443 | |
Victor Stinner | c29e29b | 2015-10-12 13:12:54 +0200 | [diff] [blame] | 3444 | void* |
Victor Stinner | e9aa595 | 2015-10-12 13:57:47 +0200 | [diff] [blame] | 3445 | _PyBytesWriter_WriteBytes(_PyBytesWriter *writer, void *ptr, |
Victor Stinner | c29e29b | 2015-10-12 13:12:54 +0200 | [diff] [blame] | 3446 | const void *bytes, Py_ssize_t size) |
Victor Stinner | ce179bf | 2015-10-09 12:57:22 +0200 | [diff] [blame] | 3447 | { |
Victor Stinner | e9aa595 | 2015-10-12 13:57:47 +0200 | [diff] [blame] | 3448 | char *str = (char *)ptr; |
| 3449 | |
Victor Stinner | ce179bf | 2015-10-09 12:57:22 +0200 | [diff] [blame] | 3450 | str = _PyBytesWriter_Prepare(writer, str, size); |
| 3451 | if (str == NULL) |
| 3452 | return NULL; |
| 3453 | |
Christian Heimes | f051e43 | 2016-09-13 20:22:02 +0200 | [diff] [blame] | 3454 | memcpy(str, bytes, size); |
Victor Stinner | ce179bf | 2015-10-09 12:57:22 +0200 | [diff] [blame] | 3455 | str += size; |
| 3456 | |
| 3457 | return str; |
| 3458 | } |