[issue5051] test_update2 in test_os.py invalid due to os.environ.clear() followed by reliance on environ COMSPEC

Roumen Petrov bugtrack at roumenpetrov.info
Wed Mar 20 00:58:38 CET 2013


Hi Ned,

> Ned Jackson Lovely added the comment:
[SNIP]
> In both cases, the currently running python executable, fetched via sys.executable and run using os.popen, is used to print the value, instead of the shell's echo. This moves things closer towards cross-platform niceness, and removes the dependency on /bin/sh.
>
> Unfortunately, I don't have a Windows machine readily available to test this on. Could apply your preferred patch, run it for me, and let me know if you have any problems?


$ uname -a
MINGW32_NT-5.1 QEMU 1.0.18(0.48/3/2) 2012-11-21 22:34 i686 Msys
---
Python 3.4.0a0 (default, Mar 20 2013, 00:32:43)
[GCC 4.7.2] on win32
---
$ cat ...test_os.py
....
     # Bug 1110478
     def test_update2(self):
         minimal_environ_keys = ('COMSPEC', 'PATH',)
         minimal_environ = {k:os.environ[k] for k in minimal_environ_keys
                             if k in os.environ}
         os.environ.clear()
         os.environ.update(HELLO="World")
         minimal_environ['HELLO'] = "World"
         os.environ.update(minimal_environ)
         python_cmd = "{0} -c \"import os;print(os.environ['HELLO'])\""
         with os.popen(python_cmd.format(sys.executable)) as popen:
             value = popen.read().strip()
             self.assertEqual(value, "World")

     # Bug 1110478
     def test_update3(self):
         self.assertTrue('HELLO' not in os.environ)
         os.environ.update(HELLO="World")
         python_cmd = "{0} -c \"import os;print(os.environ['HELLO'])\""
         with os.popen(python_cmd.format(sys.executable)) as popen:
             value = popen.read().strip()
             self.assertEqual(value, "World")

     @unittest.skipUnless(os.path.exists('/bin/sh'), 'requires /bin/sh')
     def test_os_popen_iter(self):
         with os.popen(
             "/bin/sh -c 'echo \"line1\nline2\nline3\"'") as popen:
             it = iter(popen)
             self.assertEqual(next(it), "line1\n")
             self.assertEqual(next(it), "line2\n")
             self.assertEqual(next(it), "line3\n")
             self.assertRaises(StopIteration, next, it)
....

result:
....
test_update (test.test_os.EnvironTests) ... ok
test_update2 (test.test_os.EnvironTests) ... ok
test_update3 (test.test_os.EnvironTests) ... ok
test_values (test.test_os.EnvironTests) ... ok
....

So with both (take2&take3) updates tests pass. Should work with MSVC 
builds.


May be test_os_popen_iter could be updated .

> Regards,
>
> Ned




More information about the Python-bugs-list mailing list