]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #17429: platform.linux_distribution() now decodes files from the UTF-8
authorVictor Stinner <victor.stinner@gmail.com>
Sun, 8 Dec 2013 23:01:27 +0000 (00:01 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Sun, 8 Dec 2013 23:01:27 +0000 (00:01 +0100)
encoding with the surrogateescape error handler, instead of decoding from the
locale encoding in strict mode. It fixes the function on Fedora 19 which is
probably the first major distribution release with a non-ASCII name. Patch
written by Toshio Kuratomi.

Lib/platform.py
Lib/test/test_platform.py
Misc/ACKS
Misc/NEWS

index ec5df85d6f45bcd43bc8741b08905bb68465b1c6..030ef2a3a4b61232530f5127a0ada55ed4b5aebb 100755 (executable)
@@ -129,6 +129,10 @@ except AttributeError:
         # Standard Unix uses /dev/null
         DEV_NULL = '/dev/null'
 
+# Directory to search for configuration information on Unix.
+# Constant used by test_platform to test linux_distribution().
+_UNIXCONFDIR = '/etc'
+
 ### Platform specific APIs
 
 _libc_search = re.compile(b'(__libc_init)'
@@ -315,7 +319,7 @@ def linux_distribution(distname='', version='', id='',
 
     """
     try:
-        etc = os.listdir('/etc')
+        etc = os.listdir(_UNIXCONFDIR)
     except os.error:
         # Probably not a Unix system
         return distname,version,id
@@ -331,7 +335,8 @@ def linux_distribution(distname='', version='', id='',
         return _dist_try_harder(distname,version,id)
 
     # Read the first line
-    with open('/etc/'+file, 'r') as f:
+    with open(os.path.join(_UNIXCONFDIR, file), 'r',
+              encoding='utf-8', errors='surrogateescape') as f:
         firstline = f.readline()
     _distname, _version, _id = _parse_release_file(firstline)
 
index 0dcfe0504a5e3102d8497d5749493cb4fdd77050..8a5408e9a9af98f6ad3959c49acbf0a1d08a2678 100644 (file)
@@ -1,7 +1,10 @@
+from unittest import mock
+import contextlib
 import os
 import platform
 import subprocess
 import sys
+import tempfile
 import unittest
 import warnings
 
@@ -295,6 +298,19 @@ class PlatformTest(unittest.TestCase):
                     returncode = ret >> 8
                 self.assertEqual(returncode, len(data))
 
+    def test_linux_distribution_encoding(self):
+        # Issue #17429
+        with tempfile.TemporaryDirectory() as tempdir:
+            filename = os.path.join(tempdir, 'fedora-release')
+            with open(filename, 'w', encoding='utf-8') as f:
+                f.write('Fedora release 19 (Schr\xf6dinger\u2019s Cat)\n')
+
+            with mock.patch('platform._UNIXCONFDIR', tempdir):
+                distname, version, distid = platform.linux_distribution()
+
+            self.assertEqual(distname, 'Fedora')
+            self.assertEqual(version, '19')
+            self.assertEqual(distid, 'Schr\xf6dinger\u2019s Cat')
 
 def test_main():
     support.run_unittest(
index 2c957e3c1f22ed20f99b32678d06e26276784ecc..486e2affdee654c7aa4f34cadbf70763b159ee41 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -689,6 +689,7 @@ Steven Kryskalla
 Andrew Kuchling
 Dave Kuhlman
 Jon Kuhn
+Toshio Kuratomi
 Vladimir Kushnir
 Erno Kuusela
 Ross Lagerwall
index 5eacf9c9a5900974c4e82449eecec3b73159c30b..3596b4e1ad905b37c8a373f22aeedea5202478a9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -18,6 +18,12 @@ Core and Builtins
 Library
 -------
 
+- Issue #17429: platform.linux_distribution() now decodes files from the UTF-8
+  encoding with the surrogateescape error handler, instead of decoding from the
+  locale encoding in strict mode. It fixes the function on Fedora 19 which is
+  probably the first major distribution release with a non-ASCII name. Patch
+  written by Toshio Kuratomi.
+
 - Issue #19929: Call os.read with 32768 within subprocess.Popen.communicate
   rather than 4096 for efficiency.  A microbenchmark shows Linux and OS X
   both using ~50% less cpu time this way.