annotate Objects/bytesobject.c @ 39222:b0cdcbcc5f45

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