Ensure that the default encoding used by python is never ASCII (needed when running a non frozen version of calibre on linux)

This commit is contained in:
Kovid Goyal 2010-12-10 16:10:31 -07:00
parent d1145138a9
commit 8dd120a574
2 changed files with 27 additions and 3 deletions

View File

@ -329,7 +329,7 @@ icu_lower(PyObject *self, PyObject *args) {
PyMem_Free(input);
return ret;
}
} // }}}
// title {{{
static PyObject *
@ -374,9 +374,22 @@ icu_title(PyObject *self, PyObject *args) {
PyMem_Free(input);
return ret;
} // }}}
// set_default_encoding {{{
static PyObject *
icu_set_default_encoding(PyObject *self, PyObject *args) {
char *encoding;
if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
return NULL;
if (PyUnicode_SetDefaultEncoding(encoding))
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
// }}}
static PyMethodDef icu_methods[] = {
{"upper", icu_upper, METH_VARARGS,
@ -391,6 +404,10 @@ static PyMethodDef icu_methods[] = {
"title(locale, unicode object) -> Title cased unicode object using locale rules."
},
{"set_default_encoding", icu_set_default_encoding, METH_VARARGS,
"set_default_encoding(encoding) -> Set the default encoding for the python unicode implementation."
},
{NULL} /* Sentinel */
};

View File

@ -6,6 +6,7 @@ __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
# Setup code {{{
import sys
from functools import partial
from calibre.constants import plugins
@ -85,6 +86,12 @@ load_icu()
load_collator()
_icu_not_ok = _icu is None or _collator is None
try:
if sys.getdefaultencoding().lower() == 'ascii':
_icu.set_default_encoding('utf-8')
except:
pass
# }}}
################# The string functions ########################################