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

Skip to content

Commit 3d85a6f

Browse files
author
Victor Stinner
committed
Set TESTFN_UNENCODEABLE on non-Windows OSes
* Use 0xff byte on non-Windows OSes * mbcs is now really strict by default: i closed the issue #850997, so use the filesystem encoding and not Latin-1 * Rename TESTFN_UNICODE_UNENCODEABLE to TESTFN_UNENCODEABLE
1 parent 994addc commit 3d85a6f

2 files changed

Lines changed: 37 additions & 27 deletions

File tree

Lib/test/support.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -382,29 +382,38 @@ def fcmp(x, y): # fuzzy comparison function
382382
# file system encoding, but *not* with the default (ascii) encoding
383383
TESTFN_UNICODE = TESTFN + "-\xe0\xf2"
384384
TESTFN_ENCODING = sys.getfilesystemencoding()
385-
# TESTFN_UNICODE_UNENCODEABLE is a filename that should *not* be
386-
# able to be encoded by *either* the default or filesystem encoding.
387-
# This test really only makes sense on Windows NT platforms
388-
# which have special Unicode support in posixmodule.
389-
if (not hasattr(sys, "getwindowsversion") or
390-
sys.getwindowsversion()[3] < 2): # 0=win32s or 1=9x/ME
391-
TESTFN_UNICODE_UNENCODEABLE = None
385+
386+
# TESTFN_UNENCODEABLE is a filename (str type) that should *not* be able to be
387+
# encoded by the filesystem encoding (in strict mode). It can be None if we
388+
# cannot generate such filename.
389+
if os.name in ('nt', 'ce'):
390+
if sys.getwindowsversion().platform < 2:
391+
# win32s (0) or Windows 9x/ME (1)
392+
TESTFN_UNENCODEABLE = None
393+
else:
394+
# Japanese characters (I think - from bug 846133)
395+
TESTFN_UNENCODEABLE = TESTFN + "-\u5171\u6709\u3055\u308c\u308b"
396+
try:
397+
TESTFN_UNENCODEABLE.encode(TESTFN_ENCODING)
398+
except UnicodeEncodeError:
399+
pass
400+
else:
401+
print('WARNING: The filename %r CAN be encoded by the filesystem encoding (%s). '
402+
'Unicode filename tests may not be effective'
403+
% (TESTFN_UNENCODEABLE, TESTFN_ENCODING))
404+
TESTFN_UNENCODEABLE = None
392405
else:
393-
# Japanese characters (I think - from bug 846133)
394-
TESTFN_UNICODE_UNENCODEABLE = TESTFN + "-\u5171\u6709\u3055\u308c\u308b"
395406
try:
396-
# XXX - Note - should be using TESTFN_ENCODING here - but for
397-
# Windows, "mbcs" currently always operates as if in
398-
# errors=ignore' mode - hence we get '?' characters rather than
399-
# the exception. 'Latin1' operates as we expect - ie, fails.
400-
# See [ 850997 ] mbcs encoding ignores errors
401-
TESTFN_UNICODE_UNENCODEABLE.encode("Latin1")
402-
except UnicodeEncodeError:
403-
pass
407+
# ascii and utf-8 cannot encode the byte 0xff
408+
b'\xff'.decode(TESTFN_ENCODING)
409+
except UnicodeDecodeError:
410+
# 0xff will be encoded using the surrogate character u+DCFF
411+
TESTFN_UNENCODEABLE = TESTFN_UNICODE \
412+
+ b'-\xff'.decode(TESTFN_ENCODING, 'surrogateescape')
404413
else:
405-
print('WARNING: The filename %r CAN be encoded by the filesystem. '
406-
'Unicode filename tests may not be effective'
407-
% TESTFN_UNICODE_UNENCODEABLE)
414+
# File system encoding (eg. ISO-8859-* encodings) can encode
415+
# the byte 0xff. Skip some unicode filename tests.
416+
TESTFN_UNENCODEABLE = None
408417

409418
# Save the initial cwd
410419
SAVEDCWD = os.getcwd()

Lib/test/test_unicode_file.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import unicodedata
66

77
import unittest
8-
from test.support import run_unittest, TESTFN_UNICODE, rmtree
9-
from test.support import TESTFN_ENCODING, TESTFN_UNICODE_UNENCODEABLE
8+
from test.support import (run_unittest, rmtree,
9+
TESTFN_ENCODING, TESTFN_UNICODE, TESTFN_UNENCODEABLE)
10+
1011
try:
1112
TESTFN_UNICODE.encode(TESTFN_ENCODING)
1213
except (UnicodeError, TypeError):
@@ -146,8 +147,8 @@ def _test_equivalent(self, filename1, filename2):
146147
# _test functions with each of the filename combinations we wish to test
147148
def test_single_files(self):
148149
self._test_single(TESTFN_UNICODE)
149-
if TESTFN_UNICODE_UNENCODEABLE is not None:
150-
self._test_single(TESTFN_UNICODE_UNENCODEABLE)
150+
if TESTFN_UNENCODEABLE is not None:
151+
self._test_single(TESTFN_UNENCODEABLE)
151152

152153
def test_directories(self):
153154
# For all 'equivalent' combinations:
@@ -156,9 +157,9 @@ def test_directories(self):
156157
ext = ".dir"
157158
self._do_directory(TESTFN_UNICODE+ext, TESTFN_UNICODE+ext, False)
158159
# Our directory name that can't use a non-unicode name.
159-
if TESTFN_UNICODE_UNENCODEABLE is not None:
160-
self._do_directory(TESTFN_UNICODE_UNENCODEABLE+ext,
161-
TESTFN_UNICODE_UNENCODEABLE+ext,
160+
if TESTFN_UNENCODEABLE is not None:
161+
self._do_directory(TESTFN_UNENCODEABLE+ext,
162+
TESTFN_UNENCODEABLE+ext,
162163
False)
163164

164165
def test_main():

0 commit comments

Comments
 (0)