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