PageRenderTime 79ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 1ms

/Lib/test/test_unicode_file_functions.py

https://github.com/albertz/CPython
Python | 195 lines | 143 code | 30 blank | 22 comment | 25 complexity | 28b838b3d822d366ecc2435854707482 MD5 | raw file
  1. # Test the Unicode versions of normal file functions
  2. # open, os.open, os.stat. os.listdir, os.rename, os.remove, os.mkdir, os.chdir, os.rmdir
  3. import os
  4. import sys
  5. import unittest
  6. import warnings
  7. from unicodedata import normalize
  8. from test import support
  9. filenames = [
  10. '1_abc',
  11. '2_ascii',
  12. '3_Gr\xfc\xdf-Gott',
  13. '4_\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2',
  14. '5_\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435',
  15. '6_\u306b\u307d\u3093',
  16. '7_\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1',
  17. '8_\u66e8\u66e9\u66eb',
  18. '9_\u66e8\u05e9\u3093\u0434\u0393\xdf',
  19. # Specific code points: fn, NFC(fn) and NFKC(fn) all different
  20. '10_\u1fee\u1ffd',
  21. ]
  22. # Mac OS X decomposes Unicode names, using Normal Form D.
  23. # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html
  24. # "However, most volume formats do not follow the exact specification for
  25. # these normal forms. For example, HFS Plus uses a variant of Normal Form D
  26. # in which U+2000 through U+2FFF, U+F900 through U+FAFF, and U+2F800 through
  27. # U+2FAFF are not decomposed."
  28. if sys.platform != 'darwin':
  29. filenames.extend([
  30. # Specific code points: NFC(fn), NFD(fn), NFKC(fn) and NFKD(fn) all different
  31. '11_\u0385\u03d3\u03d4',
  32. '12_\u00a8\u0301\u03d2\u0301\u03d2\u0308', # == NFD('\u0385\u03d3\u03d4')
  33. '13_\u0020\u0308\u0301\u038e\u03ab', # == NFKC('\u0385\u03d3\u03d4')
  34. '14_\u1e9b\u1fc1\u1fcd\u1fce\u1fcf\u1fdd\u1fde\u1fdf\u1fed',
  35. # Specific code points: fn, NFC(fn) and NFKC(fn) all different
  36. '15_\u1fee\u1ffd\ufad1',
  37. '16_\u2000\u2000\u2000A',
  38. '17_\u2001\u2001\u2001A',
  39. '18_\u2003\u2003\u2003A', # == NFC('\u2001\u2001\u2001A')
  40. '19_\u0020\u0020\u0020A', # '\u0020' == ' ' == NFKC('\u2000') ==
  41. # NFKC('\u2001') == NFKC('\u2003')
  42. ])
  43. # Is it Unicode-friendly?
  44. if not os.path.supports_unicode_filenames:
  45. fsencoding = sys.getfilesystemencoding()
  46. try:
  47. for name in filenames:
  48. name.encode(fsencoding)
  49. except UnicodeEncodeError:
  50. raise unittest.SkipTest("only NT+ and systems with "
  51. "Unicode-friendly filesystem encoding")
  52. class UnicodeFileTests(unittest.TestCase):
  53. files = set(filenames)
  54. normal_form = None
  55. def setUp(self):
  56. try:
  57. os.mkdir(support.TESTFN)
  58. except FileExistsError:
  59. pass
  60. self.addCleanup(support.rmtree, support.TESTFN)
  61. files = set()
  62. for name in self.files:
  63. name = os.path.join(support.TESTFN, self.norm(name))
  64. with open(name, 'wb') as f:
  65. f.write((name+'\n').encode("utf-8"))
  66. os.stat(name)
  67. files.add(name)
  68. self.files = files
  69. def norm(self, s):
  70. if self.normal_form:
  71. return normalize(self.normal_form, s)
  72. return s
  73. def _apply_failure(self, fn, filename,
  74. expected_exception=FileNotFoundError,
  75. check_filename=True):
  76. with self.assertRaises(expected_exception) as c:
  77. fn(filename)
  78. exc_filename = c.exception.filename
  79. if check_filename:
  80. self.assertEqual(exc_filename, filename, "Function '%s(%a) failed "
  81. "with bad filename in the exception: %a" %
  82. (fn.__name__, filename, exc_filename))
  83. def test_failures(self):
  84. # Pass non-existing Unicode filenames all over the place.
  85. for name in self.files:
  86. name = "not_" + name
  87. self._apply_failure(open, name)
  88. self._apply_failure(os.stat, name)
  89. self._apply_failure(os.chdir, name)
  90. self._apply_failure(os.rmdir, name)
  91. self._apply_failure(os.remove, name)
  92. self._apply_failure(os.listdir, name)
  93. if sys.platform == 'win32':
  94. # Windows is lunatic. Issue #13366.
  95. _listdir_failure = NotADirectoryError, FileNotFoundError
  96. else:
  97. _listdir_failure = NotADirectoryError
  98. def test_open(self):
  99. for name in self.files:
  100. f = open(name, 'wb')
  101. f.write((name+'\n').encode("utf-8"))
  102. f.close()
  103. os.stat(name)
  104. self._apply_failure(os.listdir, name, self._listdir_failure)
  105. # Skip the test on darwin, because darwin does normalize the filename to
  106. # NFD (a variant of Unicode NFD form). Normalize the filename to NFC, NFKC,
  107. # NFKD in Python is useless, because darwin will normalize it later and so
  108. # open(), os.stat(), etc. don't raise any exception.
  109. @unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X')
  110. def test_normalize(self):
  111. files = set(self.files)
  112. others = set()
  113. for nf in set(['NFC', 'NFD', 'NFKC', 'NFKD']):
  114. others |= set(normalize(nf, file) for file in files)
  115. others -= files
  116. for name in others:
  117. self._apply_failure(open, name)
  118. self._apply_failure(os.stat, name)
  119. self._apply_failure(os.chdir, name)
  120. self._apply_failure(os.rmdir, name)
  121. self._apply_failure(os.remove, name)
  122. self._apply_failure(os.listdir, name)
  123. # Skip the test on darwin, because darwin uses a normalization different
  124. # than Python NFD normalization: filenames are different even if we use
  125. # Python NFD normalization.
  126. @unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X')
  127. def test_listdir(self):
  128. sf0 = set(self.files)
  129. with warnings.catch_warnings():
  130. warnings.simplefilter("ignore", DeprecationWarning)
  131. f1 = os.listdir(support.TESTFN.encode(sys.getfilesystemencoding()))
  132. f2 = os.listdir(support.TESTFN)
  133. sf2 = set(os.path.join(support.TESTFN, f) for f in f2)
  134. self.assertEqual(sf0, sf2, "%a != %a" % (sf0, sf2))
  135. self.assertEqual(len(f1), len(f2))
  136. def test_rename(self):
  137. for name in self.files:
  138. os.rename(name, "tmp")
  139. os.rename("tmp", name)
  140. def test_directory(self):
  141. dirname = os.path.join(support.TESTFN, 'Gr\xfc\xdf-\u66e8\u66e9\u66eb')
  142. filename = '\xdf-\u66e8\u66e9\u66eb'
  143. with support.temp_cwd(dirname):
  144. with open(filename, 'wb') as f:
  145. f.write((filename + '\n').encode("utf-8"))
  146. os.access(filename,os.R_OK)
  147. os.remove(filename)
  148. class UnicodeNFCFileTests(UnicodeFileTests):
  149. normal_form = 'NFC'
  150. class UnicodeNFDFileTests(UnicodeFileTests):
  151. normal_form = 'NFD'
  152. class UnicodeNFKCFileTests(UnicodeFileTests):
  153. normal_form = 'NFKC'
  154. class UnicodeNFKDFileTests(UnicodeFileTests):
  155. normal_form = 'NFKD'
  156. def test_main():
  157. support.run_unittest(
  158. UnicodeFileTests,
  159. UnicodeNFCFileTests,
  160. UnicodeNFDFileTests,
  161. UnicodeNFKCFileTests,
  162. UnicodeNFKDFileTests,
  163. )
  164. if __name__ == "__main__":
  165. test_main()