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