string.translate behaviour

Peter Schneider-Kamp petersc at stud.ntnu.no
Mon May 22 07:44:23 EDT 2000


I have made a small patch against stringobject.h which
extends the string.translate function with a new syntax as
proposed by A.M. Kuchling. It is attached below (3K).

Now what function is called for translate on a unicode
object? can the function in stringobject.c ever be called
with a unicode string?

thinking-it-is-just-legacy-code-ly y'rs
Peter
--
Peter Schneider-Kamp          ++47-7388-7331
Herman Krags veg 51-11        mailto:peter at schneider-kamp.de
N-7050 Trondheim              http://schneider-kamp.de
-------------- next part --------------
diff -c --recursive python/dist/src/Objects/stringobject.c python-mod/dist/src/Objects/stringobject.c
*** python/dist/src/Objects/stringobject.c	Mon May  8 16:08:05 2000
--- python-mod/dist/src/Objects/stringobject.c	Mon May 22 10:33:26 2000
***************
*** 1293,1308 ****
  	int inlen, tablen, dellen = 0;
  	PyObject *result;
  	int trans_table[256];
! 	PyObject *tableobj, *delobj = NULL;
  
! 	if (!PyArg_ParseTuple(args, "O|O:translate",
! 			      &tableobj, &delobj))
  		return NULL;
  
  	if (PyString_Check(tableobj)) {
  		table1 = PyString_AS_STRING(tableobj);
! 		tablen = PyString_GET_SIZE(tableobj);
! 	}
  	else if (PyUnicode_Check(tableobj)) {
  		/* Unicode .translate() does not support the deletechars 
  		   parameter; instead a mapping to None will cause characters
--- 1293,1309 ----
  	int inlen, tablen, dellen = 0;
  	PyObject *result;
  	int trans_table[256];
!         char new_table[256];
! 	PyObject *tableobj, *delobj = NULL, *delchars = NULL;
  
! 	if (!PyArg_ParseTuple(args, "O|OO:translate",
! 			      &tableobj, &delobj, &delchars))
  		return NULL;
  
  	if (PyString_Check(tableobj)) {
  		table1 = PyString_AS_STRING(tableobj);
!                 tablen = PyString_GET_SIZE(tableobj);
!         }
  	else if (PyUnicode_Check(tableobj)) {
  		/* Unicode .translate() does not support the deletechars 
  		   parameter; instead a mapping to None will cause characters
***************
*** 1329,1344 ****
  		}
  		else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen))
  			return NULL;
- 
- 		if (tablen != 256) {
- 			PyErr_SetString(PyExc_ValueError,
- 			  "translation table must be 256 characters long");
- 			return NULL;
- 		}
  	}
  	else {
  		del_table = NULL;
  		dellen = 0;
  	}
  
  	table = table1;
--- 1330,1374 ----
  		}
  		else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen))
  			return NULL;
  	}
  	else {
  		del_table = NULL;
  		dellen = 0;
+                 if (tablen != 256) {
+                 	PyErr_SetString(PyExc_ValueError,
+                         "translation table must be a 256 char string.");
+                         return NULL;
+                 }
+ 	}
+ 
+        	if ((delobj != NULL) && ((tablen != 256) || (dellen == 256))) {
+         	if (tablen != dellen) {
+         		PyErr_SetString(PyExc_ValueError,
+                         "arguments fromchar and tochar must match in size");
+                         return NULL;
+                 }
+                 for (i = 0; i < 256; i++)
+                 	new_table[i] = (unsigned char)i;
+         	for (i = 0; i < tablen; i++)
+ 			new_table[table1[i]] = del_table[i];
+                 table1 = new_table;
+ 		if (delchars != NULL) {
+ 			if (PyString_Check(delchars)) {
+         			del_table = PyString_AS_STRING(delchars);
+         			dellen = PyString_GET_SIZE(delchars);
+ 			}
+ 			else if (PyUnicode_Check(delchars)) {
+ 				PyErr_SetString(PyExc_TypeError,
+ 				"deletions are implemented differently for unicode");
+         			return NULL;
+ 			}
+                         else if (PyObject_AsCharBuffer(delchars, &del_table, &dellen))
+                                 return NULL;
+                 }
+                 else {
+                         del_table = NULL;
+                         dellen = 0;
+                 }
  	}
  
  	table = table1;


More information about the Python-list mailing list