Issue #8589: Decode PYTHONWARNINGS environment variable with the file… · pythoncapi/cpython@9ca9c25 · GitHub
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

5 files changed

Lines changed: 27 additions & 12 deletions

File tree

Doc/c-api/sys.rst

Lines changed: 4 additions & 0 deletions

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

0 commit comments

Comments
 (0)