2010-05-20 00:41:05 +00:00
|
|
|
"""
|
|
|
|
Functions used to convert inputs from whatever encoding used in the system to
|
|
|
|
unicode and back.
|
|
|
|
"""
|
|
|
|
|
2015-03-03 20:04:57 +00:00
|
|
|
import sys, os, re, locale
|
|
|
|
from types import NoneType
|
|
|
|
|
2015-10-01 21:40:10 +00:00
|
|
|
from allmydata.util.assertutil import precondition, _assert
|
2010-05-20 00:41:05 +00:00
|
|
|
from twisted.python import usage
|
2015-04-28 19:43:09 +00:00
|
|
|
from twisted.python.filepath import FilePath
|
2010-06-16 04:20:12 +00:00
|
|
|
from allmydata.util import log
|
2010-07-21 23:15:07 +00:00
|
|
|
from allmydata.util.fileutil import abspath_expanduser_unicode
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2010-06-07 01:02:15 +00:00
|
|
|
|
2010-07-25 08:32:16 +00:00
|
|
|
def canonical_encoding(encoding):
|
2010-06-07 01:02:15 +00:00
|
|
|
if encoding is None:
|
2010-06-16 04:20:12 +00:00
|
|
|
log.msg("Warning: falling back to UTF-8 encoding.", level=log.WEIRD)
|
2010-06-07 01:02:15 +00:00
|
|
|
encoding = 'utf-8'
|
|
|
|
encoding = encoding.lower()
|
|
|
|
if encoding == "cp65001":
|
|
|
|
encoding = 'utf-8'
|
2010-06-16 04:20:12 +00:00
|
|
|
elif encoding == "us-ascii" or encoding == "646" or encoding == "ansi_x3.4-1968":
|
2010-06-07 01:02:15 +00:00
|
|
|
encoding = 'ascii'
|
|
|
|
|
2010-07-25 08:32:16 +00:00
|
|
|
return encoding
|
|
|
|
|
|
|
|
def check_encoding(encoding):
|
2010-06-07 01:02:15 +00:00
|
|
|
# sometimes Python returns an encoding name that it doesn't support for conversion
|
|
|
|
# fail early if this happens
|
|
|
|
try:
|
|
|
|
u"test".encode(encoding)
|
2010-06-09 01:33:02 +00:00
|
|
|
except (LookupError, AttributeError):
|
2010-06-07 01:02:15 +00:00
|
|
|
raise AssertionError("The character encoding '%s' is not supported for conversion." % (encoding,))
|
|
|
|
|
|
|
|
filesystem_encoding = None
|
2011-06-29 18:53:56 +00:00
|
|
|
io_encoding = None
|
2010-06-07 01:02:15 +00:00
|
|
|
is_unicode_platform = False
|
2015-04-28 19:43:09 +00:00
|
|
|
use_unicode_filepath = False
|
2010-06-07 01:02:15 +00:00
|
|
|
|
|
|
|
def _reload():
|
2015-04-28 19:43:09 +00:00
|
|
|
global filesystem_encoding, io_encoding, is_unicode_platform, use_unicode_filepath
|
2010-06-07 01:02:15 +00:00
|
|
|
|
2010-07-25 08:32:16 +00:00
|
|
|
filesystem_encoding = canonical_encoding(sys.getfilesystemencoding())
|
|
|
|
check_encoding(filesystem_encoding)
|
2010-06-16 04:20:12 +00:00
|
|
|
|
2010-06-07 01:02:15 +00:00
|
|
|
if sys.platform == 'win32':
|
2010-07-25 08:32:16 +00:00
|
|
|
# On Windows we install UTF-8 stream wrappers for sys.stdout and
|
|
|
|
# sys.stderr, and reencode the arguments as UTF-8 (see scripts/runner.py).
|
2011-06-29 18:53:56 +00:00
|
|
|
io_encoding = 'utf-8'
|
2010-06-07 01:02:15 +00:00
|
|
|
else:
|
2011-06-29 18:53:56 +00:00
|
|
|
ioenc = None
|
2010-07-25 08:32:16 +00:00
|
|
|
if hasattr(sys.stdout, 'encoding'):
|
2011-06-29 18:53:56 +00:00
|
|
|
ioenc = sys.stdout.encoding
|
|
|
|
if ioenc is None:
|
2010-07-25 08:32:16 +00:00
|
|
|
try:
|
2011-06-29 18:53:56 +00:00
|
|
|
ioenc = locale.getpreferredencoding()
|
2010-07-25 08:32:16 +00:00
|
|
|
except Exception:
|
|
|
|
pass # work around <http://bugs.python.org/issue1443504>
|
2011-06-29 18:53:56 +00:00
|
|
|
io_encoding = canonical_encoding(ioenc)
|
2010-07-25 08:32:16 +00:00
|
|
|
|
2011-06-29 18:53:56 +00:00
|
|
|
check_encoding(io_encoding)
|
2010-07-25 08:32:16 +00:00
|
|
|
|
2010-06-07 01:02:15 +00:00
|
|
|
is_unicode_platform = sys.platform in ["win32", "darwin"]
|
|
|
|
|
2015-10-01 21:40:10 +00:00
|
|
|
# Despite the Unicode-mode FilePath support added to Twisted in
|
|
|
|
# <https://twistedmatrix.com/trac/ticket/7805>, we can't yet use
|
|
|
|
# Unicode-mode FilePaths with INotify on non-Windows platforms
|
|
|
|
# due to <https://twistedmatrix.com/trac/ticket/7928>.
|
|
|
|
use_unicode_filepath = sys.platform == "win32"
|
2015-04-28 19:43:09 +00:00
|
|
|
|
2010-06-07 01:02:15 +00:00
|
|
|
_reload()
|
|
|
|
|
|
|
|
|
|
|
|
def get_filesystem_encoding():
|
2010-05-20 00:41:05 +00:00
|
|
|
"""
|
2010-06-07 01:02:15 +00:00
|
|
|
Returns expected encoding for local filenames.
|
2010-05-20 00:41:05 +00:00
|
|
|
"""
|
2010-06-07 01:02:15 +00:00
|
|
|
return filesystem_encoding
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2011-06-29 18:53:56 +00:00
|
|
|
def get_io_encoding():
|
2010-06-07 01:02:15 +00:00
|
|
|
"""
|
2011-06-29 18:53:56 +00:00
|
|
|
Returns expected encoding for writing to stdout or stderr, and for arguments in sys.argv.
|
2010-06-07 01:02:15 +00:00
|
|
|
"""
|
2011-06-29 18:53:56 +00:00
|
|
|
return io_encoding
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2010-06-07 01:02:15 +00:00
|
|
|
def argv_to_unicode(s):
|
|
|
|
"""
|
|
|
|
Decode given argv element to unicode. If this fails, raise a UsageError.
|
|
|
|
"""
|
2010-05-20 00:41:05 +00:00
|
|
|
precondition(isinstance(s, str), s)
|
|
|
|
|
|
|
|
try:
|
2011-06-29 18:53:56 +00:00
|
|
|
return unicode(s, io_encoding)
|
2010-05-20 00:41:05 +00:00
|
|
|
except UnicodeDecodeError:
|
2010-06-07 01:02:15 +00:00
|
|
|
raise usage.UsageError("Argument %s cannot be decoded as %s." %
|
2011-06-29 18:53:56 +00:00
|
|
|
(quote_output(s), io_encoding))
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2015-12-28 20:45:14 +00:00
|
|
|
def argv_to_abspath(s, **kwargs):
|
2010-07-21 23:15:07 +00:00
|
|
|
"""
|
|
|
|
Convenience function to decode an argv element to an absolute path, with ~ expanded.
|
|
|
|
If this fails, raise a UsageError.
|
|
|
|
"""
|
2015-12-28 20:45:14 +00:00
|
|
|
decoded = argv_to_unicode(s)
|
|
|
|
if decoded.startswith(u'-'):
|
|
|
|
raise usage.UsageError("Path argument %s cannot start with '-'.\nUse %s if you intended to refer to a file."
|
|
|
|
% (quote_output(s), quote_output(os.path.join('.', s))))
|
|
|
|
return abspath_expanduser_unicode(decoded, **kwargs)
|
2010-07-21 23:15:07 +00:00
|
|
|
|
2010-07-25 01:03:18 +00:00
|
|
|
def unicode_to_argv(s, mangle=False):
|
|
|
|
"""
|
|
|
|
Encode the given Unicode argument as a bytestring.
|
|
|
|
If the argument is to be passed to a different process, then the 'mangle' argument
|
|
|
|
should be true; on Windows, this uses a mangled encoding that will be reversed by
|
|
|
|
code in runner.py.
|
|
|
|
"""
|
|
|
|
precondition(isinstance(s, unicode), s)
|
|
|
|
|
|
|
|
if mangle and sys.platform == "win32":
|
|
|
|
# This must be the same as 'mangle' in bin/tahoe-script.template.
|
|
|
|
return str(re.sub(ur'[^\x20-\x7F]', lambda m: u'\x7F%x;' % (ord(m.group(0)),), s))
|
|
|
|
else:
|
2011-06-29 18:53:56 +00:00
|
|
|
return s.encode(io_encoding)
|
2010-07-25 01:03:18 +00:00
|
|
|
|
2010-05-20 00:41:05 +00:00
|
|
|
def unicode_to_url(s):
|
|
|
|
"""
|
|
|
|
Encode an unicode object used in an URL.
|
|
|
|
"""
|
2010-06-07 01:02:15 +00:00
|
|
|
# According to RFC 2718, non-ascii characters in URLs must be UTF-8 encoded.
|
2010-05-20 00:41:05 +00:00
|
|
|
|
2010-06-07 01:02:15 +00:00
|
|
|
# FIXME
|
|
|
|
return to_str(s)
|
|
|
|
#precondition(isinstance(s, unicode), s)
|
|
|
|
#return s.encode('utf-8')
|
|
|
|
|
|
|
|
def to_str(s):
|
|
|
|
if s is None or isinstance(s, str):
|
|
|
|
return s
|
2010-05-20 00:41:05 +00:00
|
|
|
return s.encode('utf-8')
|
|
|
|
|
2015-03-03 20:04:57 +00:00
|
|
|
def from_utf8_or_none(s):
|
|
|
|
precondition(isinstance(s, (NoneType, str)), s)
|
|
|
|
if s is None:
|
|
|
|
return s
|
|
|
|
return s.decode('utf-8')
|
|
|
|
|
2010-07-23 07:53:14 +00:00
|
|
|
PRINTABLE_ASCII = re.compile(r'^[\n\r\x20-\x7E]*$', re.DOTALL)
|
|
|
|
PRINTABLE_8BIT = re.compile(r'^[\n\r\x20-\x7E\x80-\xFF]*$', re.DOTALL)
|
2010-06-07 01:02:15 +00:00
|
|
|
|
|
|
|
def is_printable_ascii(s):
|
|
|
|
return PRINTABLE_ASCII.search(s) is not None
|
|
|
|
|
|
|
|
def unicode_to_output(s):
|
2010-05-20 00:41:05 +00:00
|
|
|
"""
|
2010-06-07 01:02:15 +00:00
|
|
|
Encode an unicode object for representation on stdout or stderr.
|
2010-05-20 00:41:05 +00:00
|
|
|
"""
|
|
|
|
precondition(isinstance(s, unicode), s)
|
|
|
|
|
2010-06-07 01:02:15 +00:00
|
|
|
try:
|
2011-06-29 18:53:56 +00:00
|
|
|
out = s.encode(io_encoding)
|
2010-06-09 05:54:48 +00:00
|
|
|
except (UnicodeEncodeError, UnicodeDecodeError):
|
2011-06-29 18:53:56 +00:00
|
|
|
raise UnicodeEncodeError(io_encoding, s, 0, 0,
|
2010-06-07 01:02:15 +00:00
|
|
|
"A string could not be encoded as %s for output to the terminal:\n%r" %
|
2011-06-29 18:53:56 +00:00
|
|
|
(io_encoding, repr(s)))
|
2010-06-07 01:02:15 +00:00
|
|
|
|
|
|
|
if PRINTABLE_8BIT.search(out) is None:
|
2011-06-29 18:53:56 +00:00
|
|
|
raise UnicodeEncodeError(io_encoding, s, 0, 0,
|
2010-06-07 01:02:15 +00:00
|
|
|
"A string encoded as %s for output to the terminal contained unsafe bytes:\n%r" %
|
2011-06-29 18:53:56 +00:00
|
|
|
(io_encoding, repr(s)))
|
2010-06-07 01:02:15 +00:00
|
|
|
return out
|
|
|
|
|
2010-07-23 07:53:14 +00:00
|
|
|
|
2013-01-04 05:31:07 +00:00
|
|
|
def _unicode_escape(m, quote_newlines):
|
2010-07-23 07:53:14 +00:00
|
|
|
u = m.group(0)
|
2013-01-04 05:31:07 +00:00
|
|
|
if u == u'"' or u == u'$' or u == u'`' or u == u'\\':
|
2010-07-23 07:53:14 +00:00
|
|
|
return u'\\' + u
|
2013-01-04 05:31:07 +00:00
|
|
|
elif u == u'\n' and not quote_newlines:
|
|
|
|
return u
|
2010-07-23 07:53:14 +00:00
|
|
|
if len(u) == 2:
|
|
|
|
codepoint = (ord(u[0])-0xD800)*0x400 + ord(u[1])-0xDC00 + 0x10000
|
|
|
|
else:
|
|
|
|
codepoint = ord(u)
|
|
|
|
if codepoint > 0xFFFF:
|
|
|
|
return u'\\U%08x' % (codepoint,)
|
|
|
|
elif codepoint > 0xFF:
|
|
|
|
return u'\\u%04x' % (codepoint,)
|
|
|
|
else:
|
|
|
|
return u'\\x%02x' % (codepoint,)
|
|
|
|
|
2013-01-04 05:31:07 +00:00
|
|
|
def _str_escape(m, quote_newlines):
|
2010-07-23 07:53:14 +00:00
|
|
|
c = m.group(0)
|
|
|
|
if c == '"' or c == '$' or c == '`' or c == '\\':
|
|
|
|
return '\\' + c
|
2013-01-04 05:31:07 +00:00
|
|
|
elif c == '\n' and not quote_newlines:
|
|
|
|
return c
|
2010-07-23 07:53:14 +00:00
|
|
|
else:
|
|
|
|
return '\\x%02x' % (ord(c),)
|
|
|
|
|
2013-01-04 05:31:07 +00:00
|
|
|
MUST_DOUBLE_QUOTE_NL = re.compile(ur'[^\x20-\x26\x28-\x7E\u00A0-\uD7FF\uE000-\uFDCF\uFDF0-\uFFFC]', re.DOTALL)
|
|
|
|
MUST_DOUBLE_QUOTE = re.compile(ur'[^\n\x20-\x26\x28-\x7E\u00A0-\uD7FF\uE000-\uFDCF\uFDF0-\uFFFC]', re.DOTALL)
|
2010-07-23 07:53:14 +00:00
|
|
|
|
|
|
|
# if we must double-quote, then we have to escape ", $ and `, but need not escape '
|
|
|
|
ESCAPABLE_UNICODE = re.compile(ur'([\uD800-\uDBFF][\uDC00-\uDFFF])|' # valid surrogate pairs
|
|
|
|
ur'[^ !#\x25-\x5B\x5D-\x5F\x61-\x7E\u00A0-\uD7FF\uE000-\uFDCF\uFDF0-\uFFFC]',
|
|
|
|
re.DOTALL)
|
|
|
|
|
|
|
|
ESCAPABLE_8BIT = re.compile( r'[^ !#\x25-\x5B\x5D-\x5F\x61-\x7E]', re.DOTALL)
|
|
|
|
|
2013-01-04 05:31:07 +00:00
|
|
|
def quote_output(s, quotemarks=True, quote_newlines=None, encoding=None):
|
2010-05-20 00:41:05 +00:00
|
|
|
"""
|
2010-06-07 01:02:15 +00:00
|
|
|
Encode either a Unicode string or a UTF-8-encoded bytestring for representation
|
|
|
|
on stdout or stderr, tolerating errors. If 'quotemarks' is True, the string is
|
2010-07-23 07:53:14 +00:00
|
|
|
always quoted; otherwise, it is quoted only if necessary to avoid ambiguity or
|
2013-01-04 05:31:07 +00:00
|
|
|
control bytes in the output. (Newlines are counted as control bytes iff
|
|
|
|
quote_newlines is True.)
|
|
|
|
|
2010-07-23 07:53:14 +00:00
|
|
|
Quoting may use either single or double quotes. Within single quotes, all
|
|
|
|
characters stand for themselves, and ' will not appear. Within double quotes,
|
|
|
|
Python-compatible backslash escaping is used.
|
2013-01-04 05:31:07 +00:00
|
|
|
|
|
|
|
If not explicitly given, quote_newlines is True when quotemarks is True.
|
2010-05-20 00:41:05 +00:00
|
|
|
"""
|
2010-06-07 01:02:15 +00:00
|
|
|
precondition(isinstance(s, (str, unicode)), s)
|
2013-01-04 05:31:07 +00:00
|
|
|
if quote_newlines is None:
|
|
|
|
quote_newlines = quotemarks
|
2010-06-07 01:02:15 +00:00
|
|
|
|
|
|
|
if isinstance(s, str):
|
|
|
|
try:
|
|
|
|
s = s.decode('utf-8')
|
|
|
|
except UnicodeDecodeError:
|
2013-01-04 05:31:07 +00:00
|
|
|
return 'b"%s"' % (ESCAPABLE_8BIT.sub(lambda m: _str_escape(m, quote_newlines), s),)
|
2010-06-07 01:02:15 +00:00
|
|
|
|
2013-01-04 05:31:07 +00:00
|
|
|
must_double_quote = quote_newlines and MUST_DOUBLE_QUOTE_NL or MUST_DOUBLE_QUOTE
|
|
|
|
if must_double_quote.search(s) is None:
|
2010-07-23 07:53:14 +00:00
|
|
|
try:
|
2011-06-29 18:53:56 +00:00
|
|
|
out = s.encode(encoding or io_encoding)
|
2010-07-23 07:53:14 +00:00
|
|
|
if quotemarks or out.startswith('"'):
|
|
|
|
return "'%s'" % (out,)
|
|
|
|
else:
|
|
|
|
return out
|
|
|
|
except (UnicodeDecodeError, UnicodeEncodeError):
|
|
|
|
pass
|
|
|
|
|
2013-01-04 05:31:07 +00:00
|
|
|
escaped = ESCAPABLE_UNICODE.sub(lambda m: _unicode_escape(m, quote_newlines), s)
|
2011-06-29 18:53:56 +00:00
|
|
|
return '"%s"' % (escaped.encode(encoding or io_encoding, 'backslashreplace'),)
|
2010-06-07 01:02:15 +00:00
|
|
|
|
|
|
|
def quote_path(path, quotemarks=True):
|
2013-01-04 05:31:07 +00:00
|
|
|
return quote_output("/".join(map(to_str, path)), quotemarks=quotemarks, quote_newlines=True)
|
2010-06-07 01:02:15 +00:00
|
|
|
|
2015-01-30 00:04:11 +00:00
|
|
|
def quote_local_unicode_path(path, quotemarks=True):
|
|
|
|
precondition(isinstance(path, unicode), path)
|
|
|
|
|
|
|
|
if sys.platform == "win32" and path.startswith(u"\\\\?\\"):
|
|
|
|
path = path[4 :]
|
|
|
|
if path.startswith(u"UNC\\"):
|
|
|
|
path = u"\\\\" + path[4 :]
|
|
|
|
|
|
|
|
return quote_output(path, quotemarks=quotemarks, quote_newlines=True)
|
|
|
|
|
2015-10-01 21:40:10 +00:00
|
|
|
def quote_filepath(path, quotemarks=True):
|
|
|
|
return quote_local_unicode_path(unicode_from_filepath(path), quotemarks=quotemarks)
|
|
|
|
|
|
|
|
def extend_filepath(fp, segments):
|
|
|
|
# We cannot use FilePath.preauthChild, because
|
|
|
|
# * it has the security flaw described in <https://twistedmatrix.com/trac/ticket/6527>;
|
|
|
|
# * it may return a FilePath in the wrong mode.
|
|
|
|
|
|
|
|
for segment in segments:
|
|
|
|
fp = fp.child(segment)
|
|
|
|
|
|
|
|
if isinstance(fp.path, unicode) and not use_unicode_filepath:
|
|
|
|
return FilePath(fp.path.encode(filesystem_encoding))
|
|
|
|
else:
|
|
|
|
return fp
|
|
|
|
|
2015-04-28 19:43:09 +00:00
|
|
|
def to_filepath(path):
|
2015-11-02 22:00:31 +00:00
|
|
|
precondition(isinstance(path, unicode if use_unicode_filepath else basestring),
|
|
|
|
path=path)
|
2015-04-28 19:43:09 +00:00
|
|
|
|
|
|
|
if isinstance(path, unicode) and not use_unicode_filepath:
|
|
|
|
path = path.encode(filesystem_encoding)
|
|
|
|
|
2015-11-02 22:00:31 +00:00
|
|
|
if sys.platform == "win32":
|
|
|
|
_assert(isinstance(path, unicode), path=path)
|
|
|
|
if path.startswith(u"\\\\?\\") and len(path) > 4:
|
|
|
|
# FilePath normally strips trailing path separators, but not in this case.
|
|
|
|
path = path.rstrip(u"\\")
|
|
|
|
|
2015-04-28 19:43:09 +00:00
|
|
|
return FilePath(path)
|
|
|
|
|
2015-10-01 21:40:10 +00:00
|
|
|
def _decode(s):
|
|
|
|
precondition(isinstance(s, basestring), s=s)
|
|
|
|
|
|
|
|
if isinstance(s, bytes):
|
|
|
|
return s.decode(filesystem_encoding)
|
|
|
|
else:
|
|
|
|
return s
|
|
|
|
|
2015-04-28 19:43:09 +00:00
|
|
|
def unicode_from_filepath(fp):
|
|
|
|
precondition(isinstance(fp, FilePath), fp=fp)
|
2015-10-01 21:40:10 +00:00
|
|
|
return _decode(fp.path)
|
2015-04-28 19:43:09 +00:00
|
|
|
|
2015-10-01 21:40:10 +00:00
|
|
|
def unicode_segments_from(base_fp, ancestor_fp):
|
|
|
|
precondition(isinstance(base_fp, FilePath), base_fp=base_fp)
|
|
|
|
precondition(isinstance(ancestor_fp, FilePath), ancestor_fp=ancestor_fp)
|
2015-04-28 19:43:09 +00:00
|
|
|
|
2015-11-02 21:41:10 +00:00
|
|
|
return base_fp.asTextMode().segmentsFrom(ancestor_fp.asTextMode())
|
2010-06-07 01:02:15 +00:00
|
|
|
|
|
|
|
def unicode_platform():
|
|
|
|
"""
|
|
|
|
Does the current platform handle Unicode filenames natively?
|
|
|
|
"""
|
|
|
|
return is_unicode_platform
|
2010-07-18 01:34:35 +00:00
|
|
|
|
|
|
|
class FilenameEncodingError(Exception):
|
|
|
|
"""
|
|
|
|
Filename cannot be encoded using the current encoding of your filesystem
|
|
|
|
(%s). Please configure your locale correctly or rename this file.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def listdir_unicode_fallback(path):
|
|
|
|
"""
|
|
|
|
This function emulates a fallback Unicode API similar to one available
|
|
|
|
under Windows or MacOS X.
|
|
|
|
|
|
|
|
If badly encoded filenames are encountered, an exception is raised.
|
|
|
|
"""
|
|
|
|
precondition(isinstance(path, unicode), path)
|
|
|
|
|
|
|
|
try:
|
|
|
|
byte_path = path.encode(filesystem_encoding)
|
|
|
|
except (UnicodeEncodeError, UnicodeDecodeError):
|
|
|
|
raise FilenameEncodingError(path)
|
|
|
|
|
|
|
|
try:
|
|
|
|
return [unicode(fn, filesystem_encoding) for fn in os.listdir(byte_path)]
|
2018-04-26 19:16:00 +00:00
|
|
|
except UnicodeDecodeError as e:
|
|
|
|
raise FilenameEncodingError(e.object)
|
2010-07-18 01:34:35 +00:00
|
|
|
|
|
|
|
def listdir_unicode(path):
|
|
|
|
"""
|
|
|
|
Wrapper around listdir() which provides safe access to the convenient
|
|
|
|
Unicode API even under platforms that don't provide one natively.
|
|
|
|
"""
|
|
|
|
precondition(isinstance(path, unicode), path)
|
|
|
|
|
|
|
|
# On Windows and MacOS X, the Unicode API is used
|
|
|
|
# On other platforms (ie. Unix systems), the byte-level API is used
|
|
|
|
|
|
|
|
if is_unicode_platform:
|
|
|
|
return os.listdir(path)
|
|
|
|
else:
|
|
|
|
return listdir_unicode_fallback(path)
|
2015-10-01 21:40:10 +00:00
|
|
|
|
|
|
|
def listdir_filepath(fp):
|
|
|
|
return listdir_unicode(unicode_from_filepath(fp))
|