Marc-Andre Lemburg:
The attached patch set includes a workaround to get Python with
Unicode compile on BSDI 4.x (courtesy Thomas Wouters; the cause
is a bug in the BSDI wchar.h header file) and Python interfaces
for the MBCS codec donated by Mark Hammond.
Also included are some minor corrections w/r to the docs of
the new "es" and "es#" parser markers (use PyMem_Free() instead
of free(); thanks to Mark Hammond for finding these).
The unicodedata tests are now in a separate file
(test_unicodedata.py) to avoid problems if the module cannot
be found.
diff --git a/Modules/_codecsmodule.c b/Modules/_codecsmodule.c
index 6c8a2d4..4f368f8 100644
--- a/Modules/_codecsmodule.c
+++ b/Modules/_codecsmodule.c
@@ -286,6 +286,26 @@
size);
}
+#ifdef MS_WIN32
+
+static PyObject *
+mbcs_decode(PyObject *self,
+ PyObject *args)
+{
+ const char *data;
+ int size;
+ const char *errors = NULL;
+
+ if (!PyArg_ParseTuple(args, "t#|z:mbcs_decode",
+ &data, &size, &errors))
+ return NULL;
+
+ return codec_tuple(PyUnicode_DecodeMBCS(data, size, errors),
+ size);
+}
+
+#endif /* MS_WIN32 */
+
/* --- Encoder ------------------------------------------------------------ */
static PyObject *
@@ -491,6 +511,28 @@
PyUnicode_GET_SIZE(str));
}
+#ifdef MS_WIN32
+
+static PyObject *
+mbcs_encode(PyObject *self,
+ PyObject *args)
+{
+ PyObject *str;
+ const char *errors = NULL;
+
+ if (!PyArg_ParseTuple(args, "U|z:mbcs_encode",
+ &str, &errors))
+ return NULL;
+
+ return codec_tuple(PyUnicode_EncodeMBCS(
+ PyUnicode_AS_UNICODE(str),
+ PyUnicode_GET_SIZE(str),
+ errors),
+ PyUnicode_GET_SIZE(str));
+}
+
+#endif /* MS_WIN32 */
+
/* --- Module API --------------------------------------------------------- */
static PyMethodDef _codecs_functions[] = {
@@ -519,6 +561,10 @@
{"charmap_decode", charmap_decode, 1},
{"readbuffer_encode", readbuffer_encode, 1},
{"charbuffer_encode", charbuffer_encode, 1},
+#ifdef MS_WIN32
+ {"mbcs_encode", mbcs_encode, 1},
+ {"mbcs_decode", mbcs_decode, 1},
+#endif
{NULL, NULL} /* sentinel */
};