diff options
author | Victor Stinner <vstinner@python.org> | 2020-01-24 10:53:44 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-24 10:53:44 (GMT) |
commit | 161e7b36b1ea871a1352ccfc1d4f4c1eda76830f (patch) | |
tree | 0f44995e5fadbd3cedb76688e71a06693dcd7270 /Lib | |
parent | 2d5097663d7f80921fb07cdcd26c9d59cf71f1a2 (diff) | |
download | cpython-161e7b36b1ea871a1352ccfc1d4f4c1eda76830f.zip cpython-161e7b36b1ea871a1352ccfc1d4f4c1eda76830f.tar.gz cpython-161e7b36b1ea871a1352ccfc1d4f4c1eda76830f.tar.bz2 |
bpo-39413: Implement os.unsetenv() on Windows (GH-18163)
The os.unsetenv() function is now also available on Windows.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_os.py | 43 |
1 files changed, 35 insertions, 8 deletions
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 82c441c..dbdc00c 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -953,17 +953,44 @@ class EnvironTests(mapping_tests.BasicTestMappingProtocol): value_str = value.decode(sys.getfilesystemencoding(), 'surrogateescape') self.assertEqual(os.environ['bytes'], value_str) + @unittest.skipUnless(hasattr(os, 'putenv'), "Test needs os.putenv()") + @unittest.skipUnless(hasattr(os, 'unsetenv'), "Test needs os.unsetenv()") + def test_putenv_unsetenv(self): + name = "PYTHONTESTVAR" + value = "testvalue" + code = f'import os; print(repr(os.environ.get({name!r})))' + + with support.EnvironmentVarGuard() as env: + env.pop(name, None) + + os.putenv(name, value) + proc = subprocess.run([sys.executable, '-c', code], check=True, + stdout=subprocess.PIPE, text=True) + self.assertEqual(proc.stdout.rstrip(), repr(value)) + + os.unsetenv(name) + proc = subprocess.run([sys.executable, '-c', code], check=True, + stdout=subprocess.PIPE, text=True) + self.assertEqual(proc.stdout.rstrip(), repr(None)) + # On OS X < 10.6, unsetenv() doesn't return a value (bpo-13415). @support.requires_mac_ver(10, 6) - def test_unset_error(self): + @unittest.skipUnless(hasattr(os, 'putenv'), "Test needs os.putenv()") + @unittest.skipUnless(hasattr(os, 'unsetenv'), "Test needs os.unsetenv()") + def test_putenv_unsetenv_error(self): + # Empty variable name is invalid. + # "=" and null character are not allowed in a variable name. + for name in ('', '=name', 'na=me', 'name=', 'name\0', 'na\0me'): + self.assertRaises((OSError, ValueError), os.putenv, name, "value") + self.assertRaises((OSError, ValueError), os.unsetenv, name) + if sys.platform == "win32": - # an environment variable is limited to 32,767 characters - key = 'x' * 50000 - self.assertRaises(ValueError, os.environ.__delitem__, key) - else: - # "=" is not allowed in a variable name - key = 'key=' - self.assertRaises(OSError, os.environ.__delitem__, key) + # On Windows, an environment variable string ("name=value" string) + # is limited to 32,767 characters + longstr = 'x' * 32_768 + self.assertRaises(ValueError, os.putenv, longstr, "1") + self.assertRaises(ValueError, os.putenv, "X", longstr) + self.assertRaises(ValueError, os.unsetenv, longstr) def test_key_type(self): missing = 'missingkey' |