0001 # Test the Unicode versions of normal file functions 0002 # open, os.open, os.stat. os.listdir, os.rename, os.remove, os.mkdir, os.chdir, os.rmdir 0003 import sys, os, unittest 0004 from test import test_support 0005 if not os.path.supports_unicode_filenames: 0006 raise test_support.TestSkipped, "test works only on NT+" 0007 0008 filenames = [ 0009 'abc', 0010 u'ascii', 0011 u'Gr\xfc\xdf-Gott', 0012 u'\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2', 0013 u'\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435', 0014 u'\u306b\u307d\u3093', 0015 u'\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1', 0016 u'\u66e8\u66e9\u66eb', 0017 u'\u66e8\u05e9\u3093\u0434\u0393\xdf', 0018 ] 0019 0020 # Destroy directory dirname and all files under it, to one level. 0021 def deltree(dirname): 0022 # Don't hide legitimate errors: if one of these suckers exists, it's 0023 # an error if we can't remove it. 0024 if os.path.exists(dirname): 0025 # must pass unicode to os.listdir() so we get back unicode results. 0026 for fname in os.listdir(unicode(dirname)): 0027 os.unlink(os.path.join(dirname, fname)) 0028 os.rmdir(dirname) 0029 0030 class UnicodeFileTests(unittest.TestCase): 0031 files = [os.path.join(test_support.TESTFN, f) for f in filenames] 0032 0033 def setUp(self): 0034 try: 0035 os.mkdir(test_support.TESTFN) 0036 except OSError: 0037 pass 0038 for name in self.files: 0039 f = open(name, 'w') 0040 f.write((name+'\n').encode("utf-8")) 0041 f.close() 0042 os.stat(name) 0043 0044 def tearDown(self): 0045 deltree(test_support.TESTFN) 0046 0047 def _apply_failure(self, fn, filename, expected_exception, 0048 check_fn_in_exception = True): 0049 try: 0050 fn(filename) 0051 raise test_support.TestFailed("Expected to fail calling '%s(%r)'" 0052 % (fn.__name__, filename)) 0053 except expected_exception, details: 0054 if check_fn_in_exception and details.filename != filename: 0055 raise test_support.TestFailed("Function '%s(%r) failed with " 0056 "bad filename in the exception: %r" 0057 % (fn.__name__, filename, 0058 details.filename)) 0059 0060 def test_failures(self): 0061 # Pass non-existing Unicode filenames all over the place. 0062 for name in self.files: 0063 name = "not_" + name 0064 self._apply_failure(open, name, IOError) 0065 self._apply_failure(os.stat, name, OSError) 0066 self._apply_failure(os.chdir, name, OSError) 0067 self._apply_failure(os.rmdir, name, OSError) 0068 self._apply_failure(os.remove, name, OSError) 0069 # listdir may append a wildcard to the filename, so dont check 0070 self._apply_failure(os.listdir, name, OSError, False) 0071 0072 def test_open(self): 0073 for name in self.files: 0074 f = open(name, 'w') 0075 f.write((name+'\n').encode("utf-8")) 0076 f.close() 0077 os.stat(name) 0078 0079 def test_listdir(self): 0080 f1 = os.listdir(test_support.TESTFN) 0081 f1.sort() 0082 f2 = os.listdir(unicode(test_support.TESTFN, 0083 sys.getfilesystemencoding())) 0084 f2.sort() 0085 print f1 0086 print f2 0087 0088 def test_rename(self): 0089 for name in self.files: 0090 os.rename(name,"tmp") 0091 os.rename("tmp",name) 0092 0093 def test_directory(self): 0094 dirname = os.path.join(test_support.TESTFN,u'Gr\xfc\xdf-\u66e8\u66e9\u66eb') 0095 filename = u'\xdf-\u66e8\u66e9\u66eb' 0096 oldwd = os.getcwd() 0097 os.mkdir(dirname) 0098 os.chdir(dirname) 0099 f = open(filename, 'w') 0100 f.write((filename + '\n').encode("utf-8")) 0101 f.close() 0102 print repr(filename) 0103 os.access(filename,os.R_OK) 0104 os.remove(filename) 0105 os.chdir(oldwd) 0106 os.rmdir(dirname) 0107 0108 def test_main(): 0109 try: 0110 test_support.run_unittest(UnicodeFileTests) 0111 finally: 0112 deltree(test_support.TESTFN) 0113 0114 if __name__ == "__main__": 0115 test_main() 0116
Generated by PyXR 0.9.4