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