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