Mercurial > cpython-lru_cache-weakref
annotate Objects/bytesobject.c @ 43757:2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
I suspect this has some problems when the subclass is evil,
but that's for later.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Sat, 03 Nov 2007 00:24:24 +0000 |
| parents | 0502a8c64ef2 |
| children | 0d462d789b18 |
| rev | line source |
|---|---|
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 /* Bytes object implementation */ |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 /* XXX TO DO: optimizations */ |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 #define PY_SSIZE_T_CLEAN |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 #include "Python.h" |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
7 #include "structmember.h" |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
8 #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
|
9 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
10 static PyBytesObject *nullbytes = NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
11 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
12 void |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
13 PyBytes_Fini(void) |
|
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 Py_CLEAR(nullbytes); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
16 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
17 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
18 int |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
19 PyBytes_Init(void) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
20 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
21 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
|
22 if (nullbytes == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
23 return 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
24 nullbytes->ob_bytes = NULL; |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
25 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
|
26 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
|
27 return 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
28 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
29 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
30 /* end nullbytes support */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
31 |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
32 /* Helpers */ |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
33 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
34 static int |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
35 _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
|
36 { |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
37 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
|
38 |
|
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 (PyInt_Check(arg)) { |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
40 face_value = PyInt_AsLong(arg); |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
41 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
|
42 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
|
43 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
|
44 } |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
45 } else { |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
46 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
|
47 return 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
48 } |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
49 |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
50 *value = face_value; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
51 return 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
52 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
53 |
|
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
|
54 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
|
55 bytes_getbuffer(PyBytesObject *obj, Py_buffer *view, int flags) |
| 42779 | 56 { |
|
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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 } |
| 42779 | 63 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
|
64 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
|
65 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
|
66 ptr = obj->ob_bytes; |
|
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 ret = PyBuffer_FillInfo(view, ptr, Py_Size(obj), 0, flags); |
|
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 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
|
69 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
|
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 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
|
72 } |
|
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
|
73 |
|
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 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
|
75 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
|
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 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
|
78 } |
|
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
|
79 |
|
42893
2c02ba72714b
Make some internal functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
42779
diff
changeset
|
80 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
|
81 _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
|
82 { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
83 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
|
84 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
85 if (buffer == NULL || buffer->bf_getbuffer == NULL) |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
86 { |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
87 PyErr_Format(PyExc_TypeError, |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
88 "Type %.100s doesn't support the buffer API", |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
89 Py_Type(obj)->tp_name); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
90 return -1; |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
91 } |
|
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
|
92 |
|
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 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
|
94 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
|
95 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
|
96 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
97 |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 /* 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
|
99 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 PyObject * |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
101 PyBytes_FromObject(PyObject *input) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
102 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
103 return PyObject_CallFunctionObjArgs((PyObject *)&PyBytes_Type, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
104 input, NULL); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
105 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
106 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
107 PyObject * |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
108 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
|
109 { |
|
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 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
|
111 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
|
112 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
113 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
|
114 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 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
|
116 if (new == NULL) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
117 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
|
118 |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
119 if (size == 0) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
120 new->ob_bytes = NULL; |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
121 alloc = 0; |
|
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
122 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
123 else { |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
124 alloc = size + 1; |
|
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
125 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
|
126 if (new->ob_bytes == NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
127 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
|
128 return PyErr_NoMemory(); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
129 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
130 if (bytes != NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
131 memcpy(new->ob_bytes, bytes, size); |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
132 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
|
133 } |
| 42335 | 134 Py_Size(new) = size; |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
135 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
|
136 new->ob_exports = 0; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
137 |
|
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
|
138 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
|
139 } |
|
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 |
|
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 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
|
142 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
|
143 { |
|
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 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
|
145 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
|
146 |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
147 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
|
148 } |
|
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 |
|
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 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
|
151 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
|
152 { |
|
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 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
|
154 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
|
155 |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
156 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
|
157 } |
|
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 |
|
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 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
|
160 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
|
161 { |
|
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 void *sval; |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
163 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
|
164 |
|
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(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
|
166 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
|
167 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
|
168 |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
169 if (size < alloc / 2) { |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
170 /* 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
|
171 alloc = size + 1; |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
172 } |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
173 else if (size < alloc) { |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
174 /* Within allocated size; quick exit */ |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
175 Py_Size(self) = size; |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
176 ((PyBytesObject *)self)->ob_bytes[size] = '\0'; /* Trailing null */ |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
177 return 0; |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
178 } |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
179 else if (size <= alloc * 1.125) { |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
180 /* Moderate upsize; overallocate similar to list_resize() */ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
181 alloc = size + (size >> 3) + (size < 9 ? 3 : 6); |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
182 } |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
183 else { |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
184 /* 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
|
185 alloc = size + 1; |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
186 } |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
187 |
|
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
|
188 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
|
189 /* |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
190 fprintf(stderr, "%d: %s", ((PyBytesObject *)self)->ob_exports, |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
191 ((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
|
192 */ |
|
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 PyErr_SetString(PyExc_BufferError, |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
194 "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
|
195 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
|
196 } |
|
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
|
197 |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
198 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
|
199 if (sval == NULL) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
200 PyErr_NoMemory(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
201 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
|
202 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
204 ((PyBytesObject *)self)->ob_bytes = sval; |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
205 Py_Size(self) = size; |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
206 ((PyBytesObject *)self)->ob_alloc = alloc; |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
207 ((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
|
208 |
|
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
|
209 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
|
210 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
211 |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
212 PyObject * |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
213 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
|
214 { |
|
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
|
215 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
|
216 Py_buffer va, vb; |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
217 PyBytesObject *result; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
218 |
|
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
|
219 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
|
220 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
|
221 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
|
222 _getbuffer(b, &vb) < 0) { |
| 42779 | 223 if (va.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
|
224 PyObject_ReleaseBuffer(a, &va); |
|
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
|
225 if (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
|
226 PyObject_ReleaseBuffer(b, &vb); |
|
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 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
|
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 Py_Type(a)->tp_name, Py_Type(b)->tp_name); |
|
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
|
229 return NULL; |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
230 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
231 |
|
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
|
232 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
|
233 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
|
234 PyObject_ReleaseBuffer(a, &va); |
|
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 PyObject_ReleaseBuffer(b, &vb); |
|
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
|
236 return PyErr_NoMemory(); |
|
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
|
237 } |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
238 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
239 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
|
240 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
|
241 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
|
242 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
|
243 } |
| 42779 | 244 |
|
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
|
245 PyObject_ReleaseBuffer(a, &va); |
|
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
|
246 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
|
247 return (PyObject *)result; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
248 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
249 |
|
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
|
250 /* 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
|
251 |
|
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 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
|
253 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
|
254 { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
255 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
|
256 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 static PyObject * |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
259 bytes_concat(PyBytesObject *self, PyObject *other) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
260 { |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
261 return PyBytes_Concat((PyObject *)self, other); |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
262 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
263 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
264 static PyObject * |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
265 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
|
266 { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
267 Py_ssize_t mysize; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
268 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
|
269 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
|
270 |
|
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
|
271 if (_getbuffer(other, &vo) < 0) { |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
272 PyErr_Format(PyExc_TypeError, "can't concat bytes to %.100s", |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
273 Py_Type(self)->tp_name); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
274 return NULL; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
275 } |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
276 |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
277 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
|
278 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
|
279 if (size < 0) { |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
280 PyObject_ReleaseBuffer(other, &vo); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
281 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
|
282 } |
|
41597
cb3c92062ec4
Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents:
41595
diff
changeset
|
283 if (size < self->ob_alloc) { |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
284 Py_Size(self) = size; |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
285 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
|
286 } |
|
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
|
287 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
|
288 PyObject_ReleaseBuffer(other, &vo); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
289 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
|
290 } |
|
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
|
291 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
|
292 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
|
293 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
|
294 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
|
295 } |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
296 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
297 static PyObject * |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
298 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
|
299 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
300 PyBytesObject *result; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
301 Py_ssize_t mysize; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
302 Py_ssize_t size; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
303 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
304 if (count < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
305 count = 0; |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
306 mysize = Py_Size(self); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
307 size = mysize * count; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
308 if (count != 0 && size / count != mysize) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
309 return PyErr_NoMemory(); |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
310 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
|
311 if (result != NULL && size != 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
312 if (mysize == 1) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
313 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
|
314 else { |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
315 Py_ssize_t i; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
316 for (i = 0; i < count; i++) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
317 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
|
318 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
319 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
320 return (PyObject *)result; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
321 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
322 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
323 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
|
324 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
|
325 { |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
326 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
|
327 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
|
328 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
329 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
|
330 count = 0; |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
331 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
|
332 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
|
333 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
|
334 return PyErr_NoMemory(); |
|
41597
cb3c92062ec4
Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents:
41595
diff
changeset
|
335 if (size < self->ob_alloc) { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
336 Py_Size(self) = size; |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
337 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
|
338 } |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
339 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
|
340 return NULL; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
341 |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
342 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
|
343 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
|
344 else { |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
345 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
|
346 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
|
347 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
|
348 } |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
349 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
350 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
|
351 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
|
352 } |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
353 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
354 static int |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
355 bytes_substring(PyBytesObject *self, PyBytesObject *other) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
356 { |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
357 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
|
358 |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
359 if (Py_Size(other) == 1) { |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
360 return memchr(self->ob_bytes, other->ob_bytes[0], |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
361 Py_Size(self)) != NULL; |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
362 } |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
363 if (Py_Size(other) == 0) |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
364 return 1; /* Edge case */ |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
365 for (i = 0; i + Py_Size(other) <= Py_Size(self); i++) { |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
366 /* XXX Yeah, yeah, lots of optimizations possible... */ |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
367 if (memcmp(self->ob_bytes + i, other->ob_bytes, Py_Size(other)) == 0) |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
368 return 1; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
369 } |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
370 return 0; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
371 } |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
372 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
373 static int |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
374 bytes_contains(PyBytesObject *self, PyObject *value) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
375 { |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
376 Py_ssize_t ival; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
377 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
378 if (PyBytes_Check(value)) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
379 return bytes_substring(self, (PyBytesObject *)value); |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
380 |
|
39631
8d71f9a6c1c3
Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents:
39610
diff
changeset
|
381 ival = PyNumber_AsSsize_t(value, PyExc_ValueError); |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
382 if (ival == -1 && PyErr_Occurred()) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
383 return -1; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
384 if (ival < 0 || ival >= 256) { |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
385 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
386 return -1; |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
387 } |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
388 |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
389 return memchr(self->ob_bytes, ival, Py_Size(self)) != NULL; |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
390 } |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
391 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
392 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
|
393 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
|
394 { |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
395 if (i < 0) |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
396 i += Py_Size(self); |
|
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
397 if (i < 0 || i >= Py_Size(self)) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
398 PyErr_SetString(PyExc_IndexError, "bytes index out of range"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
399 return NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
400 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
401 return PyInt_FromLong((unsigned char)(self->ob_bytes[i])); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
402 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
403 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
404 static PyObject * |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
405 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
|
406 { |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
407 if (PyIndex_Check(item)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
408 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
|
409 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
410 if (i == -1 && PyErr_Occurred()) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
411 return NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
412 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
413 if (i < 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
414 i += PyBytes_GET_SIZE(self); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
415 |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
416 if (i < 0 || i >= Py_Size(self)) { |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
417 PyErr_SetString(PyExc_IndexError, "bytes index out of range"); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
418 return NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
419 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
420 return PyInt_FromLong((unsigned char)(self->ob_bytes[i])); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
421 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
422 else if (PySlice_Check(item)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
423 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
|
424 if (PySlice_GetIndicesEx((PySliceObject *)item, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
425 PyBytes_GET_SIZE(self), |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
426 &start, &stop, &step, &slicelength) < 0) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
427 return NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
428 } |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
429 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
430 if (slicelength <= 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
431 return PyBytes_FromStringAndSize("", 0); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
432 else if (step == 1) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
433 return PyBytes_FromStringAndSize(self->ob_bytes + start, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
434 slicelength); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
435 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
436 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
437 char *source_buf = PyBytes_AS_STRING(self); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
438 char *result_buf = (char *)PyMem_Malloc(slicelength); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
439 PyObject *result; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
440 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
441 if (result_buf == NULL) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
442 return PyErr_NoMemory(); |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
443 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
444 for (cur = start, i = 0; i < slicelength; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
445 cur += step, i++) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
446 result_buf[i] = source_buf[cur]; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
447 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
448 result = PyBytes_FromStringAndSize(result_buf, slicelength); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
449 PyMem_Free(result_buf); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
450 return result; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
451 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
452 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
453 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
454 PyErr_SetString(PyExc_TypeError, "bytes indices must be integers"); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
455 return NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
456 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
457 } |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
458 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
459 static int |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
460 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
|
461 PyObject *values) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
462 { |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
463 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
|
464 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
|
465 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
|
466 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
|
467 |
|
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
|
468 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
|
469 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
|
470 /* 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
|
471 int err; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
472 values = PyBytes_FromObject(values); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
473 if (values == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
474 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
475 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
|
476 Py_DECREF(values); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
477 return err; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
478 } |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
479 if (values == NULL) { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
480 /* 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
|
481 bytes = NULL; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
482 needed = 0; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
483 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
484 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
|
485 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
|
486 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
|
487 "can't set bytes slice from %.100s", |
|
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
|
488 Py_Type(values)->tp_name); |
|
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
|
489 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
|
490 } |
|
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 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
|
492 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
|
493 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
494 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
495 if (lo < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
496 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
|
497 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
|
498 hi = lo; |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
499 if (hi > Py_Size(self)) |
|
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
500 hi = Py_Size(self); |
|
37960
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 avail = hi - lo; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
503 if (avail < 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
504 lo = hi = avail = 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
505 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
506 if (avail != needed) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
507 if (avail > needed) { |
|
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 0 lo hi old_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
510 | |<----avail----->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
511 | |<-needed->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
512 0 lo new_hi new_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
513 */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
514 memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi, |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
515 Py_Size(self) - hi); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
516 } |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
517 /* 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
|
518 if (PyBytes_Resize((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
|
519 Py_Size(self) + needed - avail) < 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
|
520 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
|
521 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
|
522 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
523 if (avail < needed) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
524 /* |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
525 0 lo hi old_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
526 | |<-avail->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
527 | |<----needed---->|<-----tomove------>| |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
528 0 lo new_hi new_size |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
529 */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
530 memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi, |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
531 Py_Size(self) - lo - needed); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
532 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
533 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
534 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
535 if (needed > 0) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
536 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
|
537 |
| 42779 | 538 |
|
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
|
539 finish: |
| 42779 | 540 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
|
541 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
|
542 return res; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
543 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
544 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
545 static int |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
546 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
|
547 { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
548 Py_ssize_t ival; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
549 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
550 if (i < 0) |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
551 i += Py_Size(self); |
|
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
552 |
|
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
553 if (i < 0 || i >= Py_Size(self)) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
554 PyErr_SetString(PyExc_IndexError, "bytes index out of range"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
555 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
556 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
557 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
558 if (value == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
559 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
|
560 |
|
39631
8d71f9a6c1c3
Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents:
39610
diff
changeset
|
561 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
|
562 if (ival == -1 && PyErr_Occurred()) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
563 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
564 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
565 if (ival < 0 || ival >= 256) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
566 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
|
567 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
568 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
569 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
570 self->ob_bytes[i] = ival; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
571 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
|
572 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
573 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
574 static int |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
575 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
|
576 { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
577 Py_ssize_t start, stop, step, slicelen, needed; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
578 char *bytes; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
579 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
580 if (PyIndex_Check(item)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
581 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
|
582 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
583 if (i == -1 && PyErr_Occurred()) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
584 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
585 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
586 if (i < 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
587 i += PyBytes_GET_SIZE(self); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
588 |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
589 if (i < 0 || i >= Py_Size(self)) { |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
590 PyErr_SetString(PyExc_IndexError, "bytes index out of range"); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
591 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
592 } |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
593 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
594 if (values == NULL) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
595 /* Fall through to slice assignment */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
596 start = i; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
597 stop = i + 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
598 step = 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
599 slicelen = 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
600 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
601 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
602 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
|
603 if (ival == -1 && PyErr_Occurred()) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
604 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
605 if (ival < 0 || ival >= 256) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
606 PyErr_SetString(PyExc_ValueError, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
607 "byte must be in range(0, 256)"); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
608 return -1; |
|
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 self->ob_bytes[i] = (char)ival; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
611 return 0; |
|
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 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
614 else if (PySlice_Check(item)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
615 if (PySlice_GetIndicesEx((PySliceObject *)item, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
616 PyBytes_GET_SIZE(self), |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
617 &start, &stop, &step, &slicelen) < 0) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
618 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
619 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
620 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
621 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
622 PyErr_SetString(PyExc_TypeError, "bytes indices must be integer"); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
623 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
624 } |
|
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 if (values == NULL) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
627 bytes = NULL; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
628 needed = 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
629 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
630 else if (values == (PyObject *)self || !PyBytes_Check(values)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
631 /* Make a copy an call this function recursively */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
632 int err; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
633 values = PyBytes_FromObject(values); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
634 if (values == NULL) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
635 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
636 err = bytes_ass_subscript(self, item, values); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
637 Py_DECREF(values); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
638 return err; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
639 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
640 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
641 assert(PyBytes_Check(values)); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
642 bytes = ((PyBytesObject *)values)->ob_bytes; |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
643 needed = Py_Size(values); |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
644 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
645 /* 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
|
646 if ((step < 0 && start < stop) || |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
647 (step > 0 && start > stop)) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
648 stop = start; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
649 if (step == 1) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
650 if (slicelen != needed) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
651 if (slicelen > needed) { |
|
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 0 start stop old_size |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
654 | |<---slicelen--->|<-----tomove------>| |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
655 | |<-needed->|<-----tomove------>| |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
656 0 lo new_hi new_size |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
657 */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
658 memmove(self->ob_bytes + start + needed, self->ob_bytes + stop, |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
659 Py_Size(self) - stop); |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
660 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
661 if (PyBytes_Resize((PyObject *)self, |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
662 Py_Size(self) + needed - slicelen) < 0) |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
663 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
664 if (slicelen < needed) { |
|
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 0 lo hi old_size |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
667 | |<-avail->|<-----tomove------>| |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
668 | |<----needed---->|<-----tomove------>| |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
669 0 lo new_hi new_size |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
670 */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
671 memmove(self->ob_bytes + start + needed, self->ob_bytes + stop, |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
672 Py_Size(self) - start - needed); |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
673 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
674 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
675 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
676 if (needed > 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
677 memcpy(self->ob_bytes + start, bytes, needed); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
678 |
|
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 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
682 if (needed == 0) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
683 /* Delete slice */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
684 Py_ssize_t cur, i; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
685 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
686 if (step < 0) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
687 stop = start + 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
688 start = stop + step * (slicelen - 1) - 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
689 step = -step; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
690 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
691 for (cur = start, i = 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
692 i < slicelen; cur += step, i++) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
693 Py_ssize_t lim = step - 1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
694 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
695 if (cur + step >= PyBytes_GET_SIZE(self)) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
696 lim = PyBytes_GET_SIZE(self) - cur - 1; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
697 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
698 memmove(self->ob_bytes + cur - i, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
699 self->ob_bytes + cur + 1, lim); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
700 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
701 /* 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
|
702 cur = start + slicelen*step; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
703 if (cur < PyBytes_GET_SIZE(self)) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
704 memmove(self->ob_bytes + cur - slicelen, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
705 self->ob_bytes + cur, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
706 PyBytes_GET_SIZE(self) - cur); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
707 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
708 if (PyBytes_Resize((PyObject *)self, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
709 PyBytes_GET_SIZE(self) - slicelen) < 0) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
710 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
711 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
712 return 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
713 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
714 else { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
715 /* Assign slice */ |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
716 Py_ssize_t cur, i; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
717 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
718 if (needed != slicelen) { |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
719 PyErr_Format(PyExc_ValueError, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
720 "attempt to assign bytes of size %zd " |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
721 "to extended slice of size %zd", |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
722 needed, slicelen); |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
723 return -1; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
724 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
725 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
|
726 self->ob_bytes[cur] = bytes[i]; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
727 return 0; |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
728 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
729 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
730 } |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
731 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
732 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
|
733 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
|
734 { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
735 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
|
736 PyObject *arg = NULL; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
737 const char *encoding = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
738 const char *errors = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
739 Py_ssize_t count; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
740 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
|
741 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
|
742 |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
743 if (Py_Size(self) != 0) { |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
744 /* Empty previous contents (yes, do this first of all!) */ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
745 if (PyBytes_Resize((PyObject *)self, 0) < 0) |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
746 return -1; |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
747 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
748 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
749 /* Parse arguments */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
750 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
|
751 &arg, &encoding, &errors)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
752 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
|
753 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
754 /* 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
|
755 if (arg == NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
756 if (encoding != NULL || errors != NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
757 PyErr_SetString(PyExc_TypeError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
758 "encoding or errors without sequence argument"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
759 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
760 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
761 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
|
762 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
763 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
764 if (PyUnicode_Check(arg)) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
765 /* 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
|
766 PyObject *encoded, *new; |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
767 if (encoding == NULL) { |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
768 PyErr_SetString(PyExc_TypeError, |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
769 "string argument without an encoding"); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
770 return -1; |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
771 } |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
772 encoded = PyCodec_Encode(arg, encoding, errors); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
773 if (encoded == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
774 return -1; |
|
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
|
775 if (!PyBytes_Check(encoded) && !PyString_Check(encoded)) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
776 PyErr_Format(PyExc_TypeError, |
|
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
|
777 "encoder did not return a str8 or bytes object (type=%.400s)", |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
778 Py_Type(encoded)->tp_name); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
779 Py_DECREF(encoded); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
780 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
781 } |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
782 new = bytes_iconcat(self, encoded); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
783 Py_DECREF(encoded); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
784 if (new == NULL) |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
785 return -1; |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
786 Py_DECREF(new); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
787 return 0; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
788 } |
|
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 /* 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
|
791 if (encoding != NULL || errors != NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
792 PyErr_SetString(PyExc_TypeError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
793 "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
|
794 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
795 } |
|
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
|
796 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
797 /* 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
|
798 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
|
799 if (count == -1 && PyErr_Occurred()) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
800 PyErr_Clear(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
801 else { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
802 if (count < 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
803 PyErr_SetString(PyExc_ValueError, "negative count"); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
804 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
805 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
806 if (count > 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
807 if (PyBytes_Resize((PyObject *)self, count)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
808 return -1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
809 memset(self->ob_bytes, 0, count); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
810 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
811 return 0; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
812 } |
| 42779 | 813 |
|
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
|
814 /* Use the modern buffer interface */ |
|
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
|
815 if (PyObject_CheckBuffer(arg)) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
816 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
|
817 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
|
818 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
|
819 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
|
820 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
|
821 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
|
822 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
|
823 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
|
824 PyObject_ReleaseBuffer(arg, &view); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
825 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
|
826 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
|
827 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
|
828 return -1; |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
829 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
830 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
831 /* 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
|
832 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
833 /* 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
|
834 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
|
835 if (it == NULL) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
836 return -1; |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
837 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
|
838 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
839 /* 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
|
840 for (;;) { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
841 PyObject *item; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
842 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
|
843 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
844 /* Get the next item */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
845 item = iternext(it); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
846 if (item == NULL) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
847 if (PyErr_Occurred()) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
848 if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
849 goto error; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
850 PyErr_Clear(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
851 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
852 break; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
853 } |
|
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
|
854 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
855 /* 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
|
856 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
|
857 Py_DECREF(item); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
858 if (value == -1 && PyErr_Occurred()) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
859 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
|
860 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
861 /* Range check */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
862 if (value < 0 || value >= 256) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
863 PyErr_SetString(PyExc_ValueError, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
864 "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
|
865 goto error; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
866 } |
|
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
|
867 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
868 /* Append the byte */ |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
869 if (Py_Size(self) < self->ob_alloc) |
|
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
870 Py_Size(self)++; |
|
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
871 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
|
872 goto error; |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
873 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
|
874 } |
|
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 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
876 /* 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
|
877 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
|
878 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
|
879 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
880 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
|
881 /* 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
|
882 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
|
883 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
|
884 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
885 |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
886 /* 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
|
887 "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
|
888 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
|
889 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
|
890 { |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
891 static const char *hexdigits = "0123456789abcdef"; |
| 42335 | 892 size_t newsize = 3 + 4 * Py_Size(self); |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
893 PyObject *v; |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
894 if (newsize > PY_SSIZE_T_MAX || newsize / 4 != Py_Size(self)) { |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
895 PyErr_SetString(PyExc_OverflowError, |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
896 "bytes object is too large to make repr"); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
897 return NULL; |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
898 } |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
899 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
|
900 if (v == NULL) { |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
901 return NULL; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
902 } |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
903 else { |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
904 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
|
905 register Py_UNICODE c; |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
906 register Py_UNICODE *p; |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
907 int quote = '\''; |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
908 |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
909 p = PyUnicode_AS_UNICODE(v); |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
910 *p++ = 'b'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
911 *p++ = quote; |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
912 for (i = 0; i < Py_Size(self); i++) { |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
913 /* 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
|
914 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
|
915 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
|
916 c = self->ob_bytes[i]; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
917 if (c == quote || c == '\\') |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
918 *p++ = '\\', *p++ = c; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
919 else if (c == '\t') |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
920 *p++ = '\\', *p++ = 't'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
921 else if (c == '\n') |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
922 *p++ = '\\', *p++ = 'n'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
923 else if (c == '\r') |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
924 *p++ = '\\', *p++ = 'r'; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
925 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
|
926 *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
|
927 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
|
928 *p++ = '\\'; |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
929 *p++ = 'x'; |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
930 *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
|
931 *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
|
932 } |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
933 else |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
934 *p++ = c; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
935 } |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
936 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
|
937 *p++ = quote; |
|
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
938 *p = '\0'; |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
939 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
|
940 Py_DECREF(v); |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
941 return NULL; |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41668
diff
changeset
|
942 } |
|
40898
b58a4374ac21
Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents:
40472
diff
changeset
|
943 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
|
944 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
945 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
946 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
947 static PyObject * |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
948 bytes_str(PyBytesObject *self) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
949 { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
950 return PyString_FromStringAndSize(self->ob_bytes, Py_Size(self)); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
951 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
952 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
953 static PyObject * |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
954 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
|
955 { |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
956 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
|
957 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
|
958 PyObject *res; |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
959 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
|
960 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
|
961 |
|
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
|
962 /* 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
|
963 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
|
964 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
|
965 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
|
966 PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) { |
|
43508
47c02791ec6b
Patch #1049 by Thomas Lee.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
967 Py_INCREF(Py_NotImplemented); |
|
47c02791ec6b
Patch #1049 by Thomas Lee.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
968 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
|
969 } |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
970 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
971 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
|
972 if (self_size < 0) { |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
973 PyErr_Clear(); |
|
41412
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
974 Py_INCREF(Py_NotImplemented); |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
975 return Py_NotImplemented; |
|
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
976 } |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
977 |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
978 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
|
979 if (other_size < 0) { |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
980 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
|
981 PyObject_ReleaseBuffer(self, &self_bytes); |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
982 Py_INCREF(Py_NotImplemented); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
983 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
|
984 } |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
985 |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
986 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
|
987 /* 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
|
988 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
|
989 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
990 else { |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
991 minsize = self_size; |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
992 if (other_size < minsize) |
|
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
993 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
|
994 |
|
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
|
995 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
|
996 /* 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
|
997 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
998 if (cmp == 0) { |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
999 if (self_size < other_size) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1000 cmp = -1; |
|
41411
339015d98b78
For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents:
40982
diff
changeset
|
1001 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
|
1002 cmp = 1; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1003 } |
|
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
|
1004 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1005 switch (op) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1006 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
|
1007 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
|
1008 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
|
1009 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
|
1010 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
|
1011 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
|
1012 } |
|
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
|
1013 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1014 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1015 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
|
1016 PyObject_ReleaseBuffer(self, &self_bytes); |
| 42779 | 1017 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
|
1018 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
|
1019 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
|
1020 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1021 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1022 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
|
1023 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
|
1024 { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1025 if (self->ob_bytes != 0) { |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
1026 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
|
1027 } |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
1028 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
|
1029 } |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1030 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1031 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1032 /* -------------------------------------------------------------------- */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1033 /* Methods */ |
|
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 #define STRINGLIB_CHAR char |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1036 #define STRINGLIB_CMP memcmp |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1037 #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
|
1038 #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
|
1039 #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
|
1040 #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
|
1041 #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
|
1042 #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
|
1043 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1044 #include "stringlib/fastsearch.h" |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1045 #include "stringlib/count.h" |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1046 #include "stringlib/find.h" |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1047 #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
|
1048 #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
|
1049 #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
|
1050 |
|
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 /* 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
|
1053 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
|
1054 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1055 Py_LOCAL_INLINE(void) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1056 _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
|
1057 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1058 if (*end > len) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1059 *end = len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1060 else if (*end < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1061 *end += len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1062 if (*end < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1063 *end = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1064 if (*start < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1065 *start += len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1066 if (*start < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1067 *start = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1068 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1069 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1070 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1071 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
|
1072 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
|
1073 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1074 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
|
1075 Py_buffer subbuf; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1076 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
|
1077 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
|
1078 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1079 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
|
1080 _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
|
1081 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
|
1082 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
|
1083 return -2; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1084 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
|
1085 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
|
1086 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
|
1087 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
|
1088 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
|
1089 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
|
1090 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
|
1091 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
|
1092 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
|
1093 return res; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1094 } |
|
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 PyDoc_STRVAR(find__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1097 "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
|
1098 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1099 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
|
1100 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
|
1101 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
|
1102 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1103 Return -1 on failure."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1104 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1105 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1106 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
|
1107 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1108 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
|
1109 if (result == -2) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1110 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1111 return PyInt_FromSsize_t(result); |
|
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 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1114 PyDoc_STRVAR(count__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1115 "B.count(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
|
1116 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1117 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
|
1118 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
|
1119 as in slice notation."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1120 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1121 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1122 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
|
1123 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1124 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
|
1125 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
|
1126 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
|
1127 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
|
1128 PyObject *count_obj; |
|
40978
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 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
|
1131 _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
|
1132 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1133 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1134 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
|
1135 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1136 |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41769
diff
changeset
|
1137 _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
|
1138 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1139 count_obj = PyInt_FromSsize_t( |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1140 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
|
1141 ); |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1142 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
|
1143 return count_obj; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1144 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1145 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1146 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1147 PyDoc_STRVAR(index__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1148 "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
|
1149 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1150 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
|
1151 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1152 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1153 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
|
1154 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1155 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
|
1156 if (result == -2) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1157 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1158 if (result == -1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1159 PyErr_SetString(PyExc_ValueError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1160 "subsection not found"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1161 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1162 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1163 return PyInt_FromSsize_t(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1164 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1165 |
|
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 PyDoc_STRVAR(rfind__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1168 "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
|
1169 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1170 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
|
1171 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
|
1172 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
|
1173 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1174 Return -1 on failure."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1175 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1176 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1177 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
|
1178 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1179 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
|
1180 if (result == -2) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1181 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1182 return PyInt_FromSsize_t(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1183 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1184 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1185 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1186 PyDoc_STRVAR(rindex__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1187 "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
|
1188 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1189 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
|
1190 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1191 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1192 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
|
1193 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1194 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
|
1195 if (result == -2) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1196 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1197 if (result == -1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1198 PyErr_SetString(PyExc_ValueError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1199 "subsection not found"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1200 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1201 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1202 return PyInt_FromSsize_t(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1203 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1204 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1205 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1206 /* 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
|
1207 * 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
|
1208 * -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
|
1209 */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1210 Py_LOCAL(int) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1211 _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
|
1212 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
|
1213 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1214 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
|
1215 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
|
1216 Py_buffer vsubstr; |
|
43632
0067a6ab4854
Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents:
43576
diff
changeset
|
1217 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
|
1218 |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1219 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
|
1220 |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1221 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
|
1222 return -1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1223 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1224 _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
|
1225 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1226 if (direction < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1227 /* 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
|
1228 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
|
1229 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
|
1230 } |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1231 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1232 /* 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
|
1233 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
|
1234 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
|
1235 } |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1236 |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1237 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
|
1238 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
|
1239 } |
|
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) |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1241 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
|
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 done: |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1244 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
|
1245 return rv; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1246 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1247 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1248 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1249 PyDoc_STRVAR(startswith__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1250 "B.startswith(prefix[, start[, end]]) -> bool\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1251 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1252 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
|
1253 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
|
1254 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
|
1255 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
|
1256 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1257 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1258 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
|
1259 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1260 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
|
1261 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
|
1262 PyObject *subobj; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1263 int result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1264 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1265 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
|
1266 _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
|
1267 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1268 if (PyTuple_Check(subobj)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1269 Py_ssize_t i; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1270 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
|
1271 result = _bytes_tailmatch(self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1272 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
|
1273 start, end, -1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1274 if (result == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1275 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1276 else if (result) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1277 Py_RETURN_TRUE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1278 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1279 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1280 Py_RETURN_FALSE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1281 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1282 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
|
1283 if (result == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1284 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1285 else |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1286 return PyBool_FromLong(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1287 } |
|
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 PyDoc_STRVAR(endswith__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1290 "B.endswith(suffix[, start[, end]]) -> bool\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1291 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1292 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
|
1293 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
|
1294 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
|
1295 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
|
1296 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1297 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1298 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
|
1299 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1300 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
|
1301 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
|
1302 PyObject *subobj; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1303 int result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1304 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1305 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
|
1306 _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
|
1307 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1308 if (PyTuple_Check(subobj)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1309 Py_ssize_t i; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1310 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
|
1311 result = _bytes_tailmatch(self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1312 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
|
1313 start, end, +1); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1314 if (result == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1315 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1316 else if (result) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1317 Py_RETURN_TRUE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1318 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1319 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1320 Py_RETURN_FALSE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1321 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1322 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
|
1323 if (result == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1324 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1325 else |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1326 return PyBool_FromLong(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1327 } |
|
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 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1330 PyDoc_STRVAR(translate__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1331 "B.translate(table [,deletechars]) -> bytes\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1332 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1333 Return a copy of the bytes B, where all characters occurring\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1334 in the optional argument deletechars are removed, and the\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1335 remaining characters have been mapped through the given\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1336 translation table, which must be a bytes of length 256."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1337 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1338 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1339 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
|
1340 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1341 register char *input, *output; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1342 register const char *table; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1343 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
|
1344 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
|
1345 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
|
1346 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
|
1347 PyObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1348 int trans_table[256]; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1349 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
|
1350 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
|
1351 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1352 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
|
1353 &tableobj, &delobj)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1354 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1355 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1356 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
|
1357 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1358 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1359 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
|
1360 PyErr_SetString(PyExc_ValueError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1361 "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
|
1362 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
|
1363 goto done; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1364 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1365 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1366 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
|
1367 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
|
1368 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
|
1369 goto done; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1370 } |
|
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 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
|
1373 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
|
1374 vdel.len = 0; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1375 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1376 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1377 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
|
1378 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
|
1379 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
|
1380 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
|
1381 goto done; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1382 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
|
1383 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
|
1384 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1385 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
|
1386 /* 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
|
1387 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
|
1388 c = Py_CHARMASK(*input++); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1389 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
|
1390 changed = 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1391 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1392 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
|
1393 goto done; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1394 Py_DECREF(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1395 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
|
1396 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
|
1397 goto done; |
|
40978
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 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1400 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
|
1401 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
|
1402 |
|
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 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
|
1404 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
|
1405 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1406 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
|
1407 c = Py_CHARMASK(*input++); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1408 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
|
1409 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
|
1410 continue; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1411 changed = 1; |
|
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 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
|
1414 Py_DECREF(result); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1415 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
|
1416 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
|
1417 goto done; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1418 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1419 /* 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
|
1420 if (inlen > 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1421 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
|
1422 |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1423 done: |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
1424 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
|
1425 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
|
1426 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
|
1427 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1428 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1429 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1430 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1431 #define FORWARD 1 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1432 #define REVERSE -1 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1433 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1434 /* 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
|
1435 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1436 #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
|
1437 ((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
|
1438 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1439 /* 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
|
1440 #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
|
1441 (target[offset] == pattern[0] && \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1442 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
|
1443 !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
|
1444 |
|
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 /* 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
|
1447 /* 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
|
1448 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1449 return_self(PyBytesObject *self) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1450 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1451 if (PyBytes_CheckExact(self)) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1452 Py_INCREF(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1453 return (PyBytesObject *)self; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1454 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1455 return (PyBytesObject *)PyBytes_FromStringAndSize( |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1456 PyBytes_AS_STRING(self), |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1457 PyBytes_GET_SIZE(self)); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1458 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1459 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1460 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
|
1461 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
|
1462 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1463 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
|
1464 const char *start=target; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1465 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
|
1466 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1467 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
|
1468 count++; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1469 if (count >= maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1470 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1471 start += 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1472 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1473 return count; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1474 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1475 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1476 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
|
1477 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
|
1478 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
|
1479 Py_ssize_t start, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1480 Py_ssize_t end, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1481 int direction) |
|
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 if (start < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1484 start += target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1485 if (start < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1486 start = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1487 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1488 if (end > target_len) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1489 end = target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1490 } else if (end < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1491 end += target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1492 if (end < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1493 end = 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 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1496 /* 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
|
1497 if (pattern_len == 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1498 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
|
1499 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1500 end -= pattern_len; |
|
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 if (direction < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1503 for (; end >= start; end--) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1504 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
|
1505 return end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1506 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1507 for (; start <= end; start++) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1508 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
|
1509 return start; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1510 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1511 return -1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1512 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1513 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1514 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
|
1515 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
|
1516 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
|
1517 Py_ssize_t start, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1518 Py_ssize_t end, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1519 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
|
1520 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1521 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
|
1522 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1523 if (start < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1524 start += target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1525 if (start < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1526 start = 0; |
|
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 if (end > target_len) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1529 end = target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1530 } else if (end < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1531 end += target_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1532 if (end < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1533 end = 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 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1536 /* 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
|
1537 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
|
1538 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
|
1539 return target_len+1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1540 return maxcount; |
|
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 end -= pattern_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1544 if (direction < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1545 for (; (end >= start); end--) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1546 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
|
1547 count++; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1548 if (--maxcount <= 0) break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1549 end -= pattern_len-1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1550 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1551 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1552 for (; (start <= end); start++) |
|
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, start, 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) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1556 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1557 start += pattern_len-1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1558 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1559 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1560 return count; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1561 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1562 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1563 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1564 /* 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
|
1565 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1566 /* 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
|
1567 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1568 replace_interleave(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1569 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
|
1570 Py_ssize_t maxcount) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1571 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1572 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
|
1573 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
|
1574 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
|
1575 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1576 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1577 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
|
1578 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1579 /* 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
|
1580 count = self_len+1; |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
1581 if (maxcount < count) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1582 count = maxcount; |
|
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 /* Check for overflow */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1585 /* 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
|
1586 product = count * to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1587 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
|
1588 PyErr_SetString(PyExc_OverflowError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1589 "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
|
1590 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1591 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1592 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
|
1593 if (result_len < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1594 PyErr_SetString(PyExc_OverflowError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1595 "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
|
1596 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1597 } |
|
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 if (! (result = (PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1600 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
|
1601 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1602 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1603 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
|
1604 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
|
1605 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1606 /* 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
|
1607 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1608 /* 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
|
1609 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
|
1610 result_s += to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1611 count -= 1; |
|
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 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
|
1614 *result_s++ = *self_s++; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1615 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
|
1616 result_s += to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1617 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1618 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1619 /* 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
|
1620 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
|
1621 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1622 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1623 } |
|
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 /* 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
|
1626 /* 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
|
1627 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1628 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
|
1629 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
|
1630 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1631 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
|
1632 char *start, *next, *end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1633 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
|
1634 Py_ssize_t count; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1635 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1636 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1637 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
|
1638 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
|
1639 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1640 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
|
1641 if (count == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1642 return return_self(self); |
|
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 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1645 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
|
1646 assert(result_len>=0); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1647 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1648 if ( (result = (PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1649 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
|
1650 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1651 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
|
1652 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1653 start = self_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1654 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
|
1655 while (count-- > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1656 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
|
1657 if (next == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1658 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1659 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
|
1660 result_s += (next-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1661 start = next+1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1662 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1663 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
|
1664 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1665 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1666 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1667 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1668 /* 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
|
1669 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1670 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1671 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
|
1672 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
|
1673 Py_ssize_t maxcount) |
|
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 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
|
1676 char *start, *next, *end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1677 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
|
1678 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
|
1679 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1680 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1681 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
|
1682 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
|
1683 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1684 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
|
1685 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1686 0, self_len, 1, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1687 maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1688 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1689 if (count == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1690 /* no matches */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1691 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1692 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1693 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1694 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
|
1695 assert (result_len>=0); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1696 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1697 if ( (result = (PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1698 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
|
1699 return NULL; |
|
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_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
|
1702 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1703 start = self_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1704 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
|
1705 while (count-- > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1706 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
|
1707 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1708 0, end-start, FORWARD); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1709 if (offset == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1710 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1711 next = start + offset; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1712 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1713 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
|
1714 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1715 result_s += (next-start); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1716 start = next+from_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1717 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1718 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
|
1719 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1720 } |
|
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 /* 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
|
1723 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1724 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
|
1725 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
|
1726 Py_ssize_t maxcount) |
|
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 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
|
1729 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
|
1730 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1731 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1732 /* 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
|
1733 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
|
1734 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
|
1735 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1736 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
|
1737 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1738 if (next == NULL) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1739 /* 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
|
1740 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1741 } |
|
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 /* 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
|
1744 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
|
1745 if (result == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1746 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1747 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
|
1748 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
|
1749 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1750 /* 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
|
1751 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
|
1752 *start = to_c; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1753 start++; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1754 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
|
1755 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1756 while (--maxcount > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1757 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
|
1758 if (next == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1759 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1760 *next = to_c; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1761 start = next+1; |
|
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 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1764 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1765 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1766 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1767 /* 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
|
1768 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1769 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
|
1770 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
|
1771 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
|
1772 Py_ssize_t maxcount) |
|
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 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
|
1775 char *self_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1776 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
|
1777 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1778 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1779 /* 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
|
1780 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1781 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
|
1782 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
|
1783 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1784 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
|
1785 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1786 0, self_len, FORWARD); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1787 if (offset == -1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1788 /* 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
|
1789 return return_self(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 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1792 /* 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
|
1793 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
|
1794 if (result == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1795 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1796 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
|
1797 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
|
1798 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1799 /* 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
|
1800 start = result_s + offset; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1801 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
|
1802 start += from_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1803 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
|
1804 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1805 while ( --maxcount > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1806 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
|
1807 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1808 0, end-start, FORWARD); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1809 if (offset==-1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1810 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1811 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
|
1812 start += offset+from_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1813 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1814 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1815 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1816 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1817 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1818 /* 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
|
1819 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1820 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
|
1821 char from_c, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1822 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
|
1823 Py_ssize_t maxcount) |
|
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 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
|
1826 char *start, *next, *end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1827 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
|
1828 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
|
1829 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1830 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1831 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
|
1832 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
|
1833 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1834 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
|
1835 if (count == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1836 /* no matches, return unchanged */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1837 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1838 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1839 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1840 /* 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
|
1841 /* 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
|
1842 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
|
1843 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
|
1844 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
|
1845 return NULL; |
|
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 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
|
1848 if (result_len < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1849 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
|
1850 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1851 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1852 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1853 if ( (result = (PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1854 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
|
1855 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1856 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
|
1857 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1858 start = self_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1859 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
|
1860 while (count-- > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1861 next = findchar(start, end-start, from_c); |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
1862 if (next == NULL) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1863 break; |
|
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 if (next == start) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1866 /* replace with the 'to' */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1867 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
|
1868 result_s += to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1869 start += 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1870 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1871 /* 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
|
1872 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
|
1873 result_s += (next-start); |
|
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 = next+1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1877 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1878 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1879 /* 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
|
1880 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
|
1881 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1882 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1883 } |
|
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 /* 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
|
1886 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1887 replace_substring(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1888 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
|
1889 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
|
1890 Py_ssize_t maxcount) |
|
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 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
|
1893 char *start, *next, *end; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1894 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
|
1895 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
|
1896 PyBytesObject *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1897 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1898 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
|
1899 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
|
1900 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1901 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
|
1902 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1903 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
|
1904 if (count == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1905 /* no matches, return unchanged */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1906 return return_self(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 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1909 /* Check for overflow */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1910 /* 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
|
1911 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
|
1912 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
|
1913 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
|
1914 return NULL; |
|
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 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
|
1917 if (result_len < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1918 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
|
1919 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1920 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1921 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1922 if ( (result = (PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1923 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
|
1924 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1925 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
|
1926 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1927 start = self_s; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1928 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
|
1929 while (count-- > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1930 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
|
1931 from_s, from_len, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1932 0, end-start, FORWARD); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1933 if (offset == -1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1934 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1935 next = start+offset; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1936 if (next == start) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1937 /* replace with the 'to' */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1938 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
|
1939 result_s += to_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1940 start += from_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1941 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1942 /* 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
|
1943 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
|
1944 result_s += (next-start); |
|
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 = next+from_len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1948 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1949 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1950 /* 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
|
1951 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
|
1952 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1953 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1954 } |
|
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 Py_LOCAL(PyBytesObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1958 replace(PyBytesObject *self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1959 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
|
1960 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
|
1961 Py_ssize_t maxcount) |
|
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 if (maxcount < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1964 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
|
1965 } 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
|
1966 /* 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
|
1967 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1968 } |
|
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 (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
|
1972 /* 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
|
1973 return return_self(self); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1974 } |
|
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 /* 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
|
1977 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1978 if (from_len == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1979 /* 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
|
1980 /* >>> "Python".replace("", ".") */ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1981 /* '.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
|
1982 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
|
1983 } |
|
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 /* 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
|
1986 /* 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
|
1987 /* 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
|
1988 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
|
1989 return return_self(self); |
|
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 if (to_len == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1993 /* 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
|
1994 if (from_len == 1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1995 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
|
1996 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
|
1997 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
1998 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
|
1999 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2000 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2001 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2002 /* 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
|
2003 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2004 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
|
2005 if (from_len == 1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2006 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
|
2007 self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2008 from_s[0], |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2009 to_s[0], |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2010 maxcount); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2011 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2012 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
|
2013 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
|
2014 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2015 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2016 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2017 /* 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
|
2018 if (from_len == 1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2019 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
|
2020 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 } else { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2022 /* 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
|
2023 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
|
2024 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2025 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2026 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
2027 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2028 PyDoc_STRVAR(replace__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2029 "B.replace (old, new[, count]) -> bytes\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2030 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2031 Return a copy of bytes B with all occurrences of subsection\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2032 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
|
2033 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
|
2034 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2035 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2036 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
|
2037 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2038 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
|
2039 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
|
2040 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
|
2041 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2042 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
|
2043 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2044 |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2045 if (_getbuffer(from, &vfrom) < 0) |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2046 return NULL; |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2047 if (_getbuffer(to, &vto) < 0) { |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2048 PyObject_ReleaseBuffer(from, &vfrom); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2049 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
|
2050 } |
|
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
|
2051 |
|
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
|
2052 res = (PyObject *)replace((PyBytesObject *) self, |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2053 vfrom.buf, vfrom.len, |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2054 vto.buf, vto.len, count); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2055 |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2056 PyObject_ReleaseBuffer(from, &vfrom); |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2057 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
|
2058 return res; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2059 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2060 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2061 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2062 /* 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
|
2063 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
|
2064 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
|
2065 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
|
2066 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
|
2067 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
|
2068 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2069 #define MAX_PREALLOC 12 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2070 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2071 /* 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
|
2072 #define PREALLOC_SIZE(maxsplit) \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2073 (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
|
2074 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2075 #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
|
2076 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
|
2077 (right) - (left)); \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2078 if (str == NULL) \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2079 goto onError; \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2080 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
|
2081 Py_DECREF(str); \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2082 goto onError; \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2083 } \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2084 else \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2085 Py_DECREF(str); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2086 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2087 #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
|
2088 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
|
2089 (right) - (left)); \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2090 if (str == NULL) \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2091 goto onError; \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2092 if (count < MAX_PREALLOC) { \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2093 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
|
2094 } else { \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2095 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
|
2096 Py_DECREF(str); \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2097 goto onError; \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2098 } \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2099 else \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2100 Py_DECREF(str); \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2101 } \ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2102 count++; } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2103 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2104 /* Always force the list to the expected size. */ |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
2105 #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
|
2106 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2107 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2108 Py_LOCAL_INLINE(PyObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2109 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
|
2110 { |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2111 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
|
2112 PyObject *str; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2113 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
|
2114 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2115 if (list == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2116 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2117 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2118 i = j = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2119 while ((j < len) && (maxcount-- > 0)) { |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2120 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
|
2121 /* 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
|
2122 if (s[j] == ch) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2123 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
|
2124 i = j = j + 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2125 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2126 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2127 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2128 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2129 if (i <= len) { |
|
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, len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2131 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2132 FIX_PREALLOC_SIZE(list); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2133 return list; |
|
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 onError: |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2136 Py_DECREF(list); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2137 return NULL; |
|
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 |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2140 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2141 Py_LOCAL_INLINE(PyObject *) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2142 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
|
2143 { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2144 register Py_ssize_t i, j, count = 0; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2145 PyObject *str; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2146 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
|
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 if (list == NULL) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2149 return NULL; |
|
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 for (i = j = 0; i < len; ) { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2152 /* find a token */ |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2153 while (i < len && ISSPACE(s[i])) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2154 i++; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2155 j = i; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2156 while (i < len && !ISSPACE(s[i])) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2157 i++; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2158 if (j < i) { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2159 if (maxcount-- <= 0) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2160 break; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2161 SPLIT_ADD(s, j, i); |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2162 while (i < len && ISSPACE(s[i])) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2163 i++; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2164 j = i; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2165 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2166 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2167 if (j < len) { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2168 SPLIT_ADD(s, j, len); |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2169 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2170 FIX_PREALLOC_SIZE(list); |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2171 return list; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2172 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2173 onError: |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2174 Py_DECREF(list); |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2175 return NULL; |
|
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 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2178 PyDoc_STRVAR(split__doc__, |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2179 "B.split([sep [, maxsplit]]) -> list of bytes\n\ |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2180 \n\ |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2181 Return a list of the bytes in the string B, using sep as the delimiter.\n\ |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2182 If sep is not given, B is split on ASCII whitespace charcters\n\ |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2183 (space, tab, return, newline, formfeed, vertical tab).\n\ |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2184 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
|
2185 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2186 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2187 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
|
2188 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2189 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
|
2190 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
|
2191 const char *s = PyBytes_AS_STRING(self), *sub; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2192 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
|
2193 Py_buffer vsub; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2194 #ifdef USE_FAST |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2195 Py_ssize_t pos; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2196 #endif |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2197 |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2198 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
|
2199 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2200 if (maxsplit < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2201 maxsplit = PY_SSIZE_T_MAX; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2202 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2203 if (subobj == Py_None) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2204 return split_whitespace(s, len, maxsplit); |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2205 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2206 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
|
2207 return NULL; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2208 sub = vsub.buf; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2209 n = vsub.len; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2210 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2211 if (n == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2212 PyErr_SetString(PyExc_ValueError, "empty separator"); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2213 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
|
2214 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2215 } |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2216 if (n == 1) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2217 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
|
2218 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2219 list = PyList_New(PREALLOC_SIZE(maxsplit)); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2220 if (list == NULL) { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2221 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
|
2222 return NULL; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2223 } |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2224 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2225 #ifdef USE_FAST |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2226 i = j = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2227 while (maxsplit-- > 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2228 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
|
2229 if (pos < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2230 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2231 j = i+pos; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2232 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
|
2233 i = j + n; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2234 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2235 #else |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2236 i = j = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2237 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
|
2238 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
|
2239 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
|
2240 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
|
2241 i = j = j + n; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2242 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2243 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2244 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2245 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2246 #endif |
|
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, len); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2248 FIX_PREALLOC_SIZE(list); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2249 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
|
2250 return list; |
|
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 onError: |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2253 Py_DECREF(list); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2254 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
|
2255 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2256 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2257 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2258 PyDoc_STRVAR(partition__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2259 "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
|
2260 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2261 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
|
2262 the separator itself, and the part after it. If the separator is not\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2263 found, returns B and two empty bytes."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2264 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2265 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2266 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
|
2267 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2268 PyObject *bytesep, *result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2269 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2270 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
|
2271 if (! bytesep) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2272 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2273 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2274 result = stringlib_partition( |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2275 (PyObject*) self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2276 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
2277 bytesep, |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2278 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
|
2279 ); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2280 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2281 Py_DECREF(bytesep); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2282 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2283 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2284 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2285 PyDoc_STRVAR(rpartition__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2286 "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
|
2287 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2288 Searches for the separator sep in B, starting at the end of B, and returns\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2289 the part before it, the separator itself, and the part after it. If the\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2290 separator is not found, returns two empty bytes and B."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2291 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2292 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2293 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
|
2294 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2295 PyObject *bytesep, *result; |
|
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 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
|
2298 if (! bytesep) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2299 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2300 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2301 result = stringlib_rpartition( |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2302 (PyObject*) self, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2303 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
2304 bytesep, |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2305 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
|
2306 ); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2307 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2308 Py_DECREF(bytesep); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2309 return result; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2310 } |
|
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 Py_LOCAL_INLINE(PyObject *) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2313 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
|
2314 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2315 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
|
2316 PyObject *str; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2317 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
|
2318 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2319 if (list == NULL) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2320 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2321 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2322 i = j = len - 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2323 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
|
2324 for (; i >= 0; i--) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2325 if (s[i] == ch) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2326 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
|
2327 j = i = i - 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2328 break; |
|
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 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2331 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2332 if (j >= -1) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2333 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
|
2334 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2335 FIX_PREALLOC_SIZE(list); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2336 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
|
2337 goto onError; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2338 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2339 return list; |
|
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 onError: |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2342 Py_DECREF(list); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2343 return NULL; |
|
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 |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2346 Py_LOCAL_INLINE(PyObject *) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2347 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
|
2348 { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2349 register Py_ssize_t i, j, count = 0; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2350 PyObject *str; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2351 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2352 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2353 if (list == NULL) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2354 return NULL; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2355 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2356 for (i = j = len - 1; i >= 0; ) { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2357 /* find a token */ |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2358 while (i >= 0 && Py_UNICODE_ISSPACE(s[i])) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2359 i--; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2360 j = i; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2361 while (i >= 0 && !Py_UNICODE_ISSPACE(s[i])) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2362 i--; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2363 if (j > i) { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2364 if (maxcount-- <= 0) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2365 break; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2366 SPLIT_ADD(s, i + 1, j + 1); |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2367 while (i >= 0 && Py_UNICODE_ISSPACE(s[i])) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2368 i--; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2369 j = i; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2370 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2371 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2372 if (j >= 0) { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2373 SPLIT_ADD(s, 0, j + 1); |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2374 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2375 FIX_PREALLOC_SIZE(list); |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2376 if (PyList_Reverse(list) < 0) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2377 goto onError; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2378 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2379 return list; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2380 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2381 onError: |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2382 Py_DECREF(list); |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2383 return NULL; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2384 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2385 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2386 PyDoc_STRVAR(rsplit__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2387 "B.rsplit(sep [,maxsplit]) -> list of bytes\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2388 \n\ |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2389 Return a list of the sections in the byte B, using sep as the delimiter,\n\ |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2390 starting at the end of the bytes and working to the front.\n\ |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2391 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
|
2392 (space, tab, return, newline, formfeed, vertical tab).\n\ |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2393 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
|
2394 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2395 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2396 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
|
2397 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2398 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
|
2399 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
|
2400 const char *s = PyBytes_AS_STRING(self), *sub; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2401 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
|
2402 Py_buffer vsub; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2403 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2404 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
|
2405 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2406 if (maxsplit < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2407 maxsplit = PY_SSIZE_T_MAX; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2408 |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2409 if (subobj == Py_None) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2410 return rsplit_whitespace(s, len, maxsplit); |
|
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 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
|
2413 return NULL; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2414 sub = vsub.buf; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2415 n = vsub.len; |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2416 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2417 if (n == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2418 PyErr_SetString(PyExc_ValueError, "empty separator"); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2419 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
|
2420 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2421 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2422 else if (n == 1) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2423 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
|
2424 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2425 list = PyList_New(PREALLOC_SIZE(maxsplit)); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2426 if (list == NULL) { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2427 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
|
2428 return NULL; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2429 } |
|
40978
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 j = len; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2432 i = j - n; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2433 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2434 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
|
2435 for (; i>=0; i--) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2436 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
|
2437 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
|
2438 j = i; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2439 i -= n; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2440 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2441 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2442 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2443 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2444 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
|
2445 FIX_PREALLOC_SIZE(list); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2446 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
|
2447 goto onError; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2448 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
|
2449 return list; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2450 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2451 onError: |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2452 Py_DECREF(list); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2453 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
|
2454 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2455 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2456 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2457 PyDoc_STRVAR(extend__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2458 "B.extend(iterable int) -> None\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2459 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2460 Append all the elements from the iterator or sequence to the\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2461 end of the bytes."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2462 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2463 bytes_extend(PyBytesObject *self, PyObject *arg) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2464 { |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
2465 /* XXX(gps): The docstring says any iterable int will do but the |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
2466 * bytes_setslice code only accepts something supporting PEP 3118. |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
2467 * A list or tuple of 0 <= int <= 255 is supposed to work. */ |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
2468 /* bug being tracked on: http://bugs.python.org/issue1283 */ |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
2469 if (bytes_setslice(self, Py_Size(self), Py_Size(self), arg) == -1) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2470 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2471 Py_RETURN_NONE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2472 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2473 |
|
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 PyDoc_STRVAR(reverse__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2476 "B.reverse() -> None\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2477 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2478 Reverse the order of the values in bytes in place."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2479 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2480 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
|
2481 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2482 char swap, *head, *tail; |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
2483 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
|
2484 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2485 j = n / 2; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2486 head = self->ob_bytes; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2487 tail = head + n - 1; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2488 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
|
2489 swap = *head; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2490 *head++ = *tail; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2491 *tail-- = swap; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2492 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2493 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2494 Py_RETURN_NONE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2495 } |
|
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 PyDoc_STRVAR(insert__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2498 "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
|
2499 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2500 Insert a single item into the bytes before the given index."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2501 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2502 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
|
2503 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2504 int value; |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
2505 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
|
2506 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2507 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
|
2508 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2509 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2510 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
|
2511 PyErr_SetString(PyExc_OverflowError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2512 "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
|
2513 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2514 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2515 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
|
2516 PyErr_SetString(PyExc_ValueError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2517 "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
|
2518 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2519 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2520 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
|
2521 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2522 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2523 if (where < 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2524 where += n; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2525 if (where < 0) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2526 where = 0; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2527 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2528 if (where > n) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2529 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
|
2530 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
|
2531 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
|
2532 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2533 Py_RETURN_NONE; |
|
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 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2536 PyDoc_STRVAR(append__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2537 "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
|
2538 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2539 Append a single item to the end of the bytes."); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2540 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2541 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
|
2542 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2543 int value; |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
2544 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
|
2545 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2546 if (! _getbytevalue(arg, &value)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2547 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2548 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
|
2549 PyErr_SetString(PyExc_OverflowError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2550 "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
|
2551 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2552 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2553 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
|
2554 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2555 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2556 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
|
2557 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2558 Py_RETURN_NONE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2559 } |
|
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 PyDoc_STRVAR(pop__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2562 "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
|
2563 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2564 Remove and return a single item from the bytes. If no index\n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2565 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
|
2566 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2567 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
|
2568 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2569 int value; |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
2570 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
|
2571 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2572 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
|
2573 return NULL; |
|
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 if (n == 0) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2576 PyErr_SetString(PyExc_OverflowError, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2577 "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
|
2578 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2579 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2580 if (where < 0) |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
2581 where += Py_Size(self); |
|
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
2582 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
|
2583 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
|
2584 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2585 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2586 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2587 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
|
2588 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
|
2589 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
|
2590 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2591 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2592 return PyInt_FromLong(value); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2593 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2594 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2595 PyDoc_STRVAR(remove__doc__, |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2596 "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
|
2597 \n\ |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2598 Remove the first occurance of a value in bytes"); |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2599 static PyObject * |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2600 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
|
2601 { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2602 int value; |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
2603 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
|
2604 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2605 if (! _getbytevalue(arg, &value)) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2606 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2607 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2608 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
|
2609 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
|
2610 break; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2611 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2612 if (where == n) { |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2613 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
|
2614 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2615 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2616 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2617 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
|
2618 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
|
2619 return NULL; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2620 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2621 Py_RETURN_NONE; |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2622 } |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2623 |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2624 /* 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
|
2625 |
|
42893
2c02ba72714b
Make some internal functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
42779
diff
changeset
|
2626 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
|
2627 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
|
2628 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
|
2629 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2630 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
|
2631 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
|
2632 i++; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2633 return i; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2634 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2635 |
|
42893
2c02ba72714b
Make some internal functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
42779
diff
changeset
|
2636 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
|
2637 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
|
2638 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
|
2639 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2640 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
|
2641 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
|
2642 i--; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2643 return i + 1; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2644 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2645 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2646 PyDoc_STRVAR(strip__doc__, |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2647 "B.strip([bytes]) -> bytes\n\ |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2648 \n\ |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2649 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
|
2650 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
|
2651 static PyObject * |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2652 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
|
2653 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2654 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
|
2655 void *myptr, *argptr; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2656 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
|
2657 Py_buffer varg; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2658 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
|
2659 return NULL; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2660 if (arg == Py_None) { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2661 argptr = "\t\n\r\f\v "; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2662 argsize = 6; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2663 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2664 else { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2665 if (_getbuffer(arg, &varg) < 0) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2666 return NULL; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2667 argptr = varg.buf; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2668 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
|
2669 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2670 myptr = self->ob_bytes; |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
2671 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
|
2672 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
|
2673 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
|
2674 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
|
2675 else |
|
96098abc8f8f
Fix core dump in an endcase of b.strip() that I missed.
Guido van Rossum <guido@python.org>
parents:
42457
diff
changeset
|
2676 right = rstrip_helper(myptr, mysize, argptr, argsize); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2677 if (arg != Py_None) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2678 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
|
2679 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
|
2680 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2681 |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2682 PyDoc_STRVAR(lstrip__doc__, |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2683 "B.lstrip([bytes]) -> bytes\n\ |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2684 \n\ |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2685 Strip leading bytes contained in the argument.\n\ |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2686 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
|
2687 static PyObject * |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2688 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
|
2689 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2690 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
|
2691 void *myptr, *argptr; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2692 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
|
2693 Py_buffer varg; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2694 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
|
2695 return NULL; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2696 if (arg == Py_None) { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2697 argptr = "\t\n\r\f\v "; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2698 argsize = 6; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2699 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2700 else { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2701 if (_getbuffer(arg, &varg) < 0) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2702 return NULL; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2703 argptr = varg.buf; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2704 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
|
2705 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2706 myptr = self->ob_bytes; |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
2707 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
|
2708 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
|
2709 right = mysize; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2710 if (arg != Py_None) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2711 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
|
2712 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
|
2713 } |
|
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 PyDoc_STRVAR(rstrip__doc__, |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2716 "B.rstrip([bytes]) -> bytes\n\ |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2717 \n\ |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2718 Strip trailing bytes contained in the argument.\n\ |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2719 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
|
2720 static PyObject * |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2721 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
|
2722 { |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2723 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
|
2724 void *myptr, *argptr; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2725 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
|
2726 Py_buffer varg; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2727 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
|
2728 return NULL; |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2729 if (arg == Py_None) { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2730 argptr = "\t\n\r\f\v "; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2731 argsize = 6; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2732 } |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2733 else { |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2734 if (_getbuffer(arg, &varg) < 0) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2735 return NULL; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2736 argptr = varg.buf; |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2737 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
|
2738 } |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2739 myptr = self->ob_bytes; |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
2740 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
|
2741 left = 0; |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41448
diff
changeset
|
2742 right = rstrip_helper(myptr, mysize, argptr, argsize); |
|
43321
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2743 if (arg != Py_None) |
|
282e2d0c2937
Bug # 1125 (my code).
Guido van Rossum <guido@python.org>
parents:
43150
diff
changeset
|
2744 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
|
2745 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
|
2746 } |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2747 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2748 PyDoc_STRVAR(decode_doc, |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2749 "B.decode([encoding[,errors]]) -> unicode obect.\n\ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2750 \n\ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2751 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
|
2752 to the default encoding. errors may be given to set a different error\n\ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2753 handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2754 a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2755 as well as any other name registerd with codecs.register_error that is\n\ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2756 able to handle UnicodeDecodeErrors."); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2757 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2758 static PyObject * |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2759 bytes_decode(PyObject *self, PyObject *args) |
|
41448
458c8db2e946
Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents:
41426
diff
changeset
|
2760 { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2761 const char *encoding = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2762 const char *errors = NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2763 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2764 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
|
2765 return NULL; |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2766 if (encoding == NULL) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2767 encoding = PyUnicode_GetDefaultEncoding(); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2768 return PyCodec_Decode(self, encoding, errors); |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2769 } |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2770 |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2771 PyDoc_STRVAR(alloc_doc, |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2772 "B.__alloc__() -> int\n\ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2773 \n\ |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2774 Returns the number of bytes actually allocated."); |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2775 |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2776 static PyObject * |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2777 bytes_alloc(PyBytesObject *self) |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2778 { |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2779 return PyInt_FromSsize_t(self->ob_alloc); |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2780 } |
|
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
2781 |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2782 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
|
2783 "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
|
2784 \n\ |
|
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
|
2785 Concatenates any number of bytes objects, with B in between each pair.\n\ |
|
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
|
2786 Example: b'.'.join([b'ab', b'pq', b'rs']) -> b'ab.pq.rs'."); |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2787 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2788 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
|
2789 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
|
2790 { |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2791 PyObject *seq; |
| 42335 | 2792 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
|
2793 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
|
2794 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
|
2795 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
|
2796 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
|
2797 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
|
2798 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
|
2799 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2800 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
|
2801 if (seq == NULL) |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2802 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
|
2803 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
|
2804 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
|
2805 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2806 /* Compute the total size, and check that they are all bytes */ |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2807 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
|
2808 PyObject *obj = items[i]; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2809 if (!PyBytes_Check(obj)) { |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2810 PyErr_Format(PyExc_TypeError, |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2811 "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
|
2812 "(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
|
2813 /* XXX %ld isn't right on Win64 */ |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
2814 (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
|
2815 goto error; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2816 } |
|
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
|
2817 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
|
2818 totalsize += mysize; |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2819 totalsize += PyBytes_GET_SIZE(obj); |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2820 if (totalsize < 0) { |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2821 PyErr_NoMemory(); |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2822 goto error; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2823 } |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2824 } |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2825 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2826 /* 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
|
2827 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
|
2828 if (result == NULL) |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2829 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
|
2830 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
|
2831 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
|
2832 PyObject *obj = items[i]; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2833 Py_ssize_t size = PyBytes_GET_SIZE(obj); |
|
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
|
2834 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
|
2835 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
|
2836 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
|
2837 } |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2838 memcpy(dest, PyBytes_AS_STRING(obj), size); |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2839 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
|
2840 } |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2841 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2842 /* Done */ |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2843 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
|
2844 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
|
2845 |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2846 /* 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
|
2847 error: |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2848 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
|
2849 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
|
2850 } |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38115
diff
changeset
|
2851 |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2852 PyDoc_STRVAR(fromhex_doc, |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2853 "bytes.fromhex(string) -> bytes\n\ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2854 \n\ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2855 Create a bytes object from a string of hexadecimal numbers.\n\ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2856 Spaces between two numbers are accepted. Example:\n\ |
|
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
|
2857 bytes.fromhex('10 1112') -> b'\\x10\\x11\\x12'."); |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2858 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2859 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
|
2860 hex_digit_to_int(Py_UNICODE c) |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2861 { |
|
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
|
2862 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
|
2863 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
|
2864 if (ISDIGIT(c)) |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2865 return c - '0'; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2866 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
|
2867 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
|
2868 c = TOLOWER(c); |
|
40968
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2869 if (c >= 'a' && c <= 'f') |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2870 return c - 'a' + 10; |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2871 } |
|
2d9d10702ec4
Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents:
40967
diff
changeset
|
2872 return -1; |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2873 } |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2874 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2875 static PyObject * |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2876 bytes_fromhex(PyObject *cls, PyObject *args) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2877 { |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
2878 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
|
2879 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
|
2880 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
|
2881 Py_ssize_t hexlen, byteslen, i, j; |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2882 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
|
2883 |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43689
diff
changeset
|
2884 if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj)) |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2885 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
|
2886 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
|
2887 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
|
2888 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
|
2889 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
|
2890 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
|
2891 if (!newbytes) |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2892 return NULL; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2893 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
|
2894 for (i = j = 0; i < hexlen; i += 2) { |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2895 /* 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
|
2896 while (hex[i] == ' ') |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2897 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
|
2898 if (i >= hexlen) |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2899 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
|
2900 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
|
2901 bot = hex_digit_to_int(hex[i+1]); |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2902 if (top == -1 || bot == -1) { |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2903 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
|
2904 "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
|
2905 "fromhex() arg at position %zd", i); |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2906 goto error; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2907 } |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2908 buf[j++] = (top << 4) + bot; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2909 } |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2910 if (PyBytes_Resize(newbytes, j) < 0) |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2911 goto error; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2912 return newbytes; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2913 |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2914 error: |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2915 Py_DECREF(newbytes); |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2916 return NULL; |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2917 } |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2918 |
|
41426
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2919 PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2920 |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2921 static PyObject * |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2922 bytes_reduce(PyBytesObject *self) |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2923 { |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
2924 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
|
2925 if (self->ob_bytes) |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2926 latin1 = PyUnicode_DecodeLatin1(self->ob_bytes, |
|
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2927 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
|
2928 else |
|
42975
71cd5598b74d
Commit strict str/bytes distinction.
Guido van Rossum <guido@python.org>
parents:
42893
diff
changeset
|
2929 latin1 = PyUnicode_FromString(""); |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
2930 |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
2931 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
|
2932 if (dict == NULL) { |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
2933 PyErr_Clear(); |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
2934 dict = Py_None; |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
2935 Py_INCREF(dict); |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
2936 } |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
2937 |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
2938 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
|
2939 } |
|
4561f6da3b7d
Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
2940 |
|
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
|
2941 static PySequenceMethods bytes_as_sequence = { |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2942 (lenfunc)bytes_length, /* sq_length */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2943 (binaryfunc)bytes_concat, /* sq_concat */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2944 (ssizeargfunc)bytes_repeat, /* sq_repeat */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2945 (ssizeargfunc)bytes_getitem, /* sq_item */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2946 0, /* sq_slice */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2947 (ssizeobjargproc)bytes_setitem, /* sq_ass_item */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2948 0, /* sq_ass_slice */ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2949 (objobjproc)bytes_contains, /* sq_contains */ |
|
40967
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2950 (binaryfunc)bytes_iconcat, /* sq_inplace_concat */ |
|
a9e6c73e0fa5
Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents:
40898
diff
changeset
|
2951 (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
|
2952 }; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2953 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2954 static PyMappingMethods bytes_as_mapping = { |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
2955 (lenfunc)bytes_length, |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
2956 (binaryfunc)bytes_subscript, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
2957 (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
|
2958 }; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2959 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2960 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
|
2961 (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
|
2962 (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
|
2963 }; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2964 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2965 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
|
2966 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
|
2967 {"__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
|
2968 {"__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
|
2969 {"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
|
2970 {"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
|
2971 _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
|
2972 {"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
|
2973 {"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
|
2974 {"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
|
2975 {"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
|
2976 {"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
|
2977 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
|
2978 {"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
|
2979 {"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
|
2980 {"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
|
2981 fromhex_doc}, |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40968
diff
changeset
|
2982 {"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
|
2983 {"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
|
2984 {"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
|
2985 _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
|
2986 {"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
|
2987 _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
|
2988 {"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
|
2989 _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
|
2990 {"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
|
2991 _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
|
2992 {"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
|
2993 _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
|
2994 {"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
|
2995 _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
|
2996 {"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
|
2997 _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
|
2998 {"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
|
2999 {"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
|
3000 {"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
|
3001 {"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
|
3002 {"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
|
3003 {"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
|
3004 {"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
|
3005 {"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
|
3006 {"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
|
3007 {"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
|
3008 {"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
|
3009 {"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
|
3010 {"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
|
3011 {"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
|
3012 {"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
|
3013 {"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
|
3014 {"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
|
3015 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
|
3016 {"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
|
3017 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
|
3018 {"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
|
3019 {"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
|
3020 _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
|
3021 {"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
|
3022 {"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
|
3023 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
|
3024 {"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
|
3025 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__}, |
|
38115
a6a1359aab84
Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
3026 {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
|
3027 }; |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3028 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3029 PyDoc_STRVAR(bytes_doc, |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3030 "bytes([iterable]) -> new array of bytes.\n\ |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3031 \n\ |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3032 If an argument is given it must be an iterable yielding ints in range(256)."); |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3033 |
|
43689
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3034 static PyObject *bytes_iter(PyObject *seq); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3035 |
|
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
|
3036 PyTypeObject PyBytes_Type = { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42319
diff
changeset
|
3037 PyVarObject_HEAD_INIT(&PyType_Type, 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
|
3038 "bytes", |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3039 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
|
3040 0, |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3041 (destructor)bytes_dealloc, /* tp_dealloc */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3042 0, /* tp_print */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3043 0, /* tp_getattr */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3044 0, /* tp_setattr */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3045 0, /* tp_compare */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3046 (reprfunc)bytes_repr, /* tp_repr */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3047 0, /* tp_as_number */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3048 &bytes_as_sequence, /* tp_as_sequence */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3049 &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
|
3050 0, /* tp_hash */ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3051 0, /* tp_call */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3052 (reprfunc)bytes_str, /* tp_str */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3053 PyObject_GenericGetAttr, /* tp_getattro */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3054 0, /* tp_setattro */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3055 &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
|
3056 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
|
3057 bytes_doc, /* tp_doc */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3058 0, /* tp_traverse */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3059 0, /* tp_clear */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3060 (richcmpfunc)bytes_richcompare, /* tp_richcompare */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3061 0, /* tp_weaklistoffset */ |
|
43689
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3062 bytes_iter, /* tp_iter */ |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3063 0, /* tp_iternext */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3064 bytes_methods, /* tp_methods */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3065 0, /* tp_members */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3066 0, /* tp_getset */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3067 0, /* tp_base */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3068 0, /* tp_dict */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3069 0, /* tp_descr_get */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3070 0, /* tp_descr_set */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3071 0, /* tp_dictoffset */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3072 (initproc)bytes_init, /* tp_init */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3073 PyType_GenericAlloc, /* tp_alloc */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3074 PyType_GenericNew, /* tp_new */ |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
3075 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
|
3076 }; |
|
43689
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3077 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3078 /*********************** Bytes Iterator ****************************/ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3079 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3080 typedef struct { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3081 PyObject_HEAD |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3082 Py_ssize_t it_index; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3083 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
|
3084 } bytesiterobject; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3085 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3086 static void |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3087 bytesiter_dealloc(bytesiterobject *it) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3088 { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3089 _PyObject_GC_UNTRACK(it); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3090 Py_XDECREF(it->it_seq); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3091 PyObject_GC_Del(it); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3092 } |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3093 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3094 static int |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3095 bytesiter_traverse(bytesiterobject *it, visitproc visit, void *arg) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3096 { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3097 Py_VISIT(it->it_seq); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3098 return 0; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3099 } |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3100 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3101 static PyObject * |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3102 bytesiter_next(bytesiterobject *it) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3103 { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3104 PyBytesObject *seq; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3105 PyObject *item; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3106 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3107 assert(it != NULL); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3108 seq = it->it_seq; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3109 if (seq == NULL) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3110 return NULL; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3111 assert(PyBytes_Check(seq)); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3112 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3113 if (it->it_index < PyBytes_GET_SIZE(seq)) { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3114 item = PyInt_FromLong( |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3115 (unsigned char)seq->ob_bytes[it->it_index]); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3116 if (item != NULL) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3117 ++it->it_index; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3118 return item; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3119 } |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3120 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3121 Py_DECREF(seq); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3122 it->it_seq = NULL; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3123 return NULL; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3124 } |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3125 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3126 static PyObject * |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3127 bytesiter_length_hint(bytesiterobject *it) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3128 { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3129 Py_ssize_t len = 0; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3130 if (it->it_seq) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3131 len = PyBytes_GET_SIZE(it->it_seq) - it->it_index; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3132 return PyInt_FromSsize_t(len); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3133 } |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3134 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3135 PyDoc_STRVAR(length_hint_doc, |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3136 "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
|
3137 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3138 static PyMethodDef bytesiter_methods[] = { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3139 {"__length_hint__", (PyCFunction)bytesiter_length_hint, METH_NOARGS, |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3140 length_hint_doc}, |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3141 {NULL, NULL} /* sentinel */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3142 }; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3143 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3144 PyTypeObject PyBytesIter_Type = { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3145 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3146 "bytesiterator", /* tp_name */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3147 sizeof(bytesiterobject), /* tp_basicsize */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3148 0, /* tp_itemsize */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3149 /* methods */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3150 (destructor)bytesiter_dealloc, /* tp_dealloc */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3151 0, /* tp_print */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3152 0, /* tp_getattr */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3153 0, /* tp_setattr */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3154 0, /* tp_compare */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3155 0, /* tp_repr */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3156 0, /* tp_as_number */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3157 0, /* tp_as_sequence */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3158 0, /* tp_as_mapping */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3159 0, /* tp_hash */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3160 0, /* tp_call */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3161 0, /* tp_str */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3162 PyObject_GenericGetAttr, /* tp_getattro */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3163 0, /* tp_setattro */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3164 0, /* tp_as_buffer */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3165 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
|
3166 0, /* tp_doc */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3167 (traverseproc)bytesiter_traverse, /* tp_traverse */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3168 0, /* tp_clear */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3169 0, /* tp_richcompare */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3170 0, /* tp_weaklistoffset */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3171 PyObject_SelfIter, /* tp_iter */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3172 (iternextfunc)bytesiter_next, /* tp_iternext */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3173 bytesiter_methods, /* tp_methods */ |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3174 0, |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3175 }; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3176 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3177 static PyObject * |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3178 bytes_iter(PyObject *seq) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3179 { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3180 bytesiterobject *it; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3181 |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3182 if (!PyBytes_Check(seq)) { |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3183 PyErr_BadInternalCall(); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3184 return NULL; |
|
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 it = PyObject_GC_New(bytesiterobject, &PyBytesIter_Type); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3187 if (it == NULL) |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3188 return NULL; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3189 it->it_index = 0; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3190 Py_INCREF(seq); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3191 it->it_seq = (PyBytesObject *)seq; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3192 _PyObject_GC_TRACK(it); |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3193 return (PyObject *)it; |
|
64ce3bf3a6a9
Patch 1335 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
43687
diff
changeset
|
3194 } |
