[Python-checkins] cpython (merge 3.3 -> default): (Merge 3.3) Issue #17429: platform.linux_distribution() now decodes files from

victor.stinner python-checkins at python.org
Mon Dec 9 00:04:42 CET 2013


http://hg.python.org/cpython/rev/407f18c8ce8a
changeset:   87841:407f18c8ce8a
parent:      87839:4de4b5a4e405
parent:      87840:4580976c07cb
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Mon Dec 09 00:04:09 2013 +0100
summary:
  (Merge 3.3) 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.

files:
  Lib/platform.py           |   9 +++++++--
  Lib/test/test_platform.py |  16 ++++++++++++++++
  Misc/ACKS                 |   1 +
  Misc/NEWS                 |   6 ++++++
  4 files changed, 30 insertions(+), 2 deletions(-)


diff --git a/Lib/platform.py b/Lib/platform.py
--- a/Lib/platform.py
+++ b/Lib/platform.py
@@ -129,6 +129,10 @@
         # 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 @@
 
     """
     try:
-        etc = os.listdir('/etc')
+        etc = os.listdir(_UNIXCONFDIR)
     except OSError:
         # Probably not a Unix system
         return distname,version,id
@@ -331,7 +335,8 @@
         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)
 
diff --git a/Lib/test/test_platform.py b/Lib/test/test_platform.py
--- a/Lib/test/test_platform.py
+++ b/Lib/test/test_platform.py
@@ -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 @@
                     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(
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -711,6 +711,7 @@
 Andrew Kuchling
 Dave Kuhlman
 Jon Kuhn
+Toshio Kuratomi
 Vladimir Kushnir
 Erno Kuusela
 Ross Lagerwall
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,12 @@
 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 #19343: Expose FreeBSD-specific APIs in resource module.  Original
   patch by Koobs.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list