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