[Python-checkins] CVS: python/dist/src/Modules _sre.c,2.13,2.14

Fredrik Lundh python-dev@python.org
Fri, 30 Jun 2000 00:05:50 -0700


Update of /cvsroot/python/python/dist/src/Modules
In directory slayer.i.sourceforge.net:/tmp/cvs-serv21938/Modules

Modified Files:
	_sre.c 
Log Message:

- fixed default value handling in group/groupdict

- added test suite


Index: _sre.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/_sre.c,v
retrieving revision 2.13
retrieving revision 2.14
diff -C2 -r2.13 -r2.14
*** _sre.c	2000/06/30 00:27:46	2.13
--- _sre.c	2000/06/30 07:05:48	2.14
***************
*** 1567,1571 ****
  
  static PyObject*
! match_getslice_by_index(MatchObject* self, int index)
  {
  	if (index < 0 || index >= self->groups) {
--- 1567,1571 ----
  
  static PyObject*
! match_getslice_by_index(MatchObject* self, int index, PyObject* def)
  {
  	if (index < 0 || index >= self->groups) {
***************
*** 1579,1585 ****
  
  	if (self->string == Py_None || self->mark[index+index] < 0) {
! 		/* return None if the string or group is undefined */
! 		Py_INCREF(Py_None);
! 		return Py_None;
  	}
  
--- 1579,1585 ----
  
  	if (self->string == Py_None || self->mark[index+index] < 0) {
! 		/* return default value if the string or group is undefined */
! 		Py_INCREF(def);
! 		return def;
  	}
  
***************
*** 1606,1612 ****
  
  static PyObject*
! match_getslice(MatchObject* self, PyObject* index)
  {
! 	return match_getslice_by_index(self, match_getindex(self, index));
  }
  
--- 1606,1612 ----
  
  static PyObject*
! match_getslice(MatchObject* self, PyObject* index, PyObject* def)
  {
! 	return match_getslice_by_index(self, match_getindex(self, index), def);
  }
  
***************
*** 1621,1628 ****
  	switch (size) {
  	case 0:
! 		result = match_getslice(self, Py_False);
  		break;
  	case 1:
! 		result = match_getslice(self, PyTuple_GET_ITEM(args, 0));
  		break;
  	default:
--- 1621,1628 ----
  	switch (size) {
  	case 0:
! 		result = match_getslice(self, Py_False, Py_None);
  		break;
  	case 1:
! 		result = match_getslice(self, PyTuple_GET_ITEM(args, 0), Py_None);
  		break;
  	default:
***************
*** 1632,1636 ****
  			return NULL;
  		for (i = 0; i < size; i++) {
! 			PyObject* item = match_getslice(self, PyTuple_GET_ITEM(args, i));
  			if (!item) {
  				Py_DECREF(result);
--- 1632,1638 ----
  			return NULL;
  		for (i = 0; i < size; i++) {
! 			PyObject* item = match_getslice(
!                 self, PyTuple_GET_ITEM(args, i), Py_None
!                 );
  			if (!item) {
  				Py_DECREF(result);
***************
*** 1650,1654 ****
  	int index;
  
!     /* FIXME: <fl> handle default value! */
  
  	result = PyTuple_New(self->groups-1);
--- 1652,1658 ----
  	int index;
  
! 	PyObject* def = Py_None;
! 	if (!PyArg_ParseTuple(args, "|O", &def))
! 		return NULL;
  
  	result = PyTuple_New(self->groups-1);
***************
*** 1658,1663 ****
  	for (index = 1; index < self->groups; index++) {
  		PyObject* item;
! 		/* FIXME: <fl> handle default! */
! 		item = match_getslice_by_index(self, index);
  		if (!item) {
  			Py_DECREF(result);
--- 1662,1666 ----
  	for (index = 1; index < self->groups; index++) {
  		PyObject* item;
! 		item = match_getslice_by_index(self, index, def);
  		if (!item) {
  			Py_DECREF(result);
***************
*** 1677,1691 ****
  	int index;
  
!     /* FIXME: <fl> handle default value! */
  
  	result = PyDict_New();
! 	if (!result)
! 		return NULL;
! 	if (!self->pattern->groupindex)
  		return result;
  
  	keys = PyMapping_Keys(self->pattern->groupindex);
! 	if (!keys)
  		return NULL;
  
  	for (index = 0; index < PyList_GET_SIZE(keys); index++) {
--- 1680,1696 ----
  	int index;
  
! 	PyObject* def = Py_None;
! 	if (!PyArg_ParseTuple(args, "|O", &def))
! 		return NULL;
  
  	result = PyDict_New();
! 	if (!result || !self->pattern->groupindex)
  		return result;
  
  	keys = PyMapping_Keys(self->pattern->groupindex);
! 	if (!keys) {
!         Py_DECREF(result);
  		return NULL;
+     }
  
  	for (index = 0; index < PyList_GET_SIZE(keys); index++) {
***************
*** 1698,1702 ****
  			return NULL;
  		}
! 		item = match_getslice(self, key);
  		if (!item) {
  			Py_DECREF(key);
--- 1703,1707 ----
  			return NULL;
  		}
! 		item = match_getslice(self, key, def);
  		if (!item) {
  			Py_DECREF(key);