Mercurial > cpython-lru_cache-weakref
annotate Objects/bytesobject.c @ 39643:3dd61ffd049e
Fix buglet in slice assignment of bytesobjects: assigning to b[3:0] ('stop'
being before 'start') would actually assign to b[0:0] (or whatever 'stop'
was)
| author | Thomas Wouters <thomas@python.org> |
|---|---|
| date | Wed, 23 Aug 2006 23:20:29 +0000 |
| parents | 8d71f9a6c1c3 |
| children | 9548ca0e6560 |
| 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 |
|
39631
8d71f9a6c1c3
Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents:
39610
diff
changeset
|
248 ival = PyNumber_AsSsize_t(value, PyExc_ValueError); |
|
38017
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 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
|
252 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
|
253 return -1; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
254 } |
|
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 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
|
257 } |
|
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 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
|
260 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
|
261 { |
|
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 if (i < 0) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
263 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
|
264 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
|
265 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
|
266 return NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
267 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
268 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
|
269 } |
|
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 static PyObject * |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
272 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
|
273 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
274 if (lo < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
275 lo = 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
276 if (hi > self->ob_size) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
277 hi = self->ob_size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
278 if (lo >= hi) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
279 lo = hi = 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
280 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
|
281 } |
|
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 static int |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
284 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
|
285 PyObject *values) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
286 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
287 int avail; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
288 int needed; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
289 char *bytes; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
290 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
291 if (values == NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
292 bytes = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
293 needed = 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
294 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
295 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
|
296 /* 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
|
297 int err; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
298 values = PyBytes_FromObject(values); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
299 if (values == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
300 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
301 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
|
302 Py_DECREF(values); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
303 return err; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
304 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
305 else { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
306 assert(PyBytes_Check(values)); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
307 bytes = ((PyBytesObject *)values)->ob_bytes; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
308 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
|
309 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
310 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
311 if (lo < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
312 lo = 0; |
|
39643
3dd61ffd049e
Fix buglet in slice assignment of bytesobjects: assigning to b[3:0] ('stop'
Thomas Wouters <thomas@python.org>
parents:
39631
diff
changeset
|
313 if (hi < lo) |
|
3dd61ffd049e
Fix buglet in slice assignment of bytesobjects: assigning to b[3:0] ('stop'
Thomas Wouters <thomas@python.org>
parents:
39631
diff
changeset
|
314 hi = lo; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
315 if (hi > self->ob_size) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
316 hi = self->ob_size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
317 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
318 avail = hi - lo; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
319 if (avail < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
320 lo = hi = avail = 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
321 |
|
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 if (avail > needed) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
324 /* |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
325 0 lo hi old_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
326 | |<----avail----->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
327 | |<-needed->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
328 0 lo new_hi new_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
329 */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
330 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
|
331 self->ob_size - hi); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
332 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
333 if (PyBytes_Resize((PyObject *)self, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
334 self->ob_size + needed - avail) < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
335 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
336 if (avail < needed) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
337 /* |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
338 0 lo hi old_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
339 | |<-avail->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
340 | |<----needed---->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
341 0 lo new_hi new_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
342 */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
343 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
|
344 self->ob_size - lo - needed); |
|
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 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
348 if (needed > 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
349 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
|
350 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
351 return 0; |
|
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 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
354 static int |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
355 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
|
356 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
357 Py_ssize_t ival; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
358 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
359 if (i < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
360 i += self->ob_size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
361 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
362 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
|
363 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
|
364 return -1; |
|
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 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
367 if (value == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
368 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
|
369 |
|
39631
8d71f9a6c1c3
Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents:
39610
diff
changeset
|
370 ival = PyNumber_AsSsize_t(value, PyExc_ValueError); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
371 if (ival == -1 && PyErr_Occurred()) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
372 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
373 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
374 if (ival < 0 || ival >= 256) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
375 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
|
376 return -1; |
|
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 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
379 self->ob_bytes[i] = ival; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
380 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
|
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 |
|
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 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
|
384 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
|
385 { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
386 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
|
387 PyObject *arg = NULL; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
388 const char *encoding = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
389 const char *errors = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
390 Py_ssize_t count; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
391 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
|
392 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
|
393 |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
394 if (self->ob_size != 0) { |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
395 /* Empty previous contents (yes, do this first of all!) */ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
396 if (PyBytes_Resize((PyObject *)self, 0) < 0) |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
397 return -1; |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
398 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
399 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
400 /* Parse arguments */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
401 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
|
402 &arg, &encoding, &errors)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
403 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
|
404 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
405 /* 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
|
406 if (arg == NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
407 if (encoding != NULL || errors != NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
408 PyErr_SetString(PyExc_TypeError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
409 "encoding or errors without sequence argument"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
410 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
411 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
412 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
|
413 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
414 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
415 if (PyUnicode_Check(arg)) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
416 /* Encode via the codec registry */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
417 PyObject *encoded; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
418 char *bytes; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
419 Py_ssize_t size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
420 if (encoding == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
421 encoding = PyUnicode_GetDefaultEncoding(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
422 encoded = PyCodec_Encode(arg, encoding, errors); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
423 if (encoded == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
424 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
425 if (!PyString_Check(encoded)) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
426 PyErr_Format(PyExc_TypeError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
427 "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
|
428 encoded->ob_type->tp_name); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
429 Py_DECREF(encoded); |
|
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 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
432 bytes = PyString_AS_STRING(encoded); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
433 size = PyString_GET_SIZE(encoded); |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
434 if (size <= self->ob_alloc) |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
435 self->ob_size = size; |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
436 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
|
437 Py_DECREF(encoded); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
438 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
439 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
440 memcpy(self->ob_bytes, bytes, size); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
441 Py_DECREF(encoded); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
442 return 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
443 } |
|
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
|
444 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
445 /* 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
|
446 if (encoding != NULL || errors != NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
447 PyErr_SetString(PyExc_TypeError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
448 "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
|
449 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
450 } |
|
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
|
451 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
452 /* Is it an int? */ |
|
39631
8d71f9a6c1c3
Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents:
39610
diff
changeset
|
453 count = PyNumber_AsSsize_t(arg, PyExc_ValueError); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
454 if (count == -1 && PyErr_Occurred()) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
455 PyErr_Clear(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
456 else { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
457 if (count < 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
458 PyErr_SetString(PyExc_ValueError, "negative count"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
459 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
460 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
461 if (count > 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
462 if (PyBytes_Resize((PyObject *)self, count)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
463 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
464 memset(self->ob_bytes, 0, count); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
465 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
466 return 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
467 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
468 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
469 if (PyObject_CheckReadBuffer(arg)) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
470 const void *bytes; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
471 Py_ssize_t size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
472 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
|
473 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
474 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
|
475 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
476 memcpy(self->ob_bytes, bytes, size); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
477 return 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
478 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
479 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
480 /* 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
|
481 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
482 /* 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
|
483 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
|
484 if (it == NULL) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
485 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
|
486 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
|
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 /* 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
|
489 for (;;) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
490 PyObject *item; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
491 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
|
492 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
493 /* Get the next item */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
494 item = iternext(it); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
495 if (item == NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
496 if (PyErr_Occurred()) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
497 if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
498 goto error; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
499 PyErr_Clear(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
500 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
501 break; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
502 } |
|
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
|
503 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
504 /* Interpret it as an int (__index__) */ |
|
39631
8d71f9a6c1c3
Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents:
39610
diff
changeset
|
505 value = PyNumber_AsSsize_t(item, PyExc_ValueError); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
506 Py_DECREF(item); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
507 if (value == -1 && PyErr_Occurred()) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
508 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
|
509 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
510 /* Range check */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
511 if (value < 0 || value >= 256) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
512 PyErr_SetString(PyExc_ValueError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
513 "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
|
514 goto error; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
515 } |
|
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
|
516 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
517 /* Append the byte */ |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
518 if (self->ob_size < self->ob_alloc) |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
519 self->ob_size++; |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
520 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
|
521 goto error; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
522 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
|
523 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
524 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
525 /* 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
|
526 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
|
527 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
|
528 |
|
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 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
|
530 /* 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
|
531 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
|
532 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
|
533 } |
|
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 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
|
536 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
|
537 { |
|
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 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
|
539 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
|
540 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
|
541 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
|
542 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
|
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 if (self->ob_size == 0) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
545 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
|
546 |
|
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 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
|
548 if (list == NULL) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
549 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
|
550 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
551 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
|
552 if (str == NULL) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
553 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
|
554 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
555 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
|
556 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
|
557 if (err < 0) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
558 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
|
559 |
|
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 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
|
561 char buffer[20]; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
562 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
|
563 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
|
564 if (str == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
565 goto error; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
566 err = PyList_Append(list, str); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
567 Py_DECREF(str); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
568 if (err < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
569 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
|
570 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
571 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
572 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
|
573 if (str == NULL) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
574 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
|
575 |
|
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 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
|
577 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
|
578 if (err < 0) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
579 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
|
580 |
|
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 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
|
582 if (str == NULL) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
583 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
|
584 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
585 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
|
586 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
|
587 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
|
588 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
|
589 |
|
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 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
|
591 /* 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
|
592 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
|
593 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
|
594 } |
|
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 static PyObject * |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
597 bytes_str(PyBytesObject *self) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
598 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
599 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
|
600 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
601 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
602 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
|
603 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
|
604 { |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
605 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
|
606 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
|
607 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
|
608 |
|
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 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
|
610 Py_INCREF(Py_NotImplemented); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
611 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
|
612 } |
|
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 |
|
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 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
|
615 /* 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
|
616 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
|
617 } |
|
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 else { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
619 minsize = self->ob_size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
620 if (other->ob_size < minsize) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
621 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
|
622 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
623 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
|
624 /* 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
|
625 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
626 if (cmp == 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
627 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
|
628 cmp = -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
629 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
|
630 cmp = 1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
631 } |
|
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
|
632 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
633 switch (op) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
634 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
|
635 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
|
636 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
|
637 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
|
638 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
|
639 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
|
640 } |
|
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
|
641 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
642 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
643 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
|
644 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
|
645 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
|
646 } |
|
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 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
|
649 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
|
650 { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
651 if (self->ob_bytes != 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
652 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
|
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 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
|
655 } |
|
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 static Py_ssize_t |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
658 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
|
659 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
660 if (index != 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
661 PyErr_SetString(PyExc_SystemError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
662 "accessing non-existent string segment"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
663 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
664 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
665 *ptr = (void *)self->ob_bytes; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
666 return self->ob_size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
667 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
668 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
669 static Py_ssize_t |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
670 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
|
671 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
672 if (lenp) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
673 *lenp = self->ob_size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
674 return 1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
675 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
676 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
677 PyDoc_STRVAR(decode_doc, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
678 "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
|
679 \n\ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
680 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
|
681 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
|
682 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
|
683 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
|
684 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
|
685 able to handle UnicodeDecodeErrors."); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
686 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
687 static PyObject * |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
688 bytes_decode(PyObject *self, PyObject *args) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
689 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
690 const char *encoding = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
691 const char *errors = NULL; |
|
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 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
|
694 return NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
695 if (encoding == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
696 encoding = PyUnicode_GetDefaultEncoding(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
697 return PyCodec_Decode(self, encoding, errors); |
|
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 |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
700 PyDoc_STRVAR(alloc_doc, |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
701 "B.__alloc__() -> int\n\ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
702 \n\ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
703 Returns the number of bytes actually allocated."); |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
704 |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
705 static PyObject * |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
706 bytes_alloc(PyBytesObject *self) |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
707 { |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
708 return PyInt_FromSsize_t(self->ob_alloc); |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
709 } |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
710 |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
711 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
|
712 "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
|
713 \n\ |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
714 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
|
715 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
|
716 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
717 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
|
718 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
|
719 { |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
720 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
|
721 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
|
722 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
|
723 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
|
724 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
|
725 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
|
726 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
|
727 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
728 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
|
729 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
|
730 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
|
731 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
|
732 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
|
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 /* 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
|
735 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
|
736 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
|
737 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
|
738 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
|
739 "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
|
740 "(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
|
741 /* 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
|
742 (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
|
743 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
|
744 } |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
745 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
|
746 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
|
747 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
|
748 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
|
749 } |
|
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 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
752 /* 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
|
753 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
|
754 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
|
755 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
|
756 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
|
757 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
|
758 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
|
759 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
|
760 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
|
761 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
|
762 } |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
763 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
764 /* Done */ |
|
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_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
|
766 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
|
767 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
768 /* 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
|
769 error: |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
770 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
|
771 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
|
772 } |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
773 |
|
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
|
774 static PySequenceMethods bytes_as_sequence = { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
775 (lenfunc)bytes_length, /*sq_length*/ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
776 (binaryfunc)bytes_concat, /*sq_concat*/ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
777 (ssizeargfunc)bytes_repeat, /*sq_repeat*/ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
778 (ssizeargfunc)bytes_getitem, /*sq_item*/ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
779 (ssizessizeargfunc)bytes_getslice, /*sq_slice*/ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
780 (ssizeobjargproc)bytes_setitem, /*sq_ass_item*/ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
781 (ssizessizeobjargproc)bytes_setslice, /* sq_ass_slice */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
782 (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
|
783 (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
|
784 (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
|
785 }; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
786 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
787 static PyMappingMethods bytes_as_mapping = { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
788 (lenfunc)bytes_length, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
789 (binaryfunc)0, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
790 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
|
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 PyBufferProcs bytes_as_buffer = { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
794 (readbufferproc)bytes_getbuffer, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
795 (writebufferproc)bytes_getbuffer, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
796 (segcountproc)bytes_getsegcount, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
797 /* 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
|
798 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
|
799 (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
|
800 }; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
801 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
802 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
|
803 bytes_methods[] = { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
804 {"decode", (PyCFunction)bytes_decode, METH_VARARGS, decode_doc}, |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
805 {"__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
|
806 {"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
|
807 {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
|
808 }; |
|
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 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
810 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
|
811 "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
|
812 \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
|
813 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
|
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 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
|
816 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
|
817 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
|
818 "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
|
819 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
|
820 0, |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
821 (destructor)bytes_dealloc, /* tp_dealloc */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
822 0, /* tp_print */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
823 0, /* tp_getattr */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
824 0, /* tp_setattr */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
825 0, /* tp_compare */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
826 (reprfunc)bytes_repr, /* tp_repr */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
827 0, /* tp_as_number */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
828 &bytes_as_sequence, /* tp_as_sequence */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
829 &bytes_as_mapping, /* tp_as_mapping */ |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39222
diff
changeset
|
830 0, /* tp_hash */ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
831 0, /* tp_call */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
832 (reprfunc)bytes_str, /* tp_str */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
833 PyObject_GenericGetAttr, /* tp_getattro */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
834 0, /* tp_setattro */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
835 &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
|
836 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
837 /* bytes is 'final' or 'sealed' */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
838 bytes_doc, /* tp_doc */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
839 0, /* tp_traverse */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
840 0, /* tp_clear */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
841 (richcmpfunc)bytes_richcompare, /* tp_richcompare */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
842 0, /* tp_weaklistoffset */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
843 0, /* tp_iter */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
844 0, /* tp_iternext */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
845 bytes_methods, /* tp_methods */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
846 0, /* tp_members */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
847 0, /* tp_getset */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
848 0, /* tp_base */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
849 0, /* tp_dict */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
850 0, /* tp_descr_get */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
851 0, /* tp_descr_set */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
852 0, /* tp_dictoffset */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
853 (initproc)bytes_init, /* tp_init */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
854 PyType_GenericAlloc, /* tp_alloc */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
855 PyType_GenericNew, /* tp_new */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
856 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
|
857 }; |
