Skip to content

Commit 95596d5

Browse files
miss-islingtonambv
andauthored
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 82b218f) Co-authored-by: Łukasz Langa <[email protected]>
1 parent 421b543 commit 95596d5

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

Lib/test/test_httpservers.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,18 @@ def test_html_escape_filename(self):
591591
#!%s
592592
import os
593593
594-
print("Content-type: text/plain")
594+
print("X-ambv: was here")
595+
print("Content-type: text/html")
595596
print()
596-
print(repr(os.environ))
597+
print("<pre>")
598+
for k, v in os.environ.items():
599+
try:
600+
k.encode('ascii')
601+
v.encode('ascii')
602+
except UnicodeEncodeError:
603+
continue # see: BPO-44647
604+
print(f"{k}={v}")
605+
print("</pre>")
597606
"""
598607

599608

@@ -848,8 +857,8 @@ def test_accept(self):
848857
with self.subTest(headers):
849858
res = self.request('/cgi-bin/file6.py', 'GET', headers=headers)
850859
self.assertEqual(http.HTTPStatus.OK, res.status)
851-
expected = f"'HTTP_ACCEPT': {expected!r}"
852-
self.assertIn(expected.encode('ascii'), res.read())
860+
expected = f"HTTP_ACCEPT={expected}".encode('ascii')
861+
self.assertIn(expected, res.read())
853862

854863

855864
class SocketlessRequestHandler(SimpleHTTPRequestHandler):

0 commit comments

Comments
 (0)