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