1 | n/a | /* |
---|
2 | n/a | * Secret Labs' Regular Expression Engine |
---|
3 | n/a | * |
---|
4 | n/a | * regular expression matching engine |
---|
5 | n/a | * |
---|
6 | n/a | * partial history: |
---|
7 | n/a | * 1999-10-24 fl created (based on existing template matcher code) |
---|
8 | n/a | * 2000-03-06 fl first alpha, sort of |
---|
9 | n/a | * 2000-08-01 fl fixes for 1.6b1 |
---|
10 | n/a | * 2000-08-07 fl use PyOS_CheckStack() if available |
---|
11 | n/a | * 2000-09-20 fl added expand method |
---|
12 | n/a | * 2001-03-20 fl lots of fixes for 2.1b2 |
---|
13 | n/a | * 2001-04-15 fl export copyright as Python attribute, not global |
---|
14 | n/a | * 2001-04-28 fl added __copy__ methods (work in progress) |
---|
15 | n/a | * 2001-05-14 fl fixes for 1.5.2 compatibility |
---|
16 | n/a | * 2001-07-01 fl added BIGCHARSET support (from Martin von Loewis) |
---|
17 | n/a | * 2001-10-18 fl fixed group reset issue (from Matthew Mueller) |
---|
18 | n/a | * 2001-10-20 fl added split primitive; reenable unicode for 1.6/2.0/2.1 |
---|
19 | n/a | * 2001-10-21 fl added sub/subn primitive |
---|
20 | n/a | * 2001-10-24 fl added finditer primitive (for 2.2 only) |
---|
21 | n/a | * 2001-12-07 fl fixed memory leak in sub/subn (Guido van Rossum) |
---|
22 | n/a | * 2002-11-09 fl fixed empty sub/subn return type |
---|
23 | n/a | * 2003-04-18 mvl fully support 4-byte codes |
---|
24 | n/a | * 2003-10-17 gn implemented non recursive scheme |
---|
25 | n/a | * 2013-02-04 mrab added fullmatch primitive |
---|
26 | n/a | * |
---|
27 | n/a | * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved. |
---|
28 | n/a | * |
---|
29 | n/a | * This version of the SRE library can be redistributed under CNRI's |
---|
30 | n/a | * Python 1.6 license. For any other use, please contact Secret Labs |
---|
31 | n/a | * AB (info@pythonware.com). |
---|
32 | n/a | * |
---|
33 | n/a | * Portions of this engine have been developed in cooperation with |
---|
34 | n/a | * CNRI. Hewlett-Packard provided funding for 1.6 integration and |
---|
35 | n/a | * other compatibility work. |
---|
36 | n/a | */ |
---|
37 | n/a | |
---|
38 | n/a | static const char copyright[] = |
---|
39 | n/a | " SRE 2.2.2 Copyright (c) 1997-2002 by Secret Labs AB "; |
---|
40 | n/a | |
---|
41 | n/a | #define PY_SSIZE_T_CLEAN |
---|
42 | n/a | |
---|
43 | n/a | #include "Python.h" |
---|
44 | n/a | #include "structmember.h" /* offsetof */ |
---|
45 | n/a | |
---|
46 | n/a | #include "sre.h" |
---|
47 | n/a | |
---|
48 | n/a | #define SRE_CODE_BITS (8 * sizeof(SRE_CODE)) |
---|
49 | n/a | |
---|
50 | n/a | #include <ctype.h> |
---|
51 | n/a | |
---|
52 | n/a | /* name of this module, minus the leading underscore */ |
---|
53 | n/a | #if !defined(SRE_MODULE) |
---|
54 | n/a | #define SRE_MODULE "sre" |
---|
55 | n/a | #endif |
---|
56 | n/a | |
---|
57 | n/a | #define SRE_PY_MODULE "re" |
---|
58 | n/a | |
---|
59 | n/a | /* defining this one enables tracing */ |
---|
60 | n/a | #undef VERBOSE |
---|
61 | n/a | |
---|
62 | n/a | /* -------------------------------------------------------------------- */ |
---|
63 | n/a | /* optional features */ |
---|
64 | n/a | |
---|
65 | n/a | /* enables copy/deepcopy handling (work in progress) */ |
---|
66 | n/a | #undef USE_BUILTIN_COPY |
---|
67 | n/a | |
---|
68 | n/a | /* -------------------------------------------------------------------- */ |
---|
69 | n/a | |
---|
70 | n/a | #if defined(_MSC_VER) |
---|
71 | n/a | #pragma optimize("agtw", on) /* doesn't seem to make much difference... */ |
---|
72 | n/a | #pragma warning(disable: 4710) /* who cares if functions are not inlined ;-) */ |
---|
73 | n/a | /* fastest possible local call under MSVC */ |
---|
74 | n/a | #define LOCAL(type) static __inline type __fastcall |
---|
75 | n/a | #elif defined(USE_INLINE) |
---|
76 | n/a | #define LOCAL(type) static inline type |
---|
77 | n/a | #else |
---|
78 | n/a | #define LOCAL(type) static type |
---|
79 | n/a | #endif |
---|
80 | n/a | |
---|
81 | n/a | /* error codes */ |
---|
82 | n/a | #define SRE_ERROR_ILLEGAL -1 /* illegal opcode */ |
---|
83 | n/a | #define SRE_ERROR_STATE -2 /* illegal state */ |
---|
84 | n/a | #define SRE_ERROR_RECURSION_LIMIT -3 /* runaway recursion */ |
---|
85 | n/a | #define SRE_ERROR_MEMORY -9 /* out of memory */ |
---|
86 | n/a | #define SRE_ERROR_INTERRUPTED -10 /* signal handler raised exception */ |
---|
87 | n/a | |
---|
88 | n/a | #if defined(VERBOSE) |
---|
89 | n/a | #define TRACE(v) printf v |
---|
90 | n/a | #else |
---|
91 | n/a | #define TRACE(v) |
---|
92 | n/a | #endif |
---|
93 | n/a | |
---|
94 | n/a | /* -------------------------------------------------------------------- */ |
---|
95 | n/a | /* search engine state */ |
---|
96 | n/a | |
---|
97 | n/a | #define SRE_IS_DIGIT(ch)\ |
---|
98 | n/a | ((ch) < 128 && Py_ISDIGIT(ch)) |
---|
99 | n/a | #define SRE_IS_SPACE(ch)\ |
---|
100 | n/a | ((ch) < 128 && Py_ISSPACE(ch)) |
---|
101 | n/a | #define SRE_IS_LINEBREAK(ch)\ |
---|
102 | n/a | ((ch) == '\n') |
---|
103 | n/a | #define SRE_IS_ALNUM(ch)\ |
---|
104 | n/a | ((ch) < 128 && Py_ISALNUM(ch)) |
---|
105 | n/a | #define SRE_IS_WORD(ch)\ |
---|
106 | n/a | ((ch) < 128 && (Py_ISALNUM(ch) || (ch) == '_')) |
---|
107 | n/a | |
---|
108 | n/a | static unsigned int sre_lower(unsigned int ch) |
---|
109 | n/a | { |
---|
110 | n/a | return ((ch) < 128 ? Py_TOLOWER(ch) : ch); |
---|
111 | n/a | } |
---|
112 | n/a | |
---|
113 | n/a | static unsigned int sre_upper(unsigned int ch) |
---|
114 | n/a | { |
---|
115 | n/a | return ((ch) < 128 ? Py_TOUPPER(ch) : ch); |
---|
116 | n/a | } |
---|
117 | n/a | |
---|
118 | n/a | /* locale-specific character predicates */ |
---|
119 | n/a | /* !(c & ~N) == (c < N+1) for any unsigned c, this avoids |
---|
120 | n/a | * warnings when c's type supports only numbers < N+1 */ |
---|
121 | n/a | #define SRE_LOC_IS_ALNUM(ch) (!((ch) & ~255) ? isalnum((ch)) : 0) |
---|
122 | n/a | #define SRE_LOC_IS_WORD(ch) (SRE_LOC_IS_ALNUM((ch)) || (ch) == '_') |
---|
123 | n/a | |
---|
124 | n/a | static unsigned int sre_lower_locale(unsigned int ch) |
---|
125 | n/a | { |
---|
126 | n/a | return ((ch) < 256 ? (unsigned int)tolower((ch)) : ch); |
---|
127 | n/a | } |
---|
128 | n/a | |
---|
129 | n/a | static unsigned int sre_upper_locale(unsigned int ch) |
---|
130 | n/a | { |
---|
131 | n/a | return ((ch) < 256 ? (unsigned int)toupper((ch)) : ch); |
---|
132 | n/a | } |
---|
133 | n/a | |
---|
134 | n/a | /* unicode-specific character predicates */ |
---|
135 | n/a | |
---|
136 | n/a | #define SRE_UNI_IS_DIGIT(ch) Py_UNICODE_ISDECIMAL(ch) |
---|
137 | n/a | #define SRE_UNI_IS_SPACE(ch) Py_UNICODE_ISSPACE(ch) |
---|
138 | n/a | #define SRE_UNI_IS_LINEBREAK(ch) Py_UNICODE_ISLINEBREAK(ch) |
---|
139 | n/a | #define SRE_UNI_IS_ALNUM(ch) Py_UNICODE_ISALNUM(ch) |
---|
140 | n/a | #define SRE_UNI_IS_WORD(ch) (SRE_UNI_IS_ALNUM(ch) || (ch) == '_') |
---|
141 | n/a | |
---|
142 | n/a | static unsigned int sre_lower_unicode(unsigned int ch) |
---|
143 | n/a | { |
---|
144 | n/a | return (unsigned int) Py_UNICODE_TOLOWER(ch); |
---|
145 | n/a | } |
---|
146 | n/a | |
---|
147 | n/a | static unsigned int sre_upper_unicode(unsigned int ch) |
---|
148 | n/a | { |
---|
149 | n/a | return (unsigned int) Py_UNICODE_TOUPPER(ch); |
---|
150 | n/a | } |
---|
151 | n/a | |
---|
152 | n/a | LOCAL(int) |
---|
153 | n/a | sre_category(SRE_CODE category, unsigned int ch) |
---|
154 | n/a | { |
---|
155 | n/a | switch (category) { |
---|
156 | n/a | |
---|
157 | n/a | case SRE_CATEGORY_DIGIT: |
---|
158 | n/a | return SRE_IS_DIGIT(ch); |
---|
159 | n/a | case SRE_CATEGORY_NOT_DIGIT: |
---|
160 | n/a | return !SRE_IS_DIGIT(ch); |
---|
161 | n/a | case SRE_CATEGORY_SPACE: |
---|
162 | n/a | return SRE_IS_SPACE(ch); |
---|
163 | n/a | case SRE_CATEGORY_NOT_SPACE: |
---|
164 | n/a | return !SRE_IS_SPACE(ch); |
---|
165 | n/a | case SRE_CATEGORY_WORD: |
---|
166 | n/a | return SRE_IS_WORD(ch); |
---|
167 | n/a | case SRE_CATEGORY_NOT_WORD: |
---|
168 | n/a | return !SRE_IS_WORD(ch); |
---|
169 | n/a | case SRE_CATEGORY_LINEBREAK: |
---|
170 | n/a | return SRE_IS_LINEBREAK(ch); |
---|
171 | n/a | case SRE_CATEGORY_NOT_LINEBREAK: |
---|
172 | n/a | return !SRE_IS_LINEBREAK(ch); |
---|
173 | n/a | |
---|
174 | n/a | case SRE_CATEGORY_LOC_WORD: |
---|
175 | n/a | return SRE_LOC_IS_WORD(ch); |
---|
176 | n/a | case SRE_CATEGORY_LOC_NOT_WORD: |
---|
177 | n/a | return !SRE_LOC_IS_WORD(ch); |
---|
178 | n/a | |
---|
179 | n/a | case SRE_CATEGORY_UNI_DIGIT: |
---|
180 | n/a | return SRE_UNI_IS_DIGIT(ch); |
---|
181 | n/a | case SRE_CATEGORY_UNI_NOT_DIGIT: |
---|
182 | n/a | return !SRE_UNI_IS_DIGIT(ch); |
---|
183 | n/a | case SRE_CATEGORY_UNI_SPACE: |
---|
184 | n/a | return SRE_UNI_IS_SPACE(ch); |
---|
185 | n/a | case SRE_CATEGORY_UNI_NOT_SPACE: |
---|
186 | n/a | return !SRE_UNI_IS_SPACE(ch); |
---|
187 | n/a | case SRE_CATEGORY_UNI_WORD: |
---|
188 | n/a | return SRE_UNI_IS_WORD(ch); |
---|
189 | n/a | case SRE_CATEGORY_UNI_NOT_WORD: |
---|
190 | n/a | return !SRE_UNI_IS_WORD(ch); |
---|
191 | n/a | case SRE_CATEGORY_UNI_LINEBREAK: |
---|
192 | n/a | return SRE_UNI_IS_LINEBREAK(ch); |
---|
193 | n/a | case SRE_CATEGORY_UNI_NOT_LINEBREAK: |
---|
194 | n/a | return !SRE_UNI_IS_LINEBREAK(ch); |
---|
195 | n/a | } |
---|
196 | n/a | return 0; |
---|
197 | n/a | } |
---|
198 | n/a | |
---|
199 | n/a | /* helpers */ |
---|
200 | n/a | |
---|
201 | n/a | static void |
---|
202 | n/a | data_stack_dealloc(SRE_STATE* state) |
---|
203 | n/a | { |
---|
204 | n/a | if (state->data_stack) { |
---|
205 | n/a | PyMem_FREE(state->data_stack); |
---|
206 | n/a | state->data_stack = NULL; |
---|
207 | n/a | } |
---|
208 | n/a | state->data_stack_size = state->data_stack_base = 0; |
---|
209 | n/a | } |
---|
210 | n/a | |
---|
211 | n/a | static int |
---|
212 | n/a | data_stack_grow(SRE_STATE* state, Py_ssize_t size) |
---|
213 | n/a | { |
---|
214 | n/a | Py_ssize_t minsize, cursize; |
---|
215 | n/a | minsize = state->data_stack_base+size; |
---|
216 | n/a | cursize = state->data_stack_size; |
---|
217 | n/a | if (cursize < minsize) { |
---|
218 | n/a | void* stack; |
---|
219 | n/a | cursize = minsize+minsize/4+1024; |
---|
220 | n/a | TRACE(("allocate/grow stack %" PY_FORMAT_SIZE_T "d\n", cursize)); |
---|
221 | n/a | stack = PyMem_REALLOC(state->data_stack, cursize); |
---|
222 | n/a | if (!stack) { |
---|
223 | n/a | data_stack_dealloc(state); |
---|
224 | n/a | return SRE_ERROR_MEMORY; |
---|
225 | n/a | } |
---|
226 | n/a | state->data_stack = (char *)stack; |
---|
227 | n/a | state->data_stack_size = cursize; |
---|
228 | n/a | } |
---|
229 | n/a | return 0; |
---|
230 | n/a | } |
---|
231 | n/a | |
---|
232 | n/a | /* generate 8-bit version */ |
---|
233 | n/a | |
---|
234 | n/a | #define SRE_CHAR Py_UCS1 |
---|
235 | n/a | #define SIZEOF_SRE_CHAR 1 |
---|
236 | n/a | #define SRE(F) sre_ucs1_##F |
---|
237 | n/a | #include "sre_lib.h" |
---|
238 | n/a | |
---|
239 | n/a | /* generate 16-bit unicode version */ |
---|
240 | n/a | |
---|
241 | n/a | #define SRE_CHAR Py_UCS2 |
---|
242 | n/a | #define SIZEOF_SRE_CHAR 2 |
---|
243 | n/a | #define SRE(F) sre_ucs2_##F |
---|
244 | n/a | #include "sre_lib.h" |
---|
245 | n/a | |
---|
246 | n/a | /* generate 32-bit unicode version */ |
---|
247 | n/a | |
---|
248 | n/a | #define SRE_CHAR Py_UCS4 |
---|
249 | n/a | #define SIZEOF_SRE_CHAR 4 |
---|
250 | n/a | #define SRE(F) sre_ucs4_##F |
---|
251 | n/a | #include "sre_lib.h" |
---|
252 | n/a | |
---|
253 | n/a | /* -------------------------------------------------------------------- */ |
---|
254 | n/a | /* factories and destructors */ |
---|
255 | n/a | |
---|
256 | n/a | /* see sre.h for object declarations */ |
---|
257 | n/a | static PyObject*pattern_new_match(PatternObject*, SRE_STATE*, Py_ssize_t); |
---|
258 | n/a | static PyObject *pattern_scanner(PatternObject *, PyObject *, Py_ssize_t, Py_ssize_t); |
---|
259 | n/a | |
---|
260 | n/a | |
---|
261 | n/a | /*[clinic input] |
---|
262 | n/a | module _sre |
---|
263 | n/a | class _sre.SRE_Pattern "PatternObject *" "&Pattern_Type" |
---|
264 | n/a | class _sre.SRE_Match "MatchObject *" "&Match_Type" |
---|
265 | n/a | class _sre.SRE_Scanner "ScannerObject *" "&Scanner_Type" |
---|
266 | n/a | [clinic start generated code]*/ |
---|
267 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=b0230ec19a0deac8]*/ |
---|
268 | n/a | |
---|
269 | n/a | static PyTypeObject Pattern_Type; |
---|
270 | n/a | static PyTypeObject Match_Type; |
---|
271 | n/a | static PyTypeObject Scanner_Type; |
---|
272 | n/a | |
---|
273 | n/a | /*[clinic input] |
---|
274 | n/a | _sre.getcodesize -> int |
---|
275 | n/a | [clinic start generated code]*/ |
---|
276 | n/a | |
---|
277 | n/a | static int |
---|
278 | n/a | _sre_getcodesize_impl(PyObject *module) |
---|
279 | n/a | /*[clinic end generated code: output=e0db7ce34a6dd7b1 input=bd6f6ecf4916bb2b]*/ |
---|
280 | n/a | { |
---|
281 | n/a | return sizeof(SRE_CODE); |
---|
282 | n/a | } |
---|
283 | n/a | |
---|
284 | n/a | /*[clinic input] |
---|
285 | n/a | _sre.getlower -> int |
---|
286 | n/a | |
---|
287 | n/a | character: int |
---|
288 | n/a | flags: int |
---|
289 | n/a | / |
---|
290 | n/a | |
---|
291 | n/a | [clinic start generated code]*/ |
---|
292 | n/a | |
---|
293 | n/a | static int |
---|
294 | n/a | _sre_getlower_impl(PyObject *module, int character, int flags) |
---|
295 | n/a | /*[clinic end generated code: output=47eebc4c1214feb5 input=087d2f1c44bbca6f]*/ |
---|
296 | n/a | { |
---|
297 | n/a | if (flags & SRE_FLAG_LOCALE) |
---|
298 | n/a | return sre_lower_locale(character); |
---|
299 | n/a | if (flags & SRE_FLAG_UNICODE) |
---|
300 | n/a | return sre_lower_unicode(character); |
---|
301 | n/a | return sre_lower(character); |
---|
302 | n/a | } |
---|
303 | n/a | |
---|
304 | n/a | LOCAL(void) |
---|
305 | n/a | state_reset(SRE_STATE* state) |
---|
306 | n/a | { |
---|
307 | n/a | /* FIXME: dynamic! */ |
---|
308 | n/a | /*memset(state->mark, 0, sizeof(*state->mark) * SRE_MARK_SIZE);*/ |
---|
309 | n/a | |
---|
310 | n/a | state->lastmark = -1; |
---|
311 | n/a | state->lastindex = -1; |
---|
312 | n/a | |
---|
313 | n/a | state->repeat = NULL; |
---|
314 | n/a | |
---|
315 | n/a | data_stack_dealloc(state); |
---|
316 | n/a | } |
---|
317 | n/a | |
---|
318 | n/a | static void* |
---|
319 | n/a | getstring(PyObject* string, Py_ssize_t* p_length, |
---|
320 | n/a | int* p_isbytes, int* p_charsize, |
---|
321 | n/a | Py_buffer *view) |
---|
322 | n/a | { |
---|
323 | n/a | /* given a python object, return a data pointer, a length (in |
---|
324 | n/a | characters), and a character size. return NULL if the object |
---|
325 | n/a | is not a string (or not compatible) */ |
---|
326 | n/a | |
---|
327 | n/a | /* Unicode objects do not support the buffer API. So, get the data |
---|
328 | n/a | directly instead. */ |
---|
329 | n/a | if (PyUnicode_Check(string)) { |
---|
330 | n/a | if (PyUnicode_READY(string) == -1) |
---|
331 | n/a | return NULL; |
---|
332 | n/a | *p_length = PyUnicode_GET_LENGTH(string); |
---|
333 | n/a | *p_charsize = PyUnicode_KIND(string); |
---|
334 | n/a | *p_isbytes = 0; |
---|
335 | n/a | return PyUnicode_DATA(string); |
---|
336 | n/a | } |
---|
337 | n/a | |
---|
338 | n/a | /* get pointer to byte string buffer */ |
---|
339 | n/a | if (PyObject_GetBuffer(string, view, PyBUF_SIMPLE) != 0) { |
---|
340 | n/a | PyErr_SetString(PyExc_TypeError, "expected string or bytes-like object"); |
---|
341 | n/a | return NULL; |
---|
342 | n/a | } |
---|
343 | n/a | |
---|
344 | n/a | *p_length = view->len; |
---|
345 | n/a | *p_charsize = 1; |
---|
346 | n/a | *p_isbytes = 1; |
---|
347 | n/a | |
---|
348 | n/a | if (view->buf == NULL) { |
---|
349 | n/a | PyErr_SetString(PyExc_ValueError, "Buffer is NULL"); |
---|
350 | n/a | PyBuffer_Release(view); |
---|
351 | n/a | view->buf = NULL; |
---|
352 | n/a | return NULL; |
---|
353 | n/a | } |
---|
354 | n/a | return view->buf; |
---|
355 | n/a | } |
---|
356 | n/a | |
---|
357 | n/a | LOCAL(PyObject*) |
---|
358 | n/a | state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string, |
---|
359 | n/a | Py_ssize_t start, Py_ssize_t end) |
---|
360 | n/a | { |
---|
361 | n/a | /* prepare state object */ |
---|
362 | n/a | |
---|
363 | n/a | Py_ssize_t length; |
---|
364 | n/a | int isbytes, charsize; |
---|
365 | n/a | void* ptr; |
---|
366 | n/a | |
---|
367 | n/a | memset(state, 0, sizeof(SRE_STATE)); |
---|
368 | n/a | |
---|
369 | n/a | state->mark = PyMem_New(void *, pattern->groups * 2); |
---|
370 | n/a | if (!state->mark) { |
---|
371 | n/a | PyErr_NoMemory(); |
---|
372 | n/a | goto err; |
---|
373 | n/a | } |
---|
374 | n/a | state->lastmark = -1; |
---|
375 | n/a | state->lastindex = -1; |
---|
376 | n/a | |
---|
377 | n/a | state->buffer.buf = NULL; |
---|
378 | n/a | ptr = getstring(string, &length, &isbytes, &charsize, &state->buffer); |
---|
379 | n/a | if (!ptr) |
---|
380 | n/a | goto err; |
---|
381 | n/a | |
---|
382 | n/a | if (isbytes && pattern->isbytes == 0) { |
---|
383 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
384 | n/a | "cannot use a string pattern on a bytes-like object"); |
---|
385 | n/a | goto err; |
---|
386 | n/a | } |
---|
387 | n/a | if (!isbytes && pattern->isbytes > 0) { |
---|
388 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
389 | n/a | "cannot use a bytes pattern on a string-like object"); |
---|
390 | n/a | goto err; |
---|
391 | n/a | } |
---|
392 | n/a | |
---|
393 | n/a | /* adjust boundaries */ |
---|
394 | n/a | if (start < 0) |
---|
395 | n/a | start = 0; |
---|
396 | n/a | else if (start > length) |
---|
397 | n/a | start = length; |
---|
398 | n/a | |
---|
399 | n/a | if (end < 0) |
---|
400 | n/a | end = 0; |
---|
401 | n/a | else if (end > length) |
---|
402 | n/a | end = length; |
---|
403 | n/a | |
---|
404 | n/a | state->isbytes = isbytes; |
---|
405 | n/a | state->charsize = charsize; |
---|
406 | n/a | |
---|
407 | n/a | state->beginning = ptr; |
---|
408 | n/a | |
---|
409 | n/a | state->start = (void*) ((char*) ptr + start * state->charsize); |
---|
410 | n/a | state->end = (void*) ((char*) ptr + end * state->charsize); |
---|
411 | n/a | |
---|
412 | n/a | Py_INCREF(string); |
---|
413 | n/a | state->string = string; |
---|
414 | n/a | state->pos = start; |
---|
415 | n/a | state->endpos = end; |
---|
416 | n/a | |
---|
417 | n/a | if (pattern->flags & SRE_FLAG_LOCALE) { |
---|
418 | n/a | state->lower = sre_lower_locale; |
---|
419 | n/a | state->upper = sre_upper_locale; |
---|
420 | n/a | } |
---|
421 | n/a | else if (pattern->flags & SRE_FLAG_UNICODE) { |
---|
422 | n/a | state->lower = sre_lower_unicode; |
---|
423 | n/a | state->upper = sre_upper_unicode; |
---|
424 | n/a | } |
---|
425 | n/a | else { |
---|
426 | n/a | state->lower = sre_lower; |
---|
427 | n/a | state->upper = sre_upper; |
---|
428 | n/a | } |
---|
429 | n/a | |
---|
430 | n/a | return string; |
---|
431 | n/a | err: |
---|
432 | n/a | PyMem_Del(state->mark); |
---|
433 | n/a | state->mark = NULL; |
---|
434 | n/a | if (state->buffer.buf) |
---|
435 | n/a | PyBuffer_Release(&state->buffer); |
---|
436 | n/a | return NULL; |
---|
437 | n/a | } |
---|
438 | n/a | |
---|
439 | n/a | LOCAL(void) |
---|
440 | n/a | state_fini(SRE_STATE* state) |
---|
441 | n/a | { |
---|
442 | n/a | if (state->buffer.buf) |
---|
443 | n/a | PyBuffer_Release(&state->buffer); |
---|
444 | n/a | Py_XDECREF(state->string); |
---|
445 | n/a | data_stack_dealloc(state); |
---|
446 | n/a | PyMem_Del(state->mark); |
---|
447 | n/a | state->mark = NULL; |
---|
448 | n/a | } |
---|
449 | n/a | |
---|
450 | n/a | /* calculate offset from start of string */ |
---|
451 | n/a | #define STATE_OFFSET(state, member)\ |
---|
452 | n/a | (((char*)(member) - (char*)(state)->beginning) / (state)->charsize) |
---|
453 | n/a | |
---|
454 | n/a | LOCAL(PyObject*) |
---|
455 | n/a | getslice(int isbytes, const void *ptr, |
---|
456 | n/a | PyObject* string, Py_ssize_t start, Py_ssize_t end) |
---|
457 | n/a | { |
---|
458 | n/a | if (isbytes) { |
---|
459 | n/a | if (PyBytes_CheckExact(string) && |
---|
460 | n/a | start == 0 && end == PyBytes_GET_SIZE(string)) { |
---|
461 | n/a | Py_INCREF(string); |
---|
462 | n/a | return string; |
---|
463 | n/a | } |
---|
464 | n/a | return PyBytes_FromStringAndSize( |
---|
465 | n/a | (const char *)ptr + start, end - start); |
---|
466 | n/a | } |
---|
467 | n/a | else { |
---|
468 | n/a | return PyUnicode_Substring(string, start, end); |
---|
469 | n/a | } |
---|
470 | n/a | } |
---|
471 | n/a | |
---|
472 | n/a | LOCAL(PyObject*) |
---|
473 | n/a | state_getslice(SRE_STATE* state, Py_ssize_t index, PyObject* string, int empty) |
---|
474 | n/a | { |
---|
475 | n/a | Py_ssize_t i, j; |
---|
476 | n/a | |
---|
477 | n/a | index = (index - 1) * 2; |
---|
478 | n/a | |
---|
479 | n/a | if (string == Py_None || index >= state->lastmark || !state->mark[index] || !state->mark[index+1]) { |
---|
480 | n/a | if (empty) |
---|
481 | n/a | /* want empty string */ |
---|
482 | n/a | i = j = 0; |
---|
483 | n/a | else { |
---|
484 | n/a | Py_RETURN_NONE; |
---|
485 | n/a | } |
---|
486 | n/a | } else { |
---|
487 | n/a | i = STATE_OFFSET(state, state->mark[index]); |
---|
488 | n/a | j = STATE_OFFSET(state, state->mark[index+1]); |
---|
489 | n/a | } |
---|
490 | n/a | |
---|
491 | n/a | return getslice(state->isbytes, state->beginning, string, i, j); |
---|
492 | n/a | } |
---|
493 | n/a | |
---|
494 | n/a | static void |
---|
495 | n/a | pattern_error(Py_ssize_t status) |
---|
496 | n/a | { |
---|
497 | n/a | switch (status) { |
---|
498 | n/a | case SRE_ERROR_RECURSION_LIMIT: |
---|
499 | n/a | /* This error code seems to be unused. */ |
---|
500 | n/a | PyErr_SetString( |
---|
501 | n/a | PyExc_RecursionError, |
---|
502 | n/a | "maximum recursion limit exceeded" |
---|
503 | n/a | ); |
---|
504 | n/a | break; |
---|
505 | n/a | case SRE_ERROR_MEMORY: |
---|
506 | n/a | PyErr_NoMemory(); |
---|
507 | n/a | break; |
---|
508 | n/a | case SRE_ERROR_INTERRUPTED: |
---|
509 | n/a | /* An exception has already been raised, so let it fly */ |
---|
510 | n/a | break; |
---|
511 | n/a | default: |
---|
512 | n/a | /* other error codes indicate compiler/engine bugs */ |
---|
513 | n/a | PyErr_SetString( |
---|
514 | n/a | PyExc_RuntimeError, |
---|
515 | n/a | "internal error in regular expression engine" |
---|
516 | n/a | ); |
---|
517 | n/a | } |
---|
518 | n/a | } |
---|
519 | n/a | |
---|
520 | n/a | static void |
---|
521 | n/a | pattern_dealloc(PatternObject* self) |
---|
522 | n/a | { |
---|
523 | n/a | if (self->weakreflist != NULL) |
---|
524 | n/a | PyObject_ClearWeakRefs((PyObject *) self); |
---|
525 | n/a | Py_XDECREF(self->pattern); |
---|
526 | n/a | Py_XDECREF(self->groupindex); |
---|
527 | n/a | Py_XDECREF(self->indexgroup); |
---|
528 | n/a | PyObject_DEL(self); |
---|
529 | n/a | } |
---|
530 | n/a | |
---|
531 | n/a | LOCAL(Py_ssize_t) |
---|
532 | n/a | sre_match(SRE_STATE* state, SRE_CODE* pattern, int match_all) |
---|
533 | n/a | { |
---|
534 | n/a | if (state->charsize == 1) |
---|
535 | n/a | return sre_ucs1_match(state, pattern, match_all); |
---|
536 | n/a | if (state->charsize == 2) |
---|
537 | n/a | return sre_ucs2_match(state, pattern, match_all); |
---|
538 | n/a | assert(state->charsize == 4); |
---|
539 | n/a | return sre_ucs4_match(state, pattern, match_all); |
---|
540 | n/a | } |
---|
541 | n/a | |
---|
542 | n/a | LOCAL(Py_ssize_t) |
---|
543 | n/a | sre_search(SRE_STATE* state, SRE_CODE* pattern) |
---|
544 | n/a | { |
---|
545 | n/a | if (state->charsize == 1) |
---|
546 | n/a | return sre_ucs1_search(state, pattern); |
---|
547 | n/a | if (state->charsize == 2) |
---|
548 | n/a | return sre_ucs2_search(state, pattern); |
---|
549 | n/a | assert(state->charsize == 4); |
---|
550 | n/a | return sre_ucs4_search(state, pattern); |
---|
551 | n/a | } |
---|
552 | n/a | |
---|
553 | n/a | /*[clinic input] |
---|
554 | n/a | _sre.SRE_Pattern.match |
---|
555 | n/a | |
---|
556 | n/a | string: object |
---|
557 | n/a | pos: Py_ssize_t = 0 |
---|
558 | n/a | endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize |
---|
559 | n/a | |
---|
560 | n/a | Matches zero or more characters at the beginning of the string. |
---|
561 | n/a | [clinic start generated code]*/ |
---|
562 | n/a | |
---|
563 | n/a | static PyObject * |
---|
564 | n/a | _sre_SRE_Pattern_match_impl(PatternObject *self, PyObject *string, |
---|
565 | n/a | Py_ssize_t pos, Py_ssize_t endpos) |
---|
566 | n/a | /*[clinic end generated code: output=ea2d838888510661 input=a2ba191647abebe5]*/ |
---|
567 | n/a | { |
---|
568 | n/a | SRE_STATE state; |
---|
569 | n/a | Py_ssize_t status; |
---|
570 | n/a | PyObject *match; |
---|
571 | n/a | |
---|
572 | n/a | if (!state_init(&state, (PatternObject *)self, string, pos, endpos)) |
---|
573 | n/a | return NULL; |
---|
574 | n/a | |
---|
575 | n/a | state.ptr = state.start; |
---|
576 | n/a | |
---|
577 | n/a | TRACE(("|%p|%p|MATCH\n", PatternObject_GetCode(self), state.ptr)); |
---|
578 | n/a | |
---|
579 | n/a | status = sre_match(&state, PatternObject_GetCode(self), 0); |
---|
580 | n/a | |
---|
581 | n/a | TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr)); |
---|
582 | n/a | if (PyErr_Occurred()) { |
---|
583 | n/a | state_fini(&state); |
---|
584 | n/a | return NULL; |
---|
585 | n/a | } |
---|
586 | n/a | |
---|
587 | n/a | match = pattern_new_match(self, &state, status); |
---|
588 | n/a | state_fini(&state); |
---|
589 | n/a | return match; |
---|
590 | n/a | } |
---|
591 | n/a | |
---|
592 | n/a | /*[clinic input] |
---|
593 | n/a | _sre.SRE_Pattern.fullmatch |
---|
594 | n/a | |
---|
595 | n/a | string: object |
---|
596 | n/a | pos: Py_ssize_t = 0 |
---|
597 | n/a | endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize |
---|
598 | n/a | |
---|
599 | n/a | Matches against all of the string |
---|
600 | n/a | [clinic start generated code]*/ |
---|
601 | n/a | |
---|
602 | n/a | static PyObject * |
---|
603 | n/a | _sre_SRE_Pattern_fullmatch_impl(PatternObject *self, PyObject *string, |
---|
604 | n/a | Py_ssize_t pos, Py_ssize_t endpos) |
---|
605 | n/a | /*[clinic end generated code: output=5833c47782a35f4a input=a6f640614aaefceb]*/ |
---|
606 | n/a | { |
---|
607 | n/a | SRE_STATE state; |
---|
608 | n/a | Py_ssize_t status; |
---|
609 | n/a | PyObject *match; |
---|
610 | n/a | |
---|
611 | n/a | if (!state_init(&state, self, string, pos, endpos)) |
---|
612 | n/a | return NULL; |
---|
613 | n/a | |
---|
614 | n/a | state.ptr = state.start; |
---|
615 | n/a | |
---|
616 | n/a | TRACE(("|%p|%p|FULLMATCH\n", PatternObject_GetCode(self), state.ptr)); |
---|
617 | n/a | |
---|
618 | n/a | status = sre_match(&state, PatternObject_GetCode(self), 1); |
---|
619 | n/a | |
---|
620 | n/a | TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr)); |
---|
621 | n/a | if (PyErr_Occurred()) { |
---|
622 | n/a | state_fini(&state); |
---|
623 | n/a | return NULL; |
---|
624 | n/a | } |
---|
625 | n/a | |
---|
626 | n/a | match = pattern_new_match(self, &state, status); |
---|
627 | n/a | state_fini(&state); |
---|
628 | n/a | return match; |
---|
629 | n/a | } |
---|
630 | n/a | |
---|
631 | n/a | /*[clinic input] |
---|
632 | n/a | _sre.SRE_Pattern.search |
---|
633 | n/a | |
---|
634 | n/a | string: object |
---|
635 | n/a | pos: Py_ssize_t = 0 |
---|
636 | n/a | endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize |
---|
637 | n/a | |
---|
638 | n/a | Scan through string looking for a match, and return a corresponding match object instance. |
---|
639 | n/a | |
---|
640 | n/a | Return None if no position in the string matches. |
---|
641 | n/a | [clinic start generated code]*/ |
---|
642 | n/a | |
---|
643 | n/a | static PyObject * |
---|
644 | n/a | _sre_SRE_Pattern_search_impl(PatternObject *self, PyObject *string, |
---|
645 | n/a | Py_ssize_t pos, Py_ssize_t endpos) |
---|
646 | n/a | /*[clinic end generated code: output=25f302a644e951e8 input=4ae5cb7dc38fed1b]*/ |
---|
647 | n/a | { |
---|
648 | n/a | SRE_STATE state; |
---|
649 | n/a | Py_ssize_t status; |
---|
650 | n/a | PyObject *match; |
---|
651 | n/a | |
---|
652 | n/a | if (!state_init(&state, self, string, pos, endpos)) |
---|
653 | n/a | return NULL; |
---|
654 | n/a | |
---|
655 | n/a | TRACE(("|%p|%p|SEARCH\n", PatternObject_GetCode(self), state.ptr)); |
---|
656 | n/a | |
---|
657 | n/a | status = sre_search(&state, PatternObject_GetCode(self)); |
---|
658 | n/a | |
---|
659 | n/a | TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr)); |
---|
660 | n/a | |
---|
661 | n/a | if (PyErr_Occurred()) { |
---|
662 | n/a | state_fini(&state); |
---|
663 | n/a | return NULL; |
---|
664 | n/a | } |
---|
665 | n/a | |
---|
666 | n/a | match = pattern_new_match(self, &state, status); |
---|
667 | n/a | state_fini(&state); |
---|
668 | n/a | return match; |
---|
669 | n/a | } |
---|
670 | n/a | |
---|
671 | n/a | static PyObject* |
---|
672 | n/a | call(const char* module, const char* function, PyObject* args) |
---|
673 | n/a | { |
---|
674 | n/a | PyObject* name; |
---|
675 | n/a | PyObject* mod; |
---|
676 | n/a | PyObject* func; |
---|
677 | n/a | PyObject* result; |
---|
678 | n/a | |
---|
679 | n/a | if (!args) |
---|
680 | n/a | return NULL; |
---|
681 | n/a | name = PyUnicode_FromString(module); |
---|
682 | n/a | if (!name) |
---|
683 | n/a | return NULL; |
---|
684 | n/a | mod = PyImport_Import(name); |
---|
685 | n/a | Py_DECREF(name); |
---|
686 | n/a | if (!mod) |
---|
687 | n/a | return NULL; |
---|
688 | n/a | func = PyObject_GetAttrString(mod, function); |
---|
689 | n/a | Py_DECREF(mod); |
---|
690 | n/a | if (!func) |
---|
691 | n/a | return NULL; |
---|
692 | n/a | result = PyObject_CallObject(func, args); |
---|
693 | n/a | Py_DECREF(func); |
---|
694 | n/a | Py_DECREF(args); |
---|
695 | n/a | return result; |
---|
696 | n/a | } |
---|
697 | n/a | |
---|
698 | n/a | #ifdef USE_BUILTIN_COPY |
---|
699 | n/a | static int |
---|
700 | n/a | deepcopy(PyObject** object, PyObject* memo) |
---|
701 | n/a | { |
---|
702 | n/a | PyObject* copy; |
---|
703 | n/a | |
---|
704 | n/a | copy = call( |
---|
705 | n/a | "copy", "deepcopy", |
---|
706 | n/a | PyTuple_Pack(2, *object, memo) |
---|
707 | n/a | ); |
---|
708 | n/a | if (!copy) |
---|
709 | n/a | return 0; |
---|
710 | n/a | |
---|
711 | n/a | Py_SETREF(*object, copy); |
---|
712 | n/a | |
---|
713 | n/a | return 1; /* success */ |
---|
714 | n/a | } |
---|
715 | n/a | #endif |
---|
716 | n/a | |
---|
717 | n/a | /*[clinic input] |
---|
718 | n/a | _sre.SRE_Pattern.findall |
---|
719 | n/a | |
---|
720 | n/a | string: object |
---|
721 | n/a | pos: Py_ssize_t = 0 |
---|
722 | n/a | endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize |
---|
723 | n/a | |
---|
724 | n/a | Return a list of all non-overlapping matches of pattern in string. |
---|
725 | n/a | [clinic start generated code]*/ |
---|
726 | n/a | |
---|
727 | n/a | static PyObject * |
---|
728 | n/a | _sre_SRE_Pattern_findall_impl(PatternObject *self, PyObject *string, |
---|
729 | n/a | Py_ssize_t pos, Py_ssize_t endpos) |
---|
730 | n/a | /*[clinic end generated code: output=f4966baceea60aca input=5b6a4ee799741563]*/ |
---|
731 | n/a | { |
---|
732 | n/a | SRE_STATE state; |
---|
733 | n/a | PyObject* list; |
---|
734 | n/a | Py_ssize_t status; |
---|
735 | n/a | Py_ssize_t i, b, e; |
---|
736 | n/a | |
---|
737 | n/a | if (!state_init(&state, self, string, pos, endpos)) |
---|
738 | n/a | return NULL; |
---|
739 | n/a | |
---|
740 | n/a | list = PyList_New(0); |
---|
741 | n/a | if (!list) { |
---|
742 | n/a | state_fini(&state); |
---|
743 | n/a | return NULL; |
---|
744 | n/a | } |
---|
745 | n/a | |
---|
746 | n/a | while (state.start <= state.end) { |
---|
747 | n/a | |
---|
748 | n/a | PyObject* item; |
---|
749 | n/a | |
---|
750 | n/a | state_reset(&state); |
---|
751 | n/a | |
---|
752 | n/a | state.ptr = state.start; |
---|
753 | n/a | |
---|
754 | n/a | status = sre_search(&state, PatternObject_GetCode(self)); |
---|
755 | n/a | if (PyErr_Occurred()) |
---|
756 | n/a | goto error; |
---|
757 | n/a | |
---|
758 | n/a | if (status <= 0) { |
---|
759 | n/a | if (status == 0) |
---|
760 | n/a | break; |
---|
761 | n/a | pattern_error(status); |
---|
762 | n/a | goto error; |
---|
763 | n/a | } |
---|
764 | n/a | |
---|
765 | n/a | /* don't bother to build a match object */ |
---|
766 | n/a | switch (self->groups) { |
---|
767 | n/a | case 0: |
---|
768 | n/a | b = STATE_OFFSET(&state, state.start); |
---|
769 | n/a | e = STATE_OFFSET(&state, state.ptr); |
---|
770 | n/a | item = getslice(state.isbytes, state.beginning, |
---|
771 | n/a | string, b, e); |
---|
772 | n/a | if (!item) |
---|
773 | n/a | goto error; |
---|
774 | n/a | break; |
---|
775 | n/a | case 1: |
---|
776 | n/a | item = state_getslice(&state, 1, string, 1); |
---|
777 | n/a | if (!item) |
---|
778 | n/a | goto error; |
---|
779 | n/a | break; |
---|
780 | n/a | default: |
---|
781 | n/a | item = PyTuple_New(self->groups); |
---|
782 | n/a | if (!item) |
---|
783 | n/a | goto error; |
---|
784 | n/a | for (i = 0; i < self->groups; i++) { |
---|
785 | n/a | PyObject* o = state_getslice(&state, i+1, string, 1); |
---|
786 | n/a | if (!o) { |
---|
787 | n/a | Py_DECREF(item); |
---|
788 | n/a | goto error; |
---|
789 | n/a | } |
---|
790 | n/a | PyTuple_SET_ITEM(item, i, o); |
---|
791 | n/a | } |
---|
792 | n/a | break; |
---|
793 | n/a | } |
---|
794 | n/a | |
---|
795 | n/a | status = PyList_Append(list, item); |
---|
796 | n/a | Py_DECREF(item); |
---|
797 | n/a | if (status < 0) |
---|
798 | n/a | goto error; |
---|
799 | n/a | |
---|
800 | n/a | if (state.ptr == state.start) |
---|
801 | n/a | state.start = (void*) ((char*) state.ptr + state.charsize); |
---|
802 | n/a | else |
---|
803 | n/a | state.start = state.ptr; |
---|
804 | n/a | |
---|
805 | n/a | } |
---|
806 | n/a | |
---|
807 | n/a | state_fini(&state); |
---|
808 | n/a | return list; |
---|
809 | n/a | |
---|
810 | n/a | error: |
---|
811 | n/a | Py_DECREF(list); |
---|
812 | n/a | state_fini(&state); |
---|
813 | n/a | return NULL; |
---|
814 | n/a | |
---|
815 | n/a | } |
---|
816 | n/a | |
---|
817 | n/a | /*[clinic input] |
---|
818 | n/a | _sre.SRE_Pattern.finditer |
---|
819 | n/a | |
---|
820 | n/a | string: object |
---|
821 | n/a | pos: Py_ssize_t = 0 |
---|
822 | n/a | endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize |
---|
823 | n/a | |
---|
824 | n/a | Return an iterator over all non-overlapping matches for the RE pattern in string. |
---|
825 | n/a | |
---|
826 | n/a | For each match, the iterator returns a match object. |
---|
827 | n/a | [clinic start generated code]*/ |
---|
828 | n/a | |
---|
829 | n/a | static PyObject * |
---|
830 | n/a | _sre_SRE_Pattern_finditer_impl(PatternObject *self, PyObject *string, |
---|
831 | n/a | Py_ssize_t pos, Py_ssize_t endpos) |
---|
832 | n/a | /*[clinic end generated code: output=0bbb1a0aeb38bb14 input=612aab69e9fe08e4]*/ |
---|
833 | n/a | { |
---|
834 | n/a | PyObject* scanner; |
---|
835 | n/a | PyObject* search; |
---|
836 | n/a | PyObject* iterator; |
---|
837 | n/a | |
---|
838 | n/a | scanner = pattern_scanner(self, string, pos, endpos); |
---|
839 | n/a | if (!scanner) |
---|
840 | n/a | return NULL; |
---|
841 | n/a | |
---|
842 | n/a | search = PyObject_GetAttrString(scanner, "search"); |
---|
843 | n/a | Py_DECREF(scanner); |
---|
844 | n/a | if (!search) |
---|
845 | n/a | return NULL; |
---|
846 | n/a | |
---|
847 | n/a | iterator = PyCallIter_New(search, Py_None); |
---|
848 | n/a | Py_DECREF(search); |
---|
849 | n/a | |
---|
850 | n/a | return iterator; |
---|
851 | n/a | } |
---|
852 | n/a | |
---|
853 | n/a | /*[clinic input] |
---|
854 | n/a | _sre.SRE_Pattern.scanner |
---|
855 | n/a | |
---|
856 | n/a | string: object |
---|
857 | n/a | pos: Py_ssize_t = 0 |
---|
858 | n/a | endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize |
---|
859 | n/a | |
---|
860 | n/a | [clinic start generated code]*/ |
---|
861 | n/a | |
---|
862 | n/a | static PyObject * |
---|
863 | n/a | _sre_SRE_Pattern_scanner_impl(PatternObject *self, PyObject *string, |
---|
864 | n/a | Py_ssize_t pos, Py_ssize_t endpos) |
---|
865 | n/a | /*[clinic end generated code: output=54ea548aed33890b input=3aacdbde77a3a637]*/ |
---|
866 | n/a | { |
---|
867 | n/a | return pattern_scanner(self, string, pos, endpos); |
---|
868 | n/a | } |
---|
869 | n/a | |
---|
870 | n/a | /*[clinic input] |
---|
871 | n/a | _sre.SRE_Pattern.split |
---|
872 | n/a | |
---|
873 | n/a | string: object |
---|
874 | n/a | maxsplit: Py_ssize_t = 0 |
---|
875 | n/a | |
---|
876 | n/a | Split string by the occurrences of pattern. |
---|
877 | n/a | [clinic start generated code]*/ |
---|
878 | n/a | |
---|
879 | n/a | static PyObject * |
---|
880 | n/a | _sre_SRE_Pattern_split_impl(PatternObject *self, PyObject *string, |
---|
881 | n/a | Py_ssize_t maxsplit) |
---|
882 | n/a | /*[clinic end generated code: output=7ac66f381c45e0be input=1eeeb10dafc9947a]*/ |
---|
883 | n/a | { |
---|
884 | n/a | SRE_STATE state; |
---|
885 | n/a | PyObject* list; |
---|
886 | n/a | PyObject* item; |
---|
887 | n/a | Py_ssize_t status; |
---|
888 | n/a | Py_ssize_t n; |
---|
889 | n/a | Py_ssize_t i; |
---|
890 | n/a | void* last; |
---|
891 | n/a | |
---|
892 | n/a | assert(self->codesize != 0); |
---|
893 | n/a | if (self->code[0] != SRE_OP_INFO || self->code[3] == 0) { |
---|
894 | n/a | if (self->code[0] == SRE_OP_INFO && self->code[4] == 0) { |
---|
895 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
896 | n/a | "split() requires a non-empty pattern match."); |
---|
897 | n/a | return NULL; |
---|
898 | n/a | } |
---|
899 | n/a | if (PyErr_WarnEx(PyExc_FutureWarning, |
---|
900 | n/a | "split() requires a non-empty pattern match.", |
---|
901 | n/a | 1) < 0) |
---|
902 | n/a | return NULL; |
---|
903 | n/a | } |
---|
904 | n/a | |
---|
905 | n/a | if (!state_init(&state, self, string, 0, PY_SSIZE_T_MAX)) |
---|
906 | n/a | return NULL; |
---|
907 | n/a | |
---|
908 | n/a | list = PyList_New(0); |
---|
909 | n/a | if (!list) { |
---|
910 | n/a | state_fini(&state); |
---|
911 | n/a | return NULL; |
---|
912 | n/a | } |
---|
913 | n/a | |
---|
914 | n/a | n = 0; |
---|
915 | n/a | last = state.start; |
---|
916 | n/a | |
---|
917 | n/a | while (!maxsplit || n < maxsplit) { |
---|
918 | n/a | |
---|
919 | n/a | state_reset(&state); |
---|
920 | n/a | |
---|
921 | n/a | state.ptr = state.start; |
---|
922 | n/a | |
---|
923 | n/a | status = sre_search(&state, PatternObject_GetCode(self)); |
---|
924 | n/a | if (PyErr_Occurred()) |
---|
925 | n/a | goto error; |
---|
926 | n/a | |
---|
927 | n/a | if (status <= 0) { |
---|
928 | n/a | if (status == 0) |
---|
929 | n/a | break; |
---|
930 | n/a | pattern_error(status); |
---|
931 | n/a | goto error; |
---|
932 | n/a | } |
---|
933 | n/a | |
---|
934 | n/a | if (state.start == state.ptr) { |
---|
935 | n/a | if (last == state.end || state.ptr == state.end) |
---|
936 | n/a | break; |
---|
937 | n/a | /* skip one character */ |
---|
938 | n/a | state.start = (void*) ((char*) state.ptr + state.charsize); |
---|
939 | n/a | continue; |
---|
940 | n/a | } |
---|
941 | n/a | |
---|
942 | n/a | /* get segment before this match */ |
---|
943 | n/a | item = getslice(state.isbytes, state.beginning, |
---|
944 | n/a | string, STATE_OFFSET(&state, last), |
---|
945 | n/a | STATE_OFFSET(&state, state.start) |
---|
946 | n/a | ); |
---|
947 | n/a | if (!item) |
---|
948 | n/a | goto error; |
---|
949 | n/a | status = PyList_Append(list, item); |
---|
950 | n/a | Py_DECREF(item); |
---|
951 | n/a | if (status < 0) |
---|
952 | n/a | goto error; |
---|
953 | n/a | |
---|
954 | n/a | /* add groups (if any) */ |
---|
955 | n/a | for (i = 0; i < self->groups; i++) { |
---|
956 | n/a | item = state_getslice(&state, i+1, string, 0); |
---|
957 | n/a | if (!item) |
---|
958 | n/a | goto error; |
---|
959 | n/a | status = PyList_Append(list, item); |
---|
960 | n/a | Py_DECREF(item); |
---|
961 | n/a | if (status < 0) |
---|
962 | n/a | goto error; |
---|
963 | n/a | } |
---|
964 | n/a | |
---|
965 | n/a | n = n + 1; |
---|
966 | n/a | |
---|
967 | n/a | last = state.start = state.ptr; |
---|
968 | n/a | |
---|
969 | n/a | } |
---|
970 | n/a | |
---|
971 | n/a | /* get segment following last match (even if empty) */ |
---|
972 | n/a | item = getslice(state.isbytes, state.beginning, |
---|
973 | n/a | string, STATE_OFFSET(&state, last), state.endpos |
---|
974 | n/a | ); |
---|
975 | n/a | if (!item) |
---|
976 | n/a | goto error; |
---|
977 | n/a | status = PyList_Append(list, item); |
---|
978 | n/a | Py_DECREF(item); |
---|
979 | n/a | if (status < 0) |
---|
980 | n/a | goto error; |
---|
981 | n/a | |
---|
982 | n/a | state_fini(&state); |
---|
983 | n/a | return list; |
---|
984 | n/a | |
---|
985 | n/a | error: |
---|
986 | n/a | Py_DECREF(list); |
---|
987 | n/a | state_fini(&state); |
---|
988 | n/a | return NULL; |
---|
989 | n/a | |
---|
990 | n/a | } |
---|
991 | n/a | |
---|
992 | n/a | static PyObject* |
---|
993 | n/a | pattern_subx(PatternObject* self, PyObject* ptemplate, PyObject* string, |
---|
994 | n/a | Py_ssize_t count, Py_ssize_t subn) |
---|
995 | n/a | { |
---|
996 | n/a | SRE_STATE state; |
---|
997 | n/a | PyObject* list; |
---|
998 | n/a | PyObject* joiner; |
---|
999 | n/a | PyObject* item; |
---|
1000 | n/a | PyObject* filter; |
---|
1001 | n/a | PyObject* match; |
---|
1002 | n/a | void* ptr; |
---|
1003 | n/a | Py_ssize_t status; |
---|
1004 | n/a | Py_ssize_t n; |
---|
1005 | n/a | Py_ssize_t i, b, e; |
---|
1006 | n/a | int isbytes, charsize; |
---|
1007 | n/a | int filter_is_callable; |
---|
1008 | n/a | Py_buffer view; |
---|
1009 | n/a | |
---|
1010 | n/a | if (PyCallable_Check(ptemplate)) { |
---|
1011 | n/a | /* sub/subn takes either a function or a template */ |
---|
1012 | n/a | filter = ptemplate; |
---|
1013 | n/a | Py_INCREF(filter); |
---|
1014 | n/a | filter_is_callable = 1; |
---|
1015 | n/a | } else { |
---|
1016 | n/a | /* if not callable, check if it's a literal string */ |
---|
1017 | n/a | int literal; |
---|
1018 | n/a | view.buf = NULL; |
---|
1019 | n/a | ptr = getstring(ptemplate, &n, &isbytes, &charsize, &view); |
---|
1020 | n/a | b = charsize; |
---|
1021 | n/a | if (ptr) { |
---|
1022 | n/a | if (charsize == 1) |
---|
1023 | n/a | literal = memchr(ptr, '\\', n) == NULL; |
---|
1024 | n/a | else |
---|
1025 | n/a | literal = PyUnicode_FindChar(ptemplate, '\\', 0, n, 1) == -1; |
---|
1026 | n/a | } else { |
---|
1027 | n/a | PyErr_Clear(); |
---|
1028 | n/a | literal = 0; |
---|
1029 | n/a | } |
---|
1030 | n/a | if (view.buf) |
---|
1031 | n/a | PyBuffer_Release(&view); |
---|
1032 | n/a | if (literal) { |
---|
1033 | n/a | filter = ptemplate; |
---|
1034 | n/a | Py_INCREF(filter); |
---|
1035 | n/a | filter_is_callable = 0; |
---|
1036 | n/a | } else { |
---|
1037 | n/a | /* not a literal; hand it over to the template compiler */ |
---|
1038 | n/a | filter = call( |
---|
1039 | n/a | SRE_PY_MODULE, "_subx", |
---|
1040 | n/a | PyTuple_Pack(2, self, ptemplate) |
---|
1041 | n/a | ); |
---|
1042 | n/a | if (!filter) |
---|
1043 | n/a | return NULL; |
---|
1044 | n/a | filter_is_callable = PyCallable_Check(filter); |
---|
1045 | n/a | } |
---|
1046 | n/a | } |
---|
1047 | n/a | |
---|
1048 | n/a | if (!state_init(&state, self, string, 0, PY_SSIZE_T_MAX)) { |
---|
1049 | n/a | Py_DECREF(filter); |
---|
1050 | n/a | return NULL; |
---|
1051 | n/a | } |
---|
1052 | n/a | |
---|
1053 | n/a | list = PyList_New(0); |
---|
1054 | n/a | if (!list) { |
---|
1055 | n/a | Py_DECREF(filter); |
---|
1056 | n/a | state_fini(&state); |
---|
1057 | n/a | return NULL; |
---|
1058 | n/a | } |
---|
1059 | n/a | |
---|
1060 | n/a | n = i = 0; |
---|
1061 | n/a | |
---|
1062 | n/a | while (!count || n < count) { |
---|
1063 | n/a | |
---|
1064 | n/a | state_reset(&state); |
---|
1065 | n/a | |
---|
1066 | n/a | state.ptr = state.start; |
---|
1067 | n/a | |
---|
1068 | n/a | status = sre_search(&state, PatternObject_GetCode(self)); |
---|
1069 | n/a | if (PyErr_Occurred()) |
---|
1070 | n/a | goto error; |
---|
1071 | n/a | |
---|
1072 | n/a | if (status <= 0) { |
---|
1073 | n/a | if (status == 0) |
---|
1074 | n/a | break; |
---|
1075 | n/a | pattern_error(status); |
---|
1076 | n/a | goto error; |
---|
1077 | n/a | } |
---|
1078 | n/a | |
---|
1079 | n/a | b = STATE_OFFSET(&state, state.start); |
---|
1080 | n/a | e = STATE_OFFSET(&state, state.ptr); |
---|
1081 | n/a | |
---|
1082 | n/a | if (i < b) { |
---|
1083 | n/a | /* get segment before this match */ |
---|
1084 | n/a | item = getslice(state.isbytes, state.beginning, |
---|
1085 | n/a | string, i, b); |
---|
1086 | n/a | if (!item) |
---|
1087 | n/a | goto error; |
---|
1088 | n/a | status = PyList_Append(list, item); |
---|
1089 | n/a | Py_DECREF(item); |
---|
1090 | n/a | if (status < 0) |
---|
1091 | n/a | goto error; |
---|
1092 | n/a | |
---|
1093 | n/a | } else if (i == b && i == e && n > 0) |
---|
1094 | n/a | /* ignore empty match on latest position */ |
---|
1095 | n/a | goto next; |
---|
1096 | n/a | |
---|
1097 | n/a | if (filter_is_callable) { |
---|
1098 | n/a | /* pass match object through filter */ |
---|
1099 | n/a | match = pattern_new_match(self, &state, 1); |
---|
1100 | n/a | if (!match) |
---|
1101 | n/a | goto error; |
---|
1102 | n/a | item = PyObject_CallFunctionObjArgs(filter, match, NULL); |
---|
1103 | n/a | Py_DECREF(match); |
---|
1104 | n/a | if (!item) |
---|
1105 | n/a | goto error; |
---|
1106 | n/a | } else { |
---|
1107 | n/a | /* filter is literal string */ |
---|
1108 | n/a | item = filter; |
---|
1109 | n/a | Py_INCREF(item); |
---|
1110 | n/a | } |
---|
1111 | n/a | |
---|
1112 | n/a | /* add to list */ |
---|
1113 | n/a | if (item != Py_None) { |
---|
1114 | n/a | status = PyList_Append(list, item); |
---|
1115 | n/a | Py_DECREF(item); |
---|
1116 | n/a | if (status < 0) |
---|
1117 | n/a | goto error; |
---|
1118 | n/a | } |
---|
1119 | n/a | |
---|
1120 | n/a | i = e; |
---|
1121 | n/a | n = n + 1; |
---|
1122 | n/a | |
---|
1123 | n/a | next: |
---|
1124 | n/a | /* move on */ |
---|
1125 | n/a | if (state.ptr == state.end) |
---|
1126 | n/a | break; |
---|
1127 | n/a | if (state.ptr == state.start) |
---|
1128 | n/a | state.start = (void*) ((char*) state.ptr + state.charsize); |
---|
1129 | n/a | else |
---|
1130 | n/a | state.start = state.ptr; |
---|
1131 | n/a | |
---|
1132 | n/a | } |
---|
1133 | n/a | |
---|
1134 | n/a | /* get segment following last match */ |
---|
1135 | n/a | if (i < state.endpos) { |
---|
1136 | n/a | item = getslice(state.isbytes, state.beginning, |
---|
1137 | n/a | string, i, state.endpos); |
---|
1138 | n/a | if (!item) |
---|
1139 | n/a | goto error; |
---|
1140 | n/a | status = PyList_Append(list, item); |
---|
1141 | n/a | Py_DECREF(item); |
---|
1142 | n/a | if (status < 0) |
---|
1143 | n/a | goto error; |
---|
1144 | n/a | } |
---|
1145 | n/a | |
---|
1146 | n/a | state_fini(&state); |
---|
1147 | n/a | |
---|
1148 | n/a | Py_DECREF(filter); |
---|
1149 | n/a | |
---|
1150 | n/a | /* convert list to single string (also removes list) */ |
---|
1151 | n/a | joiner = getslice(state.isbytes, state.beginning, string, 0, 0); |
---|
1152 | n/a | if (!joiner) { |
---|
1153 | n/a | Py_DECREF(list); |
---|
1154 | n/a | return NULL; |
---|
1155 | n/a | } |
---|
1156 | n/a | if (PyList_GET_SIZE(list) == 0) { |
---|
1157 | n/a | Py_DECREF(list); |
---|
1158 | n/a | item = joiner; |
---|
1159 | n/a | } |
---|
1160 | n/a | else { |
---|
1161 | n/a | if (state.isbytes) |
---|
1162 | n/a | item = _PyBytes_Join(joiner, list); |
---|
1163 | n/a | else |
---|
1164 | n/a | item = PyUnicode_Join(joiner, list); |
---|
1165 | n/a | Py_DECREF(joiner); |
---|
1166 | n/a | Py_DECREF(list); |
---|
1167 | n/a | if (!item) |
---|
1168 | n/a | return NULL; |
---|
1169 | n/a | } |
---|
1170 | n/a | |
---|
1171 | n/a | if (subn) |
---|
1172 | n/a | return Py_BuildValue("Nn", item, n); |
---|
1173 | n/a | |
---|
1174 | n/a | return item; |
---|
1175 | n/a | |
---|
1176 | n/a | error: |
---|
1177 | n/a | Py_DECREF(list); |
---|
1178 | n/a | state_fini(&state); |
---|
1179 | n/a | Py_DECREF(filter); |
---|
1180 | n/a | return NULL; |
---|
1181 | n/a | |
---|
1182 | n/a | } |
---|
1183 | n/a | |
---|
1184 | n/a | /*[clinic input] |
---|
1185 | n/a | _sre.SRE_Pattern.sub |
---|
1186 | n/a | |
---|
1187 | n/a | repl: object |
---|
1188 | n/a | string: object |
---|
1189 | n/a | count: Py_ssize_t = 0 |
---|
1190 | n/a | |
---|
1191 | n/a | Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. |
---|
1192 | n/a | [clinic start generated code]*/ |
---|
1193 | n/a | |
---|
1194 | n/a | static PyObject * |
---|
1195 | n/a | _sre_SRE_Pattern_sub_impl(PatternObject *self, PyObject *repl, |
---|
1196 | n/a | PyObject *string, Py_ssize_t count) |
---|
1197 | n/a | /*[clinic end generated code: output=1dbf2ec3479cba00 input=c53d70be0b3caf86]*/ |
---|
1198 | n/a | { |
---|
1199 | n/a | return pattern_subx(self, repl, string, count, 0); |
---|
1200 | n/a | } |
---|
1201 | n/a | |
---|
1202 | n/a | /*[clinic input] |
---|
1203 | n/a | _sre.SRE_Pattern.subn |
---|
1204 | n/a | |
---|
1205 | n/a | repl: object |
---|
1206 | n/a | string: object |
---|
1207 | n/a | count: Py_ssize_t = 0 |
---|
1208 | n/a | |
---|
1209 | n/a | Return the tuple (new_string, number_of_subs_made) found by replacing the leftmost non-overlapping occurrences of pattern with the replacement repl. |
---|
1210 | n/a | [clinic start generated code]*/ |
---|
1211 | n/a | |
---|
1212 | n/a | static PyObject * |
---|
1213 | n/a | _sre_SRE_Pattern_subn_impl(PatternObject *self, PyObject *repl, |
---|
1214 | n/a | PyObject *string, Py_ssize_t count) |
---|
1215 | n/a | /*[clinic end generated code: output=0d9522cd529e9728 input=e7342d7ce6083577]*/ |
---|
1216 | n/a | { |
---|
1217 | n/a | return pattern_subx(self, repl, string, count, 1); |
---|
1218 | n/a | } |
---|
1219 | n/a | |
---|
1220 | n/a | /*[clinic input] |
---|
1221 | n/a | _sre.SRE_Pattern.__copy__ |
---|
1222 | n/a | |
---|
1223 | n/a | [clinic start generated code]*/ |
---|
1224 | n/a | |
---|
1225 | n/a | static PyObject * |
---|
1226 | n/a | _sre_SRE_Pattern___copy___impl(PatternObject *self) |
---|
1227 | n/a | /*[clinic end generated code: output=85dedc2db1bd8694 input=a730a59d863bc9f5]*/ |
---|
1228 | n/a | { |
---|
1229 | n/a | #ifdef USE_BUILTIN_COPY |
---|
1230 | n/a | PatternObject* copy; |
---|
1231 | n/a | int offset; |
---|
1232 | n/a | |
---|
1233 | n/a | copy = PyObject_NEW_VAR(PatternObject, &Pattern_Type, self->codesize); |
---|
1234 | n/a | if (!copy) |
---|
1235 | n/a | return NULL; |
---|
1236 | n/a | |
---|
1237 | n/a | offset = offsetof(PatternObject, groups); |
---|
1238 | n/a | |
---|
1239 | n/a | Py_XINCREF(self->groupindex); |
---|
1240 | n/a | Py_XINCREF(self->indexgroup); |
---|
1241 | n/a | Py_XINCREF(self->pattern); |
---|
1242 | n/a | |
---|
1243 | n/a | memcpy((char*) copy + offset, (char*) self + offset, |
---|
1244 | n/a | sizeof(PatternObject) + self->codesize * sizeof(SRE_CODE) - offset); |
---|
1245 | n/a | copy->weakreflist = NULL; |
---|
1246 | n/a | |
---|
1247 | n/a | return (PyObject*) copy; |
---|
1248 | n/a | #else |
---|
1249 | n/a | PyErr_SetString(PyExc_TypeError, "cannot copy this pattern object"); |
---|
1250 | n/a | return NULL; |
---|
1251 | n/a | #endif |
---|
1252 | n/a | } |
---|
1253 | n/a | |
---|
1254 | n/a | /*[clinic input] |
---|
1255 | n/a | _sre.SRE_Pattern.__deepcopy__ |
---|
1256 | n/a | |
---|
1257 | n/a | memo: object |
---|
1258 | n/a | |
---|
1259 | n/a | [clinic start generated code]*/ |
---|
1260 | n/a | |
---|
1261 | n/a | static PyObject * |
---|
1262 | n/a | _sre_SRE_Pattern___deepcopy___impl(PatternObject *self, PyObject *memo) |
---|
1263 | n/a | /*[clinic end generated code: output=75efe69bd12c5d7d input=3959719482c07f70]*/ |
---|
1264 | n/a | { |
---|
1265 | n/a | #ifdef USE_BUILTIN_COPY |
---|
1266 | n/a | PatternObject* copy; |
---|
1267 | n/a | |
---|
1268 | n/a | copy = (PatternObject*) pattern_copy(self); |
---|
1269 | n/a | if (!copy) |
---|
1270 | n/a | return NULL; |
---|
1271 | n/a | |
---|
1272 | n/a | if (!deepcopy(©->groupindex, memo) || |
---|
1273 | n/a | !deepcopy(©->indexgroup, memo) || |
---|
1274 | n/a | !deepcopy(©->pattern, memo)) { |
---|
1275 | n/a | Py_DECREF(copy); |
---|
1276 | n/a | return NULL; |
---|
1277 | n/a | } |
---|
1278 | n/a | |
---|
1279 | n/a | #else |
---|
1280 | n/a | PyErr_SetString(PyExc_TypeError, "cannot deepcopy this pattern object"); |
---|
1281 | n/a | return NULL; |
---|
1282 | n/a | #endif |
---|
1283 | n/a | } |
---|
1284 | n/a | |
---|
1285 | n/a | static PyObject * |
---|
1286 | n/a | pattern_repr(PatternObject *obj) |
---|
1287 | n/a | { |
---|
1288 | n/a | static const struct { |
---|
1289 | n/a | const char *name; |
---|
1290 | n/a | int value; |
---|
1291 | n/a | } flag_names[] = { |
---|
1292 | n/a | {"re.TEMPLATE", SRE_FLAG_TEMPLATE}, |
---|
1293 | n/a | {"re.IGNORECASE", SRE_FLAG_IGNORECASE}, |
---|
1294 | n/a | {"re.LOCALE", SRE_FLAG_LOCALE}, |
---|
1295 | n/a | {"re.MULTILINE", SRE_FLAG_MULTILINE}, |
---|
1296 | n/a | {"re.DOTALL", SRE_FLAG_DOTALL}, |
---|
1297 | n/a | {"re.UNICODE", SRE_FLAG_UNICODE}, |
---|
1298 | n/a | {"re.VERBOSE", SRE_FLAG_VERBOSE}, |
---|
1299 | n/a | {"re.DEBUG", SRE_FLAG_DEBUG}, |
---|
1300 | n/a | {"re.ASCII", SRE_FLAG_ASCII}, |
---|
1301 | n/a | }; |
---|
1302 | n/a | PyObject *result = NULL; |
---|
1303 | n/a | PyObject *flag_items; |
---|
1304 | n/a | size_t i; |
---|
1305 | n/a | int flags = obj->flags; |
---|
1306 | n/a | |
---|
1307 | n/a | /* Omit re.UNICODE for valid string patterns. */ |
---|
1308 | n/a | if (obj->isbytes == 0 && |
---|
1309 | n/a | (flags & (SRE_FLAG_LOCALE|SRE_FLAG_UNICODE|SRE_FLAG_ASCII)) == |
---|
1310 | n/a | SRE_FLAG_UNICODE) |
---|
1311 | n/a | flags &= ~SRE_FLAG_UNICODE; |
---|
1312 | n/a | |
---|
1313 | n/a | flag_items = PyList_New(0); |
---|
1314 | n/a | if (!flag_items) |
---|
1315 | n/a | return NULL; |
---|
1316 | n/a | |
---|
1317 | n/a | for (i = 0; i < Py_ARRAY_LENGTH(flag_names); i++) { |
---|
1318 | n/a | if (flags & flag_names[i].value) { |
---|
1319 | n/a | PyObject *item = PyUnicode_FromString(flag_names[i].name); |
---|
1320 | n/a | if (!item) |
---|
1321 | n/a | goto done; |
---|
1322 | n/a | |
---|
1323 | n/a | if (PyList_Append(flag_items, item) < 0) { |
---|
1324 | n/a | Py_DECREF(item); |
---|
1325 | n/a | goto done; |
---|
1326 | n/a | } |
---|
1327 | n/a | Py_DECREF(item); |
---|
1328 | n/a | flags &= ~flag_names[i].value; |
---|
1329 | n/a | } |
---|
1330 | n/a | } |
---|
1331 | n/a | if (flags) { |
---|
1332 | n/a | PyObject *item = PyUnicode_FromFormat("0x%x", flags); |
---|
1333 | n/a | if (!item) |
---|
1334 | n/a | goto done; |
---|
1335 | n/a | |
---|
1336 | n/a | if (PyList_Append(flag_items, item) < 0) { |
---|
1337 | n/a | Py_DECREF(item); |
---|
1338 | n/a | goto done; |
---|
1339 | n/a | } |
---|
1340 | n/a | Py_DECREF(item); |
---|
1341 | n/a | } |
---|
1342 | n/a | |
---|
1343 | n/a | if (PyList_Size(flag_items) > 0) { |
---|
1344 | n/a | PyObject *flags_result; |
---|
1345 | n/a | PyObject *sep = PyUnicode_FromString("|"); |
---|
1346 | n/a | if (!sep) |
---|
1347 | n/a | goto done; |
---|
1348 | n/a | flags_result = PyUnicode_Join(sep, flag_items); |
---|
1349 | n/a | Py_DECREF(sep); |
---|
1350 | n/a | if (!flags_result) |
---|
1351 | n/a | goto done; |
---|
1352 | n/a | result = PyUnicode_FromFormat("re.compile(%.200R, %S)", |
---|
1353 | n/a | obj->pattern, flags_result); |
---|
1354 | n/a | Py_DECREF(flags_result); |
---|
1355 | n/a | } |
---|
1356 | n/a | else { |
---|
1357 | n/a | result = PyUnicode_FromFormat("re.compile(%.200R)", obj->pattern); |
---|
1358 | n/a | } |
---|
1359 | n/a | |
---|
1360 | n/a | done: |
---|
1361 | n/a | Py_DECREF(flag_items); |
---|
1362 | n/a | return result; |
---|
1363 | n/a | } |
---|
1364 | n/a | |
---|
1365 | n/a | PyDoc_STRVAR(pattern_doc, "Compiled regular expression objects"); |
---|
1366 | n/a | |
---|
1367 | n/a | /* PatternObject's 'groupindex' method. */ |
---|
1368 | n/a | static PyObject * |
---|
1369 | n/a | pattern_groupindex(PatternObject *self) |
---|
1370 | n/a | { |
---|
1371 | n/a | return PyDictProxy_New(self->groupindex); |
---|
1372 | n/a | } |
---|
1373 | n/a | |
---|
1374 | n/a | static int _validate(PatternObject *self); /* Forward */ |
---|
1375 | n/a | |
---|
1376 | n/a | /*[clinic input] |
---|
1377 | n/a | _sre.compile |
---|
1378 | n/a | |
---|
1379 | n/a | pattern: object |
---|
1380 | n/a | flags: int |
---|
1381 | n/a | code: object(subclass_of='&PyList_Type') |
---|
1382 | n/a | groups: Py_ssize_t |
---|
1383 | n/a | groupindex: object(subclass_of='&PyDict_Type') |
---|
1384 | n/a | indexgroup: object(subclass_of='&PyTuple_Type') |
---|
1385 | n/a | |
---|
1386 | n/a | [clinic start generated code]*/ |
---|
1387 | n/a | |
---|
1388 | n/a | static PyObject * |
---|
1389 | n/a | _sre_compile_impl(PyObject *module, PyObject *pattern, int flags, |
---|
1390 | n/a | PyObject *code, Py_ssize_t groups, PyObject *groupindex, |
---|
1391 | n/a | PyObject *indexgroup) |
---|
1392 | n/a | /*[clinic end generated code: output=ef9c2b3693776404 input=0a68476dbbe5db30]*/ |
---|
1393 | n/a | { |
---|
1394 | n/a | /* "compile" pattern descriptor to pattern object */ |
---|
1395 | n/a | |
---|
1396 | n/a | PatternObject* self; |
---|
1397 | n/a | Py_ssize_t i, n; |
---|
1398 | n/a | |
---|
1399 | n/a | n = PyList_GET_SIZE(code); |
---|
1400 | n/a | /* coverity[ampersand_in_size] */ |
---|
1401 | n/a | self = PyObject_NEW_VAR(PatternObject, &Pattern_Type, n); |
---|
1402 | n/a | if (!self) |
---|
1403 | n/a | return NULL; |
---|
1404 | n/a | self->weakreflist = NULL; |
---|
1405 | n/a | self->pattern = NULL; |
---|
1406 | n/a | self->groupindex = NULL; |
---|
1407 | n/a | self->indexgroup = NULL; |
---|
1408 | n/a | |
---|
1409 | n/a | self->codesize = n; |
---|
1410 | n/a | |
---|
1411 | n/a | for (i = 0; i < n; i++) { |
---|
1412 | n/a | PyObject *o = PyList_GET_ITEM(code, i); |
---|
1413 | n/a | unsigned long value = PyLong_AsUnsignedLong(o); |
---|
1414 | n/a | self->code[i] = (SRE_CODE) value; |
---|
1415 | n/a | if ((unsigned long) self->code[i] != value) { |
---|
1416 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
1417 | n/a | "regular expression code size limit exceeded"); |
---|
1418 | n/a | break; |
---|
1419 | n/a | } |
---|
1420 | n/a | } |
---|
1421 | n/a | |
---|
1422 | n/a | if (PyErr_Occurred()) { |
---|
1423 | n/a | Py_DECREF(self); |
---|
1424 | n/a | return NULL; |
---|
1425 | n/a | } |
---|
1426 | n/a | |
---|
1427 | n/a | if (pattern == Py_None) { |
---|
1428 | n/a | self->isbytes = -1; |
---|
1429 | n/a | } |
---|
1430 | n/a | else { |
---|
1431 | n/a | Py_ssize_t p_length; |
---|
1432 | n/a | int charsize; |
---|
1433 | n/a | Py_buffer view; |
---|
1434 | n/a | view.buf = NULL; |
---|
1435 | n/a | if (!getstring(pattern, &p_length, &self->isbytes, |
---|
1436 | n/a | &charsize, &view)) { |
---|
1437 | n/a | Py_DECREF(self); |
---|
1438 | n/a | return NULL; |
---|
1439 | n/a | } |
---|
1440 | n/a | if (view.buf) |
---|
1441 | n/a | PyBuffer_Release(&view); |
---|
1442 | n/a | } |
---|
1443 | n/a | |
---|
1444 | n/a | Py_INCREF(pattern); |
---|
1445 | n/a | self->pattern = pattern; |
---|
1446 | n/a | |
---|
1447 | n/a | self->flags = flags; |
---|
1448 | n/a | |
---|
1449 | n/a | self->groups = groups; |
---|
1450 | n/a | |
---|
1451 | n/a | Py_INCREF(groupindex); |
---|
1452 | n/a | self->groupindex = groupindex; |
---|
1453 | n/a | |
---|
1454 | n/a | Py_INCREF(indexgroup); |
---|
1455 | n/a | self->indexgroup = indexgroup; |
---|
1456 | n/a | |
---|
1457 | n/a | if (!_validate(self)) { |
---|
1458 | n/a | Py_DECREF(self); |
---|
1459 | n/a | return NULL; |
---|
1460 | n/a | } |
---|
1461 | n/a | |
---|
1462 | n/a | return (PyObject*) self; |
---|
1463 | n/a | } |
---|
1464 | n/a | |
---|
1465 | n/a | /* -------------------------------------------------------------------- */ |
---|
1466 | n/a | /* Code validation */ |
---|
1467 | n/a | |
---|
1468 | n/a | /* To learn more about this code, have a look at the _compile() function in |
---|
1469 | n/a | Lib/sre_compile.py. The validation functions below checks the code array |
---|
1470 | n/a | for conformance with the code patterns generated there. |
---|
1471 | n/a | |
---|
1472 | n/a | The nice thing about the generated code is that it is position-independent: |
---|
1473 | n/a | all jumps are relative jumps forward. Also, jumps don't cross each other: |
---|
1474 | n/a | the target of a later jump is always earlier than the target of an earlier |
---|
1475 | n/a | jump. IOW, this is okay: |
---|
1476 | n/a | |
---|
1477 | n/a | J---------J-------T--------T |
---|
1478 | n/a | \ \_____/ / |
---|
1479 | n/a | \______________________/ |
---|
1480 | n/a | |
---|
1481 | n/a | but this is not: |
---|
1482 | n/a | |
---|
1483 | n/a | J---------J-------T--------T |
---|
1484 | n/a | \_________\_____/ / |
---|
1485 | n/a | \____________/ |
---|
1486 | n/a | |
---|
1487 | n/a | It also helps that SRE_CODE is always an unsigned type. |
---|
1488 | n/a | */ |
---|
1489 | n/a | |
---|
1490 | n/a | /* Defining this one enables tracing of the validator */ |
---|
1491 | n/a | #undef VVERBOSE |
---|
1492 | n/a | |
---|
1493 | n/a | /* Trace macro for the validator */ |
---|
1494 | n/a | #if defined(VVERBOSE) |
---|
1495 | n/a | #define VTRACE(v) printf v |
---|
1496 | n/a | #else |
---|
1497 | n/a | #define VTRACE(v) do {} while(0) /* do nothing */ |
---|
1498 | n/a | #endif |
---|
1499 | n/a | |
---|
1500 | n/a | /* Report failure */ |
---|
1501 | n/a | #define FAIL do { VTRACE(("FAIL: %d\n", __LINE__)); return 0; } while (0) |
---|
1502 | n/a | |
---|
1503 | n/a | /* Extract opcode, argument, or skip count from code array */ |
---|
1504 | n/a | #define GET_OP \ |
---|
1505 | n/a | do { \ |
---|
1506 | n/a | VTRACE(("%p: ", code)); \ |
---|
1507 | n/a | if (code >= end) FAIL; \ |
---|
1508 | n/a | op = *code++; \ |
---|
1509 | n/a | VTRACE(("%lu (op)\n", (unsigned long)op)); \ |
---|
1510 | n/a | } while (0) |
---|
1511 | n/a | #define GET_ARG \ |
---|
1512 | n/a | do { \ |
---|
1513 | n/a | VTRACE(("%p= ", code)); \ |
---|
1514 | n/a | if (code >= end) FAIL; \ |
---|
1515 | n/a | arg = *code++; \ |
---|
1516 | n/a | VTRACE(("%lu (arg)\n", (unsigned long)arg)); \ |
---|
1517 | n/a | } while (0) |
---|
1518 | n/a | #define GET_SKIP_ADJ(adj) \ |
---|
1519 | n/a | do { \ |
---|
1520 | n/a | VTRACE(("%p= ", code)); \ |
---|
1521 | n/a | if (code >= end) FAIL; \ |
---|
1522 | n/a | skip = *code; \ |
---|
1523 | n/a | VTRACE(("%lu (skip to %p)\n", \ |
---|
1524 | n/a | (unsigned long)skip, code+skip)); \ |
---|
1525 | n/a | if (skip-adj > (uintptr_t)(end - code)) \ |
---|
1526 | n/a | FAIL; \ |
---|
1527 | n/a | code++; \ |
---|
1528 | n/a | } while (0) |
---|
1529 | n/a | #define GET_SKIP GET_SKIP_ADJ(0) |
---|
1530 | n/a | |
---|
1531 | n/a | static int |
---|
1532 | n/a | _validate_charset(SRE_CODE *code, SRE_CODE *end) |
---|
1533 | n/a | { |
---|
1534 | n/a | /* Some variables are manipulated by the macros above */ |
---|
1535 | n/a | SRE_CODE op; |
---|
1536 | n/a | SRE_CODE arg; |
---|
1537 | n/a | SRE_CODE offset; |
---|
1538 | n/a | int i; |
---|
1539 | n/a | |
---|
1540 | n/a | while (code < end) { |
---|
1541 | n/a | GET_OP; |
---|
1542 | n/a | switch (op) { |
---|
1543 | n/a | |
---|
1544 | n/a | case SRE_OP_NEGATE: |
---|
1545 | n/a | break; |
---|
1546 | n/a | |
---|
1547 | n/a | case SRE_OP_LITERAL: |
---|
1548 | n/a | GET_ARG; |
---|
1549 | n/a | break; |
---|
1550 | n/a | |
---|
1551 | n/a | case SRE_OP_RANGE: |
---|
1552 | n/a | case SRE_OP_RANGE_IGNORE: |
---|
1553 | n/a | GET_ARG; |
---|
1554 | n/a | GET_ARG; |
---|
1555 | n/a | break; |
---|
1556 | n/a | |
---|
1557 | n/a | case SRE_OP_CHARSET: |
---|
1558 | n/a | offset = 256/SRE_CODE_BITS; /* 256-bit bitmap */ |
---|
1559 | n/a | if (offset > (uintptr_t)(end - code)) |
---|
1560 | n/a | FAIL; |
---|
1561 | n/a | code += offset; |
---|
1562 | n/a | break; |
---|
1563 | n/a | |
---|
1564 | n/a | case SRE_OP_BIGCHARSET: |
---|
1565 | n/a | GET_ARG; /* Number of blocks */ |
---|
1566 | n/a | offset = 256/sizeof(SRE_CODE); /* 256-byte table */ |
---|
1567 | n/a | if (offset > (uintptr_t)(end - code)) |
---|
1568 | n/a | FAIL; |
---|
1569 | n/a | /* Make sure that each byte points to a valid block */ |
---|
1570 | n/a | for (i = 0; i < 256; i++) { |
---|
1571 | n/a | if (((unsigned char *)code)[i] >= arg) |
---|
1572 | n/a | FAIL; |
---|
1573 | n/a | } |
---|
1574 | n/a | code += offset; |
---|
1575 | n/a | offset = arg * (256/SRE_CODE_BITS); /* 256-bit bitmap times arg */ |
---|
1576 | n/a | if (offset > (uintptr_t)(end - code)) |
---|
1577 | n/a | FAIL; |
---|
1578 | n/a | code += offset; |
---|
1579 | n/a | break; |
---|
1580 | n/a | |
---|
1581 | n/a | case SRE_OP_CATEGORY: |
---|
1582 | n/a | GET_ARG; |
---|
1583 | n/a | switch (arg) { |
---|
1584 | n/a | case SRE_CATEGORY_DIGIT: |
---|
1585 | n/a | case SRE_CATEGORY_NOT_DIGIT: |
---|
1586 | n/a | case SRE_CATEGORY_SPACE: |
---|
1587 | n/a | case SRE_CATEGORY_NOT_SPACE: |
---|
1588 | n/a | case SRE_CATEGORY_WORD: |
---|
1589 | n/a | case SRE_CATEGORY_NOT_WORD: |
---|
1590 | n/a | case SRE_CATEGORY_LINEBREAK: |
---|
1591 | n/a | case SRE_CATEGORY_NOT_LINEBREAK: |
---|
1592 | n/a | case SRE_CATEGORY_LOC_WORD: |
---|
1593 | n/a | case SRE_CATEGORY_LOC_NOT_WORD: |
---|
1594 | n/a | case SRE_CATEGORY_UNI_DIGIT: |
---|
1595 | n/a | case SRE_CATEGORY_UNI_NOT_DIGIT: |
---|
1596 | n/a | case SRE_CATEGORY_UNI_SPACE: |
---|
1597 | n/a | case SRE_CATEGORY_UNI_NOT_SPACE: |
---|
1598 | n/a | case SRE_CATEGORY_UNI_WORD: |
---|
1599 | n/a | case SRE_CATEGORY_UNI_NOT_WORD: |
---|
1600 | n/a | case SRE_CATEGORY_UNI_LINEBREAK: |
---|
1601 | n/a | case SRE_CATEGORY_UNI_NOT_LINEBREAK: |
---|
1602 | n/a | break; |
---|
1603 | n/a | default: |
---|
1604 | n/a | FAIL; |
---|
1605 | n/a | } |
---|
1606 | n/a | break; |
---|
1607 | n/a | |
---|
1608 | n/a | default: |
---|
1609 | n/a | FAIL; |
---|
1610 | n/a | |
---|
1611 | n/a | } |
---|
1612 | n/a | } |
---|
1613 | n/a | |
---|
1614 | n/a | return 1; |
---|
1615 | n/a | } |
---|
1616 | n/a | |
---|
1617 | n/a | static int |
---|
1618 | n/a | _validate_inner(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups) |
---|
1619 | n/a | { |
---|
1620 | n/a | /* Some variables are manipulated by the macros above */ |
---|
1621 | n/a | SRE_CODE op; |
---|
1622 | n/a | SRE_CODE arg; |
---|
1623 | n/a | SRE_CODE skip; |
---|
1624 | n/a | |
---|
1625 | n/a | VTRACE(("code=%p, end=%p\n", code, end)); |
---|
1626 | n/a | |
---|
1627 | n/a | if (code > end) |
---|
1628 | n/a | FAIL; |
---|
1629 | n/a | |
---|
1630 | n/a | while (code < end) { |
---|
1631 | n/a | GET_OP; |
---|
1632 | n/a | switch (op) { |
---|
1633 | n/a | |
---|
1634 | n/a | case SRE_OP_MARK: |
---|
1635 | n/a | /* We don't check whether marks are properly nested; the |
---|
1636 | n/a | sre_match() code is robust even if they don't, and the worst |
---|
1637 | n/a | you can get is nonsensical match results. */ |
---|
1638 | n/a | GET_ARG; |
---|
1639 | n/a | if (arg > 2 * (size_t)groups + 1) { |
---|
1640 | n/a | VTRACE(("arg=%d, groups=%d\n", (int)arg, (int)groups)); |
---|
1641 | n/a | FAIL; |
---|
1642 | n/a | } |
---|
1643 | n/a | break; |
---|
1644 | n/a | |
---|
1645 | n/a | case SRE_OP_LITERAL: |
---|
1646 | n/a | case SRE_OP_NOT_LITERAL: |
---|
1647 | n/a | case SRE_OP_LITERAL_IGNORE: |
---|
1648 | n/a | case SRE_OP_NOT_LITERAL_IGNORE: |
---|
1649 | n/a | GET_ARG; |
---|
1650 | n/a | /* The arg is just a character, nothing to check */ |
---|
1651 | n/a | break; |
---|
1652 | n/a | |
---|
1653 | n/a | case SRE_OP_SUCCESS: |
---|
1654 | n/a | case SRE_OP_FAILURE: |
---|
1655 | n/a | /* Nothing to check; these normally end the matching process */ |
---|
1656 | n/a | break; |
---|
1657 | n/a | |
---|
1658 | n/a | case SRE_OP_AT: |
---|
1659 | n/a | GET_ARG; |
---|
1660 | n/a | switch (arg) { |
---|
1661 | n/a | case SRE_AT_BEGINNING: |
---|
1662 | n/a | case SRE_AT_BEGINNING_STRING: |
---|
1663 | n/a | case SRE_AT_BEGINNING_LINE: |
---|
1664 | n/a | case SRE_AT_END: |
---|
1665 | n/a | case SRE_AT_END_LINE: |
---|
1666 | n/a | case SRE_AT_END_STRING: |
---|
1667 | n/a | case SRE_AT_BOUNDARY: |
---|
1668 | n/a | case SRE_AT_NON_BOUNDARY: |
---|
1669 | n/a | case SRE_AT_LOC_BOUNDARY: |
---|
1670 | n/a | case SRE_AT_LOC_NON_BOUNDARY: |
---|
1671 | n/a | case SRE_AT_UNI_BOUNDARY: |
---|
1672 | n/a | case SRE_AT_UNI_NON_BOUNDARY: |
---|
1673 | n/a | break; |
---|
1674 | n/a | default: |
---|
1675 | n/a | FAIL; |
---|
1676 | n/a | } |
---|
1677 | n/a | break; |
---|
1678 | n/a | |
---|
1679 | n/a | case SRE_OP_ANY: |
---|
1680 | n/a | case SRE_OP_ANY_ALL: |
---|
1681 | n/a | /* These have no operands */ |
---|
1682 | n/a | break; |
---|
1683 | n/a | |
---|
1684 | n/a | case SRE_OP_IN: |
---|
1685 | n/a | case SRE_OP_IN_IGNORE: |
---|
1686 | n/a | GET_SKIP; |
---|
1687 | n/a | /* Stop 1 before the end; we check the FAILURE below */ |
---|
1688 | n/a | if (!_validate_charset(code, code+skip-2)) |
---|
1689 | n/a | FAIL; |
---|
1690 | n/a | if (code[skip-2] != SRE_OP_FAILURE) |
---|
1691 | n/a | FAIL; |
---|
1692 | n/a | code += skip-1; |
---|
1693 | n/a | break; |
---|
1694 | n/a | |
---|
1695 | n/a | case SRE_OP_INFO: |
---|
1696 | n/a | { |
---|
1697 | n/a | /* A minimal info field is |
---|
1698 | n/a | <INFO> <1=skip> <2=flags> <3=min> <4=max>; |
---|
1699 | n/a | If SRE_INFO_PREFIX or SRE_INFO_CHARSET is in the flags, |
---|
1700 | n/a | more follows. */ |
---|
1701 | n/a | SRE_CODE flags, i; |
---|
1702 | n/a | SRE_CODE *newcode; |
---|
1703 | n/a | GET_SKIP; |
---|
1704 | n/a | newcode = code+skip-1; |
---|
1705 | n/a | GET_ARG; flags = arg; |
---|
1706 | n/a | GET_ARG; |
---|
1707 | n/a | GET_ARG; |
---|
1708 | n/a | /* Check that only valid flags are present */ |
---|
1709 | n/a | if ((flags & ~(SRE_INFO_PREFIX | |
---|
1710 | n/a | SRE_INFO_LITERAL | |
---|
1711 | n/a | SRE_INFO_CHARSET)) != 0) |
---|
1712 | n/a | FAIL; |
---|
1713 | n/a | /* PREFIX and CHARSET are mutually exclusive */ |
---|
1714 | n/a | if ((flags & SRE_INFO_PREFIX) && |
---|
1715 | n/a | (flags & SRE_INFO_CHARSET)) |
---|
1716 | n/a | FAIL; |
---|
1717 | n/a | /* LITERAL implies PREFIX */ |
---|
1718 | n/a | if ((flags & SRE_INFO_LITERAL) && |
---|
1719 | n/a | !(flags & SRE_INFO_PREFIX)) |
---|
1720 | n/a | FAIL; |
---|
1721 | n/a | /* Validate the prefix */ |
---|
1722 | n/a | if (flags & SRE_INFO_PREFIX) { |
---|
1723 | n/a | SRE_CODE prefix_len; |
---|
1724 | n/a | GET_ARG; prefix_len = arg; |
---|
1725 | n/a | GET_ARG; |
---|
1726 | n/a | /* Here comes the prefix string */ |
---|
1727 | n/a | if (prefix_len > (uintptr_t)(newcode - code)) |
---|
1728 | n/a | FAIL; |
---|
1729 | n/a | code += prefix_len; |
---|
1730 | n/a | /* And here comes the overlap table */ |
---|
1731 | n/a | if (prefix_len > (uintptr_t)(newcode - code)) |
---|
1732 | n/a | FAIL; |
---|
1733 | n/a | /* Each overlap value should be < prefix_len */ |
---|
1734 | n/a | for (i = 0; i < prefix_len; i++) { |
---|
1735 | n/a | if (code[i] >= prefix_len) |
---|
1736 | n/a | FAIL; |
---|
1737 | n/a | } |
---|
1738 | n/a | code += prefix_len; |
---|
1739 | n/a | } |
---|
1740 | n/a | /* Validate the charset */ |
---|
1741 | n/a | if (flags & SRE_INFO_CHARSET) { |
---|
1742 | n/a | if (!_validate_charset(code, newcode-1)) |
---|
1743 | n/a | FAIL; |
---|
1744 | n/a | if (newcode[-1] != SRE_OP_FAILURE) |
---|
1745 | n/a | FAIL; |
---|
1746 | n/a | code = newcode; |
---|
1747 | n/a | } |
---|
1748 | n/a | else if (code != newcode) { |
---|
1749 | n/a | VTRACE(("code=%p, newcode=%p\n", code, newcode)); |
---|
1750 | n/a | FAIL; |
---|
1751 | n/a | } |
---|
1752 | n/a | } |
---|
1753 | n/a | break; |
---|
1754 | n/a | |
---|
1755 | n/a | case SRE_OP_BRANCH: |
---|
1756 | n/a | { |
---|
1757 | n/a | SRE_CODE *target = NULL; |
---|
1758 | n/a | for (;;) { |
---|
1759 | n/a | GET_SKIP; |
---|
1760 | n/a | if (skip == 0) |
---|
1761 | n/a | break; |
---|
1762 | n/a | /* Stop 2 before the end; we check the JUMP below */ |
---|
1763 | n/a | if (!_validate_inner(code, code+skip-3, groups)) |
---|
1764 | n/a | FAIL; |
---|
1765 | n/a | code += skip-3; |
---|
1766 | n/a | /* Check that it ends with a JUMP, and that each JUMP |
---|
1767 | n/a | has the same target */ |
---|
1768 | n/a | GET_OP; |
---|
1769 | n/a | if (op != SRE_OP_JUMP) |
---|
1770 | n/a | FAIL; |
---|
1771 | n/a | GET_SKIP; |
---|
1772 | n/a | if (target == NULL) |
---|
1773 | n/a | target = code+skip-1; |
---|
1774 | n/a | else if (code+skip-1 != target) |
---|
1775 | n/a | FAIL; |
---|
1776 | n/a | } |
---|
1777 | n/a | } |
---|
1778 | n/a | break; |
---|
1779 | n/a | |
---|
1780 | n/a | case SRE_OP_REPEAT_ONE: |
---|
1781 | n/a | case SRE_OP_MIN_REPEAT_ONE: |
---|
1782 | n/a | { |
---|
1783 | n/a | SRE_CODE min, max; |
---|
1784 | n/a | GET_SKIP; |
---|
1785 | n/a | GET_ARG; min = arg; |
---|
1786 | n/a | GET_ARG; max = arg; |
---|
1787 | n/a | if (min > max) |
---|
1788 | n/a | FAIL; |
---|
1789 | n/a | if (max > SRE_MAXREPEAT) |
---|
1790 | n/a | FAIL; |
---|
1791 | n/a | if (!_validate_inner(code, code+skip-4, groups)) |
---|
1792 | n/a | FAIL; |
---|
1793 | n/a | code += skip-4; |
---|
1794 | n/a | GET_OP; |
---|
1795 | n/a | if (op != SRE_OP_SUCCESS) |
---|
1796 | n/a | FAIL; |
---|
1797 | n/a | } |
---|
1798 | n/a | break; |
---|
1799 | n/a | |
---|
1800 | n/a | case SRE_OP_REPEAT: |
---|
1801 | n/a | { |
---|
1802 | n/a | SRE_CODE min, max; |
---|
1803 | n/a | GET_SKIP; |
---|
1804 | n/a | GET_ARG; min = arg; |
---|
1805 | n/a | GET_ARG; max = arg; |
---|
1806 | n/a | if (min > max) |
---|
1807 | n/a | FAIL; |
---|
1808 | n/a | if (max > SRE_MAXREPEAT) |
---|
1809 | n/a | FAIL; |
---|
1810 | n/a | if (!_validate_inner(code, code+skip-3, groups)) |
---|
1811 | n/a | FAIL; |
---|
1812 | n/a | code += skip-3; |
---|
1813 | n/a | GET_OP; |
---|
1814 | n/a | if (op != SRE_OP_MAX_UNTIL && op != SRE_OP_MIN_UNTIL) |
---|
1815 | n/a | FAIL; |
---|
1816 | n/a | } |
---|
1817 | n/a | break; |
---|
1818 | n/a | |
---|
1819 | n/a | case SRE_OP_GROUPREF: |
---|
1820 | n/a | case SRE_OP_GROUPREF_IGNORE: |
---|
1821 | n/a | GET_ARG; |
---|
1822 | n/a | if (arg >= (size_t)groups) |
---|
1823 | n/a | FAIL; |
---|
1824 | n/a | break; |
---|
1825 | n/a | |
---|
1826 | n/a | case SRE_OP_GROUPREF_EXISTS: |
---|
1827 | n/a | /* The regex syntax for this is: '(?(group)then|else)', where |
---|
1828 | n/a | 'group' is either an integer group number or a group name, |
---|
1829 | n/a | 'then' and 'else' are sub-regexes, and 'else' is optional. */ |
---|
1830 | n/a | GET_ARG; |
---|
1831 | n/a | if (arg >= (size_t)groups) |
---|
1832 | n/a | FAIL; |
---|
1833 | n/a | GET_SKIP_ADJ(1); |
---|
1834 | n/a | code--; /* The skip is relative to the first arg! */ |
---|
1835 | n/a | /* There are two possibilities here: if there is both a 'then' |
---|
1836 | n/a | part and an 'else' part, the generated code looks like: |
---|
1837 | n/a | |
---|
1838 | n/a | GROUPREF_EXISTS |
---|
1839 | n/a | <group> |
---|
1840 | n/a | <skipyes> |
---|
1841 | n/a | ...then part... |
---|
1842 | n/a | JUMP |
---|
1843 | n/a | <skipno> |
---|
1844 | n/a | (<skipyes> jumps here) |
---|
1845 | n/a | ...else part... |
---|
1846 | n/a | (<skipno> jumps here) |
---|
1847 | n/a | |
---|
1848 | n/a | If there is only a 'then' part, it looks like: |
---|
1849 | n/a | |
---|
1850 | n/a | GROUPREF_EXISTS |
---|
1851 | n/a | <group> |
---|
1852 | n/a | <skip> |
---|
1853 | n/a | ...then part... |
---|
1854 | n/a | (<skip> jumps here) |
---|
1855 | n/a | |
---|
1856 | n/a | There is no direct way to decide which it is, and we don't want |
---|
1857 | n/a | to allow arbitrary jumps anywhere in the code; so we just look |
---|
1858 | n/a | for a JUMP opcode preceding our skip target. |
---|
1859 | n/a | */ |
---|
1860 | n/a | if (skip >= 3 && skip-3 < (uintptr_t)(end - code) && |
---|
1861 | n/a | code[skip-3] == SRE_OP_JUMP) |
---|
1862 | n/a | { |
---|
1863 | n/a | VTRACE(("both then and else parts present\n")); |
---|
1864 | n/a | if (!_validate_inner(code+1, code+skip-3, groups)) |
---|
1865 | n/a | FAIL; |
---|
1866 | n/a | code += skip-2; /* Position after JUMP, at <skipno> */ |
---|
1867 | n/a | GET_SKIP; |
---|
1868 | n/a | if (!_validate_inner(code, code+skip-1, groups)) |
---|
1869 | n/a | FAIL; |
---|
1870 | n/a | code += skip-1; |
---|
1871 | n/a | } |
---|
1872 | n/a | else { |
---|
1873 | n/a | VTRACE(("only a then part present\n")); |
---|
1874 | n/a | if (!_validate_inner(code+1, code+skip-1, groups)) |
---|
1875 | n/a | FAIL; |
---|
1876 | n/a | code += skip-1; |
---|
1877 | n/a | } |
---|
1878 | n/a | break; |
---|
1879 | n/a | |
---|
1880 | n/a | case SRE_OP_ASSERT: |
---|
1881 | n/a | case SRE_OP_ASSERT_NOT: |
---|
1882 | n/a | GET_SKIP; |
---|
1883 | n/a | GET_ARG; /* 0 for lookahead, width for lookbehind */ |
---|
1884 | n/a | code--; /* Back up over arg to simplify math below */ |
---|
1885 | n/a | if (arg & 0x80000000) |
---|
1886 | n/a | FAIL; /* Width too large */ |
---|
1887 | n/a | /* Stop 1 before the end; we check the SUCCESS below */ |
---|
1888 | n/a | if (!_validate_inner(code+1, code+skip-2, groups)) |
---|
1889 | n/a | FAIL; |
---|
1890 | n/a | code += skip-2; |
---|
1891 | n/a | GET_OP; |
---|
1892 | n/a | if (op != SRE_OP_SUCCESS) |
---|
1893 | n/a | FAIL; |
---|
1894 | n/a | break; |
---|
1895 | n/a | |
---|
1896 | n/a | default: |
---|
1897 | n/a | FAIL; |
---|
1898 | n/a | |
---|
1899 | n/a | } |
---|
1900 | n/a | } |
---|
1901 | n/a | |
---|
1902 | n/a | VTRACE(("okay\n")); |
---|
1903 | n/a | return 1; |
---|
1904 | n/a | } |
---|
1905 | n/a | |
---|
1906 | n/a | static int |
---|
1907 | n/a | _validate_outer(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups) |
---|
1908 | n/a | { |
---|
1909 | n/a | if (groups < 0 || (size_t)groups > SRE_MAXGROUPS || |
---|
1910 | n/a | code >= end || end[-1] != SRE_OP_SUCCESS) |
---|
1911 | n/a | FAIL; |
---|
1912 | n/a | return _validate_inner(code, end-1, groups); |
---|
1913 | n/a | } |
---|
1914 | n/a | |
---|
1915 | n/a | static int |
---|
1916 | n/a | _validate(PatternObject *self) |
---|
1917 | n/a | { |
---|
1918 | n/a | if (!_validate_outer(self->code, self->code+self->codesize, self->groups)) |
---|
1919 | n/a | { |
---|
1920 | n/a | PyErr_SetString(PyExc_RuntimeError, "invalid SRE code"); |
---|
1921 | n/a | return 0; |
---|
1922 | n/a | } |
---|
1923 | n/a | else |
---|
1924 | n/a | VTRACE(("Success!\n")); |
---|
1925 | n/a | return 1; |
---|
1926 | n/a | } |
---|
1927 | n/a | |
---|
1928 | n/a | /* -------------------------------------------------------------------- */ |
---|
1929 | n/a | /* match methods */ |
---|
1930 | n/a | |
---|
1931 | n/a | static void |
---|
1932 | n/a | match_dealloc(MatchObject* self) |
---|
1933 | n/a | { |
---|
1934 | n/a | Py_XDECREF(self->regs); |
---|
1935 | n/a | Py_XDECREF(self->string); |
---|
1936 | n/a | Py_DECREF(self->pattern); |
---|
1937 | n/a | PyObject_DEL(self); |
---|
1938 | n/a | } |
---|
1939 | n/a | |
---|
1940 | n/a | static PyObject* |
---|
1941 | n/a | match_getslice_by_index(MatchObject* self, Py_ssize_t index, PyObject* def) |
---|
1942 | n/a | { |
---|
1943 | n/a | Py_ssize_t length; |
---|
1944 | n/a | int isbytes, charsize; |
---|
1945 | n/a | Py_buffer view; |
---|
1946 | n/a | PyObject *result; |
---|
1947 | n/a | void* ptr; |
---|
1948 | n/a | Py_ssize_t i, j; |
---|
1949 | n/a | |
---|
1950 | n/a | if (index < 0 || index >= self->groups) { |
---|
1951 | n/a | /* raise IndexError if we were given a bad group number */ |
---|
1952 | n/a | PyErr_SetString( |
---|
1953 | n/a | PyExc_IndexError, |
---|
1954 | n/a | "no such group" |
---|
1955 | n/a | ); |
---|
1956 | n/a | return NULL; |
---|
1957 | n/a | } |
---|
1958 | n/a | |
---|
1959 | n/a | index *= 2; |
---|
1960 | n/a | |
---|
1961 | n/a | if (self->string == Py_None || self->mark[index] < 0) { |
---|
1962 | n/a | /* return default value if the string or group is undefined */ |
---|
1963 | n/a | Py_INCREF(def); |
---|
1964 | n/a | return def; |
---|
1965 | n/a | } |
---|
1966 | n/a | |
---|
1967 | n/a | ptr = getstring(self->string, &length, &isbytes, &charsize, &view); |
---|
1968 | n/a | if (ptr == NULL) |
---|
1969 | n/a | return NULL; |
---|
1970 | n/a | |
---|
1971 | n/a | i = self->mark[index]; |
---|
1972 | n/a | j = self->mark[index+1]; |
---|
1973 | n/a | i = Py_MIN(i, length); |
---|
1974 | n/a | j = Py_MIN(j, length); |
---|
1975 | n/a | result = getslice(isbytes, ptr, self->string, i, j); |
---|
1976 | n/a | if (isbytes && view.buf != NULL) |
---|
1977 | n/a | PyBuffer_Release(&view); |
---|
1978 | n/a | return result; |
---|
1979 | n/a | } |
---|
1980 | n/a | |
---|
1981 | n/a | static Py_ssize_t |
---|
1982 | n/a | match_getindex(MatchObject* self, PyObject* index) |
---|
1983 | n/a | { |
---|
1984 | n/a | Py_ssize_t i; |
---|
1985 | n/a | |
---|
1986 | n/a | if (index == NULL) |
---|
1987 | n/a | /* Default value */ |
---|
1988 | n/a | return 0; |
---|
1989 | n/a | |
---|
1990 | n/a | if (PyIndex_Check(index)) { |
---|
1991 | n/a | return PyNumber_AsSsize_t(index, NULL); |
---|
1992 | n/a | } |
---|
1993 | n/a | |
---|
1994 | n/a | i = -1; |
---|
1995 | n/a | |
---|
1996 | n/a | if (self->pattern->groupindex) { |
---|
1997 | n/a | index = PyObject_GetItem(self->pattern->groupindex, index); |
---|
1998 | n/a | if (index) { |
---|
1999 | n/a | if (PyLong_Check(index)) |
---|
2000 | n/a | i = PyLong_AsSsize_t(index); |
---|
2001 | n/a | Py_DECREF(index); |
---|
2002 | n/a | } else |
---|
2003 | n/a | PyErr_Clear(); |
---|
2004 | n/a | } |
---|
2005 | n/a | |
---|
2006 | n/a | return i; |
---|
2007 | n/a | } |
---|
2008 | n/a | |
---|
2009 | n/a | static PyObject* |
---|
2010 | n/a | match_getslice(MatchObject* self, PyObject* index, PyObject* def) |
---|
2011 | n/a | { |
---|
2012 | n/a | return match_getslice_by_index(self, match_getindex(self, index), def); |
---|
2013 | n/a | } |
---|
2014 | n/a | |
---|
2015 | n/a | /*[clinic input] |
---|
2016 | n/a | _sre.SRE_Match.expand |
---|
2017 | n/a | |
---|
2018 | n/a | template: object |
---|
2019 | n/a | |
---|
2020 | n/a | Return the string obtained by doing backslash substitution on the string template, as done by the sub() method. |
---|
2021 | n/a | [clinic start generated code]*/ |
---|
2022 | n/a | |
---|
2023 | n/a | static PyObject * |
---|
2024 | n/a | _sre_SRE_Match_expand_impl(MatchObject *self, PyObject *template) |
---|
2025 | n/a | /*[clinic end generated code: output=931b58ccc323c3a1 input=4bfdb22c2f8b146a]*/ |
---|
2026 | n/a | { |
---|
2027 | n/a | /* delegate to Python code */ |
---|
2028 | n/a | return call( |
---|
2029 | n/a | SRE_PY_MODULE, "_expand", |
---|
2030 | n/a | PyTuple_Pack(3, self->pattern, self, template) |
---|
2031 | n/a | ); |
---|
2032 | n/a | } |
---|
2033 | n/a | |
---|
2034 | n/a | static PyObject* |
---|
2035 | n/a | match_group(MatchObject* self, PyObject* args) |
---|
2036 | n/a | { |
---|
2037 | n/a | PyObject* result; |
---|
2038 | n/a | Py_ssize_t i, size; |
---|
2039 | n/a | |
---|
2040 | n/a | size = PyTuple_GET_SIZE(args); |
---|
2041 | n/a | |
---|
2042 | n/a | switch (size) { |
---|
2043 | n/a | case 0: |
---|
2044 | n/a | result = match_getslice(self, Py_False, Py_None); |
---|
2045 | n/a | break; |
---|
2046 | n/a | case 1: |
---|
2047 | n/a | result = match_getslice(self, PyTuple_GET_ITEM(args, 0), Py_None); |
---|
2048 | n/a | break; |
---|
2049 | n/a | default: |
---|
2050 | n/a | /* fetch multiple items */ |
---|
2051 | n/a | result = PyTuple_New(size); |
---|
2052 | n/a | if (!result) |
---|
2053 | n/a | return NULL; |
---|
2054 | n/a | for (i = 0; i < size; i++) { |
---|
2055 | n/a | PyObject* item = match_getslice( |
---|
2056 | n/a | self, PyTuple_GET_ITEM(args, i), Py_None |
---|
2057 | n/a | ); |
---|
2058 | n/a | if (!item) { |
---|
2059 | n/a | Py_DECREF(result); |
---|
2060 | n/a | return NULL; |
---|
2061 | n/a | } |
---|
2062 | n/a | PyTuple_SET_ITEM(result, i, item); |
---|
2063 | n/a | } |
---|
2064 | n/a | break; |
---|
2065 | n/a | } |
---|
2066 | n/a | return result; |
---|
2067 | n/a | } |
---|
2068 | n/a | |
---|
2069 | n/a | static PyObject* |
---|
2070 | n/a | match_getitem(MatchObject* self, PyObject* name) |
---|
2071 | n/a | { |
---|
2072 | n/a | return match_getslice(self, name, Py_None); |
---|
2073 | n/a | } |
---|
2074 | n/a | |
---|
2075 | n/a | /*[clinic input] |
---|
2076 | n/a | _sre.SRE_Match.groups |
---|
2077 | n/a | |
---|
2078 | n/a | default: object = None |
---|
2079 | n/a | Is used for groups that did not participate in the match. |
---|
2080 | n/a | |
---|
2081 | n/a | Return a tuple containing all the subgroups of the match, from 1. |
---|
2082 | n/a | [clinic start generated code]*/ |
---|
2083 | n/a | |
---|
2084 | n/a | static PyObject * |
---|
2085 | n/a | _sre_SRE_Match_groups_impl(MatchObject *self, PyObject *default_value) |
---|
2086 | n/a | /*[clinic end generated code: output=daf8e2641537238a input=bb069ef55dabca91]*/ |
---|
2087 | n/a | { |
---|
2088 | n/a | PyObject* result; |
---|
2089 | n/a | Py_ssize_t index; |
---|
2090 | n/a | |
---|
2091 | n/a | result = PyTuple_New(self->groups-1); |
---|
2092 | n/a | if (!result) |
---|
2093 | n/a | return NULL; |
---|
2094 | n/a | |
---|
2095 | n/a | for (index = 1; index < self->groups; index++) { |
---|
2096 | n/a | PyObject* item; |
---|
2097 | n/a | item = match_getslice_by_index(self, index, default_value); |
---|
2098 | n/a | if (!item) { |
---|
2099 | n/a | Py_DECREF(result); |
---|
2100 | n/a | return NULL; |
---|
2101 | n/a | } |
---|
2102 | n/a | PyTuple_SET_ITEM(result, index-1, item); |
---|
2103 | n/a | } |
---|
2104 | n/a | |
---|
2105 | n/a | return result; |
---|
2106 | n/a | } |
---|
2107 | n/a | |
---|
2108 | n/a | /*[clinic input] |
---|
2109 | n/a | _sre.SRE_Match.groupdict |
---|
2110 | n/a | |
---|
2111 | n/a | default: object = None |
---|
2112 | n/a | Is used for groups that did not participate in the match. |
---|
2113 | n/a | |
---|
2114 | n/a | Return a dictionary containing all the named subgroups of the match, keyed by the subgroup name. |
---|
2115 | n/a | [clinic start generated code]*/ |
---|
2116 | n/a | |
---|
2117 | n/a | static PyObject * |
---|
2118 | n/a | _sre_SRE_Match_groupdict_impl(MatchObject *self, PyObject *default_value) |
---|
2119 | n/a | /*[clinic end generated code: output=29917c9073e41757 input=0ded7960b23780aa]*/ |
---|
2120 | n/a | { |
---|
2121 | n/a | PyObject* result; |
---|
2122 | n/a | PyObject* keys; |
---|
2123 | n/a | Py_ssize_t index; |
---|
2124 | n/a | |
---|
2125 | n/a | result = PyDict_New(); |
---|
2126 | n/a | if (!result || !self->pattern->groupindex) |
---|
2127 | n/a | return result; |
---|
2128 | n/a | |
---|
2129 | n/a | keys = PyMapping_Keys(self->pattern->groupindex); |
---|
2130 | n/a | if (!keys) |
---|
2131 | n/a | goto failed; |
---|
2132 | n/a | |
---|
2133 | n/a | for (index = 0; index < PyList_GET_SIZE(keys); index++) { |
---|
2134 | n/a | int status; |
---|
2135 | n/a | PyObject* key; |
---|
2136 | n/a | PyObject* value; |
---|
2137 | n/a | key = PyList_GET_ITEM(keys, index); |
---|
2138 | n/a | if (!key) |
---|
2139 | n/a | goto failed; |
---|
2140 | n/a | value = match_getslice(self, key, default_value); |
---|
2141 | n/a | if (!value) |
---|
2142 | n/a | goto failed; |
---|
2143 | n/a | status = PyDict_SetItem(result, key, value); |
---|
2144 | n/a | Py_DECREF(value); |
---|
2145 | n/a | if (status < 0) |
---|
2146 | n/a | goto failed; |
---|
2147 | n/a | } |
---|
2148 | n/a | |
---|
2149 | n/a | Py_DECREF(keys); |
---|
2150 | n/a | |
---|
2151 | n/a | return result; |
---|
2152 | n/a | |
---|
2153 | n/a | failed: |
---|
2154 | n/a | Py_XDECREF(keys); |
---|
2155 | n/a | Py_DECREF(result); |
---|
2156 | n/a | return NULL; |
---|
2157 | n/a | } |
---|
2158 | n/a | |
---|
2159 | n/a | /*[clinic input] |
---|
2160 | n/a | _sre.SRE_Match.start -> Py_ssize_t |
---|
2161 | n/a | |
---|
2162 | n/a | group: object(c_default="NULL") = 0 |
---|
2163 | n/a | / |
---|
2164 | n/a | |
---|
2165 | n/a | Return index of the start of the substring matched by group. |
---|
2166 | n/a | [clinic start generated code]*/ |
---|
2167 | n/a | |
---|
2168 | n/a | static Py_ssize_t |
---|
2169 | n/a | _sre_SRE_Match_start_impl(MatchObject *self, PyObject *group) |
---|
2170 | n/a | /*[clinic end generated code: output=3f6e7f9df2fb5201 input=ced8e4ed4b33ee6c]*/ |
---|
2171 | n/a | { |
---|
2172 | n/a | Py_ssize_t index = match_getindex(self, group); |
---|
2173 | n/a | |
---|
2174 | n/a | if (index < 0 || index >= self->groups) { |
---|
2175 | n/a | PyErr_SetString( |
---|
2176 | n/a | PyExc_IndexError, |
---|
2177 | n/a | "no such group" |
---|
2178 | n/a | ); |
---|
2179 | n/a | return -1; |
---|
2180 | n/a | } |
---|
2181 | n/a | |
---|
2182 | n/a | /* mark is -1 if group is undefined */ |
---|
2183 | n/a | return self->mark[index*2]; |
---|
2184 | n/a | } |
---|
2185 | n/a | |
---|
2186 | n/a | /*[clinic input] |
---|
2187 | n/a | _sre.SRE_Match.end -> Py_ssize_t |
---|
2188 | n/a | |
---|
2189 | n/a | group: object(c_default="NULL") = 0 |
---|
2190 | n/a | / |
---|
2191 | n/a | |
---|
2192 | n/a | Return index of the end of the substring matched by group. |
---|
2193 | n/a | [clinic start generated code]*/ |
---|
2194 | n/a | |
---|
2195 | n/a | static Py_ssize_t |
---|
2196 | n/a | _sre_SRE_Match_end_impl(MatchObject *self, PyObject *group) |
---|
2197 | n/a | /*[clinic end generated code: output=f4240b09911f7692 input=1b799560c7f3d7e6]*/ |
---|
2198 | n/a | { |
---|
2199 | n/a | Py_ssize_t index = match_getindex(self, group); |
---|
2200 | n/a | |
---|
2201 | n/a | if (index < 0 || index >= self->groups) { |
---|
2202 | n/a | PyErr_SetString( |
---|
2203 | n/a | PyExc_IndexError, |
---|
2204 | n/a | "no such group" |
---|
2205 | n/a | ); |
---|
2206 | n/a | return -1; |
---|
2207 | n/a | } |
---|
2208 | n/a | |
---|
2209 | n/a | /* mark is -1 if group is undefined */ |
---|
2210 | n/a | return self->mark[index*2+1]; |
---|
2211 | n/a | } |
---|
2212 | n/a | |
---|
2213 | n/a | LOCAL(PyObject*) |
---|
2214 | n/a | _pair(Py_ssize_t i1, Py_ssize_t i2) |
---|
2215 | n/a | { |
---|
2216 | n/a | PyObject* pair; |
---|
2217 | n/a | PyObject* item; |
---|
2218 | n/a | |
---|
2219 | n/a | pair = PyTuple_New(2); |
---|
2220 | n/a | if (!pair) |
---|
2221 | n/a | return NULL; |
---|
2222 | n/a | |
---|
2223 | n/a | item = PyLong_FromSsize_t(i1); |
---|
2224 | n/a | if (!item) |
---|
2225 | n/a | goto error; |
---|
2226 | n/a | PyTuple_SET_ITEM(pair, 0, item); |
---|
2227 | n/a | |
---|
2228 | n/a | item = PyLong_FromSsize_t(i2); |
---|
2229 | n/a | if (!item) |
---|
2230 | n/a | goto error; |
---|
2231 | n/a | PyTuple_SET_ITEM(pair, 1, item); |
---|
2232 | n/a | |
---|
2233 | n/a | return pair; |
---|
2234 | n/a | |
---|
2235 | n/a | error: |
---|
2236 | n/a | Py_DECREF(pair); |
---|
2237 | n/a | return NULL; |
---|
2238 | n/a | } |
---|
2239 | n/a | |
---|
2240 | n/a | /*[clinic input] |
---|
2241 | n/a | _sre.SRE_Match.span |
---|
2242 | n/a | |
---|
2243 | n/a | group: object(c_default="NULL") = 0 |
---|
2244 | n/a | / |
---|
2245 | n/a | |
---|
2246 | n/a | For MatchObject m, return the 2-tuple (m.start(group), m.end(group)). |
---|
2247 | n/a | [clinic start generated code]*/ |
---|
2248 | n/a | |
---|
2249 | n/a | static PyObject * |
---|
2250 | n/a | _sre_SRE_Match_span_impl(MatchObject *self, PyObject *group) |
---|
2251 | n/a | /*[clinic end generated code: output=f02ae40594d14fe6 input=49092b6008d176d3]*/ |
---|
2252 | n/a | { |
---|
2253 | n/a | Py_ssize_t index = match_getindex(self, group); |
---|
2254 | n/a | |
---|
2255 | n/a | if (index < 0 || index >= self->groups) { |
---|
2256 | n/a | PyErr_SetString( |
---|
2257 | n/a | PyExc_IndexError, |
---|
2258 | n/a | "no such group" |
---|
2259 | n/a | ); |
---|
2260 | n/a | return NULL; |
---|
2261 | n/a | } |
---|
2262 | n/a | |
---|
2263 | n/a | /* marks are -1 if group is undefined */ |
---|
2264 | n/a | return _pair(self->mark[index*2], self->mark[index*2+1]); |
---|
2265 | n/a | } |
---|
2266 | n/a | |
---|
2267 | n/a | static PyObject* |
---|
2268 | n/a | match_regs(MatchObject* self) |
---|
2269 | n/a | { |
---|
2270 | n/a | PyObject* regs; |
---|
2271 | n/a | PyObject* item; |
---|
2272 | n/a | Py_ssize_t index; |
---|
2273 | n/a | |
---|
2274 | n/a | regs = PyTuple_New(self->groups); |
---|
2275 | n/a | if (!regs) |
---|
2276 | n/a | return NULL; |
---|
2277 | n/a | |
---|
2278 | n/a | for (index = 0; index < self->groups; index++) { |
---|
2279 | n/a | item = _pair(self->mark[index*2], self->mark[index*2+1]); |
---|
2280 | n/a | if (!item) { |
---|
2281 | n/a | Py_DECREF(regs); |
---|
2282 | n/a | return NULL; |
---|
2283 | n/a | } |
---|
2284 | n/a | PyTuple_SET_ITEM(regs, index, item); |
---|
2285 | n/a | } |
---|
2286 | n/a | |
---|
2287 | n/a | Py_INCREF(regs); |
---|
2288 | n/a | self->regs = regs; |
---|
2289 | n/a | |
---|
2290 | n/a | return regs; |
---|
2291 | n/a | } |
---|
2292 | n/a | |
---|
2293 | n/a | /*[clinic input] |
---|
2294 | n/a | _sre.SRE_Match.__copy__ |
---|
2295 | n/a | |
---|
2296 | n/a | [clinic start generated code]*/ |
---|
2297 | n/a | |
---|
2298 | n/a | static PyObject * |
---|
2299 | n/a | _sre_SRE_Match___copy___impl(MatchObject *self) |
---|
2300 | n/a | /*[clinic end generated code: output=a779c5fc8b5b4eb4 input=3bb4d30b6baddb5b]*/ |
---|
2301 | n/a | { |
---|
2302 | n/a | #ifdef USE_BUILTIN_COPY |
---|
2303 | n/a | MatchObject* copy; |
---|
2304 | n/a | Py_ssize_t slots, offset; |
---|
2305 | n/a | |
---|
2306 | n/a | slots = 2 * (self->pattern->groups+1); |
---|
2307 | n/a | |
---|
2308 | n/a | copy = PyObject_NEW_VAR(MatchObject, &Match_Type, slots); |
---|
2309 | n/a | if (!copy) |
---|
2310 | n/a | return NULL; |
---|
2311 | n/a | |
---|
2312 | n/a | /* this value a constant, but any compiler should be able to |
---|
2313 | n/a | figure that out all by itself */ |
---|
2314 | n/a | offset = offsetof(MatchObject, string); |
---|
2315 | n/a | |
---|
2316 | n/a | Py_XINCREF(self->pattern); |
---|
2317 | n/a | Py_XINCREF(self->string); |
---|
2318 | n/a | Py_XINCREF(self->regs); |
---|
2319 | n/a | |
---|
2320 | n/a | memcpy((char*) copy + offset, (char*) self + offset, |
---|
2321 | n/a | sizeof(MatchObject) + slots * sizeof(Py_ssize_t) - offset); |
---|
2322 | n/a | |
---|
2323 | n/a | return (PyObject*) copy; |
---|
2324 | n/a | #else |
---|
2325 | n/a | PyErr_SetString(PyExc_TypeError, "cannot copy this match object"); |
---|
2326 | n/a | return NULL; |
---|
2327 | n/a | #endif |
---|
2328 | n/a | } |
---|
2329 | n/a | |
---|
2330 | n/a | /*[clinic input] |
---|
2331 | n/a | _sre.SRE_Match.__deepcopy__ |
---|
2332 | n/a | |
---|
2333 | n/a | memo: object |
---|
2334 | n/a | |
---|
2335 | n/a | [clinic start generated code]*/ |
---|
2336 | n/a | |
---|
2337 | n/a | static PyObject * |
---|
2338 | n/a | _sre_SRE_Match___deepcopy___impl(MatchObject *self, PyObject *memo) |
---|
2339 | n/a | /*[clinic end generated code: output=2b657578eb03f4a3 input=b65b72489eac64cc]*/ |
---|
2340 | n/a | { |
---|
2341 | n/a | #ifdef USE_BUILTIN_COPY |
---|
2342 | n/a | MatchObject* copy; |
---|
2343 | n/a | |
---|
2344 | n/a | copy = (MatchObject*) match_copy(self); |
---|
2345 | n/a | if (!copy) |
---|
2346 | n/a | return NULL; |
---|
2347 | n/a | |
---|
2348 | n/a | if (!deepcopy((PyObject**) ©->pattern, memo) || |
---|
2349 | n/a | !deepcopy(©->string, memo) || |
---|
2350 | n/a | !deepcopy(©->regs, memo)) { |
---|
2351 | n/a | Py_DECREF(copy); |
---|
2352 | n/a | return NULL; |
---|
2353 | n/a | } |
---|
2354 | n/a | |
---|
2355 | n/a | #else |
---|
2356 | n/a | PyErr_SetString(PyExc_TypeError, "cannot deepcopy this match object"); |
---|
2357 | n/a | return NULL; |
---|
2358 | n/a | #endif |
---|
2359 | n/a | } |
---|
2360 | n/a | |
---|
2361 | n/a | PyDoc_STRVAR(match_doc, |
---|
2362 | n/a | "The result of re.match() and re.search().\n\ |
---|
2363 | n/a | Match objects always have a boolean value of True."); |
---|
2364 | n/a | |
---|
2365 | n/a | PyDoc_STRVAR(match_group_doc, |
---|
2366 | n/a | "group([group1, ...]) -> str or tuple.\n\ |
---|
2367 | n/a | Return subgroup(s) of the match by indices or names.\n\ |
---|
2368 | n/a | For 0 returns the entire match."); |
---|
2369 | n/a | |
---|
2370 | n/a | static PyObject * |
---|
2371 | n/a | match_lastindex_get(MatchObject *self) |
---|
2372 | n/a | { |
---|
2373 | n/a | if (self->lastindex >= 0) |
---|
2374 | n/a | return PyLong_FromSsize_t(self->lastindex); |
---|
2375 | n/a | Py_RETURN_NONE; |
---|
2376 | n/a | } |
---|
2377 | n/a | |
---|
2378 | n/a | static PyObject * |
---|
2379 | n/a | match_lastgroup_get(MatchObject *self) |
---|
2380 | n/a | { |
---|
2381 | n/a | if (self->pattern->indexgroup && self->lastindex >= 0) { |
---|
2382 | n/a | PyObject* result = PySequence_GetItem( |
---|
2383 | n/a | self->pattern->indexgroup, self->lastindex |
---|
2384 | n/a | ); |
---|
2385 | n/a | if (result) |
---|
2386 | n/a | return result; |
---|
2387 | n/a | PyErr_Clear(); |
---|
2388 | n/a | } |
---|
2389 | n/a | Py_RETURN_NONE; |
---|
2390 | n/a | } |
---|
2391 | n/a | |
---|
2392 | n/a | static PyObject * |
---|
2393 | n/a | match_regs_get(MatchObject *self) |
---|
2394 | n/a | { |
---|
2395 | n/a | if (self->regs) { |
---|
2396 | n/a | Py_INCREF(self->regs); |
---|
2397 | n/a | return self->regs; |
---|
2398 | n/a | } else |
---|
2399 | n/a | return match_regs(self); |
---|
2400 | n/a | } |
---|
2401 | n/a | |
---|
2402 | n/a | static PyObject * |
---|
2403 | n/a | match_repr(MatchObject *self) |
---|
2404 | n/a | { |
---|
2405 | n/a | PyObject *result; |
---|
2406 | n/a | PyObject *group0 = match_getslice_by_index(self, 0, Py_None); |
---|
2407 | n/a | if (group0 == NULL) |
---|
2408 | n/a | return NULL; |
---|
2409 | n/a | result = PyUnicode_FromFormat( |
---|
2410 | n/a | "<%s object; span=(%d, %d), match=%.50R>", |
---|
2411 | n/a | Py_TYPE(self)->tp_name, |
---|
2412 | n/a | self->mark[0], self->mark[1], group0); |
---|
2413 | n/a | Py_DECREF(group0); |
---|
2414 | n/a | return result; |
---|
2415 | n/a | } |
---|
2416 | n/a | |
---|
2417 | n/a | |
---|
2418 | n/a | static PyObject* |
---|
2419 | n/a | pattern_new_match(PatternObject* pattern, SRE_STATE* state, Py_ssize_t status) |
---|
2420 | n/a | { |
---|
2421 | n/a | /* create match object (from state object) */ |
---|
2422 | n/a | |
---|
2423 | n/a | MatchObject* match; |
---|
2424 | n/a | Py_ssize_t i, j; |
---|
2425 | n/a | char* base; |
---|
2426 | n/a | int n; |
---|
2427 | n/a | |
---|
2428 | n/a | if (status > 0) { |
---|
2429 | n/a | |
---|
2430 | n/a | /* create match object (with room for extra group marks) */ |
---|
2431 | n/a | /* coverity[ampersand_in_size] */ |
---|
2432 | n/a | match = PyObject_NEW_VAR(MatchObject, &Match_Type, |
---|
2433 | n/a | 2*(pattern->groups+1)); |
---|
2434 | n/a | if (!match) |
---|
2435 | n/a | return NULL; |
---|
2436 | n/a | |
---|
2437 | n/a | Py_INCREF(pattern); |
---|
2438 | n/a | match->pattern = pattern; |
---|
2439 | n/a | |
---|
2440 | n/a | Py_INCREF(state->string); |
---|
2441 | n/a | match->string = state->string; |
---|
2442 | n/a | |
---|
2443 | n/a | match->regs = NULL; |
---|
2444 | n/a | match->groups = pattern->groups+1; |
---|
2445 | n/a | |
---|
2446 | n/a | /* fill in group slices */ |
---|
2447 | n/a | |
---|
2448 | n/a | base = (char*) state->beginning; |
---|
2449 | n/a | n = state->charsize; |
---|
2450 | n/a | |
---|
2451 | n/a | match->mark[0] = ((char*) state->start - base) / n; |
---|
2452 | n/a | match->mark[1] = ((char*) state->ptr - base) / n; |
---|
2453 | n/a | |
---|
2454 | n/a | for (i = j = 0; i < pattern->groups; i++, j+=2) |
---|
2455 | n/a | if (j+1 <= state->lastmark && state->mark[j] && state->mark[j+1]) { |
---|
2456 | n/a | match->mark[j+2] = ((char*) state->mark[j] - base) / n; |
---|
2457 | n/a | match->mark[j+3] = ((char*) state->mark[j+1] - base) / n; |
---|
2458 | n/a | } else |
---|
2459 | n/a | match->mark[j+2] = match->mark[j+3] = -1; /* undefined */ |
---|
2460 | n/a | |
---|
2461 | n/a | match->pos = state->pos; |
---|
2462 | n/a | match->endpos = state->endpos; |
---|
2463 | n/a | |
---|
2464 | n/a | match->lastindex = state->lastindex; |
---|
2465 | n/a | |
---|
2466 | n/a | return (PyObject*) match; |
---|
2467 | n/a | |
---|
2468 | n/a | } else if (status == 0) { |
---|
2469 | n/a | |
---|
2470 | n/a | /* no match */ |
---|
2471 | n/a | Py_RETURN_NONE; |
---|
2472 | n/a | |
---|
2473 | n/a | } |
---|
2474 | n/a | |
---|
2475 | n/a | /* internal error */ |
---|
2476 | n/a | pattern_error(status); |
---|
2477 | n/a | return NULL; |
---|
2478 | n/a | } |
---|
2479 | n/a | |
---|
2480 | n/a | |
---|
2481 | n/a | /* -------------------------------------------------------------------- */ |
---|
2482 | n/a | /* scanner methods (experimental) */ |
---|
2483 | n/a | |
---|
2484 | n/a | static void |
---|
2485 | n/a | scanner_dealloc(ScannerObject* self) |
---|
2486 | n/a | { |
---|
2487 | n/a | state_fini(&self->state); |
---|
2488 | n/a | Py_XDECREF(self->pattern); |
---|
2489 | n/a | PyObject_DEL(self); |
---|
2490 | n/a | } |
---|
2491 | n/a | |
---|
2492 | n/a | /*[clinic input] |
---|
2493 | n/a | _sre.SRE_Scanner.match |
---|
2494 | n/a | |
---|
2495 | n/a | [clinic start generated code]*/ |
---|
2496 | n/a | |
---|
2497 | n/a | static PyObject * |
---|
2498 | n/a | _sre_SRE_Scanner_match_impl(ScannerObject *self) |
---|
2499 | n/a | /*[clinic end generated code: output=936b30c63d4b81eb input=881a0154f8c13d9a]*/ |
---|
2500 | n/a | { |
---|
2501 | n/a | SRE_STATE* state = &self->state; |
---|
2502 | n/a | PyObject* match; |
---|
2503 | n/a | Py_ssize_t status; |
---|
2504 | n/a | |
---|
2505 | n/a | if (state->start == NULL) |
---|
2506 | n/a | Py_RETURN_NONE; |
---|
2507 | n/a | |
---|
2508 | n/a | state_reset(state); |
---|
2509 | n/a | |
---|
2510 | n/a | state->ptr = state->start; |
---|
2511 | n/a | |
---|
2512 | n/a | status = sre_match(state, PatternObject_GetCode(self->pattern), 0); |
---|
2513 | n/a | if (PyErr_Occurred()) |
---|
2514 | n/a | return NULL; |
---|
2515 | n/a | |
---|
2516 | n/a | match = pattern_new_match((PatternObject*) self->pattern, |
---|
2517 | n/a | state, status); |
---|
2518 | n/a | |
---|
2519 | n/a | if (status == 0) |
---|
2520 | n/a | state->start = NULL; |
---|
2521 | n/a | else if (state->ptr != state->start) |
---|
2522 | n/a | state->start = state->ptr; |
---|
2523 | n/a | else if (state->ptr != state->end) |
---|
2524 | n/a | state->start = (void*) ((char*) state->ptr + state->charsize); |
---|
2525 | n/a | else |
---|
2526 | n/a | state->start = NULL; |
---|
2527 | n/a | |
---|
2528 | n/a | return match; |
---|
2529 | n/a | } |
---|
2530 | n/a | |
---|
2531 | n/a | |
---|
2532 | n/a | /*[clinic input] |
---|
2533 | n/a | _sre.SRE_Scanner.search |
---|
2534 | n/a | |
---|
2535 | n/a | [clinic start generated code]*/ |
---|
2536 | n/a | |
---|
2537 | n/a | static PyObject * |
---|
2538 | n/a | _sre_SRE_Scanner_search_impl(ScannerObject *self) |
---|
2539 | n/a | /*[clinic end generated code: output=7dc211986088f025 input=161223ee92ef9270]*/ |
---|
2540 | n/a | { |
---|
2541 | n/a | SRE_STATE* state = &self->state; |
---|
2542 | n/a | PyObject* match; |
---|
2543 | n/a | Py_ssize_t status; |
---|
2544 | n/a | |
---|
2545 | n/a | if (state->start == NULL) |
---|
2546 | n/a | Py_RETURN_NONE; |
---|
2547 | n/a | |
---|
2548 | n/a | state_reset(state); |
---|
2549 | n/a | |
---|
2550 | n/a | state->ptr = state->start; |
---|
2551 | n/a | |
---|
2552 | n/a | status = sre_search(state, PatternObject_GetCode(self->pattern)); |
---|
2553 | n/a | if (PyErr_Occurred()) |
---|
2554 | n/a | return NULL; |
---|
2555 | n/a | |
---|
2556 | n/a | match = pattern_new_match((PatternObject*) self->pattern, |
---|
2557 | n/a | state, status); |
---|
2558 | n/a | |
---|
2559 | n/a | if (status == 0) |
---|
2560 | n/a | state->start = NULL; |
---|
2561 | n/a | else if (state->ptr != state->start) |
---|
2562 | n/a | state->start = state->ptr; |
---|
2563 | n/a | else if (state->ptr != state->end) |
---|
2564 | n/a | state->start = (void*) ((char*) state->ptr + state->charsize); |
---|
2565 | n/a | else |
---|
2566 | n/a | state->start = NULL; |
---|
2567 | n/a | |
---|
2568 | n/a | return match; |
---|
2569 | n/a | } |
---|
2570 | n/a | |
---|
2571 | n/a | static PyObject * |
---|
2572 | n/a | pattern_scanner(PatternObject *self, PyObject *string, Py_ssize_t pos, Py_ssize_t endpos) |
---|
2573 | n/a | { |
---|
2574 | n/a | ScannerObject* scanner; |
---|
2575 | n/a | |
---|
2576 | n/a | /* create scanner object */ |
---|
2577 | n/a | scanner = PyObject_NEW(ScannerObject, &Scanner_Type); |
---|
2578 | n/a | if (!scanner) |
---|
2579 | n/a | return NULL; |
---|
2580 | n/a | scanner->pattern = NULL; |
---|
2581 | n/a | |
---|
2582 | n/a | /* create search state object */ |
---|
2583 | n/a | if (!state_init(&scanner->state, self, string, pos, endpos)) { |
---|
2584 | n/a | Py_DECREF(scanner); |
---|
2585 | n/a | return NULL; |
---|
2586 | n/a | } |
---|
2587 | n/a | |
---|
2588 | n/a | Py_INCREF(self); |
---|
2589 | n/a | scanner->pattern = (PyObject*) self; |
---|
2590 | n/a | |
---|
2591 | n/a | return (PyObject*) scanner; |
---|
2592 | n/a | } |
---|
2593 | n/a | |
---|
2594 | n/a | static Py_hash_t |
---|
2595 | n/a | pattern_hash(PatternObject *self) |
---|
2596 | n/a | { |
---|
2597 | n/a | Py_hash_t hash, hash2; |
---|
2598 | n/a | |
---|
2599 | n/a | hash = PyObject_Hash(self->pattern); |
---|
2600 | n/a | if (hash == -1) { |
---|
2601 | n/a | return -1; |
---|
2602 | n/a | } |
---|
2603 | n/a | |
---|
2604 | n/a | hash2 = _Py_HashBytes(self->code, sizeof(self->code[0]) * self->codesize); |
---|
2605 | n/a | hash ^= hash2; |
---|
2606 | n/a | |
---|
2607 | n/a | hash ^= self->flags; |
---|
2608 | n/a | hash ^= self->isbytes; |
---|
2609 | n/a | hash ^= self->codesize; |
---|
2610 | n/a | |
---|
2611 | n/a | if (hash == -1) { |
---|
2612 | n/a | hash = -2; |
---|
2613 | n/a | } |
---|
2614 | n/a | return hash; |
---|
2615 | n/a | } |
---|
2616 | n/a | |
---|
2617 | n/a | static PyObject* |
---|
2618 | n/a | pattern_richcompare(PyObject *lefto, PyObject *righto, int op) |
---|
2619 | n/a | { |
---|
2620 | n/a | PatternObject *left, *right; |
---|
2621 | n/a | int cmp; |
---|
2622 | n/a | |
---|
2623 | n/a | if (op != Py_EQ && op != Py_NE) { |
---|
2624 | n/a | Py_RETURN_NOTIMPLEMENTED; |
---|
2625 | n/a | } |
---|
2626 | n/a | |
---|
2627 | n/a | if (Py_TYPE(lefto) != &Pattern_Type || Py_TYPE(righto) != &Pattern_Type) { |
---|
2628 | n/a | Py_RETURN_NOTIMPLEMENTED; |
---|
2629 | n/a | } |
---|
2630 | n/a | |
---|
2631 | n/a | if (lefto == righto) { |
---|
2632 | n/a | /* a pattern is equal to itself */ |
---|
2633 | n/a | return PyBool_FromLong(op == Py_EQ); |
---|
2634 | n/a | } |
---|
2635 | n/a | |
---|
2636 | n/a | left = (PatternObject *)lefto; |
---|
2637 | n/a | right = (PatternObject *)righto; |
---|
2638 | n/a | |
---|
2639 | n/a | cmp = (left->flags == right->flags |
---|
2640 | n/a | && left->isbytes == right->isbytes |
---|
2641 | n/a | && left->codesize == right->codesize); |
---|
2642 | n/a | if (cmp) { |
---|
2643 | n/a | /* Compare the code and the pattern because the same pattern can |
---|
2644 | n/a | produce different codes depending on the locale used to compile the |
---|
2645 | n/a | pattern when the re.LOCALE flag is used. Don't compare groups, |
---|
2646 | n/a | indexgroup nor groupindex: they are derivated from the pattern. */ |
---|
2647 | n/a | cmp = (memcmp(left->code, right->code, |
---|
2648 | n/a | sizeof(left->code[0]) * left->codesize) == 0); |
---|
2649 | n/a | } |
---|
2650 | n/a | if (cmp) { |
---|
2651 | n/a | cmp = PyObject_RichCompareBool(left->pattern, right->pattern, |
---|
2652 | n/a | Py_EQ); |
---|
2653 | n/a | if (cmp < 0) { |
---|
2654 | n/a | return NULL; |
---|
2655 | n/a | } |
---|
2656 | n/a | } |
---|
2657 | n/a | if (op == Py_NE) { |
---|
2658 | n/a | cmp = !cmp; |
---|
2659 | n/a | } |
---|
2660 | n/a | return PyBool_FromLong(cmp); |
---|
2661 | n/a | } |
---|
2662 | n/a | |
---|
2663 | n/a | #include "clinic/_sre.c.h" |
---|
2664 | n/a | |
---|
2665 | n/a | static PyMethodDef pattern_methods[] = { |
---|
2666 | n/a | _SRE_SRE_PATTERN_MATCH_METHODDEF |
---|
2667 | n/a | _SRE_SRE_PATTERN_FULLMATCH_METHODDEF |
---|
2668 | n/a | _SRE_SRE_PATTERN_SEARCH_METHODDEF |
---|
2669 | n/a | _SRE_SRE_PATTERN_SUB_METHODDEF |
---|
2670 | n/a | _SRE_SRE_PATTERN_SUBN_METHODDEF |
---|
2671 | n/a | _SRE_SRE_PATTERN_FINDALL_METHODDEF |
---|
2672 | n/a | _SRE_SRE_PATTERN_SPLIT_METHODDEF |
---|
2673 | n/a | _SRE_SRE_PATTERN_FINDITER_METHODDEF |
---|
2674 | n/a | _SRE_SRE_PATTERN_SCANNER_METHODDEF |
---|
2675 | n/a | _SRE_SRE_PATTERN___COPY___METHODDEF |
---|
2676 | n/a | _SRE_SRE_PATTERN___DEEPCOPY___METHODDEF |
---|
2677 | n/a | {NULL, NULL} |
---|
2678 | n/a | }; |
---|
2679 | n/a | |
---|
2680 | n/a | static PyGetSetDef pattern_getset[] = { |
---|
2681 | n/a | {"groupindex", (getter)pattern_groupindex, (setter)NULL, |
---|
2682 | n/a | "A dictionary mapping group names to group numbers."}, |
---|
2683 | n/a | {NULL} /* Sentinel */ |
---|
2684 | n/a | }; |
---|
2685 | n/a | |
---|
2686 | n/a | #define PAT_OFF(x) offsetof(PatternObject, x) |
---|
2687 | n/a | static PyMemberDef pattern_members[] = { |
---|
2688 | n/a | {"pattern", T_OBJECT, PAT_OFF(pattern), READONLY}, |
---|
2689 | n/a | {"flags", T_INT, PAT_OFF(flags), READONLY}, |
---|
2690 | n/a | {"groups", T_PYSSIZET, PAT_OFF(groups), READONLY}, |
---|
2691 | n/a | {NULL} /* Sentinel */ |
---|
2692 | n/a | }; |
---|
2693 | n/a | |
---|
2694 | n/a | static PyTypeObject Pattern_Type = { |
---|
2695 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
2696 | n/a | "_" SRE_MODULE ".SRE_Pattern", |
---|
2697 | n/a | sizeof(PatternObject), sizeof(SRE_CODE), |
---|
2698 | n/a | (destructor)pattern_dealloc, /* tp_dealloc */ |
---|
2699 | n/a | 0, /* tp_print */ |
---|
2700 | n/a | 0, /* tp_getattr */ |
---|
2701 | n/a | 0, /* tp_setattr */ |
---|
2702 | n/a | 0, /* tp_reserved */ |
---|
2703 | n/a | (reprfunc)pattern_repr, /* tp_repr */ |
---|
2704 | n/a | 0, /* tp_as_number */ |
---|
2705 | n/a | 0, /* tp_as_sequence */ |
---|
2706 | n/a | 0, /* tp_as_mapping */ |
---|
2707 | n/a | (hashfunc)pattern_hash, /* tp_hash */ |
---|
2708 | n/a | 0, /* tp_call */ |
---|
2709 | n/a | 0, /* tp_str */ |
---|
2710 | n/a | 0, /* tp_getattro */ |
---|
2711 | n/a | 0, /* tp_setattro */ |
---|
2712 | n/a | 0, /* tp_as_buffer */ |
---|
2713 | n/a | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
2714 | n/a | pattern_doc, /* tp_doc */ |
---|
2715 | n/a | 0, /* tp_traverse */ |
---|
2716 | n/a | 0, /* tp_clear */ |
---|
2717 | n/a | pattern_richcompare, /* tp_richcompare */ |
---|
2718 | n/a | offsetof(PatternObject, weakreflist), /* tp_weaklistoffset */ |
---|
2719 | n/a | 0, /* tp_iter */ |
---|
2720 | n/a | 0, /* tp_iternext */ |
---|
2721 | n/a | pattern_methods, /* tp_methods */ |
---|
2722 | n/a | pattern_members, /* tp_members */ |
---|
2723 | n/a | pattern_getset, /* tp_getset */ |
---|
2724 | n/a | }; |
---|
2725 | n/a | |
---|
2726 | n/a | /* Match objects do not support length or assignment, but do support |
---|
2727 | n/a | __getitem__. */ |
---|
2728 | n/a | static PyMappingMethods match_as_mapping = { |
---|
2729 | n/a | NULL, |
---|
2730 | n/a | (binaryfunc)match_getitem, |
---|
2731 | n/a | NULL |
---|
2732 | n/a | }; |
---|
2733 | n/a | |
---|
2734 | n/a | static PyMethodDef match_methods[] = { |
---|
2735 | n/a | {"group", (PyCFunction) match_group, METH_VARARGS, match_group_doc}, |
---|
2736 | n/a | _SRE_SRE_MATCH_START_METHODDEF |
---|
2737 | n/a | _SRE_SRE_MATCH_END_METHODDEF |
---|
2738 | n/a | _SRE_SRE_MATCH_SPAN_METHODDEF |
---|
2739 | n/a | _SRE_SRE_MATCH_GROUPS_METHODDEF |
---|
2740 | n/a | _SRE_SRE_MATCH_GROUPDICT_METHODDEF |
---|
2741 | n/a | _SRE_SRE_MATCH_EXPAND_METHODDEF |
---|
2742 | n/a | _SRE_SRE_MATCH___COPY___METHODDEF |
---|
2743 | n/a | _SRE_SRE_MATCH___DEEPCOPY___METHODDEF |
---|
2744 | n/a | {NULL, NULL} |
---|
2745 | n/a | }; |
---|
2746 | n/a | |
---|
2747 | n/a | static PyGetSetDef match_getset[] = { |
---|
2748 | n/a | {"lastindex", (getter)match_lastindex_get, (setter)NULL}, |
---|
2749 | n/a | {"lastgroup", (getter)match_lastgroup_get, (setter)NULL}, |
---|
2750 | n/a | {"regs", (getter)match_regs_get, (setter)NULL}, |
---|
2751 | n/a | {NULL} |
---|
2752 | n/a | }; |
---|
2753 | n/a | |
---|
2754 | n/a | #define MATCH_OFF(x) offsetof(MatchObject, x) |
---|
2755 | n/a | static PyMemberDef match_members[] = { |
---|
2756 | n/a | {"string", T_OBJECT, MATCH_OFF(string), READONLY}, |
---|
2757 | n/a | {"re", T_OBJECT, MATCH_OFF(pattern), READONLY}, |
---|
2758 | n/a | {"pos", T_PYSSIZET, MATCH_OFF(pos), READONLY}, |
---|
2759 | n/a | {"endpos", T_PYSSIZET, MATCH_OFF(endpos), READONLY}, |
---|
2760 | n/a | {NULL} |
---|
2761 | n/a | }; |
---|
2762 | n/a | |
---|
2763 | n/a | /* FIXME: implement setattr("string", None) as a special case (to |
---|
2764 | n/a | detach the associated string, if any */ |
---|
2765 | n/a | |
---|
2766 | n/a | static PyTypeObject Match_Type = { |
---|
2767 | n/a | PyVarObject_HEAD_INIT(NULL,0) |
---|
2768 | n/a | "_" SRE_MODULE ".SRE_Match", |
---|
2769 | n/a | sizeof(MatchObject), sizeof(Py_ssize_t), |
---|
2770 | n/a | (destructor)match_dealloc, /* tp_dealloc */ |
---|
2771 | n/a | 0, /* tp_print */ |
---|
2772 | n/a | 0, /* tp_getattr */ |
---|
2773 | n/a | 0, /* tp_setattr */ |
---|
2774 | n/a | 0, /* tp_reserved */ |
---|
2775 | n/a | (reprfunc)match_repr, /* tp_repr */ |
---|
2776 | n/a | 0, /* tp_as_number */ |
---|
2777 | n/a | 0, /* tp_as_sequence */ |
---|
2778 | n/a | &match_as_mapping, /* tp_as_mapping */ |
---|
2779 | n/a | 0, /* tp_hash */ |
---|
2780 | n/a | 0, /* tp_call */ |
---|
2781 | n/a | 0, /* tp_str */ |
---|
2782 | n/a | 0, /* tp_getattro */ |
---|
2783 | n/a | 0, /* tp_setattro */ |
---|
2784 | n/a | 0, /* tp_as_buffer */ |
---|
2785 | n/a | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
2786 | n/a | match_doc, /* tp_doc */ |
---|
2787 | n/a | 0, /* tp_traverse */ |
---|
2788 | n/a | 0, /* tp_clear */ |
---|
2789 | n/a | 0, /* tp_richcompare */ |
---|
2790 | n/a | 0, /* tp_weaklistoffset */ |
---|
2791 | n/a | 0, /* tp_iter */ |
---|
2792 | n/a | 0, /* tp_iternext */ |
---|
2793 | n/a | match_methods, /* tp_methods */ |
---|
2794 | n/a | match_members, /* tp_members */ |
---|
2795 | n/a | match_getset, /* tp_getset */ |
---|
2796 | n/a | }; |
---|
2797 | n/a | |
---|
2798 | n/a | static PyMethodDef scanner_methods[] = { |
---|
2799 | n/a | _SRE_SRE_SCANNER_MATCH_METHODDEF |
---|
2800 | n/a | _SRE_SRE_SCANNER_SEARCH_METHODDEF |
---|
2801 | n/a | {NULL, NULL} |
---|
2802 | n/a | }; |
---|
2803 | n/a | |
---|
2804 | n/a | #define SCAN_OFF(x) offsetof(ScannerObject, x) |
---|
2805 | n/a | static PyMemberDef scanner_members[] = { |
---|
2806 | n/a | {"pattern", T_OBJECT, SCAN_OFF(pattern), READONLY}, |
---|
2807 | n/a | {NULL} /* Sentinel */ |
---|
2808 | n/a | }; |
---|
2809 | n/a | |
---|
2810 | n/a | static PyTypeObject Scanner_Type = { |
---|
2811 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
2812 | n/a | "_" SRE_MODULE ".SRE_Scanner", |
---|
2813 | n/a | sizeof(ScannerObject), 0, |
---|
2814 | n/a | (destructor)scanner_dealloc,/* tp_dealloc */ |
---|
2815 | n/a | 0, /* tp_print */ |
---|
2816 | n/a | 0, /* tp_getattr */ |
---|
2817 | n/a | 0, /* tp_setattr */ |
---|
2818 | n/a | 0, /* tp_reserved */ |
---|
2819 | n/a | 0, /* tp_repr */ |
---|
2820 | n/a | 0, /* tp_as_number */ |
---|
2821 | n/a | 0, /* tp_as_sequence */ |
---|
2822 | n/a | 0, /* tp_as_mapping */ |
---|
2823 | n/a | 0, /* tp_hash */ |
---|
2824 | n/a | 0, /* tp_call */ |
---|
2825 | n/a | 0, /* tp_str */ |
---|
2826 | n/a | 0, /* tp_getattro */ |
---|
2827 | n/a | 0, /* tp_setattro */ |
---|
2828 | n/a | 0, /* tp_as_buffer */ |
---|
2829 | n/a | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
2830 | n/a | 0, /* tp_doc */ |
---|
2831 | n/a | 0, /* tp_traverse */ |
---|
2832 | n/a | 0, /* tp_clear */ |
---|
2833 | n/a | 0, /* tp_richcompare */ |
---|
2834 | n/a | 0, /* tp_weaklistoffset */ |
---|
2835 | n/a | 0, /* tp_iter */ |
---|
2836 | n/a | 0, /* tp_iternext */ |
---|
2837 | n/a | scanner_methods, /* tp_methods */ |
---|
2838 | n/a | scanner_members, /* tp_members */ |
---|
2839 | n/a | 0, /* tp_getset */ |
---|
2840 | n/a | }; |
---|
2841 | n/a | |
---|
2842 | n/a | static PyMethodDef _functions[] = { |
---|
2843 | n/a | _SRE_COMPILE_METHODDEF |
---|
2844 | n/a | _SRE_GETCODESIZE_METHODDEF |
---|
2845 | n/a | _SRE_GETLOWER_METHODDEF |
---|
2846 | n/a | {NULL, NULL} |
---|
2847 | n/a | }; |
---|
2848 | n/a | |
---|
2849 | n/a | static struct PyModuleDef sremodule = { |
---|
2850 | n/a | PyModuleDef_HEAD_INIT, |
---|
2851 | n/a | "_" SRE_MODULE, |
---|
2852 | n/a | NULL, |
---|
2853 | n/a | -1, |
---|
2854 | n/a | _functions, |
---|
2855 | n/a | NULL, |
---|
2856 | n/a | NULL, |
---|
2857 | n/a | NULL, |
---|
2858 | n/a | NULL |
---|
2859 | n/a | }; |
---|
2860 | n/a | |
---|
2861 | n/a | PyMODINIT_FUNC PyInit__sre(void) |
---|
2862 | n/a | { |
---|
2863 | n/a | PyObject* m; |
---|
2864 | n/a | PyObject* d; |
---|
2865 | n/a | PyObject* x; |
---|
2866 | n/a | |
---|
2867 | n/a | /* Patch object types */ |
---|
2868 | n/a | if (PyType_Ready(&Pattern_Type) || PyType_Ready(&Match_Type) || |
---|
2869 | n/a | PyType_Ready(&Scanner_Type)) |
---|
2870 | n/a | return NULL; |
---|
2871 | n/a | |
---|
2872 | n/a | m = PyModule_Create(&sremodule); |
---|
2873 | n/a | if (m == NULL) |
---|
2874 | n/a | return NULL; |
---|
2875 | n/a | d = PyModule_GetDict(m); |
---|
2876 | n/a | |
---|
2877 | n/a | x = PyLong_FromLong(SRE_MAGIC); |
---|
2878 | n/a | if (x) { |
---|
2879 | n/a | PyDict_SetItemString(d, "MAGIC", x); |
---|
2880 | n/a | Py_DECREF(x); |
---|
2881 | n/a | } |
---|
2882 | n/a | |
---|
2883 | n/a | x = PyLong_FromLong(sizeof(SRE_CODE)); |
---|
2884 | n/a | if (x) { |
---|
2885 | n/a | PyDict_SetItemString(d, "CODESIZE", x); |
---|
2886 | n/a | Py_DECREF(x); |
---|
2887 | n/a | } |
---|
2888 | n/a | |
---|
2889 | n/a | x = PyLong_FromUnsignedLong(SRE_MAXREPEAT); |
---|
2890 | n/a | if (x) { |
---|
2891 | n/a | PyDict_SetItemString(d, "MAXREPEAT", x); |
---|
2892 | n/a | Py_DECREF(x); |
---|
2893 | n/a | } |
---|
2894 | n/a | |
---|
2895 | n/a | x = PyLong_FromUnsignedLong(SRE_MAXGROUPS); |
---|
2896 | n/a | if (x) { |
---|
2897 | n/a | PyDict_SetItemString(d, "MAXGROUPS", x); |
---|
2898 | n/a | Py_DECREF(x); |
---|
2899 | n/a | } |
---|
2900 | n/a | |
---|
2901 | n/a | x = PyUnicode_FromString(copyright); |
---|
2902 | n/a | if (x) { |
---|
2903 | n/a | PyDict_SetItemString(d, "copyright", x); |
---|
2904 | n/a | Py_DECREF(x); |
---|
2905 | n/a | } |
---|
2906 | n/a | return m; |
---|
2907 | n/a | } |
---|
2908 | n/a | |
---|
2909 | n/a | /* vim:ts=4:sw=4:et |
---|
2910 | n/a | */ |
---|