Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 1 | /* Bytes object implementation */ |
| 2 | |
| 3 | /* XXX TO DO: optimizations */ |
| 4 | |
| 5 | #define PY_SSIZE_T_CLEAN |
| 6 | #include "Python.h" |
Guido van Rossum | a0867f7 | 2006-05-05 04:34:18 +0000 | [diff] [blame^] | 7 | #include "structmember.h" |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 8 | |
| 9 | /* Direct API functions */ |
| 10 | |
| 11 | PyObject * |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 12 | PyBytes_FromObject(PyObject *input) |
| 13 | { |
| 14 | return PyObject_CallFunctionObjArgs((PyObject *)&PyBytes_Type, |
| 15 | input, NULL); |
| 16 | } |
| 17 | |
| 18 | PyObject * |
| 19 | PyBytes_FromStringAndSize(const char *bytes, Py_ssize_t size) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 20 | { |
| 21 | PyBytesObject *new; |
| 22 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 23 | assert(size >= 0); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 24 | |
| 25 | new = PyObject_New(PyBytesObject, &PyBytes_Type); |
| 26 | if (new == NULL) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 27 | return NULL; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 29 | if (size == 0) |
| 30 | new->ob_bytes = NULL; |
| 31 | else { |
| 32 | new->ob_bytes = PyMem_Malloc(size); |
| 33 | if (new->ob_bytes == NULL) { |
| 34 | Py_DECREF(new); |
| 35 | return NULL; |
| 36 | } |
| 37 | if (bytes != NULL) |
| 38 | memcpy(new->ob_bytes, bytes, size); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 39 | } |
Guido van Rossum | a0867f7 | 2006-05-05 04:34:18 +0000 | [diff] [blame^] | 40 | new->ob_size = new->ob_alloc = size; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 41 | |
| 42 | return (PyObject *)new; |
| 43 | } |
| 44 | |
| 45 | Py_ssize_t |
| 46 | PyBytes_Size(PyObject *self) |
| 47 | { |
| 48 | assert(self != NULL); |
| 49 | assert(PyBytes_Check(self)); |
| 50 | |
| 51 | return ((PyBytesObject *)self)->ob_size; |
| 52 | } |
| 53 | |
| 54 | char * |
| 55 | PyBytes_AsString(PyObject *self) |
| 56 | { |
| 57 | assert(self != NULL); |
| 58 | assert(PyBytes_Check(self)); |
| 59 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 60 | return ((PyBytesObject *)self)->ob_bytes; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | int |
| 64 | PyBytes_Resize(PyObject *self, Py_ssize_t size) |
| 65 | { |
| 66 | void *sval; |
Guido van Rossum | a0867f7 | 2006-05-05 04:34:18 +0000 | [diff] [blame^] | 67 | Py_ssize_t alloc = ((PyBytesObject *)self)->ob_alloc; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 68 | |
| 69 | assert(self != NULL); |
| 70 | assert(PyBytes_Check(self)); |
| 71 | assert(size >= 0); |
| 72 | |
Guido van Rossum | a0867f7 | 2006-05-05 04:34:18 +0000 | [diff] [blame^] | 73 | if (size < alloc / 2) { |
| 74 | /* Major downsize; resize down to exact size */ |
| 75 | alloc = size; |
| 76 | } |
| 77 | else if (size <= alloc) { |
| 78 | /* Within allocated size; quick exit */ |
| 79 | ((PyBytesObject *)self)->ob_size = size; |
| 80 | return 0; |
| 81 | } |
| 82 | else if (size <= alloc * 1.125) { |
| 83 | /* Moderate upsize; overallocate similar to list_resize() */ |
| 84 | alloc = size + (size >> 3) + (size < 9 ? 3 : 6); |
| 85 | } |
| 86 | else { |
| 87 | /* Major upsize; resize up to exact size */ |
| 88 | alloc = size; |
| 89 | } |
| 90 | |
| 91 | sval = PyMem_Realloc(((PyBytesObject *)self)->ob_bytes, alloc); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 92 | if (sval == NULL) { |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 93 | PyErr_NoMemory(); |
| 94 | return -1; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 97 | ((PyBytesObject *)self)->ob_bytes = sval; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 98 | ((PyBytesObject *)self)->ob_size = size; |
Guido van Rossum | a0867f7 | 2006-05-05 04:34:18 +0000 | [diff] [blame^] | 99 | ((PyBytesObject *)self)->ob_alloc = alloc; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | /* Functions stuffed into the type object */ |
| 105 | |
| 106 | static Py_ssize_t |
| 107 | bytes_length(PyBytesObject *self) |
| 108 | { |
| 109 | return self->ob_size; |
| 110 | } |
| 111 | |
| 112 | static PyObject * |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 113 | bytes_concat(PyBytesObject *self, PyObject *other) |
| 114 | { |
| 115 | PyBytesObject *result; |
| 116 | Py_ssize_t mysize; |
| 117 | Py_ssize_t size; |
| 118 | |
| 119 | if (!PyBytes_Check(other)) { |
| 120 | PyErr_Format(PyExc_TypeError, |
| 121 | "can't concat bytes to %.100s", other->ob_type->tp_name); |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | mysize = self->ob_size; |
| 126 | size = mysize + ((PyBytesObject *)other)->ob_size; |
| 127 | if (size < 0) |
| 128 | return PyErr_NoMemory(); |
| 129 | result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, size); |
| 130 | if (result != NULL) { |
| 131 | memcpy(result->ob_bytes, self->ob_bytes, self->ob_size); |
| 132 | memcpy(result->ob_bytes + self->ob_size, |
| 133 | ((PyBytesObject *)other)->ob_bytes, |
| 134 | ((PyBytesObject *)other)->ob_size); |
| 135 | } |
| 136 | return (PyObject *)result; |
| 137 | } |
| 138 | |
| 139 | static PyObject * |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 140 | bytes_iconcat(PyBytesObject *self, PyObject *other) |
| 141 | { |
| 142 | Py_ssize_t mysize; |
| 143 | Py_ssize_t osize; |
| 144 | Py_ssize_t size; |
| 145 | |
| 146 | if (!PyBytes_Check(other)) { |
| 147 | PyErr_Format(PyExc_TypeError, |
| 148 | "can't concat bytes to %.100s", other->ob_type->tp_name); |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | mysize = self->ob_size; |
| 153 | osize = ((PyBytesObject *)other)->ob_size; |
| 154 | size = mysize + osize; |
| 155 | if (size < 0) |
| 156 | return PyErr_NoMemory(); |
Guido van Rossum | a0867f7 | 2006-05-05 04:34:18 +0000 | [diff] [blame^] | 157 | if (size <= self->ob_alloc) |
| 158 | self->ob_size = size; |
| 159 | else if (PyBytes_Resize((PyObject *)self, size) < 0) |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 160 | return NULL; |
| 161 | memcpy(self->ob_bytes + mysize, ((PyBytesObject *)other)->ob_bytes, osize); |
| 162 | Py_INCREF(self); |
| 163 | return (PyObject *)self; |
| 164 | } |
| 165 | |
| 166 | static PyObject * |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 167 | bytes_repeat(PyBytesObject *self, Py_ssize_t count) |
| 168 | { |
| 169 | PyBytesObject *result; |
| 170 | Py_ssize_t mysize; |
| 171 | Py_ssize_t size; |
| 172 | |
| 173 | if (count < 0) |
| 174 | count = 0; |
| 175 | mysize = self->ob_size; |
| 176 | size = mysize * count; |
| 177 | if (count != 0 && size / count != mysize) |
| 178 | return PyErr_NoMemory(); |
| 179 | result = (PyBytesObject *)PyBytes_FromStringAndSize(NULL, size); |
| 180 | if (result != NULL && size != 0) { |
| 181 | if (mysize == 1) |
| 182 | memset(result->ob_bytes, self->ob_bytes[0], size); |
| 183 | else { |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 184 | Py_ssize_t i; |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 185 | for (i = 0; i < count; i++) |
| 186 | memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize); |
| 187 | } |
| 188 | } |
| 189 | return (PyObject *)result; |
| 190 | } |
| 191 | |
| 192 | static PyObject * |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 193 | bytes_irepeat(PyBytesObject *self, Py_ssize_t count) |
| 194 | { |
| 195 | Py_ssize_t mysize; |
| 196 | Py_ssize_t size; |
| 197 | |
| 198 | if (count < 0) |
| 199 | count = 0; |
| 200 | mysize = self->ob_size; |
| 201 | size = mysize * count; |
| 202 | if (count != 0 && size / count != mysize) |
| 203 | return PyErr_NoMemory(); |
Guido van Rossum | a0867f7 | 2006-05-05 04:34:18 +0000 | [diff] [blame^] | 204 | if (size <= self->ob_alloc) |
| 205 | self->ob_size = size; |
| 206 | else if (PyBytes_Resize((PyObject *)self, size) < 0) |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 207 | return NULL; |
| 208 | |
| 209 | if (mysize == 1) |
| 210 | memset(self->ob_bytes, self->ob_bytes[0], size); |
| 211 | else { |
| 212 | Py_ssize_t i; |
| 213 | for (i = 1; i < count; i++) |
| 214 | memcpy(self->ob_bytes + i*mysize, self->ob_bytes, mysize); |
| 215 | } |
| 216 | |
| 217 | Py_INCREF(self); |
| 218 | return (PyObject *)self; |
| 219 | } |
| 220 | |
| 221 | static int |
| 222 | bytes_substring(PyBytesObject *self, PyBytesObject *other) |
| 223 | { |
| 224 | Py_ssize_t i; |
| 225 | |
| 226 | if (other->ob_size == 1) { |
| 227 | return memchr(self->ob_bytes, other->ob_bytes[0], |
| 228 | self->ob_size) != NULL; |
| 229 | } |
| 230 | if (other->ob_size == 0) |
| 231 | return 1; /* Edge case */ |
| 232 | for (i = 0; i + other->ob_size <= self->ob_size; i++) { |
| 233 | /* XXX Yeah, yeah, lots of optimizations possible... */ |
| 234 | if (memcmp(self->ob_bytes + i, other->ob_bytes, other->ob_size) == 0) |
| 235 | return 1; |
| 236 | } |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static int |
| 241 | bytes_contains(PyBytesObject *self, PyObject *value) |
| 242 | { |
| 243 | Py_ssize_t ival; |
| 244 | |
| 245 | if (PyBytes_Check(value)) |
| 246 | return bytes_substring(self, (PyBytesObject *)value); |
| 247 | |
| 248 | ival = PyNumber_Index(value); |
| 249 | if (ival == -1 && PyErr_Occurred()) |
| 250 | return -1; |
| 251 | |
| 252 | if (ival < 0 || ival >= 256) { |
| 253 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
| 254 | return -1; |
| 255 | } |
| 256 | |
| 257 | return memchr(self->ob_bytes, ival, self->ob_size) != NULL; |
| 258 | } |
| 259 | |
| 260 | static PyObject * |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 261 | bytes_getitem(PyBytesObject *self, Py_ssize_t i) |
| 262 | { |
| 263 | if (i < 0) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 264 | i += self->ob_size; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 265 | if (i < 0 || i >= self->ob_size) { |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 266 | PyErr_SetString(PyExc_IndexError, "bytes index out of range"); |
| 267 | return NULL; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 268 | } |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 269 | return PyInt_FromLong((unsigned char)(self->ob_bytes[i])); |
| 270 | } |
| 271 | |
| 272 | static PyObject * |
| 273 | bytes_getslice(PyBytesObject *self, Py_ssize_t lo, Py_ssize_t hi) |
| 274 | { |
| 275 | if (lo < 0) |
| 276 | lo = 0; |
| 277 | if (hi > self->ob_size) |
| 278 | hi = self->ob_size; |
| 279 | if (lo >= hi) |
| 280 | lo = hi = 0; |
| 281 | return PyBytes_FromStringAndSize(self->ob_bytes + lo, hi - lo); |
| 282 | } |
| 283 | |
| 284 | static int |
| 285 | bytes_setslice(PyBytesObject *self, Py_ssize_t lo, Py_ssize_t hi, |
| 286 | PyObject *values) |
| 287 | { |
| 288 | int avail; |
| 289 | int needed; |
| 290 | char *bytes; |
| 291 | |
| 292 | if (values == NULL) { |
| 293 | bytes = NULL; |
| 294 | needed = 0; |
| 295 | } |
| 296 | else if (values == (PyObject *)self || !PyBytes_Check(values)) { |
| 297 | /* Make a copy an call this function recursively */ |
| 298 | int err; |
| 299 | values = PyBytes_FromObject(values); |
| 300 | if (values == NULL) |
| 301 | return -1; |
| 302 | err = bytes_setslice(self, lo, hi, values); |
| 303 | Py_DECREF(values); |
| 304 | return err; |
| 305 | } |
| 306 | else { |
| 307 | assert(PyBytes_Check(values)); |
| 308 | bytes = ((PyBytesObject *)values)->ob_bytes; |
| 309 | needed = ((PyBytesObject *)values)->ob_size; |
| 310 | } |
| 311 | |
| 312 | if (lo < 0) |
| 313 | lo = 0; |
| 314 | if (hi > self->ob_size) |
| 315 | hi = self->ob_size; |
| 316 | |
| 317 | avail = hi - lo; |
| 318 | if (avail < 0) |
| 319 | lo = hi = avail = 0; |
| 320 | |
| 321 | if (avail != needed) { |
| 322 | if (avail > needed) { |
| 323 | /* |
| 324 | 0 lo hi old_size |
| 325 | | |<----avail----->|<-----tomove------>| |
| 326 | | |<-needed->|<-----tomove------>| |
| 327 | 0 lo new_hi new_size |
| 328 | */ |
| 329 | memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi, |
| 330 | self->ob_size - hi); |
| 331 | } |
| 332 | if (PyBytes_Resize((PyObject *)self, |
| 333 | self->ob_size + needed - avail) < 0) |
| 334 | return -1; |
| 335 | if (avail < needed) { |
| 336 | /* |
| 337 | 0 lo hi old_size |
| 338 | | |<-avail->|<-----tomove------>| |
| 339 | | |<----needed---->|<-----tomove------>| |
| 340 | 0 lo new_hi new_size |
| 341 | */ |
| 342 | memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi, |
| 343 | self->ob_size - lo - needed); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | if (needed > 0) |
| 348 | memcpy(self->ob_bytes + lo, bytes, needed); |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static int |
| 354 | bytes_setitem(PyBytesObject *self, Py_ssize_t i, PyObject *value) |
| 355 | { |
| 356 | Py_ssize_t ival; |
| 357 | |
| 358 | if (i < 0) |
| 359 | i += self->ob_size; |
| 360 | |
| 361 | if (i < 0 || i >= self->ob_size) { |
| 362 | PyErr_SetString(PyExc_IndexError, "bytes index out of range"); |
| 363 | return -1; |
| 364 | } |
| 365 | |
| 366 | if (value == NULL) |
| 367 | return bytes_setslice(self, i, i+1, NULL); |
| 368 | |
| 369 | ival = PyNumber_Index(value); |
| 370 | if (ival == -1 && PyErr_Occurred()) |
| 371 | return -1; |
| 372 | |
| 373 | if (ival < 0 || ival >= 256) { |
| 374 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
| 375 | return -1; |
| 376 | } |
| 377 | |
| 378 | self->ob_bytes[i] = ival; |
| 379 | return 0; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static long |
| 383 | bytes_nohash(PyObject *self) |
| 384 | { |
| 385 | PyErr_SetString(PyExc_TypeError, "bytes objects are unhashable"); |
| 386 | return -1; |
| 387 | } |
| 388 | |
| 389 | static int |
| 390 | bytes_init(PyBytesObject *self, PyObject *args, PyObject *kwds) |
| 391 | { |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 392 | static char *kwlist[] = {"source", "encoding", "errors", 0}; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 393 | PyObject *arg = NULL; |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 394 | const char *encoding = NULL; |
| 395 | const char *errors = NULL; |
| 396 | Py_ssize_t count; |
| 397 | PyObject *it; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 398 | PyObject *(*iternext)(PyObject *); |
| 399 | |
Guido van Rossum | a0867f7 | 2006-05-05 04:34:18 +0000 | [diff] [blame^] | 400 | if (self->ob_size != 0) { |
| 401 | /* Empty previous contents (yes, do this first of all!) */ |
| 402 | if (PyBytes_Resize((PyObject *)self, 0) < 0) |
| 403 | return -1; |
| 404 | } |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 405 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 406 | /* Parse arguments */ |
| 407 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytes", kwlist, |
| 408 | &arg, &encoding, &errors)) |
| 409 | return -1; |
| 410 | |
| 411 | /* Make a quick exit if no first argument */ |
| 412 | if (arg == NULL) { |
| 413 | if (encoding != NULL || errors != NULL) { |
| 414 | PyErr_SetString(PyExc_TypeError, |
| 415 | "encoding or errors without sequence argument"); |
| 416 | return -1; |
| 417 | } |
| 418 | return 0; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 421 | if (PyUnicode_Check(arg)) { |
| 422 | /* Encode via the codec registry */ |
| 423 | PyObject *encoded; |
| 424 | char *bytes; |
| 425 | Py_ssize_t size; |
| 426 | if (encoding == NULL) |
| 427 | encoding = PyUnicode_GetDefaultEncoding(); |
| 428 | encoded = PyCodec_Encode(arg, encoding, errors); |
| 429 | if (encoded == NULL) |
| 430 | return -1; |
| 431 | if (!PyString_Check(encoded)) { |
| 432 | PyErr_Format(PyExc_TypeError, |
| 433 | "encoder did not return a string object (type=%.400s)", |
| 434 | encoded->ob_type->tp_name); |
| 435 | Py_DECREF(encoded); |
| 436 | return -1; |
| 437 | } |
| 438 | bytes = PyString_AS_STRING(encoded); |
| 439 | size = PyString_GET_SIZE(encoded); |
Guido van Rossum | a0867f7 | 2006-05-05 04:34:18 +0000 | [diff] [blame^] | 440 | if (size <= self->ob_alloc) |
| 441 | self->ob_size = size; |
| 442 | else if (PyBytes_Resize((PyObject *)self, size) < 0) { |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 443 | Py_DECREF(encoded); |
| 444 | return -1; |
| 445 | } |
| 446 | memcpy(self->ob_bytes, bytes, size); |
| 447 | Py_DECREF(encoded); |
| 448 | return 0; |
| 449 | } |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 450 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 451 | /* If it's not unicode, there can't be encoding or errors */ |
| 452 | if (encoding != NULL || errors != NULL) { |
| 453 | PyErr_SetString(PyExc_TypeError, |
| 454 | "encoding or errors without a string argument"); |
| 455 | return -1; |
| 456 | } |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 457 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 458 | /* Is it an int? */ |
| 459 | count = PyNumber_Index(arg); |
| 460 | if (count == -1 && PyErr_Occurred()) |
| 461 | PyErr_Clear(); |
| 462 | else { |
| 463 | if (count < 0) { |
| 464 | PyErr_SetString(PyExc_ValueError, "negative count"); |
| 465 | return -1; |
| 466 | } |
| 467 | if (count > 0) { |
| 468 | if (PyBytes_Resize((PyObject *)self, count)) |
| 469 | return -1; |
| 470 | memset(self->ob_bytes, 0, count); |
| 471 | } |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | if (PyObject_CheckReadBuffer(arg)) { |
| 476 | const void *bytes; |
| 477 | Py_ssize_t size; |
| 478 | if (PyObject_AsReadBuffer(arg, &bytes, &size) < 0) |
| 479 | return -1; |
| 480 | if (PyBytes_Resize((PyObject *)self, size) < 0) |
| 481 | return -1; |
| 482 | memcpy(self->ob_bytes, bytes, size); |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | /* XXX Optimize this if the arguments is a list, tuple */ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 487 | |
| 488 | /* Get the iterator */ |
| 489 | it = PyObject_GetIter(arg); |
| 490 | if (it == NULL) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 491 | return -1; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 492 | iternext = *it->ob_type->tp_iternext; |
| 493 | |
| 494 | /* Run the iterator to exhaustion */ |
| 495 | for (;;) { |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 496 | PyObject *item; |
| 497 | Py_ssize_t value; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 498 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 499 | /* Get the next item */ |
| 500 | item = iternext(it); |
| 501 | if (item == NULL) { |
| 502 | if (PyErr_Occurred()) { |
| 503 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 504 | goto error; |
| 505 | PyErr_Clear(); |
| 506 | } |
| 507 | break; |
| 508 | } |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 509 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 510 | /* Interpret it as an int (__index__) */ |
| 511 | value = PyNumber_Index(item); |
| 512 | Py_DECREF(item); |
| 513 | if (value == -1 && PyErr_Occurred()) |
| 514 | goto error; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 515 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 516 | /* Range check */ |
| 517 | if (value < 0 || value >= 256) { |
| 518 | PyErr_SetString(PyExc_ValueError, |
| 519 | "bytes must be in range(0, 256)"); |
| 520 | goto error; |
| 521 | } |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 522 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 523 | /* Append the byte */ |
Guido van Rossum | a0867f7 | 2006-05-05 04:34:18 +0000 | [diff] [blame^] | 524 | if (self->ob_size < self->ob_alloc) |
| 525 | self->ob_size++; |
| 526 | else if (PyBytes_Resize((PyObject *)self, self->ob_size+1) < 0) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 527 | goto error; |
| 528 | self->ob_bytes[self->ob_size-1] = value; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /* Clean up and return success */ |
| 532 | Py_DECREF(it); |
| 533 | return 0; |
| 534 | |
| 535 | error: |
| 536 | /* Error handling when it != NULL */ |
| 537 | Py_DECREF(it); |
| 538 | return -1; |
| 539 | } |
| 540 | |
| 541 | static PyObject * |
| 542 | bytes_repr(PyBytesObject *self) |
| 543 | { |
| 544 | PyObject *list; |
| 545 | PyObject *str; |
| 546 | PyObject *result; |
| 547 | int err; |
| 548 | int i; |
| 549 | |
| 550 | if (self->ob_size == 0) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 551 | return PyString_FromString("bytes()"); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 552 | |
| 553 | list = PyList_New(0); |
| 554 | if (list == NULL) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 555 | return NULL; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 556 | |
| 557 | str = PyString_FromString("bytes(["); |
| 558 | if (str == NULL) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 559 | goto error; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 560 | |
| 561 | err = PyList_Append(list, str); |
| 562 | Py_DECREF(str); |
| 563 | if (err < 0) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 564 | goto error; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 565 | |
| 566 | for (i = 0; i < self->ob_size; i++) { |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 567 | char buffer[20]; |
| 568 | sprintf(buffer, ", 0x%02x", (unsigned char) (self->ob_bytes[i])); |
| 569 | str = PyString_FromString((i == 0) ? buffer+2 : buffer); |
| 570 | if (str == NULL) |
| 571 | goto error; |
| 572 | err = PyList_Append(list, str); |
| 573 | Py_DECREF(str); |
| 574 | if (err < 0) |
| 575 | goto error; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | str = PyString_FromString("])"); |
| 579 | if (str == NULL) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 580 | goto error; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 581 | |
| 582 | err = PyList_Append(list, str); |
| 583 | Py_DECREF(str); |
| 584 | if (err < 0) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 585 | goto error; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 586 | |
| 587 | str = PyString_FromString(""); |
| 588 | if (str == NULL) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 589 | goto error; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 590 | |
| 591 | result = _PyString_Join(str, list); |
| 592 | Py_DECREF(str); |
| 593 | Py_DECREF(list); |
| 594 | return result; |
| 595 | |
| 596 | error: |
| 597 | /* Error handling when list != NULL */ |
| 598 | Py_DECREF(list); |
| 599 | return NULL; |
| 600 | } |
| 601 | |
| 602 | static PyObject * |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 603 | bytes_str(PyBytesObject *self) |
| 604 | { |
| 605 | return PyString_FromStringAndSize(self->ob_bytes, self->ob_size); |
| 606 | } |
| 607 | |
| 608 | static PyObject * |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 609 | bytes_richcompare(PyBytesObject *self, PyBytesObject *other, int op) |
| 610 | { |
| 611 | PyObject *res; |
| 612 | int minsize; |
| 613 | int cmp; |
| 614 | |
| 615 | if (!PyBytes_Check(self) || !PyBytes_Check(other)) { |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 616 | Py_INCREF(Py_NotImplemented); |
| 617 | return Py_NotImplemented; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | if (self->ob_size != other->ob_size && (op == Py_EQ || op == Py_NE)) { |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 621 | /* Shortcut: if the lengths differ, the objects differ */ |
| 622 | cmp = (op == Py_NE); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 623 | } |
| 624 | else { |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 625 | minsize = self->ob_size; |
| 626 | if (other->ob_size < minsize) |
| 627 | minsize = other->ob_size; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 628 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 629 | cmp = memcmp(self->ob_bytes, other->ob_bytes, minsize); |
| 630 | /* In ISO C, memcmp() guarantees to use unsigned bytes! */ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 631 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 632 | if (cmp == 0) { |
| 633 | if (self->ob_size < other->ob_size) |
| 634 | cmp = -1; |
| 635 | else if (self->ob_size > other->ob_size) |
| 636 | cmp = 1; |
| 637 | } |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 638 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 639 | switch (op) { |
| 640 | case Py_LT: cmp = cmp < 0; break; |
| 641 | case Py_LE: cmp = cmp <= 0; break; |
| 642 | case Py_EQ: cmp = cmp == 0; break; |
| 643 | case Py_NE: cmp = cmp != 0; break; |
| 644 | case Py_GT: cmp = cmp > 0; break; |
| 645 | case Py_GE: cmp = cmp >= 0; break; |
| 646 | } |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | res = cmp ? Py_True : Py_False; |
| 650 | Py_INCREF(res); |
| 651 | return res; |
| 652 | } |
| 653 | |
| 654 | static void |
| 655 | bytes_dealloc(PyBytesObject *self) |
| 656 | { |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 657 | if (self->ob_bytes != 0) { |
| 658 | PyMem_Free(self->ob_bytes); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 659 | } |
| 660 | self->ob_type->tp_free((PyObject *)self); |
| 661 | } |
| 662 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 663 | static Py_ssize_t |
| 664 | bytes_getbuffer(PyBytesObject *self, Py_ssize_t index, const void **ptr) |
| 665 | { |
| 666 | if (index != 0) { |
| 667 | PyErr_SetString(PyExc_SystemError, |
| 668 | "accessing non-existent string segment"); |
| 669 | return -1; |
| 670 | } |
| 671 | *ptr = (void *)self->ob_bytes; |
| 672 | return self->ob_size; |
| 673 | } |
| 674 | |
| 675 | static Py_ssize_t |
| 676 | bytes_getsegcount(PyStringObject *self, Py_ssize_t *lenp) |
| 677 | { |
| 678 | if (lenp) |
| 679 | *lenp = self->ob_size; |
| 680 | return 1; |
| 681 | } |
| 682 | |
| 683 | PyDoc_STRVAR(decode_doc, |
| 684 | "B.decode([encoding[,errors]]) -> unicode obect.\n\ |
| 685 | \n\ |
| 686 | Decodes B using the codec registered for encoding. encoding defaults\n\ |
| 687 | to the default encoding. errors may be given to set a different error\n\ |
| 688 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 689 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 690 | as well as any other name registerd with codecs.register_error that is\n\ |
| 691 | able to handle UnicodeDecodeErrors."); |
| 692 | |
| 693 | static PyObject * |
| 694 | bytes_decode(PyObject *self, PyObject *args) |
| 695 | { |
| 696 | const char *encoding = NULL; |
| 697 | const char *errors = NULL; |
| 698 | |
| 699 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 700 | return NULL; |
| 701 | if (encoding == NULL) |
| 702 | encoding = PyUnicode_GetDefaultEncoding(); |
| 703 | return PyCodec_Decode(self, encoding, errors); |
| 704 | } |
| 705 | |
Guido van Rossum | a0867f7 | 2006-05-05 04:34:18 +0000 | [diff] [blame^] | 706 | PyDoc_STRVAR(alloc_doc, |
| 707 | "B.__alloc__() -> int\n\ |
| 708 | \n\ |
| 709 | Returns the number of bytes actually allocated."); |
| 710 | |
| 711 | static PyObject * |
| 712 | bytes_alloc(PyBytesObject *self) |
| 713 | { |
| 714 | return PyInt_FromSsize_t(self->ob_alloc); |
| 715 | } |
| 716 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 717 | static PySequenceMethods bytes_as_sequence = { |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 718 | (lenfunc)bytes_length, /*sq_length*/ |
| 719 | (binaryfunc)bytes_concat, /*sq_concat*/ |
| 720 | (ssizeargfunc)bytes_repeat, /*sq_repeat*/ |
| 721 | (ssizeargfunc)bytes_getitem, /*sq_item*/ |
| 722 | (ssizessizeargfunc)bytes_getslice, /*sq_slice*/ |
| 723 | (ssizeobjargproc)bytes_setitem, /*sq_ass_item*/ |
| 724 | (ssizessizeobjargproc)bytes_setslice, /* sq_ass_slice */ |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 725 | (objobjproc)bytes_contains, /* sq_contains */ |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 726 | (binaryfunc)bytes_iconcat, /* sq_inplace_concat */ |
| 727 | (ssizeargfunc)bytes_irepeat, /* sq_inplace_repeat */ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 728 | }; |
| 729 | |
| 730 | static PyMappingMethods bytes_as_mapping = { |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 731 | (lenfunc)bytes_length, |
| 732 | (binaryfunc)0, |
| 733 | 0, |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 734 | }; |
| 735 | |
| 736 | static PyBufferProcs bytes_as_buffer = { |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 737 | (readbufferproc)bytes_getbuffer, |
| 738 | (writebufferproc)bytes_getbuffer, |
| 739 | (segcountproc)bytes_getsegcount, |
| 740 | /* XXX Bytes are not characters! But we need to implement |
| 741 | bf_getcharbuffer() so we can be used as 't#' argument to codecs. */ |
| 742 | (charbufferproc)bytes_getbuffer, |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 743 | }; |
| 744 | |
| 745 | static PyMethodDef |
| 746 | bytes_methods[] = { |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 747 | {"decode", (PyCFunction)bytes_decode, METH_VARARGS, decode_doc}, |
Guido van Rossum | a0867f7 | 2006-05-05 04:34:18 +0000 | [diff] [blame^] | 748 | {"__alloc__", (PyCFunction)bytes_alloc, METH_NOARGS, alloc_doc}, |
| 749 | {NULL} |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 750 | }; |
| 751 | |
| 752 | PyDoc_STRVAR(bytes_doc, |
| 753 | "bytes([iterable]) -> new array of bytes.\n\ |
| 754 | \n\ |
| 755 | If an argument is given it must be an iterable yielding ints in range(256)."); |
| 756 | |
| 757 | PyTypeObject PyBytes_Type = { |
| 758 | PyObject_HEAD_INIT(&PyType_Type) |
| 759 | 0, |
| 760 | "bytes", |
| 761 | sizeof(PyBytesObject), |
| 762 | 0, |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 763 | (destructor)bytes_dealloc, /* tp_dealloc */ |
| 764 | 0, /* tp_print */ |
| 765 | 0, /* tp_getattr */ |
| 766 | 0, /* tp_setattr */ |
| 767 | 0, /* tp_compare */ |
| 768 | (reprfunc)bytes_repr, /* tp_repr */ |
| 769 | 0, /* tp_as_number */ |
| 770 | &bytes_as_sequence, /* tp_as_sequence */ |
| 771 | &bytes_as_mapping, /* tp_as_mapping */ |
| 772 | bytes_nohash, /* tp_hash */ |
| 773 | 0, /* tp_call */ |
| 774 | (reprfunc)bytes_str, /* tp_str */ |
| 775 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 776 | 0, /* tp_setattro */ |
| 777 | &bytes_as_buffer, /* tp_as_buffer */ |
| 778 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */ |
| 779 | /* bytes is 'final' or 'sealed' */ |
| 780 | bytes_doc, /* tp_doc */ |
| 781 | 0, /* tp_traverse */ |
| 782 | 0, /* tp_clear */ |
| 783 | (richcmpfunc)bytes_richcompare, /* tp_richcompare */ |
| 784 | 0, /* tp_weaklistoffset */ |
| 785 | 0, /* tp_iter */ |
| 786 | 0, /* tp_iternext */ |
| 787 | bytes_methods, /* tp_methods */ |
| 788 | 0, /* tp_members */ |
| 789 | 0, /* tp_getset */ |
| 790 | 0, /* tp_base */ |
| 791 | 0, /* tp_dict */ |
| 792 | 0, /* tp_descr_get */ |
| 793 | 0, /* tp_descr_set */ |
| 794 | 0, /* tp_dictoffset */ |
| 795 | (initproc)bytes_init, /* tp_init */ |
| 796 | PyType_GenericAlloc, /* tp_alloc */ |
| 797 | PyType_GenericNew, /* tp_new */ |
| 798 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 799 | }; |