2002-10-03 20:14:10 -03:00
|
|
|
# Test the Unicode versions of normal file functions
|
|
|
|
# open, os.open, os.stat. os.listdir, os.rename, os.remove, os.mkdir, os.chdir, os.rmdir
|
2011-11-15 17:27:41 -04:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
import warnings
|
2010-03-21 15:49:50 -03:00
|
|
|
from unicodedata import normalize
|
2020-08-03 13:47:42 -03:00
|
|
|
from test.support import os_helper
|
|
|
|
|
2002-10-03 20:14:10 -03:00
|
|
|
|
|
|
|
filenames = [
|
2010-10-28 08:20:31 -03:00
|
|
|
'1_abc',
|
|
|
|
'2_ascii',
|
|
|
|
'3_Gr\xfc\xdf-Gott',
|
|
|
|
'4_\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2',
|
|
|
|
'5_\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435',
|
|
|
|
'6_\u306b\u307d\u3093',
|
|
|
|
'7_\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1',
|
|
|
|
'8_\u66e8\u66e9\u66eb',
|
|
|
|
'9_\u66e8\u05e9\u3093\u0434\u0393\xdf',
|
2017-11-05 09:37:50 -04:00
|
|
|
# Specific code points: fn, NFC(fn) and NFKC(fn) all different
|
2010-10-28 08:20:31 -03:00
|
|
|
'10_\u1fee\u1ffd',
|
2002-10-03 20:14:10 -03:00
|
|
|
]
|
|
|
|
|
Merged revisions 79297,79310,79382,79425-79427,79450 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r79297 | florent.xicluna | 2010-03-22 18:18:18 +0100 (lun, 22 mar 2010) | 2 lines
#7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
........
r79310 | florent.xicluna | 2010-03-22 23:52:11 +0100 (lun, 22 mar 2010) | 2 lines
Issue #8205: Remove the "Modules" directory from sys.path when Python is running from the build directory (POSIX only).
........
r79382 | florent.xicluna | 2010-03-24 20:33:25 +0100 (mer, 24 mar 2010) | 2 lines
Skip tests which depend on multiprocessing.sharedctypes, if _ctypes is not available.
........
r79425 | florent.xicluna | 2010-03-25 21:32:07 +0100 (jeu, 25 mar 2010) | 2 lines
Syntax cleanup `== None` -> `is None`
........
r79426 | florent.xicluna | 2010-03-25 21:33:49 +0100 (jeu, 25 mar 2010) | 2 lines
#8207: Fix test_pep277 on OS X
........
r79427 | florent.xicluna | 2010-03-25 21:39:10 +0100 (jeu, 25 mar 2010) | 2 lines
Fix test_unittest and test_warnings when running "python -Werror -m test.regrtest"
........
r79450 | florent.xicluna | 2010-03-26 20:32:44 +0100 (ven, 26 mar 2010) | 2 lines
Ensure that the failed or unexpected tests are sorted before printing.
........
2010-03-27 21:25:02 -03:00
|
|
|
# Mac OS X decomposes Unicode names, using Normal Form D.
|
|
|
|
# http://developer.apple.com/mac/library/qa/qa2001/qa1173.html
|
|
|
|
# "However, most volume formats do not follow the exact specification for
|
|
|
|
# these normal forms. For example, HFS Plus uses a variant of Normal Form D
|
|
|
|
# in which U+2000 through U+2FFF, U+F900 through U+FAFF, and U+2F800 through
|
|
|
|
# U+2FAFF are not decomposed."
|
|
|
|
if sys.platform != 'darwin':
|
|
|
|
filenames.extend([
|
2017-11-05 09:37:50 -04:00
|
|
|
# Specific code points: NFC(fn), NFD(fn), NFKC(fn) and NFKD(fn) all different
|
2010-10-28 19:57:03 -03:00
|
|
|
'11_\u0385\u03d3\u03d4',
|
|
|
|
'12_\u00a8\u0301\u03d2\u0301\u03d2\u0308', # == NFD('\u0385\u03d3\u03d4')
|
|
|
|
'13_\u0020\u0308\u0301\u038e\u03ab', # == NFKC('\u0385\u03d3\u03d4')
|
|
|
|
'14_\u1e9b\u1fc1\u1fcd\u1fce\u1fcf\u1fdd\u1fde\u1fdf\u1fed',
|
|
|
|
|
2017-11-05 09:37:50 -04:00
|
|
|
# Specific code points: fn, NFC(fn) and NFKC(fn) all different
|
2010-10-28 08:20:31 -03:00
|
|
|
'15_\u1fee\u1ffd\ufad1',
|
|
|
|
'16_\u2000\u2000\u2000A',
|
|
|
|
'17_\u2001\u2001\u2001A',
|
|
|
|
'18_\u2003\u2003\u2003A', # == NFC('\u2001\u2001\u2001A')
|
|
|
|
'19_\u0020\u0020\u0020A', # '\u0020' == ' ' == NFKC('\u2000') ==
|
2011-11-03 19:11:14 -03:00
|
|
|
# NFKC('\u2001') == NFKC('\u2003')
|
|
|
|
])
|
2010-03-21 15:49:50 -03:00
|
|
|
|
2010-08-09 14:18:05 -03:00
|
|
|
|
|
|
|
# Is it Unicode-friendly?
|
|
|
|
if not os.path.supports_unicode_filenames:
|
2010-08-18 18:23:25 -03:00
|
|
|
fsencoding = sys.getfilesystemencoding()
|
2010-08-09 14:18:05 -03:00
|
|
|
try:
|
|
|
|
for name in filenames:
|
|
|
|
name.encode(fsencoding)
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
raise unittest.SkipTest("only NT+ and systems with "
|
|
|
|
"Unicode-friendly filesystem encoding")
|
|
|
|
|
|
|
|
|
2002-10-03 20:14:10 -03:00
|
|
|
class UnicodeFileTests(unittest.TestCase):
|
2010-03-21 15:49:50 -03:00
|
|
|
files = set(filenames)
|
|
|
|
normal_form = None
|
2002-10-05 14:54:56 -03:00
|
|
|
|
2002-10-03 20:14:10 -03:00
|
|
|
def setUp(self):
|
|
|
|
try:
|
2020-08-03 13:47:42 -03:00
|
|
|
os.mkdir(os_helper.TESTFN)
|
2011-11-03 19:11:14 -03:00
|
|
|
except FileExistsError:
|
2002-10-03 20:14:10 -03:00
|
|
|
pass
|
2020-08-03 13:47:42 -03:00
|
|
|
self.addCleanup(os_helper.rmtree, os_helper.TESTFN)
|
2014-07-28 19:40:50 -03:00
|
|
|
|
2010-03-21 15:49:50 -03:00
|
|
|
files = set()
|
2002-10-03 20:14:10 -03:00
|
|
|
for name in self.files:
|
2020-08-03 13:47:42 -03:00
|
|
|
name = os.path.join(os_helper.TESTFN, self.norm(name))
|
2010-08-09 14:18:05 -03:00
|
|
|
with open(name, 'wb') as f:
|
|
|
|
f.write((name+'\n').encode("utf-8"))
|
2002-10-03 20:14:10 -03:00
|
|
|
os.stat(name)
|
2010-03-21 15:49:50 -03:00
|
|
|
files.add(name)
|
|
|
|
self.files = files
|
2002-10-03 20:14:10 -03:00
|
|
|
|
2010-03-21 15:49:50 -03:00
|
|
|
def norm(self, s):
|
|
|
|
if self.normal_form:
|
|
|
|
return normalize(self.normal_form, s)
|
|
|
|
return s
|
|
|
|
|
2011-11-03 19:11:14 -03:00
|
|
|
def _apply_failure(self, fn, filename,
|
|
|
|
expected_exception=FileNotFoundError,
|
|
|
|
check_filename=True):
|
2010-03-21 15:49:50 -03:00
|
|
|
with self.assertRaises(expected_exception) as c:
|
2002-10-03 20:14:10 -03:00
|
|
|
fn(filename)
|
2010-03-21 15:49:50 -03:00
|
|
|
exc_filename = c.exception.filename
|
2011-11-03 19:11:14 -03:00
|
|
|
if check_filename:
|
2010-10-28 08:09:09 -03:00
|
|
|
self.assertEqual(exc_filename, filename, "Function '%s(%a) failed "
|
2010-10-28 08:11:24 -03:00
|
|
|
"with bad filename in the exception: %a" %
|
2010-03-21 15:49:50 -03:00
|
|
|
(fn.__name__, filename, exc_filename))
|
2002-10-03 20:14:10 -03:00
|
|
|
|
|
|
|
def test_failures(self):
|
|
|
|
# Pass non-existing Unicode filenames all over the place.
|
|
|
|
for name in self.files:
|
|
|
|
name = "not_" + name
|
2011-11-03 19:11:14 -03:00
|
|
|
self._apply_failure(open, name)
|
|
|
|
self._apply_failure(os.stat, name)
|
|
|
|
self._apply_failure(os.chdir, name)
|
|
|
|
self._apply_failure(os.rmdir, name)
|
|
|
|
self._apply_failure(os.remove, name)
|
2011-11-07 16:56:17 -04:00
|
|
|
self._apply_failure(os.listdir, name)
|
|
|
|
|
|
|
|
if sys.platform == 'win32':
|
2011-11-07 18:24:08 -04:00
|
|
|
# Windows is lunatic. Issue #13366.
|
|
|
|
_listdir_failure = NotADirectoryError, FileNotFoundError
|
2011-11-07 16:56:17 -04:00
|
|
|
else:
|
|
|
|
_listdir_failure = NotADirectoryError
|
2002-10-03 20:14:10 -03:00
|
|
|
|
|
|
|
def test_open(self):
|
|
|
|
for name in self.files:
|
2007-10-26 01:29:23 -03:00
|
|
|
f = open(name, 'wb')
|
2002-10-03 20:14:10 -03:00
|
|
|
f.write((name+'\n').encode("utf-8"))
|
|
|
|
f.close()
|
|
|
|
os.stat(name)
|
2011-11-07 16:56:17 -04:00
|
|
|
self._apply_failure(os.listdir, name, self._listdir_failure)
|
2002-10-03 20:14:10 -03:00
|
|
|
|
2010-10-28 20:14:45 -03:00
|
|
|
# Skip the test on darwin, because darwin does normalize the filename to
|
|
|
|
# NFD (a variant of Unicode NFD form). Normalize the filename to NFC, NFKC,
|
|
|
|
# NFKD in Python is useless, because darwin will normalize it later and so
|
|
|
|
# open(), os.stat(), etc. don't raise any exception.
|
2011-11-03 19:11:14 -03:00
|
|
|
@unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X')
|
2010-03-21 15:49:50 -03:00
|
|
|
def test_normalize(self):
|
|
|
|
files = set(self.files)
|
|
|
|
others = set()
|
|
|
|
for nf in set(['NFC', 'NFD', 'NFKC', 'NFKD']):
|
|
|
|
others |= set(normalize(nf, file) for file in files)
|
|
|
|
others -= files
|
|
|
|
for name in others:
|
2011-11-03 19:11:14 -03:00
|
|
|
self._apply_failure(open, name)
|
|
|
|
self._apply_failure(os.stat, name)
|
|
|
|
self._apply_failure(os.chdir, name)
|
|
|
|
self._apply_failure(os.rmdir, name)
|
|
|
|
self._apply_failure(os.remove, name)
|
2011-11-07 16:56:17 -04:00
|
|
|
self._apply_failure(os.listdir, name)
|
2010-03-21 15:49:50 -03:00
|
|
|
|
2010-10-28 20:14:45 -03:00
|
|
|
# Skip the test on darwin, because darwin uses a normalization different
|
|
|
|
# than Python NFD normalization: filenames are different even if we use
|
|
|
|
# Python NFD normalization.
|
2011-11-03 19:11:14 -03:00
|
|
|
@unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X')
|
2002-10-03 20:14:10 -03:00
|
|
|
def test_listdir(self):
|
2010-03-21 15:49:50 -03:00
|
|
|
sf0 = set(self.files)
|
2011-11-15 17:27:41 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter("ignore", DeprecationWarning)
|
2020-08-03 13:47:42 -03:00
|
|
|
f1 = os.listdir(os_helper.TESTFN.encode(
|
|
|
|
sys.getfilesystemencoding()))
|
|
|
|
f2 = os.listdir(os_helper.TESTFN)
|
|
|
|
sf2 = set(os.path.join(os_helper.TESTFN, f) for f in f2)
|
2010-10-24 18:05:03 -03:00
|
|
|
self.assertEqual(sf0, sf2, "%a != %a" % (sf0, sf2))
|
2010-03-21 15:49:50 -03:00
|
|
|
self.assertEqual(len(f1), len(f2))
|
2002-10-03 20:14:10 -03:00
|
|
|
|
|
|
|
def test_rename(self):
|
|
|
|
for name in self.files:
|
2010-03-21 15:49:50 -03:00
|
|
|
os.rename(name, "tmp")
|
|
|
|
os.rename("tmp", name)
|
2002-10-03 20:14:10 -03:00
|
|
|
|
|
|
|
def test_directory(self):
|
2020-08-03 13:47:42 -03:00
|
|
|
dirname = os.path.join(os_helper.TESTFN,
|
|
|
|
'Gr\xfc\xdf-\u66e8\u66e9\u66eb')
|
2007-05-02 16:09:54 -03:00
|
|
|
filename = '\xdf-\u66e8\u66e9\u66eb'
|
2020-08-03 13:47:42 -03:00
|
|
|
with os_helper.temp_cwd(dirname):
|
2010-03-21 15:49:50 -03:00
|
|
|
with open(filename, 'wb') as f:
|
|
|
|
f.write((filename + '\n').encode("utf-8"))
|
|
|
|
os.access(filename,os.R_OK)
|
|
|
|
os.remove(filename)
|
|
|
|
|
|
|
|
|
|
|
|
class UnicodeNFCFileTests(UnicodeFileTests):
|
|
|
|
normal_form = 'NFC'
|
|
|
|
|
|
|
|
|
|
|
|
class UnicodeNFDFileTests(UnicodeFileTests):
|
|
|
|
normal_form = 'NFD'
|
|
|
|
|
|
|
|
|
|
|
|
class UnicodeNFKCFileTests(UnicodeFileTests):
|
|
|
|
normal_form = 'NFKC'
|
|
|
|
|
|
|
|
|
|
|
|
class UnicodeNFKDFileTests(UnicodeFileTests):
|
|
|
|
normal_form = 'NFKD'
|
|
|
|
|
2002-10-03 20:14:10 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-09-19 09:27:33 -03:00
|
|
|
unittest.main()
|