Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(13)

Side by Side Diff: Objects/stringlib/find.h

Issue 8706: accept keyword arguments on all base type methods and builtins
Patch Set: Created 8 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Objects/bytesobject.c ('k') | Objects/unicodeobject.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* stringlib: find/index implementation */ 1 /* stringlib: find/index implementation */
2 2
3 #ifndef STRINGLIB_FASTSEARCH_H 3 #ifndef STRINGLIB_FASTSEARCH_H
4 #error must include "stringlib/fastsearch.h" before including this module 4 #error must include "stringlib/fastsearch.h" before including this module
5 #endif 5 #endif
6 6
7 Py_LOCAL_INLINE(Py_ssize_t) 7 Py_LOCAL_INLINE(Py_ssize_t)
8 STRINGLIB(find)(const STRINGLIB_CHAR* str, Py_ssize_t str_len, 8 STRINGLIB(find)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
9 const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, 9 const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
10 Py_ssize_t offset) 10 Py_ssize_t offset)
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 the same behaviour for the arguments. 96 the same behaviour for the arguments.
97 97
98 It does not touch the variables received until it knows everything 98 It does not touch the variables received until it knows everything
99 is ok. 99 is ok.
100 */ 100 */
101 101
102 #define FORMAT_BUFFER_SIZE 50 102 #define FORMAT_BUFFER_SIZE 50
103 103
104 Py_LOCAL_INLINE(int) 104 Py_LOCAL_INLINE(int)
105 STRINGLIB(parse_args_finds)(const char * function_name, PyObject *args, 105 STRINGLIB(parse_args_finds)(const char * function_name, PyObject *args,
106 PyObject **subobj, 106 PyObject *kwds, PyObject **subobj,
107 Py_ssize_t *start, Py_ssize_t *end) 107 Py_ssize_t *start, Py_ssize_t *end)
108 { 108 {
109 PyObject *tmp_subobj; 109 PyObject *tmp_subobj;
110 Py_ssize_t tmp_start = 0; 110 Py_ssize_t tmp_start = 0;
111 Py_ssize_t tmp_end = PY_SSIZE_T_MAX; 111 Py_ssize_t tmp_end = PY_SSIZE_T_MAX;
112 PyObject *obj_start=Py_None, *obj_end=Py_None; 112 PyObject *obj_start=Py_None, *obj_end=Py_None;
113 char format[FORMAT_BUFFER_SIZE] = "O|OO:"; 113 char format[FORMAT_BUFFER_SIZE] = "O|OO:";
114 size_t len = strlen(format); 114 size_t len = strlen(format);
115 static char *kwlist[] = {"sub", "start", "end", 0};
115 116
116 strncpy(format + len, function_name, FORMAT_BUFFER_SIZE - len - 1); 117 strncpy(format + len, function_name, FORMAT_BUFFER_SIZE - len - 1);
117 format[FORMAT_BUFFER_SIZE - 1] = '\0'; 118 format[FORMAT_BUFFER_SIZE - 1] = '\0';
118 119
119 if (!PyArg_ParseTuple(args, format, &tmp_subobj, &obj_start, &obj_end)) 120 if (!PyArg_ParseTupleAndKeywords(args, kwds, format, kwlist, &tmp_subobj,
121 &obj_start, &obj_end))
120 return 0; 122 return 0;
121 123
122 /* To support None in "start" and "end" arguments, meaning 124 /* To support None in "start" and "end" arguments, meaning
123 the same as if they were not passed. 125 the same as if they were not passed.
124 */ 126 */
125 if (obj_start != Py_None) 127 if (obj_start != Py_None)
126 if (!_PyEval_SliceIndex(obj_start, &tmp_start)) 128 if (!_PyEval_SliceIndex(obj_start, &tmp_start))
127 return 0; 129 return 0;
128 if (obj_end != Py_None) 130 if (obj_end != Py_None)
129 if (!_PyEval_SliceIndex(obj_end, &tmp_end)) 131 if (!_PyEval_SliceIndex(obj_end, &tmp_end))
(...skipping 14 matching lines...) Expand all
144 first argument is a unicode object. 146 first argument is a unicode object.
145 147
146 Note that we receive a pointer to the pointer of the substring object, 148 Note that we receive a pointer to the pointer of the substring object,
147 so when we create that object in this function we don't DECREF it, 149 so when we create that object in this function we don't DECREF it,
148 because it continues living in the caller functions (those functions, 150 because it continues living in the caller functions (those functions,
149 after finishing using the substring, must DECREF it). 151 after finishing using the substring, must DECREF it).
150 */ 152 */
151 153
152 Py_LOCAL_INLINE(int) 154 Py_LOCAL_INLINE(int)
153 STRINGLIB(parse_args_finds_unicode)(const char * function_name, PyObject *args, 155 STRINGLIB(parse_args_finds_unicode)(const char * function_name, PyObject *args,
154 PyObject **substring, 156 PyObject *kwds, PyObject **substring,
155 Py_ssize_t *start, Py_ssize_t *end) 157 Py_ssize_t *start, Py_ssize_t *end)
156 { 158 {
157 PyObject *tmp_substring; 159 PyObject *tmp_substring;
158 160
159 if(STRINGLIB(parse_args_finds)(function_name, args, &tmp_substring, 161 if(STRINGLIB(parse_args_finds)(function_name, args, kwds, &tmp_substring,
160 start, end)) { 162 start, end)) {
161 tmp_substring = PyUnicode_FromObject(tmp_substring); 163 tmp_substring = PyUnicode_FromObject(tmp_substring);
162 if (!tmp_substring) 164 if (!tmp_substring)
163 return 0; 165 return 0;
164 *substring = tmp_substring; 166 *substring = tmp_substring;
165 return 1; 167 return 1;
166 } 168 }
167 return 0; 169 return 0;
168 } 170 }
169 171
170 #else /* !STRINGLIB_IS_UNICODE */ 172 #else /* !STRINGLIB_IS_UNICODE */
171 173
172 /* 174 /*
173 Wraps stringlib_parse_args_finds() and additionally checks whether the 175 Wraps stringlib_parse_args_finds() and additionally checks whether the
174 first argument is an integer in range(0, 256). 176 first argument is an integer in range(0, 256).
175 177
176 If this is the case, writes the integer value to the byte parameter 178 If this is the case, writes the integer value to the byte parameter
177 and sets subobj to NULL. Otherwise, sets the first argument to subobj 179 and sets subobj to NULL. Otherwise, sets the first argument to subobj
178 and doesn't touch byte. The other parameters are similar to those of 180 and doesn't touch byte. The other parameters are similar to those of
179 stringlib_parse_args_finds(). 181 stringlib_parse_args_finds().
180 */ 182 */
181 183
182 Py_LOCAL_INLINE(int) 184 Py_LOCAL_INLINE(int)
183 STRINGLIB(parse_args_finds_byte)(const char *function_name, PyObject *args, 185 STRINGLIB(parse_args_finds_byte)(const char *function_name, PyObject *args,
184 PyObject **subobj, char *byte, 186 PyObject *kwds, PyObject **subobj, char *byte,
185 Py_ssize_t *start, Py_ssize_t *end) 187 Py_ssize_t *start, Py_ssize_t *end)
186 { 188 {
187 PyObject *tmp_subobj; 189 PyObject *tmp_subobj;
188 Py_ssize_t ival; 190 Py_ssize_t ival;
189 PyObject *err; 191 PyObject *err;
190 192
191 if(!STRINGLIB(parse_args_finds)(function_name, args, &tmp_subobj, 193 if(!STRINGLIB(parse_args_finds)(function_name, args, kwds, &tmp_subobj,
192 start, end)) 194 start, end))
193 return 0; 195 return 0;
194 196
195 if (!PyNumber_Check(tmp_subobj)) { 197 if (!PyNumber_Check(tmp_subobj)) {
196 *subobj = tmp_subobj; 198 *subobj = tmp_subobj;
197 return 1; 199 return 1;
198 } 200 }
199 201
200 ival = PyNumber_AsSsize_t(tmp_subobj, PyExc_OverflowError); 202 ival = PyNumber_AsSsize_t(tmp_subobj, PyExc_OverflowError);
201 if (ival == -1) { 203 if (ival == -1) {
202 err = PyErr_Occurred(); 204 err = PyErr_Occurred();
203 if (err && !PyErr_GivenExceptionMatches(err, PyExc_OverflowError)) { 205 if (err && !PyErr_GivenExceptionMatches(err, PyExc_OverflowError)) {
204 PyErr_Clear(); 206 PyErr_Clear();
205 *subobj = tmp_subobj; 207 *subobj = tmp_subobj;
206 return 1; 208 return 1;
207 } 209 }
208 } 210 }
209 211
210 if (ival < 0 || ival > 255) { 212 if (ival < 0 || ival > 255) {
211 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); 213 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
212 return 0; 214 return 0;
213 } 215 }
214 216
215 *subobj = NULL; 217 *subobj = NULL;
216 *byte = (char)ival; 218 *byte = (char)ival;
217 return 1; 219 return 1;
218 } 220 }
219 221
220 #endif /* STRINGLIB_IS_UNICODE */ 222 #endif /* STRINGLIB_IS_UNICODE */
OLDNEW
« no previous file with comments | « Objects/bytesobject.c ('k') | Objects/unicodeobject.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+