Mercurial > cpython-lru_cache-weakref
annotate Objects/bytesobject.c @ 44351:86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
| author | Christian Heimes <christian@cheimes.de> |
|---|---|
| date | Wed, 19 Dec 2007 02:45:37 +0000 |
| parents | 33ff40e6e137 |
| children | e9764a695809 |
| rev | line source |
|---|---|
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
1 /* PyBytes (bytearray) implementation */ |
|
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
|
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 #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
|
4 #include "Python.h" |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
5 #include "structmember.h" |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
6 #include "bytes_methods.h" |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
7 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
8 static PyBytesObject *nullbytes = NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
9 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
10 void |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
11 PyBytes_Fini(void) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
12 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
13 Py_CLEAR(nullbytes); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
14 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
15 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
16 int |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
17 PyBytes_Init(void) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
18 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
19 nullbytes = PyObject_New(PyBytesObject, &PyBytes_Type); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
20 if (nullbytes == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
21 return 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
22 nullbytes->ob_bytes = NULL; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
23 Py_SIZE(nullbytes) = nullbytes->ob_alloc = 0; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
24 nullbytes->ob_exports = 0; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
25 return 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
26 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
27 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
28 /* end nullbytes support */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
29 |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
30 /* Helpers */ |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
31 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
32 static int |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
33 _getbytevalue(PyObject* arg, int *value) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
34 { |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
35 long face_value; |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
36 |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43947
diff
changeset
|
37 if (PyLong_Check(arg)) { |
|
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43947
diff
changeset
|
38 face_value = PyLong_AsLong(arg); |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
39 if (face_value < 0 || face_value >= 256) { |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
40 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
41 return 0; |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
42 } |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
43 } else { |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
44 PyErr_Format(PyExc_TypeError, "an integer is required"); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
45 return 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
46 } |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
47 |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
48 *value = face_value; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
49 return 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
50 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
51 |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
52 static int |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43321
diff
changeset
|
53 bytes_getbuffer(PyBytesObject *obj, Py_buffer *view, int flags) |
| 42779 | 54 { |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
55 int ret; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
56 void *ptr; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
57 if (view == NULL) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
58 obj->ob_exports++; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
59 return 0; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
60 } |
| 42779 | 61 if (obj->ob_bytes == NULL) |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
62 ptr = ""; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
63 else |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
64 ptr = obj->ob_bytes; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
65 ret = PyBuffer_FillInfo(view, ptr, Py_SIZE(obj), 0, flags); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
66 if (ret >= 0) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
67 obj->ob_exports++; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
68 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
69 return ret; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
70 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
71 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
72 static void |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43321
diff
changeset
|
73 bytes_releasebuffer(PyBytesObject *obj, Py_buffer *view) |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
74 { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
75 obj->ob_exports--; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
76 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
77 |
|
42893
2c02ba72714b
Make some internal functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
42779
diff
changeset
|
78 static Py_ssize_t |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43321
diff
changeset
|
79 _getbuffer(PyObject *obj, Py_buffer *view) |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
80 { |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
81 PyBufferProcs *buffer = Py_TYPE(obj)->tp_as_buffer; |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
82 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
83 if (buffer == NULL || buffer->bf_getbuffer == NULL) |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
84 { |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
85 PyErr_Format(PyExc_TypeError, |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
86 "Type %.100s doesn't support the buffer API", |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
87 Py_TYPE(obj)->tp_name); |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
88 return -1; |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
89 } |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
90 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
91 if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
92 return -1; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
93 return view->len; |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
94 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
95 |
|
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
|
96 /* 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
|
97 |
|
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 PyObject * |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
99 PyBytes_FromObject(PyObject *input) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
100 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
101 return PyObject_CallFunctionObjArgs((PyObject *)&PyBytes_Type, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
102 input, NULL); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
103 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
104 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
105 PyObject * |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
106 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
|
107 { |
|
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 PyBytesObject *new; |
|
43687
6ab64f6b4c03
Fix some Py_ssize_t warnings on Win64 that were probably bugs
Neal Norwitz <nnorwitz@gmail.com>
parents:
43632
diff
changeset
|
109 Py_ssize_t 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
|
110 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
111 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
|
112 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 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
|
114 if (new == NULL) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
115 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
|
116 |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
117 if (size == 0) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
118 new->ob_bytes = NULL; |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
119 alloc = 0; |
|
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
120 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
121 else { |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
122 alloc = size + 1; |
|
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
123 new->ob_bytes = PyMem_Malloc(alloc); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
124 if (new->ob_bytes == NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
125 Py_DECREF(new); |
|
43031
ac2d5d346252
Try to fix the problem on the Windows buildbot where this code:
Neal Norwitz <nnorwitz@gmail.com>
parents:
43001
diff
changeset
|
126 return PyErr_NoMemory(); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
127 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
128 if (bytes != NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
129 memcpy(new->ob_bytes, bytes, size); |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
130 new->ob_bytes[size] = '\0'; /* Trailing null byte */ |
|
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
|
131 } |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
132 Py_SIZE(new) = size; |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
133 new->ob_alloc = alloc; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
134 new->ob_exports = 0; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
135 |
|
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
|
136 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
|
137 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 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
|
140 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
|
141 { |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 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
|
143 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
|
144 |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
145 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
|
146 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 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
|
149 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
|
150 { |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 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
|
152 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
|
153 |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
154 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
|
155 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 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
|
158 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
|
159 { |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 void *sval; |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
161 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
|
162 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 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
|
164 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
|
165 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
|
166 |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
167 if (size < alloc / 2) { |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
168 /* Major downsize; resize down to exact size */ |
|
41597
cb3c92062ec4
Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents:
41595
diff
changeset
|
169 alloc = size + 1; |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
170 } |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
171 else if (size < alloc) { |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
172 /* Within allocated size; quick exit */ |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
173 Py_SIZE(self) = size; |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
174 ((PyBytesObject *)self)->ob_bytes[size] = '\0'; /* Trailing null */ |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
175 return 0; |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
176 } |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
177 else if (size <= alloc * 1.125) { |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
178 /* Moderate upsize; overallocate similar to list_resize() */ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
179 alloc = size + (size >> 3) + (size < 9 ? 3 : 6); |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
180 } |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
181 else { |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
182 /* Major upsize; resize up to exact size */ |
|
41597
cb3c92062ec4
Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents:
41595
diff
changeset
|
183 alloc = size + 1; |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
184 } |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
185 |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
186 if (((PyBytesObject *)self)->ob_exports > 0) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
187 /* |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
188 fprintf(stderr, "%d: %s", ((PyBytesObject *)self)->ob_exports, |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
189 ((PyBytesObject *)self)->ob_bytes); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
190 */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
191 PyErr_SetString(PyExc_BufferError, |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
192 "Existing exports of data: object cannot be re-sized"); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
193 return -1; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
194 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
195 |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
196 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
|
197 if (sval == NULL) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
198 PyErr_NoMemory(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
199 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
|
200 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
202 ((PyBytesObject *)self)->ob_bytes = sval; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
203 Py_SIZE(self) = size; |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
204 ((PyBytesObject *)self)->ob_alloc = alloc; |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
205 ((PyBytesObject *)self)->ob_bytes[size] = '\0'; /* Trailing null byte */ |
|
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
206 |
|
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
|
207 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
|
208 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
210 PyObject * |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
211 PyBytes_Concat(PyObject *a, PyObject *b) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
212 { |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
213 Py_ssize_t size; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43321
diff
changeset
|
214 Py_buffer va, vb; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
215 PyBytesObject *result = NULL; |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
216 |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
217 va.len = -1; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
218 vb.len = -1; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
219 if (_getbuffer(a, &va) < 0 || |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
220 _getbuffer(b, &vb) < 0) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
221 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
222 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
223 goto done; |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
224 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
225 |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
226 size = va.len + vb.len; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
227 if (size < 0) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
228 return PyErr_NoMemory(); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
229 goto done; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
230 } |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
231 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
232 result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, size); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
233 if (result != NULL) { |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
234 memcpy(result->ob_bytes, va.buf, va.len); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
235 memcpy(result->ob_bytes + va.len, vb.buf, vb.len); |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
236 } |
| 42779 | 237 |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
238 done: |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
239 if (va.len != -1) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
240 PyObject_ReleaseBuffer(a, &va); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
241 if (vb.len != -1) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
242 PyObject_ReleaseBuffer(b, &vb); |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
243 return (PyObject *)result; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
244 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
245 |
|
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
|
246 /* 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
|
247 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 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
|
249 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
|
250 { |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
251 return Py_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
|
252 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 static PyObject * |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
255 bytes_iconcat(PyBytesObject *self, PyObject *other) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
256 { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
257 Py_ssize_t mysize; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
258 Py_ssize_t size; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43321
diff
changeset
|
259 Py_buffer vo; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
260 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
261 if (_getbuffer(other, &vo) < 0) { |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
262 PyErr_Format(PyExc_TypeError, "can't concat bytes to %.100s", |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
263 Py_TYPE(self)->tp_name); |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
264 return NULL; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
265 } |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
266 |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
267 mysize = Py_SIZE(self); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
268 size = mysize + vo.len; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
269 if (size < 0) { |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
270 PyObject_ReleaseBuffer(other, &vo); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
271 return PyErr_NoMemory(); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
272 } |
|
41597
cb3c92062ec4
Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents:
41595
diff
changeset
|
273 if (size < self->ob_alloc) { |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
274 Py_SIZE(self) = size; |
|
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
275 self->ob_bytes[Py_SIZE(self)] = '\0'; /* Trailing null byte */ |
|
41597
cb3c92062ec4
Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents:
41595
diff
changeset
|
276 } |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
277 else if (PyBytes_Resize((PyObject *)self, size) < 0) { |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
278 PyObject_ReleaseBuffer(other, &vo); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
279 return NULL; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
280 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
281 memcpy(self->ob_bytes + mysize, vo.buf, vo.len); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
282 PyObject_ReleaseBuffer(other, &vo); |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
283 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
|
284 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
|
285 } |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
286 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
287 static PyObject * |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
288 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
|
289 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
290 PyBytesObject *result; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
291 Py_ssize_t mysize; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
292 Py_ssize_t size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
293 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
294 if (count < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
295 count = 0; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
296 mysize = Py_SIZE(self); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
297 size = mysize * count; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
298 if (count != 0 && size / count != mysize) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
299 return PyErr_NoMemory(); |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
300 result = (PyBytesObject *)PyBytes_FromStringAndSize(NULL, size); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
301 if (result != NULL && size != 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
302 if (mysize == 1) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
303 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
|
304 else { |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
305 Py_ssize_t i; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
306 for (i = 0; i < count; i++) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
307 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
|
308 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
309 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
310 return (PyObject *)result; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
311 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
312 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
313 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
|
314 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
|
315 { |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
316 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
|
317 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
|
318 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
319 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
|
320 count = 0; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
321 mysize = Py_SIZE(self); |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
322 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
|
323 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
|
324 return PyErr_NoMemory(); |
|
41597
cb3c92062ec4
Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents:
41595
diff
changeset
|
325 if (size < self->ob_alloc) { |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
326 Py_SIZE(self) = size; |
|
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
327 self->ob_bytes[Py_SIZE(self)] = '\0'; /* Trailing null byte */ |
|
41597
cb3c92062ec4
Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents:
41595
diff
changeset
|
328 } |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
329 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
|
330 return NULL; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
331 |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
332 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
|
333 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
|
334 else { |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
335 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
|
336 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
|
337 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
|
338 } |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
339 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
340 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
|
341 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
|
342 } |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
343 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
344 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
|
345 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
|
346 { |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
347 if (i < 0) |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
348 i += Py_SIZE(self); |
|
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
349 if (i < 0 || i >= Py_SIZE(self)) { |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
350 PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
351 return NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
352 } |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43947
diff
changeset
|
353 return PyLong_FromLong((unsigned char)(self->ob_bytes[i])); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
354 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
355 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
356 static PyObject * |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
357 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
|
358 { |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
359 if (PyIndex_Check(item)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
360 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
|
361 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
362 if (i == -1 && PyErr_Occurred()) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
363 return NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
364 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
365 if (i < 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
366 i += PyBytes_GET_SIZE(self); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
367 |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
368 if (i < 0 || i >= Py_SIZE(self)) { |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
369 PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
370 return NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
371 } |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43947
diff
changeset
|
372 return PyLong_FromLong((unsigned char)(self->ob_bytes[i])); |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
373 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
374 else if (PySlice_Check(item)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
375 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
|
376 if (PySlice_GetIndicesEx((PySliceObject *)item, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
377 PyBytes_GET_SIZE(self), |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
378 &start, &stop, &step, &slicelength) < 0) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
379 return NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
380 } |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
381 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
382 if (slicelength <= 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
383 return PyBytes_FromStringAndSize("", 0); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
384 else if (step == 1) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
385 return PyBytes_FromStringAndSize(self->ob_bytes + start, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
386 slicelength); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
387 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
388 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
389 char *source_buf = PyBytes_AS_STRING(self); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
390 char *result_buf = (char *)PyMem_Malloc(slicelength); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
391 PyObject *result; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
392 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
393 if (result_buf == NULL) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
394 return PyErr_NoMemory(); |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
395 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
396 for (cur = start, i = 0; i < slicelength; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
397 cur += step, i++) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
398 result_buf[i] = source_buf[cur]; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
399 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
400 result = PyBytes_FromStringAndSize(result_buf, slicelength); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
401 PyMem_Free(result_buf); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
402 return result; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
403 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
404 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
405 else { |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
406 PyErr_SetString(PyExc_TypeError, "bytearray indices must be integers"); |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
407 return NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
408 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
409 } |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
410 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
411 static int |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
412 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
|
413 PyObject *values) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
414 { |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
415 Py_ssize_t avail, needed; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
416 void *bytes; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43321
diff
changeset
|
417 Py_buffer vbytes; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
418 int res = 0; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
419 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
420 vbytes.len = -1; |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
421 if (values == (PyObject *)self) { |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
422 /* Make a copy and call this function recursively */ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
423 int err; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
424 values = PyBytes_FromObject(values); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
425 if (values == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
426 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
427 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
|
428 Py_DECREF(values); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
429 return err; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
430 } |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
431 if (values == NULL) { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
432 /* del b[lo:hi] */ |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
433 bytes = NULL; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
434 needed = 0; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
435 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
436 else { |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
437 if (_getbuffer(values, &vbytes) < 0) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
438 PyErr_Format(PyExc_TypeError, |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
439 "can't set bytes slice from %.100s", |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
440 Py_TYPE(values)->tp_name); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
441 return -1; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
442 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
443 needed = vbytes.len; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
444 bytes = vbytes.buf; |
|
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
|
445 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
446 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
447 if (lo < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
448 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
|
449 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
|
450 hi = lo; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
451 if (hi > Py_SIZE(self)) |
|
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
452 hi = Py_SIZE(self); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
453 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
454 avail = hi - lo; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
455 if (avail < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
456 lo = hi = avail = 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
457 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
458 if (avail != needed) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
459 if (avail > needed) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
460 /* |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
461 0 lo hi old_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
462 | |<----avail----->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
463 | |<-needed->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
464 0 lo new_hi new_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
465 */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
466 memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi, |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
467 Py_SIZE(self) - hi); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
468 } |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
469 /* XXX(nnorwitz): need to verify this can't overflow! */ |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
470 if (PyBytes_Resize((PyObject *)self, |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
471 Py_SIZE(self) + needed - avail) < 0) { |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
472 res = -1; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
473 goto finish; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
474 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
475 if (avail < needed) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
476 /* |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
477 0 lo hi old_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
478 | |<-avail->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
479 | |<----needed---->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
480 0 lo new_hi new_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
481 */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
482 memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi, |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
483 Py_SIZE(self) - lo - needed); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
484 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
485 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
486 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
487 if (needed > 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
488 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
|
489 |
| 42779 | 490 |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
491 finish: |
| 42779 | 492 if (vbytes.len != -1) |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
493 PyObject_ReleaseBuffer(values, &vbytes); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
494 return res; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
495 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
496 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
497 static int |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
498 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
|
499 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
500 Py_ssize_t ival; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
501 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
502 if (i < 0) |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
503 i += Py_SIZE(self); |
|
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
504 |
|
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
505 if (i < 0 || i >= Py_SIZE(self)) { |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
506 PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
507 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
508 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
509 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
510 if (value == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
511 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
|
512 |
|
39631
8d71f9a6c1c3
Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents:
39610
diff
changeset
|
513 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
|
514 if (ival == -1 && PyErr_Occurred()) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
515 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
516 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
517 if (ival < 0 || ival >= 256) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
518 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
|
519 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
520 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
521 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
522 self->ob_bytes[i] = ival; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
523 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
|
524 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
525 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
526 static int |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
527 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
|
528 { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
529 Py_ssize_t start, stop, step, slicelen, needed; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
530 char *bytes; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
531 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
532 if (PyIndex_Check(item)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
533 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
|
534 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
535 if (i == -1 && PyErr_Occurred()) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
536 return -1; |
|
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 (i < 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
539 i += PyBytes_GET_SIZE(self); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
540 |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
541 if (i < 0 || i >= Py_SIZE(self)) { |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
542 PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
543 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
544 } |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
545 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
546 if (values == NULL) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
547 /* Fall through to slice assignment */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
548 start = i; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
549 stop = i + 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
550 step = 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
551 slicelen = 1; |
|
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 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
554 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
|
555 if (ival == -1 && PyErr_Occurred()) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
556 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
557 if (ival < 0 || ival >= 256) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
558 PyErr_SetString(PyExc_ValueError, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
559 "byte must be in range(0, 256)"); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
560 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
561 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
562 self->ob_bytes[i] = (char)ival; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
563 return 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
564 } |
|
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 if (PySlice_Check(item)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
567 if (PySlice_GetIndicesEx((PySliceObject *)item, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
568 PyBytes_GET_SIZE(self), |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
569 &start, &stop, &step, &slicelen) < 0) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
570 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
571 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
572 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
573 else { |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
574 PyErr_SetString(PyExc_TypeError, "bytearray indices must be integer"); |
|
40472
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 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
578 if (values == NULL) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
579 bytes = NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
580 needed = 0; |
|
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 else if (values == (PyObject *)self || !PyBytes_Check(values)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
583 /* Make a copy an call this function recursively */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
584 int err; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
585 values = PyBytes_FromObject(values); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
586 if (values == NULL) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
587 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
588 err = bytes_ass_subscript(self, item, values); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
589 Py_DECREF(values); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
590 return err; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
591 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
592 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
593 assert(PyBytes_Check(values)); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
594 bytes = ((PyBytesObject *)values)->ob_bytes; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
595 needed = Py_SIZE(values); |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
596 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
597 /* 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
|
598 if ((step < 0 && start < stop) || |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
599 (step > 0 && start > stop)) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
600 stop = start; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
601 if (step == 1) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
602 if (slicelen != needed) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
603 if (slicelen > needed) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
604 /* |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
605 0 start stop old_size |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
606 | |<---slicelen--->|<-----tomove------>| |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
607 | |<-needed->|<-----tomove------>| |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
608 0 lo new_hi new_size |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
609 */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
610 memmove(self->ob_bytes + start + needed, self->ob_bytes + stop, |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
611 Py_SIZE(self) - stop); |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
612 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
613 if (PyBytes_Resize((PyObject *)self, |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
614 Py_SIZE(self) + needed - slicelen) < 0) |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
615 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
616 if (slicelen < needed) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
617 /* |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
618 0 lo hi old_size |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
619 | |<-avail->|<-----tomove------>| |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
620 | |<----needed---->|<-----tomove------>| |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
621 0 lo new_hi new_size |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
622 */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
623 memmove(self->ob_bytes + start + needed, self->ob_bytes + stop, |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
624 Py_SIZE(self) - start - needed); |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
625 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
626 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
627 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
628 if (needed > 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
629 memcpy(self->ob_bytes + start, bytes, needed); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
630 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
631 return 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
632 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
633 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
634 if (needed == 0) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
635 /* Delete slice */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
636 Py_ssize_t cur, i; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
637 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
638 if (step < 0) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
639 stop = start + 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
640 start = stop + step * (slicelen - 1) - 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
641 step = -step; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
642 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
643 for (cur = start, i = 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
644 i < slicelen; cur += step, i++) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
645 Py_ssize_t lim = step - 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
646 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
647 if (cur + step >= PyBytes_GET_SIZE(self)) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
648 lim = PyBytes_GET_SIZE(self) - cur - 1; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
649 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
650 memmove(self->ob_bytes + cur - i, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
651 self->ob_bytes + cur + 1, lim); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
652 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
653 /* 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
|
654 cur = start + slicelen*step; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
655 if (cur < PyBytes_GET_SIZE(self)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
656 memmove(self->ob_bytes + cur - slicelen, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
657 self->ob_bytes + cur, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
658 PyBytes_GET_SIZE(self) - cur); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
659 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
660 if (PyBytes_Resize((PyObject *)self, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
661 PyBytes_GET_SIZE(self) - slicelen) < 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
662 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
663 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
664 return 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
665 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
666 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
667 /* Assign slice */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
668 Py_ssize_t cur, i; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
669 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
670 if (needed != slicelen) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
671 PyErr_Format(PyExc_ValueError, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
672 "attempt to assign bytes of size %zd " |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
673 "to extended slice of size %zd", |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
674 needed, slicelen); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
675 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
676 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
677 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
|
678 self->ob_bytes[cur] = bytes[i]; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
679 return 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
680 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
681 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
682 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
683 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
684 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
|
685 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
|
686 { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
687 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
|
688 PyObject *arg = NULL; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
689 const char *encoding = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
690 const char *errors = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
691 Py_ssize_t count; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
692 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
|
693 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
|
694 |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
695 if (Py_SIZE(self) != 0) { |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
696 /* Empty previous contents (yes, do this first of all!) */ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
697 if (PyBytes_Resize((PyObject *)self, 0) < 0) |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
698 return -1; |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
699 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
700 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
701 /* Parse arguments */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
702 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
|
703 &arg, &encoding, &errors)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
704 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
|
705 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
706 /* 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
|
707 if (arg == NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
708 if (encoding != NULL || errors != NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
709 PyErr_SetString(PyExc_TypeError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
710 "encoding or errors without sequence argument"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
711 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
712 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
713 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
|
714 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
715 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
716 if (PyUnicode_Check(arg)) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
717 /* Encode via the codec registry */ |
|
41598
ca448c607b14
Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents:
41597
diff
changeset
|
718 PyObject *encoded, *new; |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
719 if (encoding == NULL) { |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
720 PyErr_SetString(PyExc_TypeError, |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
721 "string argument without an encoding"); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
722 return -1; |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
723 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
724 encoded = PyCodec_Encode(arg, encoding, errors); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
725 if (encoded == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
726 return -1; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
727 assert(PyString_Check(encoded)); |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
728 new = bytes_iconcat(self, encoded); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
729 Py_DECREF(encoded); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
730 if (new == NULL) |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
731 return -1; |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
732 Py_DECREF(new); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
733 return 0; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
734 } |
|
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
|
735 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
736 /* 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
|
737 if (encoding != NULL || errors != NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
738 PyErr_SetString(PyExc_TypeError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
739 "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
|
740 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
741 } |
|
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
|
742 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
743 /* 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
|
744 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
|
745 if (count == -1 && PyErr_Occurred()) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
746 PyErr_Clear(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
747 else { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
748 if (count < 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
749 PyErr_SetString(PyExc_ValueError, "negative count"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
750 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
751 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
752 if (count > 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
753 if (PyBytes_Resize((PyObject *)self, count)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
754 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
755 memset(self->ob_bytes, 0, count); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
756 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
757 return 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
758 } |
| 42779 | 759 |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
760 /* Use the buffer API */ |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
761 if (PyObject_CheckBuffer(arg)) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
762 Py_ssize_t size; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43321
diff
changeset
|
763 Py_buffer view; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
764 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
765 return -1; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
766 size = view.len; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
767 if (PyBytes_Resize((PyObject *)self, size) < 0) goto fail; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
768 if (PyBuffer_ToContiguous(self->ob_bytes, &view, size, 'C') < 0) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
769 goto fail; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
770 PyObject_ReleaseBuffer(arg, &view); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
771 return 0; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
772 fail: |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
773 PyObject_ReleaseBuffer(arg, &view); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
774 return -1; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
775 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
776 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
777 /* 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
|
778 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
779 /* 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
|
780 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
|
781 if (it == NULL) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
782 return -1; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
783 iternext = *Py_TYPE(it)->tp_iternext; |
|
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
|
784 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
785 /* 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
|
786 for (;;) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
787 PyObject *item; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
788 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
|
789 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
790 /* Get the next item */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
791 item = iternext(it); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
792 if (item == NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
793 if (PyErr_Occurred()) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
794 if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
795 goto error; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
796 PyErr_Clear(); |
|
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 break; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
799 } |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
800 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
801 /* 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
|
802 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
|
803 Py_DECREF(item); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
804 if (value == -1 && PyErr_Occurred()) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
805 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
|
806 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
807 /* Range check */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
808 if (value < 0 || value >= 256) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
809 PyErr_SetString(PyExc_ValueError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
810 "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
|
811 goto error; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
812 } |
|
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
|
813 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
814 /* Append the byte */ |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
815 if (Py_SIZE(self) < self->ob_alloc) |
|
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
816 Py_SIZE(self)++; |
|
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
817 else if (PyBytes_Resize((PyObject *)self, Py_SIZE(self)+1) < 0) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
818 goto error; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
819 self->ob_bytes[Py_SIZE(self)-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
|
820 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
821 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
822 /* 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
|
823 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
|
824 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
|
825 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
826 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
|
827 /* 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
|
828 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
|
829 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
|
830 } |
|
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 |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
832 /* 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
|
833 "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
|
834 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
|
835 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
|
836 { |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
837 static const char *hexdigits = "0123456789abcdef"; |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
838 const char *quote_prefix = "bytearray(b"; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
839 const char *quote_postfix = ")"; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
840 Py_ssize_t length = Py_SIZE(self); |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
841 /* 14 == strlen(quote_prefix) + 2 + strlen(quote_postfix) */ |
|
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
842 size_t newsize = 14 + 4 * length; |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
843 PyObject *v; |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
844 if (newsize > PY_SSIZE_T_MAX || newsize / 4 - 3 != length) { |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
845 PyErr_SetString(PyExc_OverflowError, |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
846 "bytearray 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
|
847 return NULL; |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
848 } |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
849 v = PyUnicode_FromUnicode(NULL, newsize); |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
850 if (v == NULL) { |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
851 return NULL; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
852 } |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
853 else { |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
854 register Py_ssize_t i; |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
855 register Py_UNICODE c; |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
856 register Py_UNICODE *p; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
857 int quote; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
858 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
859 /* Figure out which quote to use; single is preferred */ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
860 quote = '\''; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
861 { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
862 char *test, *start; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
863 start = PyBytes_AS_STRING(self); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
864 for (test = start; test < start+length; ++test) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
865 if (*test == '"') { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
866 quote = '\''; /* back to single */ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
867 goto decided; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
868 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
869 else if (*test == '\'') |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
870 quote = '"'; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
871 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
872 decided: |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
873 ; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
874 } |
|
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
|
875 |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
876 p = PyUnicode_AS_UNICODE(v); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
877 while (*quote_prefix) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
878 *p++ = *quote_prefix++; |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
879 *p++ = quote; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
880 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
881 for (i = 0; i < length; i++) { |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
882 /* 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
|
883 and a closing quote. */ |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
884 assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5); |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
885 c = self->ob_bytes[i]; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
886 if (c == '\'' || c == '\\') |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
887 *p++ = '\\', *p++ = c; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
888 else if (c == '\t') |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
889 *p++ = '\\', *p++ = 't'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
890 else if (c == '\n') |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
891 *p++ = '\\', *p++ = 'n'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
892 else if (c == '\r') |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
893 *p++ = '\\', *p++ = 'r'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
894 else if (c == 0) |
|
41648
bc6ad63cd5f1
repr(b"\0") should return b"\x00", not the (unusual) b"\0".
Guido van Rossum <guido@python.org>
parents:
41598
diff
changeset
|
895 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0'; |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
896 else if (c < ' ' || c >= 0x7f) { |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
897 *p++ = '\\'; |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
898 *p++ = 'x'; |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
899 *p++ = hexdigits[(c & 0xf0) >> 4]; |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
900 *p++ = hexdigits[c & 0xf]; |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
901 } |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
902 else |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
903 *p++ = c; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
904 } |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
905 assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1); |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
906 *p++ = quote; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
907 while (*quote_postfix) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
908 *p++ = *quote_postfix++; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
909 } |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
910 *p = '\0'; |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
911 if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) { |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
912 Py_DECREF(v); |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
913 return NULL; |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
914 } |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
915 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
|
916 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
917 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
918 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
919 static PyObject * |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
920 bytes_str(PyObject *op) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
921 { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
922 if (Py_BytesWarningFlag) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
923 if (PyErr_WarnEx(PyExc_BytesWarning, |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
924 "str() on a bytearray instance", 1)) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
925 return NULL; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
926 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
927 return bytes_repr((PyBytesObject*)op); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
928 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
929 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
930 static PyObject * |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
931 bytes_richcompare(PyObject *self, PyObject *other, int op) |
|
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
|
932 { |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
933 Py_ssize_t self_size, other_size; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43321
diff
changeset
|
934 Py_buffer self_bytes, other_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
|
935 PyObject *res; |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
936 Py_ssize_t minsize; |
|
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
|
937 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
|
938 |
|
43001
eafc8b23589d
Make it an error to compare a bytes object and a Unicode object.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42975
diff
changeset
|
939 /* Bytes can be compared to anything that supports the (binary) |
|
eafc8b23589d
Make it an error to compare a bytes object and a Unicode object.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42975
diff
changeset
|
940 buffer API. Except that a comparison with Unicode is always an |
|
eafc8b23589d
Make it an error to compare a bytes object and a Unicode object.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42975
diff
changeset
|
941 error, even if the comparison is for equality. */ |
|
eafc8b23589d
Make it an error to compare a bytes object and a Unicode object.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42975
diff
changeset
|
942 if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) || |
|
eafc8b23589d
Make it an error to compare a bytes object and a Unicode object.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42975
diff
changeset
|
943 PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
944 if (Py_BytesWarningFlag && op == Py_EQ) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
945 if (PyErr_WarnEx(PyExc_BytesWarning, |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
946 "Comparsion between bytearray and string", 1)) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
947 return NULL; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
948 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
949 |
|
43508
47c02791ec6b
Patch #1049 by Thomas Lee.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
950 Py_INCREF(Py_NotImplemented); |
|
47c02791ec6b
Patch #1049 by Thomas Lee.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
951 return Py_NotImplemented; |
|
43001
eafc8b23589d
Make it an error to compare a bytes object and a Unicode object.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42975
diff
changeset
|
952 } |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
953 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
954 self_size = _getbuffer(self, &self_bytes); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
955 if (self_size < 0) { |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
956 PyErr_Clear(); |
|
41412
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
957 Py_INCREF(Py_NotImplemented); |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
958 return Py_NotImplemented; |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
959 } |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
960 |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
961 other_size = _getbuffer(other, &other_bytes); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
962 if (other_size < 0) { |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
963 PyErr_Clear(); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
964 PyObject_ReleaseBuffer(self, &self_bytes); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
965 Py_INCREF(Py_NotImplemented); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
966 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
|
967 } |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
968 |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
969 if (self_size != other_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
|
970 /* 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
|
971 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
|
972 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
973 else { |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
974 minsize = self_size; |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
975 if (other_size < minsize) |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
976 minsize = other_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
|
977 |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
978 cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
979 /* 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
|
980 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
981 if (cmp == 0) { |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
982 if (self_size < other_size) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
983 cmp = -1; |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
984 else if (self_size > other_size) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
985 cmp = 1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
986 } |
|
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
|
987 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
988 switch (op) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
989 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
|
990 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
|
991 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
|
992 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
|
993 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
|
994 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
|
995 } |
|
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
|
996 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
997 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
998 res = cmp ? Py_True : Py_False; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
999 PyObject_ReleaseBuffer(self, &self_bytes); |
| 42779 | 1000 PyObject_ReleaseBuffer(other, &other_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
|
1001 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
|
1002 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
|
1003 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1004 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1005 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
|
1006 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
|
1007 { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1008 if (self->ob_bytes != 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1009 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
|
1010 } |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
1011 Py_TYPE(self)->tp_free((PyObject *)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
|
1012 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1013 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1014 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1015 /* -------------------------------------------------------------------- */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1016 /* Methods */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1017 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1018 #define STRINGLIB_CHAR char |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1019 #define STRINGLIB_CMP memcmp |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1020 #define STRINGLIB_LEN PyBytes_GET_SIZE |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1021 #define STRINGLIB_STR PyBytes_AS_STRING |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1022 #define STRINGLIB_NEW PyBytes_FromStringAndSize |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1023 #define STRINGLIB_EMPTY nullbytes |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1024 #define STRINGLIB_CHECK_EXACT PyBytes_CheckExact |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1025 #define STRINGLIB_MUTABLE 1 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1026 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1027 #include "stringlib/fastsearch.h" |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1028 #include "stringlib/count.h" |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1029 #include "stringlib/find.h" |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1030 #include "stringlib/partition.h" |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1031 #include "stringlib/ctype.h" |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1032 #include "stringlib/transmogrify.h" |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1033 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1034 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1035 /* The following Py_LOCAL_INLINE and Py_LOCAL functions |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1036 were copied from the old char* style string object. */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1037 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1038 Py_LOCAL_INLINE(void) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1039 _adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1040 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1041 if (*end > len) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1042 *end = len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1043 else if (*end < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1044 *end += len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1045 if (*end < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1046 *end = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1047 if (*start < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1048 *start += len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1049 if (*start < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1050 *start = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1051 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1052 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1053 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1054 Py_LOCAL_INLINE(Py_ssize_t) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1055 bytes_find_internal(PyBytesObject *self, PyObject *args, int dir) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1056 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1057 PyObject *subobj; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43321
diff
changeset
|
1058 Py_buffer subbuf; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1059 Py_ssize_t start=0, end=PY_SSIZE_T_MAX; |
|
43150
1bb63a32a6c8
Per Georg's suggestion, get rid of str.decode() (which always raises an
Guido van Rossum <guido@python.org>
parents:
43031
diff
changeset
|
1060 Py_ssize_t res; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1061 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1062 if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", &subobj, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1063 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1064 return -2; |
|
43150
1bb63a32a6c8
Per Georg's suggestion, get rid of str.decode() (which always raises an
Guido van Rossum <guido@python.org>
parents:
43031
diff
changeset
|
1065 if (_getbuffer(subobj, &subbuf) < 0) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1066 return -2; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1067 if (dir > 0) |
|
43150
1bb63a32a6c8
Per Georg's suggestion, get rid of str.decode() (which always raises an
Guido van Rossum <guido@python.org>
parents:
43031
diff
changeset
|
1068 res = stringlib_find_slice( |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1069 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
|
43150
1bb63a32a6c8
Per Georg's suggestion, get rid of str.decode() (which always raises an
Guido van Rossum <guido@python.org>
parents:
43031
diff
changeset
|
1070 subbuf.buf, subbuf.len, start, end); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1071 else |
|
43150
1bb63a32a6c8
Per Georg's suggestion, get rid of str.decode() (which always raises an
Guido van Rossum <guido@python.org>
parents:
43031
diff
changeset
|
1072 res = stringlib_rfind_slice( |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1073 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
|
43150
1bb63a32a6c8
Per Georg's suggestion, get rid of str.decode() (which always raises an
Guido van Rossum <guido@python.org>
parents:
43031
diff
changeset
|
1074 subbuf.buf, subbuf.len, start, end); |
|
1bb63a32a6c8
Per Georg's suggestion, get rid of str.decode() (which always raises an
Guido van Rossum <guido@python.org>
parents:
43031
diff
changeset
|
1075 PyObject_ReleaseBuffer(subobj, &subbuf); |
|
1bb63a32a6c8
Per Georg's suggestion, get rid of str.decode() (which always raises an
Guido van Rossum <guido@python.org>
parents:
43031
diff
changeset
|
1076 return res; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1077 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1078 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1079 PyDoc_STRVAR(find__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1080 "B.find(sub [,start [,end]]) -> int\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1081 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1082 Return the lowest index in B where subsection sub is found,\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1083 such that sub is contained within s[start,end]. Optional\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1084 arguments start and end are interpreted as in slice notation.\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1085 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1086 Return -1 on failure."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1087 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1088 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1089 bytes_find(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1090 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1091 Py_ssize_t result = bytes_find_internal(self, args, +1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1092 if (result == -2) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1093 return NULL; |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43947
diff
changeset
|
1094 return PyLong_FromSsize_t(result); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1095 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1096 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1097 PyDoc_STRVAR(count__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1098 "B.count(sub [,start [,end]]) -> int\n\ |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1099 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1100 Return the number of non-overlapping occurrences of subsection sub in\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1101 bytes B[start:end]. Optional arguments start and end are interpreted\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1102 as in slice notation."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1103 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1104 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1105 bytes_count(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1106 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1107 PyObject *sub_obj; |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1108 const char *str = PyBytes_AS_STRING(self); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1109 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1110 Py_buffer vsub; |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1111 PyObject *count_obj; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1112 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1113 if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1114 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1115 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1116 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1117 if (_getbuffer(sub_obj, &vsub) < 0) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1118 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1119 |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41769
diff
changeset
|
1120 _adjust_indices(&start, &end, PyBytes_GET_SIZE(self)); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1121 |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43947
diff
changeset
|
1122 count_obj = PyLong_FromSsize_t( |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1123 stringlib_count(str + start, end - start, vsub.buf, vsub.len) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1124 ); |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1125 PyObject_ReleaseBuffer(sub_obj, &vsub); |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1126 return count_obj; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1127 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1128 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1129 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1130 PyDoc_STRVAR(index__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1131 "B.index(sub [,start [,end]]) -> int\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1132 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1133 Like B.find() but raise ValueError when the subsection is not found."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1134 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1135 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1136 bytes_index(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1137 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1138 Py_ssize_t result = bytes_find_internal(self, args, +1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1139 if (result == -2) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1140 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1141 if (result == -1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1142 PyErr_SetString(PyExc_ValueError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1143 "subsection not found"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1144 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1145 } |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43947
diff
changeset
|
1146 return PyLong_FromSsize_t(result); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1147 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1148 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1149 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1150 PyDoc_STRVAR(rfind__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1151 "B.rfind(sub [,start [,end]]) -> int\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1152 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1153 Return the highest index in B where subsection sub is found,\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1154 such that sub is contained within s[start,end]. Optional\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1155 arguments start and end are interpreted as in slice notation.\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1156 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1157 Return -1 on failure."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1158 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1159 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1160 bytes_rfind(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1161 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1162 Py_ssize_t result = bytes_find_internal(self, args, -1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1163 if (result == -2) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1164 return NULL; |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43947
diff
changeset
|
1165 return PyLong_FromSsize_t(result); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1166 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1167 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1168 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1169 PyDoc_STRVAR(rindex__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1170 "B.rindex(sub [,start [,end]]) -> int\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1171 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1172 Like B.rfind() but raise ValueError when the subsection is not found."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1173 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1174 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1175 bytes_rindex(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1176 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1177 Py_ssize_t result = bytes_find_internal(self, args, -1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1178 if (result == -2) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1179 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1180 if (result == -1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1181 PyErr_SetString(PyExc_ValueError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1182 "subsection not found"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1183 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1184 } |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43947
diff
changeset
|
1185 return PyLong_FromSsize_t(result); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1186 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1187 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1188 |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1189 static int |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1190 bytes_contains(PyObject *self, PyObject *arg) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1191 { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1192 Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1193 if (ival == -1 && PyErr_Occurred()) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1194 Py_buffer varg; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1195 int pos; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1196 PyErr_Clear(); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1197 if (_getbuffer(arg, &varg) < 0) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1198 return -1; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
1199 pos = stringlib_find(PyBytes_AS_STRING(self), Py_SIZE(self), |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1200 varg.buf, varg.len, 0); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1201 PyObject_ReleaseBuffer(arg, &varg); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1202 return pos >= 0; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1203 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1204 if (ival < 0 || ival >= 256) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1205 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1206 return -1; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1207 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1208 |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
1209 return memchr(PyBytes_AS_STRING(self), ival, Py_SIZE(self)) != NULL; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1210 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1211 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1212 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1213 /* Matches the end (direction >= 0) or start (direction < 0) of self |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1214 * against substr, using the start and end arguments. Returns |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1215 * -1 on error, 0 if not found and 1 if found. |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1216 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1217 Py_LOCAL(int) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1218 _bytes_tailmatch(PyBytesObject *self, PyObject *substr, Py_ssize_t start, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1219 Py_ssize_t end, int direction) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1220 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1221 Py_ssize_t len = PyBytes_GET_SIZE(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1222 const char* str; |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1223 Py_buffer vsubstr; |
|
43632
0067a6ab4854
Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents:
43576
diff
changeset
|
1224 int rv = 0; |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1225 |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1226 str = PyBytes_AS_STRING(self); |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1227 |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1228 if (_getbuffer(substr, &vsubstr) < 0) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1229 return -1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1230 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1231 _adjust_indices(&start, &end, len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1232 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1233 if (direction < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1234 /* startswith */ |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1235 if (start+vsubstr.len > len) { |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1236 goto done; |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1237 } |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1238 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1239 /* endswith */ |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1240 if (end-start < vsubstr.len || start > len) { |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1241 goto done; |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1242 } |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1243 |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1244 if (end-vsubstr.len > start) |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1245 start = end - vsubstr.len; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1246 } |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1247 if (end-start >= vsubstr.len) |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1248 rv = ! memcmp(str+start, vsubstr.buf, vsubstr.len); |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1249 |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1250 done: |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1251 PyObject_ReleaseBuffer(substr, &vsubstr); |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1252 return rv; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1253 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1254 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1255 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1256 PyDoc_STRVAR(startswith__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1257 "B.startswith(prefix [,start [,end]]) -> bool\n\ |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1258 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1259 Return True if B starts with the specified prefix, False otherwise.\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1260 With optional start, test B beginning at that position.\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1261 With optional end, stop comparing B at that position.\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1262 prefix can also be a tuple of strings to try."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1263 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1264 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1265 bytes_startswith(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1266 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1267 Py_ssize_t start = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1268 Py_ssize_t end = PY_SSIZE_T_MAX; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1269 PyObject *subobj; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1270 int result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1271 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1272 if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1273 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1274 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1275 if (PyTuple_Check(subobj)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1276 Py_ssize_t i; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1277 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1278 result = _bytes_tailmatch(self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1279 PyTuple_GET_ITEM(subobj, i), |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1280 start, end, -1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1281 if (result == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1282 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1283 else if (result) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1284 Py_RETURN_TRUE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1285 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1286 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1287 Py_RETURN_FALSE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1288 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1289 result = _bytes_tailmatch(self, subobj, start, end, -1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1290 if (result == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1291 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1292 else |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1293 return PyBool_FromLong(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1294 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1295 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1296 PyDoc_STRVAR(endswith__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1297 "B.endswith(suffix [,start [,end]]) -> bool\n\ |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1298 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1299 Return True if B ends with the specified suffix, False otherwise.\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1300 With optional start, test B beginning at that position.\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1301 With optional end, stop comparing B at that position.\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1302 suffix can also be a tuple of strings to try."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1303 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1304 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1305 bytes_endswith(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1306 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1307 Py_ssize_t start = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1308 Py_ssize_t end = PY_SSIZE_T_MAX; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1309 PyObject *subobj; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1310 int result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1311 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1312 if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1313 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1314 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1315 if (PyTuple_Check(subobj)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1316 Py_ssize_t i; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1317 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1318 result = _bytes_tailmatch(self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1319 PyTuple_GET_ITEM(subobj, i), |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1320 start, end, +1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1321 if (result == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1322 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1323 else if (result) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1324 Py_RETURN_TRUE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1325 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1326 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1327 Py_RETURN_FALSE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1328 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1329 result = _bytes_tailmatch(self, subobj, start, end, +1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1330 if (result == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1331 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1332 else |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1333 return PyBool_FromLong(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1334 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1335 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1336 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1337 PyDoc_STRVAR(translate__doc__, |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
1338 "B.translate(table[, deletechars]) -> bytearray\n\ |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1339 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1340 Return a copy of B, where all characters occurring in the\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1341 optional argument deletechars are removed, and the remaining\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1342 characters have been mapped through the given translation\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
1343 table, which must be a bytes object of length 256."); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1344 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1345 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1346 bytes_translate(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1347 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1348 register char *input, *output; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1349 register const char *table; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1350 register Py_ssize_t i, c, changed = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1351 PyObject *input_obj = (PyObject*)self; |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1352 const char *output_start; |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1353 Py_ssize_t inlen; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1354 PyObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1355 int trans_table[256]; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1356 PyObject *tableobj, *delobj = NULL; |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1357 Py_buffer vtable, vdel; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1358 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1359 if (!PyArg_UnpackTuple(args, "translate", 1, 2, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1360 &tableobj, &delobj)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1361 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1362 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1363 if (_getbuffer(tableobj, &vtable) < 0) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1364 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1365 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1366 if (vtable.len != 256) { |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1367 PyErr_SetString(PyExc_ValueError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1368 "translation table must be 256 characters long"); |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1369 result = NULL; |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1370 goto done; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1371 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1372 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1373 if (delobj != NULL) { |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1374 if (_getbuffer(delobj, &vdel) < 0) { |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1375 result = NULL; |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1376 goto done; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1377 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1378 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1379 else { |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1380 vdel.buf = NULL; |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1381 vdel.len = 0; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1382 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1383 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1384 table = (const char *)vtable.buf; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1385 inlen = PyBytes_GET_SIZE(input_obj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1386 result = PyBytes_FromStringAndSize((char *)NULL, inlen); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1387 if (result == NULL) |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1388 goto done; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1389 output_start = output = PyBytes_AsString(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1390 input = PyBytes_AS_STRING(input_obj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1391 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1392 if (vdel.len == 0) { |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1393 /* If no deletions are required, use faster code */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1394 for (i = inlen; --i >= 0; ) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1395 c = Py_CHARMASK(*input++); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1396 if (Py_CHARMASK((*output++ = table[c])) != c) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1397 changed = 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1398 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1399 if (changed || !PyBytes_CheckExact(input_obj)) |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1400 goto done; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1401 Py_DECREF(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1402 Py_INCREF(input_obj); |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1403 result = input_obj; |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1404 goto done; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1405 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1406 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1407 for (i = 0; i < 256; i++) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1408 trans_table[i] = Py_CHARMASK(table[i]); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1409 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1410 for (i = 0; i < vdel.len; i++) |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1411 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1412 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1413 for (i = inlen; --i >= 0; ) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1414 c = Py_CHARMASK(*input++); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1415 if (trans_table[c] != -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1416 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1417 continue; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1418 changed = 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1419 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1420 if (!changed && PyBytes_CheckExact(input_obj)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1421 Py_DECREF(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1422 Py_INCREF(input_obj); |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1423 result = input_obj; |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1424 goto done; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1425 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1426 /* Fix the size of the resulting string */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1427 if (inlen > 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1428 PyBytes_Resize(result, output - output_start); |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1429 |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1430 done: |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1431 PyObject_ReleaseBuffer(tableobj, &vtable); |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1432 if (delobj != NULL) |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1433 PyObject_ReleaseBuffer(delobj, &vdel); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1434 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1435 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1436 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1437 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1438 #define FORWARD 1 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1439 #define REVERSE -1 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1440 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1441 /* find and count characters and substrings */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1442 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1443 #define findchar(target, target_len, c) \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1444 ((char *)memchr((const void *)(target), c, target_len)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1445 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1446 /* Don't call if length < 2 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1447 #define Py_STRING_MATCH(target, offset, pattern, length) \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1448 (target[offset] == pattern[0] && \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1449 target[offset+length-1] == pattern[length-1] && \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1450 !memcmp(target+offset+1, pattern+1, length-2) ) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1451 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1452 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1453 /* Bytes ops must return a string. */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1454 /* If the object is subclass of bytes, create a copy */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1455 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1456 return_self(PyBytesObject *self) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1457 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1458 if (PyBytes_CheckExact(self)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1459 Py_INCREF(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1460 return (PyBytesObject *)self; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1461 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1462 return (PyBytesObject *)PyBytes_FromStringAndSize( |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1463 PyBytes_AS_STRING(self), |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1464 PyBytes_GET_SIZE(self)); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1465 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1466 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1467 Py_LOCAL_INLINE(Py_ssize_t) |
|
43687
6ab64f6b4c03
Fix some Py_ssize_t warnings on Win64 that were probably bugs
Neal Norwitz <nnorwitz@gmail.com>
parents:
43632
diff
changeset
|
1468 countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1469 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1470 Py_ssize_t count=0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1471 const char *start=target; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1472 const char *end=target+target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1473 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1474 while ( (start=findchar(start, end-start, c)) != NULL ) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1475 count++; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1476 if (count >= maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1477 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1478 start += 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1479 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1480 return count; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1481 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1482 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1483 Py_LOCAL(Py_ssize_t) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1484 findstring(const char *target, Py_ssize_t target_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1485 const char *pattern, Py_ssize_t pattern_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1486 Py_ssize_t start, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1487 Py_ssize_t end, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1488 int direction) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1489 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1490 if (start < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1491 start += target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1492 if (start < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1493 start = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1494 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1495 if (end > target_len) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1496 end = target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1497 } else if (end < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1498 end += target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1499 if (end < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1500 end = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1501 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1502 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1503 /* zero-length substrings always match at the first attempt */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1504 if (pattern_len == 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1505 return (direction > 0) ? start : end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1506 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1507 end -= pattern_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1508 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1509 if (direction < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1510 for (; end >= start; end--) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1511 if (Py_STRING_MATCH(target, end, pattern, pattern_len)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1512 return end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1513 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1514 for (; start <= end; start++) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1515 if (Py_STRING_MATCH(target, start, pattern, pattern_len)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1516 return start; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1517 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1518 return -1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1519 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1520 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1521 Py_LOCAL_INLINE(Py_ssize_t) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1522 countstring(const char *target, Py_ssize_t target_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1523 const char *pattern, Py_ssize_t pattern_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1524 Py_ssize_t start, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1525 Py_ssize_t end, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1526 int direction, Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1527 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1528 Py_ssize_t count=0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1529 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1530 if (start < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1531 start += target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1532 if (start < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1533 start = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1534 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1535 if (end > target_len) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1536 end = target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1537 } else if (end < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1538 end += target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1539 if (end < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1540 end = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1541 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1542 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1543 /* zero-length substrings match everywhere */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1544 if (pattern_len == 0 || maxcount == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1545 if (target_len+1 < maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1546 return target_len+1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1547 return maxcount; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1548 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1549 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1550 end -= pattern_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1551 if (direction < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1552 for (; (end >= start); end--) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1553 if (Py_STRING_MATCH(target, end, pattern, pattern_len)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1554 count++; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1555 if (--maxcount <= 0) break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1556 end -= pattern_len-1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1557 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1558 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1559 for (; (start <= end); start++) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1560 if (Py_STRING_MATCH(target, start, pattern, pattern_len)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1561 count++; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1562 if (--maxcount <= 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1563 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1564 start += pattern_len-1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1565 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1566 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1567 return count; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1568 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1569 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1570 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1571 /* Algorithms for different cases of string replacement */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1572 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1573 /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1574 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1575 replace_interleave(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1576 const char *to_s, Py_ssize_t to_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1577 Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1578 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1579 char *self_s, *result_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1580 Py_ssize_t self_len, result_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1581 Py_ssize_t count, i, product; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1582 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1583 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1584 self_len = PyBytes_GET_SIZE(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1585 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1586 /* 1 at the end plus 1 after every character */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1587 count = self_len+1; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
1588 if (maxcount < count) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1589 count = maxcount; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1590 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1591 /* Check for overflow */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1592 /* result_len = count * to_len + self_len; */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1593 product = count * to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1594 if (product / to_len != count) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1595 PyErr_SetString(PyExc_OverflowError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1596 "replace string is too long"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1597 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1598 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1599 result_len = product + self_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1600 if (result_len < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1601 PyErr_SetString(PyExc_OverflowError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1602 "replace string is too long"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1603 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1604 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1605 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1606 if (! (result = (PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1607 PyBytes_FromStringAndSize(NULL, result_len)) ) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1608 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1609 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1610 self_s = PyBytes_AS_STRING(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1611 result_s = PyBytes_AS_STRING(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1612 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1613 /* TODO: special case single character, which doesn't need memcpy */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1614 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1615 /* Lay the first one down (guaranteed this will occur) */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1616 Py_MEMCPY(result_s, to_s, to_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1617 result_s += to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1618 count -= 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1619 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1620 for (i=0; i<count; i++) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1621 *result_s++ = *self_s++; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1622 Py_MEMCPY(result_s, to_s, to_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1623 result_s += to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1624 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1625 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1626 /* Copy the rest of the original string */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1627 Py_MEMCPY(result_s, self_s, self_len-i); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1628 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1629 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1630 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1631 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1632 /* Special case for deleting a single character */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1633 /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1634 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1635 replace_delete_single_character(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1636 char from_c, Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1637 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1638 char *self_s, *result_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1639 char *start, *next, *end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1640 Py_ssize_t self_len, result_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1641 Py_ssize_t count; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1642 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1643 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1644 self_len = PyBytes_GET_SIZE(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1645 self_s = PyBytes_AS_STRING(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1646 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1647 count = countchar(self_s, self_len, from_c, maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1648 if (count == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1649 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1650 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1651 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1652 result_len = self_len - count; /* from_len == 1 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1653 assert(result_len>=0); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1654 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1655 if ( (result = (PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1656 PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1657 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1658 result_s = PyBytes_AS_STRING(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1659 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1660 start = self_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1661 end = self_s + self_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1662 while (count-- > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1663 next = findchar(start, end-start, from_c); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1664 if (next == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1665 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1666 Py_MEMCPY(result_s, start, next-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1667 result_s += (next-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1668 start = next+1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1669 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1670 Py_MEMCPY(result_s, start, end-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1671 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1672 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1673 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1674 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1675 /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1676 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1677 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1678 replace_delete_substring(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1679 const char *from_s, Py_ssize_t from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1680 Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1681 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1682 char *self_s, *result_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1683 char *start, *next, *end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1684 Py_ssize_t self_len, result_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1685 Py_ssize_t count, offset; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1686 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1687 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1688 self_len = PyBytes_GET_SIZE(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1689 self_s = PyBytes_AS_STRING(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1690 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1691 count = countstring(self_s, self_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1692 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1693 0, self_len, 1, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1694 maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1695 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1696 if (count == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1697 /* no matches */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1698 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1699 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1700 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1701 result_len = self_len - (count * from_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1702 assert (result_len>=0); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1703 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1704 if ( (result = (PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1705 PyBytes_FromStringAndSize(NULL, result_len)) == NULL ) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1706 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1707 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1708 result_s = PyBytes_AS_STRING(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1709 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1710 start = self_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1711 end = self_s + self_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1712 while (count-- > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1713 offset = findstring(start, end-start, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1714 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1715 0, end-start, FORWARD); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1716 if (offset == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1717 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1718 next = start + offset; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1719 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1720 Py_MEMCPY(result_s, start, next-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1721 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1722 result_s += (next-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1723 start = next+from_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1724 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1725 Py_MEMCPY(result_s, start, end-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1726 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1727 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1728 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1729 /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1730 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1731 replace_single_character_in_place(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1732 char from_c, char to_c, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1733 Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1734 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1735 char *self_s, *result_s, *start, *end, *next; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1736 Py_ssize_t self_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1737 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1738 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1739 /* The result string will be the same size */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1740 self_s = PyBytes_AS_STRING(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1741 self_len = PyBytes_GET_SIZE(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1742 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1743 next = findchar(self_s, self_len, from_c); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1744 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1745 if (next == NULL) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1746 /* No matches; return the original bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1747 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1748 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1749 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1750 /* Need to make a new bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1751 result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1752 if (result == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1753 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1754 result_s = PyBytes_AS_STRING(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1755 Py_MEMCPY(result_s, self_s, self_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1756 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1757 /* change everything in-place, starting with this one */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1758 start = result_s + (next-self_s); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1759 *start = to_c; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1760 start++; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1761 end = result_s + self_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1762 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1763 while (--maxcount > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1764 next = findchar(start, end-start, from_c); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1765 if (next == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1766 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1767 *next = to_c; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1768 start = next+1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1769 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1770 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1771 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1772 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1773 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1774 /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1775 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1776 replace_substring_in_place(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1777 const char *from_s, Py_ssize_t from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1778 const char *to_s, Py_ssize_t to_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1779 Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1780 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1781 char *result_s, *start, *end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1782 char *self_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1783 Py_ssize_t self_len, offset; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1784 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1785 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1786 /* The result bytes will be the same size */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1787 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1788 self_s = PyBytes_AS_STRING(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1789 self_len = PyBytes_GET_SIZE(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1790 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1791 offset = findstring(self_s, self_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1792 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1793 0, self_len, FORWARD); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1794 if (offset == -1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1795 /* No matches; return the original bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1796 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1797 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1798 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1799 /* Need to make a new bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1800 result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1801 if (result == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1802 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1803 result_s = PyBytes_AS_STRING(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1804 Py_MEMCPY(result_s, self_s, self_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1805 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1806 /* change everything in-place, starting with this one */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1807 start = result_s + offset; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1808 Py_MEMCPY(start, to_s, from_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1809 start += from_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1810 end = result_s + self_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1811 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1812 while ( --maxcount > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1813 offset = findstring(start, end-start, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1814 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1815 0, end-start, FORWARD); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1816 if (offset==-1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1817 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1818 Py_MEMCPY(start+offset, to_s, from_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1819 start += offset+from_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1820 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1821 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1822 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1823 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1824 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1825 /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1826 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1827 replace_single_character(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1828 char from_c, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1829 const char *to_s, Py_ssize_t to_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1830 Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1831 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1832 char *self_s, *result_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1833 char *start, *next, *end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1834 Py_ssize_t self_len, result_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1835 Py_ssize_t count, product; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1836 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1837 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1838 self_s = PyBytes_AS_STRING(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1839 self_len = PyBytes_GET_SIZE(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1840 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1841 count = countchar(self_s, self_len, from_c, maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1842 if (count == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1843 /* no matches, return unchanged */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1844 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1845 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1846 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1847 /* use the difference between current and new, hence the "-1" */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1848 /* result_len = self_len + count * (to_len-1) */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1849 product = count * (to_len-1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1850 if (product / (to_len-1) != count) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1851 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1852 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1853 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1854 result_len = self_len + product; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1855 if (result_len < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1856 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1857 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1858 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1859 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1860 if ( (result = (PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1861 PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1862 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1863 result_s = PyBytes_AS_STRING(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1864 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1865 start = self_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1866 end = self_s + self_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1867 while (count-- > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1868 next = findchar(start, end-start, from_c); |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
1869 if (next == NULL) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1870 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1871 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1872 if (next == start) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1873 /* replace with the 'to' */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1874 Py_MEMCPY(result_s, to_s, to_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1875 result_s += to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1876 start += 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1877 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1878 /* copy the unchanged old then the 'to' */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1879 Py_MEMCPY(result_s, start, next-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1880 result_s += (next-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1881 Py_MEMCPY(result_s, to_s, to_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1882 result_s += to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1883 start = next+1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1884 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1885 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1886 /* Copy the remainder of the remaining bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1887 Py_MEMCPY(result_s, start, end-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1888 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1889 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1890 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1891 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1892 /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1893 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1894 replace_substring(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1895 const char *from_s, Py_ssize_t from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1896 const char *to_s, Py_ssize_t to_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1897 Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1898 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1899 char *self_s, *result_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1900 char *start, *next, *end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1901 Py_ssize_t self_len, result_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1902 Py_ssize_t count, offset, product; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1903 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1904 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1905 self_s = PyBytes_AS_STRING(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1906 self_len = PyBytes_GET_SIZE(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1907 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1908 count = countstring(self_s, self_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1909 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1910 0, self_len, FORWARD, maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1911 if (count == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1912 /* no matches, return unchanged */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1913 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1914 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1915 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1916 /* Check for overflow */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1917 /* result_len = self_len + count * (to_len-from_len) */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1918 product = count * (to_len-from_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1919 if (product / (to_len-from_len) != count) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1920 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1921 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1922 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1923 result_len = self_len + product; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1924 if (result_len < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1925 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1926 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1927 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1928 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1929 if ( (result = (PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1930 PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1931 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1932 result_s = PyBytes_AS_STRING(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1933 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1934 start = self_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1935 end = self_s + self_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1936 while (count-- > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1937 offset = findstring(start, end-start, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1938 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1939 0, end-start, FORWARD); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1940 if (offset == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1941 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1942 next = start+offset; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1943 if (next == start) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1944 /* replace with the 'to' */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1945 Py_MEMCPY(result_s, to_s, to_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1946 result_s += to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1947 start += from_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1948 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1949 /* copy the unchanged old then the 'to' */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1950 Py_MEMCPY(result_s, start, next-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1951 result_s += (next-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1952 Py_MEMCPY(result_s, to_s, to_len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1953 result_s += to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1954 start = next+from_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1955 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1956 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1957 /* Copy the remainder of the remaining bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1958 Py_MEMCPY(result_s, start, end-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1959 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1960 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1961 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1962 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1963 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1964 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1965 replace(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1966 const char *from_s, Py_ssize_t from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1967 const char *to_s, Py_ssize_t to_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1968 Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1969 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1970 if (maxcount < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1971 maxcount = PY_SSIZE_T_MAX; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1972 } else if (maxcount == 0 || PyBytes_GET_SIZE(self) == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1973 /* nothing to do; return the original bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1974 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1975 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1976 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1977 if (maxcount == 0 || |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1978 (from_len == 0 && to_len == 0)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1979 /* nothing to do; return the original bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1980 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1981 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1982 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1983 /* Handle zero-length special cases */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1984 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1985 if (from_len == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1986 /* insert the 'to' bytes everywhere. */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1987 /* >>> "Python".replace("", ".") */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1988 /* '.P.y.t.h.o.n.' */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1989 return replace_interleave(self, to_s, to_len, maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1990 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1991 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1992 /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1993 /* point for an empty self bytes to generate a non-empty bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1994 /* Special case so the remaining code always gets a non-empty bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1995 if (PyBytes_GET_SIZE(self) == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1996 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1997 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1998 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1999 if (to_len == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2000 /* delete all occurances of 'from' bytes */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2001 if (from_len == 1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2002 return replace_delete_single_character( |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2003 self, from_s[0], maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2004 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2005 return replace_delete_substring(self, from_s, from_len, maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2006 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2007 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2008 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2009 /* Handle special case where both bytes have the same length */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2010 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2011 if (from_len == to_len) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2012 if (from_len == 1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2013 return replace_single_character_in_place( |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2014 self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2015 from_s[0], |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2016 to_s[0], |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2017 maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2018 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2019 return replace_substring_in_place( |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2020 self, from_s, from_len, to_s, to_len, maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2021 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2022 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2023 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2024 /* Otherwise use the more generic algorithms */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2025 if (from_len == 1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2026 return replace_single_character(self, from_s[0], |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2027 to_s, to_len, maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2028 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2029 /* len('from')>=2, len('to')>=1 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2030 return replace_substring(self, from_s, from_len, to_s, to_len, maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2031 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2032 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2033 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
2034 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2035 PyDoc_STRVAR(replace__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2036 "B.replace(old, new[, count]) -> bytes\n\ |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2037 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2038 Return a copy of B with all occurrences of subsection\n\ |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2039 old replaced by new. If the optional argument count is\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2040 given, only the first count occurrences are replaced."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2041 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2042 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2043 bytes_replace(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2044 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2045 Py_ssize_t count = -1; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
2046 PyObject *from, *to, *res; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43321
diff
changeset
|
2047 Py_buffer vfrom, vto; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2048 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2049 if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2050 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2051 |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2052 if (_getbuffer(from, &vfrom) < 0) |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2053 return NULL; |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2054 if (_getbuffer(to, &vto) < 0) { |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2055 PyObject_ReleaseBuffer(from, &vfrom); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2056 return NULL; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
2057 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
2058 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
2059 res = (PyObject *)replace((PyBytesObject *) self, |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2060 vfrom.buf, vfrom.len, |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2061 vto.buf, vto.len, count); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2062 |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2063 PyObject_ReleaseBuffer(from, &vfrom); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2064 PyObject_ReleaseBuffer(to, &vto); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
2065 return res; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2066 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2067 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2068 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2069 /* Overallocate the initial list to reduce the number of reallocs for small |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2070 split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2071 resizes, to sizes 4, 8, then 16. Most observed string splits are for human |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2072 text (roughly 11 words per line) and field delimited data (usually 1-10 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2073 fields). For large strings the split algorithms are bandwidth limited |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2074 so increasing the preallocation likely will not improve things.*/ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2075 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2076 #define MAX_PREALLOC 12 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2077 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2078 /* 5 splits gives 6 elements */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2079 #define PREALLOC_SIZE(maxsplit) \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2080 (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2081 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2082 #define SPLIT_APPEND(data, left, right) \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2083 str = PyBytes_FromStringAndSize((data) + (left), \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2084 (right) - (left)); \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2085 if (str == NULL) \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2086 goto onError; \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2087 if (PyList_Append(list, str)) { \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2088 Py_DECREF(str); \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2089 goto onError; \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2090 } \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2091 else \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2092 Py_DECREF(str); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2093 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2094 #define SPLIT_ADD(data, left, right) { \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2095 str = PyBytes_FromStringAndSize((data) + (left), \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2096 (right) - (left)); \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2097 if (str == NULL) \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2098 goto onError; \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2099 if (count < MAX_PREALLOC) { \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2100 PyList_SET_ITEM(list, count, str); \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2101 } else { \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2102 if (PyList_Append(list, str)) { \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2103 Py_DECREF(str); \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2104 goto onError; \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2105 } \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2106 else \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2107 Py_DECREF(str); \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2108 } \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2109 count++; } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2110 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2111 /* Always force the list to the expected size. */ |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
2112 #define FIX_PREALLOC_SIZE(list) Py_SIZE(list) = count |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2113 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2114 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2115 Py_LOCAL_INLINE(PyObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2116 split_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2117 { |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2118 register Py_ssize_t i, j, count = 0; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2119 PyObject *str; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2120 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2121 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2122 if (list == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2123 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2124 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2125 i = j = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2126 while ((j < len) && (maxcount-- > 0)) { |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2127 for(; j < len; j++) { |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2128 /* I found that using memchr makes no difference */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2129 if (s[j] == ch) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2130 SPLIT_ADD(s, i, j); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2131 i = j = j + 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2132 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2133 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2134 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2135 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2136 if (i <= len) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2137 SPLIT_ADD(s, i, len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2138 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2139 FIX_PREALLOC_SIZE(list); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2140 return list; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2141 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2142 onError: |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2143 Py_DECREF(list); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2144 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2145 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2146 |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2147 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2148 Py_LOCAL_INLINE(PyObject *) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2149 split_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxcount) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2150 { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2151 register Py_ssize_t i, j, count = 0; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2152 PyObject *str; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2153 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2154 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2155 if (list == NULL) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2156 return NULL; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2157 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2158 for (i = j = 0; i < len; ) { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2159 /* find a token */ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2160 while (i < len && ISSPACE(s[i])) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2161 i++; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2162 j = i; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2163 while (i < len && !ISSPACE(s[i])) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2164 i++; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2165 if (j < i) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2166 if (maxcount-- <= 0) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2167 break; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2168 SPLIT_ADD(s, j, i); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2169 while (i < len && ISSPACE(s[i])) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2170 i++; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2171 j = i; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2172 } |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2173 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2174 if (j < len) { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2175 SPLIT_ADD(s, j, len); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2176 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2177 FIX_PREALLOC_SIZE(list); |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2178 return list; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2179 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2180 onError: |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2181 Py_DECREF(list); |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2182 return NULL; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2183 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2184 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2185 PyDoc_STRVAR(split__doc__, |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
2186 "B.split([sep[, maxsplit]]) -> list of bytearray\n\ |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2187 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2188 Return a list of the sections in B, using sep as the delimiter.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2189 If sep is not given, B is split on ASCII whitespace characters\n\ |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2190 (space, tab, return, newline, formfeed, vertical tab).\n\ |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2191 If maxsplit is given, at most maxsplit splits are done."); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2192 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2193 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2194 bytes_split(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2195 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2196 Py_ssize_t len = PyBytes_GET_SIZE(self), n, i, j; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2197 Py_ssize_t maxsplit = -1, count = 0; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2198 const char *s = PyBytes_AS_STRING(self), *sub; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2199 PyObject *list, *str, *subobj = Py_None; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43321
diff
changeset
|
2200 Py_buffer vsub; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2201 #ifdef USE_FAST |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2202 Py_ssize_t pos; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2203 #endif |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2204 |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2205 if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit)) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2206 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2207 if (maxsplit < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2208 maxsplit = PY_SSIZE_T_MAX; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2209 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2210 if (subobj == Py_None) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2211 return split_whitespace(s, len, maxsplit); |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2212 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2213 if (_getbuffer(subobj, &vsub) < 0) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2214 return NULL; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2215 sub = vsub.buf; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2216 n = vsub.len; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2217 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2218 if (n == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2219 PyErr_SetString(PyExc_ValueError, "empty separator"); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2220 PyObject_ReleaseBuffer(subobj, &vsub); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2221 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2222 } |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2223 if (n == 1) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2224 return split_char(s, len, sub[0], maxsplit); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2225 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2226 list = PyList_New(PREALLOC_SIZE(maxsplit)); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2227 if (list == NULL) { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2228 PyObject_ReleaseBuffer(subobj, &vsub); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2229 return NULL; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2230 } |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2231 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2232 #ifdef USE_FAST |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2233 i = j = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2234 while (maxsplit-- > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2235 pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2236 if (pos < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2237 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2238 j = i+pos; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2239 SPLIT_ADD(s, i, j); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2240 i = j + n; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2241 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2242 #else |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2243 i = j = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2244 while ((j+n <= len) && (maxsplit-- > 0)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2245 for (; j+n <= len; j++) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2246 if (Py_STRING_MATCH(s, j, sub, n)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2247 SPLIT_ADD(s, i, j); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2248 i = j = j + n; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2249 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2250 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2251 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2252 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2253 #endif |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2254 SPLIT_ADD(s, i, len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2255 FIX_PREALLOC_SIZE(list); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2256 PyObject_ReleaseBuffer(subobj, &vsub); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2257 return list; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2258 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2259 onError: |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2260 Py_DECREF(list); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2261 PyObject_ReleaseBuffer(subobj, &vsub); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2262 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2263 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2264 |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2265 /* stringlib's partition shares nullbytes in some cases. |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2266 undo this, we don't want the nullbytes to be shared. */ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2267 static PyObject * |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2268 make_nullbytes_unique(PyObject *result) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2269 { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2270 if (result != NULL) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2271 int i; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2272 assert(PyTuple_Check(result)); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2273 assert(PyTuple_GET_SIZE(result) == 3); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2274 for (i = 0; i < 3; i++) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2275 if (PyTuple_GET_ITEM(result, i) == (PyObject *)nullbytes) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2276 PyObject *new = PyBytes_FromStringAndSize(NULL, 0); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2277 if (new == NULL) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2278 Py_DECREF(result); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2279 result = NULL; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2280 break; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2281 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2282 Py_DECREF(nullbytes); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2283 PyTuple_SET_ITEM(result, i, new); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2284 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2285 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2286 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2287 return result; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2288 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2289 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2290 PyDoc_STRVAR(partition__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2291 "B.partition(sep) -> (head, sep, tail)\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2292 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2293 Searches for the separator sep in B, and returns the part before it,\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2294 the separator itself, and the part after it. If the separator is not\n\ |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
2295 found, returns B and two empty bytearray objects."); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2296 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2297 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2298 bytes_partition(PyBytesObject *self, PyObject *sep_obj) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2299 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2300 PyObject *bytesep, *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2301 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2302 bytesep = PyBytes_FromObject(sep_obj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2303 if (! bytesep) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2304 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2305 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2306 result = stringlib_partition( |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2307 (PyObject*) self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2308 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
2309 bytesep, |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2310 PyBytes_AS_STRING(bytesep), PyBytes_GET_SIZE(bytesep) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2311 ); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2312 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2313 Py_DECREF(bytesep); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2314 return make_nullbytes_unique(result); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2315 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2316 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2317 PyDoc_STRVAR(rpartition__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2318 "B.rpartition(sep) -> (tail, sep, head)\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2319 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2320 Searches for the separator sep in B, starting at the end of B,\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2321 and returns the part before it, the separator itself, and the\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2322 part after it. If the separator is not found, returns two empty\n\ |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
2323 bytearray objects and B."); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2324 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2325 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2326 bytes_rpartition(PyBytesObject *self, PyObject *sep_obj) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2327 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2328 PyObject *bytesep, *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2329 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2330 bytesep = PyBytes_FromObject(sep_obj); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2331 if (! bytesep) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2332 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2333 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2334 result = stringlib_rpartition( |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2335 (PyObject*) self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2336 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
2337 bytesep, |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2338 PyBytes_AS_STRING(bytesep), PyBytes_GET_SIZE(bytesep) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2339 ); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2340 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2341 Py_DECREF(bytesep); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2342 return make_nullbytes_unique(result); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2343 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2344 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2345 Py_LOCAL_INLINE(PyObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2346 rsplit_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2347 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2348 register Py_ssize_t i, j, count=0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2349 PyObject *str; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2350 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2351 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2352 if (list == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2353 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2354 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2355 i = j = len - 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2356 while ((i >= 0) && (maxcount-- > 0)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2357 for (; i >= 0; i--) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2358 if (s[i] == ch) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2359 SPLIT_ADD(s, i + 1, j + 1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2360 j = i = i - 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2361 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2362 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2363 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2364 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2365 if (j >= -1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2366 SPLIT_ADD(s, 0, j + 1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2367 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2368 FIX_PREALLOC_SIZE(list); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2369 if (PyList_Reverse(list) < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2370 goto onError; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2371 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2372 return list; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2373 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2374 onError: |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2375 Py_DECREF(list); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2376 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2377 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2378 |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2379 Py_LOCAL_INLINE(PyObject *) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2380 rsplit_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxcount) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2381 { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2382 register Py_ssize_t i, j, count = 0; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2383 PyObject *str; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2384 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2385 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2386 if (list == NULL) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2387 return NULL; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2388 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2389 for (i = j = len - 1; i >= 0; ) { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2390 /* find a token */ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2391 while (i >= 0 && Py_UNICODE_ISSPACE(s[i])) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2392 i--; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2393 j = i; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2394 while (i >= 0 && !Py_UNICODE_ISSPACE(s[i])) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2395 i--; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2396 if (j > i) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2397 if (maxcount-- <= 0) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2398 break; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2399 SPLIT_ADD(s, i + 1, j + 1); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2400 while (i >= 0 && Py_UNICODE_ISSPACE(s[i])) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2401 i--; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2402 j = i; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2403 } |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2404 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2405 if (j >= 0) { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2406 SPLIT_ADD(s, 0, j + 1); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2407 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2408 FIX_PREALLOC_SIZE(list); |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2409 if (PyList_Reverse(list) < 0) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2410 goto onError; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2411 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2412 return list; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2413 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2414 onError: |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2415 Py_DECREF(list); |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2416 return NULL; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2417 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2418 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2419 PyDoc_STRVAR(rsplit__doc__, |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
2420 "B.rsplit(sep[, maxsplit]) -> list of bytearray\n\ |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2421 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2422 Return a list of the sections in B, using sep as the delimiter,\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2423 starting at the end of B and working to the front.\n\ |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2424 If sep is not given, B is split on ASCII whitespace characters\n\ |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2425 (space, tab, return, newline, formfeed, vertical tab).\n\ |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2426 If maxsplit is given, at most maxsplit splits are done."); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2427 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2428 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2429 bytes_rsplit(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2430 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2431 Py_ssize_t len = PyBytes_GET_SIZE(self), n, i, j; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2432 Py_ssize_t maxsplit = -1, count = 0; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2433 const char *s = PyBytes_AS_STRING(self), *sub; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2434 PyObject *list, *str, *subobj = Py_None; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43321
diff
changeset
|
2435 Py_buffer vsub; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2436 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2437 if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit)) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2438 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2439 if (maxsplit < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2440 maxsplit = PY_SSIZE_T_MAX; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2441 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2442 if (subobj == Py_None) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2443 return rsplit_whitespace(s, len, maxsplit); |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2444 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2445 if (_getbuffer(subobj, &vsub) < 0) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2446 return NULL; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2447 sub = vsub.buf; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2448 n = vsub.len; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2449 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2450 if (n == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2451 PyErr_SetString(PyExc_ValueError, "empty separator"); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2452 PyObject_ReleaseBuffer(subobj, &vsub); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2453 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2454 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2455 else if (n == 1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2456 return rsplit_char(s, len, sub[0], maxsplit); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2457 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2458 list = PyList_New(PREALLOC_SIZE(maxsplit)); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2459 if (list == NULL) { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2460 PyObject_ReleaseBuffer(subobj, &vsub); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2461 return NULL; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2462 } |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2463 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2464 j = len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2465 i = j - n; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2466 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2467 while ( (i >= 0) && (maxsplit-- > 0) ) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2468 for (; i>=0; i--) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2469 if (Py_STRING_MATCH(s, i, sub, n)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2470 SPLIT_ADD(s, i + n, j); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2471 j = i; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2472 i -= n; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2473 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2474 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2475 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2476 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2477 SPLIT_ADD(s, 0, j); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2478 FIX_PREALLOC_SIZE(list); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2479 if (PyList_Reverse(list) < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2480 goto onError; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2481 PyObject_ReleaseBuffer(subobj, &vsub); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2482 return list; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2483 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2484 onError: |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2485 Py_DECREF(list); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2486 PyObject_ReleaseBuffer(subobj, &vsub); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2487 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2488 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2489 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2490 PyDoc_STRVAR(reverse__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2491 "B.reverse() -> None\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2492 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2493 Reverse the order of the values in B in place."); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2494 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2495 bytes_reverse(PyBytesObject *self, PyObject *unused) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2496 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2497 char swap, *head, *tail; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
2498 Py_ssize_t i, j, n = Py_SIZE(self); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2499 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2500 j = n / 2; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2501 head = self->ob_bytes; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2502 tail = head + n - 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2503 for (i = 0; i < j; i++) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2504 swap = *head; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2505 *head++ = *tail; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2506 *tail-- = swap; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2507 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2508 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2509 Py_RETURN_NONE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2510 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2511 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2512 PyDoc_STRVAR(insert__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2513 "B.insert(index, int) -> None\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2514 \n\ |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
2515 Insert a single item into the bytearray before the given index."); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2516 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2517 bytes_insert(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2518 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2519 int value; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
2520 Py_ssize_t where, n = Py_SIZE(self); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2521 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2522 if (!PyArg_ParseTuple(args, "ni:insert", &where, &value)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2523 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2524 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2525 if (n == PY_SSIZE_T_MAX) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2526 PyErr_SetString(PyExc_OverflowError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2527 "cannot add more objects to bytes"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2528 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2529 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2530 if (value < 0 || value >= 256) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2531 PyErr_SetString(PyExc_ValueError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2532 "byte must be in range(0, 256)"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2533 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2534 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2535 if (PyBytes_Resize((PyObject *)self, n + 1) < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2536 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2537 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2538 if (where < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2539 where += n; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2540 if (where < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2541 where = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2542 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2543 if (where > n) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2544 where = n; |
|
40982
431b3238593e
Fix off-by-one bug in memmove() call in bytes_insert().
Guido van Rossum <guido@python.org>
parents:
40978
diff
changeset
|
2545 memmove(self->ob_bytes + where + 1, self->ob_bytes + where, n - where); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2546 self->ob_bytes[where] = value; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2547 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2548 Py_RETURN_NONE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2549 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2550 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2551 PyDoc_STRVAR(append__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2552 "B.append(int) -> None\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2553 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2554 Append a single item to the end of B."); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2555 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2556 bytes_append(PyBytesObject *self, PyObject *arg) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2557 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2558 int value; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
2559 Py_ssize_t n = Py_SIZE(self); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2560 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2561 if (! _getbytevalue(arg, &value)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2562 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2563 if (n == PY_SSIZE_T_MAX) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2564 PyErr_SetString(PyExc_OverflowError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2565 "cannot add more objects to bytes"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2566 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2567 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2568 if (PyBytes_Resize((PyObject *)self, n + 1) < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2569 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2570 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2571 self->ob_bytes[n] = value; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2572 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2573 Py_RETURN_NONE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2574 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2575 |
|
44137
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2576 PyDoc_STRVAR(extend__doc__, |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2577 "B.extend(iterable int) -> None\n\ |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2578 \n\ |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2579 Append all the elements from the iterator or sequence to the\n\ |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2580 end of B."); |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2581 static PyObject * |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2582 bytes_extend(PyBytesObject *self, PyObject *arg) |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2583 { |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2584 PyObject *it, *item, *tmp, *res; |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2585 Py_ssize_t buf_size = 0, len = 0; |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2586 int value; |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2587 char *buf; |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2588 |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2589 /* bytes_setslice code only accepts something supporting PEP 3118. */ |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2590 if (PyObject_CheckBuffer(arg)) { |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
2591 if (bytes_setslice(self, Py_SIZE(self), Py_SIZE(self), arg) == -1) |
|
44137
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2592 return NULL; |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2593 |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2594 Py_RETURN_NONE; |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2595 } |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2596 |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2597 it = PyObject_GetIter(arg); |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2598 if (it == NULL) |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2599 return NULL; |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2600 |
|
44226
aad65a2424e3
Merged revisions 59376-59406 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44138
diff
changeset
|
2601 /* Try to determine the length of the argument. 32 is abitrary. */ |
|
aad65a2424e3
Merged revisions 59376-59406 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44138
diff
changeset
|
2602 buf_size = _PyObject_LengthHint(arg, 32); |
|
44137
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2603 |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2604 buf = (char *)PyMem_Malloc(buf_size * sizeof(char)); |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2605 if (buf == NULL) |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2606 return PyErr_NoMemory(); |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2607 |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2608 while ((item = PyIter_Next(it)) != NULL) { |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2609 if (! _getbytevalue(item, &value)) { |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2610 Py_DECREF(item); |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2611 Py_DECREF(it); |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2612 return NULL; |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2613 } |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2614 buf[len++] = value; |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2615 Py_DECREF(item); |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2616 if (len >= buf_size) { |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2617 buf_size = len + (len >> 1) + 1; |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2618 buf = (char *)PyMem_Realloc(buf, buf_size * sizeof(char)); |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2619 if (buf == NULL) { |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2620 Py_DECREF(it); |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2621 return PyErr_NoMemory(); |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2622 } |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2623 } |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2624 } |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2625 Py_DECREF(it); |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2626 |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2627 /* XXX: Is possible to avoid a full copy of the buffer? */ |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2628 tmp = PyBytes_FromStringAndSize(buf, len); |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2629 res = bytes_extend(self, tmp); |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2630 Py_DECREF(tmp); |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2631 PyMem_Free(buf); |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2632 |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2633 return res; |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2634 } |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44099
diff
changeset
|
2635 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2636 PyDoc_STRVAR(pop__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2637 "B.pop([index]) -> int\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2638 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2639 Remove and return a single item from B. If no index\n\ |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2640 argument is give, will pop the last value."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2641 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2642 bytes_pop(PyBytesObject *self, PyObject *args) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2643 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2644 int value; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
2645 Py_ssize_t where = -1, n = Py_SIZE(self); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2646 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2647 if (!PyArg_ParseTuple(args, "|n:pop", &where)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2648 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2649 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2650 if (n == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2651 PyErr_SetString(PyExc_OverflowError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2652 "cannot pop an empty bytes"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2653 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2654 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2655 if (where < 0) |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
2656 where += Py_SIZE(self); |
|
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
2657 if (where < 0 || where >= Py_SIZE(self)) { |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2658 PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2659 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2660 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2661 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2662 value = self->ob_bytes[where]; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2663 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2664 if (PyBytes_Resize((PyObject *)self, n - 1) < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2665 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2666 |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43947
diff
changeset
|
2667 return PyLong_FromLong(value); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2668 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2669 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2670 PyDoc_STRVAR(remove__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2671 "B.remove(int) -> None\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2672 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2673 Remove the first occurance of a value in B."); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2674 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2675 bytes_remove(PyBytesObject *self, PyObject *arg) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2676 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2677 int value; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
2678 Py_ssize_t where, n = Py_SIZE(self); |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2679 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2680 if (! _getbytevalue(arg, &value)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2681 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2682 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2683 for (where = 0; where < n; where++) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2684 if (self->ob_bytes[where] == value) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2685 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2686 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2687 if (where == n) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2688 PyErr_SetString(PyExc_ValueError, "value not found in bytes"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2689 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2690 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2691 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2692 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2693 if (PyBytes_Resize((PyObject *)self, n - 1) < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2694 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2695 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2696 Py_RETURN_NONE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2697 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2698 |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2699 /* XXX These two helpers could be optimized if argsize == 1 */ |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2700 |
|
42893
2c02ba72714b
Make some internal functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
42779
diff
changeset
|
2701 static Py_ssize_t |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2702 lstrip_helper(unsigned char *myptr, Py_ssize_t mysize, |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2703 void *argptr, Py_ssize_t argsize) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2704 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2705 Py_ssize_t i = 0; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2706 while (i < mysize && memchr(argptr, myptr[i], argsize)) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2707 i++; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2708 return i; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2709 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2710 |
|
42893
2c02ba72714b
Make some internal functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
42779
diff
changeset
|
2711 static Py_ssize_t |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2712 rstrip_helper(unsigned char *myptr, Py_ssize_t mysize, |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2713 void *argptr, Py_ssize_t argsize) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2714 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2715 Py_ssize_t i = mysize - 1; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2716 while (i >= 0 && memchr(argptr, myptr[i], argsize)) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2717 i--; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2718 return i + 1; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2719 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2720 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2721 PyDoc_STRVAR(strip__doc__, |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
2722 "B.strip([bytes]) -> bytearray\n\ |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2723 \n\ |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2724 Strip leading and trailing bytes contained in the argument.\n\ |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2725 If the argument is omitted, strip ASCII whitespace."); |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2726 static PyObject * |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2727 bytes_strip(PyBytesObject *self, PyObject *args) |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2728 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2729 Py_ssize_t left, right, mysize, argsize; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2730 void *myptr, *argptr; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2731 PyObject *arg = Py_None; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43321
diff
changeset
|
2732 Py_buffer varg; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2733 if (!PyArg_ParseTuple(args, "|O:strip", &arg)) |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2734 return NULL; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2735 if (arg == Py_None) { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2736 argptr = "\t\n\r\f\v "; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2737 argsize = 6; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2738 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2739 else { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2740 if (_getbuffer(arg, &varg) < 0) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2741 return NULL; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2742 argptr = varg.buf; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2743 argsize = varg.len; |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2744 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2745 myptr = self->ob_bytes; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
2746 mysize = Py_SIZE(self); |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2747 left = lstrip_helper(myptr, mysize, argptr, argsize); |
|
42499
96098abc8f8f
Fix core dump in an endcase of b.strip() that I missed.
Guido van Rossum <guido@python.org>
parents:
42457
diff
changeset
|
2748 if (left == mysize) |
|
96098abc8f8f
Fix core dump in an endcase of b.strip() that I missed.
Guido van Rossum <guido@python.org>
parents:
42457
diff
changeset
|
2749 right = left; |
|
96098abc8f8f
Fix core dump in an endcase of b.strip() that I missed.
Guido van Rossum <guido@python.org>
parents:
42457
diff
changeset
|
2750 else |
|
96098abc8f8f
Fix core dump in an endcase of b.strip() that I missed.
Guido van Rossum <guido@python.org>
parents:
42457
diff
changeset
|
2751 right = rstrip_helper(myptr, mysize, argptr, argsize); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2752 if (arg != Py_None) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2753 PyObject_ReleaseBuffer(arg, &varg); |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2754 return PyBytes_FromStringAndSize(self->ob_bytes + left, right - left); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2755 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2756 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2757 PyDoc_STRVAR(lstrip__doc__, |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
2758 "B.lstrip([bytes]) -> bytearray\n\ |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2759 \n\ |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2760 Strip leading bytes contained in the argument.\n\ |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2761 If the argument is omitted, strip leading ASCII whitespace."); |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2762 static PyObject * |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2763 bytes_lstrip(PyBytesObject *self, PyObject *args) |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2764 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2765 Py_ssize_t left, right, mysize, argsize; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2766 void *myptr, *argptr; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2767 PyObject *arg = Py_None; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43321
diff
changeset
|
2768 Py_buffer varg; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2769 if (!PyArg_ParseTuple(args, "|O:lstrip", &arg)) |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2770 return NULL; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2771 if (arg == Py_None) { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2772 argptr = "\t\n\r\f\v "; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2773 argsize = 6; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2774 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2775 else { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2776 if (_getbuffer(arg, &varg) < 0) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2777 return NULL; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2778 argptr = varg.buf; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2779 argsize = varg.len; |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2780 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2781 myptr = self->ob_bytes; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
2782 mysize = Py_SIZE(self); |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2783 left = lstrip_helper(myptr, mysize, argptr, argsize); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2784 right = mysize; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2785 if (arg != Py_None) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2786 PyObject_ReleaseBuffer(arg, &varg); |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2787 return PyBytes_FromStringAndSize(self->ob_bytes + left, right - left); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2788 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2789 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2790 PyDoc_STRVAR(rstrip__doc__, |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
2791 "B.rstrip([bytes]) -> bytearray\n\ |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2792 \n\ |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2793 Strip trailing bytes contained in the argument.\n\ |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2794 If the argument is omitted, strip trailing ASCII whitespace."); |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2795 static PyObject * |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2796 bytes_rstrip(PyBytesObject *self, PyObject *args) |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2797 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2798 Py_ssize_t left, right, mysize, argsize; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2799 void *myptr, *argptr; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2800 PyObject *arg = Py_None; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43321
diff
changeset
|
2801 Py_buffer varg; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2802 if (!PyArg_ParseTuple(args, "|O:rstrip", &arg)) |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2803 return NULL; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2804 if (arg == Py_None) { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2805 argptr = "\t\n\r\f\v "; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2806 argsize = 6; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2807 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2808 else { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2809 if (_getbuffer(arg, &varg) < 0) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2810 return NULL; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2811 argptr = varg.buf; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2812 argsize = varg.len; |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2813 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2814 myptr = self->ob_bytes; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
2815 mysize = Py_SIZE(self); |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2816 left = 0; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2817 right = rstrip_helper(myptr, mysize, argptr, argsize); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2818 if (arg != Py_None) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2819 PyObject_ReleaseBuffer(arg, &varg); |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2820 return PyBytes_FromStringAndSize(self->ob_bytes + left, right - left); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2821 } |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2822 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2823 PyDoc_STRVAR(decode_doc, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2824 "B.decode([encoding[, errors]]) -> unicode object.\n\ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2825 \n\ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2826 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
|
2827 to the default encoding. errors may be given to set a different error\n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2828 handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2829 a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 44249 | 2830 as well as any other name registered with codecs.register_error that is\n\ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2831 able to handle UnicodeDecodeErrors."); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2832 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2833 static PyObject * |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2834 bytes_decode(PyObject *self, PyObject *args) |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
2835 { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2836 const char *encoding = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2837 const char *errors = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2838 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2839 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
|
2840 return NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2841 if (encoding == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2842 encoding = PyUnicode_GetDefaultEncoding(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2843 return PyCodec_Decode(self, encoding, errors); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2844 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2845 |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2846 PyDoc_STRVAR(alloc_doc, |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2847 "B.__alloc__() -> int\n\ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2848 \n\ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2849 Returns the number of bytes actually allocated."); |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2850 |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2851 static PyObject * |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2852 bytes_alloc(PyBytesObject *self) |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2853 { |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43947
diff
changeset
|
2854 return PyLong_FromSsize_t(self->ob_alloc); |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2855 } |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2856 |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2857 PyDoc_STRVAR(join_doc, |
|
41664
a363cea32b9b
I don't know how come bytes.join() was a class method, but that's clearly
Guido van Rossum <guido@python.org>
parents:
41648
diff
changeset
|
2858 "B.join(iterable_of_bytes) -> bytes\n\ |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2859 \n\ |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
2860 Concatenates any number of bytearray objects, with B in between each pair."); |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2861 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2862 static PyObject * |
|
41664
a363cea32b9b
I don't know how come bytes.join() was a class method, but that's clearly
Guido van Rossum <guido@python.org>
parents:
41648
diff
changeset
|
2863 bytes_join(PyBytesObject *self, PyObject *it) |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2864 { |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2865 PyObject *seq; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
2866 Py_ssize_t mysize = Py_SIZE(self); |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2867 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
|
2868 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
|
2869 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
|
2870 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
|
2871 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
|
2872 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
|
2873 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2874 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
|
2875 if (seq == NULL) |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2876 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
|
2877 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
|
2878 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
|
2879 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2880 /* Compute the total size, and check that they are all bytes */ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2881 /* XXX Shouldn't we use _getbuffer() on these items instead? */ |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2882 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
|
2883 PyObject *obj = items[i]; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2884 if (!PyBytes_Check(obj) && !PyString_Check(obj)) { |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2885 PyErr_Format(PyExc_TypeError, |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2886 "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
|
2887 "(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
|
2888 /* XXX %ld isn't right on Win64 */ |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
2889 (long)i, Py_TYPE(obj)->tp_name); |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2890 goto error; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2891 } |
|
41664
a363cea32b9b
I don't know how come bytes.join() was a class method, but that's clearly
Guido van Rossum <guido@python.org>
parents:
41648
diff
changeset
|
2892 if (i > 0) |
|
a363cea32b9b
I don't know how come bytes.join() was a class method, but that's clearly
Guido van Rossum <guido@python.org>
parents:
41648
diff
changeset
|
2893 totalsize += mysize; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
2894 totalsize += Py_SIZE(obj); |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2895 if (totalsize < 0) { |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2896 PyErr_NoMemory(); |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2897 goto error; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2898 } |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2899 } |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2900 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2901 /* 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
|
2902 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
|
2903 if (result == NULL) |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2904 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
|
2905 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
|
2906 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
|
2907 PyObject *obj = items[i]; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
2908 Py_ssize_t size = Py_SIZE(obj); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2909 char *buf; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2910 if (PyBytes_Check(obj)) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2911 buf = PyBytes_AS_STRING(obj); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2912 else |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2913 buf = PyString_AS_STRING(obj); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2914 if (i) { |
|
41664
a363cea32b9b
I don't know how come bytes.join() was a class method, but that's clearly
Guido van Rossum <guido@python.org>
parents:
41648
diff
changeset
|
2915 memcpy(dest, self->ob_bytes, mysize); |
|
a363cea32b9b
I don't know how come bytes.join() was a class method, but that's clearly
Guido van Rossum <guido@python.org>
parents:
41648
diff
changeset
|
2916 dest += mysize; |
|
a363cea32b9b
I don't know how come bytes.join() was a class method, but that's clearly
Guido van Rossum <guido@python.org>
parents:
41648
diff
changeset
|
2917 } |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2918 memcpy(dest, buf, size); |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2919 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
|
2920 } |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2921 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2922 /* Done */ |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2923 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
|
2924 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
|
2925 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2926 /* 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
|
2927 error: |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2928 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
|
2929 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
|
2930 } |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2931 |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2932 PyDoc_STRVAR(fromhex_doc, |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
2933 "bytearray.fromhex(string) -> bytearray\n\ |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2934 \n\ |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
2935 Create a bytearray object from a string of hexadecimal numbers.\n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2936 Spaces between two numbers are accepted.\n\ |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
2937 Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')."); |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2938 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2939 static int |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2940 hex_digit_to_int(Py_UNICODE c) |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2941 { |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2942 if (c >= 128) |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2943 return -1; |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
2944 if (ISDIGIT(c)) |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2945 return c - '0'; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2946 else { |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
2947 if (ISUPPER(c)) |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
2948 c = TOLOWER(c); |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2949 if (c >= 'a' && c <= 'f') |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2950 return c - 'a' + 10; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2951 } |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2952 return -1; |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2953 } |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2954 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2955 static PyObject * |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2956 bytes_fromhex(PyObject *cls, PyObject *args) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2957 { |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
2958 PyObject *newbytes, *hexobj; |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
2959 char *buf; |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2960 Py_UNICODE *hex; |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2961 Py_ssize_t hexlen, byteslen, i, j; |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2962 int top, bot; |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2963 |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2964 if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj)) |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2965 return NULL; |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2966 assert(PyUnicode_Check(hexobj)); |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2967 hexlen = PyUnicode_GET_SIZE(hexobj); |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2968 hex = PyUnicode_AS_UNICODE(hexobj); |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2969 byteslen = hexlen/2; /* This overestimates if there are spaces */ |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2970 newbytes = PyBytes_FromStringAndSize(NULL, byteslen); |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2971 if (!newbytes) |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2972 return NULL; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2973 buf = PyBytes_AS_STRING(newbytes); |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2974 for (i = j = 0; i < hexlen; i += 2) { |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2975 /* skip over spaces in the input */ |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2976 while (hex[i] == ' ') |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2977 i++; |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2978 if (i >= hexlen) |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2979 break; |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2980 top = hex_digit_to_int(hex[i]); |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2981 bot = hex_digit_to_int(hex[i+1]); |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2982 if (top == -1 || bot == -1) { |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2983 PyErr_Format(PyExc_ValueError, |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2984 "non-hexadecimal number found in " |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2985 "fromhex() arg at position %zd", i); |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2986 goto error; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2987 } |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2988 buf[j++] = (top << 4) + bot; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2989 } |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2990 if (PyBytes_Resize(newbytes, j) < 0) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2991 goto error; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2992 return newbytes; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2993 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2994 error: |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2995 Py_DECREF(newbytes); |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2996 return NULL; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2997 } |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2998 |
|
41426
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2999 PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
3000 |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
3001 static PyObject * |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
3002 bytes_reduce(PyBytesObject *self) |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
3003 { |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
3004 PyObject *latin1, *dict; |
|
42457
080f2aa18015
Change PyUnicode_FromString[AndSize] to expect UTF-8.
Martin v. Löwis <martin@v.loewis.de>
parents:
42335
diff
changeset
|
3005 if (self->ob_bytes) |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
3006 latin1 = PyUnicode_DecodeLatin1(self->ob_bytes, |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
3007 Py_SIZE(self), NULL); |
|
42457
080f2aa18015
Change PyUnicode_FromString[AndSize] to expect UTF-8.
Martin v. Löwis <martin@v.loewis.de>
parents:
42335
diff
changeset
|
3008 else |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
3009 latin1 = PyUnicode_FromString(""); |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
3010 |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
3011 dict = PyObject_GetAttrString((PyObject *)self, "__dict__"); |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
3012 if (dict == NULL) { |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
3013 PyErr_Clear(); |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
3014 dict = Py_None; |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
3015 Py_INCREF(dict); |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
3016 } |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
3017 |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44249
diff
changeset
|
3018 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict); |
|
41426
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
3019 } |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
3020 |
|
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
|
3021 static PySequenceMethods bytes_as_sequence = { |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
3022 (lenfunc)bytes_length, /* sq_length */ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
3023 (binaryfunc)PyBytes_Concat, /* sq_concat */ |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
3024 (ssizeargfunc)bytes_repeat, /* sq_repeat */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
3025 (ssizeargfunc)bytes_getitem, /* sq_item */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
3026 0, /* sq_slice */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
3027 (ssizeobjargproc)bytes_setitem, /* sq_ass_item */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
3028 0, /* sq_ass_slice */ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3029 (objobjproc)bytes_contains, /* sq_contains */ |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
3030 (binaryfunc)bytes_iconcat, /* sq_inplace_concat */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
3031 (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
|
3032 }; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3033 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3034 static PyMappingMethods bytes_as_mapping = { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3035 (lenfunc)bytes_length, |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
3036 (binaryfunc)bytes_subscript, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
3037 (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
|
3038 }; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3039 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3040 static PyBufferProcs bytes_as_buffer = { |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
3041 (getbufferproc)bytes_getbuffer, |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42499
diff
changeset
|
3042 (releasebufferproc)bytes_releasebuffer, |
|
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
|
3043 }; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3044 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3045 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
|
3046 bytes_methods[] = { |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3047 {"__alloc__", (PyCFunction)bytes_alloc, METH_NOARGS, alloc_doc}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3048 {"__reduce__", (PyCFunction)bytes_reduce, METH_NOARGS, reduce_doc}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3049 {"append", (PyCFunction)bytes_append, METH_O, append__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3050 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3051 _Py_capitalize__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3052 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
3053 {"count", (PyCFunction)bytes_count, METH_VARARGS, count__doc__}, |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3054 {"decode", (PyCFunction)bytes_decode, METH_VARARGS, decode_doc}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3055 {"endswith", (PyCFunction)bytes_endswith, METH_VARARGS, endswith__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3056 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3057 expandtabs__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3058 {"extend", (PyCFunction)bytes_extend, METH_O, extend__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3059 {"find", (PyCFunction)bytes_find, METH_VARARGS, find__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3060 {"fromhex", (PyCFunction)bytes_fromhex, METH_VARARGS|METH_CLASS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3061 fromhex_doc}, |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
3062 {"index", (PyCFunction)bytes_index, METH_VARARGS, index__doc__}, |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3063 {"insert", (PyCFunction)bytes_insert, METH_VARARGS, insert__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3064 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3065 _Py_isalnum__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3066 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3067 _Py_isalpha__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3068 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3069 _Py_isdigit__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3070 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3071 _Py_islower__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3072 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3073 _Py_isspace__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3074 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3075 _Py_istitle__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3076 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3077 _Py_isupper__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3078 {"join", (PyCFunction)bytes_join, METH_O, join_doc}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3079 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3080 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3081 {"lstrip", (PyCFunction)bytes_lstrip, METH_VARARGS, lstrip__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3082 {"partition", (PyCFunction)bytes_partition, METH_O, partition__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3083 {"pop", (PyCFunction)bytes_pop, METH_VARARGS, pop__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3084 {"remove", (PyCFunction)bytes_remove, METH_O, remove__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3085 {"replace", (PyCFunction)bytes_replace, METH_VARARGS, replace__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3086 {"reverse", (PyCFunction)bytes_reverse, METH_NOARGS, reverse__doc__}, |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
3087 {"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, rfind__doc__}, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
3088 {"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, rindex__doc__}, |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3089 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3090 {"rpartition", (PyCFunction)bytes_rpartition, METH_O, rpartition__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3091 {"rsplit", (PyCFunction)bytes_rsplit, METH_VARARGS, rsplit__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3092 {"rstrip", (PyCFunction)bytes_rstrip, METH_VARARGS, rstrip__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3093 {"split", (PyCFunction)bytes_split, METH_VARARGS, split__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3094 {"splitlines", (PyCFunction)stringlib_splitlines, METH_VARARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3095 splitlines__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3096 {"startswith", (PyCFunction)bytes_startswith, METH_VARARGS , |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3097 startswith__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3098 {"strip", (PyCFunction)bytes_strip, METH_VARARGS, strip__doc__}, |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
3099 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
3100 _Py_swapcase__doc__}, |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
3101 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3102 {"translate", (PyCFunction)bytes_translate, METH_VARARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3103 translate__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
3104 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
3105 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__}, |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
3106 {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
|
3107 }; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3108 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3109 PyDoc_STRVAR(bytes_doc, |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
3110 "bytearray(iterable_of_ints) -> bytearray.\n\ |
|
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
3111 bytearray(string, encoding[, errors]) -> bytearray.\n\ |
|
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
3112 bytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray.\n\ |
|
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
3113 bytearray(memory_view) -> bytearray.\n\ |
|
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
|
3114 \n\ |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
3115 Construct an mutable bytearray object from:\n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
3116 - an iterable yielding integers in range(256)\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
3117 - a text string encoded using the specified encoding\n\ |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
3118 - a bytes or a bytearray object\n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
3119 - any object implementing the buffer API.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
3120 \n\ |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
3121 bytearray(int) -> bytearray.\n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
3122 \n\ |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
3123 Construct a zero-initialized bytearray of the given length."); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
3124 |
|
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
|
3125 |
|
43689
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3126 static PyObject *bytes_iter(PyObject *seq); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3127 |
|
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
|
3128 PyTypeObject PyBytes_Type = { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
3129 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
3130 "bytearray", |
|
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
|
3131 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
|
3132 0, |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3133 (destructor)bytes_dealloc, /* tp_dealloc */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3134 0, /* tp_print */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3135 0, /* tp_getattr */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3136 0, /* tp_setattr */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3137 0, /* tp_compare */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3138 (reprfunc)bytes_repr, /* tp_repr */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3139 0, /* tp_as_number */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3140 &bytes_as_sequence, /* tp_as_sequence */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3141 &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
|
3142 0, /* tp_hash */ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3143 0, /* tp_call */ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
3144 bytes_str, /* tp_str */ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3145 PyObject_GenericGetAttr, /* tp_getattro */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3146 0, /* tp_setattro */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3147 &bytes_as_buffer, /* tp_as_buffer */ |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
3148 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3149 bytes_doc, /* tp_doc */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3150 0, /* tp_traverse */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3151 0, /* tp_clear */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3152 (richcmpfunc)bytes_richcompare, /* tp_richcompare */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3153 0, /* tp_weaklistoffset */ |
|
43689
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3154 bytes_iter, /* tp_iter */ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3155 0, /* tp_iternext */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3156 bytes_methods, /* tp_methods */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3157 0, /* tp_members */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3158 0, /* tp_getset */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3159 0, /* tp_base */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3160 0, /* tp_dict */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3161 0, /* tp_descr_get */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3162 0, /* tp_descr_set */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3163 0, /* tp_dictoffset */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3164 (initproc)bytes_init, /* tp_init */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3165 PyType_GenericAlloc, /* tp_alloc */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3166 PyType_GenericNew, /* tp_new */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3167 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
|
3168 }; |
|
43689
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3169 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3170 /*********************** Bytes Iterator ****************************/ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3171 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3172 typedef struct { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3173 PyObject_HEAD |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3174 Py_ssize_t it_index; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3175 PyBytesObject *it_seq; /* Set to NULL when iterator is exhausted */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3176 } bytesiterobject; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3177 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3178 static void |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3179 bytesiter_dealloc(bytesiterobject *it) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3180 { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3181 _PyObject_GC_UNTRACK(it); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3182 Py_XDECREF(it->it_seq); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3183 PyObject_GC_Del(it); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3184 } |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3185 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3186 static int |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3187 bytesiter_traverse(bytesiterobject *it, visitproc visit, void *arg) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3188 { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3189 Py_VISIT(it->it_seq); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3190 return 0; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3191 } |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3192 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3193 static PyObject * |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3194 bytesiter_next(bytesiterobject *it) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3195 { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3196 PyBytesObject *seq; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3197 PyObject *item; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3198 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3199 assert(it != NULL); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3200 seq = it->it_seq; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3201 if (seq == NULL) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3202 return NULL; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3203 assert(PyBytes_Check(seq)); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3204 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3205 if (it->it_index < PyBytes_GET_SIZE(seq)) { |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43947
diff
changeset
|
3206 item = PyLong_FromLong( |
|
43689
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3207 (unsigned char)seq->ob_bytes[it->it_index]); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3208 if (item != NULL) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3209 ++it->it_index; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3210 return item; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3211 } |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3212 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3213 Py_DECREF(seq); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3214 it->it_seq = NULL; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3215 return NULL; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3216 } |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3217 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3218 static PyObject * |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3219 bytesiter_length_hint(bytesiterobject *it) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3220 { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3221 Py_ssize_t len = 0; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3222 if (it->it_seq) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3223 len = PyBytes_GET_SIZE(it->it_seq) - it->it_index; |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43947
diff
changeset
|
3224 return PyLong_FromSsize_t(len); |
|
43689
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3225 } |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3226 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3227 PyDoc_STRVAR(length_hint_doc, |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3228 "Private method returning an estimate of len(list(it))."); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3229 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3230 static PyMethodDef bytesiter_methods[] = { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3231 {"__length_hint__", (PyCFunction)bytesiter_length_hint, METH_NOARGS, |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3232 length_hint_doc}, |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3233 {NULL, NULL} /* sentinel */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3234 }; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3235 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3236 PyTypeObject PyBytesIter_Type = { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3237 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
3238 "bytearray_iterator", /* tp_name */ |
|
43689
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3239 sizeof(bytesiterobject), /* tp_basicsize */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3240 0, /* tp_itemsize */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3241 /* methods */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3242 (destructor)bytesiter_dealloc, /* tp_dealloc */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3243 0, /* tp_print */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3244 0, /* tp_getattr */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3245 0, /* tp_setattr */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3246 0, /* tp_compare */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3247 0, /* tp_repr */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3248 0, /* tp_as_number */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3249 0, /* tp_as_sequence */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3250 0, /* tp_as_mapping */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3251 0, /* tp_hash */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3252 0, /* tp_call */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3253 0, /* tp_str */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3254 PyObject_GenericGetAttr, /* tp_getattro */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3255 0, /* tp_setattro */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3256 0, /* tp_as_buffer */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3257 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3258 0, /* tp_doc */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3259 (traverseproc)bytesiter_traverse, /* tp_traverse */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3260 0, /* tp_clear */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3261 0, /* tp_richcompare */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3262 0, /* tp_weaklistoffset */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3263 PyObject_SelfIter, /* tp_iter */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3264 (iternextfunc)bytesiter_next, /* tp_iternext */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3265 bytesiter_methods, /* tp_methods */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3266 0, |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3267 }; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3268 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3269 static PyObject * |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3270 bytes_iter(PyObject *seq) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3271 { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3272 bytesiterobject *it; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3273 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3274 if (!PyBytes_Check(seq)) { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3275 PyErr_BadInternalCall(); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3276 return NULL; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3277 } |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3278 it = PyObject_GC_New(bytesiterobject, &PyBytesIter_Type); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3279 if (it == NULL) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3280 return NULL; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3281 it->it_index = 0; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3282 Py_INCREF(seq); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3283 it->it_seq = (PyBytesObject *)seq; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3284 _PyObject_GC_TRACK(it); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3285 return (PyObject *)it; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3286 } |
