Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef8b654

Browse files
committed
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.
1 parent 342c65e commit ef8b654

7 files changed

Lines changed: 197 additions & 56 deletions

File tree

Lib/ntpath.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -404,21 +404,12 @@ def normpath(path):
404404
# Return an absolute path.
405405
def abspath(path):
406406
"""Return the absolute version of a path"""
407-
try:
408-
import win32api
409-
except ImportError:
410-
global abspath
411-
def _abspath(path):
412-
if not isabs(path):
413-
path = join(os.getcwd(), path)
414-
return normpath(path)
415-
abspath = _abspath
416-
return _abspath(path)
417407
if path: # Empty path must return current working directory.
408+
from nt import _getfullpathname
418409
try:
419-
path = win32api.GetFullPathName(path)
420-
except win32api.error:
421-
pass # Bad path - return unchanged.
410+
path = _getfullpathname(path)
411+
except WindowsError:
412+
pass # Bad path - return unchanged.
422413
else:
423414
path = os.getcwd()
424415
return normpath(path)

Lib/test/output/test_unicode_file

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test_unicode_file
2+
All the Unicode tests appeared to work

Lib/test/test_support.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ def fcmp(x, y): # fuzzy comparison function
6363
TESTFN = '$test'
6464
elif os.name != 'riscos':
6565
TESTFN = '@test'
66+
# Unicode name only used if TEST_FN_ENCODING exists for the platform.
67+
TESTFN_UNICODE=u"@test-\xe0\xf2" # 2 latin characters.
68+
if os.name=="nt":
69+
TESTFN_ENCODING="mbcs"
6670
else:
6771
TESTFN = 'test'
6872
del os

Lib/test/test_unicode_file.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Test some Unicode file name semantics
2+
# We dont test many operations on files other than
3+
# that their names can be used with Unicode characters.
4+
import os
5+
6+
from test_support import verify, TestSkipped, TESTFN_UNICODE
7+
try:
8+
from test_support import TESTFN_ENCODING
9+
except ImportError:
10+
raise TestSkipped("No Unicode filesystem semantics on this platform.")
11+
12+
TESTFN_ENCODED = TESTFN_UNICODE.encode(TESTFN_ENCODING)
13+
14+
# Check with creation as Unicode string.
15+
f = open(TESTFN_UNICODE, 'wb')
16+
if not os.path.isfile(TESTFN_UNICODE):
17+
print "File doesn't exist after creating it"
18+
19+
if not os.path.isfile(TESTFN_ENCODED):
20+
print "File doesn't exist (encoded string) after creating it"
21+
22+
f.close()
23+
24+
# Test stat and chmod
25+
if os.stat(TESTFN_ENCODED) != os.stat(TESTFN_UNICODE):
26+
print "os.stat() did not agree on the 2 filenames"
27+
os.chmod(TESTFN_ENCODED, 0777)
28+
os.chmod(TESTFN_UNICODE, 0777)
29+
30+
# Test rename
31+
os.rename(TESTFN_ENCODED, TESTFN_ENCODED + ".new")
32+
os.rename(TESTFN_UNICODE+".new", TESTFN_ENCODED)
33+
34+
os.unlink(TESTFN_ENCODED)
35+
if os.path.isfile(TESTFN_ENCODED) or \
36+
os.path.isfile(TESTFN_UNICODE):
37+
print "File exists after deleting it"
38+
39+
# Check with creation as encoded string.
40+
f = open(TESTFN_ENCODED, 'wb')
41+
if not os.path.isfile(TESTFN_UNICODE) or \
42+
not os.path.isfile(TESTFN_ENCODED):
43+
print "File doesn't exist after creating it"
44+
45+
path, base = os.path.split(os.path.abspath(TESTFN_ENCODED))
46+
if base not in os.listdir(path):
47+
print "Filename did not appear in os.listdir()"
48+
49+
f.close()
50+
os.unlink(TESTFN_UNICODE)
51+
if os.path.isfile(TESTFN_ENCODED) or \
52+
os.path.isfile(TESTFN_UNICODE):
53+
print "File exists after deleting it"
54+
55+
# test os.open
56+
f = os.open(TESTFN_ENCODED, os.O_CREAT)
57+
if not os.path.isfile(TESTFN_UNICODE) or \
58+
not os.path.isfile(TESTFN_ENCODED):
59+
print "File doesn't exist after creating it"
60+
os.close(f)
61+
os.unlink(TESTFN_UNICODE)
62+
63+
# Test directories etc
64+
cwd = os.getcwd()
65+
abs_encoded = os.path.abspath(TESTFN_ENCODED) + ".dir"
66+
abs_unicode = os.path.abspath(TESTFN_UNICODE) + ".dir"
67+
os.mkdir(abs_encoded)
68+
try:
69+
os.chdir(abs_encoded)
70+
os.chdir(abs_unicode)
71+
finally:
72+
os.chdir(cwd)
73+
os.rmdir(abs_unicode)
74+
os.mkdir(abs_unicode)
75+
try:
76+
os.chdir(abs_encoded)
77+
os.chdir(abs_unicode)
78+
finally:
79+
os.chdir(cwd)
80+
os.rmdir(abs_encoded)
81+
print "All the Unicode tests appeared to work"

0 commit comments

Comments
 (0)