Skip to content

Commit 9ca9c25

Browse files
author
Victor Stinner
committed
Issue python#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.
1 parent a5bf3f5 commit 9ca9c25

File tree

5 files changed

+27
-12
lines changed

5 files changed

+27
-12
lines changed

Doc/c-api/sys.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ accessible to C code. They all work with the current interpreter thread's
8181

8282
Append *s* to :data:`sys.warnoptions`.
8383

84+
.. cfunction:: void PySys_AddWarnOptionUnicode(PyObject *unicode)
85+
86+
Append *unicode* to :data:`sys.warnoptions`.
87+
8488
.. cfunction:: void PySys_SetPath(wchar_t *path)
8589

8690
Set :data:`sys.path` to a list object of paths found in *path* which should

Include/sysmodule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ PyAPI_DATA(PyObject *) _PySys_TraceFunc, *_PySys_ProfileFunc;
2121

2222
PyAPI_FUNC(void) PySys_ResetWarnOptions(void);
2323
PyAPI_FUNC(void) PySys_AddWarnOption(const wchar_t *);
24+
PyAPI_FUNC(void) PySys_AddWarnOptionUnicode(PyObject *);
2425
PyAPI_FUNC(int) PySys_HasWarnOptions(void);
2526

2627
#ifdef __cplusplus

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in Python 3.2 Alpha 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #8589: Decode PYTHONWARNINGS environment variable with the file system
16+
encoding and surrogateespace error handler instead of the locale encoding to
17+
be consistent with os.environ. Add PySys_AddWarnOptionUnicode() function.
18+
1519
- PyObject_Dump() encodes unicode objects to utf8 with backslashreplace
1620
(instead of strict) error handler to escape surrogates
1721

Modules/main.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ Py_Main(int argc, wchar_t **argv)
425425
#else
426426
if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0') {
427427
char *buf, *oldloc;
428-
wchar_t *warning;
428+
PyObject *warning;
429429

430430
/* settle for strtok here as there's no one standard
431431
C89 wcstok */
@@ -437,9 +437,10 @@ Py_Main(int argc, wchar_t **argv)
437437
oldloc = strdup(setlocale(LC_ALL, NULL));
438438
setlocale(LC_ALL, "");
439439
for (p = strtok(buf, ","); p != NULL; p = strtok(NULL, ",")) {
440-
if ((warning = _Py_char2wchar(p)) != NULL) {
441-
PySys_AddWarnOption(warning);
442-
PyMem_Free(warning);
440+
warning = PyUnicode_DecodeFSDefault(p);
441+
if (warning != NULL) {
442+
PySys_AddWarnOptionUnicode(warning);
443+
Py_DECREF(warning);
443444
}
444445
}
445446
setlocale(LC_ALL, oldloc);

Python/sysmodule.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,21 +1048,26 @@ PySys_ResetWarnOptions(void)
10481048
}
10491049

10501050
void
1051-
PySys_AddWarnOption(const wchar_t *s)
1051+
PySys_AddWarnOptionUnicode(PyObject *unicode)
10521052
{
1053-
PyObject *str;
1054-
10551053
if (warnoptions == NULL || !PyList_Check(warnoptions)) {
10561054
Py_XDECREF(warnoptions);
10571055
warnoptions = PyList_New(0);
10581056
if (warnoptions == NULL)
10591057
return;
10601058
}
1061-
str = PyUnicode_FromWideChar(s, -1);
1062-
if (str != NULL) {
1063-
PyList_Append(warnoptions, str);
1064-
Py_DECREF(str);
1065-
}
1059+
PyList_Append(warnoptions, unicode);
1060+
}
1061+
1062+
void
1063+
PySys_AddWarnOption(const wchar_t *s)
1064+
{
1065+
PyObject *unicode;
1066+
unicode = PyUnicode_FromWideChar(s, -1);
1067+
if (unicode == NULL)
1068+
return;
1069+
PySys_AddWarnOptionUnicode(unicode);
1070+
Py_DECREF(unicode);
10661071
}
10671072

10681073
int

0 commit comments

Comments
 (0)