blob: 36b442450082621434099ada95265798770a17b4 [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 *
119bytes_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
144static PyObject *
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000145bytes_getitem(PyBytesObject *self, Py_ssize_t i)
146{
147 if (i < 0)
Guido van Rossumd624f182006-04-24 13:47:05 +0000148 i += self->ob_size;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000149 if (i < 0 || i >= self->ob_size) {
Guido van Rossumd624f182006-04-24 13:47:05 +0000150 PyErr_SetString(PyExc_IndexError, "bytes index out of range");
151 return NULL;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000152 }
Guido van Rossumd624f182006-04-24 13:47:05 +0000153 return PyInt_FromLong((unsigned char)(self->ob_bytes[i]));
154}
155
156static PyObject *
157bytes_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
168static int
169bytes_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
237static int
238bytes_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 Rossum4dfe8a12006-04-22 23:28:04 +0000264}
265
266static long
267bytes_nohash(PyObject *self)
268{
269 PyErr_SetString(PyExc_TypeError, "bytes objects are unhashable");
270 return -1;
271}
272
273static int
274bytes_init(PyBytesObject *self, PyObject *args, PyObject *kwds)
275{
Guido van Rossumd624f182006-04-24 13:47:05 +0000276 static char *kwlist[] = {"source", "encoding", "errors", 0};
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000277 PyObject *arg = NULL;
Guido van Rossumd624f182006-04-24 13:47:05 +0000278 const char *encoding = NULL;
279 const char *errors = NULL;
280 Py_ssize_t count;
281 PyObject *it;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000282 PyObject *(*iternext)(PyObject *);
283
Guido van Rossumd624f182006-04-24 13:47:05 +0000284 /* Empty previous contents (yes, do this first of all!) */
285 if (PyBytes_Resize((PyObject *)self, 0) < 0)
286 return -1;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000287
Guido van Rossumd624f182006-04-24 13:47:05 +0000288 /* 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 Rossum4dfe8a12006-04-22 23:28:04 +0000301 }
302
Guido van Rossumd624f182006-04-24 13:47:05 +0000303 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 Rossum4dfe8a12006-04-22 23:28:04 +0000330
Guido van Rossumd624f182006-04-24 13:47:05 +0000331 /* 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 Rossum4dfe8a12006-04-22 23:28:04 +0000337
Guido van Rossumd624f182006-04-24 13:47:05 +0000338 /* 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 Rossum4dfe8a12006-04-22 23:28:04 +0000367
368 /* Get the iterator */
369 it = PyObject_GetIter(arg);
370 if (it == NULL)
Guido van Rossumd624f182006-04-24 13:47:05 +0000371 return -1;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000372 iternext = *it->ob_type->tp_iternext;
373
374 /* Run the iterator to exhaustion */
375 for (;;) {
Guido van Rossumd624f182006-04-24 13:47:05 +0000376 PyObject *item;
377 Py_ssize_t value;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000378
Guido van Rossumd624f182006-04-24 13:47:05 +0000379 /* 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 Rossum4dfe8a12006-04-22 23:28:04 +0000389
Guido van Rossumd624f182006-04-24 13:47:05 +0000390 /* 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 Rossum4dfe8a12006-04-22 23:28:04 +0000395
Guido van Rossumd624f182006-04-24 13:47:05 +0000396 /* 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 Rossum4dfe8a12006-04-22 23:28:04 +0000402
Guido van Rossumd624f182006-04-24 13:47:05 +0000403 /* 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 Rossum4dfe8a12006-04-22 23:28:04 +0000408 }
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
420static PyObject *
421bytes_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 Rossumd624f182006-04-24 13:47:05 +0000430 return PyString_FromString("bytes()");
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000431
432 list = PyList_New(0);
433 if (list == NULL)
Guido van Rossumd624f182006-04-24 13:47:05 +0000434 return NULL;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000435
436 str = PyString_FromString("bytes([");
437 if (str == NULL)
Guido van Rossumd624f182006-04-24 13:47:05 +0000438 goto error;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000439
440 err = PyList_Append(list, str);
441 Py_DECREF(str);
442 if (err < 0)
Guido van Rossumd624f182006-04-24 13:47:05 +0000443 goto error;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000444
445 for (i = 0; i < self->ob_size; i++) {
Guido van Rossumd624f182006-04-24 13:47:05 +0000446 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 Rossum4dfe8a12006-04-22 23:28:04 +0000455 }
456
457 str = PyString_FromString("])");
458 if (str == NULL)
Guido van Rossumd624f182006-04-24 13:47:05 +0000459 goto error;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000460
461 err = PyList_Append(list, str);
462 Py_DECREF(str);
463 if (err < 0)
Guido van Rossumd624f182006-04-24 13:47:05 +0000464 goto error;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000465
466 str = PyString_FromString("");
467 if (str == NULL)
Guido van Rossumd624f182006-04-24 13:47:05 +0000468 goto error;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000469
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
481static PyObject *
Guido van Rossumd624f182006-04-24 13:47:05 +0000482bytes_str(PyBytesObject *self)
483{
484 return PyString_FromStringAndSize(self->ob_bytes, self->ob_size);
485}
486
487static PyObject *
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000488bytes_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 Rossumd624f182006-04-24 13:47:05 +0000495 Py_INCREF(Py_NotImplemented);
496 return Py_NotImplemented;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000497 }
498
499 if (self->ob_size != other->ob_size && (op == Py_EQ || op == Py_NE)) {
Guido van Rossumd624f182006-04-24 13:47:05 +0000500 /* Shortcut: if the lengths differ, the objects differ */
501 cmp = (op == Py_NE);
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000502 }
503 else {
Guido van Rossumd624f182006-04-24 13:47:05 +0000504 minsize = self->ob_size;
505 if (other->ob_size < minsize)
506 minsize = other->ob_size;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000507
Guido van Rossumd624f182006-04-24 13:47:05 +0000508 cmp = memcmp(self->ob_bytes, other->ob_bytes, minsize);
509 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000510
Guido van Rossumd624f182006-04-24 13:47:05 +0000511 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 Rossum4dfe8a12006-04-22 23:28:04 +0000517
Guido van Rossumd624f182006-04-24 13:47:05 +0000518 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 Rossum4dfe8a12006-04-22 23:28:04 +0000526 }
527
528 res = cmp ? Py_True : Py_False;
529 Py_INCREF(res);
530 return res;
531}
532
533static void
534bytes_dealloc(PyBytesObject *self)
535{
Guido van Rossumd624f182006-04-24 13:47:05 +0000536 if (self->ob_bytes != 0) {
537 PyMem_Free(self->ob_bytes);
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000538 }
539 self->ob_type->tp_free((PyObject *)self);
540}
541
Guido van Rossumd624f182006-04-24 13:47:05 +0000542static Py_ssize_t
543bytes_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
554static Py_ssize_t
555bytes_getsegcount(PyStringObject *self, Py_ssize_t *lenp)
556{
557 if (lenp)
558 *lenp = self->ob_size;
559 return 1;
560}
561
562PyDoc_STRVAR(decode_doc,
563"B.decode([encoding[,errors]]) -> unicode obect.\n\
564\n\
565Decodes B using the codec registered for encoding. encoding defaults\n\
566to the default encoding. errors may be given to set a different error\n\
567handling scheme. Default is 'strict' meaning that encoding errors raise\n\
568a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\
569as well as any other name registerd with codecs.register_error that is\n\
570able to handle UnicodeDecodeErrors.");
571
572static PyObject *
573bytes_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 Rossum4dfe8a12006-04-22 23:28:04 +0000585static PySequenceMethods bytes_as_sequence = {
Guido van Rossumd624f182006-04-24 13:47:05 +0000586 (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 Rossum4dfe8a12006-04-22 23:28:04 +0000598};
599
600static PyMappingMethods bytes_as_mapping = {
Guido van Rossumd624f182006-04-24 13:47:05 +0000601 (lenfunc)bytes_length,
602 (binaryfunc)0,
603 0,
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000604};
605
606static PyBufferProcs bytes_as_buffer = {
Guido van Rossumd624f182006-04-24 13:47:05 +0000607 (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 Rossum4dfe8a12006-04-22 23:28:04 +0000613};
614
615static PyMethodDef
616bytes_methods[] = {
Guido van Rossumd624f182006-04-24 13:47:05 +0000617 {"decode", (PyCFunction)bytes_decode, METH_VARARGS, decode_doc},
618 {NULL, NULL}
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000619};
620
621PyDoc_STRVAR(bytes_doc,
622"bytes([iterable]) -> new array of bytes.\n\
623\n\
624If an argument is given it must be an iterable yielding ints in range(256).");
625
626PyTypeObject PyBytes_Type = {
627 PyObject_HEAD_INIT(&PyType_Type)
628 0,
629 "bytes",
630 sizeof(PyBytesObject),
631 0,
Guido van Rossumd624f182006-04-24 13:47:05 +0000632 (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 Rossum4dfe8a12006-04-22 23:28:04 +0000668};