Mercurial > cpython-lru_cache-weakref
annotate Objects/bytesobject.c @ 79063:2bdc8c8ea42e
Issue #15977: Fix memory leak in Modules/_ssl.c when the function _set_npn_protocols() is called multiple times
| author | Christian Heimes <christian@cheimes.de> |
|---|---|
| date | Thu, 20 Sep 2012 12:42:54 +0200 |
| parents | 8fbbc7c8748e |
| children | 8b4fff41dee5 a7ec0a1b0f7c |
| rev | line source |
|---|---|
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1 /* bytes object implementation */ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
3 #define PY_SSIZE_T_CLEAN |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
4 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
5 #include "Python.h" |
| 6 | 6 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43503
diff
changeset
|
7 #include "bytes_methods.h" |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
8 #include <stddef.h> |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
9 |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
10 static Py_ssize_t |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
11 _getbuffer(PyObject *obj, Py_buffer *view) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
12 { |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
13 PyBufferProcs *buffer = Py_TYPE(obj)->tp_as_buffer; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
14 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
15 if (buffer == NULL || buffer->bf_getbuffer == NULL) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
16 { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
17 PyErr_Format(PyExc_TypeError, |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
18 "Type %.100s doesn't support the buffer API", |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
19 Py_TYPE(obj)->tp_name); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
20 return -1; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
21 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
22 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
23 if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
24 return -1; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
25 return view->len; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
26 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
27 |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
28 #ifdef COUNT_ALLOCS |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
29 Py_ssize_t null_strings, one_strings; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
30 #endif |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
31 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
32 static PyBytesObject *characters[UCHAR_MAX + 1]; |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
33 static PyBytesObject *nullstring; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
34 |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
35 /* PyBytesObject_SIZE gives the basic size of a string; any memory allocation |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
36 for a string of length n should request PyBytesObject_SIZE + n bytes. |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
37 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
38 Using PyBytesObject_SIZE instead of sizeof(PyBytesObject) saves |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
39 3 bytes per string allocation on a typical system. |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
40 */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
41 #define PyBytesObject_SIZE (offsetof(PyBytesObject, ob_sval) + 1) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
42 |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
43 /* |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
44 For PyBytes_FromString(), the parameter `str' points to a null-terminated |
|
21737
30a817038af0
Updated patch #487906: Revise inline docs.
Martin v. Löwis <martin@v.loewis.de>
parents:
21327
diff
changeset
|
45 string containing exactly `size' bytes. |
|
30a817038af0
Updated patch #487906: Revise inline docs.
Martin v. Löwis <martin@v.loewis.de>
parents:
21327
diff
changeset
|
46 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
47 For PyBytes_FromStringAndSize(), the parameter the parameter `str' is |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
48 either NULL or else points to a string containing at least `size' bytes. |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
49 For PyBytes_FromStringAndSize(), the string in the `str' parameter does |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
50 not have to be null-terminated. (Therefore it is safe to construct a |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
51 substring by calling `PyBytes_FromStringAndSize(origstring, substrlen)'.) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
52 If `str' is NULL then PyBytes_FromStringAndSize() will allocate `size+1' |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
53 bytes (setting the last byte to the null terminating character) and you can |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
54 fill in the data yourself. If `str' is non-NULL then the resulting |
|
58289
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
55 PyBytes object must be treated as immutable and you must not fill in nor |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
56 alter the data yourself, since the strings may be shared. |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
57 |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
58 The PyObject member `op->ob_size', which denotes the number of "extra |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
59 items" in a variable-size object, will contain the number of bytes |
|
68906
a729dfdbd24b
Issue #11634: Remove misleading paragraph from a comment
Eli Bendersky <eliben@gmail.com>
parents:
68489
diff
changeset
|
60 allocated for string data, not counting the null terminating character. |
|
a729dfdbd24b
Issue #11634: Remove misleading paragraph from a comment
Eli Bendersky <eliben@gmail.com>
parents:
68489
diff
changeset
|
61 It is therefore equal to the `size' parameter (for |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
62 PyBytes_FromStringAndSize()) or the length of the string in the `str' |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
63 parameter (for PyBytes_FromString()). |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
64 */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
65 PyObject * |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
66 PyBytes_FromStringAndSize(const char *str, Py_ssize_t size) |
| 6 | 67 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
68 register PyBytesObject *op; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
69 if (size < 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
70 PyErr_SetString(PyExc_SystemError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
71 "Negative size passed to PyBytes_FromStringAndSize"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
72 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
73 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
74 if (size == 0 && (op = nullstring) != NULL) { |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
75 #ifdef COUNT_ALLOCS |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
76 null_strings++; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
77 #endif |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
78 Py_INCREF(op); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
79 return (PyObject *)op; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
80 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
81 if (size == 1 && str != NULL && |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
82 (op = characters[*str & UCHAR_MAX]) != NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
83 { |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
84 #ifdef COUNT_ALLOCS |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
85 one_strings++; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
86 #endif |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
87 Py_INCREF(op); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
88 return (PyObject *)op; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
89 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
90 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
91 if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
92 PyErr_SetString(PyExc_OverflowError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
93 "byte string is too large"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
94 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
95 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
96 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
97 /* Inline PyObject_NewVar */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
98 op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
99 if (op == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
100 return PyErr_NoMemory(); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
101 PyObject_INIT_VAR(op, &PyBytes_Type, size); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
102 op->ob_shash = -1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
103 if (str != NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
104 Py_MEMCPY(op->ob_sval, str, size); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
105 op->ob_sval[size] = '\0'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
106 /* share short strings */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
107 if (size == 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
108 nullstring = op; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
109 Py_INCREF(op); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
110 } else if (size == 1 && str != NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
111 characters[*str & UCHAR_MAX] = op; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
112 Py_INCREF(op); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
113 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
114 return (PyObject *) op; |
| 6 | 115 } |
| 116 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
117 PyObject * |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
118 PyBytes_FromString(const char *str) |
| 6 | 119 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
120 register size_t size; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
121 register PyBytesObject *op; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
122 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
123 assert(str != NULL); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
124 size = strlen(str); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
125 if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
126 PyErr_SetString(PyExc_OverflowError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
127 "byte string is too long"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
128 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
129 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
130 if (size == 0 && (op = nullstring) != NULL) { |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
131 #ifdef COUNT_ALLOCS |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
132 null_strings++; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
133 #endif |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
134 Py_INCREF(op); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
135 return (PyObject *)op; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
136 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
137 if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
138 #ifdef COUNT_ALLOCS |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
139 one_strings++; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
140 #endif |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
141 Py_INCREF(op); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
142 return (PyObject *)op; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
143 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
144 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
145 /* Inline PyObject_NewVar */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
146 op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
147 if (op == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
148 return PyErr_NoMemory(); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
149 PyObject_INIT_VAR(op, &PyBytes_Type, size); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
150 op->ob_shash = -1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
151 Py_MEMCPY(op->ob_sval, str, size+1); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
152 /* share short strings */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
153 if (size == 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
154 nullstring = op; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
155 Py_INCREF(op); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
156 } else if (size == 1) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
157 characters[*str & UCHAR_MAX] = op; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
158 Py_INCREF(op); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
159 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
160 return (PyObject *) op; |
| 6 | 161 } |
| 162 | |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
163 PyObject * |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
164 PyBytes_FromFormatV(const char *format, va_list vargs) |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
165 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
166 va_list count; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
167 Py_ssize_t n = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
168 const char* f; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
169 char *s; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
170 PyObject* string; |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
171 |
|
63924
9108e7dcd7d8
Issue #2443: Added a new macro, Py_VA_COPY, which is equivalent to C99
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63913
diff
changeset
|
172 Py_VA_COPY(count, vargs); |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
173 /* step 1: figure out how large a buffer we need */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
174 for (f = format; *f; f++) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
175 if (*f == '%') { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
176 const char* p = f; |
|
65980
d31eaec883eb
Issue #10288: The deprecated family of "char"-handling macros
David Malcolm <dmalcolm@redhat.com>
parents:
65494
diff
changeset
|
177 while (*++f && *f != '%' && !Py_ISALPHA(*f)) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
178 ; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
179 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
180 /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
181 * they don't affect the amount of space we reserve. |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
182 */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
183 if ((*f == 'l' || *f == 'z') && |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
184 (f[1] == 'd' || f[1] == 'u')) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
185 ++f; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
186 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
187 switch (*f) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
188 case 'c': |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
189 (void)va_arg(count, int); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
190 /* fall through... */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
191 case '%': |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
192 n++; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
193 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
194 case 'd': case 'u': case 'i': case 'x': |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
195 (void) va_arg(count, int); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
196 /* 20 bytes is enough to hold a 64-bit |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
197 integer. Decimal takes the most space. |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
198 This isn't enough for octal. */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
199 n += 20; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
200 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
201 case 's': |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
202 s = va_arg(count, char*); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
203 n += strlen(s); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
204 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
205 case 'p': |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
206 (void) va_arg(count, int); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
207 /* maximum 64-bit pointer representation: |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
208 * 0xffffffffffffffff |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
209 * so 19 characters is enough. |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
210 * XXX I count 18 -- what's the extra for? |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
211 */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
212 n += 19; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
213 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
214 default: |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
215 /* if we stumble upon an unknown |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
216 formatting code, copy the rest of |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
217 the format string to the output |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
218 string. (we cannot just skip the |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
219 code, since there's no way to know |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
220 what's in the argument list) */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
221 n += strlen(p); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
222 goto expand; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
223 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
224 } else |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
225 n++; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
226 } |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
227 expand: |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
228 /* step 2: fill the buffer */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
229 /* Since we've analyzed how much space we need for the worst case, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
230 use sprintf directly instead of the slower PyOS_snprintf. */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
231 string = PyBytes_FromStringAndSize(NULL, n); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
232 if (!string) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
233 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
234 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
235 s = PyBytes_AsString(string); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
236 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
237 for (f = format; *f; f++) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
238 if (*f == '%') { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
239 const char* p = f++; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
240 Py_ssize_t i; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
241 int longflag = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
242 int size_tflag = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
243 /* parse the width.precision part (we're only |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
244 interested in the precision value, if any) */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
245 n = 0; |
|
65980
d31eaec883eb
Issue #10288: The deprecated family of "char"-handling macros
David Malcolm <dmalcolm@redhat.com>
parents:
65494
diff
changeset
|
246 while (Py_ISDIGIT(*f)) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
247 n = (n*10) + *f++ - '0'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
248 if (*f == '.') { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
249 f++; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
250 n = 0; |
|
65980
d31eaec883eb
Issue #10288: The deprecated family of "char"-handling macros
David Malcolm <dmalcolm@redhat.com>
parents:
65494
diff
changeset
|
251 while (Py_ISDIGIT(*f)) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
252 n = (n*10) + *f++ - '0'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
253 } |
|
65980
d31eaec883eb
Issue #10288: The deprecated family of "char"-handling macros
David Malcolm <dmalcolm@redhat.com>
parents:
65494
diff
changeset
|
254 while (*f && *f != '%' && !Py_ISALPHA(*f)) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
255 f++; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
256 /* handle the long flag, but only for %ld and %lu. |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
257 others can be added when necessary. */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
258 if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
259 longflag = 1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
260 ++f; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
261 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
262 /* handle the size_t flag. */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
263 if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
264 size_tflag = 1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
265 ++f; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
266 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
267 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
268 switch (*f) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
269 case 'c': |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
270 *s++ = va_arg(vargs, int); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
271 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
272 case 'd': |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
273 if (longflag) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
274 sprintf(s, "%ld", va_arg(vargs, long)); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
275 else if (size_tflag) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
276 sprintf(s, "%" PY_FORMAT_SIZE_T "d", |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
277 va_arg(vargs, Py_ssize_t)); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
278 else |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
279 sprintf(s, "%d", va_arg(vargs, int)); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
280 s += strlen(s); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
281 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
282 case 'u': |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
283 if (longflag) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
284 sprintf(s, "%lu", |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
285 va_arg(vargs, unsigned long)); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
286 else if (size_tflag) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
287 sprintf(s, "%" PY_FORMAT_SIZE_T "u", |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
288 va_arg(vargs, size_t)); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
289 else |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
290 sprintf(s, "%u", |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
291 va_arg(vargs, unsigned int)); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
292 s += strlen(s); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
293 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
294 case 'i': |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
295 sprintf(s, "%i", va_arg(vargs, int)); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
296 s += strlen(s); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
297 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
298 case 'x': |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
299 sprintf(s, "%x", va_arg(vargs, int)); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
300 s += strlen(s); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
301 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
302 case 's': |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
303 p = va_arg(vargs, char*); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
304 i = strlen(p); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
305 if (n > 0 && i > n) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
306 i = n; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
307 Py_MEMCPY(s, p, i); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
308 s += i; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
309 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
310 case 'p': |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
311 sprintf(s, "%p", va_arg(vargs, void*)); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
312 /* %p is ill-defined: ensure leading 0x. */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
313 if (s[1] == 'X') |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
314 s[1] = 'x'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
315 else if (s[1] != 'x') { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
316 memmove(s+2, s, strlen(s)+1); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
317 s[0] = '0'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
318 s[1] = 'x'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
319 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
320 s += strlen(s); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
321 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
322 case '%': |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
323 *s++ = '%'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
324 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
325 default: |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
326 strcpy(s, p); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
327 s += strlen(s); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
328 goto end; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
329 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
330 } else |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
331 *s++ = *f; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
332 } |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
333 |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
334 end: |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
335 _PyBytes_Resize(&string, s - PyBytes_AS_STRING(string)); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
336 return string; |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
337 } |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
338 |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
339 PyObject * |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
340 PyBytes_FromFormat(const char *format, ...) |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
341 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
342 PyObject* ret; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
343 va_list vargs; |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
344 |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
345 #ifdef HAVE_STDARG_PROTOTYPES |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
346 va_start(vargs, format); |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
347 #else |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
348 va_start(vargs); |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
349 #endif |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
350 ret = PyBytes_FromFormatV(format, vargs); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
351 va_end(vargs); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
352 return ret; |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
353 } |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
354 |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
355 static void |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
356 bytes_dealloc(PyObject *op) |
| 750 | 357 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
358 Py_TYPE(op)->tp_free(op); |
| 750 | 359 } |
| 360 | |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
361 /* Unescape a backslash-escaped string. If unicode is non-zero, |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
362 the string is a u-literal. If recode_encoding is non-zero, |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
363 the string is UTF-8 encoded and should be re-encoded in the |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
364 specified encoding. */ |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
365 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
366 PyObject *PyBytes_DecodeEscape(const char *s, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
367 Py_ssize_t len, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
368 const char *errors, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
369 Py_ssize_t unicode, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
370 const char *recode_encoding) |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
371 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
372 int c; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
373 char *p, *buf; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
374 const char *end; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
375 PyObject *v; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
376 Py_ssize_t newlen = recode_encoding ? 4*len:len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
377 v = PyBytes_FromStringAndSize((char *)NULL, newlen); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
378 if (v == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
379 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
380 p = buf = PyBytes_AsString(v); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
381 end = s + len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
382 while (s < end) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
383 if (*s != '\\') { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
384 non_esc: |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
385 if (recode_encoding && (*s & 0x80)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
386 PyObject *u, *w; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
387 char *r; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
388 const char* t; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
389 Py_ssize_t rn; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
390 t = s; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
391 /* Decode non-ASCII bytes as UTF-8. */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
392 while (t < end && (*t & 0x80)) t++; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
393 u = PyUnicode_DecodeUTF8(s, t - s, errors); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
394 if(!u) goto failed; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
395 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
396 /* Recode them in target encoding. */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
397 w = PyUnicode_AsEncodedString( |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
398 u, recode_encoding, errors); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
399 Py_DECREF(u); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
400 if (!w) goto failed; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
401 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
402 /* Append bytes to output buffer. */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
403 assert(PyBytes_Check(w)); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
404 r = PyBytes_AS_STRING(w); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
405 rn = PyBytes_GET_SIZE(w); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
406 Py_MEMCPY(p, r, rn); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
407 p += rn; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
408 Py_DECREF(w); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
409 s = t; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
410 } else { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
411 *p++ = *s++; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
412 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
413 continue; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
414 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
415 s++; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
416 if (s==end) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
417 PyErr_SetString(PyExc_ValueError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
418 "Trailing \\ in string"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
419 goto failed; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
420 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
421 switch (*s++) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
422 /* XXX This assumes ASCII! */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
423 case '\n': break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
424 case '\\': *p++ = '\\'; break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
425 case '\'': *p++ = '\''; break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
426 case '\"': *p++ = '\"'; break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
427 case 'b': *p++ = '\b'; break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
428 case 'f': *p++ = '\014'; break; /* FF */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
429 case 't': *p++ = '\t'; break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
430 case 'n': *p++ = '\n'; break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
431 case 'r': *p++ = '\r'; break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
432 case 'v': *p++ = '\013'; break; /* VT */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
433 case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
434 case '0': case '1': case '2': case '3': |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
435 case '4': case '5': case '6': case '7': |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
436 c = s[-1] - '0'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
437 if (s < end && '0' <= *s && *s <= '7') { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
438 c = (c<<3) + *s++ - '0'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
439 if (s < end && '0' <= *s && *s <= '7') |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
440 c = (c<<3) + *s++ - '0'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
441 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
442 *p++ = c; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
443 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
444 case 'x': |
|
65980
d31eaec883eb
Issue #10288: The deprecated family of "char"-handling macros
David Malcolm <dmalcolm@redhat.com>
parents:
65494
diff
changeset
|
445 if (s+1 < end && Py_ISXDIGIT(s[0]) && Py_ISXDIGIT(s[1])) { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
446 unsigned int x = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
447 c = Py_CHARMASK(*s); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
448 s++; |
|
65980
d31eaec883eb
Issue #10288: The deprecated family of "char"-handling macros
David Malcolm <dmalcolm@redhat.com>
parents:
65494
diff
changeset
|
449 if (Py_ISDIGIT(c)) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
450 x = c - '0'; |
|
65980
d31eaec883eb
Issue #10288: The deprecated family of "char"-handling macros
David Malcolm <dmalcolm@redhat.com>
parents:
65494
diff
changeset
|
451 else if (Py_ISLOWER(c)) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
452 x = 10 + c - 'a'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
453 else |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
454 x = 10 + c - 'A'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
455 x = x << 4; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
456 c = Py_CHARMASK(*s); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
457 s++; |
|
65980
d31eaec883eb
Issue #10288: The deprecated family of "char"-handling macros
David Malcolm <dmalcolm@redhat.com>
parents:
65494
diff
changeset
|
458 if (Py_ISDIGIT(c)) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
459 x += c - '0'; |
|
65980
d31eaec883eb
Issue #10288: The deprecated family of "char"-handling macros
David Malcolm <dmalcolm@redhat.com>
parents:
65494
diff
changeset
|
460 else if (Py_ISLOWER(c)) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
461 x += 10 + c - 'a'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
462 else |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
463 x += 10 + c - 'A'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
464 *p++ = x; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
465 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
466 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
467 if (!errors || strcmp(errors, "strict") == 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
468 PyErr_SetString(PyExc_ValueError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
469 "invalid \\x escape"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
470 goto failed; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
471 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
472 if (strcmp(errors, "replace") == 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
473 *p++ = '?'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
474 } else if (strcmp(errors, "ignore") == 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
475 /* do nothing */; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
476 else { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
477 PyErr_Format(PyExc_ValueError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
478 "decoding error; unknown " |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
479 "error handling code: %.400s", |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
480 errors); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
481 goto failed; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
482 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
483 default: |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
484 *p++ = '\\'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
485 s--; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
486 goto non_esc; /* an arbitrary number of unescaped |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
487 UTF-8 bytes may follow. */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
488 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
489 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
490 if (p-buf < newlen) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
491 _PyBytes_Resize(&v, p - buf); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
492 return v; |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
493 failed: |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
494 Py_DECREF(v); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
495 return NULL; |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
496 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
497 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
498 /* -------------------------------------------------------------------- */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
499 /* object api */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
500 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
501 Py_ssize_t |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
502 PyBytes_Size(register PyObject *op) |
| 6 | 503 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
504 if (!PyBytes_Check(op)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
505 PyErr_Format(PyExc_TypeError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
506 "expected bytes, %.200s found", Py_TYPE(op)->tp_name); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
507 return -1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
508 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
509 return Py_SIZE(op); |
| 6 | 510 } |
| 511 | |
|
44548
05bcb790ef63
Clean up PyString_Size().
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44351
diff
changeset
|
512 char * |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
513 PyBytes_AsString(register PyObject *op) |
| 6 | 514 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
515 if (!PyBytes_Check(op)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
516 PyErr_Format(PyExc_TypeError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
517 "expected bytes, %.200s found", Py_TYPE(op)->tp_name); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
518 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
519 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
520 return ((PyBytesObject *)op)->ob_sval; |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
521 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
522 |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
523 int |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
524 PyBytes_AsStringAndSize(register PyObject *obj, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
525 register char **s, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
526 register Py_ssize_t *len) |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
527 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
528 if (s == NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
529 PyErr_BadInternalCall(); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
530 return -1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
531 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
532 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
533 if (!PyBytes_Check(obj)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
534 PyErr_Format(PyExc_TypeError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
535 "expected bytes, %.200s found", Py_TYPE(obj)->tp_name); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
536 return -1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
537 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
538 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
539 *s = PyBytes_AS_STRING(obj); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
540 if (len != NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
541 *len = PyBytes_GET_SIZE(obj); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
542 else if (strlen(*s) != (size_t)PyBytes_GET_SIZE(obj)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
543 PyErr_SetString(PyExc_TypeError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
544 "expected bytes with no null"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
545 return -1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
546 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
547 return 0; |
| 6 | 548 } |
| 549 | |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
550 /* -------------------------------------------------------------------- */ |
| 6 | 551 /* Methods */ |
| 552 | |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
553 #include "stringlib/stringdefs.h" |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
554 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
555 #include "stringlib/fastsearch.h" |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
556 #include "stringlib/count.h" |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
557 #include "stringlib/find.h" |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
558 #include "stringlib/partition.h" |
|
58289
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
559 #include "stringlib/split.h" |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43503
diff
changeset
|
560 #include "stringlib/ctype.h" |
|
57515
f37f6b9b224b
Issue #5748: bytesobject.c should not have its own private defines for stringlib macros. Also removed unused defines and include for localutil.h.
Eric Smith <eric@trueblade.com>
parents:
56099
diff
changeset
|
561 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43503
diff
changeset
|
562 #include "stringlib/transmogrify.h" |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
563 |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
564 PyObject * |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
565 PyBytes_Repr(PyObject *obj, int smartquotes) |
| 6 | 566 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
567 register PyBytesObject* op = (PyBytesObject*) obj; |
| 72475 | 568 Py_ssize_t i, length = Py_SIZE(op); |
| 569 size_t newsize, squotes, dquotes; | |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
570 PyObject *v; |
| 72475 | 571 unsigned char quote, *s, *p; |
| 572 | |
| 573 /* Compute size of output string */ | |
| 574 squotes = dquotes = 0; | |
| 575 newsize = 3; /* b'' */ | |
| 576 s = (unsigned char*)op->ob_sval; | |
| 577 for (i = 0; i < length; i++) { | |
| 578 switch(s[i]) { | |
| 579 case '\'': squotes++; newsize++; break; | |
| 580 case '"': dquotes++; newsize++; break; | |
| 581 case '\\': case '\t': case '\n': case '\r': | |
| 582 newsize += 2; break; /* \C */ | |
| 583 default: | |
| 584 if (s[i] < ' ' || s[i] >= 0x7f) | |
| 585 newsize += 4; /* \xHH */ | |
| 586 else | |
| 587 newsize++; | |
| 588 } | |
| 589 } | |
| 590 quote = '\''; | |
| 591 if (smartquotes && squotes && !dquotes) | |
| 592 quote = '"'; | |
| 593 if (squotes && quote == '\'') | |
| 594 newsize += squotes; | |
|
72520
e6a2b54c1d16
Fix hex_digit_to_int() prototype: expect Py_UCS4, not Py_UNICODE
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
595 |
| 72475 | 596 if (newsize > (PY_SSIZE_T_MAX - sizeof(PyUnicodeObject) - 1)) { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
597 PyErr_SetString(PyExc_OverflowError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
598 "bytes object is too large to make repr"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
599 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
600 } |
| 72475 | 601 |
| 602 v = PyUnicode_New(newsize, 127); | |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
603 if (v == NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
604 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
605 } |
| 72475 | 606 p = PyUnicode_1BYTE_DATA(v); |
| 607 | |
| 608 *p++ = 'b', *p++ = quote; | |
| 609 for (i = 0; i < length; i++) { | |
| 610 unsigned char c = op->ob_sval[i]; | |
| 611 if (c == quote || c == '\\') | |
| 612 *p++ = '\\', *p++ = c; | |
| 613 else if (c == '\t') | |
| 614 *p++ = '\\', *p++ = 't'; | |
| 615 else if (c == '\n') | |
| 616 *p++ = '\\', *p++ = 'n'; | |
| 617 else if (c == '\r') | |
| 618 *p++ = '\\', *p++ = 'r'; | |
| 619 else if (c < ' ' || c >= 0x7f) { | |
| 620 *p++ = '\\'; | |
| 621 *p++ = 'x'; | |
|
72918
d76338eacf7c
Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72520
diff
changeset
|
622 *p++ = Py_hexdigits[(c & 0xf0) >> 4]; |
|
d76338eacf7c
Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72520
diff
changeset
|
623 *p++ = Py_hexdigits[c & 0xf]; |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
624 } |
| 72475 | 625 else |
| 626 *p++ = c; | |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
627 } |
| 72475 | 628 *p++ = quote; |
|
76573
1439e2d1f490
Check newly created consistency using _PyUnicode_CheckConsistency(str, 1)
Victor Stinner <victor.stinner@gmail.com>
parents:
76193
diff
changeset
|
629 assert(_PyUnicode_CheckConsistency(v, 1)); |
| 72475 | 630 return v; |
| 6 | 631 } |
| 632 | |
|
17645
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
633 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
634 bytes_repr(PyObject *op) |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
635 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
636 return PyBytes_Repr(op, 1); |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
637 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
638 |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
639 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
640 bytes_str(PyObject *op) |
|
17645
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
641 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
642 if (Py_BytesWarningFlag) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
643 if (PyErr_WarnEx(PyExc_BytesWarning, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
644 "str() on a bytes instance", 1)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
645 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
646 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
647 return bytes_repr(op); |
|
17645
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
648 } |
|
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
649 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
650 static Py_ssize_t |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
651 bytes_length(PyBytesObject *a) |
| 6 | 652 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
653 return Py_SIZE(a); |
| 6 | 654 } |
| 655 | |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
656 /* This is also used by PyBytes_Concat() */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
657 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
658 bytes_concat(PyObject *a, PyObject *b) |
| 6 | 659 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
660 Py_ssize_t size; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
661 Py_buffer va, vb; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
662 PyObject *result = NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
663 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
664 va.len = -1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
665 vb.len = -1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
666 if (_getbuffer(a, &va) < 0 || |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
667 _getbuffer(b, &vb) < 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
668 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
669 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
670 goto done; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
671 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
672 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
673 /* Optimize end cases */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
674 if (va.len == 0 && PyBytes_CheckExact(b)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
675 result = b; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
676 Py_INCREF(result); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
677 goto done; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
678 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
679 if (vb.len == 0 && PyBytes_CheckExact(a)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
680 result = a; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
681 Py_INCREF(result); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
682 goto done; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
683 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
684 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
685 size = va.len + vb.len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
686 if (size < 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
687 PyErr_NoMemory(); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
688 goto done; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
689 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
690 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
691 result = PyBytes_FromStringAndSize(NULL, size); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
692 if (result != NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
693 memcpy(PyBytes_AS_STRING(result), va.buf, va.len); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
694 memcpy(PyBytes_AS_STRING(result) + va.len, vb.buf, vb.len); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
695 } |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
696 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
697 done: |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
698 if (va.len != -1) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
699 PyBuffer_Release(&va); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
700 if (vb.len != -1) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
701 PyBuffer_Release(&vb); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
702 return result; |
| 6 | 703 } |
| 704 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
705 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
706 bytes_repeat(register PyBytesObject *a, register Py_ssize_t n) |
| 6 | 707 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
708 register Py_ssize_t i; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
709 register Py_ssize_t j; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
710 register Py_ssize_t size; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
711 register PyBytesObject *op; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
712 size_t nbytes; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
713 if (n < 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
714 n = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
715 /* watch out for overflows: the size can overflow int, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
716 * and the # of bytes needed can overflow size_t |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
717 */ |
|
63913
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
718 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n) { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
719 PyErr_SetString(PyExc_OverflowError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
720 "repeated bytes are too long"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
721 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
722 } |
|
63913
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
723 size = Py_SIZE(a) * n; |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
724 if (size == Py_SIZE(a) && PyBytes_CheckExact(a)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
725 Py_INCREF(a); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
726 return (PyObject *)a; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
727 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
728 nbytes = (size_t)size; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
729 if (nbytes + PyBytesObject_SIZE <= nbytes) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
730 PyErr_SetString(PyExc_OverflowError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
731 "repeated bytes are too long"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
732 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
733 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
734 op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + nbytes); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
735 if (op == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
736 return PyErr_NoMemory(); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
737 PyObject_INIT_VAR(op, &PyBytes_Type, size); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
738 op->ob_shash = -1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
739 op->ob_sval[size] = '\0'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
740 if (Py_SIZE(a) == 1 && n > 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
741 memset(op->ob_sval, a->ob_sval[0] , n); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
742 return (PyObject *) op; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
743 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
744 i = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
745 if (i < size) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
746 Py_MEMCPY(op->ob_sval, a->ob_sval, Py_SIZE(a)); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
747 i = Py_SIZE(a); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
748 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
749 while (i < size) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
750 j = (i <= size-i) ? i : size-i; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
751 Py_MEMCPY(op->ob_sval+i, op->ob_sval, j); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
752 i += j; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
753 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
754 return (PyObject *) op; |
| 6 | 755 } |
| 756 | |
|
11942
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
757 static int |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
758 bytes_contains(PyObject *self, PyObject *arg) |
|
11942
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
759 { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
760 Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
761 if (ival == -1 && PyErr_Occurred()) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
762 Py_buffer varg; |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
763 Py_ssize_t pos; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
764 PyErr_Clear(); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
765 if (_getbuffer(arg, &varg) < 0) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
766 return -1; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
767 pos = stringlib_find(PyBytes_AS_STRING(self), Py_SIZE(self), |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
768 varg.buf, varg.len, 0); |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
769 PyBuffer_Release(&varg); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
770 return pos >= 0; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
771 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
772 if (ival < 0 || ival >= 256) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
773 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
774 return -1; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
775 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
776 |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
777 return memchr(PyBytes_AS_STRING(self), (int) ival, Py_SIZE(self)) != NULL; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
778 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
779 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
780 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
781 bytes_item(PyBytesObject *a, register Py_ssize_t i) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
782 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
783 if (i < 0 || i >= Py_SIZE(a)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
784 PyErr_SetString(PyExc_IndexError, "index out of range"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
785 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
786 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
787 return PyLong_FromLong((unsigned char)a->ob_sval[i]); |
|
11942
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
788 } |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
789 |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
790 static PyObject* |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
791 bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op) |
| 6 | 792 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
793 int c; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
794 Py_ssize_t len_a, len_b; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
795 Py_ssize_t min_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
796 PyObject *result; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
797 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
798 /* Make sure both arguments are strings. */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
799 if (!(PyBytes_Check(a) && PyBytes_Check(b))) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
800 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE) && |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
801 (PyObject_IsInstance((PyObject*)a, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
802 (PyObject*)&PyUnicode_Type) || |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
803 PyObject_IsInstance((PyObject*)b, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
804 (PyObject*)&PyUnicode_Type))) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
805 if (PyErr_WarnEx(PyExc_BytesWarning, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
806 "Comparison between bytes and string", 1)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
807 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
808 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
809 result = Py_NotImplemented; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
810 goto out; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
811 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
812 if (a == b) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
813 switch (op) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
814 case Py_EQ:case Py_LE:case Py_GE: |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
815 result = Py_True; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
816 goto out; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
817 case Py_NE:case Py_LT:case Py_GT: |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
818 result = Py_False; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
819 goto out; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
820 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
821 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
822 if (op == Py_EQ) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
823 /* Supporting Py_NE here as well does not save |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
824 much time, since Py_NE is rarely used. */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
825 if (Py_SIZE(a) == Py_SIZE(b) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
826 && (a->ob_sval[0] == b->ob_sval[0] |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
827 && memcmp(a->ob_sval, b->ob_sval, Py_SIZE(a)) == 0)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
828 result = Py_True; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
829 } else { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
830 result = Py_False; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
831 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
832 goto out; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
833 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
834 len_a = Py_SIZE(a); len_b = Py_SIZE(b); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
835 min_len = (len_a < len_b) ? len_a : len_b; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
836 if (min_len > 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
837 c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
838 if (c==0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
839 c = memcmp(a->ob_sval, b->ob_sval, min_len); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
840 } else |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
841 c = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
842 if (c == 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
843 c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
844 switch (op) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
845 case Py_LT: c = c < 0; break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
846 case Py_LE: c = c <= 0; break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
847 case Py_EQ: assert(0); break; /* unreachable */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
848 case Py_NE: c = c != 0; break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
849 case Py_GT: c = c > 0; break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
850 case Py_GE: c = c >= 0; break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
851 default: |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
852 result = Py_NotImplemented; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
853 goto out; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
854 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
855 result = c ? Py_True : Py_False; |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
856 out: |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
857 Py_INCREF(result); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
858 return result; |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
859 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
860 |
|
65494
e8969fb822fc
make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents:
64564
diff
changeset
|
861 static Py_hash_t |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
862 bytes_hash(PyBytesObject *a) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
863 { |
|
73669
215690b782f8
Issue #13411: memoryview objects are now hashable when the underlying object is hashable.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73655
diff
changeset
|
864 if (a->ob_shash == -1) { |
|
215690b782f8
Issue #13411: memoryview objects are now hashable when the underlying object is hashable.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73655
diff
changeset
|
865 /* Can't fail */ |
|
215690b782f8
Issue #13411: memoryview objects are now hashable when the underlying object is hashable.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73655
diff
changeset
|
866 a->ob_shash = _Py_HashBytes((unsigned char *) a->ob_sval, Py_SIZE(a)); |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
867 } |
|
73669
215690b782f8
Issue #13411: memoryview objects are now hashable when the underlying object is hashable.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73655
diff
changeset
|
868 return a->ob_shash; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
869 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
870 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
871 static PyObject* |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
872 bytes_subscript(PyBytesObject* self, PyObject* item) |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
873 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
874 if (PyIndex_Check(item)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
875 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
876 if (i == -1 && PyErr_Occurred()) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
877 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
878 if (i < 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
879 i += PyBytes_GET_SIZE(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
880 if (i < 0 || i >= PyBytes_GET_SIZE(self)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
881 PyErr_SetString(PyExc_IndexError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
882 "index out of range"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
883 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
884 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
885 return PyLong_FromLong((unsigned char)self->ob_sval[i]); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
886 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
887 else if (PySlice_Check(item)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
888 Py_ssize_t start, stop, step, slicelength, cur, i; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
889 char* source_buf; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
890 char* result_buf; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
891 PyObject* result; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
892 |
|
66664
62b61abd02b8
Merge branches/pep-0384.
Martin v. Löwis <martin@v.loewis.de>
parents:
66605
diff
changeset
|
893 if (PySlice_GetIndicesEx(item, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
894 PyBytes_GET_SIZE(self), |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
895 &start, &stop, &step, &slicelength) < 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
896 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
897 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
898 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
899 if (slicelength <= 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
900 return PyBytes_FromStringAndSize("", 0); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
901 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
902 else if (start == 0 && step == 1 && |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
903 slicelength == PyBytes_GET_SIZE(self) && |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
904 PyBytes_CheckExact(self)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
905 Py_INCREF(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
906 return (PyObject *)self; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
907 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
908 else if (step == 1) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
909 return PyBytes_FromStringAndSize( |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
910 PyBytes_AS_STRING(self) + start, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
911 slicelength); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
912 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
913 else { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
914 source_buf = PyBytes_AS_STRING(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
915 result = PyBytes_FromStringAndSize(NULL, slicelength); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
916 if (result == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
917 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
918 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
919 result_buf = PyBytes_AS_STRING(result); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
920 for (cur = start, i = 0; i < slicelength; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
921 cur += step, i++) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
922 result_buf[i] = source_buf[cur]; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
923 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
924 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
925 return result; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
926 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
927 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
928 else { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
929 PyErr_Format(PyExc_TypeError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
930 "byte indices must be integers, not %.200s", |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
931 Py_TYPE(item)->tp_name); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
932 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
933 } |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
934 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
935 |
|
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:
42594
diff
changeset
|
936 static int |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
937 bytes_buffer_getbuffer(PyBytesObject *self, Py_buffer *view, int flags) |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
938 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
939 return PyBuffer_FillInfo(view, (PyObject*)self, (void *)self->ob_sval, Py_SIZE(self), |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
940 1, flags); |
|
9305
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
941 } |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
942 |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
943 static PySequenceMethods bytes_as_sequence = { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
944 (lenfunc)bytes_length, /*sq_length*/ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
945 (binaryfunc)bytes_concat, /*sq_concat*/ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
946 (ssizeargfunc)bytes_repeat, /*sq_repeat*/ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
947 (ssizeargfunc)bytes_item, /*sq_item*/ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
948 0, /*sq_slice*/ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
949 0, /*sq_ass_item*/ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
950 0, /*sq_ass_slice*/ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
951 (objobjproc)bytes_contains /*sq_contains*/ |
| 6 | 952 }; |
| 953 | |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
954 static PyMappingMethods bytes_as_mapping = { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
955 (lenfunc)bytes_length, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
956 (binaryfunc)bytes_subscript, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
957 0, |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
958 }; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
959 |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
960 static PyBufferProcs bytes_as_buffer = { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
961 (getbufferproc)bytes_buffer_getbuffer, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
962 NULL, |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
963 }; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
964 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
965 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
966 #define LEFTSTRIP 0 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
967 #define RIGHTSTRIP 1 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
968 #define BOTHSTRIP 2 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
969 |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
970 /* Arrays indexed by above */ |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
971 static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
972 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
973 #define STRIPNAME(i) (stripformat[i]+3) |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
974 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
975 PyDoc_STRVAR(split__doc__, |
|
75291
db968ac2b82c
#14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
75129
diff
changeset
|
976 "B.split(sep=None, maxsplit=-1) -> list of bytes\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
977 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
978 Return a list of the sections in B, using sep as the delimiter.\n\ |
|
47067
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46909
diff
changeset
|
979 If sep is not specified or is None, B is split on ASCII whitespace\n\ |
|
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46909
diff
changeset
|
980 characters (space, tab, return, newline, formfeed, vertical tab).\n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
981 If maxsplit is given, at most maxsplit splits are done."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
982 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
983 static PyObject * |
|
75291
db968ac2b82c
#14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
75129
diff
changeset
|
984 bytes_split(PyBytesObject *self, PyObject *args, PyObject *kwds) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
985 { |
|
75291
db968ac2b82c
#14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
75129
diff
changeset
|
986 static char *kwlist[] = {"sep", "maxsplit", 0}; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
987 Py_ssize_t len = PyBytes_GET_SIZE(self), n; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
988 Py_ssize_t maxsplit = -1; |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
989 const char *s = PyBytes_AS_STRING(self), *sub; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
990 Py_buffer vsub; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
991 PyObject *list, *subobj = Py_None; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
992 |
|
75291
db968ac2b82c
#14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
75129
diff
changeset
|
993 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
|
db968ac2b82c
#14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
75129
diff
changeset
|
994 kwlist, &subobj, &maxsplit)) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
995 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
996 if (maxsplit < 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
997 maxsplit = PY_SSIZE_T_MAX; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
998 if (subobj == Py_None) |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
999 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1000 if (_getbuffer(subobj, &vsub) < 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1001 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1002 sub = vsub.buf; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1003 n = vsub.len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1004 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1005 list = stringlib_split((PyObject*) self, s, len, sub, n, maxsplit); |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1006 PyBuffer_Release(&vsub); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1007 return list; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1008 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1009 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1010 PyDoc_STRVAR(partition__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1011 "B.partition(sep) -> (head, sep, tail)\n\ |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1012 \n\ |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1013 Search for the separator sep in B, and return the part before it,\n\ |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1014 the separator itself, and the part after it. If the separator is not\n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1015 found, returns B and two empty bytes objects."); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1016 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1017 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1018 bytes_partition(PyBytesObject *self, PyObject *sep_obj) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1019 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1020 const char *sep; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1021 Py_ssize_t sep_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1022 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1023 if (PyBytes_Check(sep_obj)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1024 sep = PyBytes_AS_STRING(sep_obj); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1025 sep_len = PyBytes_GET_SIZE(sep_obj); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1026 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1027 else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1028 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1029 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1030 return stringlib_partition( |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1031 (PyObject*) self, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1032 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1033 sep_obj, sep, sep_len |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1034 ); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1035 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1036 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1037 PyDoc_STRVAR(rpartition__doc__, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1038 "B.rpartition(sep) -> (head, sep, tail)\n\ |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1039 \n\ |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1040 Search for the separator sep in B, starting at the end of B,\n\ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1041 and return the part before it, the separator itself, and the\n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1042 part after it. If the separator is not found, returns two empty\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1043 bytes objects and B."); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1044 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1045 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1046 bytes_rpartition(PyBytesObject *self, PyObject *sep_obj) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1047 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1048 const char *sep; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1049 Py_ssize_t sep_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1050 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1051 if (PyBytes_Check(sep_obj)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1052 sep = PyBytes_AS_STRING(sep_obj); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1053 sep_len = PyBytes_GET_SIZE(sep_obj); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1054 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1055 else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1056 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1057 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1058 return stringlib_rpartition( |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1059 (PyObject*) self, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1060 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1061 sep_obj, sep, sep_len |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1062 ); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1063 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1064 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1065 PyDoc_STRVAR(rsplit__doc__, |
|
75291
db968ac2b82c
#14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
75129
diff
changeset
|
1066 "B.rsplit(sep=None, maxsplit=-1) -> list of bytes\n\ |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1067 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1068 Return a list of the sections in B, using sep as the delimiter,\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1069 starting at the end of B and working to the front.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1070 If sep is not given, B is split on ASCII whitespace characters\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1071 (space, tab, return, newline, formfeed, vertical tab).\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1072 If maxsplit is given, at most maxsplit splits are done."); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1073 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1074 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1075 static PyObject * |
|
75291
db968ac2b82c
#14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
75129
diff
changeset
|
1076 bytes_rsplit(PyBytesObject *self, PyObject *args, PyObject *kwds) |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1077 { |
|
75291
db968ac2b82c
#14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
75129
diff
changeset
|
1078 static char *kwlist[] = {"sep", "maxsplit", 0}; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1079 Py_ssize_t len = PyBytes_GET_SIZE(self), n; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1080 Py_ssize_t maxsplit = -1; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1081 const char *s = PyBytes_AS_STRING(self), *sub; |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1082 Py_buffer vsub; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1083 PyObject *list, *subobj = Py_None; |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1084 |
|
75291
db968ac2b82c
#14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
75129
diff
changeset
|
1085 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
|
db968ac2b82c
#14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
75129
diff
changeset
|
1086 kwlist, &subobj, &maxsplit)) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1087 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1088 if (maxsplit < 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1089 maxsplit = PY_SSIZE_T_MAX; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1090 if (subobj == Py_None) |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1091 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1092 if (_getbuffer(subobj, &vsub) < 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1093 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1094 sub = vsub.buf; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1095 n = vsub.len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1096 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1097 list = stringlib_rsplit((PyObject*) self, s, len, sub, n, maxsplit); |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1098 PyBuffer_Release(&vsub); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1099 return list; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1100 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1101 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1102 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1103 PyDoc_STRVAR(join__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1104 "B.join(iterable_of_bytes) -> bytes\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1105 \n\ |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1106 Concatenate any number of bytes objects, with B in between each pair.\n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1107 Example: b'.'.join([b'ab', b'pq', b'rs']) -> b'ab.pq.rs'."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1108 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1109 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1110 bytes_join(PyObject *self, PyObject *orig) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1111 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1112 char *sep = PyBytes_AS_STRING(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1113 const Py_ssize_t seplen = PyBytes_GET_SIZE(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1114 PyObject *res = NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1115 char *p; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1116 Py_ssize_t seqlen = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1117 size_t sz = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1118 Py_ssize_t i; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1119 PyObject *seq, *item; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1120 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1121 seq = PySequence_Fast(orig, ""); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1122 if (seq == NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1123 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1124 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1125 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1126 seqlen = PySequence_Size(seq); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1127 if (seqlen == 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1128 Py_DECREF(seq); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1129 return PyBytes_FromString(""); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1130 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1131 if (seqlen == 1) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1132 item = PySequence_Fast_GET_ITEM(seq, 0); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1133 if (PyBytes_CheckExact(item)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1134 Py_INCREF(item); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1135 Py_DECREF(seq); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1136 return item; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1137 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1138 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1139 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1140 /* There are at least two things to join, or else we have a subclass |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1141 * of the builtin types in the sequence. |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1142 * Do a pre-pass to figure out the total amount of space we'll |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1143 * need (sz), and see whether all argument are bytes. |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1144 */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1145 /* XXX Shouldn't we use _getbuffer() on these items instead? */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1146 for (i = 0; i < seqlen; i++) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1147 const size_t old_sz = sz; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1148 item = PySequence_Fast_GET_ITEM(seq, i); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1149 if (!PyBytes_Check(item) && !PyByteArray_Check(item)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1150 PyErr_Format(PyExc_TypeError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1151 "sequence item %zd: expected bytes," |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1152 " %.80s found", |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1153 i, Py_TYPE(item)->tp_name); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1154 Py_DECREF(seq); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1155 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1156 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1157 sz += Py_SIZE(item); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1158 if (i != 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1159 sz += seplen; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1160 if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1161 PyErr_SetString(PyExc_OverflowError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1162 "join() result is too long for bytes"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1163 Py_DECREF(seq); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1164 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1165 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1166 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1167 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1168 /* Allocate result space. */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1169 res = PyBytes_FromStringAndSize((char*)NULL, sz); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1170 if (res == NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1171 Py_DECREF(seq); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1172 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1173 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1174 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1175 /* Catenate everything. */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1176 /* I'm not worried about a PyByteArray item growing because there's |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1177 nowhere in this function where we release the GIL. */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1178 p = PyBytes_AS_STRING(res); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1179 for (i = 0; i < seqlen; ++i) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1180 size_t n; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1181 char *q; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1182 if (i) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1183 Py_MEMCPY(p, sep, seplen); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1184 p += seplen; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1185 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1186 item = PySequence_Fast_GET_ITEM(seq, i); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1187 n = Py_SIZE(item); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1188 if (PyBytes_Check(item)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1189 q = PyBytes_AS_STRING(item); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1190 else |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1191 q = PyByteArray_AS_STRING(item); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1192 Py_MEMCPY(p, q, n); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1193 p += n; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1194 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1195 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1196 Py_DECREF(seq); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1197 return res; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1198 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1199 |
|
18130
8eea2efb34cd
Reformat decl of new _PyString_Join. Add NEWS blurb about repr() speedup.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
1200 PyObject * |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1201 _PyBytes_Join(PyObject *sep, PyObject *x) |
|
18129
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1202 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1203 assert(sep != NULL && PyBytes_Check(sep)); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1204 assert(x != NULL); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1205 return bytes_join(sep, x); |
|
18129
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1206 } |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1207 |
|
58289
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
1208 /* helper macro to fixup start/end slice values */ |
|
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
1209 #define ADJUST_INDICES(start, end, len) \ |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1210 if (end > len) \ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1211 end = len; \ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1212 else if (end < 0) { \ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1213 end += len; \ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1214 if (end < 0) \ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1215 end = 0; \ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1216 } \ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1217 if (start < 0) { \ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1218 start += len; \ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1219 if (start < 0) \ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1220 start = 0; \ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1221 } |
|
23850
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1222 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1223 Py_LOCAL_INLINE(Py_ssize_t) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1224 bytes_find_internal(PyBytesObject *self, PyObject *args, int dir) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1225 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1226 PyObject *subobj; |
|
73038
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1227 char byte; |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1228 Py_buffer subbuf; |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1229 const char *sub; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1230 Py_ssize_t sub_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1231 Py_ssize_t start=0, end=PY_SSIZE_T_MAX; |
|
73038
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1232 Py_ssize_t res; |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1233 |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1234 if (!stringlib_parse_args_finds_byte("find/rfind/index/rindex", |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1235 args, &subobj, &byte, &start, &end)) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1236 return -2; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1237 |
|
73038
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1238 if (subobj) { |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1239 if (_getbuffer(subobj, &subbuf) < 0) |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1240 return -2; |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1241 |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1242 sub = subbuf.buf; |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1243 sub_len = subbuf.len; |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1244 } |
|
73038
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1245 else { |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1246 sub = &byte; |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1247 sub_len = 1; |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1248 } |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1249 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1250 if (dir > 0) |
|
73038
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1251 res = stringlib_find_slice( |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1252 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1253 sub, sub_len, start, end); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1254 else |
|
73038
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1255 res = stringlib_rfind_slice( |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1256 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1257 sub, sub_len, start, end); |
|
73038
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1258 |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1259 if (subobj) |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1260 PyBuffer_Release(&subbuf); |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1261 |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1262 return res; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1263 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1264 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1265 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1266 PyDoc_STRVAR(find__doc__, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1267 "B.find(sub[, start[, end]]) -> int\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1268 \n\ |
|
58289
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
1269 Return the lowest index in B where substring sub is found,\n\ |
|
71527
c3aebd01a033
Fix closes Issue12621 - Fix docstrings of find and rfind methods of bytes/bytearry/unicodeobject.
Senthil Kumaran <senthil@uthcode.com>
parents:
69564
diff
changeset
|
1270 such that sub is contained within B[start:end]. Optional\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1271 arguments start and end are interpreted as in slice notation.\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1272 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1273 Return -1 on failure."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1274 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1275 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1276 bytes_find(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1277 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1278 Py_ssize_t result = bytes_find_internal(self, args, +1); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1279 if (result == -2) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1280 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1281 return PyLong_FromSsize_t(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1282 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1283 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1284 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1285 PyDoc_STRVAR(index__doc__, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1286 "B.index(sub[, start[, end]]) -> int\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1287 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1288 Like B.find() but raise ValueError when the substring is not found."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1289 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1290 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1291 bytes_index(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1292 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1293 Py_ssize_t result = bytes_find_internal(self, args, +1); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1294 if (result == -2) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1295 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1296 if (result == -1) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1297 PyErr_SetString(PyExc_ValueError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1298 "substring not found"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1299 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1300 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1301 return PyLong_FromSsize_t(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1302 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1303 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1304 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1305 PyDoc_STRVAR(rfind__doc__, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1306 "B.rfind(sub[, start[, end]]) -> int\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1307 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1308 Return the highest index in B where substring sub is found,\n\ |
|
71527
c3aebd01a033
Fix closes Issue12621 - Fix docstrings of find and rfind methods of bytes/bytearry/unicodeobject.
Senthil Kumaran <senthil@uthcode.com>
parents:
69564
diff
changeset
|
1309 such that sub is contained within B[start:end]. Optional\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1310 arguments start and end are interpreted as in slice notation.\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1311 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1312 Return -1 on failure."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1313 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1314 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1315 bytes_rfind(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1316 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1317 Py_ssize_t result = bytes_find_internal(self, args, -1); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1318 if (result == -2) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1319 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1320 return PyLong_FromSsize_t(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1321 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1322 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1323 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1324 PyDoc_STRVAR(rindex__doc__, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1325 "B.rindex(sub[, start[, end]]) -> int\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1326 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1327 Like B.rfind() but raise ValueError when the substring is not found."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1328 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1329 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1330 bytes_rindex(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1331 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1332 Py_ssize_t result = bytes_find_internal(self, args, -1); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1333 if (result == -2) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1334 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1335 if (result == -1) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1336 PyErr_SetString(PyExc_ValueError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1337 "substring not found"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1338 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1339 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1340 return PyLong_FromSsize_t(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1341 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1342 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1343 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1344 Py_LOCAL_INLINE(PyObject *) |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1345 do_xstrip(PyBytesObject *self, int striptype, PyObject *sepobj) |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1346 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1347 Py_buffer vsep; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1348 char *s = PyBytes_AS_STRING(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1349 Py_ssize_t len = PyBytes_GET_SIZE(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1350 char *sep; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1351 Py_ssize_t seplen; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1352 Py_ssize_t i, j; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1353 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1354 if (_getbuffer(sepobj, &vsep) < 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1355 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1356 sep = vsep.buf; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1357 seplen = vsep.len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1358 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1359 i = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1360 if (striptype != RIGHTSTRIP) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1361 while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1362 i++; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1363 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1364 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1365 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1366 j = len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1367 if (striptype != LEFTSTRIP) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1368 do { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1369 j--; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1370 } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen)); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1371 j++; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1372 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1373 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1374 PyBuffer_Release(&vsep); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1375 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1376 if (i == 0 && j == len && PyBytes_CheckExact(self)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1377 Py_INCREF(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1378 return (PyObject*)self; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1379 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1380 else |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1381 return PyBytes_FromStringAndSize(s+i, j-i); |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1382 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1383 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1384 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1385 Py_LOCAL_INLINE(PyObject *) |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1386 do_strip(PyBytesObject *self, int striptype) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1387 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1388 char *s = PyBytes_AS_STRING(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1389 Py_ssize_t len = PyBytes_GET_SIZE(self), i, j; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1390 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1391 i = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1392 if (striptype != RIGHTSTRIP) { |
|
65980
d31eaec883eb
Issue #10288: The deprecated family of "char"-handling macros
David Malcolm <dmalcolm@redhat.com>
parents:
65494
diff
changeset
|
1393 while (i < len && Py_ISSPACE(s[i])) { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1394 i++; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1395 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1396 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1397 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1398 j = len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1399 if (striptype != LEFTSTRIP) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1400 do { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1401 j--; |
|
65980
d31eaec883eb
Issue #10288: The deprecated family of "char"-handling macros
David Malcolm <dmalcolm@redhat.com>
parents:
65494
diff
changeset
|
1402 } while (j >= i && Py_ISSPACE(s[j])); |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1403 j++; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1404 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1405 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1406 if (i == 0 && j == len && PyBytes_CheckExact(self)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1407 Py_INCREF(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1408 return (PyObject*)self; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1409 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1410 else |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1411 return PyBytes_FromStringAndSize(s+i, j-i); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1412 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1413 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1414 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1415 Py_LOCAL_INLINE(PyObject *) |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1416 do_argstrip(PyBytesObject *self, int striptype, PyObject *args) |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1417 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1418 PyObject *sep = NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1419 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1420 if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1421 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1422 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1423 if (sep != NULL && sep != Py_None) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1424 return do_xstrip(self, striptype, sep); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1425 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1426 return do_strip(self, striptype); |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1427 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1428 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1429 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1430 PyDoc_STRVAR(strip__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1431 "B.strip([bytes]) -> bytes\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1432 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1433 Strip leading and trailing bytes contained in the argument.\n\ |
|
74579
960d93deb8c2
Fix #13834: strip() strips leading and trailing whitespace.
Georg Brandl <georg@python.org>
parents:
74415
diff
changeset
|
1434 If the argument is omitted, strip leading and trailing ASCII whitespace."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1435 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1436 bytes_strip(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1437 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1438 if (PyTuple_GET_SIZE(args) == 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1439 return do_strip(self, BOTHSTRIP); /* Common case */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1440 else |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1441 return do_argstrip(self, BOTHSTRIP, args); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1442 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1443 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1444 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1445 PyDoc_STRVAR(lstrip__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1446 "B.lstrip([bytes]) -> bytes\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1447 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1448 Strip leading bytes contained in the argument.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1449 If the argument is omitted, strip leading ASCII whitespace."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1450 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1451 bytes_lstrip(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1452 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1453 if (PyTuple_GET_SIZE(args) == 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1454 return do_strip(self, LEFTSTRIP); /* Common case */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1455 else |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1456 return do_argstrip(self, LEFTSTRIP, args); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1457 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1458 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1459 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1460 PyDoc_STRVAR(rstrip__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1461 "B.rstrip([bytes]) -> bytes\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1462 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1463 Strip trailing bytes contained in the argument.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1464 If the argument is omitted, strip trailing ASCII whitespace."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1465 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1466 bytes_rstrip(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1467 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1468 if (PyTuple_GET_SIZE(args) == 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1469 return do_strip(self, RIGHTSTRIP); /* Common case */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1470 else |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1471 return do_argstrip(self, RIGHTSTRIP, args); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1472 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1473 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1474 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1475 PyDoc_STRVAR(count__doc__, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1476 "B.count(sub[, start[, end]]) -> int\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1477 \n\ |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1478 Return the number of non-overlapping occurrences of substring sub in\n\ |
|
58289
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
1479 string B[start:end]. Optional arguments start and end are interpreted\n\ |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1480 as in slice notation."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1481 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1482 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1483 bytes_count(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1484 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1485 PyObject *sub_obj; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1486 const char *str = PyBytes_AS_STRING(self), *sub; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1487 Py_ssize_t sub_len; |
|
73038
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1488 char byte; |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1489 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1490 |
|
73038
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1491 Py_buffer vsub; |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1492 PyObject *count_obj; |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1493 |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1494 if (!stringlib_parse_args_finds_byte("count", args, &sub_obj, &byte, |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1495 &start, &end)) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1496 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1497 |
|
73038
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1498 if (sub_obj) { |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1499 if (_getbuffer(sub_obj, &vsub) < 0) |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1500 return NULL; |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1501 |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1502 sub = vsub.buf; |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1503 sub_len = vsub.len; |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1504 } |
|
73038
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1505 else { |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1506 sub = &byte; |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1507 sub_len = 1; |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1508 } |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1509 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1510 ADJUST_INDICES(start, end, PyBytes_GET_SIZE(self)); |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1511 |
|
73038
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1512 count_obj = PyLong_FromSsize_t( |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1513 stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1514 ); |
|
73038
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1515 |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1516 if (sub_obj) |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1517 PyBuffer_Release(&vsub); |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1518 |
|
c1effa2cdd20
Issue #12170: The count(), find(), rfind(), index() and rindex() methods
Antoine Pitrou <solipsis@pitrou.net>
parents:
72918
diff
changeset
|
1519 return count_obj; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1520 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1521 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1522 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1523 PyDoc_STRVAR(translate__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1524 "B.translate(table[, deletechars]) -> bytes\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1525 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1526 Return a copy of B, where all characters occurring in the\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1527 optional argument deletechars are removed, and the remaining\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1528 characters have been mapped through the given translation\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1529 table, which must be a bytes object of length 256."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1530 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1531 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1532 bytes_translate(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1533 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1534 register char *input, *output; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1535 const char *table; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1536 register Py_ssize_t i, c, changed = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1537 PyObject *input_obj = (PyObject*)self; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1538 const char *output_start, *del_table=NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1539 Py_ssize_t inlen, tablen, dellen = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1540 PyObject *result; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1541 int trans_table[256]; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1542 PyObject *tableobj, *delobj = NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1543 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1544 if (!PyArg_UnpackTuple(args, "translate", 1, 2, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1545 &tableobj, &delobj)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1546 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1547 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1548 if (PyBytes_Check(tableobj)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1549 table = PyBytes_AS_STRING(tableobj); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1550 tablen = PyBytes_GET_SIZE(tableobj); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1551 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1552 else if (tableobj == Py_None) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1553 table = NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1554 tablen = 256; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1555 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1556 else if (PyObject_AsCharBuffer(tableobj, &table, &tablen)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1557 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1558 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1559 if (tablen != 256) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1560 PyErr_SetString(PyExc_ValueError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1561 "translation table must be 256 characters long"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1562 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1563 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1564 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1565 if (delobj != NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1566 if (PyBytes_Check(delobj)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1567 del_table = PyBytes_AS_STRING(delobj); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1568 dellen = PyBytes_GET_SIZE(delobj); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1569 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1570 else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1571 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1572 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1573 else { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1574 del_table = NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1575 dellen = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1576 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1577 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1578 inlen = PyBytes_GET_SIZE(input_obj); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1579 result = PyBytes_FromStringAndSize((char *)NULL, inlen); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1580 if (result == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1581 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1582 output_start = output = PyBytes_AsString(result); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1583 input = PyBytes_AS_STRING(input_obj); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1584 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1585 if (dellen == 0 && table != NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1586 /* If no deletions are required, use faster code */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1587 for (i = inlen; --i >= 0; ) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1588 c = Py_CHARMASK(*input++); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1589 if (Py_CHARMASK((*output++ = table[c])) != c) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1590 changed = 1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1591 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1592 if (changed || !PyBytes_CheckExact(input_obj)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1593 return result; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1594 Py_DECREF(result); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1595 Py_INCREF(input_obj); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1596 return input_obj; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1597 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1598 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1599 if (table == NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1600 for (i = 0; i < 256; i++) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1601 trans_table[i] = Py_CHARMASK(i); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1602 } else { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1603 for (i = 0; i < 256; i++) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1604 trans_table[i] = Py_CHARMASK(table[i]); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1605 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1606 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1607 for (i = 0; i < dellen; i++) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1608 trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1609 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1610 for (i = inlen; --i >= 0; ) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1611 c = Py_CHARMASK(*input++); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1612 if (trans_table[c] != -1) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1613 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1614 continue; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1615 changed = 1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1616 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1617 if (!changed && PyBytes_CheckExact(input_obj)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1618 Py_DECREF(result); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1619 Py_INCREF(input_obj); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1620 return input_obj; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1621 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1622 /* Fix the size of the resulting string */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1623 if (inlen > 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1624 _PyBytes_Resize(&result, output - output_start); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1625 return result; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1626 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1627 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1628 |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1629 static PyObject * |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1630 bytes_maketrans(PyObject *null, PyObject *args) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1631 { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1632 return _Py_bytes_maketrans(args); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1633 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1634 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1635 /* find and count characters and substrings */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1636 |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1637 #define findchar(target, target_len, c) \ |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1638 ((char *)memchr((const void *)(target), c, target_len)) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1639 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1640 /* String ops must return a string. */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1641 /* If the object is subclass of string, create a copy */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1642 Py_LOCAL(PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1643 return_self(PyBytesObject *self) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1644 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1645 if (PyBytes_CheckExact(self)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1646 Py_INCREF(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1647 return self; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1648 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1649 return (PyBytesObject *)PyBytes_FromStringAndSize( |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1650 PyBytes_AS_STRING(self), |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1651 PyBytes_GET_SIZE(self)); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1652 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1653 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1654 Py_LOCAL_INLINE(Py_ssize_t) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1655 countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1656 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1657 Py_ssize_t count=0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1658 const char *start=target; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1659 const char *end=target+target_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1660 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1661 while ( (start=findchar(start, end-start, c)) != NULL ) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1662 count++; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1663 if (count >= maxcount) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1664 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1665 start += 1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1666 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1667 return count; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1668 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1669 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1670 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1671 /* Algorithms for different cases of string replacement */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1672 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1673 /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1674 Py_LOCAL(PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1675 replace_interleave(PyBytesObject *self, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1676 const char *to_s, Py_ssize_t to_len, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1677 Py_ssize_t maxcount) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1678 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1679 char *self_s, *result_s; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1680 Py_ssize_t self_len, result_len; |
|
63913
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
1681 Py_ssize_t count, i; |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1682 PyBytesObject *result; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1683 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1684 self_len = PyBytes_GET_SIZE(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1685 |
|
63913
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
1686 /* 1 at the end plus 1 after every character; |
|
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
1687 count = min(maxcount, self_len + 1) */ |
|
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
1688 if (maxcount <= self_len) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1689 count = maxcount; |
|
63913
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
1690 else |
|
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
1691 /* Can't overflow: self_len + 1 <= maxcount <= PY_SSIZE_T_MAX. */ |
|
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
1692 count = self_len + 1; |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1693 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1694 /* Check for overflow */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1695 /* result_len = count * to_len + self_len; */ |
|
63913
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
1696 assert(count > 0); |
|
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
1697 if (to_len > (PY_SSIZE_T_MAX - self_len) / count) { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1698 PyErr_SetString(PyExc_OverflowError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1699 "replacement bytes are too long"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1700 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1701 } |
|
63913
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
1702 result_len = count * to_len + self_len; |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1703 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1704 if (! (result = (PyBytesObject *) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1705 PyBytes_FromStringAndSize(NULL, result_len)) ) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1706 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1707 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1708 self_s = PyBytes_AS_STRING(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1709 result_s = PyBytes_AS_STRING(result); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1710 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1711 /* TODO: special case single character, which doesn't need memcpy */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1712 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1713 /* Lay the first one down (guaranteed this will occur) */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1714 Py_MEMCPY(result_s, to_s, to_len); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1715 result_s += to_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1716 count -= 1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1717 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1718 for (i=0; i<count; i++) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1719 *result_s++ = *self_s++; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1720 Py_MEMCPY(result_s, to_s, to_len); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1721 result_s += to_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1722 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1723 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1724 /* Copy the rest of the original string */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1725 Py_MEMCPY(result_s, self_s, self_len-i); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1726 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1727 return result; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1728 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1729 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1730 /* Special case for deleting a single character */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1731 /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1732 Py_LOCAL(PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1733 replace_delete_single_character(PyBytesObject *self, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1734 char from_c, Py_ssize_t maxcount) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1735 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1736 char *self_s, *result_s; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1737 char *start, *next, *end; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1738 Py_ssize_t self_len, result_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1739 Py_ssize_t count; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1740 PyBytesObject *result; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1741 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1742 self_len = PyBytes_GET_SIZE(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1743 self_s = PyBytes_AS_STRING(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1744 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1745 count = countchar(self_s, self_len, from_c, maxcount); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1746 if (count == 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1747 return return_self(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1748 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1749 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1750 result_len = self_len - count; /* from_len == 1 */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1751 assert(result_len>=0); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1752 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1753 if ( (result = (PyBytesObject *) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1754 PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1755 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1756 result_s = PyBytes_AS_STRING(result); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1757 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1758 start = self_s; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1759 end = self_s + self_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1760 while (count-- > 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1761 next = findchar(start, end-start, from_c); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1762 if (next == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1763 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1764 Py_MEMCPY(result_s, start, next-start); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1765 result_s += (next-start); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1766 start = next+1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1767 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1768 Py_MEMCPY(result_s, start, end-start); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1769 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1770 return result; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1771 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1772 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1773 /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1774 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1775 Py_LOCAL(PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1776 replace_delete_substring(PyBytesObject *self, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1777 const char *from_s, Py_ssize_t from_len, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1778 Py_ssize_t maxcount) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1779 char *self_s, *result_s; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1780 char *start, *next, *end; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1781 Py_ssize_t self_len, result_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1782 Py_ssize_t count, offset; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1783 PyBytesObject *result; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1784 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1785 self_len = PyBytes_GET_SIZE(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1786 self_s = PyBytes_AS_STRING(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1787 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1788 count = stringlib_count(self_s, self_len, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1789 from_s, from_len, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1790 maxcount); |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1791 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1792 if (count == 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1793 /* no matches */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1794 return return_self(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1795 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1796 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1797 result_len = self_len - (count * from_len); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1798 assert (result_len>=0); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1799 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1800 if ( (result = (PyBytesObject *) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1801 PyBytes_FromStringAndSize(NULL, result_len)) == NULL ) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1802 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1803 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1804 result_s = PyBytes_AS_STRING(result); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1805 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1806 start = self_s; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1807 end = self_s + self_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1808 while (count-- > 0) { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1809 offset = stringlib_find(start, end-start, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1810 from_s, from_len, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1811 0); |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1812 if (offset == -1) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1813 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1814 next = start + offset; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1815 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1816 Py_MEMCPY(result_s, start, next-start); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1817 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1818 result_s += (next-start); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1819 start = next+from_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1820 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1821 Py_MEMCPY(result_s, start, end-start); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1822 return result; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1823 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1824 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1825 /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1826 Py_LOCAL(PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1827 replace_single_character_in_place(PyBytesObject *self, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1828 char from_c, char to_c, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1829 Py_ssize_t maxcount) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1830 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1831 char *self_s, *result_s, *start, *end, *next; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1832 Py_ssize_t self_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1833 PyBytesObject *result; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1834 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1835 /* The result string will be the same size */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1836 self_s = PyBytes_AS_STRING(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1837 self_len = PyBytes_GET_SIZE(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1838 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1839 next = findchar(self_s, self_len, from_c); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1840 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1841 if (next == NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1842 /* No matches; return the original string */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1843 return return_self(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1844 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1845 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1846 /* Need to make a new string */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1847 result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1848 if (result == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1849 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1850 result_s = PyBytes_AS_STRING(result); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1851 Py_MEMCPY(result_s, self_s, self_len); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1852 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1853 /* change everything in-place, starting with this one */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1854 start = result_s + (next-self_s); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1855 *start = to_c; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1856 start++; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1857 end = result_s + self_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1858 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1859 while (--maxcount > 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1860 next = findchar(start, end-start, from_c); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1861 if (next == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1862 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1863 *next = to_c; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1864 start = next+1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1865 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1866 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1867 return result; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1868 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1869 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1870 /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1871 Py_LOCAL(PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1872 replace_substring_in_place(PyBytesObject *self, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1873 const char *from_s, Py_ssize_t from_len, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1874 const char *to_s, Py_ssize_t to_len, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1875 Py_ssize_t maxcount) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1876 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1877 char *result_s, *start, *end; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1878 char *self_s; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1879 Py_ssize_t self_len, offset; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1880 PyBytesObject *result; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1881 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1882 /* The result string will be the same size */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1883 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1884 self_s = PyBytes_AS_STRING(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1885 self_len = PyBytes_GET_SIZE(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1886 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1887 offset = stringlib_find(self_s, self_len, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1888 from_s, from_len, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1889 0); |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1890 if (offset == -1) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1891 /* No matches; return the original string */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1892 return return_self(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1893 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1894 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1895 /* Need to make a new string */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1896 result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1897 if (result == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1898 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1899 result_s = PyBytes_AS_STRING(result); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1900 Py_MEMCPY(result_s, self_s, self_len); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1901 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1902 /* change everything in-place, starting with this one */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1903 start = result_s + offset; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1904 Py_MEMCPY(start, to_s, from_len); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1905 start += from_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1906 end = result_s + self_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1907 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1908 while ( --maxcount > 0) { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1909 offset = stringlib_find(start, end-start, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1910 from_s, from_len, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
1911 0); |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1912 if (offset==-1) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1913 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1914 Py_MEMCPY(start+offset, to_s, from_len); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1915 start += offset+from_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1916 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1917 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1918 return result; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1919 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1920 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1921 /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1922 Py_LOCAL(PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1923 replace_single_character(PyBytesObject *self, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1924 char from_c, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1925 const char *to_s, Py_ssize_t to_len, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1926 Py_ssize_t maxcount) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1927 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1928 char *self_s, *result_s; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1929 char *start, *next, *end; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1930 Py_ssize_t self_len, result_len; |
|
63913
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
1931 Py_ssize_t count; |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1932 PyBytesObject *result; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1933 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1934 self_s = PyBytes_AS_STRING(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1935 self_len = PyBytes_GET_SIZE(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1936 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1937 count = countchar(self_s, self_len, from_c, maxcount); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1938 if (count == 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1939 /* no matches, return unchanged */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1940 return return_self(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1941 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1942 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1943 /* use the difference between current and new, hence the "-1" */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1944 /* result_len = self_len + count * (to_len-1) */ |
|
63913
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
1945 assert(count > 0); |
|
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
1946 if (to_len - 1 > (PY_SSIZE_T_MAX - self_len) / count) { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1947 PyErr_SetString(PyExc_OverflowError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1948 "replacement bytes are too long"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1949 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1950 } |
|
63913
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
1951 result_len = self_len + count * (to_len - 1); |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1952 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1953 if ( (result = (PyBytesObject *) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1954 PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1955 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1956 result_s = PyBytes_AS_STRING(result); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1957 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1958 start = self_s; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1959 end = self_s + self_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1960 while (count-- > 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1961 next = findchar(start, end-start, from_c); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1962 if (next == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1963 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1964 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1965 if (next == start) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1966 /* replace with the 'to' */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1967 Py_MEMCPY(result_s, to_s, to_len); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1968 result_s += to_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1969 start += 1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1970 } else { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1971 /* copy the unchanged old then the 'to' */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1972 Py_MEMCPY(result_s, start, next-start); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1973 result_s += (next-start); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1974 Py_MEMCPY(result_s, to_s, to_len); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1975 result_s += to_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1976 start = next+1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1977 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1978 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1979 /* Copy the remainder of the remaining string */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1980 Py_MEMCPY(result_s, start, end-start); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1981 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1982 return result; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1983 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1984 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1985 /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1986 Py_LOCAL(PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1987 replace_substring(PyBytesObject *self, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1988 const char *from_s, Py_ssize_t from_len, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1989 const char *to_s, Py_ssize_t to_len, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1990 Py_ssize_t maxcount) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1991 char *self_s, *result_s; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1992 char *start, *next, *end; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1993 Py_ssize_t self_len, result_len; |
|
63913
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
1994 Py_ssize_t count, offset; |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1995 PyBytesObject *result; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1996 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1997 self_s = PyBytes_AS_STRING(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1998 self_len = PyBytes_GET_SIZE(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
1999 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2000 count = stringlib_count(self_s, self_len, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2001 from_s, from_len, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2002 maxcount); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2003 |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2004 if (count == 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2005 /* no matches, return unchanged */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2006 return return_self(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2007 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2008 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2009 /* Check for overflow */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2010 /* result_len = self_len + count * (to_len-from_len) */ |
|
63913
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
2011 assert(count > 0); |
|
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
2012 if (to_len - from_len > (PY_SSIZE_T_MAX - self_len) / count) { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2013 PyErr_SetString(PyExc_OverflowError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2014 "replacement bytes are too long"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2015 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2016 } |
|
63913
7dd5f5cfa179
Issue #9530: Fix undefined-behaviour-inducing overflow checks in bytes and bytearray implementations.
Mark Dickinson <dickinsm@gmail.com>
parents:
63869
diff
changeset
|
2017 result_len = self_len + count * (to_len-from_len); |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2018 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2019 if ( (result = (PyBytesObject *) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2020 PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2021 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2022 result_s = PyBytes_AS_STRING(result); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2023 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2024 start = self_s; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2025 end = self_s + self_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2026 while (count-- > 0) { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2027 offset = stringlib_find(start, end-start, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2028 from_s, from_len, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2029 0); |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2030 if (offset == -1) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2031 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2032 next = start+offset; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2033 if (next == start) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2034 /* replace with the 'to' */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2035 Py_MEMCPY(result_s, to_s, to_len); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2036 result_s += to_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2037 start += from_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2038 } else { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2039 /* copy the unchanged old then the 'to' */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2040 Py_MEMCPY(result_s, start, next-start); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2041 result_s += (next-start); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2042 Py_MEMCPY(result_s, to_s, to_len); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2043 result_s += to_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2044 start = next+from_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2045 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2046 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2047 /* Copy the remainder of the remaining string */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2048 Py_MEMCPY(result_s, start, end-start); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2049 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2050 return result; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2051 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2052 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2053 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2054 Py_LOCAL(PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2055 replace(PyBytesObject *self, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2056 const char *from_s, Py_ssize_t from_len, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2057 const char *to_s, Py_ssize_t to_len, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2058 Py_ssize_t maxcount) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2059 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2060 if (maxcount < 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2061 maxcount = PY_SSIZE_T_MAX; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2062 } else if (maxcount == 0 || PyBytes_GET_SIZE(self) == 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2063 /* nothing to do; return the original string */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2064 return return_self(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2065 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2066 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2067 if (maxcount == 0 || |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2068 (from_len == 0 && to_len == 0)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2069 /* nothing to do; return the original string */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2070 return return_self(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2071 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2072 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2073 /* Handle zero-length special cases */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2074 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2075 if (from_len == 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2076 /* insert the 'to' string everywhere. */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2077 /* >>> "Python".replace("", ".") */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2078 /* '.P.y.t.h.o.n.' */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2079 return replace_interleave(self, to_s, to_len, maxcount); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2080 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2081 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2082 /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2083 /* point for an empty self string to generate a non-empty string */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2084 /* Special case so the remaining code always gets a non-empty string */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2085 if (PyBytes_GET_SIZE(self) == 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2086 return return_self(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2087 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2088 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2089 if (to_len == 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2090 /* delete all occurrences of 'from' string */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2091 if (from_len == 1) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2092 return replace_delete_single_character( |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2093 self, from_s[0], maxcount); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2094 } else { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2095 return replace_delete_substring(self, from_s, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2096 from_len, maxcount); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2097 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2098 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2099 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2100 /* Handle special case where both strings have the same length */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2101 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2102 if (from_len == to_len) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2103 if (from_len == 1) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2104 return replace_single_character_in_place( |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2105 self, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2106 from_s[0], |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2107 to_s[0], |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2108 maxcount); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2109 } else { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2110 return replace_substring_in_place( |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2111 self, from_s, from_len, to_s, to_len, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2112 maxcount); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2113 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2114 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2115 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2116 /* Otherwise use the more generic algorithms */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2117 if (from_len == 1) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2118 return replace_single_character(self, from_s[0], |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2119 to_s, to_len, maxcount); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2120 } else { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2121 /* len('from')>=2, len('to')>=1 */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2122 return replace_substring(self, from_s, from_len, to_s, to_len, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2123 maxcount); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2124 } |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2125 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2126 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2127 PyDoc_STRVAR(replace__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2128 "B.replace(old, new[, count]) -> bytes\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2129 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2130 Return a copy of B with all occurrences of subsection\n\ |
|
30751
6fdde96a7222
Avoid confusing name for the 3rd argument to str.replace().
Fred Drake <fdrake@acm.org>
parents:
30706
diff
changeset
|
2131 old replaced by new. If the optional argument count is\n\ |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2132 given, only first count occurances are replaced."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2133 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2134 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2135 bytes_replace(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2136 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2137 Py_ssize_t count = -1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2138 PyObject *from, *to; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2139 const char *from_s, *to_s; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2140 Py_ssize_t from_len, to_len; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2141 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2142 if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2143 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2144 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2145 if (PyBytes_Check(from)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2146 from_s = PyBytes_AS_STRING(from); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2147 from_len = PyBytes_GET_SIZE(from); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2148 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2149 else if (PyObject_AsCharBuffer(from, &from_s, &from_len)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2150 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2151 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2152 if (PyBytes_Check(to)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2153 to_s = PyBytes_AS_STRING(to); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2154 to_len = PyBytes_GET_SIZE(to); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2155 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2156 else if (PyObject_AsCharBuffer(to, &to_s, &to_len)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2157 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2158 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2159 return (PyObject *)replace((PyBytesObject *) self, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2160 from_s, from_len, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2161 to_s, to_len, count); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2162 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2163 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2164 /** End DALKE **/ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2165 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2166 /* Matches the end (direction >= 0) or start (direction < 0) of self |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2167 * against substr, using the start and end arguments. Returns |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2168 * -1 on error, 0 if not found and 1 if found. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2169 */ |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2170 Py_LOCAL(int) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2171 _bytes_tailmatch(PyBytesObject *self, PyObject *substr, Py_ssize_t start, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2172 Py_ssize_t end, int direction) |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2173 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2174 Py_ssize_t len = PyBytes_GET_SIZE(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2175 Py_ssize_t slen; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2176 const char* sub; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2177 const char* str; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2178 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2179 if (PyBytes_Check(substr)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2180 sub = PyBytes_AS_STRING(substr); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2181 slen = PyBytes_GET_SIZE(substr); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2182 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2183 else if (PyObject_AsCharBuffer(substr, &sub, &slen)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2184 return -1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2185 str = PyBytes_AS_STRING(self); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2186 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2187 ADJUST_INDICES(start, end, len); |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2188 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2189 if (direction < 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2190 /* startswith */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2191 if (start+slen > len) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2192 return 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2193 } else { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2194 /* endswith */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2195 if (end-start < slen || start > len) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2196 return 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2197 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2198 if (end-slen > start) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2199 start = end - slen; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2200 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2201 if (end-start >= slen) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2202 return ! memcmp(str+start, sub, slen); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2203 return 0; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2204 } |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2205 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2206 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2207 PyDoc_STRVAR(startswith__doc__, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2208 "B.startswith(prefix[, start[, end]]) -> bool\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2209 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2210 Return True if B starts with the specified prefix, False otherwise.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2211 With optional start, test B beginning at that position.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2212 With optional end, stop comparing B at that position.\n\ |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2213 prefix can also be a tuple of bytes to try."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2214 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2215 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2216 bytes_startswith(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2217 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2218 Py_ssize_t start = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2219 Py_ssize_t end = PY_SSIZE_T_MAX; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2220 PyObject *subobj; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2221 int result; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2222 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2223 if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2224 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2225 if (PyTuple_Check(subobj)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2226 Py_ssize_t i; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2227 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2228 result = _bytes_tailmatch(self, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2229 PyTuple_GET_ITEM(subobj, i), |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2230 start, end, -1); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2231 if (result == -1) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2232 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2233 else if (result) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2234 Py_RETURN_TRUE; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2235 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2236 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2237 Py_RETURN_FALSE; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2238 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2239 result = _bytes_tailmatch(self, subobj, start, end, -1); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2240 if (result == -1) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2241 if (PyErr_ExceptionMatches(PyExc_TypeError)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2242 PyErr_Format(PyExc_TypeError, "startswith first arg must be bytes " |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2243 "or a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2244 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2245 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2246 else |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2247 return PyBool_FromLong(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2248 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2249 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2250 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2251 PyDoc_STRVAR(endswith__doc__, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2252 "B.endswith(suffix[, start[, end]]) -> bool\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2253 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2254 Return True if B ends with the specified suffix, False otherwise.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2255 With optional start, test B beginning at that position.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2256 With optional end, stop comparing B at that position.\n\ |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2257 suffix can also be a tuple of bytes to try."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2258 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2259 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2260 bytes_endswith(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2261 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2262 Py_ssize_t start = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2263 Py_ssize_t end = PY_SSIZE_T_MAX; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2264 PyObject *subobj; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2265 int result; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2266 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2267 if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2268 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2269 if (PyTuple_Check(subobj)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2270 Py_ssize_t i; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2271 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2272 result = _bytes_tailmatch(self, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2273 PyTuple_GET_ITEM(subobj, i), |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2274 start, end, +1); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2275 if (result == -1) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2276 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2277 else if (result) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2278 Py_RETURN_TRUE; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2279 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2280 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2281 Py_RETURN_FALSE; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2282 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2283 result = _bytes_tailmatch(self, subobj, start, end, +1); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2284 if (result == -1) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2285 if (PyErr_ExceptionMatches(PyExc_TypeError)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2286 PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or " |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2287 "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2288 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2289 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2290 else |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2291 return PyBool_FromLong(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2292 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2293 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2294 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2295 PyDoc_STRVAR(decode__doc__, |
|
66070
f8208c33978f
str, bytes, bytearray docstring: remove unnecessary [...]
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66069
diff
changeset
|
2296 "B.decode(encoding='utf-8', errors='strict') -> str\n\ |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2297 \n\ |
|
66069
8154db8a1593
Fix encode/decode method doc of str, bytes, bytearray types
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65980
diff
changeset
|
2298 Decode B using the codec registered for encoding. Default encoding\n\ |
|
8154db8a1593
Fix encode/decode method doc of str, bytes, bytearray types
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65980
diff
changeset
|
2299 is 'utf-8'. errors may be given to set a different error\n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2300 handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2301 a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25017
diff
changeset
|
2302 as well as any other name registerd with codecs.register_error that is\n\ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25017
diff
changeset
|
2303 able to handle UnicodeDecodeErrors."); |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2304 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2305 static PyObject * |
|
56099
1b7bdae02179
Merged revisions 74929 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53235
diff
changeset
|
2306 bytes_decode(PyObject *self, PyObject *args, PyObject *kwargs) |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2307 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2308 const char *encoding = NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2309 const char *errors = NULL; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2310 static char *kwlist[] = {"encoding", "errors", 0}; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2311 |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2312 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", kwlist, &encoding, &errors)) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2313 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2314 if (encoding == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2315 encoding = PyUnicode_GetDefaultEncoding(); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2316 return PyUnicode_FromEncodedObject(self, encoding, errors); |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2317 } |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2318 |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2319 |
|
58289
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
2320 PyDoc_STRVAR(splitlines__doc__, |
|
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
2321 "B.splitlines([keepends]) -> list of lines\n\ |
|
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
2322 \n\ |
|
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
2323 Return a list of the lines in B, breaking at line boundaries.\n\ |
|
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
2324 Line breaks are not included in the resulting list unless keepends\n\ |
|
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
2325 is given and true."); |
|
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
2326 |
|
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
2327 static PyObject* |
|
72462
0b2660384d4e
Issue #13012: Allow 'keepends' to be passed as a keyword argument in str.splitlines, bytes.splitlines and bytearray.splitlines.
Mark Dickinson <mdickinson@enthought.com>
parents:
71528
diff
changeset
|
2328 bytes_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
|
58289
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
2329 { |
|
72462
0b2660384d4e
Issue #13012: Allow 'keepends' to be passed as a keyword argument in str.splitlines, bytes.splitlines and bytearray.splitlines.
Mark Dickinson <mdickinson@enthought.com>
parents:
71528
diff
changeset
|
2330 static char *kwlist[] = {"keepends", 0}; |
|
58289
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
2331 int keepends = 0; |
|
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
2332 |
|
72462
0b2660384d4e
Issue #13012: Allow 'keepends' to be passed as a keyword argument in str.splitlines, bytes.splitlines and bytearray.splitlines.
Mark Dickinson <mdickinson@enthought.com>
parents:
71528
diff
changeset
|
2333 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
|
0b2660384d4e
Issue #13012: Allow 'keepends' to be passed as a keyword argument in str.splitlines, bytes.splitlines and bytearray.splitlines.
Mark Dickinson <mdickinson@enthought.com>
parents:
71528
diff
changeset
|
2334 kwlist, &keepends)) |
|
62210
865e34cd7e5b
Merged revisions 81860 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61466
diff
changeset
|
2335 return NULL; |
|
58289
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
2336 |
|
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
2337 return stringlib_splitlines( |
|
62210
865e34cd7e5b
Merged revisions 81860 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61466
diff
changeset
|
2338 (PyObject*) self, PyBytes_AS_STRING(self), |
|
865e34cd7e5b
Merged revisions 81860 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61466
diff
changeset
|
2339 PyBytes_GET_SIZE(self), keepends |
|
865e34cd7e5b
Merged revisions 81860 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61466
diff
changeset
|
2340 ); |
|
58289
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
2341 } |
|
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
2342 |
|
5ca1948a8fd6
Merged revisions 77461 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58264
diff
changeset
|
2343 |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2344 PyDoc_STRVAR(fromhex_doc, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2345 "bytes.fromhex(string) -> bytes\n\ |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2346 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2347 Create a bytes object from a string of hexadecimal numbers.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2348 Spaces between two numbers are accepted.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2349 Example: bytes.fromhex('B9 01EF') -> b'\\xb9\\x01\\xef'."); |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2350 |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2351 static int |
|
72520
e6a2b54c1d16
Fix hex_digit_to_int() prototype: expect Py_UCS4, not Py_UNICODE
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72475
diff
changeset
|
2352 hex_digit_to_int(Py_UCS4 c) |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2353 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2354 if (c >= 128) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2355 return -1; |
|
65980
d31eaec883eb
Issue #10288: The deprecated family of "char"-handling macros
David Malcolm <dmalcolm@redhat.com>
parents:
65494
diff
changeset
|
2356 if (Py_ISDIGIT(c)) |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2357 return c - '0'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2358 else { |
|
65980
d31eaec883eb
Issue #10288: The deprecated family of "char"-handling macros
David Malcolm <dmalcolm@redhat.com>
parents:
65494
diff
changeset
|
2359 if (Py_ISUPPER(c)) |
|
d31eaec883eb
Issue #10288: The deprecated family of "char"-handling macros
David Malcolm <dmalcolm@redhat.com>
parents:
65494
diff
changeset
|
2360 c = Py_TOLOWER(c); |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2361 if (c >= 'a' && c <= 'f') |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2362 return c - 'a' + 10; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2363 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2364 return -1; |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2365 } |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2366 |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2367 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2368 bytes_fromhex(PyObject *cls, PyObject *args) |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2369 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2370 PyObject *newstring, *hexobj; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2371 char *buf; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2372 Py_ssize_t hexlen, byteslen, i, j; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2373 int top, bot; |
| 72475 | 2374 void *data; |
| 2375 unsigned int kind; | |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2376 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2377 if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2378 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2379 assert(PyUnicode_Check(hexobj)); |
| 72475 | 2380 if (PyUnicode_READY(hexobj)) |
| 2381 return NULL; | |
| 2382 kind = PyUnicode_KIND(hexobj); | |
| 2383 data = PyUnicode_DATA(hexobj); | |
| 2384 hexlen = PyUnicode_GET_LENGTH(hexobj); | |
| 2385 | |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2386 byteslen = hexlen/2; /* This overestimates if there are spaces */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2387 newstring = PyBytes_FromStringAndSize(NULL, byteslen); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2388 if (!newstring) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2389 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2390 buf = PyBytes_AS_STRING(newstring); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2391 for (i = j = 0; i < hexlen; i += 2) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2392 /* skip over spaces in the input */ |
| 72475 | 2393 while (PyUnicode_READ(kind, data, i) == ' ') |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2394 i++; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2395 if (i >= hexlen) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2396 break; |
| 72475 | 2397 top = hex_digit_to_int(PyUnicode_READ(kind, data, i)); |
| 2398 bot = hex_digit_to_int(PyUnicode_READ(kind, data, i+1)); | |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2399 if (top == -1 || bot == -1) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2400 PyErr_Format(PyExc_ValueError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2401 "non-hexadecimal number found in " |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2402 "fromhex() arg at position %zd", i); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2403 goto error; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2404 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2405 buf[j++] = (top << 4) + bot; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2406 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2407 if (j != byteslen && _PyBytes_Resize(&newstring, j) < 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2408 goto error; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2409 return newstring; |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2410 |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2411 error: |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2412 Py_XDECREF(newstring); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2413 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2414 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2415 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2416 PyDoc_STRVAR(sizeof__doc__, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2417 "B.__sizeof__() -> size of B in memory, in bytes"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2418 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2419 static PyObject * |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2420 bytes_sizeof(PyBytesObject *v) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2421 { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2422 Py_ssize_t res; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2423 res = PyBytesObject_SIZE + Py_SIZE(v) * Py_TYPE(v)->tp_itemsize; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2424 return PyLong_FromSsize_t(res); |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2425 } |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2426 |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2427 |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27248
diff
changeset
|
2428 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2429 bytes_getnewargs(PyBytesObject *v) |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27248
diff
changeset
|
2430 { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2431 return Py_BuildValue("(y#)", v->ob_sval, Py_SIZE(v)); |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27248
diff
changeset
|
2432 } |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27248
diff
changeset
|
2433 |
|
45052
e43238760c38
Fixed issue #1973: bytes.fromhex('') raises SystemError
Christian Heimes <christian@cheimes.de>
parents:
44548
diff
changeset
|
2434 |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2435 static PyMethodDef |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2436 bytes_methods[] = { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2437 {"__getnewargs__", (PyCFunction)bytes_getnewargs, METH_NOARGS}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2438 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2439 _Py_capitalize__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2440 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2441 {"count", (PyCFunction)bytes_count, METH_VARARGS, count__doc__}, |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2442 {"decode", (PyCFunction)bytes_decode, METH_VARARGS | METH_KEYWORDS, decode__doc__}, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2443 {"endswith", (PyCFunction)bytes_endswith, METH_VARARGS, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2444 endswith__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2445 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2446 expandtabs__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2447 {"find", (PyCFunction)bytes_find, METH_VARARGS, find__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2448 {"fromhex", (PyCFunction)bytes_fromhex, METH_VARARGS|METH_CLASS, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2449 fromhex_doc}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2450 {"index", (PyCFunction)bytes_index, METH_VARARGS, index__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2451 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2452 _Py_isalnum__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2453 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2454 _Py_isalpha__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2455 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2456 _Py_isdigit__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2457 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2458 _Py_islower__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2459 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2460 _Py_isspace__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2461 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2462 _Py_istitle__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2463 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2464 _Py_isupper__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2465 {"join", (PyCFunction)bytes_join, METH_O, join__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2466 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2467 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2468 {"lstrip", (PyCFunction)bytes_lstrip, METH_VARARGS, lstrip__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2469 {"maketrans", (PyCFunction)bytes_maketrans, METH_VARARGS|METH_STATIC, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2470 _Py_maketrans__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2471 {"partition", (PyCFunction)bytes_partition, METH_O, partition__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2472 {"replace", (PyCFunction)bytes_replace, METH_VARARGS, replace__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2473 {"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, rfind__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2474 {"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, rindex__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2475 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2476 {"rpartition", (PyCFunction)bytes_rpartition, METH_O, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2477 rpartition__doc__}, |
|
75291
db968ac2b82c
#14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
75129
diff
changeset
|
2478 {"rsplit", (PyCFunction)bytes_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2479 {"rstrip", (PyCFunction)bytes_rstrip, METH_VARARGS, rstrip__doc__}, |
|
75291
db968ac2b82c
#14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
75129
diff
changeset
|
2480 {"split", (PyCFunction)bytes_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
|
72462
0b2660384d4e
Issue #13012: Allow 'keepends' to be passed as a keyword argument in str.splitlines, bytes.splitlines and bytearray.splitlines.
Mark Dickinson <mdickinson@enthought.com>
parents:
71528
diff
changeset
|
2481 {"splitlines", (PyCFunction)bytes_splitlines, METH_VARARGS | METH_KEYWORDS, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2482 splitlines__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2483 {"startswith", (PyCFunction)bytes_startswith, METH_VARARGS, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2484 startswith__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2485 {"strip", (PyCFunction)bytes_strip, METH_VARARGS, strip__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2486 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2487 _Py_swapcase__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2488 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2489 {"translate", (PyCFunction)bytes_translate, METH_VARARGS, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2490 translate__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2491 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2492 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2493 {"__sizeof__", (PyCFunction)bytes_sizeof, METH_NOARGS, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2494 sizeof__doc__}, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2495 {NULL, NULL} /* sentinel */ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2496 }; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2497 |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24184
diff
changeset
|
2498 static PyObject * |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2499 str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2500 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2501 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2502 bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2503 { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2504 PyObject *x = NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2505 const char *encoding = NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2506 const char *errors = NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2507 PyObject *new = NULL; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2508 Py_ssize_t size; |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2509 static char *kwlist[] = {"source", "encoding", "errors", 0}; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2510 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2511 if (type != &PyBytes_Type) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2512 return str_subtype_new(type, args, kwds); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2513 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytes", kwlist, &x, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2514 &encoding, &errors)) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2515 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2516 if (x == NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2517 if (encoding != NULL || errors != NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2518 PyErr_SetString(PyExc_TypeError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2519 "encoding or errors without sequence " |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2520 "argument"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2521 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2522 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2523 return PyBytes_FromString(""); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2524 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2525 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2526 if (PyUnicode_Check(x)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2527 /* Encode via the codec registry */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2528 if (encoding == NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2529 PyErr_SetString(PyExc_TypeError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2530 "string argument without an encoding"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2531 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2532 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2533 new = PyUnicode_AsEncodedString(x, encoding, errors); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2534 if (new == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2535 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2536 assert(PyBytes_Check(new)); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2537 return new; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2538 } |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2539 /* Is it an integer? */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2540 size = PyNumber_AsSsize_t(x, PyExc_OverflowError); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2541 if (size == -1 && PyErr_Occurred()) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2542 if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2543 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2544 PyErr_Clear(); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2545 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2546 else if (size < 0) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2547 PyErr_SetString(PyExc_ValueError, "negative count"); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2548 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2549 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2550 else { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2551 new = PyBytes_FromStringAndSize(NULL, size); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2552 if (new == NULL) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2553 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2554 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2555 if (size > 0) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2556 memset(((PyBytesObject*)new)->ob_sval, 0, size); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2557 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2558 return new; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2559 } |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2560 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2561 /* If it's not unicode, there can't be encoding or errors */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2562 if (encoding != NULL || errors != NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2563 PyErr_SetString(PyExc_TypeError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2564 "encoding or errors without a string argument"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2565 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2566 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2567 return PyObject_Bytes(x); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2568 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2569 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2570 PyObject * |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2571 PyBytes_FromObject(PyObject *x) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2572 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2573 PyObject *new, *it; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2574 Py_ssize_t i, size; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2575 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2576 if (x == NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2577 PyErr_BadInternalCall(); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2578 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2579 } |
|
77130
f8cd75e8a1d6
Issue #14889: PyBytes_FromObject(bytes) now just increfs and returns.
Larry Hastings <larry@hastings.org>
parents:
76588
diff
changeset
|
2580 |
|
f8cd75e8a1d6
Issue #14889: PyBytes_FromObject(bytes) now just increfs and returns.
Larry Hastings <larry@hastings.org>
parents:
76588
diff
changeset
|
2581 if (PyBytes_CheckExact(x)) { |
|
f8cd75e8a1d6
Issue #14889: PyBytes_FromObject(bytes) now just increfs and returns.
Larry Hastings <larry@hastings.org>
parents:
76588
diff
changeset
|
2582 Py_INCREF(x); |
|
f8cd75e8a1d6
Issue #14889: PyBytes_FromObject(bytes) now just increfs and returns.
Larry Hastings <larry@hastings.org>
parents:
76588
diff
changeset
|
2583 return x; |
|
f8cd75e8a1d6
Issue #14889: PyBytes_FromObject(bytes) now just increfs and returns.
Larry Hastings <larry@hastings.org>
parents:
76588
diff
changeset
|
2584 } |
|
f8cd75e8a1d6
Issue #14889: PyBytes_FromObject(bytes) now just increfs and returns.
Larry Hastings <larry@hastings.org>
parents:
76588
diff
changeset
|
2585 |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2586 /* Use the modern buffer interface */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2587 if (PyObject_CheckBuffer(x)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2588 Py_buffer view; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2589 if (PyObject_GetBuffer(x, &view, PyBUF_FULL_RO) < 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2590 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2591 new = PyBytes_FromStringAndSize(NULL, view.len); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2592 if (!new) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2593 goto fail; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2594 if (PyBuffer_ToContiguous(((PyBytesObject *)new)->ob_sval, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2595 &view, view.len, 'C') < 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2596 goto fail; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2597 PyBuffer_Release(&view); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2598 return new; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2599 fail: |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2600 Py_XDECREF(new); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2601 PyBuffer_Release(&view); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2602 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2603 } |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2604 if (PyUnicode_Check(x)) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2605 PyErr_SetString(PyExc_TypeError, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2606 "cannot convert unicode object to bytes"); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2607 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2608 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2609 |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2610 if (PyList_CheckExact(x)) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2611 new = PyBytes_FromStringAndSize(NULL, Py_SIZE(x)); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2612 if (new == NULL) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2613 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2614 for (i = 0; i < Py_SIZE(x); i++) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2615 Py_ssize_t value = PyNumber_AsSsize_t( |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2616 PyList_GET_ITEM(x, i), PyExc_ValueError); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2617 if (value == -1 && PyErr_Occurred()) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2618 Py_DECREF(new); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2619 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2620 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2621 if (value < 0 || value >= 256) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2622 PyErr_SetString(PyExc_ValueError, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2623 "bytes must be in range(0, 256)"); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2624 Py_DECREF(new); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2625 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2626 } |
|
64041
9028f23db74b
Fix some compilation warnings under 64-bit Windows (issue #9566).
Antoine Pitrou <solipsis@pitrou.net>
parents:
63924
diff
changeset
|
2627 ((PyBytesObject *)new)->ob_sval[i] = (char) value; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2628 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2629 return new; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2630 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2631 if (PyTuple_CheckExact(x)) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2632 new = PyBytes_FromStringAndSize(NULL, Py_SIZE(x)); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2633 if (new == NULL) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2634 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2635 for (i = 0; i < Py_SIZE(x); i++) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2636 Py_ssize_t value = PyNumber_AsSsize_t( |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2637 PyTuple_GET_ITEM(x, i), PyExc_ValueError); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2638 if (value == -1 && PyErr_Occurred()) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2639 Py_DECREF(new); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2640 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2641 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2642 if (value < 0 || value >= 256) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2643 PyErr_SetString(PyExc_ValueError, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2644 "bytes must be in range(0, 256)"); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2645 Py_DECREF(new); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2646 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2647 } |
|
64041
9028f23db74b
Fix some compilation warnings under 64-bit Windows (issue #9566).
Antoine Pitrou <solipsis@pitrou.net>
parents:
63924
diff
changeset
|
2648 ((PyBytesObject *)new)->ob_sval[i] = (char) value; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2649 } |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2650 return new; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2651 } |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2652 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2653 /* For iterator version, create a string object and resize as needed */ |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2654 size = _PyObject_LengthHint(x, 64); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2655 if (size == -1 && PyErr_Occurred()) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2656 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2657 /* Allocate an extra byte to prevent PyBytes_FromStringAndSize() from |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2658 returning a shared empty bytes string. This required because we |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2659 want to call _PyBytes_Resize() the returned object, which we can |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2660 only do on bytes objects with refcount == 1. */ |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2661 size += 1; |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2662 new = PyBytes_FromStringAndSize(NULL, size); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2663 if (new == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2664 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2665 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2666 /* Get the iterator */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2667 it = PyObject_GetIter(x); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2668 if (it == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2669 goto error; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2670 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2671 /* Run the iterator to exhaustion */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2672 for (i = 0; ; i++) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2673 PyObject *item; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2674 Py_ssize_t value; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2675 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2676 /* Get the next item */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2677 item = PyIter_Next(it); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2678 if (item == NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2679 if (PyErr_Occurred()) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2680 goto error; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2681 break; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2682 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2683 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2684 /* Interpret it as an int (__index__) */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2685 value = PyNumber_AsSsize_t(item, PyExc_ValueError); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2686 Py_DECREF(item); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2687 if (value == -1 && PyErr_Occurred()) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2688 goto error; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2689 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2690 /* Range check */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2691 if (value < 0 || value >= 256) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2692 PyErr_SetString(PyExc_ValueError, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2693 "bytes must be in range(0, 256)"); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2694 goto error; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2695 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2696 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2697 /* Append the byte */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2698 if (i >= size) { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60640
diff
changeset
|
2699 size = 2 * size + 1; |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2700 if (_PyBytes_Resize(&new, size) < 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2701 goto error; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2702 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2703 ((PyBytesObject *)new)->ob_sval[i] = (char) value; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2704 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2705 _PyBytes_Resize(&new, i); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2706 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2707 /* Clean up and return success */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2708 Py_DECREF(it); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2709 return new; |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2710 |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2711 error: |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2712 /* Error handling when new != NULL */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2713 Py_XDECREF(it); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2714 Py_DECREF(new); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2715 return NULL; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2716 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2717 |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2718 static PyObject * |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2719 str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2720 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2721 PyObject *tmp, *pnew; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2722 Py_ssize_t n; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2723 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2724 assert(PyType_IsSubtype(type, &PyBytes_Type)); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2725 tmp = bytes_new(&PyBytes_Type, args, kwds); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2726 if (tmp == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2727 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2728 assert(PyBytes_CheckExact(tmp)); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2729 n = PyBytes_GET_SIZE(tmp); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2730 pnew = type->tp_alloc(type, n); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2731 if (pnew != NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2732 Py_MEMCPY(PyBytes_AS_STRING(pnew), |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2733 PyBytes_AS_STRING(tmp), n+1); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2734 ((PyBytesObject *)pnew)->ob_shash = |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2735 ((PyBytesObject *)tmp)->ob_shash; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2736 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2737 Py_DECREF(tmp); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2738 return pnew; |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2739 } |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2740 |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2741 PyDoc_STRVAR(bytes_doc, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2742 "bytes(iterable_of_ints) -> bytes\n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2743 bytes(string, encoding[, errors]) -> bytes\n\ |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2744 bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer\n\ |
|
74020
beac7d6c7be4
Issue #11231: Fix bytes and bytearray docstrings
Victor Stinner <victor.stinner@haypocalc.com>
parents:
71527
diff
changeset
|
2745 bytes(int) -> bytes object of size given by the parameter initialized with null bytes\n\ |
|
beac7d6c7be4
Issue #11231: Fix bytes and bytearray docstrings
Victor Stinner <victor.stinner@haypocalc.com>
parents:
71527
diff
changeset
|
2746 bytes() -> empty bytes object\n\ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2747 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2748 Construct an immutable array of bytes from:\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2749 - an iterable yielding integers in range(256)\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2750 - a text string encoded using the specified encoding\n\ |
|
74020
beac7d6c7be4
Issue #11231: Fix bytes and bytearray docstrings
Victor Stinner <victor.stinner@haypocalc.com>
parents:
71527
diff
changeset
|
2751 - any object implementing the buffer API.\n\ |
|
beac7d6c7be4
Issue #11231: Fix bytes and bytearray docstrings
Victor Stinner <victor.stinner@haypocalc.com>
parents:
71527
diff
changeset
|
2752 - an integer"); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2753 |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2754 static PyObject *bytes_iter(PyObject *seq); |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2755 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2756 PyTypeObject PyBytes_Type = { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2757 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2758 "bytes", |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2759 PyBytesObject_SIZE, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2760 sizeof(char), |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2761 bytes_dealloc, /* tp_dealloc */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2762 0, /* tp_print */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2763 0, /* tp_getattr */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2764 0, /* tp_setattr */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2765 0, /* tp_reserved */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2766 (reprfunc)bytes_repr, /* tp_repr */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2767 0, /* tp_as_number */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2768 &bytes_as_sequence, /* tp_as_sequence */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2769 &bytes_as_mapping, /* tp_as_mapping */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2770 (hashfunc)bytes_hash, /* tp_hash */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2771 0, /* tp_call */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2772 bytes_str, /* tp_str */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2773 PyObject_GenericGetAttr, /* tp_getattro */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2774 0, /* tp_setattro */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2775 &bytes_as_buffer, /* tp_as_buffer */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2776 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2777 Py_TPFLAGS_BYTES_SUBCLASS, /* tp_flags */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2778 bytes_doc, /* tp_doc */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2779 0, /* tp_traverse */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2780 0, /* tp_clear */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2781 (richcmpfunc)bytes_richcompare, /* tp_richcompare */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2782 0, /* tp_weaklistoffset */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2783 bytes_iter, /* tp_iter */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2784 0, /* tp_iternext */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2785 bytes_methods, /* tp_methods */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2786 0, /* tp_members */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2787 0, /* tp_getset */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2788 &PyBaseObject_Type, /* tp_base */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2789 0, /* tp_dict */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2790 0, /* tp_descr_get */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2791 0, /* tp_descr_set */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2792 0, /* tp_dictoffset */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2793 0, /* tp_init */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2794 0, /* tp_alloc */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2795 bytes_new, /* tp_new */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2796 PyObject_Del, /* tp_free */ |
| 6 | 2797 }; |
| 2798 | |
| 2799 void | |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2800 PyBytes_Concat(register PyObject **pv, register PyObject *w) |
| 6 | 2801 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2802 register PyObject *v; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2803 assert(pv != NULL); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2804 if (*pv == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2805 return; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2806 if (w == NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2807 Py_DECREF(*pv); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2808 *pv = NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2809 return; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2810 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2811 v = bytes_concat(*pv, w); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2812 Py_DECREF(*pv); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2813 *pv = v; |
| 6 | 2814 } |
| 2815 | |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2816 void |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2817 PyBytes_ConcatAndDel(register PyObject **pv, register PyObject *w) |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2818 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2819 PyBytes_Concat(pv, w); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2820 Py_XDECREF(w); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2821 } |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2822 |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2823 |
| 6 | 2824 /* The following function breaks the notion that strings are immutable: |
| 2825 it changes the size of a string. We get away with this only if there | |
| 2826 is only one module referencing the object. You can also think of it | |
| 2827 as creating a new string object and destroying the old one, only | |
| 2828 more efficiently. In any case, don't use this if the string may | |
|
23295
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23225
diff
changeset
|
2829 already be known to some other part of the code... |
|
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23225
diff
changeset
|
2830 Note that if there's not enough memory to resize the string, the original |
|
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23225
diff
changeset
|
2831 string object at *pv is deallocated, *pv is set to NULL, an "out of |
|
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23225
diff
changeset
|
2832 memory" exception is set, and -1 is returned. Else (on success) 0 is |
|
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23225
diff
changeset
|
2833 returned, and the value in *pv may or may not be the same as on input. |
|
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23225
diff
changeset
|
2834 As always, an extra byte is allocated for a trailing \0 byte (newsize |
|
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23225
diff
changeset
|
2835 does *not* include that), and a trailing \0 byte is stored. |
|
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23225
diff
changeset
|
2836 */ |
| 6 | 2837 |
| 2838 int | |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2839 _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize) |
| 6 | 2840 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2841 register PyObject *v; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2842 register PyBytesObject *sv; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2843 v = *pv; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2844 if (!PyBytes_Check(v) || Py_REFCNT(v) != 1 || newsize < 0) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2845 *pv = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2846 Py_DECREF(v); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2847 PyErr_BadInternalCall(); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2848 return -1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2849 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2850 /* XXX UNREF/NEWREF interface should be more symmetrical */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2851 _Py_DEC_REFTOTAL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2852 _Py_ForgetReference(v); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2853 *pv = (PyObject *) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2854 PyObject_REALLOC((char *)v, PyBytesObject_SIZE + newsize); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2855 if (*pv == NULL) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2856 PyObject_Del(v); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2857 PyErr_NoMemory(); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2858 return -1; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2859 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2860 _Py_NewReference(*pv); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2861 sv = (PyBytesObject *) *pv; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2862 Py_SIZE(sv) = newsize; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2863 sv->ob_sval[newsize] = '\0'; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2864 sv->ob_shash = -1; /* invalidate cached hash value */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2865 return 0; |
| 6 | 2866 } |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2867 |
|
5920
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
2868 void |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2869 PyBytes_Fini(void) |
|
5920
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
2870 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2871 int i; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2872 for (i = 0; i < UCHAR_MAX + 1; i++) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2873 Py_XDECREF(characters[i]); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2874 characters[i] = NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2875 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2876 Py_XDECREF(nullstring); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2877 nullstring = NULL; |
|
5920
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
2878 } |
|
16751
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
2879 |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2880 /*********************** Bytes Iterator ****************************/ |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2881 |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2882 typedef struct { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2883 PyObject_HEAD |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2884 Py_ssize_t it_index; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2885 PyBytesObject *it_seq; /* Set to NULL when iterator is exhausted */ |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2886 } striterobject; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2887 |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2888 static void |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2889 striter_dealloc(striterobject *it) |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2890 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2891 _PyObject_GC_UNTRACK(it); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2892 Py_XDECREF(it->it_seq); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2893 PyObject_GC_Del(it); |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2894 } |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2895 |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2896 static int |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2897 striter_traverse(striterobject *it, visitproc visit, void *arg) |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2898 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2899 Py_VISIT(it->it_seq); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2900 return 0; |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2901 } |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2902 |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2903 static PyObject * |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2904 striter_next(striterobject *it) |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2905 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2906 PyBytesObject *seq; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2907 PyObject *item; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2908 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2909 assert(it != NULL); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2910 seq = it->it_seq; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2911 if (seq == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2912 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2913 assert(PyBytes_Check(seq)); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2914 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2915 if (it->it_index < PyBytes_GET_SIZE(seq)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2916 item = PyLong_FromLong( |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2917 (unsigned char)seq->ob_sval[it->it_index]); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2918 if (item != NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2919 ++it->it_index; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2920 return item; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2921 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2922 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2923 Py_DECREF(seq); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2924 it->it_seq = NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2925 return NULL; |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2926 } |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2927 |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2928 static PyObject * |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2929 striter_len(striterobject *it) |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2930 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2931 Py_ssize_t len = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2932 if (it->it_seq) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2933 len = PyBytes_GET_SIZE(it->it_seq) - it->it_index; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2934 return PyLong_FromSsize_t(len); |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2935 } |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2936 |
|
39544
01034018e8b2
Make the it_index field in the str/unicode iterators Py_ssize_t's.
Guido van Rossum <guido@python.org>
parents:
39536
diff
changeset
|
2937 PyDoc_STRVAR(length_hint_doc, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2938 "Private method returning an estimate of len(list(it))."); |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2939 |
|
76091
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2940 static PyObject * |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2941 striter_reduce(striterobject *it) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2942 { |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2943 if (it->it_seq != NULL) { |
|
76105
fb907ee4fdfa
Rename _PyIter_GetBuiltin to _PyObject_GetBuiltin, and do not include it in the stable ABI.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76091
diff
changeset
|
2944 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
|
76091
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2945 it->it_seq, it->it_index); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2946 } else { |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2947 PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2948 if (u == NULL) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2949 return NULL; |
|
76105
fb907ee4fdfa
Rename _PyIter_GetBuiltin to _PyObject_GetBuiltin, and do not include it in the stable ABI.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76091
diff
changeset
|
2950 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
|
76091
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2951 } |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2952 } |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2953 |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2954 PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2955 |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2956 static PyObject * |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2957 striter_setstate(striterobject *it, PyObject *state) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2958 { |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2959 Py_ssize_t index = PyLong_AsSsize_t(state); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2960 if (index == -1 && PyErr_Occurred()) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2961 return NULL; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2962 if (index < 0) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2963 index = 0; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2964 it->it_index = index; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2965 Py_RETURN_NONE; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2966 } |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2967 |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2968 PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2969 |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2970 static PyMethodDef striter_methods[] = { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2971 {"__length_hint__", (PyCFunction)striter_len, METH_NOARGS, |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2972 length_hint_doc}, |
|
76091
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2973 {"__reduce__", (PyCFunction)striter_reduce, METH_NOARGS, |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2974 reduce_doc}, |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2975 {"__setstate__", (PyCFunction)striter_setstate, METH_O, |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
75291
diff
changeset
|
2976 setstate_doc}, |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2977 {NULL, NULL} /* sentinel */ |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2978 }; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
2979 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2980 PyTypeObject PyBytesIter_Type = { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2981 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2982 "bytes_iterator", /* tp_name */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2983 sizeof(striterobject), /* tp_basicsize */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2984 0, /* tp_itemsize */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2985 /* methods */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2986 (destructor)striter_dealloc, /* tp_dealloc */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2987 0, /* tp_print */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2988 0, /* tp_getattr */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2989 0, /* tp_setattr */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2990 0, /* tp_reserved */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2991 0, /* tp_repr */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2992 0, /* tp_as_number */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2993 0, /* tp_as_sequence */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2994 0, /* tp_as_mapping */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2995 0, /* tp_hash */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2996 0, /* tp_call */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2997 0, /* tp_str */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2998 PyObject_GenericGetAttr, /* tp_getattro */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
2999 0, /* tp_setattro */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3000 0, /* tp_as_buffer */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3001 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3002 0, /* tp_doc */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3003 (traverseproc)striter_traverse, /* tp_traverse */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3004 0, /* tp_clear */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3005 0, /* tp_richcompare */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3006 0, /* tp_weaklistoffset */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3007 PyObject_SelfIter, /* tp_iter */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3008 (iternextfunc)striter_next, /* tp_iternext */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3009 striter_methods, /* tp_methods */ |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3010 0, |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3011 }; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3012 |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3013 static PyObject * |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3014 bytes_iter(PyObject *seq) |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3015 { |
|
76191
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3016 striterobject *it; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3017 |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3018 if (!PyBytes_Check(seq)) { |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3019 PyErr_BadInternalCall(); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3020 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3021 } |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3022 it = PyObject_GC_New(striterobject, &PyBytesIter_Type); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3023 if (it == NULL) |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3024 return NULL; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3025 it->it_index = 0; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3026 Py_INCREF(seq); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3027 it->it_seq = (PyBytesObject *)seq; |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3028 _PyObject_GC_TRACK(it); |
|
64bb1d258322
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
47301
diff
changeset
|
3029 return (PyObject *)it; |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3030 } |
