| 
									
										
										
										
											2002-10-03 23:14:10 +00: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 | 
					
						
							| 
									
										
										
										
											2003-07-16 03:46:38 +00:00
										 |  |  | import sys, os, unittest | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  | from unicodedata import normalize | 
					
						
							| 
									
										
										
										
											2008-05-20 21:35:26 +00:00
										 |  |  | from test import support | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | filenames = [ | 
					
						
							| 
									
										
										
										
											2002-10-07 17:27:15 +00:00
										 |  |  |     'abc', | 
					
						
							| 
									
										
										
										
											2007-05-02 19:09:54 +00:00
										 |  |  |     'ascii', | 
					
						
							|  |  |  |     'Gr\xfc\xdf-Gott', | 
					
						
							|  |  |  |     '\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2', | 
					
						
							|  |  |  |     '\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435', | 
					
						
							|  |  |  |     '\u306b\u307d\u3093', | 
					
						
							|  |  |  |     '\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1', | 
					
						
							|  |  |  |     '\u66e8\u66e9\u66eb', | 
					
						
							|  |  |  |     '\u66e8\u05e9\u3093\u0434\u0393\xdf', | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  |     # Specific code points: fn, NFC(fn) and NFKC(fn) all differents | 
					
						
							|  |  |  |     '\u1fee\u1ffd', | 
					
						
							|  |  |  |     # Specific code points: NFC(fn), NFD(fn), NFKC(fn) and NFKD(fn) all differents | 
					
						
							|  |  |  |     '\u0385\u03d3\u03d4', | 
					
						
							|  |  |  |     '\u00a8\u0301\u03d2\u0301\u03d2\u0308',     # == NFD('\u0385\u03d3\u03d4') | 
					
						
							|  |  |  |     '\u0020\u0308\u0301\u038e\u03ab',           # == NFKC('\u0385\u03d3\u03d4') | 
					
						
							|  |  |  |     '\u1e9b\u1fc1\u1fcd\u1fce\u1fcf\u1fdd\u1fde\u1fdf\u1fed', | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00: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-28 00:25:02 +00: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([ | 
					
						
							|  |  |  |         # Specific code points: fn, NFC(fn) and NFKC(fn) all differents | 
					
						
							|  |  |  |         '\u1fee\u1ffd\ufad1', | 
					
						
							|  |  |  |         '\u2000\u2000\u2000A', | 
					
						
							|  |  |  |         '\u2001\u2001\u2001A', | 
					
						
							|  |  |  |         '\u2003\u2003\u2003A',  # == NFC('\u2001\u2001\u2001A') | 
					
						
							|  |  |  |         '\u0020\u0020\u0020A',  # '\u0020' == ' ' == NFKC('\u2000') == | 
					
						
							|  |  |  |                                 #  NFKC('\u2001') == NFKC('\u2003') | 
					
						
							|  |  |  | ]) | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-09 17:18:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Is it Unicode-friendly? | 
					
						
							|  |  |  | if not os.path.supports_unicode_filenames: | 
					
						
							| 
									
										
										
										
											2010-08-18 21:23:25 +00:00
										 |  |  |     fsencoding = sys.getfilesystemencoding() | 
					
						
							| 
									
										
										
										
											2010-08-09 17:18:05 +00: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-05 17:54:56 +00:00
										 |  |  | # Destroy directory dirname and all files under it, to one level. | 
					
						
							|  |  |  | def deltree(dirname): | 
					
						
							|  |  |  |     # Don't hide legitimate errors:  if one of these suckers exists, it's | 
					
						
							|  |  |  |     # an error if we can't remove it. | 
					
						
							|  |  |  |     if os.path.exists(dirname): | 
					
						
							| 
									
										
										
										
											2002-10-08 02:44:31 +00:00
										 |  |  |         # must pass unicode to os.listdir() so we get back unicode results. | 
					
						
							| 
									
										
										
										
											2007-05-02 19:09:54 +00:00
										 |  |  |         for fname in os.listdir(str(dirname)): | 
					
						
							| 
									
										
										
										
											2002-10-05 17:54:56 +00:00
										 |  |  |             os.unlink(os.path.join(dirname, fname)) | 
					
						
							|  |  |  |         os.rmdir(dirname) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00:00
										 |  |  | class UnicodeFileTests(unittest.TestCase): | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  |     files = set(filenames) | 
					
						
							|  |  |  |     normal_form = None | 
					
						
							| 
									
										
										
										
											2002-10-05 17:54:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00:00
										 |  |  |     def setUp(self): | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2008-05-20 21:35:26 +00:00
										 |  |  |             os.mkdir(support.TESTFN) | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00:00
										 |  |  |         except OSError: | 
					
						
							|  |  |  |             pass | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  |         files = set() | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00:00
										 |  |  |         for name in self.files: | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  |             name = os.path.join(support.TESTFN, self.norm(name)) | 
					
						
							| 
									
										
										
										
											2010-08-09 17:18:05 +00:00
										 |  |  |             with open(name, 'wb') as f: | 
					
						
							|  |  |  |                 f.write((name+'\n').encode("utf-8")) | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00:00
										 |  |  |             os.stat(name) | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  |             files.add(name) | 
					
						
							|  |  |  |         self.files = files | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def tearDown(self): | 
					
						
							| 
									
										
										
										
											2008-05-20 21:35:26 +00:00
										 |  |  |         deltree(support.TESTFN) | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  |     def norm(self, s): | 
					
						
							|  |  |  |         if self.normal_form: | 
					
						
							|  |  |  |             return normalize(self.normal_form, s) | 
					
						
							|  |  |  |         return s | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00:00
										 |  |  |     def _apply_failure(self, fn, filename, expected_exception, | 
					
						
							|  |  |  |                        check_fn_in_exception = True): | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  |         with self.assertRaises(expected_exception) as c: | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00:00
										 |  |  |             fn(filename) | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  |         exc_filename = c.exception.filename | 
					
						
							|  |  |  |         # the "filename" exception attribute may be encoded | 
					
						
							|  |  |  |         if isinstance(exc_filename, bytes): | 
					
						
							|  |  |  |             filename = filename.encode(sys.getfilesystemencoding()) | 
					
						
							|  |  |  |         if check_fn_in_exception: | 
					
						
							|  |  |  |             self.assertEqual(exc_filename, filename, "Function '%s(%r) failed " | 
					
						
							|  |  |  |                              "with bad filename in the exception: %r" % | 
					
						
							|  |  |  |                              (fn.__name__, filename, exc_filename)) | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_failures(self): | 
					
						
							|  |  |  |         # Pass non-existing Unicode filenames all over the place. | 
					
						
							|  |  |  |         for name in self.files: | 
					
						
							|  |  |  |             name = "not_" + name | 
					
						
							|  |  |  |             self._apply_failure(open, name, IOError) | 
					
						
							|  |  |  |             self._apply_failure(os.stat, name, OSError) | 
					
						
							|  |  |  |             self._apply_failure(os.chdir, name, OSError) | 
					
						
							|  |  |  |             self._apply_failure(os.rmdir, name, OSError) | 
					
						
							|  |  |  |             self._apply_failure(os.remove, name, OSError) | 
					
						
							|  |  |  |             # listdir may append a wildcard to the filename, so dont check | 
					
						
							|  |  |  |             self._apply_failure(os.listdir, name, OSError, False) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_open(self): | 
					
						
							|  |  |  |         for name in self.files: | 
					
						
							| 
									
										
										
										
											2007-10-26 04:29:23 +00:00
										 |  |  |             f = open(name, 'wb') | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00:00
										 |  |  |             f.write((name+'\n').encode("utf-8")) | 
					
						
							|  |  |  |             f.close() | 
					
						
							|  |  |  |             os.stat(name) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00: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 | 
					
						
							|  |  |  |         if sys.platform == 'darwin': | 
					
						
							|  |  |  |             files = set(normalize('NFD', file) for file in files) | 
					
						
							|  |  |  |         for name in others: | 
					
						
							|  |  |  |             if sys.platform == 'darwin' and normalize('NFD', name) in files: | 
					
						
							| 
									
										
											  
											
												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-28 00:25:02 +00:00
										 |  |  |                 # Mac OS X decomposes Unicode names.  See comment above. | 
					
						
							| 
									
										
										
										
											2010-08-19 17:35:00 +00:00
										 |  |  |                 try: | 
					
						
							|  |  |  |                     os.stat(name) | 
					
						
							|  |  |  |                 except OSError as err: | 
					
						
							|  |  |  |                     raise AssertionError("File %a doesn't exist" % name) | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  |                 continue | 
					
						
							|  |  |  |             self._apply_failure(open, name, IOError) | 
					
						
							|  |  |  |             self._apply_failure(os.stat, name, OSError) | 
					
						
							|  |  |  |             self._apply_failure(os.chdir, name, OSError) | 
					
						
							|  |  |  |             self._apply_failure(os.rmdir, name, OSError) | 
					
						
							|  |  |  |             self._apply_failure(os.remove, name, OSError) | 
					
						
							|  |  |  |             # listdir may append a wildcard to the filename, so dont check | 
					
						
							|  |  |  |             self._apply_failure(os.listdir, name, OSError, False) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00:00
										 |  |  |     def test_listdir(self): | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  |         sf0 = set(self.files) | 
					
						
							|  |  |  |         f1 = os.listdir(support.TESTFN.encode(sys.getfilesystemencoding())) | 
					
						
							|  |  |  |         f2 = os.listdir(support.TESTFN) | 
					
						
							|  |  |  |         if sys.platform == 'darwin': | 
					
						
							| 
									
										
											  
											
												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-28 00:25:02 +00:00
										 |  |  |             # Mac OS X decomposes Unicode names.  See comment above. | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  |             sf0 = set(normalize('NFD', f) for f in self.files) | 
					
						
							|  |  |  |             f2 = [normalize('NFD', f) for f in f2] | 
					
						
							|  |  |  |         sf2 = set(os.path.join(support.TESTFN, f) for f in f2) | 
					
						
							|  |  |  |         self.assertEqual(sf0, sf2) | 
					
						
							|  |  |  |         self.assertEqual(len(f1), len(f2)) | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_rename(self): | 
					
						
							|  |  |  |         for name in self.files: | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  |             os.rename(name, "tmp") | 
					
						
							|  |  |  |             os.rename("tmp", name) | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_directory(self): | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  |         dirname = os.path.join(support.TESTFN, 'Gr\xfc\xdf-\u66e8\u66e9\u66eb') | 
					
						
							| 
									
										
										
										
											2007-05-02 19:09:54 +00:00
										 |  |  |         filename = '\xdf-\u66e8\u66e9\u66eb' | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00:00
										 |  |  |         oldwd = os.getcwd() | 
					
						
							|  |  |  |         os.mkdir(dirname) | 
					
						
							|  |  |  |         os.chdir(dirname) | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  |         try: | 
					
						
							|  |  |  |             with open(filename, 'wb') as f: | 
					
						
							|  |  |  |                 f.write((filename + '\n').encode("utf-8")) | 
					
						
							|  |  |  |             os.access(filename,os.R_OK) | 
					
						
							|  |  |  |             os.remove(filename) | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             os.chdir(oldwd) | 
					
						
							|  |  |  |             os.rmdir(dirname) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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 23:14:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_main(): | 
					
						
							| 
									
										
										
										
											2002-10-05 17:54:56 +00:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  |         support.run_unittest( | 
					
						
							|  |  |  |             UnicodeFileTests, | 
					
						
							|  |  |  |             UnicodeNFCFileTests, | 
					
						
							|  |  |  |             UnicodeNFDFileTests, | 
					
						
							|  |  |  |             UnicodeNFKCFileTests, | 
					
						
							|  |  |  |             UnicodeNFKDFileTests, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2002-10-05 17:54:56 +00:00
										 |  |  |     finally: | 
					
						
							| 
									
										
										
										
											2008-05-20 21:35:26 +00:00
										 |  |  |         deltree(support.TESTFN) | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-21 18:49:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-03 23:14:10 +00:00
										 |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     test_main() |