regexmodule.c 15.1 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1 2 3 4 5 6 7
/*
XXX support range parameter on search
XXX support mstop parameter on search
*/


/* Regular expression objects */
8 9
/* This uses Tatu Ylonen's copyleft-free reimplementation of
   GNU regular expressions */
Guido van Rossum's avatar
Guido van Rossum committed
10

11
#include "Python.h"
Guido van Rossum's avatar
Guido van Rossum committed
12

Guido van Rossum's avatar
Guido van Rossum committed
13 14
#include <ctype.h>

15
#include "regexpr.h"
Guido van Rossum's avatar
Guido van Rossum committed
16

17
static PyObject *RegexError;	/* Exception */	
Guido van Rossum's avatar
Guido van Rossum committed
18 19

typedef struct {
20
	PyObject_HEAD
Guido van Rossum's avatar
Guido van Rossum committed
21 22 23
	struct re_pattern_buffer re_patbuf; /* The compiled expression */
	struct re_registers re_regs; /* The registers from the last match */
	char re_fastmap[256];	/* Storage for fastmap */
24 25 26 27 28
	PyObject *re_translate;	/* String object for translate table */
	PyObject *re_lastok;	/* String object last matched/searched */
	PyObject *re_groupindex;	/* Group name to index dictionary */
	PyObject *re_givenpat;	/* Pattern with symbolic groups */
	PyObject *re_realpat;	/* Pattern without symbolic groups */
Guido van Rossum's avatar
Guido van Rossum committed
29 30 31 32 33
} regexobject;

/* Regex object methods */

static void
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
34
reg_dealloc(regexobject *re)
Guido van Rossum's avatar
Guido van Rossum committed
35
{
36
	if (re->re_patbuf.buffer)
37
		free(re->re_patbuf.buffer);
38 39 40 41 42
	Py_XDECREF(re->re_translate);
	Py_XDECREF(re->re_lastok);
	Py_XDECREF(re->re_groupindex);
	Py_XDECREF(re->re_givenpat);
	Py_XDECREF(re->re_realpat);
43
	PyObject_Del(re);
Guido van Rossum's avatar
Guido van Rossum committed
44 45
}

46
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
47
makeresult(struct re_registers *regs)
Guido van Rossum's avatar
Guido van Rossum committed
48
{
49 50 51
	PyObject *v;
	int i;
	static PyObject *filler = NULL;
52

53 54 55 56 57 58 59 60
	if (filler == NULL) {
		filler = Py_BuildValue("(ii)", -1, -1);
		if (filler == NULL)
			return NULL;
	}
	v = PyTuple_New(RE_NREGS);
	if (v == NULL)
		return NULL;
61

62 63 64 65 66 67 68 69 70 71
	for (i = 0; i < RE_NREGS; i++) {
		int lo = regs->start[i];
		int hi = regs->end[i];
		PyObject *w;
		if (lo == -1 && hi == -1) {
			w = filler;
			Py_INCREF(w);
		}
		else
			w = Py_BuildValue("(ii)", lo, hi);
72 73
		if (w == NULL || PyTuple_SetItem(v, i, w) < 0) {
			Py_DECREF(v);
74
			return NULL;
Guido van Rossum's avatar
Guido van Rossum committed
75 76 77 78 79
		}
	}
	return v;
}

80
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
81
regobj_match(regexobject *re, PyObject *args)
Guido van Rossum's avatar
Guido van Rossum committed
82
{
83
	PyObject *argstring;
Guido van Rossum's avatar
Guido van Rossum committed
84
	char *buffer;
85
	int size;
86
	int offset = 0;
Guido van Rossum's avatar
Guido van Rossum committed
87
	int result;
88

89
	if (!PyArg_ParseTuple(args, "O|i:match", &argstring, &offset))
90
		return NULL;
91
	if (!PyArg_Parse(argstring, "t#", &buffer, &size))
92 93
		return NULL;

94
	if (offset < 0 || offset > size) {
95
		PyErr_SetString(RegexError, "match offset out of range");
96 97
		return NULL;
	}
98
	Py_XDECREF(re->re_lastok);
99
	re->re_lastok = NULL;
100
	result = _Py_re_match(&re->re_patbuf, (unsigned char *)buffer, size, offset,
101
			      &re->re_regs);
Guido van Rossum's avatar
Guido van Rossum committed
102
	if (result < -1) {
103 104 105 106
		/* Serious failure of some sort; if re_match didn't 
		   set an exception, raise a generic error */
	        if (!PyErr_Occurred())
		        PyErr_SetString(RegexError, "match failure");
Guido van Rossum's avatar
Guido van Rossum committed
107 108
		return NULL;
	}
109
	if (result >= 0) {
110 111
		Py_INCREF(argstring);
		re->re_lastok = argstring;
112
	}
113
	return PyInt_FromLong((long)result); /* Length of the match or -1 */
Guido van Rossum's avatar
Guido van Rossum committed
114
}
115

116
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
117
regobj_search(regexobject *re, PyObject *args)
Guido van Rossum's avatar
Guido van Rossum committed
118
{
119
	PyObject *argstring;
Guido van Rossum's avatar
Guido van Rossum committed
120 121
	char *buffer;
	int size;
122
	int offset = 0;
Guido van Rossum's avatar
Guido van Rossum committed
123 124
	int range;
	int result;
125
	
126
	if (!PyArg_ParseTuple(args, "O|i:search", &argstring, &offset))
127
		return NULL;
128
	if (!PyArg_Parse(argstring, "t#:search", &buffer, &size))
129 130
		return NULL;

131
	if (offset < 0 || offset > size) {
132
		PyErr_SetString(RegexError, "search offset out of range");
133
		return NULL;
Guido van Rossum's avatar
Guido van Rossum committed
134
	}
135 136 137 138
	/* NB: In Emacs 18.57, the documentation for re_search[_2] and
	   the implementation don't match: the documentation states that
	   |range| positions are tried, while the code tries |range|+1
	   positions.  It seems more productive to believe the code! */
139
	range = size - offset;
140
	Py_XDECREF(re->re_lastok);
141
	re->re_lastok = NULL;
142
	result = _Py_re_search(&re->re_patbuf, (unsigned char *)buffer, size, offset, range,
Guido van Rossum's avatar
Guido van Rossum committed
143 144
			   &re->re_regs);
	if (result < -1) {
145 146 147 148
		/* Serious failure of some sort; if re_match didn't 
		   set an exception, raise a generic error */
	        if (!PyErr_Occurred())
	  	        PyErr_SetString(RegexError, "match failure");
Guido van Rossum's avatar
Guido van Rossum committed
149 150
		return NULL;
	}
151
	if (result >= 0) {
152 153
		Py_INCREF(argstring);
		re->re_lastok = argstring;
154
	}
155
	return PyInt_FromLong((long)result); /* Position of the match or -1 */
Guido van Rossum's avatar
Guido van Rossum committed
156 157
}

158 159 160 161
/* get the group from the regex where index can be a string (group name) or
   an integer index [0 .. 99]
 */
static PyObject*
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
162
group_from_index(regexobject *re, PyObject *index)
163 164
{
	int i, a, b;
165 166 167 168 169 170 171 172
	char *v;

	if (PyString_Check(index))
		if (re->re_groupindex == NULL ||
		    !(index = PyDict_GetItem(re->re_groupindex, index)))
		{
			PyErr_SetString(RegexError,
					"group() group name doesn't exist");
173
			return NULL;
174
		}
175 176 177 178 179

	i = PyInt_AsLong(index);
	if (i == -1 && PyErr_Occurred())
		return NULL;

180
	if (i < 0 || i >= RE_NREGS) {
181
		PyErr_SetString(RegexError, "group() index out of range");
182 183 184
		return NULL;
	}
	if (re->re_lastok == NULL) {
185
		PyErr_SetString(RegexError,
186
			   "group() only valid after successful match/search");
187 188 189 190 191
		return NULL;
	}
	a = re->re_regs.start[i];
	b = re->re_regs.end[i];
	if (a < 0 || b < 0) {
192 193
		Py_INCREF(Py_None);
		return Py_None;
194
	}
195 196 197 198 199 200 201 202 203
	
	if (!(v = PyString_AsString(re->re_lastok)))
		return NULL;

	return PyString_FromStringAndSize(v+a, b-a);
}


static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
204
regobj_group(regexobject *re, PyObject *args)
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
{
	int n = PyTuple_Size(args);
	int i;
	PyObject *res = NULL;

	if (n < 0)
		return NULL;
	if (n == 0) {
		PyErr_SetString(PyExc_TypeError, "not enough arguments");
		return NULL;
	}
	if (n == 1) {
		/* return value is a single string */
		PyObject *index = PyTuple_GetItem(args, 0);
		if (!index)
			return NULL;
		
		return group_from_index(re, index);
	}

	/* return value is a tuple */
	if (!(res = PyTuple_New(n)))
		return NULL;

	for (i = 0; i < n; i++) {
		PyObject *index = PyTuple_GetItem(args, i);
		PyObject *group = NULL;

		if (!index)
			goto finally;
		if (!(group = group_from_index(re, index)))
			goto finally;
		if (PyTuple_SetItem(res, i, group) < 0)
			goto finally;
	}
	return res;

  finally:
	Py_DECREF(res);
	return NULL;
245 246
}

247

248
static struct PyMethodDef reg_methods[] = {
249 250 251
	{"match",	(PyCFunction)regobj_match, METH_VARARGS},
	{"search",	(PyCFunction)regobj_search, METH_VARARGS},
	{"group",	(PyCFunction)regobj_group, METH_VARARGS},
Guido van Rossum's avatar
Guido van Rossum committed
252 253 254
	{NULL,		NULL}		/* sentinel */
};

255 256 257 258 259 260 261 262 263


static char* members[] = {
	"last", "regs", "translate",
	"groupindex", "realpat", "givenpat",
	NULL
};


264
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
265
regobj_getattr(regexobject *re, char *name)
Guido van Rossum's avatar
Guido van Rossum committed
266
{
267
	if (strcmp(name, "regs") == 0) {
268
		if (re->re_lastok == NULL) {
269 270
			Py_INCREF(Py_None);
			return Py_None;
271 272 273
		}
		return makeresult(&re->re_regs);
	}
274 275
	if (strcmp(name, "last") == 0) {
		if (re->re_lastok == NULL) {
276 277
			Py_INCREF(Py_None);
			return Py_None;
278
		}
279
		Py_INCREF(re->re_lastok);
280 281
		return re->re_lastok;
	}
282 283
	if (strcmp(name, "translate") == 0) {
		if (re->re_translate == NULL) {
284 285
			Py_INCREF(Py_None);
			return Py_None;
286
		}
287
		Py_INCREF(re->re_translate);
288 289
		return re->re_translate;
	}
290 291
	if (strcmp(name, "groupindex") == 0) {
		if (re->re_groupindex == NULL) {
292 293
			Py_INCREF(Py_None);
			return Py_None;
294
		}
295
		Py_INCREF(re->re_groupindex);
296 297 298 299
		return re->re_groupindex;
	}
	if (strcmp(name, "realpat") == 0) {
		if (re->re_realpat == NULL) {
300 301
			Py_INCREF(Py_None);
			return Py_None;
302
		}
303
		Py_INCREF(re->re_realpat);
304 305 306 307
		return re->re_realpat;
	}
	if (strcmp(name, "givenpat") == 0) {
		if (re->re_givenpat == NULL) {
308 309
			Py_INCREF(Py_None);
			return Py_None;
310
		}
311
		Py_INCREF(re->re_givenpat);
312 313
		return re->re_givenpat;
	}
314
	if (strcmp(name, "__members__") == 0) {
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
		int i = 0;
		PyObject *list = NULL;

		/* okay, so it's unlikely this list will change that often.
		   still, it's easier to change it in just one place.
		 */
		while (members[i])
			i++;
		if (!(list = PyList_New(i)))
			return NULL;

		i = 0;
		while (members[i]) {
			PyObject* v = PyString_FromString(members[i]);
			if (!v || PyList_SetItem(list, i, v) < 0) {
330
				Py_DECREF(list);
331
				return NULL;
332
			}
333
			i++;
334 335 336
		}
		return list;
	}
337
	return Py_FindMethod(reg_methods, (PyObject *)re, name);
Guido van Rossum's avatar
Guido van Rossum committed
338 339
}

340
static PyTypeObject Regextype = {
341
	PyObject_HEAD_INIT(NULL)
342
	0,				     /*ob_size*/
343
	"regex.regex",			     /*tp_name*/
344 345
	sizeof(regexobject),		     /*tp_size*/
	0,				     /*tp_itemsize*/
Guido van Rossum's avatar
Guido van Rossum committed
346
	/* methods */
347 348 349 350 351 352
	(destructor)reg_dealloc,	     /*tp_dealloc*/
	0,				     /*tp_print*/
	(getattrfunc)regobj_getattr,	     /*tp_getattr*/
	0,				     /*tp_setattr*/
	0,				     /*tp_compare*/
	0,				     /*tp_repr*/
Guido van Rossum's avatar
Guido van Rossum committed
353 354
};

355 356 357 358 359 360
/* reference counting invariants:
   pattern: borrowed
   translate: borrowed
   givenpat: borrowed
   groupindex: transferred
*/
361
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
362
newregexobject(PyObject *pattern, PyObject *translate, PyObject *givenpat, PyObject *groupindex)
Guido van Rossum's avatar
Guido van Rossum committed
363 364
{
	regexobject *re;
365 366
	char *pat;
	int size;
367

368
	if (!PyArg_Parse(pattern, "t#", &pat, &size))
369 370
		return NULL;
	
371 372
	if (translate != NULL && PyString_Size(translate) != 256) {
		PyErr_SetString(RegexError,
373
				"translation table must be 256 bytes");
374 375
		return NULL;
	}
376
	re = PyObject_New(regexobject, &Regextype);
Guido van Rossum's avatar
Guido van Rossum committed
377 378 379 380
	if (re != NULL) {
		char *error;
		re->re_patbuf.buffer = NULL;
		re->re_patbuf.allocated = 0;
381
		re->re_patbuf.fastmap = (unsigned char *)re->re_fastmap;
382
		if (translate) {
383
			re->re_patbuf.translate = (unsigned char *)PyString_AsString(translate);
384 385 386 387
			if (!re->re_patbuf.translate)
				goto finally;
			Py_INCREF(translate);
		}
388 389 390 391
		else
			re->re_patbuf.translate = NULL;
		re->re_translate = translate;
		re->re_lastok = NULL;
392
		re->re_groupindex = groupindex;
393
		Py_INCREF(pattern);
394
		re->re_realpat = pattern;
395
		Py_INCREF(givenpat);
396
		re->re_givenpat = givenpat;
397
		error = _Py_re_compile_pattern((unsigned char *)pat, size, &re->re_patbuf);
Guido van Rossum's avatar
Guido van Rossum committed
398
		if (error != NULL) {
399
			PyErr_SetString(RegexError, error);
400
			goto finally;
Guido van Rossum's avatar
Guido van Rossum committed
401 402
		}
	}
403
	return (PyObject *)re;
404 405 406
  finally:
	Py_DECREF(re);
	return NULL;
Guido van Rossum's avatar
Guido van Rossum committed
407 408
}

409
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
410
regex_compile(PyObject *self, PyObject *args)
Guido van Rossum's avatar
Guido van Rossum committed
411
{
412 413
	PyObject *pat = NULL;
	PyObject *tran = NULL;
414

415
	if (!PyArg_ParseTuple(args, "S|S:compile", &pat, &tran))
416
		return NULL;
417
	return newregexobject(pat, tran, pat, NULL);
Guido van Rossum's avatar
Guido van Rossum committed
418 419
}

420
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
421
symcomp(PyObject *pattern, PyObject *gdict)
422
{
423
	char *opat, *oend, *o, *n, *g, *v;
424
	int group_count = 0;
425
	int sz;
426 427
	int escaped = 0;
	char name_buf[128];
428
	PyObject *npattern;
429 430
	int require_escape = re_syntax & RE_NO_BK_PARENS ? 0 : 1;

431 432 433 434 435 436 437 438 439
	if (!(opat = PyString_AsString(pattern)))
		return NULL;

	if ((sz = PyString_Size(pattern)) < 0)
		return NULL;

	oend = opat + sz;
	o = opat;

440
	if (oend == opat) {
441
		Py_INCREF(pattern);
442 443 444
		return pattern;
	}

445 446
	if (!(npattern = PyString_FromStringAndSize((char*)NULL, sz)) ||
	    !(n = PyString_AsString(npattern)))
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
		return NULL;

	while (o < oend) {
		if (*o == '(' && escaped == require_escape) {
			char *backtrack;
			escaped = 0;
			++group_count;
			*n++ = *o;
			if (++o >= oend || *o != '<')
				continue;
			/* *o == '<' */
			if (o+1 < oend && *(o+1) == '>')
				continue;
			backtrack = o;
			g = name_buf;
			for (++o; o < oend;) {
				if (*o == '>') {
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
				    PyObject *group_name = NULL;
				    PyObject *group_index = NULL;
				    *g++ = '\0';
				    group_name = PyString_FromString(name_buf);
				    group_index = PyInt_FromLong(group_count);
				    if (group_name == NULL ||
					group_index == NULL ||
					PyDict_SetItem(gdict, group_name,
						       group_index) != 0)
				    {
					    Py_XDECREF(group_name);
					    Py_XDECREF(group_index);
					    Py_XDECREF(npattern);
					    return NULL;
				    }
479 480
				    Py_DECREF(group_name);
				    Py_DECREF(group_index);
481 482
				    ++o;     /* eat the '>' */
				    break;
483
				}
Guido van Rossum's avatar
Guido van Rossum committed
484
				if (!isalnum(Py_CHARMASK(*o)) && *o != '_') {
485 486 487 488 489 490
					o = backtrack;
					break;
				}
				*g++ = *o++;
			}
		}
491
		else if (*o == '[' && !escaped) {
492
			*n++ = *o;
493
			++o;		     /* eat the char following '[' */
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
			*n++ = *o;
			while (o < oend && *o != ']') {
				++o;
				*n++ = *o;
			}
			if (o < oend)
				++o;
		}
		else if (*o == '\\') {
			escaped = 1;
			*n++ = *o;
			++o;
		}
		else {
			escaped = 0;
			*n++ = *o;
			++o;
		}
	}

514 515 516 517 518 519
	if (!(v = PyString_AsString(npattern))) {
		Py_DECREF(npattern);
		return NULL;
	}
	/* _PyString_Resize() decrements npattern on failure */
	if (_PyString_Resize(&npattern, n - v) == 0)
520 521 522 523 524 525 526
		return npattern;
	else {
		return NULL;
	}

}

527
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
528
regex_symcomp(PyObject *self, PyObject *args)
529
{
530 531 532 533
	PyObject *pattern;
	PyObject *tran = NULL;
	PyObject *gdict = NULL;
	PyObject *npattern;
534
	PyObject *retval = NULL;
535

536
	if (!PyArg_ParseTuple(args, "S|S:symcomp", &pattern, &tran))
537 538
		return NULL;

539
	gdict = PyDict_New();
540
	if (gdict == NULL || (npattern = symcomp(pattern, gdict)) == NULL) {
541 542
		Py_DECREF(gdict);
		Py_DECREF(pattern);
543 544
		return NULL;
	}
545 546 547
	retval = newregexobject(npattern, tran, pattern, gdict);
	Py_DECREF(npattern);
	return retval;
548 549 550
}


551 552
static PyObject *cache_pat;
static PyObject *cache_prog;
Guido van Rossum's avatar
Guido van Rossum committed
553 554

static int
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
555
update_cache(PyObject *pat)
Guido van Rossum's avatar
Guido van Rossum committed
556
{
557 558 559 560 561 562
	PyObject *tuple = Py_BuildValue("(O)", pat);
	int status = 0;

	if (!tuple)
		return -1;

Guido van Rossum's avatar
Guido van Rossum committed
563
	if (pat != cache_pat) {
564
		Py_XDECREF(cache_pat);
Guido van Rossum's avatar
Guido van Rossum committed
565
		cache_pat = NULL;
566
		Py_XDECREF(cache_prog);
567 568 569 570 571
		cache_prog = regex_compile((PyObject *)NULL, tuple);
		if (cache_prog == NULL) {
			status = -1;
			goto finally;
		}
Guido van Rossum's avatar
Guido van Rossum committed
572
		cache_pat = pat;
573
		Py_INCREF(cache_pat);
Guido van Rossum's avatar
Guido van Rossum committed
574
	}
575 576 577
  finally:
	Py_DECREF(tuple);
	return status;
Guido van Rossum's avatar
Guido van Rossum committed
578 579
}

580
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
581
regex_match(PyObject *self, PyObject *args)
Guido van Rossum's avatar
Guido van Rossum committed
582
{
583
	PyObject *pat, *string;
584 585
	PyObject *tuple, *v;

Neal Norwitz's avatar
Neal Norwitz committed
586
	if (!PyArg_ParseTuple(args, "SS:match", &pat, &string))
Guido van Rossum's avatar
Guido van Rossum committed
587 588 589
		return NULL;
	if (update_cache(pat) < 0)
		return NULL;
590 591 592 593 594 595

	if (!(tuple = Py_BuildValue("(S)", string)))
		return NULL;
	v = regobj_match((regexobject *)cache_prog, tuple);
	Py_DECREF(tuple);
	return v;
Guido van Rossum's avatar
Guido van Rossum committed
596 597
}

598
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
599
regex_search(PyObject *self, PyObject *args)
Guido van Rossum's avatar
Guido van Rossum committed
600
{
601
	PyObject *pat, *string;
602 603
	PyObject *tuple, *v;

Neal Norwitz's avatar
Neal Norwitz committed
604
	if (!PyArg_ParseTuple(args, "SS:search", &pat, &string))
Guido van Rossum's avatar
Guido van Rossum committed
605 606 607
		return NULL;
	if (update_cache(pat) < 0)
		return NULL;
608 609 610 611 612 613

	if (!(tuple = Py_BuildValue("(S)", string)))
		return NULL;
	v = regobj_search((regexobject *)cache_prog, tuple);
	Py_DECREF(tuple);
	return v;
Guido van Rossum's avatar
Guido van Rossum committed
614 615
}

616
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
617
regex_set_syntax(PyObject *self, PyObject *args)
Guido van Rossum's avatar
Guido van Rossum committed
618 619
{
	int syntax;
Neal Norwitz's avatar
Neal Norwitz committed
620
	if (!PyArg_ParseTuple(args, "i:set_syntax", &syntax))
Guido van Rossum's avatar
Guido van Rossum committed
621 622
		return NULL;
	syntax = re_set_syntax(syntax);
623 624 625 626 627
	/* wipe the global pattern cache */
	Py_XDECREF(cache_pat);
	cache_pat = NULL;
	Py_XDECREF(cache_prog);
	cache_prog = NULL;
628
	return PyInt_FromLong((long)syntax);
Guido van Rossum's avatar
Guido van Rossum committed
629 630
}

631
static PyObject *
Neal Norwitz's avatar
Neal Norwitz committed
632
regex_get_syntax(PyObject *self)
633 634 635 636 637
{
	return PyInt_FromLong((long)re_syntax);
}


638
static struct PyMethodDef regex_global_methods[] = {
639 640
	{"compile",	regex_compile, METH_VARARGS},
	{"symcomp",	regex_symcomp, METH_VARARGS},
Neal Norwitz's avatar
Neal Norwitz committed
641 642 643 644
	{"match",	regex_match, METH_VARARGS},
	{"search",	regex_search, METH_VARARGS},
	{"set_syntax",	regex_set_syntax, METH_VARARGS},
	{"get_syntax",  (PyCFunction)regex_get_syntax, METH_NOARGS},
645
	{NULL,		NULL}		     /* sentinel */
Guido van Rossum's avatar
Guido van Rossum committed
646 647
};

648
DL_EXPORT(void)
649
initregex(void)
Guido van Rossum's avatar
Guido van Rossum committed
650
{
651
	PyObject *m, *d, *v;
652 653
	int i;
	char *s;
Guido van Rossum's avatar
Guido van Rossum committed
654
	
655 656 657
	/* Initialize object type */
	Regextype.ob_type = &PyType_Type;

658 659
	m = Py_InitModule("regex", regex_global_methods);
	d = PyModule_GetDict(m);
660

661 662 663
	if (PyErr_Warn(PyExc_DeprecationWarning,
		       "the regex module is deprecated; "
		       "please use the re module") < 0)
664
		return;
Guido van Rossum's avatar
Guido van Rossum committed
665 666
	
	/* Initialize regex.error exception */
667
	v = RegexError = PyErr_NewException("regex.error", NULL, NULL);
668 669 670
	if (v == NULL || PyDict_SetItemString(d, "error", v) != 0)
		goto finally;
	
671
	/* Initialize regex.casefold constant */
672 673 674 675 676 677 678 679 680 681 682
	if (!(v = PyString_FromStringAndSize((char *)NULL, 256)))
		goto finally;
	
	if (!(s = PyString_AsString(v)))
		goto finally;

	for (i = 0; i < 256; i++) {
		if (isupper(i))
			s[i] = tolower(i);
		else
			s[i] = i;
683
	}
684 685 686 687 688 689 690
	if (PyDict_SetItemString(d, "casefold", v) < 0)
		goto finally;
	Py_DECREF(v);

	if (!PyErr_Occurred())
		return;
  finally:
691
	/* Nothing */ ;
Guido van Rossum's avatar
Guido van Rossum committed
692
}