From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Thu, 15 Jul 2021 20:09:55 +0000 (-0700) Subject: bpo-44647: Fix test_httpservers failing on Unicode characters in os.environ on Window... X-Git-Tag: v3.9.7~156 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=95596d5921eeab9ae49f0dc31263a249013b6849;p=thirdparty%2FPython%2Fcpython.git bpo-44647: Fix test_httpservers failing on Unicode characters in os.environ on Windows (GH-27161) (#27170) GH-23638 introduced a new test for Accept: headers in CGI HTTP servers. This test serializes all of os.environ on the server side. For non-UTF8 locales this can fail for some Unicode characters found in environment variables. This change fixes the HTTP_ACCEPT test. (cherry picked from commit 82b218f36ce6ef910bda5af227a9fd5be613c94f) Co-authored-by: Ɓukasz Langa --- diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py index b33fedaafc5a..70e562f4052d 100644 --- a/Lib/test/test_httpservers.py +++ b/Lib/test/test_httpservers.py @@ -591,9 +591,18 @@ cgi_file6 = """\ #!%s import os -print("Content-type: text/plain") +print("X-ambv: was here") +print("Content-type: text/html") print() -print(repr(os.environ)) +print("
")
+for k, v in os.environ.items():
+    try:
+        k.encode('ascii')
+        v.encode('ascii')
+    except UnicodeEncodeError:
+        continue  # see: BPO-44647
+    print(f"{k}={v}")
+print("
") """ @@ -848,8 +857,8 @@ class CGIHTTPServerTestCase(BaseTestCase): with self.subTest(headers): res = self.request('/cgi-bin/file6.py', 'GET', headers=headers) self.assertEqual(http.HTTPStatus.OK, res.status) - expected = f"'HTTP_ACCEPT': {expected!r}" - self.assertIn(expected.encode('ascii'), res.read()) + expected = f"HTTP_ACCEPT={expected}".encode('ascii') + self.assertIn(expected, res.read()) class SocketlessRequestHandler(SimpleHTTPRequestHandler):