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