Patch #445762: Support --disable-unicode
- Do not compile unicodeobject, unicodectype, and unicodedata if Unicode is disabled
- check for Py_USING_UNICODE in all places that use Unicode functions
- disables unicode literals, and the builtin functions
- add the types.StringTypes list
- remove Unicode literals from most tests.
diff --git a/Modules/_codecsmodule.c b/Modules/_codecsmodule.c
index 61f25d1..a085bcf 100644
--- a/Modules/_codecsmodule.c
+++ b/Modules/_codecsmodule.c
@@ -71,6 +71,7 @@
return NULL;
}
+#ifdef Py_USING_UNICODE
/* --- Helpers ------------------------------------------------------------ */
static
@@ -621,12 +622,14 @@
}
#endif /* MS_WIN32 */
+#endif /* Py_USING_UNICODE */
/* --- Module API --------------------------------------------------------- */
static PyMethodDef _codecs_functions[] = {
{"register", codecregister, 1},
{"lookup", codeclookup, 1},
+#ifdef Py_USING_UNICODE
{"utf_8_encode", utf_8_encode, 1},
{"utf_8_decode", utf_8_decode, 1},
{"utf_16_encode", utf_16_encode, 1},
@@ -654,6 +657,7 @@
{"mbcs_encode", mbcs_encode, 1},
{"mbcs_decode", mbcs_decode, 1},
#endif
+#endif /* Py_USING_UNICODE */
{NULL, NULL} /* sentinel */
};
diff --git a/Modules/_sre.c b/Modules/_sre.c
index 1776a16..9943c30 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -63,7 +63,7 @@
/* defining this one enables tracing */
#undef VERBOSE
-#if PY_VERSION_HEX >= 0x01060000
+#if PY_VERSION_HEX >= 0x01060000 && defined(Py_USING_UNICODE)
/* defining this enables unicode support (default under 1.6a1 and later) */
#define HAVE_UNICODE
#endif
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
index 9b73307..eedb0c1 100644
--- a/Modules/_tkinter.c
+++ b/Modules/_tkinter.c
@@ -255,6 +255,7 @@
{
if (PyString_Check(value))
return PyString_AsString(value);
+#ifdef Py_USING_UNICODE
else if (PyUnicode_Check(value)) {
PyObject *v = PyUnicode_AsUTF8String(value);
if (v == NULL)
@@ -266,6 +267,7 @@
Py_DECREF(v);
return PyString_AsString(v);
}
+#endif
else {
PyObject *v = PyObject_Str(value);
if (v == NULL)
@@ -520,6 +522,7 @@
ckfree(FREECAST argv);
return result;
}
+#ifdef Py_USING_UNICODE
else if (PyUnicode_Check(value)) {
#if TKMAJORMINOR <= 8001
/* In Tcl 8.1 we must use UTF-8 */
@@ -542,6 +545,7 @@
PyUnicode_GET_SIZE(value));
#endif /* TKMAJORMINOR > 8001 */
}
+#endif
else {
PyObject *v = PyObject_Str(value);
if (!v)
@@ -616,13 +620,16 @@
so would confuse applications that expect a string. */
char *s = Tcl_GetStringResult(interp);
char *p = s;
+
/* If the result contains any bytes with the top bit set,
it's UTF-8 and we should decode it to Unicode */
+#ifdef Py_USING_UNICODE
while (*p != '\0') {
if (*p & 0x80)
break;
p++;
}
+
if (*p == '\0')
res = PyString_FromStringAndSize(s, (int)(p-s));
else {
@@ -634,6 +641,10 @@
res = PyString_FromStringAndSize(s, (int)(p-s));
}
}
+#else
+ p = strchr(p, '\0');
+ res = PyString_FromStringAndSize(s, (int)(p-s));
+#endif
}
LEAVE_OVERLAP_TCL
diff --git a/Modules/cPickle.c b/Modules/cPickle.c
index b27339f..bb0d281 100644
--- a/Modules/cPickle.c
+++ b/Modules/cPickle.c
@@ -1172,6 +1172,7 @@
}
+#ifdef Py_USING_UNICODE
/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
backslash and newline characters to \uXXXX escapes. */
static PyObject *
@@ -1289,6 +1290,7 @@
Py_XDECREF(repr);
return -1;
}
+#endif
static int
@@ -1824,11 +1826,13 @@
goto finally;
}
+#ifdef Py_USING_UNICODE
case 'u':
if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
res = save_unicode(self, args, 0);
goto finally;
}
+#endif
}
if (args->ob_refcnt > 1) {
@@ -1857,12 +1861,14 @@
}
break;
+#ifdef Py_USING_UNICODE
case 'u':
if (type == &PyUnicode_Type) {
res = save_unicode(self, args, 1);
goto finally;
}
break;
+#endif
case 't':
if (type == &PyTuple_Type) {
@@ -2818,6 +2824,7 @@
}
+#ifdef Py_USING_UNICODE
static int
load_unicode(Unpicklerobject *self) {
PyObject *str = 0;
@@ -2836,8 +2843,10 @@
finally:
return res;
}
+#endif
+#ifdef Py_USING_UNICODE
static int
load_binunicode(Unpicklerobject *self) {
PyObject *unicode;
@@ -2857,6 +2866,7 @@
PDATA_PUSH(self->stack, unicode, -1);
return 0;
}
+#endif
static int
@@ -3615,6 +3625,7 @@
break;
continue;
+#ifdef Py_USING_UNICODE
case UNICODE:
if (load_unicode(self) < 0)
break;
@@ -3624,6 +3635,7 @@
if (load_binunicode(self) < 0)
break;
continue;
+#endif
case EMPTY_TUPLE:
if (load_empty_tuple(self) < 0)
@@ -3905,6 +3917,7 @@
break;
continue;
+#ifdef Py_USING_UNICODE
case UNICODE:
if (load_unicode(self) < 0)
break;
@@ -3914,6 +3927,7 @@
if (load_binunicode(self) < 0)
break;
continue;
+#endif
case EMPTY_TUPLE:
if (load_empty_tuple(self) < 0)
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c
index 4bddc46..3311093 100644
--- a/Modules/pyexpat.c
+++ b/Modules/pyexpat.c
@@ -28,6 +28,11 @@
#define Py_TPFLAGS_GC 0
#endif
+#if (PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION > 5) || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 2)
+/* In Python 1.6, 2.0 and 2.1, disabling Unicode was not possible. */
+#define Py_USING_UNICODE
+#endif
+
enum HandlerTypes {
StartElement,
EndElement,
@@ -173,7 +178,7 @@
}
#endif
-#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
+#ifdef Py_USING_UNICODE
#if EXPAT_VERSION == 0x010200
static PyObject *
conv_atts_using_unicode(XML_Char **atts)
@@ -370,7 +375,7 @@
return res;
}
-#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
+#ifndef Py_USING_UNICODE
#define STRING_CONV_FUNC conv_string_to_utf8
#else
/* Python 1.6 and later versions */
@@ -506,7 +511,7 @@
const XML_Char *data),
("(O&O&)",STRING_CONV_FUNC,target, STRING_CONV_FUNC,data))
-#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
+#ifndef Py_USING_UNICODE
VOID_HANDLER(CharacterData,
(void *userData, const XML_Char *data, int len),
("(N)", conv_string_len_to_utf8(data,len)))
@@ -531,7 +536,7 @@
STRING_CONV_FUNC,notationName))
#if EXPAT_VERSION >= 0x015f00
-#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
+#ifndef Py_USING_UNICODE
VOID_HANDLER(EntityDecl,
(void *userData,
const XML_Char *entityName,
@@ -608,7 +613,7 @@
return conv_content_model(model, conv_string_to_utf8);
}
-#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
+#ifdef Py_USING_UNICODE
static PyObject *
conv_content_model_unicode(XML_Content * const model)
{
@@ -678,7 +683,7 @@
(void *userData),
("()"))
-#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
+#ifndef Py_USING_UNICODE
VOID_HANDLER(Default,
(void *userData, const XML_Char *s, int len),
("(N)", conv_string_len_to_utf8(s,len)))
@@ -1064,7 +1069,7 @@
/* ---------- */
-#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
+#ifdef Py_USING_UNICODE
/*
pyexpat international encoding support.
@@ -1158,8 +1163,7 @@
return NULL;
}
XML_SetUserData(self->itself, (void *)self);
-#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
-#else
+#ifdef Py_USING_UNICODE
XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
#endif
@@ -1292,7 +1296,7 @@
}
if (strcmp(name, "returns_unicode") == 0) {
if (PyObject_IsTrue(v)) {
-#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
+#ifndef Py_USING_UNICODE
PyErr_SetString(PyExc_ValueError,
"Cannot return Unicode strings in Python 1.5");
return -1;
@@ -1545,8 +1549,7 @@
info.minor, info.micro));
}
#endif
-#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
-#else
+#ifdef Py_USING_UNICODE
init_template_buffer();
#endif
/* XXX When Expat supports some way of figuring out how it was