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