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