Skip to content

Commit 6b93456

Browse files
authored
Merge pull request #758 from nexB/688-fsencode-fsdecode-755-unicode-test-files
Ensure fsencode/fsdecode works on ASCII FS. Use tarball for unicode test files
2 parents 8186fa1 + 4ee8053 commit 6b93456

22 files changed

Lines changed: 64 additions & 37 deletions

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def read(*names, **kwargs):
143143
'bz2file >= 0.98',
144144

145145
# commoncode
146-
'backports.os == 0.1rc1',
146+
'backports.os == 0.1.1',
147147
'future >= 0.16.0, < 0.17.0',
148148
'text-unidecode >= 1.0, < 2.0',
149149

src/commoncode/command.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@
5353
# Python 3
5454
unicode = str
5555

56+
try:
57+
from os import fsencode
58+
except ImportError:
59+
from backports.os import fsencode
60+
5661

5762
"""
5863
Minimal wrapper for executing external commands in sub-processes. The approach
@@ -344,7 +349,7 @@ def load_lib(libname, root_dir):
344349
if os.path.exists(so):
345350
if not isinstance(so, bytes):
346351
# ensure that the path is not Unicode...
347-
so = so.encode(fileutils.FS_ENCODING)
352+
so = fsencode(so)
348353
lib = ctypes.CDLL(so)
349354
if lib and lib._name:
350355
return lib

src/commoncode/fileutils.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
# Python 3
3636
unicode = str
3737

38+
try:
39+
from os import fsencode
40+
except ImportError:
41+
from backports.os import fsencode
42+
from backports.os import fsdecode
43+
44+
3845
import codecs
3946
import errno
4047
import os
@@ -45,7 +52,6 @@
4552
import sys
4653
import tempfile
4754

48-
from backports import os as osb
4955

5056
from commoncode import filetype
5157
from commoncode.filetype import is_rwx
@@ -77,8 +83,6 @@ def logger_debug(*args):
7783
return logger.debug(' '.join(isinstance(a, basestring) and a or repr(a) for a in args))
7884

7985

80-
FS_ENCODING = sys.getfilesystemencoding() or sys.getdefaultencoding()
81-
8286
# Paths can only be sanely handled as raw bytes on Linux
8387
PATH_TYPE = bytes if on_linux else unicode
8488
POSIX_PATH_SEP = b'/' if on_linux else '/'
@@ -217,8 +221,8 @@ def path_to_unicode(path):
217221
"""
218222
if isinstance(path, unicode):
219223
return path
220-
if TRACE: logger_debug('path_to_unicode:', osb.fsdecode(path))
221-
return osb.fsdecode(path)
224+
if TRACE: logger_debug('path_to_unicode:', fsdecode(path))
225+
return fsdecode(path)
222226

223227

224228
def path_to_bytes(path):
@@ -227,8 +231,8 @@ def path_to_bytes(path):
227231
"""
228232
if isinstance(path, bytes):
229233
return path
230-
if TRACE: logger_debug('path_to_bytes:' , repr(osb.fsencode(path)))
231-
return osb.fsencode(path)
234+
if TRACE: logger_debug('path_to_bytes:' , repr(fsencode(path)))
235+
return fsencode(path)
232236

233237

234238
def is_posixpath(location):

src/commoncode/testcase.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828
from __future__ import division
2929
from __future__ import unicode_literals
3030

31-
from unittest import TestCase as TestCaseClass
32-
3331
import filecmp
32+
from functools import partial
3433
import os
3534
import shutil
3635
import stat
3736
import sys
3837
import tarfile
38+
from unittest import TestCase as TestCaseClass
3939
import zipfile
4040

4141
from commoncode import fileutils
@@ -278,19 +278,27 @@ def extract_test_tar(self, test_path, verbatim=False):
278278
def extract_test_tar_raw(self, test_path, *args, **kwargs):
279279
return self.__extract(test_path, extract_tar_raw)
280280

281+
def extract_test_tar_unicode(self, test_path, *args, **kwargs):
282+
return self.__extract(test_path, extract_tar_uni)
281283

282-
def extract_tar_raw(test_path, target_dir, *args, **kwargs):
284+
285+
def _extract_tar_raw(test_path, target_dir, to_bytes, *args, **kwargs):
283286
"""
284287
Raw simplified extract for certain really weird paths and file
285288
names.
286289
"""
287-
# use bytes for paths on ALL OSes (though this may fail on macOS)
288-
target_dir = path_to_bytes(target_dir)
289-
test_path = path_to_bytes(test_path)
290+
if to_bytes:
291+
# use bytes for paths on ALL OSes (though this may fail on macOS)
292+
target_dir = path_to_bytes(target_dir)
293+
test_path = path_to_bytes(test_path)
290294
tar = tarfile.open(test_path)
291295
tar.extractall(path=target_dir)
292296
tar.close()
293297

298+
extract_tar_raw = partial(_extract_tar_raw, to_bytes=True)
299+
300+
extract_tar_uni = partial(_extract_tar_raw, to_bytes=False)
301+
294302

295303
def extract_tar(location, target_dir, verbatim=False, *args, **kwargs):
296304
"""

src/extractcode/libarchive2.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@
5151
from extractcode import ExtractErrorPasswordProtected
5252

5353

54+
# Python 2 and 3 support
55+
try:
56+
from os import fsencode
57+
except ImportError:
58+
from backports.os import fsencode
59+
60+
5461
logger = logging.getLogger(__name__)
5562
DEBUG = False
5663
# logging.basicConfig(level=logging.DEBUG)
@@ -104,7 +111,7 @@ def load_lib():
104111
if os.path.exists(libarchive):
105112
if not isinstance(libarchive, bytes):
106113
# ensure that the path is not Unicode...
107-
libarchive = libarchive.encode(fileutils.FS_ENCODING)
114+
libarchive = fsencode(libarchive)
108115
lib = ctypes.CDLL(libarchive)
109116
if lib and lib._name:
110117
return lib

src/typecode/magic2.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,17 @@
4848

4949
import os.path
5050
import ctypes
51-
import sys
5251

5352
from commoncode import system
5453
from commoncode import command
5554

55+
# Python 2 and 3 support
56+
try:
57+
from os import fsencode
58+
except ImportError:
59+
from backports.os import fsencode
60+
61+
5662
"""
5763
magic2 is minimal and specialized wrapper around a vendored libmagic file
5864
identification library. This is NOT thread-safe. It is based on python-magic
@@ -204,7 +210,7 @@ def load_lib():
204210
if os.path.exists(magic_so):
205211
if not isinstance(magic_so, bytes):
206212
# ensure that the path is not Unicode...
207-
magic_so = magic_so.encode(sys.getfilesystemencoding() or sys.getdefaultencoding())
213+
magic_so = fsencode(magic_so)
208214
lib = ctypes.CDLL(magic_so)
209215
if lib and lib._name:
210216
return lib
639 Bytes
Binary file not shown.

tests/scancode/data/unicode_fixtures/.hidden

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/scancode/data/unicode_fixtures/empty.txt

Whitespace-only changes.

tests/scancode/data/unicode_fixtures/foo bar

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)