Skip to content

Commit 9f5595d

Browse files
committed
Use backports.os for fsencode/decode everywhere #688
Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent 05f3190 commit 9f5595d

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

src/commoncode/command.py

+6-1
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

+11-9
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,10 +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()
81-
# normalize the encoding name
82-
FS_ENCODING = codecs.lookup(FS_ENCODING).name
83-
8486
# Paths can only be sanely handled as raw bytes on Linux
8587
PATH_TYPE = bytes if on_linux else unicode
8688
POSIX_PATH_SEP = b'/' if on_linux else '/'
@@ -219,8 +221,8 @@ def path_to_unicode(path):
219221
"""
220222
if isinstance(path, unicode):
221223
return path
222-
if TRACE: logger_debug('path_to_unicode:', osb.fsdecode(path))
223-
return osb.fsdecode(path)
224+
if TRACE: logger_debug('path_to_unicode:', fsdecode(path))
225+
return fsdecode(path)
224226

225227

226228
def path_to_bytes(path):
@@ -229,8 +231,8 @@ def path_to_bytes(path):
229231
"""
230232
if isinstance(path, bytes):
231233
return path
232-
if TRACE: logger_debug('path_to_bytes:' , repr(osb.fsencode(path)))
233-
return osb.fsencode(path)
234+
if TRACE: logger_debug('path_to_bytes:' , repr(fsencode(path)))
235+
return fsencode(path)
234236

235237

236238
def is_posixpath(location):

src/extractcode/libarchive2.py

+8-1
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

+8-2
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

0 commit comments

Comments
 (0)