Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad6139a

Browse files
committed
Merged revisions 79936 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r79936 | philip.jenvey | 2010-04-10 15:27:15 -0500 (Sat, 10 Apr 2010) | 3 lines fix PYTHONWARNINGS handling to not modify the original env value and improve its tests ........
1 parent 75825a7 commit ad6139a

2 files changed

Lines changed: 24 additions & 16 deletions

File tree

Lib/test/test_warnings.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,8 @@ def test_single_warning(self):
694694
p = subprocess.Popen([sys.executable,
695695
"-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
696696
stdout=subprocess.PIPE, env=newenv)
697-
self.assertEqual(p.stdout.read(), b"['ignore::DeprecationWarning']")
697+
self.assertEqual(p.communicate()[0], b"['ignore::DeprecationWarning']")
698+
self.assertEqual(p.wait(), 0)
698699

699700
def test_comma_separated_warnings(self):
700701
newenv = os.environ.copy()
@@ -703,17 +704,19 @@ def test_comma_separated_warnings(self):
703704
p = subprocess.Popen([sys.executable,
704705
"-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
705706
stdout=subprocess.PIPE, env=newenv)
706-
self.assertEqual(p.stdout.read(),
707+
self.assertEqual(p.communicate()[0],
707708
b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")
709+
self.assertEqual(p.wait(), 0)
708710

709711
def test_envvar_and_command_line(self):
710712
newenv = os.environ.copy()
711713
newenv["PYTHONWARNINGS"] = "ignore::DeprecationWarning"
712714
p = subprocess.Popen([sys.executable, "-W" "ignore::UnicodeWarning",
713715
"-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
714716
stdout=subprocess.PIPE, env=newenv)
715-
self.assertEqual(p.stdout.read(),
717+
self.assertEqual(p.communicate()[0],
716718
b"['ignore::UnicodeWarning', 'ignore::DeprecationWarning']")
719+
self.assertEqual(p.wait(), 0)
717720

718721
class CEnvironmentVariableTests(EnvironmentVariableTests):
719722
module = c_warnings

Modules/main.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -403,22 +403,27 @@ Py_Main(int argc, wchar_t **argv)
403403
Py_NoUserSiteDirectory = 1;
404404

405405
if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0') {
406-
char *buf;
407-
wchar_t *warning;
408-
size_t len;
409-
410-
for (buf = strtok(p, ",");
411-
buf != NULL;
412-
buf = strtok(NULL, ",")) {
413-
len = strlen(buf);
414-
warning = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
415-
if (warning == NULL)
406+
char *buf, *warning;
407+
408+
buf = (char *)malloc(strlen(p) + 1);
409+
if (buf == NULL)
410+
Py_FatalError(
411+
"not enough memory to copy PYTHONWARNINGS");
412+
strcpy(buf, p);
413+
for (warning = strtok(buf, ",");
414+
warning != NULL;
415+
warning = strtok(NULL, ",")) {
416+
wchar_t *wide_warning;
417+
size_t len = strlen(buf);
418+
wide_warning = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
419+
if (wide_warning == NULL)
416420
Py_FatalError(
417421
"not enough memory to copy PYTHONWARNINGS");
418-
mbstowcs(warning, buf, len);
419-
PySys_AddWarnOption(warning);
420-
free(warning);
422+
mbstowcs(wide_warning, warning, len);
423+
PySys_AddWarnOption(wide_warning);
424+
free(wide_warning);
421425
}
426+
free(buf);
422427
}
423428

424429
if (command == NULL && module == NULL && _PyOS_optind < argc &&

0 commit comments

Comments
 (0)