Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 2 | XXX support range parameter on search |
| 3 | XXX support mstop parameter on search |
| 4 | */ |
| 5 | |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 6 | |
| 7 | /* Regular expression objects */ |
Guido van Rossum | 1cab95c | 1992-01-19 16:31:57 +0000 | [diff] [blame] | 8 | /* This uses Tatu Ylonen's copyleft-free reimplementation of |
| 9 | GNU regular expressions */ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 11 | #include "Python.h" |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 13 | #include <ctype.h> |
| 14 | |
Guido van Rossum | 1cab95c | 1992-01-19 16:31:57 +0000 | [diff] [blame] | 15 | #include "regexpr.h" |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 17 | static PyObject *RegexError; /* Exception */ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 18 | |
| 19 | typedef struct { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 20 | PyObject_HEAD |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 21 | struct re_pattern_buffer re_patbuf; /* The compiled expression */ |
| 22 | struct re_registers re_regs; /* The registers from the last match */ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 23 | char re_fastmap[256]; /* Storage for fastmap */ |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 24 | PyObject *re_translate; /* String object for translate table */ |
| 25 | PyObject *re_lastok; /* String object last matched/searched */ |
| 26 | PyObject *re_groupindex; /* Group name to index dictionary */ |
| 27 | PyObject *re_givenpat; /* Pattern with symbolic groups */ |
| 28 | PyObject *re_realpat; /* Pattern without symbolic groups */ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 29 | } regexobject; |
| 30 | |
| 31 | /* Regex object methods */ |
| 32 | |
| 33 | static void |
Peter Schneider-Kamp | 7d0c71a | 2000-07-10 13:05:29 +0000 | [diff] [blame] | 34 | reg_dealloc(regexobject *re) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 35 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 36 | if (re->re_patbuf.buffer) |
Vladimir Marangozov | 9e3d73a | 2000-07-12 00:49:17 +0000 | [diff] [blame] | 37 | free(re->re_patbuf.buffer); |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 38 | Py_XDECREF(re->re_translate); |
| 39 | Py_XDECREF(re->re_lastok); |
| 40 | Py_XDECREF(re->re_groupindex); |
| 41 | Py_XDECREF(re->re_givenpat); |
| 42 | Py_XDECREF(re->re_realpat); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 43 | PyObject_Del(re); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 46 | static PyObject * |
Peter Schneider-Kamp | 7d0c71a | 2000-07-10 13:05:29 +0000 | [diff] [blame] | 47 | makeresult(struct re_registers *regs) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 48 | { |
Guido van Rossum | c196202 | 1996-10-08 14:18:42 +0000 | [diff] [blame] | 49 | PyObject *v; |
| 50 | int i; |
| 51 | static PyObject *filler = NULL; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | c196202 | 1996-10-08 14:18:42 +0000 | [diff] [blame] | 53 | if (filler == NULL) { |
| 54 | filler = Py_BuildValue("(ii)", -1, -1); |
| 55 | if (filler == NULL) |
| 56 | return NULL; |
| 57 | } |
| 58 | v = PyTuple_New(RE_NREGS); |
| 59 | if (v == NULL) |
| 60 | return NULL; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | c196202 | 1996-10-08 14:18:42 +0000 | [diff] [blame] | 62 | for (i = 0; i < RE_NREGS; i++) { |
| 63 | int lo = regs->start[i]; |
| 64 | int hi = regs->end[i]; |
| 65 | PyObject *w; |
| 66 | if (lo == -1 && hi == -1) { |
| 67 | w = filler; |
| 68 | Py_INCREF(w); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 69 | } |
Guido van Rossum | c196202 | 1996-10-08 14:18:42 +0000 | [diff] [blame] | 70 | else |
| 71 | w = Py_BuildValue("(ii)", lo, hi); |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 72 | if (w == NULL || PyTuple_SetItem(v, i, w) < 0) { |
| 73 | Py_DECREF(v); |
Guido van Rossum | c196202 | 1996-10-08 14:18:42 +0000 | [diff] [blame] | 74 | return NULL; |
| 75 | } |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 76 | } |
| 77 | return v; |
| 78 | } |
| 79 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 80 | static PyObject * |
Peter Schneider-Kamp | 7d0c71a | 2000-07-10 13:05:29 +0000 | [diff] [blame] | 81 | regobj_match(regexobject *re, PyObject *args) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 82 | { |
Guido van Rossum | 4a807f5 | 1997-05-12 16:04:09 +0000 | [diff] [blame] | 83 | PyObject *argstring; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 84 | char *buffer; |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 85 | int size; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 86 | int offset = 0; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 87 | int result; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 89 | if (!PyArg_ParseTuple(args, "O|i:match", &argstring, &offset)) |
Guido van Rossum | 4a807f5 | 1997-05-12 16:04:09 +0000 | [diff] [blame] | 90 | return NULL; |
Guido van Rossum | 7e48898 | 1998-10-08 02:25:24 +0000 | [diff] [blame] | 91 | if (!PyArg_Parse(argstring, "t#", &buffer, &size)) |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 92 | return NULL; |
| 93 | |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 94 | if (offset < 0 || offset > size) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 95 | PyErr_SetString(RegexError, "match offset out of range"); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 96 | return NULL; |
| 97 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 98 | Py_XDECREF(re->re_lastok); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 99 | re->re_lastok = NULL; |
Guido van Rossum | ed2554a | 1997-08-18 15:31:24 +0000 | [diff] [blame] | 100 | result = _Py_re_match(&re->re_patbuf, (unsigned char *)buffer, size, offset, |
Guido van Rossum | 0318bd6 | 1997-08-14 14:35:12 +0000 | [diff] [blame] | 101 | &re->re_regs); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 102 | if (result < -1) { |
Guido van Rossum | 95e8053 | 1997-08-13 22:34:14 +0000 | [diff] [blame] | 103 | /* Serious failure of some sort; if re_match didn't |
| 104 | set an exception, raise a generic error */ |
| 105 | if (!PyErr_Occurred()) |
| 106 | PyErr_SetString(RegexError, "match failure"); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 107 | return NULL; |
| 108 | } |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 109 | if (result >= 0) { |
Guido van Rossum | 4a807f5 | 1997-05-12 16:04:09 +0000 | [diff] [blame] | 110 | Py_INCREF(argstring); |
| 111 | re->re_lastok = argstring; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 112 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 113 | return PyInt_FromLong((long)result); /* Length of the match or -1 */ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 114 | } |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 116 | static PyObject * |
Peter Schneider-Kamp | 7d0c71a | 2000-07-10 13:05:29 +0000 | [diff] [blame] | 117 | regobj_search(regexobject *re, PyObject *args) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 118 | { |
Guido van Rossum | 4a807f5 | 1997-05-12 16:04:09 +0000 | [diff] [blame] | 119 | PyObject *argstring; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 120 | char *buffer; |
| 121 | int size; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 122 | int offset = 0; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 123 | int range; |
| 124 | int result; |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 126 | if (!PyArg_ParseTuple(args, "O|i:search", &argstring, &offset)) |
Guido van Rossum | 4a807f5 | 1997-05-12 16:04:09 +0000 | [diff] [blame] | 127 | return NULL; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 128 | if (!PyArg_Parse(argstring, "t#:search", &buffer, &size)) |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 129 | return NULL; |
| 130 | |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 131 | if (offset < 0 || offset > size) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 132 | PyErr_SetString(RegexError, "search offset out of range"); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 133 | return NULL; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 134 | } |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 135 | /* NB: In Emacs 18.57, the documentation for re_search[_2] and |
| 136 | the implementation don't match: the documentation states that |
| 137 | |range| positions are tried, while the code tries |range|+1 |
| 138 | positions. It seems more productive to believe the code! */ |
Guido van Rossum | 2d78590 | 1992-01-26 18:12:41 +0000 | [diff] [blame] | 139 | range = size - offset; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 140 | Py_XDECREF(re->re_lastok); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 141 | re->re_lastok = NULL; |
Guido van Rossum | ed2554a | 1997-08-18 15:31:24 +0000 | [diff] [blame] | 142 | result = _Py_re_search(&re->re_patbuf, (unsigned char *)buffer, size, offset, range, |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 143 | &re->re_regs); |
| 144 | if (result < -1) { |
Guido van Rossum | 95e8053 | 1997-08-13 22:34:14 +0000 | [diff] [blame] | 145 | /* Serious failure of some sort; if re_match didn't |
| 146 | set an exception, raise a generic error */ |
| 147 | if (!PyErr_Occurred()) |
| 148 | PyErr_SetString(RegexError, "match failure"); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 149 | return NULL; |
| 150 | } |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 151 | if (result >= 0) { |
Guido van Rossum | 4a807f5 | 1997-05-12 16:04:09 +0000 | [diff] [blame] | 152 | Py_INCREF(argstring); |
| 153 | re->re_lastok = argstring; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 154 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 155 | return PyInt_FromLong((long)result); /* Position of the match or -1 */ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 158 | /* get the group from the regex where index can be a string (group name) or |
| 159 | an integer index [0 .. 99] |
| 160 | */ |
| 161 | static PyObject* |
Peter Schneider-Kamp | 7d0c71a | 2000-07-10 13:05:29 +0000 | [diff] [blame] | 162 | group_from_index(regexobject *re, PyObject *index) |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 163 | { |
| 164 | int i, a, b; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 165 | char *v; |
| 166 | |
| 167 | if (PyString_Check(index)) |
| 168 | if (re->re_groupindex == NULL || |
| 169 | !(index = PyDict_GetItem(re->re_groupindex, index))) |
| 170 | { |
| 171 | PyErr_SetString(RegexError, |
| 172 | "group() group name doesn't exist"); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 173 | return NULL; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 174 | } |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 175 | |
| 176 | i = PyInt_AsLong(index); |
| 177 | if (i == -1 && PyErr_Occurred()) |
| 178 | return NULL; |
| 179 | |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 180 | if (i < 0 || i >= RE_NREGS) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 181 | PyErr_SetString(RegexError, "group() index out of range"); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 182 | return NULL; |
| 183 | } |
| 184 | if (re->re_lastok == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 185 | PyErr_SetString(RegexError, |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 186 | "group() only valid after successful match/search"); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 187 | return NULL; |
| 188 | } |
| 189 | a = re->re_regs.start[i]; |
| 190 | b = re->re_regs.end[i]; |
| 191 | if (a < 0 || b < 0) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 192 | Py_INCREF(Py_None); |
| 193 | return Py_None; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 194 | } |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 195 | |
| 196 | if (!(v = PyString_AsString(re->re_lastok))) |
| 197 | return NULL; |
| 198 | |
| 199 | return PyString_FromStringAndSize(v+a, b-a); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 202 | |
| 203 | static PyObject * |
Peter Schneider-Kamp | 7d0c71a | 2000-07-10 13:05:29 +0000 | [diff] [blame] | 204 | regobj_group(regexobject *re, PyObject *args) |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 205 | { |
| 206 | int n = PyTuple_Size(args); |
| 207 | int i; |
| 208 | PyObject *res = NULL; |
| 209 | |
| 210 | if (n < 0) |
| 211 | return NULL; |
| 212 | if (n == 0) { |
| 213 | PyErr_SetString(PyExc_TypeError, "not enough arguments"); |
| 214 | return NULL; |
| 215 | } |
| 216 | if (n == 1) { |
| 217 | /* return value is a single string */ |
| 218 | PyObject *index = PyTuple_GetItem(args, 0); |
| 219 | if (!index) |
| 220 | return NULL; |
| 221 | |
| 222 | return group_from_index(re, index); |
| 223 | } |
| 224 | |
| 225 | /* return value is a tuple */ |
| 226 | if (!(res = PyTuple_New(n))) |
| 227 | return NULL; |
| 228 | |
| 229 | for (i = 0; i < n; i++) { |
| 230 | PyObject *index = PyTuple_GetItem(args, i); |
| 231 | PyObject *group = NULL; |
| 232 | |
| 233 | if (!index) |
| 234 | goto finally; |
| 235 | if (!(group = group_from_index(re, index))) |
| 236 | goto finally; |
| 237 | if (PyTuple_SetItem(res, i, group) < 0) |
| 238 | goto finally; |
| 239 | } |
| 240 | return res; |
| 241 | |
| 242 | finally: |
| 243 | Py_DECREF(res); |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 248 | static struct PyMethodDef reg_methods[] = { |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 249 | {"match", (PyCFunction)regobj_match, 1}, |
| 250 | {"search", (PyCFunction)regobj_search, 1}, |
| 251 | {"group", (PyCFunction)regobj_group, 1}, |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 252 | {NULL, NULL} /* sentinel */ |
| 253 | }; |
| 254 | |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 255 | |
| 256 | |
| 257 | static char* members[] = { |
| 258 | "last", "regs", "translate", |
| 259 | "groupindex", "realpat", "givenpat", |
| 260 | NULL |
| 261 | }; |
| 262 | |
| 263 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 264 | static PyObject * |
Peter Schneider-Kamp | 7d0c71a | 2000-07-10 13:05:29 +0000 | [diff] [blame] | 265 | regobj_getattr(regexobject *re, char *name) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 266 | { |
Guido van Rossum | b824fc6 | 1992-01-01 14:52:16 +0000 | [diff] [blame] | 267 | if (strcmp(name, "regs") == 0) { |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 268 | if (re->re_lastok == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 269 | Py_INCREF(Py_None); |
| 270 | return Py_None; |
Guido van Rossum | b824fc6 | 1992-01-01 14:52:16 +0000 | [diff] [blame] | 271 | } |
| 272 | return makeresult(&re->re_regs); |
| 273 | } |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 274 | if (strcmp(name, "last") == 0) { |
| 275 | if (re->re_lastok == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 276 | Py_INCREF(Py_None); |
| 277 | return Py_None; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 278 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 279 | Py_INCREF(re->re_lastok); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 280 | return re->re_lastok; |
| 281 | } |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 282 | if (strcmp(name, "translate") == 0) { |
| 283 | if (re->re_translate == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 284 | Py_INCREF(Py_None); |
| 285 | return Py_None; |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 286 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 287 | Py_INCREF(re->re_translate); |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 288 | return re->re_translate; |
| 289 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 290 | if (strcmp(name, "groupindex") == 0) { |
| 291 | if (re->re_groupindex == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 292 | Py_INCREF(Py_None); |
| 293 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 294 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 295 | Py_INCREF(re->re_groupindex); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 296 | return re->re_groupindex; |
| 297 | } |
| 298 | if (strcmp(name, "realpat") == 0) { |
| 299 | if (re->re_realpat == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 300 | Py_INCREF(Py_None); |
| 301 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 302 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 303 | Py_INCREF(re->re_realpat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 304 | return re->re_realpat; |
| 305 | } |
| 306 | if (strcmp(name, "givenpat") == 0) { |
| 307 | if (re->re_givenpat == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 308 | Py_INCREF(Py_None); |
| 309 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 310 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 311 | Py_INCREF(re->re_givenpat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 312 | return re->re_givenpat; |
| 313 | } |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 314 | if (strcmp(name, "__members__") == 0) { |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 315 | int i = 0; |
| 316 | PyObject *list = NULL; |
| 317 | |
| 318 | /* okay, so it's unlikely this list will change that often. |
| 319 | still, it's easier to change it in just one place. |
| 320 | */ |
| 321 | while (members[i]) |
| 322 | i++; |
| 323 | if (!(list = PyList_New(i))) |
| 324 | return NULL; |
| 325 | |
| 326 | i = 0; |
| 327 | while (members[i]) { |
| 328 | PyObject* v = PyString_FromString(members[i]); |
| 329 | if (!v || PyList_SetItem(list, i, v) < 0) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 330 | Py_DECREF(list); |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 331 | return NULL; |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 332 | } |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 333 | i++; |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 334 | } |
| 335 | return list; |
| 336 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 337 | return Py_FindMethod(reg_methods, (PyObject *)re, name); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 340 | static PyTypeObject Regextype = { |
| 341 | PyObject_HEAD_INIT(&PyType_Type) |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 342 | 0, /*ob_size*/ |
| 343 | "regex", /*tp_name*/ |
| 344 | sizeof(regexobject), /*tp_size*/ |
| 345 | 0, /*tp_itemsize*/ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 346 | /* methods */ |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 347 | (destructor)reg_dealloc, /*tp_dealloc*/ |
| 348 | 0, /*tp_print*/ |
| 349 | (getattrfunc)regobj_getattr, /*tp_getattr*/ |
| 350 | 0, /*tp_setattr*/ |
| 351 | 0, /*tp_compare*/ |
| 352 | 0, /*tp_repr*/ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 353 | }; |
| 354 | |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 355 | /* reference counting invariants: |
| 356 | pattern: borrowed |
| 357 | translate: borrowed |
| 358 | givenpat: borrowed |
| 359 | groupindex: transferred |
| 360 | */ |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 361 | static PyObject * |
Peter Schneider-Kamp | 7d0c71a | 2000-07-10 13:05:29 +0000 | [diff] [blame] | 362 | newregexobject(PyObject *pattern, PyObject *translate, PyObject *givenpat, PyObject *groupindex) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 363 | { |
| 364 | regexobject *re; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 365 | char *pat; |
| 366 | int size; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 367 | |
Guido van Rossum | 7e48898 | 1998-10-08 02:25:24 +0000 | [diff] [blame] | 368 | if (!PyArg_Parse(pattern, "t#", &pat, &size)) |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 369 | return NULL; |
| 370 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 371 | if (translate != NULL && PyString_Size(translate) != 256) { |
| 372 | PyErr_SetString(RegexError, |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 373 | "translation table must be 256 bytes"); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 374 | return NULL; |
| 375 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 376 | re = PyObject_New(regexobject, &Regextype); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 377 | if (re != NULL) { |
| 378 | char *error; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 379 | re->re_patbuf.buffer = NULL; |
| 380 | re->re_patbuf.allocated = 0; |
Guido van Rossum | ed2554a | 1997-08-18 15:31:24 +0000 | [diff] [blame] | 381 | re->re_patbuf.fastmap = (unsigned char *)re->re_fastmap; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 382 | if (translate) { |
Guido van Rossum | ed2554a | 1997-08-18 15:31:24 +0000 | [diff] [blame] | 383 | re->re_patbuf.translate = (unsigned char *)PyString_AsString(translate); |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 384 | if (!re->re_patbuf.translate) |
| 385 | goto finally; |
| 386 | Py_INCREF(translate); |
| 387 | } |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 388 | else |
| 389 | re->re_patbuf.translate = NULL; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 390 | re->re_translate = translate; |
| 391 | re->re_lastok = NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 392 | re->re_groupindex = groupindex; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 393 | Py_INCREF(pattern); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 394 | re->re_realpat = pattern; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 395 | Py_INCREF(givenpat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 396 | re->re_givenpat = givenpat; |
Guido van Rossum | d19c04a | 1997-09-03 00:47:36 +0000 | [diff] [blame] | 397 | error = _Py_re_compile_pattern((unsigned char *)pat, size, &re->re_patbuf); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 398 | if (error != NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 399 | PyErr_SetString(RegexError, error); |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 400 | goto finally; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 403 | return (PyObject *)re; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 404 | finally: |
| 405 | Py_DECREF(re); |
| 406 | return NULL; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 409 | static PyObject * |
Peter Schneider-Kamp | 7d0c71a | 2000-07-10 13:05:29 +0000 | [diff] [blame] | 410 | regex_compile(PyObject *self, PyObject *args) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 411 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 412 | PyObject *pat = NULL; |
| 413 | PyObject *tran = NULL; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 414 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 415 | if (!PyArg_ParseTuple(args, "S|S:compile", &pat, &tran)) |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 416 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 417 | return newregexobject(pat, tran, pat, NULL); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 420 | static PyObject * |
Peter Schneider-Kamp | 7d0c71a | 2000-07-10 13:05:29 +0000 | [diff] [blame] | 421 | symcomp(PyObject *pattern, PyObject *gdict) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 422 | { |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 423 | char *opat, *oend, *o, *n, *g, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 424 | int group_count = 0; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 425 | int sz; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 426 | int escaped = 0; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 427 | char name_buf[128]; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 428 | PyObject *npattern; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 429 | int require_escape = re_syntax & RE_NO_BK_PARENS ? 0 : 1; |
| 430 | |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 431 | if (!(opat = PyString_AsString(pattern))) |
| 432 | return NULL; |
| 433 | |
| 434 | if ((sz = PyString_Size(pattern)) < 0) |
| 435 | return NULL; |
| 436 | |
| 437 | oend = opat + sz; |
| 438 | o = opat; |
| 439 | |
Guido van Rossum | ab28c56 | 1996-06-11 18:33:14 +0000 | [diff] [blame] | 440 | if (oend == opat) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 441 | Py_INCREF(pattern); |
Guido van Rossum | ab28c56 | 1996-06-11 18:33:14 +0000 | [diff] [blame] | 442 | return pattern; |
| 443 | } |
| 444 | |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 445 | if (!(npattern = PyString_FromStringAndSize((char*)NULL, sz)) || |
| 446 | !(n = PyString_AsString(npattern))) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 447 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 448 | |
| 449 | while (o < oend) { |
| 450 | if (*o == '(' && escaped == require_escape) { |
| 451 | char *backtrack; |
| 452 | escaped = 0; |
| 453 | ++group_count; |
| 454 | *n++ = *o; |
| 455 | if (++o >= oend || *o != '<') |
| 456 | continue; |
| 457 | /* *o == '<' */ |
| 458 | if (o+1 < oend && *(o+1) == '>') |
| 459 | continue; |
| 460 | backtrack = o; |
| 461 | g = name_buf; |
| 462 | for (++o; o < oend;) { |
| 463 | if (*o == '>') { |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 464 | PyObject *group_name = NULL; |
| 465 | PyObject *group_index = NULL; |
| 466 | *g++ = '\0'; |
| 467 | group_name = PyString_FromString(name_buf); |
| 468 | group_index = PyInt_FromLong(group_count); |
| 469 | if (group_name == NULL || |
| 470 | group_index == NULL || |
| 471 | PyDict_SetItem(gdict, group_name, |
| 472 | group_index) != 0) |
| 473 | { |
| 474 | Py_XDECREF(group_name); |
| 475 | Py_XDECREF(group_index); |
| 476 | Py_XDECREF(npattern); |
| 477 | return NULL; |
| 478 | } |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 479 | Py_DECREF(group_name); |
| 480 | Py_DECREF(group_index); |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 481 | ++o; /* eat the '>' */ |
| 482 | break; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 483 | } |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 484 | if (!isalnum(Py_CHARMASK(*o)) && *o != '_') { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 485 | o = backtrack; |
| 486 | break; |
| 487 | } |
| 488 | *g++ = *o++; |
| 489 | } |
| 490 | } |
Guido van Rossum | 0cbaff4 | 1996-10-23 17:53:06 +0000 | [diff] [blame] | 491 | else if (*o == '[' && !escaped) { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 492 | *n++ = *o; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 493 | ++o; /* eat the char following '[' */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 494 | *n++ = *o; |
| 495 | while (o < oend && *o != ']') { |
| 496 | ++o; |
| 497 | *n++ = *o; |
| 498 | } |
| 499 | if (o < oend) |
| 500 | ++o; |
| 501 | } |
| 502 | else if (*o == '\\') { |
| 503 | escaped = 1; |
| 504 | *n++ = *o; |
| 505 | ++o; |
| 506 | } |
| 507 | else { |
| 508 | escaped = 0; |
| 509 | *n++ = *o; |
| 510 | ++o; |
| 511 | } |
| 512 | } |
| 513 | |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 514 | if (!(v = PyString_AsString(npattern))) { |
| 515 | Py_DECREF(npattern); |
| 516 | return NULL; |
| 517 | } |
| 518 | /* _PyString_Resize() decrements npattern on failure */ |
| 519 | if (_PyString_Resize(&npattern, n - v) == 0) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 520 | return npattern; |
| 521 | else { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 522 | return NULL; |
| 523 | } |
| 524 | |
| 525 | } |
| 526 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 527 | static PyObject * |
Peter Schneider-Kamp | 7d0c71a | 2000-07-10 13:05:29 +0000 | [diff] [blame] | 528 | regex_symcomp(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 529 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 530 | PyObject *pattern; |
| 531 | PyObject *tran = NULL; |
| 532 | PyObject *gdict = NULL; |
| 533 | PyObject *npattern; |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 534 | PyObject *retval = NULL; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 535 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 536 | if (!PyArg_ParseTuple(args, "S|S:symcomp", &pattern, &tran)) |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 537 | return NULL; |
| 538 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 539 | gdict = PyDict_New(); |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 540 | if (gdict == NULL || (npattern = symcomp(pattern, gdict)) == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 541 | Py_DECREF(gdict); |
| 542 | Py_DECREF(pattern); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 543 | return NULL; |
| 544 | } |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 545 | retval = newregexobject(npattern, tran, pattern, gdict); |
| 546 | Py_DECREF(npattern); |
| 547 | return retval; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 551 | static PyObject *cache_pat; |
| 552 | static PyObject *cache_prog; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 553 | |
| 554 | static int |
Peter Schneider-Kamp | 7d0c71a | 2000-07-10 13:05:29 +0000 | [diff] [blame] | 555 | update_cache(PyObject *pat) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 556 | { |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 557 | PyObject *tuple = Py_BuildValue("(O)", pat); |
| 558 | int status = 0; |
| 559 | |
| 560 | if (!tuple) |
| 561 | return -1; |
| 562 | |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 563 | if (pat != cache_pat) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 564 | Py_XDECREF(cache_pat); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 565 | cache_pat = NULL; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 566 | Py_XDECREF(cache_prog); |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 567 | cache_prog = regex_compile((PyObject *)NULL, tuple); |
| 568 | if (cache_prog == NULL) { |
| 569 | status = -1; |
| 570 | goto finally; |
| 571 | } |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 572 | cache_pat = pat; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 573 | Py_INCREF(cache_pat); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 574 | } |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 575 | finally: |
| 576 | Py_DECREF(tuple); |
| 577 | return status; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 580 | static PyObject * |
Peter Schneider-Kamp | 7d0c71a | 2000-07-10 13:05:29 +0000 | [diff] [blame] | 581 | regex_match(PyObject *self, PyObject *args) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 582 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 583 | PyObject *pat, *string; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 584 | PyObject *tuple, *v; |
| 585 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 586 | if (!PyArg_Parse(args, "(SS)", &pat, &string)) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 587 | return NULL; |
| 588 | if (update_cache(pat) < 0) |
| 589 | return NULL; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 590 | |
| 591 | if (!(tuple = Py_BuildValue("(S)", string))) |
| 592 | return NULL; |
| 593 | v = regobj_match((regexobject *)cache_prog, tuple); |
| 594 | Py_DECREF(tuple); |
| 595 | return v; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 598 | static PyObject * |
Peter Schneider-Kamp | 7d0c71a | 2000-07-10 13:05:29 +0000 | [diff] [blame] | 599 | regex_search(PyObject *self, PyObject *args) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 600 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 601 | PyObject *pat, *string; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 602 | PyObject *tuple, *v; |
| 603 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 604 | if (!PyArg_Parse(args, "(SS)", &pat, &string)) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 605 | return NULL; |
| 606 | if (update_cache(pat) < 0) |
| 607 | return NULL; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 608 | |
| 609 | if (!(tuple = Py_BuildValue("(S)", string))) |
| 610 | return NULL; |
| 611 | v = regobj_search((regexobject *)cache_prog, tuple); |
| 612 | Py_DECREF(tuple); |
| 613 | return v; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 616 | static PyObject * |
Peter Schneider-Kamp | 7d0c71a | 2000-07-10 13:05:29 +0000 | [diff] [blame] | 617 | regex_set_syntax(PyObject *self, PyObject *args) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 618 | { |
| 619 | int syntax; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 620 | if (!PyArg_Parse(args, "i", &syntax)) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 621 | return NULL; |
| 622 | syntax = re_set_syntax(syntax); |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 623 | /* wipe the global pattern cache */ |
| 624 | Py_XDECREF(cache_pat); |
| 625 | cache_pat = NULL; |
| 626 | Py_XDECREF(cache_prog); |
| 627 | cache_prog = NULL; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 628 | return PyInt_FromLong((long)syntax); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Barry Warsaw | 909d7c3 | 1997-02-18 18:48:50 +0000 | [diff] [blame] | 631 | static PyObject * |
Peter Schneider-Kamp | 7d0c71a | 2000-07-10 13:05:29 +0000 | [diff] [blame] | 632 | regex_get_syntax(PyObject *self, PyObject *args) |
Barry Warsaw | 909d7c3 | 1997-02-18 18:48:50 +0000 | [diff] [blame] | 633 | { |
| 634 | if (!PyArg_Parse(args, "")) |
| 635 | return NULL; |
| 636 | return PyInt_FromLong((long)re_syntax); |
| 637 | } |
| 638 | |
| 639 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 640 | static struct PyMethodDef regex_global_methods[] = { |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 641 | {"compile", regex_compile, 1}, |
| 642 | {"symcomp", regex_symcomp, 1}, |
Guido van Rossum | 295d171 | 1995-02-19 15:55:19 +0000 | [diff] [blame] | 643 | {"match", regex_match, 0}, |
| 644 | {"search", regex_search, 0}, |
| 645 | {"set_syntax", regex_set_syntax, 0}, |
Barry Warsaw | 909d7c3 | 1997-02-18 18:48:50 +0000 | [diff] [blame] | 646 | {"get_syntax", regex_get_syntax, 0}, |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 647 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 648 | }; |
| 649 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 650 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 651 | initregex(void) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 652 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 653 | PyObject *m, *d, *v; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 654 | int i; |
| 655 | char *s; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 656 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 657 | m = Py_InitModule("regex", regex_global_methods); |
| 658 | d = PyModule_GetDict(m); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 659 | |
| 660 | /* Initialize regex.error exception */ |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 661 | v = RegexError = PyErr_NewException("regex.error", NULL, NULL); |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 662 | if (v == NULL || PyDict_SetItemString(d, "error", v) != 0) |
| 663 | goto finally; |
| 664 | |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 665 | /* Initialize regex.casefold constant */ |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 666 | if (!(v = PyString_FromStringAndSize((char *)NULL, 256))) |
| 667 | goto finally; |
| 668 | |
| 669 | if (!(s = PyString_AsString(v))) |
| 670 | goto finally; |
| 671 | |
| 672 | for (i = 0; i < 256; i++) { |
| 673 | if (isupper(i)) |
| 674 | s[i] = tolower(i); |
| 675 | else |
| 676 | s[i] = i; |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 677 | } |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 678 | if (PyDict_SetItemString(d, "casefold", v) < 0) |
| 679 | goto finally; |
| 680 | Py_DECREF(v); |
| 681 | |
| 682 | if (!PyErr_Occurred()) |
| 683 | return; |
| 684 | finally: |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 685 | /* Nothing */ ; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 686 | } |