]> granicus.if.org Git - python/commitdiff
Issue #20599: Force ASCII encoding for stdout in test_cleanup() of test_builtin
authorVictor Stinner <victor.stinner@gmail.com>
Wed, 12 Feb 2014 17:27:55 +0000 (18:27 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Wed, 12 Feb 2014 17:27:55 +0000 (18:27 +0100)
On Windows, the codec of sys.stdout is implemented in Python. At exit, the
codec may be unloaded before the destructor tries to write something to
sys.stdout.

Lib/test/test_builtin.py

index 8a307b976a82a834e7faa7580ed2bb2f4ea64c54..5c14de357762ad7761e2471cf734ebc912d61f6a 100644 (file)
@@ -1604,10 +1604,10 @@ class ShutdownTest(unittest.TestCase):
 
             class C:
                 def __del__(self):
-                    print("before", flush=True)
+                    print("before")
                     # Check that builtins still exist
                     len(())
-                    print("after", flush=True)
+                    print("after")
 
             c = C()
             # Make this module survive until builtins and sys are cleaned
@@ -1617,7 +1617,15 @@ class ShutdownTest(unittest.TestCase):
             # through a GC phase.
             here = sys.modules[__name__]
             """
-        rc, out, err = assert_python_ok("-c", code)
+        # Issue #20599: Force ASCII encoding to get a codec implemented in C,
+        # otherwise the codec may be unloaded before C.__del__() is called, and
+        # so print("before") fails because the codec cannot be used to encode
+        # "before" to sys.stdout.encoding. For example, on Windows,
+        # sys.stdout.encoding is the OEM code page and these code pages are
+        # implemented in Python
+        rc, out, err = assert_python_ok("-c", code,
+                                        PYTHONIOENCODING="ascii",
+                                        __cleanenv=True)
         self.assertEqual(["before", "after"], out.decode().splitlines())