annotate Modules/regexmodule.c @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 91dd223829c6
children 40631ae74b17
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /*
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 XXX support range parameter on search
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 XXX support mstop parameter on search
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 /* Regular expression objects */
615
40a0ec994c54 Use Tatu Ylonen's copyleft-free reimplementation of
Guido van Rossum <guido@python.org>
parents: 551
diff changeset
8 /* This uses Tatu Ylonen's copyleft-free reimplementation of
40a0ec994c54 Use Tatu Ylonen's copyleft-free reimplementation of
Guido van Rossum <guido@python.org>
parents: 551
diff changeset
9 GNU regular expressions */
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
11 #include "Python.h"
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
4600
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
13 #include <ctype.h>
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
14
615
40a0ec994c54 Use Tatu Ylonen's copyleft-free reimplementation of
Guido van Rossum <guido@python.org>
parents: 551
diff changeset
15 #include "regexpr.h"
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
17 static PyObject *RegexError; /* Exception */
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 typedef struct {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
20 PyObject_HEAD
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 struct re_pattern_buffer re_patbuf; /* The compiled expression */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 struct re_registers re_regs; /* The registers from the last match */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 char re_fastmap[256]; /* Storage for fastmap */
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
24 PyObject *re_translate; /* String object for translate table */
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
25 PyObject *re_lastok; /* String object last matched/searched */
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
26 PyObject *re_groupindex; /* Group name to index dictionary */
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
27 PyObject *re_givenpat; /* Pattern with symbolic groups */
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
28 PyObject *re_realpat; /* Pattern without symbolic groups */
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 } regexobject;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 /* Regex object methods */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 static void
13504
8256d6198ec2 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
34 reg_dealloc(regexobject *re)
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
36 if (re->re_patbuf.buffer)
13557
c396b602617d Fix mixed mallocs: re->re_patbuf.buffer is allocated with std malloc().
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13504
diff changeset
37 free(re->re_patbuf.buffer);
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
38 Py_XDECREF(re->re_translate);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
39 Py_XDECREF(re->re_lastok);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
40 Py_XDECREF(re->re_groupindex);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
41 Py_XDECREF(re->re_givenpat);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
42 Py_XDECREF(re->re_realpat);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
43 PyObject_Del(re);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
46 static PyObject *
13504
8256d6198ec2 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
47 makeresult(struct re_registers *regs)
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 {
4372
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
49 PyObject *v;
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
50 int i;
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
51 static PyObject *filler = NULL;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
52
4372
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
53 if (filler == NULL) {
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
54 filler = Py_BuildValue("(ii)", -1, -1);
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
55 if (filler == NULL)
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
56 return NULL;
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
57 }
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
58 v = PyTuple_New(RE_NREGS);
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
59 if (v == NULL)
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
60 return NULL;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
61
4372
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
62 for (i = 0; i < RE_NREGS; i++) {
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
63 int lo = regs->start[i];
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
64 int hi = regs->end[i];
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
65 PyObject *w;
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
66 if (lo == -1 && hi == -1) {
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
67 w = filler;
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
68 Py_INCREF(w);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 }
4372
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
70 else
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
71 w = Py_BuildValue("(ii)", lo, hi);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
72 if (w == NULL || PyTuple_SetItem(v, i, w) < 0) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
73 Py_DECREF(v);
4372
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
74 return NULL;
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
75 }
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 return v;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
80 static PyObject *
13504
8256d6198ec2 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
81 regobj_match(regexobject *re, PyObject *args)
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 {
5481
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
83 PyObject *argstring;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 char *buffer;
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
85 int size;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
86 int offset = 0;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 int result;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
88
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
89 if (!PyArg_ParseTuple(args, "O|i:match", &argstring, &offset))
5481
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
90 return NULL;
9307
928ea5a409e5 Use the t# format where appropriate. Greg Stein.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
91 if (!PyArg_Parse(argstring, "t#", &buffer, &size))
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
92 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
93
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
94 if (offset < 0 || offset > size) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
95 PyErr_SetString(RegexError, "match offset out of range");
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
96 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
97 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
98 Py_XDECREF(re->re_lastok);
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
99 re->re_lastok = NULL;
6094
a22fca7a5c5a Casts by Jack to shut up the Mac compiler.
Guido van Rossum <guido@python.org>
parents: 6014
diff changeset
100 result = _Py_re_match(&re->re_patbuf, (unsigned char *)buffer, size, offset,
6014
2f36e0bb37c3 Use _Py_re_match/search instead of re_match/search; these may become
Guido van Rossum <guido@python.org>
parents: 6004
diff changeset
101 &re->re_regs);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 if (result < -1) {
6004
3f59b34eb90f 1.5a3 prerelease 1 from AMK
Guido van Rossum <guido@python.org>
parents: 5481
diff changeset
103 /* Serious failure of some sort; if re_match didn't
3f59b34eb90f 1.5a3 prerelease 1 from AMK
Guido van Rossum <guido@python.org>
parents: 5481
diff changeset
104 set an exception, raise a generic error */
3f59b34eb90f 1.5a3 prerelease 1 from AMK
Guido van Rossum <guido@python.org>
parents: 5481
diff changeset
105 if (!PyErr_Occurred())
3f59b34eb90f 1.5a3 prerelease 1 from AMK
Guido van Rossum <guido@python.org>
parents: 5481
diff changeset
106 PyErr_SetString(RegexError, "match failure");
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
109 if (result >= 0) {
5481
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
110 Py_INCREF(argstring);
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
111 re->re_lastok = argstring;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
112 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
113 return PyInt_FromLong((long)result); /* Length of the match or -1 */
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 }
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
115
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
116 static PyObject *
13504
8256d6198ec2 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
117 regobj_search(regexobject *re, PyObject *args)
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 {
5481
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
119 PyObject *argstring;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 char *buffer;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 int size;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
122 int offset = 0;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 int range;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 int result;
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
125
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
126 if (!PyArg_ParseTuple(args, "O|i:search", &argstring, &offset))
5481
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
127 return NULL;
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
128 if (!PyArg_Parse(argstring, "t#:search", &buffer, &size))
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
129 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
130
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
131 if (offset < 0 || offset > size) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
132 PyErr_SetString(RegexError, "search offset out of range");
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
133 return NULL;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 }
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
135 /* NB: In Emacs 18.57, the documentation for re_search[_2] and
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
136 the implementation don't match: the documentation states that
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
137 |range| positions are tried, while the code tries |range|+1
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
138 positions. It seems more productive to believe the code! */
628
d420af9c1b75 Make "range" one smaller (discrepancy between doc and source for regex.c!).
Guido van Rossum <guido@python.org>
parents: 615
diff changeset
139 range = size - offset;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
140 Py_XDECREF(re->re_lastok);
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
141 re->re_lastok = NULL;
6094
a22fca7a5c5a Casts by Jack to shut up the Mac compiler.
Guido van Rossum <guido@python.org>
parents: 6014
diff changeset
142 result = _Py_re_search(&re->re_patbuf, (unsigned char *)buffer, size, offset, range,
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 &re->re_regs);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 if (result < -1) {
6004
3f59b34eb90f 1.5a3 prerelease 1 from AMK
Guido van Rossum <guido@python.org>
parents: 5481
diff changeset
145 /* Serious failure of some sort; if re_match didn't
3f59b34eb90f 1.5a3 prerelease 1 from AMK
Guido van Rossum <guido@python.org>
parents: 5481
diff changeset
146 set an exception, raise a generic error */
3f59b34eb90f 1.5a3 prerelease 1 from AMK
Guido van Rossum <guido@python.org>
parents: 5481
diff changeset
147 if (!PyErr_Occurred())
3f59b34eb90f 1.5a3 prerelease 1 from AMK
Guido van Rossum <guido@python.org>
parents: 5481
diff changeset
148 PyErr_SetString(RegexError, "match failure");
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
151 if (result >= 0) {
5481
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
152 Py_INCREF(argstring);
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
153 re->re_lastok = argstring;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
154 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
155 return PyInt_FromLong((long)result); /* Position of the match or -1 */
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
158 /* get the group from the regex where index can be a string (group name) or
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
159 an integer index [0 .. 99]
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
160 */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
161 static PyObject*
13504
8256d6198ec2 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
162 group_from_index(regexobject *re, PyObject *index)
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
163 {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
164 int i, a, b;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
165 char *v;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
166
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
167 if (PyString_Check(index))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
168 if (re->re_groupindex == NULL ||
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
169 !(index = PyDict_GetItem(re->re_groupindex, index)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
170 {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
171 PyErr_SetString(RegexError,
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
172 "group() group name doesn't exist");
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
173 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
174 }
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
175
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
176 i = PyInt_AsLong(index);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
177 if (i == -1 && PyErr_Occurred())
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
178 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
179
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
180 if (i < 0 || i >= RE_NREGS) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
181 PyErr_SetString(RegexError, "group() index out of range");
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
182 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
183 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
184 if (re->re_lastok == NULL) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
185 PyErr_SetString(RegexError,
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
186 "group() only valid after successful match/search");
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
187 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
188 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
189 a = re->re_regs.start[i];
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
190 b = re->re_regs.end[i];
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
191 if (a < 0 || b < 0) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
192 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
193 return Py_None;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
194 }
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
195
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
196 if (!(v = PyString_AsString(re->re_lastok)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
197 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
198
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
199 return PyString_FromStringAndSize(v+a, b-a);
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
200 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
201
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
202
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
203 static PyObject *
13504
8256d6198ec2 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
204 regobj_group(regexobject *re, PyObject *args)
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
205 {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
206 int n = PyTuple_Size(args);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
207 int i;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
208 PyObject *res = NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
209
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
210 if (n < 0)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
211 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
212 if (n == 0) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
213 PyErr_SetString(PyExc_TypeError, "not enough arguments");
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
214 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
215 }
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
216 if (n == 1) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
217 /* return value is a single string */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
218 PyObject *index = PyTuple_GetItem(args, 0);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
219 if (!index)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
220 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
221
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
222 return group_from_index(re, index);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
223 }
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
224
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
225 /* return value is a tuple */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
226 if (!(res = PyTuple_New(n)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
227 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
228
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
229 for (i = 0; i < n; i++) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
230 PyObject *index = PyTuple_GetItem(args, i);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
231 PyObject *group = NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
232
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
233 if (!index)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
234 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
235 if (!(group = group_from_index(re, index)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
236 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
237 if (PyTuple_SetItem(res, i, group) < 0)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
238 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
239 }
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
240 return res;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
241
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
242 finally:
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
243 Py_DECREF(res);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
244 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
245 }
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
246
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
247
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
248 static struct PyMethodDef reg_methods[] = {
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
249 {"match", (PyCFunction)regobj_match, 1},
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
250 {"search", (PyCFunction)regobj_search, 1},
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
251 {"group", (PyCFunction)regobj_group, 1},
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 {NULL, NULL} /* sentinel */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 };
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
254
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
255
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
256
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
257 static char* members[] = {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
258 "last", "regs", "translate",
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
259 "groupindex", "realpat", "givenpat",
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
260 NULL
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
261 };
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
262
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
263
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
264 static PyObject *
13504
8256d6198ec2 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
265 regobj_getattr(regexobject *re, char *name)
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 {
551
5a61f827f47c 'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents: 536
diff changeset
267 if (strcmp(name, "regs") == 0) {
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
268 if (re->re_lastok == NULL) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
269 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
270 return Py_None;
551
5a61f827f47c 'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents: 536
diff changeset
271 }
5a61f827f47c 'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents: 536
diff changeset
272 return makeresult(&re->re_regs);
5a61f827f47c 'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents: 536
diff changeset
273 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
274 if (strcmp(name, "last") == 0) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
275 if (re->re_lastok == NULL) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
276 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
277 return Py_None;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
278 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
279 Py_INCREF(re->re_lastok);
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
280 return re->re_lastok;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
281 }
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
282 if (strcmp(name, "translate") == 0) {
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
283 if (re->re_translate == NULL) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
284 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
285 return Py_None;
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
286 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
287 Py_INCREF(re->re_translate);
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
288 return re->re_translate;
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
289 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
290 if (strcmp(name, "groupindex") == 0) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
291 if (re->re_groupindex == NULL) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
292 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
293 return Py_None;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
294 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
295 Py_INCREF(re->re_groupindex);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
296 return re->re_groupindex;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
297 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
298 if (strcmp(name, "realpat") == 0) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
299 if (re->re_realpat == NULL) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
300 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
301 return Py_None;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
302 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
303 Py_INCREF(re->re_realpat);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
304 return re->re_realpat;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
305 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
306 if (strcmp(name, "givenpat") == 0) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
307 if (re->re_givenpat == NULL) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
308 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
309 return Py_None;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
310 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
311 Py_INCREF(re->re_givenpat);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
312 return re->re_givenpat;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
313 }
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
314 if (strcmp(name, "__members__") == 0) {
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
315 int i = 0;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
316 PyObject *list = NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
317
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
318 /* okay, so it's unlikely this list will change that often.
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
319 still, it's easier to change it in just one place.
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
320 */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
321 while (members[i])
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
322 i++;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
323 if (!(list = PyList_New(i)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
324 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
325
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
326 i = 0;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
327 while (members[i]) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
328 PyObject* v = PyString_FromString(members[i]);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
329 if (!v || PyList_SetItem(list, i, v) < 0) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
330 Py_DECREF(list);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
331 return NULL;
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
332 }
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
333 i++;
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
334 }
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
335 return list;
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
336 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
337 return Py_FindMethod(reg_methods, (PyObject *)re, name);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
339
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
340 static PyTypeObject Regextype = {
16106
28c414dcb3e1 SF Patch #103185, by jlt63: Some more standard modules cleanup for Cygwin
Guido van Rossum <guido@python.org>
parents: 15624
diff changeset
341 PyObject_HEAD_INIT(NULL)
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
342 0, /*ob_size*/
21313
91dd223829c6 Patch supplied by Burton Radons for his own SF bug #487390: Modifying
Guido van Rossum <guido@python.org>
parents: 18441
diff changeset
343 "regex.regex", /*tp_name*/
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
344 sizeof(regexobject), /*tp_size*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
345 0, /*tp_itemsize*/
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 /* methods */
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
347 (destructor)reg_dealloc, /*tp_dealloc*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
348 0, /*tp_print*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
349 (getattrfunc)regobj_getattr, /*tp_getattr*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
350 0, /*tp_setattr*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
351 0, /*tp_compare*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
352 0, /*tp_repr*/
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 };
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
354
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
355 /* reference counting invariants:
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
356 pattern: borrowed
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
357 translate: borrowed
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
358 givenpat: borrowed
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
359 groupindex: transferred
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
360 */
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
361 static PyObject *
13504
8256d6198ec2 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
362 newregexobject(PyObject *pattern, PyObject *translate, PyObject *givenpat, PyObject *groupindex)
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 regexobject *re;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
365 char *pat;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
366 int size;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
367
9307
928ea5a409e5 Use the t# format where appropriate. Greg Stein.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
368 if (!PyArg_Parse(pattern, "t#", &pat, &size))
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
369 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
370
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
371 if (translate != NULL && PyString_Size(translate) != 256) {
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
372 PyErr_SetString(RegexError,
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
373 "translation table must be 256 bytes");
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
374 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
375 }
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
376 re = PyObject_New(regexobject, &Regextype);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 if (re != NULL) {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 char *error;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 re->re_patbuf.buffer = NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 re->re_patbuf.allocated = 0;
6094
a22fca7a5c5a Casts by Jack to shut up the Mac compiler.
Guido van Rossum <guido@python.org>
parents: 6014
diff changeset
381 re->re_patbuf.fastmap = (unsigned char *)re->re_fastmap;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
382 if (translate) {
6094
a22fca7a5c5a Casts by Jack to shut up the Mac compiler.
Guido van Rossum <guido@python.org>
parents: 6014
diff changeset
383 re->re_patbuf.translate = (unsigned char *)PyString_AsString(translate);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
384 if (!re->re_patbuf.translate)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
385 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
386 Py_INCREF(translate);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
387 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
388 else
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
389 re->re_patbuf.translate = NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
390 re->re_translate = translate;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
391 re->re_lastok = NULL;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
392 re->re_groupindex = groupindex;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
393 Py_INCREF(pattern);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
394 re->re_realpat = pattern;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
395 Py_INCREF(givenpat);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
396 re->re_givenpat = givenpat;
6220
ed4924f3b2a5 Change [_Py_]re_compile_pattern() to return a char*.
Guido van Rossum <guido@python.org>
parents: 6094
diff changeset
397 error = _Py_re_compile_pattern((unsigned char *)pat, size, &re->re_patbuf);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 if (error != NULL) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
399 PyErr_SetString(RegexError, error);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
400 goto finally;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
403 return (PyObject *)re;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
404 finally:
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
405 Py_DECREF(re);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
406 return NULL;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
408
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
409 static PyObject *
13504
8256d6198ec2 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
410 regex_compile(PyObject *self, PyObject *args)
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
412 PyObject *pat = NULL;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
413 PyObject *tran = NULL;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
414
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
415 if (!PyArg_ParseTuple(args, "S|S:compile", &pat, &tran))
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
416 return NULL;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
417 return newregexobject(pat, tran, pat, NULL);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
419
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
420 static PyObject *
13504
8256d6198ec2 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
421 symcomp(PyObject *pattern, PyObject *gdict)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
422 {
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
423 char *opat, *oend, *o, *n, *g, *v;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
424 int group_count = 0;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
425 int sz;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
426 int escaped = 0;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
427 char name_buf[128];
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
428 PyObject *npattern;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
429 int require_escape = re_syntax & RE_NO_BK_PARENS ? 0 : 1;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
430
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
431 if (!(opat = PyString_AsString(pattern)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
432 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
433
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
434 if ((sz = PyString_Size(pattern)) < 0)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
435 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
436
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
437 oend = opat + sz;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
438 o = opat;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
439
3664
ad03e21fda54 Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
440 if (oend == opat) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
441 Py_INCREF(pattern);
3664
ad03e21fda54 Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
442 return pattern;
ad03e21fda54 Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
443 }
ad03e21fda54 Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
444
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
445 if (!(npattern = PyString_FromStringAndSize((char*)NULL, sz)) ||
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
446 !(n = PyString_AsString(npattern)))
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
447 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
448
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
449 while (o < oend) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
450 if (*o == '(' && escaped == require_escape) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
451 char *backtrack;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
452 escaped = 0;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
453 ++group_count;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
454 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
455 if (++o >= oend || *o != '<')
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
456 continue;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
457 /* *o == '<' */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
458 if (o+1 < oend && *(o+1) == '>')
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
459 continue;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
460 backtrack = o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
461 g = name_buf;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
462 for (++o; o < oend;) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
463 if (*o == '>') {
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
464 PyObject *group_name = NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
465 PyObject *group_index = NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
466 *g++ = '\0';
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
467 group_name = PyString_FromString(name_buf);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
468 group_index = PyInt_FromLong(group_count);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
469 if (group_name == NULL ||
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
470 group_index == NULL ||
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
471 PyDict_SetItem(gdict, group_name,
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
472 group_index) != 0)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
473 {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
474 Py_XDECREF(group_name);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
475 Py_XDECREF(group_index);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
476 Py_XDECREF(npattern);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
477 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
478 }
4917
1bcd4972da7e Nailed a couple of memory leaks, caught by Purify.
Barry Warsaw <barry@python.org>
parents: 4808
diff changeset
479 Py_DECREF(group_name);
1bcd4972da7e Nailed a couple of memory leaks, caught by Purify.
Barry Warsaw <barry@python.org>
parents: 4808
diff changeset
480 Py_DECREF(group_index);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
481 ++o; /* eat the '>' */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
482 break;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
483 }
2310
07913fb8f92f use Py_CHARMASK
Guido van Rossum <guido@python.org>
parents: 2234
diff changeset
484 if (!isalnum(Py_CHARMASK(*o)) && *o != '_') {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
485 o = backtrack;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
486 break;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
487 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
488 *g++ = *o++;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
489 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
490 }
4478
267ae58232d9 Fix case where you start a new group immediately after a label.
Guido van Rossum <guido@python.org>
parents: 4372
diff changeset
491 else if (*o == '[' && !escaped) {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
492 *n++ = *o;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
493 ++o; /* eat the char following '[' */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
494 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
495 while (o < oend && *o != ']') {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
496 ++o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
497 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
498 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
499 if (o < oend)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
500 ++o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
501 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
502 else if (*o == '\\') {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
503 escaped = 1;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
504 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
505 ++o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
506 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
507 else {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
508 escaped = 0;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
509 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
510 ++o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
511 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
512 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
513
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
514 if (!(v = PyString_AsString(npattern))) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
515 Py_DECREF(npattern);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
516 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
517 }
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
518 /* _PyString_Resize() decrements npattern on failure */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
519 if (_PyString_Resize(&npattern, n - v) == 0)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
520 return npattern;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
521 else {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
522 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
523 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
524
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
525 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
526
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
527 static PyObject *
13504
8256d6198ec2 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
528 regex_symcomp(PyObject *self, PyObject *args)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
529 {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
530 PyObject *pattern;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
531 PyObject *tran = NULL;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
532 PyObject *gdict = NULL;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
533 PyObject *npattern;
4917
1bcd4972da7e Nailed a couple of memory leaks, caught by Purify.
Barry Warsaw <barry@python.org>
parents: 4808
diff changeset
534 PyObject *retval = NULL;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
535
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
536 if (!PyArg_ParseTuple(args, "S|S:symcomp", &pattern, &tran))
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
537 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
538
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
539 gdict = PyDict_New();
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
540 if (gdict == NULL || (npattern = symcomp(pattern, gdict)) == NULL) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
541 Py_DECREF(gdict);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
542 Py_DECREF(pattern);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
543 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
544 }
4917
1bcd4972da7e Nailed a couple of memory leaks, caught by Purify.
Barry Warsaw <barry@python.org>
parents: 4808
diff changeset
545 retval = newregexobject(npattern, tran, pattern, gdict);
1bcd4972da7e Nailed a couple of memory leaks, caught by Purify.
Barry Warsaw <barry@python.org>
parents: 4808
diff changeset
546 Py_DECREF(npattern);
1bcd4972da7e Nailed a couple of memory leaks, caught by Purify.
Barry Warsaw <barry@python.org>
parents: 4808
diff changeset
547 return retval;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
548 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
549
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
550
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
551 static PyObject *cache_pat;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
552 static PyObject *cache_prog;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
553
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 static int
13504
8256d6198ec2 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
555 update_cache(PyObject *pat)
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
556 {
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
557 PyObject *tuple = Py_BuildValue("(O)", pat);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
558 int status = 0;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
559
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
560 if (!tuple)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
561 return -1;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
562
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
563 if (pat != cache_pat) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
564 Py_XDECREF(cache_pat);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
565 cache_pat = NULL;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
566 Py_XDECREF(cache_prog);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
567 cache_prog = regex_compile((PyObject *)NULL, tuple);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
568 if (cache_prog == NULL) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
569 status = -1;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
570 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
571 }
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
572 cache_pat = pat;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
573 Py_INCREF(cache_pat);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 }
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
575 finally:
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
576 Py_DECREF(tuple);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
577 return status;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
579
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
580 static PyObject *
13504
8256d6198ec2 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
581 regex_match(PyObject *self, PyObject *args)
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
582 {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
583 PyObject *pat, *string;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
584 PyObject *tuple, *v;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
585
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
586 if (!PyArg_Parse(args, "(SS)", &pat, &string))
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
588 if (update_cache(pat) < 0)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
589 return NULL;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
590
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
591 if (!(tuple = Py_BuildValue("(S)", string)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
592 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
593 v = regobj_match((regexobject *)cache_prog, tuple);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
594 Py_DECREF(tuple);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
595 return v;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
597
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
598 static PyObject *
13504
8256d6198ec2 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
599 regex_search(PyObject *self, PyObject *args)
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
601 PyObject *pat, *string;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
602 PyObject *tuple, *v;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
603
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
604 if (!PyArg_Parse(args, "(SS)", &pat, &string))
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 if (update_cache(pat) < 0)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
607 return NULL;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
608
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
609 if (!(tuple = Py_BuildValue("(S)", string)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
610 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
611 v = regobj_search((regexobject *)cache_prog, tuple);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
612 Py_DECREF(tuple);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
613 return v;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
615
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
616 static PyObject *
13504
8256d6198ec2 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
617 regex_set_syntax(PyObject *self, PyObject *args)
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
618 {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
619 int syntax;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
620 if (!PyArg_Parse(args, "i", &syntax))
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
622 syntax = re_set_syntax(syntax);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
623 /* wipe the global pattern cache */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
624 Py_XDECREF(cache_pat);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
625 cache_pat = NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
626 Py_XDECREF(cache_prog);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
627 cache_prog = NULL;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
628 return PyInt_FromLong((long)syntax);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
629 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
630
5129
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
631 static PyObject *
13504
8256d6198ec2 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
632 regex_get_syntax(PyObject *self, PyObject *args)
5129
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
633 {
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
634 if (!PyArg_Parse(args, ""))
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
635 return NULL;
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
636 return PyInt_FromLong((long)re_syntax);
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
637 }
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
638
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
639
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
640 static struct PyMethodDef regex_global_methods[] = {
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
641 {"compile", regex_compile, 1},
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
642 {"symcomp", regex_symcomp, 1},
2397
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2310
diff changeset
643 {"match", regex_match, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2310
diff changeset
644 {"search", regex_search, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2310
diff changeset
645 {"set_syntax", regex_set_syntax, 0},
5129
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
646 {"get_syntax", regex_get_syntax, 0},
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
647 {NULL, NULL} /* sentinel */
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
648 };
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
649
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 9307
diff changeset
650 DL_EXPORT(void)
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13557
diff changeset
651 initregex(void)
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
652 {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
653 PyObject *m, *d, *v;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
654 int i;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
655 char *s;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
656
16106
28c414dcb3e1 SF Patch #103185, by jlt63: Some more standard modules cleanup for Cygwin
Guido van Rossum <guido@python.org>
parents: 15624
diff changeset
657 /* Initialize object type */
28c414dcb3e1 SF Patch #103185, by jlt63: Some more standard modules cleanup for Cygwin
Guido van Rossum <guido@python.org>
parents: 15624
diff changeset
658 Regextype.ob_type = &PyType_Type;
28c414dcb3e1 SF Patch #103185, by jlt63: Some more standard modules cleanup for Cygwin
Guido van Rossum <guido@python.org>
parents: 15624
diff changeset
659
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
660 m = Py_InitModule("regex", regex_global_methods);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
661 d = PyModule_GetDict(m);
15624
50de3b198a62 Adding a warning about the regex module. This is the first official
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
662
18435
eb04a0564a81 initregex(): Check return value of PyErr_Warn() and propagate the exception
Thomas Wouters <thomas@python.org>
parents: 16106
diff changeset
663 if (PyErr_Warn(PyExc_DeprecationWarning,
eb04a0564a81 initregex(): Check return value of PyErr_Warn() and propagate the exception
Thomas Wouters <thomas@python.org>
parents: 16106
diff changeset
664 "the regex module is deprecated; "
eb04a0564a81 initregex(): Check return value of PyErr_Warn() and propagate the exception
Thomas Wouters <thomas@python.org>
parents: 16106
diff changeset
665 "please use the re module") < 0)
18441
65c9287abf85 initregex(): this function is declared void, so the recent change to
Tim Peters <tim.peters@gmail.com>
parents: 18435
diff changeset
666 return;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
667
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
668 /* Initialize regex.error exception */
6367
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 6220
diff changeset
669 v = RegexError = PyErr_NewException("regex.error", NULL, NULL);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
670 if (v == NULL || PyDict_SetItemString(d, "error", v) != 0)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
671 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
672
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
673 /* Initialize regex.casefold constant */
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
674 if (!(v = PyString_FromStringAndSize((char *)NULL, 256)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
675 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
676
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
677 if (!(s = PyString_AsString(v)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
678 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
679
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
680 for (i = 0; i < 256; i++) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
681 if (isupper(i))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
682 s[i] = tolower(i);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
683 else
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
684 s[i] = i;
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
685 }
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
686 if (PyDict_SetItemString(d, "casefold", v) < 0)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
687 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
688 Py_DECREF(v);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
689
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
690 if (!PyErr_Occurred())
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
691 return;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
692 finally:
6367
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 6220
diff changeset
693 /* Nothing */ ;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
694 }