diff options
author | Mark Hammond <mhammond@skippinet.com.au> | 2001-05-13 08:04:26 (GMT) |
---|---|---|
committer | Mark Hammond <mhammond@skippinet.com.au> | 2001-05-13 08:04:26 (GMT) |
commit | ef8b654bbea15dc55767a7095e01dff7a3ca86cb (patch) | |
tree | 778653b95245ae2d31e5a5ed94c0c491e1687b15 /Lib | |
parent | 342c65e19ac0cc47bf2b21026c76e63440b23748 (diff) | |
download | cpython-ef8b654bbea15dc55767a7095e01dff7a3ca86cb.zip cpython-ef8b654bbea15dc55767a7095e01dff7a3ca86cb.tar.gz cpython-ef8b654bbea15dc55767a7095e01dff7a3ca86cb.tar.bz2 |
Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/ntpath.py | 17 | ||||
-rw-r--r-- | Lib/test/output/test_unicode_file | 2 | ||||
-rw-r--r-- | Lib/test/test_support.py | 4 | ||||
-rw-r--r-- | Lib/test/test_unicode_file.py | 81 |
4 files changed, 91 insertions, 13 deletions
diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 63860ce7..47c1acf 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -404,21 +404,12 @@ def normpath(path): # Return an absolute path. def abspath(path): """Return the absolute version of a path""" - try: - import win32api - except ImportError: - global abspath - def _abspath(path): - if not isabs(path): - path = join(os.getcwd(), path) - return normpath(path) - abspath = _abspath - return _abspath(path) if path: # Empty path must return current working directory. + from nt import _getfullpathname try: - path = win32api.GetFullPathName(path) - except win32api.error: - pass # Bad path - return unchanged. + path = _getfullpathname(path) + except WindowsError: + pass # Bad path - return unchanged. else: path = os.getcwd() return normpath(path) diff --git a/Lib/test/output/test_unicode_file b/Lib/test/output/test_unicode_file new file mode 100644 index 0000000..6e984d8 --- /dev/null +++ b/Lib/test/output/test_unicode_file @@ -0,0 +1,2 @@ +test_unicode_file +All the Unicode tests appeared to work diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index 330b9c8..88a3c5e 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -63,6 +63,10 @@ if os.name == 'java': TESTFN = '$test' elif os.name != 'riscos': TESTFN = '@test' + # Unicode name only used if TEST_FN_ENCODING exists for the platform. + TESTFN_UNICODE=u"@test-\xe0\xf2" # 2 latin characters. + if os.name=="nt": + TESTFN_ENCODING="mbcs" else: TESTFN = 'test' del os diff --git a/Lib/test/test_unicode_file.py b/Lib/test/test_unicode_file.py new file mode 100644 index 0000000..7078197 --- /dev/null +++ b/Lib/test/test_unicode_file.py @@ -0,0 +1,81 @@ +# Test some Unicode file name semantics +# We dont test many operations on files other than +# that their names can be used with Unicode characters. +import os + +from test_support import verify, TestSkipped, TESTFN_UNICODE +try: + from test_support import TESTFN_ENCODING +except ImportError: + raise TestSkipped("No Unicode filesystem semantics on this platform.") + +TESTFN_ENCODED = TESTFN_UNICODE.encode(TESTFN_ENCODING) + +# Check with creation as Unicode string. +f = open(TESTFN_UNICODE, 'wb') +if not os.path.isfile(TESTFN_UNICODE): + print "File doesn't exist after creating it" + +if not os.path.isfile(TESTFN_ENCODED): + print "File doesn't exist (encoded string) after creating it" + +f.close() + +# Test stat and chmod +if os.stat(TESTFN_ENCODED) != os.stat(TESTFN_UNICODE): + print "os.stat() did not agree on the 2 filenames" +os.chmod(TESTFN_ENCODED, 0777) +os.chmod(TESTFN_UNICODE, 0777) + +# Test rename +os.rename(TESTFN_ENCODED, TESTFN_ENCODED + ".new") +os.rename(TESTFN_UNICODE+".new", TESTFN_ENCODED) + +os.unlink(TESTFN_ENCODED) +if os.path.isfile(TESTFN_ENCODED) or \ + os.path.isfile(TESTFN_UNICODE): + print "File exists after deleting it" + +# Check with creation as encoded string. +f = open(TESTFN_ENCODED, 'wb') +if not os.path.isfile(TESTFN_UNICODE) or \ + not os.path.isfile(TESTFN_ENCODED): + print "File doesn't exist after creating it" + +path, base = os.path.split(os.path.abspath(TESTFN_ENCODED)) +if base not in os.listdir(path): + print "Filename did not appear in os.listdir()" + +f.close() +os.unlink(TESTFN_UNICODE) +if os.path.isfile(TESTFN_ENCODED) or \ + os.path.isfile(TESTFN_UNICODE): + print "File exists after deleting it" + +# test os.open +f = os.open(TESTFN_ENCODED, os.O_CREAT) +if not os.path.isfile(TESTFN_UNICODE) or \ + not os.path.isfile(TESTFN_ENCODED): + print "File doesn't exist after creating it" +os.close(f) +os.unlink(TESTFN_UNICODE) + +# Test directories etc +cwd = os.getcwd() +abs_encoded = os.path.abspath(TESTFN_ENCODED) + ".dir" +abs_unicode = os.path.abspath(TESTFN_UNICODE) + ".dir" +os.mkdir(abs_encoded) +try: + os.chdir(abs_encoded) + os.chdir(abs_unicode) +finally: + os.chdir(cwd) + os.rmdir(abs_unicode) +os.mkdir(abs_unicode) +try: + os.chdir(abs_encoded) + os.chdir(abs_unicode) +finally: + os.chdir(cwd) + os.rmdir(abs_encoded) +print "All the Unicode tests appeared to work" |