blob: c4f9eeca995c7bde4dca45b92766760f846d3af3 [file] [log] [blame]
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001/* 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
10PyObject *
Guido van Rossumd624f182006-04-24 13:47:05 +000011PyBytes_FromObject(PyObject *input)
12{
13 return PyObject_CallFunctionObjArgs((PyObject *)&PyBytes_Type,
14 input, NULL);
15}
16
17PyObject *
18PyBytes_FromStringAndSize(const char *bytes, Py_ssize_t size)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000019{
20 PyBytesObject *new;
21
Guido van Rossumd624f182006-04-24 13:47:05 +000022 assert(size >= 0);
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000023
24 new = PyObject_New(PyBytesObject, &PyBytes_Type);
25 if (new == NULL)
Guido van Rossumd624f182006-04-24 13:47:05 +000026 return NULL;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000027
Guido van Rossumd624f182006-04-24 13:47:05 +000028 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 Rossum4dfe8a12006-04-22 23:28:04 +000039 }
40
41 return (PyObject *)new;
42}
43
44Py_ssize_t
45PyBytes_Size(PyObject *self)
46{
47 assert(self != NULL);
48 assert(PyBytes_Check(self));
49
50 return ((PyBytesObject *)self)->ob_size;
51}
52
53char *
54PyBytes_AsString(PyObject *self)
55{
56 assert(self != NULL);
57 assert(PyBytes_Check(self));
58
Guido van Rossumd624f182006-04-24 13:47:05 +000059 return ((PyBytesObject *)self)->ob_bytes;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000060}
61
62int
63PyBytes_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 Rossumd624f182006-04-24 13:47:05 +000071 sval = PyMem_Realloc(((PyBytesObject *)self)->ob_bytes, size);
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000072 if (sval == NULL) {
Guido van Rossumd624f182006-04-24 13:47:05 +000073 PyErr_NoMemory();
74 return -1;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000075 }
76
Guido van Rossumd624f182006-04-24 13:47:05 +000077 ((PyBytesObject *)self)->ob_bytes = sval;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000078 ((PyBytesObject *)self)->ob_size = size;
79
80 return 0;
81}
82
83/* Functions stuffed into the type object */
84
85static Py_ssize_t
86bytes_length(PyBytesObject *self)
87{
88 return self->ob_size;
89}
90
91static PyObject *
Guido van Rossumd624f182006-04-24 13:47:05 +000092bytes_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
118static PyObject *
Guido van Rossum13e57212006-04-27 22:54:26 +0000119bytes_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
143static PyObject *
Guido van Rossumd624f182006-04-24 13:47:05 +0000144bytes_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 Rossum13e57212006-04-27 22:54:26 +0000161 Py_ssize_t i;
Guido van Rossumd624f182006-04-24 13:47:05 +0000162 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
169static PyObject *
Guido van Rossum13e57212006-04-27 22:54:26 +0000170bytes_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
196static int
197bytes_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
215static int
216bytes_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
235static PyObject *
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000236bytes_getitem(PyBytesObject *self, Py_ssize_t i)
237{
238 if (i < 0)
Guido van Rossumd624f182006-04-24 13:47:05 +0000239 i += self->ob_size;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000240 if (i < 0 || i >= self->ob_size) {
Guido van Rossumd624f182006-04-24 13:47:05 +0000241 PyErr_SetString(PyExc_IndexError, "bytes index out of range");
242 return NULL;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000243 }
Guido van Rossumd624f182006-04-24 13:47:05 +0000244 return PyInt_FromLong((unsigned char)(self->ob_bytes[i]));
245}
246
247static PyObject *
248bytes_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
259static int
260bytes_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
328static int
329bytes_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 Rossum4dfe8a12006-04-22 23:28:04 +0000355}
356
357static long
358bytes_nohash(PyObject *self)
359{
360 PyErr_SetString(PyExc_TypeError, "bytes objects are unhashable");
361 return -1;
362}
363
364static int
365bytes_init(PyBytesObject *self, PyObject *args, PyObject *kwds)
366{
Guido van Rossumd624f182006-04-24 13:47:05 +0000367 static char *kwlist[] = {"source", "encoding", "errors", 0};
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000368 PyObject *arg = NULL;
Guido van Rossumd624f182006-04-24 13:47:05 +0000369 const char *encoding = NULL;
370 const char *errors = NULL;
371 Py_ssize_t count;
372 PyObject *it;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000373 PyObject *(*iternext)(PyObject *);
374
Guido van Rossumd624f182006-04-24 13:47:05 +0000375 /* Empty previous contents (yes, do this first of all!) */
376 if (PyBytes_Resize((PyObject *)self, 0) < 0)
377 return -1;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000378
Guido van Rossumd624f182006-04-24 13:47:05 +0000379 /* 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 Rossum4dfe8a12006-04-22 23:28:04 +0000392 }
393
Guido van Rossumd624f182006-04-24 13:47:05 +0000394 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 Rossum4dfe8a12006-04-22 23:28:04 +0000421
Guido van Rossumd624f182006-04-24 13:47:05 +0000422 /* 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 Rossum4dfe8a12006-04-22 23:28:04 +0000428
Guido van Rossumd624f182006-04-24 13:47:05 +0000429 /* 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 Rossum4dfe8a12006-04-22 23:28:04 +0000458
459 /* Get the iterator */
460 it = PyObject_GetIter(arg);
461 if (it == NULL)
Guido van Rossumd624f182006-04-24 13:47:05 +0000462 return -1;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000463 iternext = *it->ob_type->tp_iternext;
464
465 /* Run the iterator to exhaustion */
466 for (;;) {
Guido van Rossumd624f182006-04-24 13:47:05 +0000467 PyObject *item;
468 Py_ssize_t value;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000469
Guido van Rossumd624f182006-04-24 13:47:05 +0000470 /* 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 Rossum4dfe8a12006-04-22 23:28:04 +0000480
Guido van Rossumd624f182006-04-24 13:47:05 +0000481 /* 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 Rossum4dfe8a12006-04-22 23:28:04 +0000486
Guido van Rossumd624f182006-04-24 13:47:05 +0000487 /* 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 Rossum4dfe8a12006-04-22 23:28:04 +0000493
Guido van Rossumd624f182006-04-24 13:47:05 +0000494 /* 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 Rossum4dfe8a12006-04-22 23:28:04 +0000499 }
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
511static PyObject *
512bytes_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 Rossumd624f182006-04-24 13:47:05 +0000521 return PyString_FromString("bytes()");
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000522
523 list = PyList_New(0);
524 if (list == NULL)
Guido van Rossumd624f182006-04-24 13:47:05 +0000525 return NULL;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000526
527 str = PyString_FromString("bytes([");
528 if (str == NULL)
Guido van Rossumd624f182006-04-24 13:47:05 +0000529 goto error;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000530
531 err = PyList_Append(list, str);
532 Py_DECREF(str);
533 if (err < 0)
Guido van Rossumd624f182006-04-24 13:47:05 +0000534 goto error;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000535
536 for (i = 0; i < self->ob_size; i++) {
Guido van Rossumd624f182006-04-24 13:47:05 +0000537 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 Rossum4dfe8a12006-04-22 23:28:04 +0000546 }
547
548 str = PyString_FromString("])");
549 if (str == NULL)
Guido van Rossumd624f182006-04-24 13:47:05 +0000550 goto error;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000551
552 err = PyList_Append(list, str);
553 Py_DECREF(str);
554 if (err < 0)
Guido van Rossumd624f182006-04-24 13:47:05 +0000555 goto error;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000556
557 str = PyString_FromString("");
558 if (str == NULL)
Guido van Rossumd624f182006-04-24 13:47:05 +0000559 goto error;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000560
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
572static PyObject *
Guido van Rossumd624f182006-04-24 13:47:05 +0000573bytes_str(PyBytesObject *self)
574{
575 return PyString_FromStringAndSize(self->ob_bytes, self->ob_size);
576}
577
578static PyObject *
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000579bytes_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 Rossumd624f182006-04-24 13:47:05 +0000586 Py_INCREF(Py_NotImplemented);
587 return Py_NotImplemented;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000588 }
589
590 if (self->ob_size != other->ob_size && (op == Py_EQ || op == Py_NE)) {
Guido van Rossumd624f182006-04-24 13:47:05 +0000591 /* Shortcut: if the lengths differ, the objects differ */
592 cmp = (op == Py_NE);
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000593 }
594 else {
Guido van Rossumd624f182006-04-24 13:47:05 +0000595 minsize = self->ob_size;
596 if (other->ob_size < minsize)
597 minsize = other->ob_size;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000598
Guido van Rossumd624f182006-04-24 13:47:05 +0000599 cmp = memcmp(self->ob_bytes, other->ob_bytes, minsize);
600 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000601
Guido van Rossumd624f182006-04-24 13:47:05 +0000602 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 Rossum4dfe8a12006-04-22 23:28:04 +0000608
Guido van Rossumd624f182006-04-24 13:47:05 +0000609 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 Rossum4dfe8a12006-04-22 23:28:04 +0000617 }
618
619 res = cmp ? Py_True : Py_False;
620 Py_INCREF(res);
621 return res;
622}
623
624static void
625bytes_dealloc(PyBytesObject *self)
626{
Guido van Rossumd624f182006-04-24 13:47:05 +0000627 if (self->ob_bytes != 0) {
628 PyMem_Free(self->ob_bytes);
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000629 }
630 self->ob_type->tp_free((PyObject *)self);
631}
632
Guido van Rossumd624f182006-04-24 13:47:05 +0000633static Py_ssize_t
634bytes_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
645static Py_ssize_t
646bytes_getsegcount(PyStringObject *self, Py_ssize_t *lenp)
647{
648 if (lenp)
649 *lenp = self->ob_size;
650 return 1;
651}
652
653PyDoc_STRVAR(decode_doc,
654"B.decode([encoding[,errors]]) -> unicode obect.\n\
655\n\
656Decodes B using the codec registered for encoding. encoding defaults\n\
657to the default encoding. errors may be given to set a different error\n\
658handling scheme. Default is 'strict' meaning that encoding errors raise\n\
659a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\
660as well as any other name registerd with codecs.register_error that is\n\
661able to handle UnicodeDecodeErrors.");
662
663static PyObject *
664bytes_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 Rossum4dfe8a12006-04-22 23:28:04 +0000676static PySequenceMethods bytes_as_sequence = {
Guido van Rossumd624f182006-04-24 13:47:05 +0000677 (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 Rossumd624f182006-04-24 13:47:05 +0000684 (objobjproc)bytes_contains, /* sq_contains */
Guido van Rossum13e57212006-04-27 22:54:26 +0000685 (binaryfunc)bytes_iconcat, /* sq_inplace_concat */
686 (ssizeargfunc)bytes_irepeat, /* sq_inplace_repeat */
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000687};
688
689static PyMappingMethods bytes_as_mapping = {
Guido van Rossumd624f182006-04-24 13:47:05 +0000690 (lenfunc)bytes_length,
691 (binaryfunc)0,
692 0,
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000693};
694
695static PyBufferProcs bytes_as_buffer = {
Guido van Rossumd624f182006-04-24 13:47:05 +0000696 (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 Rossum4dfe8a12006-04-22 23:28:04 +0000702};
703
704static PyMethodDef
705bytes_methods[] = {
Guido van Rossumd624f182006-04-24 13:47:05 +0000706 {"decode", (PyCFunction)bytes_decode, METH_VARARGS, decode_doc},
707 {NULL, NULL}
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000708};
709
710PyDoc_STRVAR(bytes_doc,
711"bytes([iterable]) -> new array of bytes.\n\
712\n\
713If an argument is given it must be an iterable yielding ints in range(256).");
714
715PyTypeObject PyBytes_Type = {
716 PyObject_HEAD_INIT(&PyType_Type)
717 0,
718 "bytes",
719 sizeof(PyBytesObject),
720 0,
Guido van Rossumd624f182006-04-24 13:47:05 +0000721 (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 Rossum4dfe8a12006-04-22 23:28:04 +0000757};