mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
fix PYTHONWARNINGS handling to not modify the original env value and improve
its tests
This commit is contained in:
parent
b60ee469cd
commit
cdd98fb463
2 changed files with 18 additions and 10 deletions
|
@ -683,7 +683,8 @@ class EnvironmentVariableTests(BaseTest):
|
||||||
p = subprocess.Popen([sys.executable,
|
p = subprocess.Popen([sys.executable,
|
||||||
"-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
|
"-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
|
||||||
stdout=subprocess.PIPE, env=newenv)
|
stdout=subprocess.PIPE, env=newenv)
|
||||||
self.assertEqual(p.stdout.read(), "['ignore::DeprecationWarning']")
|
self.assertEqual(p.communicate()[0], "['ignore::DeprecationWarning']")
|
||||||
|
self.assertEqual(p.wait(), 0)
|
||||||
|
|
||||||
def test_comma_separated_warnings(self):
|
def test_comma_separated_warnings(self):
|
||||||
newenv = os.environ.copy()
|
newenv = os.environ.copy()
|
||||||
|
@ -692,8 +693,9 @@ class EnvironmentVariableTests(BaseTest):
|
||||||
p = subprocess.Popen([sys.executable,
|
p = subprocess.Popen([sys.executable,
|
||||||
"-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
|
"-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
|
||||||
stdout=subprocess.PIPE, env=newenv)
|
stdout=subprocess.PIPE, env=newenv)
|
||||||
self.assertEqual(p.stdout.read(),
|
self.assertEqual(p.communicate()[0],
|
||||||
"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")
|
"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")
|
||||||
|
self.assertEqual(p.wait(), 0)
|
||||||
|
|
||||||
def test_envvar_and_command_line(self):
|
def test_envvar_and_command_line(self):
|
||||||
newenv = os.environ.copy()
|
newenv = os.environ.copy()
|
||||||
|
@ -701,8 +703,9 @@ class EnvironmentVariableTests(BaseTest):
|
||||||
p = subprocess.Popen([sys.executable, "-W" "ignore::UnicodeWarning",
|
p = subprocess.Popen([sys.executable, "-W" "ignore::UnicodeWarning",
|
||||||
"-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
|
"-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
|
||||||
stdout=subprocess.PIPE, env=newenv)
|
stdout=subprocess.PIPE, env=newenv)
|
||||||
self.assertEqual(p.stdout.read(),
|
self.assertEqual(p.communicate()[0],
|
||||||
"['ignore::UnicodeWarning', 'ignore::DeprecationWarning']")
|
"['ignore::UnicodeWarning', 'ignore::DeprecationWarning']")
|
||||||
|
self.assertEqual(p.wait(), 0)
|
||||||
|
|
||||||
class CEnvironmentVariableTests(EnvironmentVariableTests):
|
class CEnvironmentVariableTests(EnvironmentVariableTests):
|
||||||
module = c_warnings
|
module = c_warnings
|
||||||
|
|
|
@ -421,14 +421,19 @@ Py_Main(int argc, char **argv)
|
||||||
(p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
|
(p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
|
||||||
Py_NoUserSiteDirectory = 1;
|
Py_NoUserSiteDirectory = 1;
|
||||||
|
|
||||||
if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0')
|
if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0') {
|
||||||
{
|
char *buf, *warning;
|
||||||
char* warning = strtok(p, ",");
|
|
||||||
while (warning != NULL)
|
buf = (char *)malloc(strlen(p) + 1);
|
||||||
{
|
if (buf == NULL)
|
||||||
|
Py_FatalError(
|
||||||
|
"not enough memory to copy PYTHONWARNINGS");
|
||||||
|
strcpy(buf, p);
|
||||||
|
for (warning = strtok(buf, ",");
|
||||||
|
warning != NULL;
|
||||||
|
warning = strtok(NULL, ","))
|
||||||
PySys_AddWarnOption(warning);
|
PySys_AddWarnOption(warning);
|
||||||
warning = strtok(NULL, ",");
|
free(buf);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (command == NULL && module == NULL && _PyOS_optind < argc &&
|
if (command == NULL && module == NULL && _PyOS_optind < argc &&
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue