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