Commit 2fe2ca90 authored by Robert Bradshaw's avatar Robert Bradshaw

Cleanup default encoding.

parent 719cc3fc
...@@ -45,9 +45,8 @@ static int __Pyx_sys_getdefaultencoding_not_ascii; ...@@ -45,9 +45,8 @@ static int __Pyx_sys_getdefaultencoding_not_ascii;
static int __Pyx_init_sys_getdefaultencoding_not_ascii() { static int __Pyx_init_sys_getdefaultencoding_not_ascii() {
PyObject* sys = NULL; PyObject* sys = NULL;
PyObject* default_encoding = NULL; PyObject* default_encoding = NULL;
PyObject* codecs = NULL; PyObject* ascii_chars_u = NULL;
PyObject* normalized_encoding = NULL; PyObject* ascii_chars_b = NULL;
PyObject* normalized_encoding_name = NULL;
sys = PyImport_ImportModule("sys"); sys = PyImport_ImportModule("sys");
if (sys == NULL) goto bad; if (sys == NULL) goto bad;
default_encoding = PyObject_CallMethod(sys, (char*) (const char*) "getdefaultencoding", NULL); default_encoding = PyObject_CallMethod(sys, (char*) (const char*) "getdefaultencoding", NULL);
...@@ -55,43 +54,34 @@ static int __Pyx_init_sys_getdefaultencoding_not_ascii() { ...@@ -55,43 +54,34 @@ static int __Pyx_init_sys_getdefaultencoding_not_ascii() {
if (strcmp(PyBytes_AsString(default_encoding), "ascii") == 0) { if (strcmp(PyBytes_AsString(default_encoding), "ascii") == 0) {
__Pyx_sys_getdefaultencoding_not_ascii = 0; __Pyx_sys_getdefaultencoding_not_ascii = 0;
} else { } else {
char* normalized_encoding_c; const char* default_encoding_c = PyBytes_AS_STRING(default_encoding);
codecs = PyImport_ImportModule("codecs"); char ascii_chars[128];
if (codecs == NULL) goto bad; int c;
normalized_encoding = PyObject_CallMethod(codecs, (char*) (const char*) "lookup", (char*) (const char*) "O", default_encoding); for (c = 0; c < 128; c++) {
if (normalized_encoding == NULL) goto bad; ascii_chars[c] = c;
normalized_encoding_name = PyObject_GetAttrString(normalized_encoding, (char*) (const char*) "name"); }
if (normalized_encoding_name == NULL) goto bad; __Pyx_sys_getdefaultencoding_not_ascii = 1;
normalized_encoding_c = PyBytes_AsString(normalized_encoding_name); ascii_chars_u = PyUnicode_DecodeASCII(ascii_chars, 128, NULL);
if (normalized_encoding_c == NULL) goto bad; if (ascii_chars_u == NULL) goto bad;
__Pyx_sys_getdefaultencoding_not_ascii = strcmp(normalized_encoding_c, "ascii"); ascii_chars_b = PyUnicode_AsEncodedString(ascii_chars_u, default_encoding_c, NULL);
if (!__Pyx_sys_getdefaultencoding_not_ascii) { if (ascii_chars_b == NULL || strncmp(ascii_chars, PyBytes_AS_STRING(ascii_chars_b), 128) != 0) {
int ascii_compatible = PyErr_Format(
(strncmp(normalized_encoding_c, "iso8859-", 8) == 0 || PyExc_ValueError,
strcmp(normalized_encoding_c, "macroman") == 0 || "This module compiled with c_string_encoding=ascii, but default encoding '%s' is not a superset of ascii.",
strcmp(normalized_encoding_c, "utf-8") == 0); default_encoding_c);
// I've never heard of a system where this happens, but it might... goto bad;
if (!ascii_compatible) {
PyErr_Format(
PyExc_ValueError,
"This module compiled with c_string_encoding=ascii, but default encoding '%s' is not a superset of ascii.",
normalized_encoding_c);
goto bad;
}
} }
} }
Py_XDECREF(sys); Py_XDECREF(sys);
Py_XDECREF(default_encoding); Py_XDECREF(default_encoding);
Py_XDECREF(codecs); Py_XDECREF(ascii_chars_u);
Py_XDECREF(normalized_encoding); Py_XDECREF(ascii_chars_b);
Py_XDECREF(normalized_encoding_name);
return 0; return 0;
bad: bad:
Py_XDECREF(sys); Py_XDECREF(sys);
Py_XDECREF(default_encoding); Py_XDECREF(default_encoding);
Py_XDECREF(codecs); Py_XDECREF(ascii_chars_u);
Py_XDECREF(normalized_encoding); Py_XDECREF(ascii_chars_b);
Py_XDECREF(normalized_encoding_name);
return -1; return -1;
} }
#else #else
...@@ -122,6 +112,7 @@ static CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_ ...@@ -122,6 +112,7 @@ static CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_
#if PY_VERSION_HEX < 0x03030000 #if PY_VERSION_HEX < 0x03030000
// borrowed, cached reference // borrowed, cached reference
PyObject* defenc = _PyUnicode_AsDefaultEncodedString(o, NULL); PyObject* defenc = _PyUnicode_AsDefaultEncodedString(o, NULL);
if (!defenc) return NULL;
char* maybe_ascii = PyBytes_AS_STRING(defenc); char* maybe_ascii = PyBytes_AS_STRING(defenc);
char* end = maybe_ascii + PyBytes_GET_SIZE(defenc); char* end = maybe_ascii + PyBytes_GET_SIZE(defenc);
char* c; char* c;
...@@ -135,8 +126,8 @@ static CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_ ...@@ -135,8 +126,8 @@ static CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_
*length = PyBytes_GET_SIZE(defenc); *length = PyBytes_GET_SIZE(defenc);
return maybe_ascii; return maybe_ascii;
#else /* PY_VERSION_HEX < 0x03030000 */ #else /* PY_VERSION_HEX < 0x03030000 */
PyUnicode_READY(o); if (PyUnicode_READY(o) == -1) return NULL;
if (PyUnicode_IS_ASCIII(o)) { if (PyUnicode_IS_ASCII(o)) {
// cached for the lifetime of the object // cached for the lifetime of the object
*length = PyUnicode_GET_DATA_SIZE(o); *length = PyUnicode_GET_DATA_SIZE(o);
return PyUnicode_AsUTF8(o); return PyUnicode_AsUTF8(o);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment