Skip to content

Commit

Permalink
use sys.getfilesystemencoding()
Browse files Browse the repository at this point in the history
so we are in sync with os.fsencode/decode
  • Loading branch information
lazka committed Aug 18, 2016
1 parent 29e6da5 commit 68cca5f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 deletions senf/_fsnative.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# included in all copies or substantial portions of the Software.

import os
import locale
import sys
import ctypes

from . import _winapi as winapi
Expand All @@ -32,7 +32,7 @@ def _fsnative(text):
# no good way to handle a failure from the user side. Instead
# fall back to utf-8 which is the most likely the right choice in
# a mis-configured environment
encoding = _fsencoding()
encoding = _encoding()
try:
return text.encode(encoding)
except UnicodeEncodeError:
Expand Down Expand Up @@ -89,12 +89,12 @@ def __new__(cls, text=u""):
fsnative = _create_fsnative(fsnative_type)


def _fsencoding():
def _encoding():
"""The encoding used for paths, argv, environ, stdout and stdin"""

assert os.name != "nt", "only call on unix code paths"

return locale.getpreferredencoding() or "utf-8"
return sys.getfilesystemencoding() or "utf-8"


def path2fsn(path):
Expand All @@ -120,7 +120,7 @@ def path2fsn(path):
path = path.decode("ascii")
else:
if isinstance(path, unicode):
path = path.encode(_fsencoding())
path = path.encode(_encoding())
else:
# TODO: If it ever gets added to Python we should call os.fspath() here
if isinstance(path, bytes):
Expand Down Expand Up @@ -155,12 +155,12 @@ def fsn2text(path):
raise TypeError("path needs to be %s", fsnative_type.__name__)

if fsnative_type is bytes:
return path.decode(_fsencoding(), "replace")
return path.decode(_encoding(), "replace")
else:
if PY2 or os.name == "nt":
return path
else:
return os.fsencode(path).decode(_fsencoding(), "replace")
return os.fsencode(path).decode(_encoding(), "replace")


def fsn2bytes(path, encoding):
Expand Down
4 changes: 2 additions & 2 deletions senf/_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import ctypes
import re

from ._fsnative import _fsencoding, path2fsn
from ._fsnative import _encoding, path2fsn
from ._compat import text_type, PY2
from . import _winapi as winapi

Expand Down Expand Up @@ -56,7 +56,7 @@ def _print_default(objects, sep, end, file, flush):
if os.name == "nt":
encoding = "utf-8"
else:
encoding = _fsencoding()
encoding = _encoding()

if isinstance(sep, text_type):
sep = sep.encode(encoding, "replace")
Expand Down

0 comments on commit 68cca5f

Please sign in to comment.