Issue #8589: Decode PYTHONWARNINGS environment variable with the file system

encoding and surrogateespace error handler instead of the locale encoding to be
consistent with os.environ. Add PySys_AddWarnOptionUnicode() function.
This commit is contained in:
Victor Stinner 2010-05-19 16:53:30 +00:00
parent a5bf3f520c
commit 9ca9c25bcd
5 changed files with 27 additions and 12 deletions

View File

@ -81,6 +81,10 @@ accessible to C code. They all work with the current interpreter thread's
Append *s* to :data:`sys.warnoptions`. Append *s* to :data:`sys.warnoptions`.
.. cfunction:: void PySys_AddWarnOptionUnicode(PyObject *unicode)
Append *unicode* to :data:`sys.warnoptions`.
.. cfunction:: void PySys_SetPath(wchar_t *path) .. cfunction:: void PySys_SetPath(wchar_t *path)
Set :data:`sys.path` to a list object of paths found in *path* which should Set :data:`sys.path` to a list object of paths found in *path* which should

View File

@ -21,6 +21,7 @@ PyAPI_DATA(PyObject *) _PySys_TraceFunc, *_PySys_ProfileFunc;
PyAPI_FUNC(void) PySys_ResetWarnOptions(void); PyAPI_FUNC(void) PySys_ResetWarnOptions(void);
PyAPI_FUNC(void) PySys_AddWarnOption(const wchar_t *); PyAPI_FUNC(void) PySys_AddWarnOption(const wchar_t *);
PyAPI_FUNC(void) PySys_AddWarnOptionUnicode(PyObject *);
PyAPI_FUNC(int) PySys_HasWarnOptions(void); PyAPI_FUNC(int) PySys_HasWarnOptions(void);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -12,6 +12,10 @@ What's New in Python 3.2 Alpha 1?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #8589: Decode PYTHONWARNINGS environment variable with the file system
encoding and surrogateespace error handler instead of the locale encoding to
be consistent with os.environ. Add PySys_AddWarnOptionUnicode() function.
- PyObject_Dump() encodes unicode objects to utf8 with backslashreplace - PyObject_Dump() encodes unicode objects to utf8 with backslashreplace
(instead of strict) error handler to escape surrogates (instead of strict) error handler to escape surrogates

View File

@ -425,7 +425,7 @@ Py_Main(int argc, wchar_t **argv)
#else #else
if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0') { if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0') {
char *buf, *oldloc; char *buf, *oldloc;
wchar_t *warning; PyObject *warning;
/* settle for strtok here as there's no one standard /* settle for strtok here as there's no one standard
C89 wcstok */ C89 wcstok */
@ -437,9 +437,10 @@ Py_Main(int argc, wchar_t **argv)
oldloc = strdup(setlocale(LC_ALL, NULL)); oldloc = strdup(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
for (p = strtok(buf, ","); p != NULL; p = strtok(NULL, ",")) { for (p = strtok(buf, ","); p != NULL; p = strtok(NULL, ",")) {
if ((warning = _Py_char2wchar(p)) != NULL) { warning = PyUnicode_DecodeFSDefault(p);
PySys_AddWarnOption(warning); if (warning != NULL) {
PyMem_Free(warning); PySys_AddWarnOptionUnicode(warning);
Py_DECREF(warning);
} }
} }
setlocale(LC_ALL, oldloc); setlocale(LC_ALL, oldloc);

View File

@ -1048,21 +1048,26 @@ PySys_ResetWarnOptions(void)
} }
void void
PySys_AddWarnOption(const wchar_t *s) PySys_AddWarnOptionUnicode(PyObject *unicode)
{ {
PyObject *str;
if (warnoptions == NULL || !PyList_Check(warnoptions)) { if (warnoptions == NULL || !PyList_Check(warnoptions)) {
Py_XDECREF(warnoptions); Py_XDECREF(warnoptions);
warnoptions = PyList_New(0); warnoptions = PyList_New(0);
if (warnoptions == NULL) if (warnoptions == NULL)
return; return;
} }
str = PyUnicode_FromWideChar(s, -1); PyList_Append(warnoptions, unicode);
if (str != NULL) { }
PyList_Append(warnoptions, str);
Py_DECREF(str); void
} PySys_AddWarnOption(const wchar_t *s)
{
PyObject *unicode;
unicode = PyUnicode_FromWideChar(s, -1);
if (unicode == NULL)
return;
PySys_AddWarnOptionUnicode(unicode);
Py_DECREF(unicode);
} }
int int