parsers._asciitransform: also accept a fallback function
authorSiddharth Agarwal <sid0@fb.com>
Tue, 31 Mar 2015 23:22:03 -0700
changeset 24606 e4a733c34bc6
parent 24605 98744856b7d3
child 24607 f5b527024fcc
parsers._asciitransform: also accept a fallback function This function will be used in upcoming patches to provide a C implementation of the function to generate the foldmap.
mercurial/parsers.c
--- a/mercurial/parsers.c	Wed Apr 01 00:38:56 2015 -0700
+++ b/mercurial/parsers.c	Tue Mar 31 23:22:03 2015 -0700
@@ -115,7 +115,8 @@
 }
 
 static inline PyObject *_asciitransform(PyObject *str_obj,
-					const char table[128])
+					const char table[128],
+					PyObject *fallback_fn)
 {
 	char *str, *newstr;
 	Py_ssize_t i, len;
@@ -134,11 +135,16 @@
 	for (i = 0; i < len; i++) {
 		char c = str[i];
 		if (c & 0x80) {
-			PyObject *err = PyUnicodeDecodeError_Create(
-				"ascii", str, len, i, (i + 1),
-				"unexpected code byte");
-			PyErr_SetObject(PyExc_UnicodeDecodeError, err);
-			Py_XDECREF(err);
+			if (fallback_fn != NULL) {
+				ret = PyObject_CallFunctionObjArgs(fallback_fn,
+					str_obj, NULL);
+			} else {
+				PyObject *err = PyUnicodeDecodeError_Create(
+					"ascii", str, len, i, (i + 1),
+					"unexpected code byte");
+				PyErr_SetObject(PyExc_UnicodeDecodeError, err);
+				Py_XDECREF(err);
+			}
 			goto quit;
 		}
 		newstr[i] = table[(unsigned char)c];
@@ -156,7 +162,7 @@
 	PyObject *str_obj;
 	if (!PyArg_ParseTuple(args, "O!:asciilower", &PyBytes_Type, &str_obj))
 		return NULL;
-	return _asciitransform(str_obj, lowertable);
+	return _asciitransform(str_obj, lowertable, NULL);
 }
 
 static PyObject *asciiupper(PyObject *self, PyObject *args)
@@ -164,7 +170,7 @@
 	PyObject *str_obj;
 	if (!PyArg_ParseTuple(args, "O!:asciiupper", &PyBytes_Type, &str_obj))
 		return NULL;
-	return _asciitransform(str_obj, uppertable);
+	return _asciitransform(str_obj, uppertable, NULL);
 }
 
 /*