2010-06-17 03:44:09 +00:00
|
|
|
|
|
|
|
lumiere_nfc = u"lumi\u00E8re"
|
|
|
|
Artonwall_nfc = u"\u00C4rtonwall.mp3"
|
|
|
|
Artonwall_nfd = u"A\u0308rtonwall.mp3"
|
2010-05-20 00:41:05 +00:00
|
|
|
|
|
|
|
TEST_FILENAMES = (
|
2010-06-17 03:44:09 +00:00
|
|
|
Artonwall_nfc,
|
2010-05-20 00:41:05 +00:00
|
|
|
u'test_file',
|
|
|
|
u'Blah blah.txt',
|
|
|
|
)
|
|
|
|
|
|
|
|
# The following main helps to generate a test class for other operating
|
|
|
|
# systems.
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2010-07-18 01:34:35 +00:00
|
|
|
import sys, os
|
|
|
|
import tempfile
|
|
|
|
import shutil
|
2010-05-20 00:41:05 +00:00
|
|
|
import platform
|
2010-06-07 01:02:15 +00:00
|
|
|
|
2010-05-20 00:41:05 +00:00
|
|
|
if len(sys.argv) != 2:
|
2010-06-17 03:44:09 +00:00
|
|
|
print "Usage: %s lumi<e-grave>re" % sys.argv[0]
|
2010-05-20 00:41:05 +00:00
|
|
|
sys.exit(1)
|
2010-07-25 08:32:16 +00:00
|
|
|
|
|
|
|
if sys.platform == "win32":
|
|
|
|
try:
|
|
|
|
from allmydata.windows.fixups import initialize
|
|
|
|
except ImportError:
|
|
|
|
print "set PYTHONPATH to the src directory"
|
|
|
|
sys.exit(1)
|
|
|
|
initialize()
|
|
|
|
|
2010-05-20 00:41:05 +00:00
|
|
|
print
|
2010-07-18 01:34:35 +00:00
|
|
|
print "class MyWeirdOS(EncodingUtil, unittest.TestCase):"
|
2010-05-20 00:41:05 +00:00
|
|
|
print " uname = '%s'" % ' '.join(platform.uname())
|
2010-07-25 08:32:16 +00:00
|
|
|
print " argv = %s" % repr(sys.argv[1])
|
2010-05-20 00:41:05 +00:00
|
|
|
print " platform = '%s'" % sys.platform
|
2010-06-07 01:02:15 +00:00
|
|
|
print " filesystem_encoding = '%s'" % sys.getfilesystemencoding()
|
2011-06-29 18:53:56 +00:00
|
|
|
print " io_encoding = '%s'" % sys.stdout.encoding
|
2010-07-18 01:34:35 +00:00
|
|
|
try:
|
|
|
|
tmpdir = tempfile.mkdtemp()
|
|
|
|
for fname in TEST_FILENAMES:
|
2011-08-09 00:11:17 +00:00
|
|
|
open(os.path.join(tmpdir, fname), 'w').close()
|
2010-07-18 01:34:35 +00:00
|
|
|
|
|
|
|
# Use Unicode API under Windows or MacOS X
|
|
|
|
if sys.platform in ('win32', 'darwin'):
|
|
|
|
dirlist = os.listdir(unicode(tmpdir))
|
|
|
|
else:
|
|
|
|
dirlist = os.listdir(tmpdir)
|
|
|
|
|
|
|
|
print " dirlist = %s" % repr(dirlist)
|
|
|
|
except:
|
|
|
|
print " # Oops, I cannot write filenames containing non-ascii characters"
|
2010-05-20 00:41:05 +00:00
|
|
|
print
|
|
|
|
|
2010-07-18 01:34:35 +00:00
|
|
|
shutil.rmtree(tmpdir)
|
2010-05-20 00:41:05 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
2015-07-17 21:03:53 +00:00
|
|
|
|
2010-07-18 01:34:35 +00:00
|
|
|
import os, sys, locale
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2015-07-17 21:03:53 +00:00
|
|
|
from twisted.trial import unittest
|
|
|
|
|
2015-11-02 21:43:41 +00:00
|
|
|
from twisted.python.filepath import FilePath
|
|
|
|
|
2010-06-07 01:02:15 +00:00
|
|
|
from allmydata.test.common_util import ReallyEqualMixin
|
2015-01-30 00:50:18 +00:00
|
|
|
from allmydata.util import encodingutil, fileutil
|
2010-07-12 00:30:15 +00:00
|
|
|
from allmydata.util.encodingutil import argv_to_unicode, unicode_to_url, \
|
2015-01-30 00:04:11 +00:00
|
|
|
unicode_to_output, quote_output, quote_path, quote_local_unicode_path, \
|
2015-11-02 21:43:41 +00:00
|
|
|
quote_filepath, unicode_platform, listdir_unicode, FilenameEncodingError, \
|
|
|
|
get_io_encoding, get_filesystem_encoding, to_str, from_utf8_or_none, _reload, \
|
|
|
|
to_filepath, extend_filepath, unicode_from_filepath, unicode_segments_from
|
2010-07-18 01:34:35 +00:00
|
|
|
from allmydata.dirnode import normalize
|
2010-06-07 01:02:15 +00:00
|
|
|
|
2010-05-20 00:41:05 +00:00
|
|
|
from twisted.python import usage
|
|
|
|
|
2015-07-17 21:03:53 +00:00
|
|
|
|
|
|
|
class MockStdout(object):
|
|
|
|
pass
|
|
|
|
|
2010-07-18 01:34:35 +00:00
|
|
|
class EncodingUtilErrors(ReallyEqualMixin, unittest.TestCase):
|
2015-07-17 21:03:53 +00:00
|
|
|
def test_get_io_encoding(self):
|
|
|
|
mock_stdout = MockStdout()
|
|
|
|
self.patch(sys, 'stdout', mock_stdout)
|
2010-06-07 01:02:15 +00:00
|
|
|
|
|
|
|
mock_stdout.encoding = 'UTF-8'
|
|
|
|
_reload()
|
2011-06-29 18:53:56 +00:00
|
|
|
self.failUnlessReallyEqual(get_io_encoding(), 'utf-8')
|
2010-06-07 01:02:15 +00:00
|
|
|
|
|
|
|
mock_stdout.encoding = 'cp65001'
|
|
|
|
_reload()
|
2011-06-29 18:53:56 +00:00
|
|
|
self.failUnlessReallyEqual(get_io_encoding(), 'utf-8')
|
2010-06-07 01:02:15 +00:00
|
|
|
|
|
|
|
mock_stdout.encoding = 'koi8-r'
|
2010-07-25 08:32:16 +00:00
|
|
|
expected = sys.platform == "win32" and 'utf-8' or 'koi8-r'
|
2010-06-07 01:02:15 +00:00
|
|
|
_reload()
|
2011-06-29 18:53:56 +00:00
|
|
|
self.failUnlessReallyEqual(get_io_encoding(), expected)
|
2010-06-07 01:02:15 +00:00
|
|
|
|
|
|
|
mock_stdout.encoding = 'nonexistent_encoding'
|
2010-07-25 08:32:16 +00:00
|
|
|
if sys.platform == "win32":
|
|
|
|
_reload()
|
2011-06-29 18:53:56 +00:00
|
|
|
self.failUnlessReallyEqual(get_io_encoding(), 'utf-8')
|
2010-07-25 08:32:16 +00:00
|
|
|
else:
|
|
|
|
self.failUnlessRaises(AssertionError, _reload)
|
2010-06-04 14:12:51 +00:00
|
|
|
|
2015-07-17 21:03:53 +00:00
|
|
|
def test_get_io_encoding_not_from_stdout(self):
|
|
|
|
preferredencoding = 'koi8-r'
|
|
|
|
def call_locale_getpreferredencoding():
|
|
|
|
return preferredencoding
|
|
|
|
self.patch(locale, 'getpreferredencoding', call_locale_getpreferredencoding)
|
|
|
|
mock_stdout = MockStdout()
|
|
|
|
self.patch(sys, 'stdout', mock_stdout)
|
2010-07-17 04:58:16 +00:00
|
|
|
|
2015-07-17 21:03:53 +00:00
|
|
|
expected = sys.platform == "win32" and 'utf-8' or 'koi8-r'
|
|
|
|
_reload()
|
|
|
|
self.failUnlessReallyEqual(get_io_encoding(), expected)
|
2010-07-17 04:58:16 +00:00
|
|
|
|
2015-07-17 21:03:53 +00:00
|
|
|
mock_stdout.encoding = None
|
|
|
|
_reload()
|
|
|
|
self.failUnlessReallyEqual(get_io_encoding(), expected)
|
2010-07-17 04:58:16 +00:00
|
|
|
|
2015-07-17 21:03:53 +00:00
|
|
|
preferredencoding = None
|
|
|
|
_reload()
|
|
|
|
self.failUnlessReallyEqual(get_io_encoding(), 'utf-8')
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2010-07-25 08:32:16 +00:00
|
|
|
def test_argv_to_unicode(self):
|
2011-06-29 18:53:56 +00:00
|
|
|
encodingutil.io_encoding = 'utf-8'
|
2010-05-20 00:41:05 +00:00
|
|
|
self.failUnlessRaises(usage.UsageError,
|
|
|
|
argv_to_unicode,
|
2010-06-17 03:44:09 +00:00
|
|
|
lumiere_nfc.encode('latin1'))
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2010-07-25 08:32:16 +00:00
|
|
|
def test_unicode_to_output(self):
|
2011-06-29 18:53:56 +00:00
|
|
|
encodingutil.io_encoding = 'koi8-r'
|
2010-06-17 03:44:09 +00:00
|
|
|
self.failUnlessRaises(UnicodeEncodeError, unicode_to_output, lumiere_nfc)
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2015-07-17 21:03:53 +00:00
|
|
|
def test_no_unicode_normalization(self):
|
2010-07-18 01:34:35 +00:00
|
|
|
# Pretend to run on a Unicode platform.
|
2015-07-17 21:03:53 +00:00
|
|
|
# listdir_unicode normalized to NFC in 1.7beta, but now doesn't.
|
|
|
|
|
|
|
|
def call_os_listdir(path):
|
|
|
|
return [Artonwall_nfd]
|
|
|
|
self.patch(os, 'listdir', call_os_listdir)
|
|
|
|
self.patch(sys, 'platform', 'darwin')
|
|
|
|
|
|
|
|
_reload()
|
|
|
|
self.failUnlessReallyEqual(listdir_unicode(u'/dummy'), [Artonwall_nfd])
|
|
|
|
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2010-07-18 01:34:35 +00:00
|
|
|
# The following tests apply only to platforms that don't store filenames as
|
|
|
|
# Unicode entities on the filesystem.
|
|
|
|
class EncodingUtilNonUnicodePlatform(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
# Mock sys.platform because unicode_platform() uses it
|
|
|
|
self.original_platform = sys.platform
|
|
|
|
sys.platform = 'linux'
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
sys.platform = self.original_platform
|
|
|
|
_reload()
|
|
|
|
|
2015-07-17 21:03:53 +00:00
|
|
|
def test_listdir_unicode(self):
|
2010-07-18 01:34:35 +00:00
|
|
|
# What happens if latin1-encoded filenames are encountered on an UTF-8
|
|
|
|
# filesystem?
|
2015-07-17 21:03:53 +00:00
|
|
|
def call_os_listdir(path):
|
|
|
|
return [
|
|
|
|
lumiere_nfc.encode('utf-8'),
|
|
|
|
lumiere_nfc.encode('latin1')
|
|
|
|
]
|
|
|
|
self.patch(os, 'listdir', call_os_listdir)
|
|
|
|
|
|
|
|
sys_filesystemencoding = 'utf-8'
|
|
|
|
def call_sys_getfilesystemencoding():
|
|
|
|
return sys_filesystemencoding
|
|
|
|
self.patch(sys, 'getfilesystemencoding', call_sys_getfilesystemencoding)
|
2010-07-18 01:34:35 +00:00
|
|
|
|
|
|
|
_reload()
|
|
|
|
self.failUnlessRaises(FilenameEncodingError,
|
|
|
|
listdir_unicode,
|
|
|
|
u'/dummy')
|
2011-08-09 00:11:17 +00:00
|
|
|
|
2010-07-18 01:34:35 +00:00
|
|
|
# We're trying to list a directory whose name cannot be represented in
|
|
|
|
# the filesystem encoding. This should fail.
|
2015-07-17 21:03:53 +00:00
|
|
|
sys_filesystemencoding = 'ascii'
|
2010-07-18 01:34:35 +00:00
|
|
|
_reload()
|
|
|
|
self.failUnlessRaises(FilenameEncodingError,
|
|
|
|
listdir_unicode,
|
|
|
|
u'/' + lumiere_nfc)
|
|
|
|
|
2010-07-25 08:32:16 +00:00
|
|
|
|
2010-07-18 01:34:35 +00:00
|
|
|
class EncodingUtil(ReallyEqualMixin):
|
2010-05-20 00:41:05 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.original_platform = sys.platform
|
|
|
|
sys.platform = self.platform
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
sys.platform = self.original_platform
|
2010-06-07 01:02:15 +00:00
|
|
|
_reload()
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2015-07-17 21:03:53 +00:00
|
|
|
def test_argv_to_unicode(self):
|
2010-05-20 00:41:05 +00:00
|
|
|
if 'argv' not in dir(self):
|
2010-06-07 01:02:15 +00:00
|
|
|
return
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2015-07-17 21:03:53 +00:00
|
|
|
mock_stdout = MockStdout()
|
|
|
|
mock_stdout.encoding = self.io_encoding
|
|
|
|
self.patch(sys, 'stdout', mock_stdout)
|
|
|
|
|
2010-06-17 03:44:09 +00:00
|
|
|
argu = lumiere_nfc
|
2010-05-20 00:41:05 +00:00
|
|
|
argv = self.argv
|
2010-06-07 01:02:15 +00:00
|
|
|
_reload()
|
|
|
|
self.failUnlessReallyEqual(argv_to_unicode(argv), argu)
|
2010-05-20 00:41:05 +00:00
|
|
|
|
|
|
|
def test_unicode_to_url(self):
|
2010-06-17 03:44:09 +00:00
|
|
|
self.failUnless(unicode_to_url(lumiere_nfc), "lumi\xc3\xa8re")
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2015-07-17 21:03:53 +00:00
|
|
|
def test_unicode_to_output(self):
|
2010-07-25 08:32:16 +00:00
|
|
|
if 'argv' not in dir(self):
|
2010-06-07 01:02:15 +00:00
|
|
|
return
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2015-07-17 21:03:53 +00:00
|
|
|
mock_stdout = MockStdout()
|
|
|
|
mock_stdout.encoding = self.io_encoding
|
|
|
|
self.patch(sys, 'stdout', mock_stdout)
|
|
|
|
|
2010-06-07 01:02:15 +00:00
|
|
|
_reload()
|
2010-07-25 08:32:16 +00:00
|
|
|
self.failUnlessReallyEqual(unicode_to_output(lumiere_nfc), self.argv)
|
2010-05-20 00:41:05 +00:00
|
|
|
|
|
|
|
def test_unicode_platform(self):
|
|
|
|
matrix = {
|
|
|
|
'linux2': False,
|
2011-09-15 20:26:20 +00:00
|
|
|
'linux3': False,
|
2010-05-21 14:00:53 +00:00
|
|
|
'openbsd4': False,
|
2010-05-20 00:41:05 +00:00
|
|
|
'win32': True,
|
|
|
|
'darwin': True,
|
|
|
|
}
|
|
|
|
|
2010-06-07 01:02:15 +00:00
|
|
|
_reload()
|
|
|
|
self.failUnlessReallyEqual(unicode_platform(), matrix[self.platform])
|
2011-08-09 00:11:17 +00:00
|
|
|
|
2015-07-17 21:03:53 +00:00
|
|
|
def test_listdir_unicode(self):
|
2010-07-18 01:34:35 +00:00
|
|
|
if 'dirlist' not in dir(self):
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
u"test".encode(self.filesystem_encoding)
|
|
|
|
except (LookupError, AttributeError):
|
|
|
|
raise unittest.SkipTest("This platform does not support the '%s' filesystem encoding "
|
|
|
|
"that we are testing for the benefit of a different platform."
|
|
|
|
% (self.filesystem_encoding,))
|
|
|
|
|
2015-07-17 21:03:53 +00:00
|
|
|
def call_os_listdir(path):
|
|
|
|
return self.dirlist
|
|
|
|
self.patch(os, 'listdir', call_os_listdir)
|
|
|
|
|
|
|
|
def call_sys_getfilesystemencoding():
|
|
|
|
return self.filesystem_encoding
|
|
|
|
self.patch(sys, 'getfilesystemencoding', call_sys_getfilesystemencoding)
|
2010-07-18 01:34:35 +00:00
|
|
|
|
|
|
|
_reload()
|
|
|
|
filenames = listdir_unicode(u'/dummy')
|
|
|
|
|
|
|
|
self.failUnlessEqual(set([normalize(fname) for fname in filenames]),
|
|
|
|
set(TEST_FILENAMES))
|
|
|
|
|
|
|
|
|
|
|
|
class StdlibUnicode(unittest.TestCase):
|
|
|
|
"""This mainly tests that some of the stdlib functions support Unicode paths, but also that
|
|
|
|
listdir_unicode works for valid filenames."""
|
|
|
|
|
|
|
|
def skip_if_cannot_represent_filename(self, u):
|
|
|
|
enc = get_filesystem_encoding()
|
|
|
|
if not unicode_platform():
|
|
|
|
try:
|
|
|
|
u.encode(enc)
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
raise unittest.SkipTest("A non-ASCII filename could not be encoded on this platform.")
|
|
|
|
|
|
|
|
def test_mkdir_open_exists_abspath_listdir_expanduser(self):
|
|
|
|
self.skip_if_cannot_represent_filename(lumiere_nfc)
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.mkdir(lumiere_nfc)
|
|
|
|
except EnvironmentError, e:
|
|
|
|
raise unittest.SkipTest("%r\nIt is possible that the filesystem on which this test is being run "
|
|
|
|
"does not support Unicode, even though the platform does." % (e,))
|
|
|
|
|
2010-07-22 00:14:18 +00:00
|
|
|
fn = lumiere_nfc + u'/' + lumiere_nfc + u'.txt'
|
2010-07-18 01:34:35 +00:00
|
|
|
open(fn, 'wb').close()
|
|
|
|
self.failUnless(os.path.exists(fn))
|
2010-07-22 00:14:18 +00:00
|
|
|
self.failUnless(os.path.exists(os.path.join(os.getcwdu(), fn)))
|
2010-07-18 01:34:35 +00:00
|
|
|
filenames = listdir_unicode(lumiere_nfc)
|
|
|
|
|
|
|
|
# We only require that the listing includes a filename that is canonically equivalent
|
|
|
|
# to lumiere_nfc (on Mac OS X, it will be the NFD equivalent).
|
|
|
|
self.failUnlessIn(lumiere_nfc + ".txt", set([normalize(fname) for fname in filenames]))
|
|
|
|
|
2015-01-30 00:50:18 +00:00
|
|
|
expanded = fileutil.expanduser(u"~/" + lumiere_nfc)
|
|
|
|
self.failIfIn(u"~", expanded)
|
2010-07-18 01:34:35 +00:00
|
|
|
self.failUnless(expanded.endswith(lumiere_nfc), expanded)
|
|
|
|
|
2010-07-18 15:14:20 +00:00
|
|
|
def test_open_unrepresentable(self):
|
2010-07-18 01:34:35 +00:00
|
|
|
if unicode_platform():
|
|
|
|
raise unittest.SkipTest("This test is not applicable to platforms that represent filenames as Unicode.")
|
|
|
|
|
2010-07-18 03:03:33 +00:00
|
|
|
enc = get_filesystem_encoding()
|
|
|
|
fn = u'\u2621.txt'
|
|
|
|
try:
|
|
|
|
fn.encode(enc)
|
|
|
|
raise unittest.SkipTest("This test cannot be run unless we know a filename that is not representable.")
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
self.failUnlessRaises(UnicodeEncodeError, open, fn, 'wb')
|
2010-07-18 01:34:35 +00:00
|
|
|
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2010-07-23 07:53:14 +00:00
|
|
|
class QuoteOutput(ReallyEqualMixin, unittest.TestCase):
|
2010-07-25 08:32:16 +00:00
|
|
|
def tearDown(self):
|
|
|
|
_reload()
|
|
|
|
|
2013-01-04 05:31:07 +00:00
|
|
|
def _check(self, inp, out, enc, optional_quotes, quote_newlines):
|
2010-07-23 07:53:14 +00:00
|
|
|
out2 = out
|
|
|
|
if optional_quotes:
|
|
|
|
out2 = out2[1:-1]
|
2013-01-04 05:31:07 +00:00
|
|
|
self.failUnlessReallyEqual(quote_output(inp, encoding=enc, quote_newlines=quote_newlines), out)
|
|
|
|
self.failUnlessReallyEqual(quote_output(inp, encoding=enc, quotemarks=False, quote_newlines=quote_newlines), out2)
|
2010-07-25 08:32:16 +00:00
|
|
|
if out[0:2] == 'b"':
|
|
|
|
pass
|
|
|
|
elif isinstance(inp, str):
|
2013-01-04 05:31:07 +00:00
|
|
|
self.failUnlessReallyEqual(quote_output(unicode(inp), encoding=enc, quote_newlines=quote_newlines), out)
|
|
|
|
self.failUnlessReallyEqual(quote_output(unicode(inp), encoding=enc, quotemarks=False, quote_newlines=quote_newlines), out2)
|
2010-07-25 08:32:16 +00:00
|
|
|
else:
|
2013-01-04 05:31:07 +00:00
|
|
|
self.failUnlessReallyEqual(quote_output(inp.encode('utf-8'), encoding=enc, quote_newlines=quote_newlines), out)
|
|
|
|
self.failUnlessReallyEqual(quote_output(inp.encode('utf-8'), encoding=enc, quotemarks=False, quote_newlines=quote_newlines), out2)
|
2010-07-23 07:53:14 +00:00
|
|
|
|
|
|
|
def _test_quote_output_all(self, enc):
|
2013-01-04 05:31:07 +00:00
|
|
|
def check(inp, out, optional_quotes=False, quote_newlines=None):
|
|
|
|
self._check(inp, out, enc, optional_quotes, quote_newlines)
|
2010-07-23 07:53:14 +00:00
|
|
|
|
|
|
|
# optional single quotes
|
|
|
|
check("foo", "'foo'", True)
|
|
|
|
check("\\", "'\\'", True)
|
|
|
|
check("$\"`", "'$\"`'", True)
|
2013-01-04 05:31:07 +00:00
|
|
|
check("\n", "'\n'", True, quote_newlines=False)
|
2010-07-23 07:53:14 +00:00
|
|
|
|
|
|
|
# mandatory single quotes
|
|
|
|
check("\"", "'\"'")
|
|
|
|
|
|
|
|
# double quotes
|
|
|
|
check("'", "\"'\"")
|
2013-01-04 05:31:07 +00:00
|
|
|
check("\n", "\"\\x0a\"", quote_newlines=True)
|
2010-07-23 07:53:14 +00:00
|
|
|
check("\x00", "\"\\x00\"")
|
|
|
|
|
|
|
|
# invalid Unicode and astral planes
|
|
|
|
check(u"\uFDD0\uFDEF", "\"\\ufdd0\\ufdef\"")
|
|
|
|
check(u"\uDC00\uD800", "\"\\udc00\\ud800\"")
|
|
|
|
check(u"\uDC00\uD800\uDC00", "\"\\udc00\\U00010000\"")
|
|
|
|
check(u"\uD800\uDC00", "\"\\U00010000\"")
|
|
|
|
check(u"\uD800\uDC01", "\"\\U00010001\"")
|
|
|
|
check(u"\uD801\uDC00", "\"\\U00010400\"")
|
|
|
|
check(u"\uDBFF\uDFFF", "\"\\U0010ffff\"")
|
|
|
|
check(u"'\uDBFF\uDFFF", "\"'\\U0010ffff\"")
|
|
|
|
check(u"\"\uDBFF\uDFFF", "\"\\\"\\U0010ffff\"")
|
|
|
|
|
|
|
|
# invalid UTF-8
|
|
|
|
check("\xFF", "b\"\\xff\"")
|
|
|
|
check("\x00\"$\\`\x80\xFF", "b\"\\x00\\\"\\$\\\\\\`\\x80\\xff\"")
|
|
|
|
|
|
|
|
def test_quote_output_ascii(self, enc='ascii'):
|
2013-01-04 05:31:07 +00:00
|
|
|
def check(inp, out, optional_quotes=False, quote_newlines=None):
|
|
|
|
self._check(inp, out, enc, optional_quotes, quote_newlines)
|
2010-07-23 07:53:14 +00:00
|
|
|
|
|
|
|
self._test_quote_output_all(enc)
|
|
|
|
check(u"\u00D7", "\"\\xd7\"")
|
|
|
|
check(u"'\u00D7", "\"'\\xd7\"")
|
|
|
|
check(u"\"\u00D7", "\"\\\"\\xd7\"")
|
|
|
|
check(u"\u2621", "\"\\u2621\"")
|
|
|
|
check(u"'\u2621", "\"'\\u2621\"")
|
|
|
|
check(u"\"\u2621", "\"\\\"\\u2621\"")
|
2013-01-04 05:31:07 +00:00
|
|
|
check(u"\n", "'\n'", True, quote_newlines=False)
|
|
|
|
check(u"\n", "\"\\x0a\"", quote_newlines=True)
|
2010-07-23 07:53:14 +00:00
|
|
|
|
|
|
|
def test_quote_output_latin1(self, enc='latin1'):
|
2013-01-04 05:31:07 +00:00
|
|
|
def check(inp, out, optional_quotes=False, quote_newlines=None):
|
|
|
|
self._check(inp, out.encode('latin1'), enc, optional_quotes, quote_newlines)
|
2010-07-23 07:53:14 +00:00
|
|
|
|
|
|
|
self._test_quote_output_all(enc)
|
|
|
|
check(u"\u00D7", u"'\u00D7'", True)
|
|
|
|
check(u"'\u00D7", u"\"'\u00D7\"")
|
|
|
|
check(u"\"\u00D7", u"'\"\u00D7'")
|
|
|
|
check(u"\u00D7\"", u"'\u00D7\"'", True)
|
|
|
|
check(u"\u2621", u"\"\\u2621\"")
|
|
|
|
check(u"'\u2621", u"\"'\\u2621\"")
|
|
|
|
check(u"\"\u2621", u"\"\\\"\\u2621\"")
|
2013-01-04 05:31:07 +00:00
|
|
|
check(u"\n", u"'\n'", True, quote_newlines=False)
|
|
|
|
check(u"\n", u"\"\\x0a\"", quote_newlines=True)
|
2010-07-23 07:53:14 +00:00
|
|
|
|
|
|
|
def test_quote_output_utf8(self, enc='utf-8'):
|
2013-01-04 05:31:07 +00:00
|
|
|
def check(inp, out, optional_quotes=False, quote_newlines=None):
|
|
|
|
self._check(inp, out.encode('utf-8'), enc, optional_quotes, quote_newlines)
|
2010-07-23 07:53:14 +00:00
|
|
|
|
|
|
|
self._test_quote_output_all(enc)
|
|
|
|
check(u"\u2621", u"'\u2621'", True)
|
|
|
|
check(u"'\u2621", u"\"'\u2621\"")
|
|
|
|
check(u"\"\u2621", u"'\"\u2621'")
|
|
|
|
check(u"\u2621\"", u"'\u2621\"'", True)
|
2013-01-04 05:31:07 +00:00
|
|
|
check(u"\n", u"'\n'", True, quote_newlines=False)
|
|
|
|
check(u"\n", u"\"\\x0a\"", quote_newlines=True)
|
2010-07-23 07:53:14 +00:00
|
|
|
|
2010-07-25 08:32:16 +00:00
|
|
|
def test_quote_output_default(self):
|
2015-12-28 20:34:41 +00:00
|
|
|
self.patch(encodingutil, 'io_encoding', 'ascii')
|
2010-07-23 07:53:14 +00:00
|
|
|
self.test_quote_output_ascii(None)
|
|
|
|
|
2015-12-28 20:34:41 +00:00
|
|
|
self.patch(encodingutil, 'io_encoding', 'latin1')
|
2010-07-23 07:53:14 +00:00
|
|
|
self.test_quote_output_latin1(None)
|
|
|
|
|
2015-12-28 20:34:41 +00:00
|
|
|
self.patch(encodingutil, 'io_encoding', 'utf-8')
|
2010-07-23 07:53:14 +00:00
|
|
|
self.test_quote_output_utf8(None)
|
|
|
|
|
2015-01-30 00:04:11 +00:00
|
|
|
|
2015-11-02 21:43:41 +00:00
|
|
|
def win32_other(win32, other):
|
|
|
|
return win32 if sys.platform == "win32" else other
|
|
|
|
|
2015-01-30 00:04:11 +00:00
|
|
|
class QuotePaths(ReallyEqualMixin, unittest.TestCase):
|
|
|
|
def test_quote_path(self):
|
|
|
|
self.failUnlessReallyEqual(quote_path([u'foo', u'bar']), "'foo/bar'")
|
|
|
|
self.failUnlessReallyEqual(quote_path([u'foo', u'bar'], quotemarks=True), "'foo/bar'")
|
2015-02-05 02:13:23 +00:00
|
|
|
self.failUnlessReallyEqual(quote_path([u'foo', u'bar'], quotemarks=False), "foo/bar")
|
2015-01-30 00:04:11 +00:00
|
|
|
self.failUnlessReallyEqual(quote_path([u'foo', u'\nbar']), '"foo/\\x0abar"')
|
|
|
|
self.failUnlessReallyEqual(quote_path([u'foo', u'\nbar'], quotemarks=True), '"foo/\\x0abar"')
|
2015-02-05 02:13:23 +00:00
|
|
|
self.failUnlessReallyEqual(quote_path([u'foo', u'\nbar'], quotemarks=False), '"foo/\\x0abar"')
|
2015-01-30 00:04:11 +00:00
|
|
|
|
|
|
|
self.failUnlessReallyEqual(quote_local_unicode_path(u"\\\\?\\C:\\foo"),
|
2015-02-05 02:22:27 +00:00
|
|
|
win32_other("'C:\\foo'", "'\\\\?\\C:\\foo'"))
|
|
|
|
self.failUnlessReallyEqual(quote_local_unicode_path(u"\\\\?\\C:\\foo", quotemarks=True),
|
|
|
|
win32_other("'C:\\foo'", "'\\\\?\\C:\\foo'"))
|
|
|
|
self.failUnlessReallyEqual(quote_local_unicode_path(u"\\\\?\\C:\\foo", quotemarks=False),
|
|
|
|
win32_other("C:\\foo", "\\\\?\\C:\\foo"))
|
2015-01-30 00:04:11 +00:00
|
|
|
self.failUnlessReallyEqual(quote_local_unicode_path(u"\\\\?\\UNC\\foo\\bar"),
|
2015-02-05 02:22:27 +00:00
|
|
|
win32_other("'\\\\foo\\bar'", "'\\\\?\\UNC\\foo\\bar'"))
|
|
|
|
self.failUnlessReallyEqual(quote_local_unicode_path(u"\\\\?\\UNC\\foo\\bar", quotemarks=True),
|
|
|
|
win32_other("'\\\\foo\\bar'", "'\\\\?\\UNC\\foo\\bar'"))
|
|
|
|
self.failUnlessReallyEqual(quote_local_unicode_path(u"\\\\?\\UNC\\foo\\bar", quotemarks=False),
|
|
|
|
win32_other("\\\\foo\\bar", "\\\\?\\UNC\\foo\\bar"))
|
2015-01-30 00:04:11 +00:00
|
|
|
|
2015-11-02 21:43:41 +00:00
|
|
|
def test_quote_filepath(self):
|
|
|
|
foo_bar_fp = FilePath(win32_other(u'C:\\foo\\bar', u'/foo/bar'))
|
|
|
|
self.failUnlessReallyEqual(quote_filepath(foo_bar_fp),
|
|
|
|
win32_other("'C:\\foo\\bar'", "'/foo/bar'"))
|
|
|
|
self.failUnlessReallyEqual(quote_filepath(foo_bar_fp, quotemarks=True),
|
|
|
|
win32_other("'C:\\foo\\bar'", "'/foo/bar'"))
|
|
|
|
self.failUnlessReallyEqual(quote_filepath(foo_bar_fp, quotemarks=False),
|
|
|
|
win32_other("C:\\foo\\bar", "/foo/bar"))
|
|
|
|
|
2015-11-02 22:00:31 +00:00
|
|
|
foo_longfp = FilePath(u'\\\\?\\C:\\foo')
|
|
|
|
self.failUnlessReallyEqual(quote_filepath(foo_longfp),
|
|
|
|
win32_other("'C:\\foo'", "'\\\\?\\C:\\foo'"))
|
|
|
|
self.failUnlessReallyEqual(quote_filepath(foo_longfp, quotemarks=True),
|
|
|
|
win32_other("'C:\\foo'", "'\\\\?\\C:\\foo'"))
|
|
|
|
self.failUnlessReallyEqual(quote_filepath(foo_longfp, quotemarks=False),
|
|
|
|
win32_other("C:\\foo", "\\\\?\\C:\\foo"))
|
|
|
|
|
2015-11-02 21:43:41 +00:00
|
|
|
|
|
|
|
class FilePaths(ReallyEqualMixin, unittest.TestCase):
|
|
|
|
def test_to_filepath(self):
|
|
|
|
foo_u = win32_other(u'C:\\foo', u'/foo')
|
|
|
|
|
|
|
|
nosep_fp = to_filepath(foo_u)
|
|
|
|
sep_fp = to_filepath(foo_u + os.path.sep)
|
|
|
|
|
|
|
|
for fp in (nosep_fp, sep_fp):
|
|
|
|
self.failUnlessReallyEqual(fp, FilePath(foo_u))
|
|
|
|
self.failUnlessReallyEqual(fp.path, foo_u)
|
|
|
|
|
|
|
|
if sys.platform == "win32":
|
|
|
|
long_u = u'\\\\?\\C:\\foo'
|
|
|
|
longfp = to_filepath(long_u + u'\\')
|
|
|
|
self.failUnlessReallyEqual(longfp, FilePath(long_u))
|
|
|
|
self.failUnlessReallyEqual(longfp.path, long_u)
|
|
|
|
|
|
|
|
def test_extend_filepath(self):
|
|
|
|
foo_bfp = FilePath(win32_other(b'C:\\foo', b'/foo'))
|
|
|
|
foo_ufp = FilePath(win32_other(u'C:\\foo', u'/foo'))
|
|
|
|
foo_bar_baz_u = win32_other(u'C:\\foo\\bar\\baz', u'/foo/bar/baz')
|
|
|
|
|
|
|
|
for foo_fp in (foo_bfp, foo_ufp):
|
|
|
|
fp = extend_filepath(foo_fp, [u'bar', u'baz'])
|
|
|
|
self.failUnlessReallyEqual(fp, FilePath(foo_bar_baz_u))
|
|
|
|
self.failUnlessReallyEqual(fp.path, foo_bar_baz_u)
|
|
|
|
|
|
|
|
def test_unicode_from_filepath(self):
|
|
|
|
foo_bfp = FilePath(win32_other(b'C:\\foo', b'/foo'))
|
|
|
|
foo_ufp = FilePath(win32_other(u'C:\\foo', u'/foo'))
|
|
|
|
foo_u = win32_other(u'C:\\foo', u'/foo')
|
|
|
|
|
|
|
|
for foo_fp in (foo_bfp, foo_ufp):
|
|
|
|
self.failUnlessReallyEqual(unicode_from_filepath(foo_fp), foo_u)
|
|
|
|
|
|
|
|
def test_unicode_segments_from(self):
|
|
|
|
foo_bfp = FilePath(win32_other(b'C:\\foo', b'/foo'))
|
|
|
|
foo_ufp = FilePath(win32_other(u'C:\\foo', u'/foo'))
|
|
|
|
foo_bar_baz_bfp = FilePath(win32_other(b'C:\\foo\\bar\\baz', b'/foo/bar/baz'))
|
|
|
|
foo_bar_baz_ufp = FilePath(win32_other(u'C:\\foo\\bar\\baz', u'/foo/bar/baz'))
|
|
|
|
|
|
|
|
for foo_fp in (foo_bfp, foo_ufp):
|
|
|
|
for foo_bar_baz_fp in (foo_bar_baz_bfp, foo_bar_baz_ufp):
|
|
|
|
self.failUnlessReallyEqual(unicode_segments_from(foo_bar_baz_fp, foo_fp),
|
|
|
|
[u'bar', u'baz'])
|
|
|
|
|
2010-07-23 07:53:14 +00:00
|
|
|
|
2010-07-18 01:34:35 +00:00
|
|
|
class UbuntuKarmicUTF8(EncodingUtil, unittest.TestCase):
|
2010-05-20 00:41:05 +00:00
|
|
|
uname = 'Linux korn 2.6.31-14-generic #48-Ubuntu SMP Fri Oct 16 14:05:01 UTC 2009 x86_64'
|
|
|
|
argv = 'lumi\xc3\xa8re'
|
|
|
|
platform = 'linux2'
|
2010-06-07 01:02:15 +00:00
|
|
|
filesystem_encoding = 'UTF-8'
|
2011-06-29 18:53:56 +00:00
|
|
|
io_encoding = 'UTF-8'
|
2010-07-18 01:34:35 +00:00
|
|
|
dirlist = ['test_file', '\xc3\x84rtonwall.mp3', 'Blah blah.txt']
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2010-07-18 01:34:35 +00:00
|
|
|
class UbuntuKarmicLatin1(EncodingUtil, unittest.TestCase):
|
2010-05-20 00:41:05 +00:00
|
|
|
uname = 'Linux korn 2.6.31-14-generic #48-Ubuntu SMP Fri Oct 16 14:05:01 UTC 2009 x86_64'
|
|
|
|
argv = 'lumi\xe8re'
|
|
|
|
platform = 'linux2'
|
2010-06-07 01:02:15 +00:00
|
|
|
filesystem_encoding = 'ISO-8859-1'
|
2011-06-29 18:53:56 +00:00
|
|
|
io_encoding = 'ISO-8859-1'
|
2010-07-18 01:34:35 +00:00
|
|
|
dirlist = ['test_file', 'Blah blah.txt', '\xc4rtonwall.mp3']
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2010-07-25 08:32:16 +00:00
|
|
|
class Windows(EncodingUtil, unittest.TestCase):
|
2010-05-20 00:41:05 +00:00
|
|
|
uname = 'Windows XP 5.1.2600 x86 x86 Family 15 Model 75 Step ping 2, AuthenticAMD'
|
2010-07-25 08:32:16 +00:00
|
|
|
argv = 'lumi\xc3\xa8re'
|
2010-05-20 00:41:05 +00:00
|
|
|
platform = 'win32'
|
2010-06-07 01:02:15 +00:00
|
|
|
filesystem_encoding = 'mbcs'
|
2011-06-29 18:53:56 +00:00
|
|
|
io_encoding = 'utf-8'
|
2010-07-18 01:34:35 +00:00
|
|
|
dirlist = [u'Blah blah.txt', u'test_file', u'\xc4rtonwall.mp3']
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2010-07-18 01:34:35 +00:00
|
|
|
class MacOSXLeopard(EncodingUtil, unittest.TestCase):
|
2010-05-20 00:41:05 +00:00
|
|
|
uname = 'Darwin g5.local 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:57:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_PPC Power Macintosh powerpc'
|
2010-06-07 01:02:15 +00:00
|
|
|
output = 'lumi\xc3\xa8re'
|
2010-05-20 00:41:05 +00:00
|
|
|
platform = 'darwin'
|
2010-06-07 01:02:15 +00:00
|
|
|
filesystem_encoding = 'utf-8'
|
2011-06-29 18:53:56 +00:00
|
|
|
io_encoding = 'UTF-8'
|
2010-07-18 01:34:35 +00:00
|
|
|
dirlist = [u'A\u0308rtonwall.mp3', u'Blah blah.txt', u'test_file']
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2010-07-18 01:34:35 +00:00
|
|
|
class MacOSXLeopard7bit(EncodingUtil, unittest.TestCase):
|
2010-05-20 00:41:05 +00:00
|
|
|
uname = 'Darwin g5.local 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:57:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_PPC Power Macintosh powerpc'
|
|
|
|
platform = 'darwin'
|
2010-06-07 01:02:15 +00:00
|
|
|
filesystem_encoding = 'utf-8'
|
2011-06-29 18:53:56 +00:00
|
|
|
io_encoding = 'US-ASCII'
|
2010-07-18 01:34:35 +00:00
|
|
|
dirlist = [u'A\u0308rtonwall.mp3', u'Blah blah.txt', u'test_file']
|
2010-05-21 14:00:53 +00:00
|
|
|
|
2010-07-18 01:34:35 +00:00
|
|
|
class OpenBSD(EncodingUtil, unittest.TestCase):
|
2010-05-21 14:00:53 +00:00
|
|
|
uname = 'OpenBSD 4.1 GENERIC#187 i386 Intel(R) Celeron(R) CPU 2.80GHz ("GenuineIntel" 686-class)'
|
|
|
|
platform = 'openbsd4'
|
2010-06-07 01:02:15 +00:00
|
|
|
filesystem_encoding = '646'
|
2011-06-29 18:53:56 +00:00
|
|
|
io_encoding = '646'
|
2010-05-21 14:00:53 +00:00
|
|
|
# Oops, I cannot write filenames containing non-ascii characters
|
2015-03-03 20:04:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestToFromStr(ReallyEqualMixin, unittest.TestCase):
|
|
|
|
def test_to_str(self):
|
|
|
|
self.failUnlessReallyEqual(to_str("foo"), "foo")
|
|
|
|
self.failUnlessReallyEqual(to_str("lumi\xc3\xa8re"), "lumi\xc3\xa8re")
|
|
|
|
self.failUnlessReallyEqual(to_str("\xFF"), "\xFF") # passes through invalid UTF-8 -- is this what we want?
|
|
|
|
self.failUnlessReallyEqual(to_str(u"lumi\u00E8re"), "lumi\xc3\xa8re")
|
|
|
|
self.failUnlessReallyEqual(to_str(None), None)
|
|
|
|
|
|
|
|
def test_from_utf8_or_none(self):
|
|
|
|
self.failUnlessRaises(AssertionError, from_utf8_or_none, u"foo")
|
|
|
|
self.failUnlessReallyEqual(from_utf8_or_none("lumi\xc3\xa8re"), u"lumi\u00E8re")
|
|
|
|
self.failUnlessReallyEqual(from_utf8_or_none(None), None)
|
|
|
|
self.failUnlessRaises(UnicodeDecodeError, from_utf8_or_none, "\xFF")
|