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