Mercurial > cpython-lru_cache-weakref
annotate Objects/bytesobject.c @ 40968:2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Tue, 27 Feb 2007 08:49:18 +0000 |
| parents | a9e6c73e0fa5 |
| children | 61fee1853ca8 |
| 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 * |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
272 bytes_subscript(PyBytesObject *self, PyObject *item) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
273 { |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
274 if (PyIndex_Check(item)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
275 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
276 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
277 if (i == -1 && PyErr_Occurred()) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
278 return NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
279 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
280 if (i < 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
281 i += PyBytes_GET_SIZE(self); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
282 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
283 if (i < 0 || i >= self->ob_size) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
284 PyErr_SetString(PyExc_IndexError, "bytes index out of range"); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
285 return NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
286 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
287 return PyInt_FromLong((unsigned char)(self->ob_bytes[i])); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
288 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
289 else if (PySlice_Check(item)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
290 Py_ssize_t start, stop, step, slicelength, cur, i; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
291 if (PySlice_GetIndicesEx((PySliceObject *)item, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
292 PyBytes_GET_SIZE(self), |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
293 &start, &stop, &step, &slicelength) < 0) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
294 return NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
295 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
296 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
297 if (slicelength <= 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
298 return PyBytes_FromStringAndSize("", 0); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
299 else if (step == 1) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
300 return PyBytes_FromStringAndSize(self->ob_bytes + start, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
301 slicelength); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
302 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
303 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
304 char *source_buf = PyBytes_AS_STRING(self); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
305 char *result_buf = (char *)PyMem_Malloc(slicelength); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
306 PyObject *result; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
307 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
308 if (result_buf == NULL) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
309 return PyErr_NoMemory(); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
310 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
311 for (cur = start, i = 0; i < slicelength; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
312 cur += step, i++) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
313 result_buf[i] = source_buf[cur]; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
314 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
315 result = PyBytes_FromStringAndSize(result_buf, slicelength); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
316 PyMem_Free(result_buf); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
317 return result; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
318 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
319 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
320 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
321 PyErr_SetString(PyExc_TypeError, "bytes indices must be integers"); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
322 return NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
323 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
324 } |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
325 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
326 static int |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
327 bytes_setslice(PyBytesObject *self, Py_ssize_t lo, Py_ssize_t hi, |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
328 PyObject *values) |
|
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 int avail; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
331 int needed; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
332 char *bytes; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
333 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
334 if (values == NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
335 bytes = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
336 needed = 0; |
|
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 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
|
339 /* 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
|
340 int err; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
341 values = PyBytes_FromObject(values); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
342 if (values == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
343 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
344 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
|
345 Py_DECREF(values); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
346 return err; |
|
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 else { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
349 assert(PyBytes_Check(values)); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
350 bytes = ((PyBytesObject *)values)->ob_bytes; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
351 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
|
352 } |
|
37960
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 if (lo < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
355 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
|
356 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
|
357 hi = lo; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
358 if (hi > self->ob_size) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
359 hi = self->ob_size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
360 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
361 avail = hi - lo; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
362 if (avail < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
363 lo = hi = avail = 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
364 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
365 if (avail != needed) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
366 if (avail > needed) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
367 /* |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
368 0 lo hi old_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
369 | |<----avail----->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
370 | |<-needed->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
371 0 lo new_hi new_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
372 */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
373 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
|
374 self->ob_size - hi); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
375 } |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
376 if (PyBytes_Resize((PyObject *)self, |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
377 self->ob_size + needed - avail) < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
378 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
379 if (avail < needed) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
380 /* |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
381 0 lo hi old_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
382 | |<-avail->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
383 | |<----needed---->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
384 0 lo new_hi new_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
385 */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
386 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
|
387 self->ob_size - lo - needed); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
388 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
389 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
390 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
391 if (needed > 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
392 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
|
393 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
394 return 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
395 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
396 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
397 static int |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
398 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
|
399 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
400 Py_ssize_t ival; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
401 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
402 if (i < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
403 i += self->ob_size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
404 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
405 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
|
406 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
|
407 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
408 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
409 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
410 if (value == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
411 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
|
412 |
|
39631
8d71f9a6c1c3
Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents:
39610
diff
changeset
|
413 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
|
414 if (ival == -1 && PyErr_Occurred()) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
415 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
416 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
417 if (ival < 0 || ival >= 256) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
418 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
|
419 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
420 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
421 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
422 self->ob_bytes[i] = ival; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
423 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
|
424 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
425 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
426 static int |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
427 bytes_ass_subscript(PyBytesObject *self, PyObject *item, PyObject *values) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
428 { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
429 Py_ssize_t start, stop, step, slicelen, needed; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
430 char *bytes; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
431 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
432 if (PyIndex_Check(item)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
433 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
434 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
435 if (i == -1 && PyErr_Occurred()) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
436 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
437 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
438 if (i < 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
439 i += PyBytes_GET_SIZE(self); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
440 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
441 if (i < 0 || i >= self->ob_size) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
442 PyErr_SetString(PyExc_IndexError, "bytes index out of range"); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
443 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
444 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
445 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
446 if (values == NULL) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
447 /* Fall through to slice assignment */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
448 start = i; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
449 stop = i + 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
450 step = 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
451 slicelen = 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
452 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
453 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
454 Py_ssize_t ival = PyNumber_AsSsize_t(values, PyExc_ValueError); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
455 if (ival == -1 && PyErr_Occurred()) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
456 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
457 if (ival < 0 || ival >= 256) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
458 PyErr_SetString(PyExc_ValueError, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
459 "byte must be in range(0, 256)"); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
460 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
461 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
462 self->ob_bytes[i] = (char)ival; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
463 return 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
464 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
465 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
466 else if (PySlice_Check(item)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
467 if (PySlice_GetIndicesEx((PySliceObject *)item, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
468 PyBytes_GET_SIZE(self), |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
469 &start, &stop, &step, &slicelen) < 0) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
470 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
471 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
472 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
473 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
474 PyErr_SetString(PyExc_TypeError, "bytes indices must be integer"); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
475 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
476 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
477 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
478 if (values == NULL) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
479 bytes = NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
480 needed = 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
481 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
482 else if (values == (PyObject *)self || !PyBytes_Check(values)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
483 /* Make a copy an call this function recursively */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
484 int err; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
485 values = PyBytes_FromObject(values); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
486 if (values == NULL) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
487 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
488 err = bytes_ass_subscript(self, item, values); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
489 Py_DECREF(values); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
490 return err; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
491 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
492 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
493 assert(PyBytes_Check(values)); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
494 bytes = ((PyBytesObject *)values)->ob_bytes; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
495 needed = ((PyBytesObject *)values)->ob_size; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
496 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
497 /* Make sure b[5:2] = ... inserts before 5, not before 2. */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
498 if ((step < 0 && start < stop) || |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
499 (step > 0 && start > stop)) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
500 stop = start; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
501 if (step == 1) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
502 if (slicelen != needed) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
503 if (slicelen > needed) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
504 /* |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
505 0 start stop old_size |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
506 | |<---slicelen--->|<-----tomove------>| |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
507 | |<-needed->|<-----tomove------>| |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
508 0 lo new_hi new_size |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
509 */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
510 memmove(self->ob_bytes + start + needed, self->ob_bytes + stop, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
511 self->ob_size - stop); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
512 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
513 if (PyBytes_Resize((PyObject *)self, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
514 self->ob_size + needed - slicelen) < 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
515 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
516 if (slicelen < needed) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
517 /* |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
518 0 lo hi old_size |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
519 | |<-avail->|<-----tomove------>| |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
520 | |<----needed---->|<-----tomove------>| |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
521 0 lo new_hi new_size |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
522 */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
523 memmove(self->ob_bytes + start + needed, self->ob_bytes + stop, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
524 self->ob_size - start - needed); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
525 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
526 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
527 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
528 if (needed > 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
529 memcpy(self->ob_bytes + start, bytes, needed); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
530 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
531 return 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
532 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
533 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
534 if (needed == 0) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
535 /* Delete slice */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
536 Py_ssize_t cur, i; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
537 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
538 if (step < 0) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
539 stop = start + 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
540 start = stop + step * (slicelen - 1) - 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
541 step = -step; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
542 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
543 for (cur = start, i = 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
544 i < slicelen; cur += step, i++) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
545 Py_ssize_t lim = step - 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
546 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
547 if (cur + step >= PyBytes_GET_SIZE(self)) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
548 lim = PyBytes_GET_SIZE(self) - cur - 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
549 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
550 memmove(self->ob_bytes + cur - i, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
551 self->ob_bytes + cur + 1, lim); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
552 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
553 /* Move the tail of the bytes, in one chunk */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
554 cur = start + slicelen*step; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
555 if (cur < PyBytes_GET_SIZE(self)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
556 memmove(self->ob_bytes + cur - slicelen, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
557 self->ob_bytes + cur, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
558 PyBytes_GET_SIZE(self) - cur); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
559 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
560 if (PyBytes_Resize((PyObject *)self, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
561 PyBytes_GET_SIZE(self) - slicelen) < 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
562 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
563 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
564 return 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
565 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
566 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
567 /* Assign slice */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
568 Py_ssize_t cur, i; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
569 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
570 if (needed != slicelen) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
571 PyErr_Format(PyExc_ValueError, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
572 "attempt to assign bytes of size %zd " |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
573 "to extended slice of size %zd", |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
574 needed, slicelen); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
575 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
576 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
577 for (cur = start, i = 0; i < slicelen; cur += step, i++) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
578 self->ob_bytes[cur] = bytes[i]; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
579 return 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
580 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
581 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
582 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
583 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
584 static int |
|
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
|
585 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
|
586 { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
587 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
|
588 PyObject *arg = NULL; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
589 const char *encoding = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
590 const char *errors = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
591 Py_ssize_t count; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
592 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
|
593 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
|
594 |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
595 if (self->ob_size != 0) { |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
596 /* Empty previous contents (yes, do this first of all!) */ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
597 if (PyBytes_Resize((PyObject *)self, 0) < 0) |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
598 return -1; |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
599 } |
|
37960
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 /* Parse arguments */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
602 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
|
603 &arg, &encoding, &errors)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
604 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
|
605 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
606 /* 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
|
607 if (arg == NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
608 if (encoding != NULL || errors != NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
609 PyErr_SetString(PyExc_TypeError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
610 "encoding or errors without sequence argument"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
611 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
612 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
613 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
|
614 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
615 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
616 if (PyUnicode_Check(arg)) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
617 /* Encode via the codec registry */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
618 PyObject *encoded; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
619 char *bytes; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
620 Py_ssize_t size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
621 if (encoding == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
622 encoding = PyUnicode_GetDefaultEncoding(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
623 encoded = PyCodec_Encode(arg, encoding, errors); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
624 if (encoded == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
625 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
626 if (!PyString_Check(encoded)) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
627 PyErr_Format(PyExc_TypeError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
628 "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
|
629 encoded->ob_type->tp_name); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
630 Py_DECREF(encoded); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
631 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
632 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
633 bytes = PyString_AS_STRING(encoded); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
634 size = PyString_GET_SIZE(encoded); |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
635 if (size <= self->ob_alloc) |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
636 self->ob_size = size; |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
637 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
|
638 Py_DECREF(encoded); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
639 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
640 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
641 memcpy(self->ob_bytes, bytes, size); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
642 Py_DECREF(encoded); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
643 return 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
644 } |
|
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
|
645 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
646 /* 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
|
647 if (encoding != NULL || errors != NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
648 PyErr_SetString(PyExc_TypeError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
649 "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
|
650 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
651 } |
|
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
|
652 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
653 /* 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
|
654 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
|
655 if (count == -1 && PyErr_Occurred()) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
656 PyErr_Clear(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
657 else { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
658 if (count < 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
659 PyErr_SetString(PyExc_ValueError, "negative count"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
660 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
661 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
662 if (count > 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
663 if (PyBytes_Resize((PyObject *)self, count)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
664 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
665 memset(self->ob_bytes, 0, count); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
666 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
667 return 0; |
|
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 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
670 if (PyObject_CheckReadBuffer(arg)) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
671 const void *bytes; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
672 Py_ssize_t size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
673 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
|
674 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
675 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
|
676 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
677 memcpy(self->ob_bytes, bytes, size); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
678 return 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
679 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
680 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
681 /* 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
|
682 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
683 /* 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
|
684 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
|
685 if (it == NULL) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
686 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
|
687 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
|
688 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
689 /* 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
|
690 for (;;) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
691 PyObject *item; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
692 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
|
693 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
694 /* Get the next item */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
695 item = iternext(it); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
696 if (item == NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
697 if (PyErr_Occurred()) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
698 if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
699 goto error; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
700 PyErr_Clear(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
701 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
702 break; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
703 } |
|
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
|
704 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
705 /* 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
|
706 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
|
707 Py_DECREF(item); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
708 if (value == -1 && PyErr_Occurred()) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
709 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
|
710 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
711 /* Range check */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
712 if (value < 0 || value >= 256) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
713 PyErr_SetString(PyExc_ValueError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
714 "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
|
715 goto error; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
716 } |
|
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
|
717 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
718 /* Append the byte */ |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
719 if (self->ob_size < self->ob_alloc) |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
720 self->ob_size++; |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
721 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
|
722 goto error; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
723 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
|
724 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
725 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
726 /* 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
|
727 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
|
728 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
|
729 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
730 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
|
731 /* 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
|
732 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
|
733 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
|
734 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
735 |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
736 /* Mostly copied from string_repr, but without the |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
737 "smart quote" functionality. */ |
|
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
|
738 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
|
739 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
|
740 { |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
741 size_t newsize = 3 + 4 * self->ob_size; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
742 PyObject *v; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
743 if (newsize > PY_SSIZE_T_MAX || newsize / 4 != self->ob_size) { |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
744 PyErr_SetString(PyExc_OverflowError, |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
745 "bytes object is too large to make repr"); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
746 return NULL; |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
747 } |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
748 v = PyString_FromStringAndSize((char *)NULL, newsize); |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
749 if (v == NULL) { |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
750 return NULL; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
751 } |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
752 else { |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
753 register Py_ssize_t i; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
754 register char c; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
755 register char *p; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
756 int quote = '\''; |
|
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
|
757 |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
758 p = PyString_AS_STRING(v); |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
759 *p++ = 'b'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
760 *p++ = quote; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
761 for (i = 0; i < self->ob_size; i++) { |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
762 /* There's at least enough room for a hex escape |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
763 and a closing quote. */ |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
764 assert(newsize - (p - PyString_AS_STRING(v)) >= 5); |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
765 c = self->ob_bytes[i]; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
766 if (c == quote || c == '\\') |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
767 *p++ = '\\', *p++ = c; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
768 else if (c == '\t') |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
769 *p++ = '\\', *p++ = 't'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
770 else if (c == '\n') |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
771 *p++ = '\\', *p++ = 'n'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
772 else if (c == '\r') |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
773 *p++ = '\\', *p++ = 'r'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
774 else if (c == 0) |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
775 *p++ = '\\', *p++ = '0'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
776 else if (c < ' ' || c >= 0x7f) { |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
777 /* For performance, we don't want to call |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
778 PyOS_snprintf here (extra layers of |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
779 function call). */ |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
780 sprintf(p, "\\x%02x", c & 0xff); |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
781 p += 4; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
782 } |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
783 else |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
784 *p++ = c; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
785 } |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
786 assert(newsize - (p - PyString_AS_STRING(v)) >= 1); |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
787 *p++ = quote; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
788 *p = '\0'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
789 _PyString_Resize( |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
790 &v, (p - PyString_AS_STRING(v))); |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
791 return v; |
|
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
|
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 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
794 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
795 static PyObject * |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
796 bytes_str(PyBytesObject *self) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
797 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
798 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
|
799 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
800 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
801 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
|
802 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
|
803 { |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
804 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
|
805 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
|
806 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
|
807 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
808 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
|
809 Py_INCREF(Py_NotImplemented); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
810 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
|
811 } |
|
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 |
|
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 (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
|
814 /* 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
|
815 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
|
816 } |
|
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 else { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
818 minsize = self->ob_size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
819 if (other->ob_size < minsize) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
820 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
|
821 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
822 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
|
823 /* 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
|
824 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
825 if (cmp == 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
826 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
|
827 cmp = -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
828 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
|
829 cmp = 1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
830 } |
|
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
|
831 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
832 switch (op) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
833 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
|
834 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
|
835 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
|
836 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
|
837 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
|
838 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
|
839 } |
|
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
|
840 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
841 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
842 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
|
843 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
|
844 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
|
845 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
846 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
847 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
|
848 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
|
849 { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
850 if (self->ob_bytes != 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
851 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
|
852 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
853 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
|
854 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
855 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
856 static Py_ssize_t |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
857 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
|
858 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
859 if (index != 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
860 PyErr_SetString(PyExc_SystemError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
861 "accessing non-existent string segment"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
862 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
863 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
864 *ptr = (void *)self->ob_bytes; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
865 return self->ob_size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
866 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
867 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
868 static Py_ssize_t |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
869 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
|
870 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
871 if (lenp) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
872 *lenp = self->ob_size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
873 return 1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
874 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
875 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
876 PyDoc_STRVAR(decode_doc, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
877 "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
|
878 \n\ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
879 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
|
880 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
|
881 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
|
882 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
|
883 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
|
884 able to handle UnicodeDecodeErrors."); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
885 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
886 static PyObject * |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
887 bytes_decode(PyObject *self, PyObject *args) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
888 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
889 const char *encoding = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
890 const char *errors = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
891 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
892 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
|
893 return NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
894 if (encoding == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
895 encoding = PyUnicode_GetDefaultEncoding(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
896 return PyCodec_Decode(self, encoding, errors); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
897 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
898 |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
899 PyDoc_STRVAR(alloc_doc, |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
900 "B.__alloc__() -> int\n\ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
901 \n\ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
902 Returns the number of bytes actually allocated."); |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
903 |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
904 static PyObject * |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
905 bytes_alloc(PyBytesObject *self) |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
906 { |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
907 return PyInt_FromSsize_t(self->ob_alloc); |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
908 } |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
909 |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
910 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
|
911 "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
|
912 \n\ |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
913 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
|
914 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
|
915 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
916 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
|
917 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
|
918 { |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
919 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
|
920 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
|
921 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
|
922 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
|
923 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
|
924 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
|
925 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
|
926 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
927 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
|
928 if (seq == NULL) |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
929 return NULL; |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
930 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
|
931 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
|
932 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
933 /* 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
|
934 for (i = 0; i < n; i++) { |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
935 PyObject *obj = items[i]; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
936 if (!PyBytes_Check(obj)) { |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
937 PyErr_Format(PyExc_TypeError, |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
938 "can only join an iterable of bytes " |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
939 "(item %ld has type '%.100s')", |
|
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
|
940 /* XXX %ld isn't right on Win64 */ |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
941 (long)i, obj->ob_type->tp_name); |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
942 goto error; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
943 } |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
944 totalsize += PyBytes_GET_SIZE(obj); |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
945 if (totalsize < 0) { |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
946 PyErr_NoMemory(); |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
947 goto error; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
948 } |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
949 } |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
950 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
951 /* 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
|
952 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
|
953 if (result == NULL) |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
954 goto error; |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
955 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
|
956 for (i = 0; i < n; i++) { |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
957 PyObject *obj = items[i]; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
958 Py_ssize_t size = PyBytes_GET_SIZE(obj); |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
959 memcpy(dest, PyBytes_AS_STRING(obj), size); |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
960 dest += size; |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
961 } |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
962 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
963 /* Done */ |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
964 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
|
965 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
|
966 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
967 /* 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
|
968 error: |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
969 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
|
970 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
|
971 } |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
972 |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
973 PyDoc_STRVAR(fromhex_doc, |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
974 "bytes.fromhex(string) -> bytes\n\ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
975 \n\ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
976 Create a bytes object from a string of hexadecimal numbers.\n\ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
977 Spaces between two numbers are accepted. Example:\n\ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
978 bytes.fromhex('10 2030') -> bytes([0x10, 0x20, 0x30])."); |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
979 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
980 static int |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
981 hex_digit_to_int(int c) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
982 { |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
983 if (isdigit(c)) |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
984 return c - '0'; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
985 else { |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
986 if (isupper(c)) |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
987 c = tolower(c); |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
988 if (c >= 'a' && c <= 'f') |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
989 return c - 'a' + 10; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
990 } |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
991 return -1; |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
992 } |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
993 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
994 static PyObject * |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
995 bytes_fromhex(PyObject *cls, PyObject *args) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
996 { |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
997 PyObject *newbytes; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
998 char *hex, *buf; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
999 Py_ssize_t len, byteslen, i, j; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1000 int top, bot; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1001 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1002 if (!PyArg_ParseTuple(args, "s#:fromhex", &hex, &len)) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1003 return NULL; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1004 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1005 byteslen = len / 2; /* max length if there are no spaces */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1006 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1007 newbytes = PyBytes_FromStringAndSize(NULL, byteslen); |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1008 if (!newbytes) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1009 return NULL; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1010 buf = PyBytes_AS_STRING(newbytes); |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1011 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1012 for (i = j = 0; ; i += 2) { |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1013 /* skip over spaces in the input */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1014 while (Py_CHARMASK(hex[i]) == ' ') |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1015 i++; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1016 if (i >= len) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1017 break; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1018 top = hex_digit_to_int(Py_CHARMASK(hex[i])); |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1019 bot = hex_digit_to_int(Py_CHARMASK(hex[i+1])); |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1020 if (top == -1 || bot == -1) { |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1021 PyErr_Format(PyExc_ValueError, |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1022 "non-hexadecimal number string '%c%c' found in " |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1023 "fromhex() arg at position %zd", |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1024 hex[i], hex[i+1], i); |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1025 goto error; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1026 } |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1027 buf[j++] = (top << 4) + bot; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1028 } |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1029 if (PyBytes_Resize(newbytes, j) < 0) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1030 goto error; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1031 return newbytes; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1032 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1033 error: |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1034 Py_DECREF(newbytes); |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1035 return NULL; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1036 } |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1037 |
|
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
|
1038 static PySequenceMethods bytes_as_sequence = { |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1039 (lenfunc)bytes_length, /* sq_length */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1040 (binaryfunc)bytes_concat, /* sq_concat */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1041 (ssizeargfunc)bytes_repeat, /* sq_repeat */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1042 (ssizeargfunc)bytes_getitem, /* sq_item */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1043 0, /* sq_slice */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1044 (ssizeobjargproc)bytes_setitem, /* sq_ass_item */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1045 0, /* sq_ass_slice */ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1046 (objobjproc)bytes_contains, /* sq_contains */ |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1047 (binaryfunc)bytes_iconcat, /* sq_inplace_concat */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1048 (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
|
1049 }; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1050 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1051 static PyMappingMethods bytes_as_mapping = { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1052 (lenfunc)bytes_length, |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
1053 (binaryfunc)bytes_subscript, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
1054 (objobjargproc)bytes_ass_subscript, |
|
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
|
1055 }; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1056 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1057 static PyBufferProcs bytes_as_buffer = { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1058 (readbufferproc)bytes_getbuffer, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1059 (writebufferproc)bytes_getbuffer, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1060 (segcountproc)bytes_getsegcount, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1061 /* 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
|
1062 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
|
1063 (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
|
1064 }; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1065 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1066 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
|
1067 bytes_methods[] = { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1068 {"decode", (PyCFunction)bytes_decode, METH_VARARGS, decode_doc}, |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
1069 {"__alloc__", (PyCFunction)bytes_alloc, METH_NOARGS, alloc_doc}, |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1070 {"fromhex", (PyCFunction)bytes_fromhex, METH_VARARGS|METH_CLASS, fromhex_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
|
1071 {"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
|
1072 {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
|
1073 }; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1074 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1075 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
|
1076 "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
|
1077 \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
|
1078 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
|
1079 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1080 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
|
1081 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
|
1082 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
|
1083 "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
|
1084 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
|
1085 0, |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1086 (destructor)bytes_dealloc, /* tp_dealloc */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1087 0, /* tp_print */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1088 0, /* tp_getattr */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1089 0, /* tp_setattr */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1090 0, /* tp_compare */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1091 (reprfunc)bytes_repr, /* tp_repr */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1092 0, /* tp_as_number */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1093 &bytes_as_sequence, /* tp_as_sequence */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1094 &bytes_as_mapping, /* tp_as_mapping */ |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
1095 0, /* tp_hash */ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1096 0, /* tp_call */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1097 (reprfunc)bytes_str, /* tp_str */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1098 PyObject_GenericGetAttr, /* tp_getattro */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1099 0, /* tp_setattro */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1100 &bytes_as_buffer, /* tp_as_buffer */ |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
1101 /* bytes is 'final' or 'sealed' */ |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
1102 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1103 bytes_doc, /* tp_doc */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1104 0, /* tp_traverse */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1105 0, /* tp_clear */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1106 (richcmpfunc)bytes_richcompare, /* tp_richcompare */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1107 0, /* tp_weaklistoffset */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1108 0, /* tp_iter */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1109 0, /* tp_iternext */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1110 bytes_methods, /* tp_methods */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1111 0, /* tp_members */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1112 0, /* tp_getset */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1113 0, /* tp_base */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1114 0, /* tp_dict */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1115 0, /* tp_descr_get */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1116 0, /* tp_descr_set */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1117 0, /* tp_dictoffset */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1118 (initproc)bytes_init, /* tp_init */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1119 PyType_GenericAlloc, /* tp_alloc */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1120 PyType_GenericNew, /* tp_new */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1121 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
|
1122 }; |
