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