Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6c6f851

Browse files
author
Victor Stinner
committed
Issue #9425: skip tests if a filename is not encodable
1 parent 87c9d6c commit 6c6f851

6 files changed

Lines changed: 29 additions & 4 deletions

File tree

Lib/test/test_import.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@ def test_import_initless_directory_warning(self):
291291

292292
def test_import_by_filename(self):
293293
path = os.path.abspath(TESTFN)
294+
encoding = sys.getfilesystemencoding()
295+
try:
296+
path.encode(encoding)
297+
except UnicodeEncodeError:
298+
self.skipTest('path is not encodable to {}'.format(encoding))
294299
with self.assertRaises(ImportError) as c:
295300
__import__(path)
296301
self.assertEqual("Import by filename is not supported.",

Lib/test/test_sax.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
TEST_XMLFILE = findfile("test.xml", subdir="xmltestdata")
2020
TEST_XMLFILE_OUT = findfile("test.xml.out", subdir="xmltestdata")
21+
try:
22+
TEST_XMLFILE.encode("utf8")
23+
TEST_XMLFILE_OUT.encode("utf8")
24+
except UnicodeEncodeError:
25+
raise unittest.SkipTest("filename is not encodable to utf8")
2126

2227
ns_uri = "http://www.python.org/xml-ns/saxtest/"
2328

Lib/test/test_sys.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,10 @@ def test_main_invalid_unicode(self):
509509
p = subprocess.Popen([sys.executable, "-c", code], stderr=subprocess.PIPE)
510510
stdout, stderr = p.communicate()
511511
self.assertEqual(p.returncode, 1)
512-
self.assert_(b"UnicodeEncodeError:" in stderr,
513-
"%r not in %s" % (b"UniodeEncodeError:", ascii(stderr)))
512+
self.assertIn(
513+
br"UnicodeEncodeError: 'utf-8' codec can't encode character "
514+
br"'\udcff' in position 7: surrogates not allowed",
515+
stderr)
514516

515517
def test_sys_flags(self):
516518
self.assertTrue(sys.flags)

Lib/test/test_urllib.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,12 @@ def tearDown(self):
232232
except: pass
233233

234234
def constructLocalFileUrl(self, filePath):
235-
return "file://%s" % urllib.request.pathname2url(
236-
os.path.abspath(filePath))
235+
filePath = os.path.abspath(filePath)
236+
try:
237+
filePath.encode("utf8")
238+
except UnicodeEncodeError:
239+
raise unittest.SkipTest("filePath is not encodable to utf8")
240+
return "file://%s" % urllib.request.pathname2url(filePath)
237241

238242
def createNewTempFile(self, data=b""):
239243
"""Creates a new temporary file containing the specified data,

Lib/test/test_urllib2.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,10 @@ def test_processors(self):
597597

598598

599599
def sanepathname2url(path):
600+
try:
601+
path.encode("utf8")
602+
except UnicodeEncodeError:
603+
raise unittest.SkipTest("path is not encodable to utf8")
600604
urlpath = urllib.request.pathname2url(path)
601605
if os.name == "nt" and urlpath.startswith("///"):
602606
urlpath = urlpath[2:]

Lib/test/test_xml_etree.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@
1313

1414
import sys
1515
import cgi
16+
import unittest
1617

1718
from test import support
1819
from test.support import findfile
1920

2021
from xml.etree import ElementTree as ET
2122

2223
SIMPLE_XMLFILE = findfile("simple.xml", subdir="xmltestdata")
24+
try:
25+
SIMPLE_XMLFILE.encode("utf8")
26+
except UnicodeEncodeError:
27+
raise unittest.SkipTest("filename is not encodable to utf8")
2328
SIMPLE_NS_XMLFILE = findfile("simple-ns.xml", subdir="xmltestdata")
2429

2530
SAMPLE_XML = """\

0 commit comments

Comments
 (0)