From 6c6f851eae204c3a4f85108c3d564133d58c887a Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 7 Aug 2010 10:09:35 +0000 Subject: Issue #9425: skip tests if a filename is not encodable --- Lib/test/test_import.py | 5 +++++ Lib/test/test_sax.py | 5 +++++ Lib/test/test_sys.py | 6 ++++-- Lib/test/test_urllib.py | 8 ++++++-- Lib/test/test_urllib2.py | 4 ++++ Lib/test/test_xml_etree.py | 5 +++++ 6 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py index 447c17f..11cf544 100644 --- a/Lib/test/test_import.py +++ b/Lib/test/test_import.py @@ -291,6 +291,11 @@ class ImportTests(unittest.TestCase): def test_import_by_filename(self): path = os.path.abspath(TESTFN) + encoding = sys.getfilesystemencoding() + try: + path.encode(encoding) + except UnicodeEncodeError: + self.skipTest('path is not encodable to {}'.format(encoding)) with self.assertRaises(ImportError) as c: __import__(path) self.assertEqual("Import by filename is not supported.", diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py index 9b28b3f..911e763 100644 --- a/Lib/test/test_sax.py +++ b/Lib/test/test_sax.py @@ -18,6 +18,11 @@ import unittest TEST_XMLFILE = findfile("test.xml", subdir="xmltestdata") TEST_XMLFILE_OUT = findfile("test.xml.out", subdir="xmltestdata") +try: + TEST_XMLFILE.encode("utf8") + TEST_XMLFILE_OUT.encode("utf8") +except UnicodeEncodeError: + raise unittest.SkipTest("filename is not encodable to utf8") ns_uri = "http://www.python.org/xml-ns/saxtest/" diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index eb5d877..244874d 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -509,8 +509,10 @@ class SysModuleTest(unittest.TestCase): p = subprocess.Popen([sys.executable, "-c", code], stderr=subprocess.PIPE) stdout, stderr = p.communicate() self.assertEqual(p.returncode, 1) - self.assert_(b"UnicodeEncodeError:" in stderr, - "%r not in %s" % (b"UniodeEncodeError:", ascii(stderr))) + self.assertIn( + br"UnicodeEncodeError: 'utf-8' codec can't encode character " + br"'\udcff' in position 7: surrogates not allowed", + stderr) def test_sys_flags(self): self.assertTrue(sys.flags) diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index 98308b6..2a9102c 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -232,8 +232,12 @@ class urlretrieve_FileTests(unittest.TestCase): except: pass def constructLocalFileUrl(self, filePath): - return "file://%s" % urllib.request.pathname2url( - os.path.abspath(filePath)) + filePath = os.path.abspath(filePath) + try: + filePath.encode("utf8") + except UnicodeEncodeError: + raise unittest.SkipTest("filePath is not encodable to utf8") + return "file://%s" % urllib.request.pathname2url(filePath) def createNewTempFile(self, data=b""): """Creates a new temporary file containing the specified data, diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 4d117c7..b2f7ea8 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -597,6 +597,10 @@ class OpenerDirectorTests(unittest.TestCase): def sanepathname2url(path): + try: + path.encode("utf8") + except UnicodeEncodeError: + raise unittest.SkipTest("path is not encodable to utf8") urlpath = urllib.request.pathname2url(path) if os.name == "nt" and urlpath.startswith("///"): urlpath = urlpath[2:] diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 249ac64..e7c8a89 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -13,6 +13,7 @@ import sys import cgi +import unittest from test import support from test.support import findfile @@ -20,6 +21,10 @@ from test.support import findfile from xml.etree import ElementTree as ET SIMPLE_XMLFILE = findfile("simple.xml", subdir="xmltestdata") +try: + SIMPLE_XMLFILE.encode("utf8") +except UnicodeEncodeError: + raise unittest.SkipTest("filename is not encodable to utf8") SIMPLE_NS_XMLFILE = findfile("simple-ns.xml", subdir="xmltestdata") SAMPLE_XML = """\ -- cgit v0.12