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