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