from allmydata.scripts.common import BaseOptions
from allmydata.scripts import debug, create_node, startstop_node, cli, keygen, stats_gatherer
-from allmydata.util.encodingutil import quote_output, get_argv_encoding
+from allmydata.util.encodingutil import quote_output, get_io_encoding
def GROUP(s):
# Usage.parseOptions compares argv[1] against command[0], so it will
c = c.subOptions
print >>stdout, str(c)
try:
- msg = e.args[0].decode(get_argv_encoding())
+ msg = e.args[0].decode(get_io_encoding())
except Exception:
msg = repr(e)
print >>stdout, "%s: %s\n" % (sys.argv[0], quote_output(msg, quotemarks=False))
from allmydata.util.assertutil import precondition
from allmydata.util.encodingutil import listdir_unicode, unicode_platform, \
- quote_output, get_output_encoding, get_argv_encoding, get_filesystem_encoding, \
+ quote_output, get_io_encoding, get_filesystem_encoding, \
unicode_to_output, unicode_to_argv, to_str
from allmydata.util.fileutil import abspath_expanduser_unicode
self.set_up_grid()
try:
- etudes_arg = u"\u00E9tudes".encode(get_argv_encoding())
- lumiere_arg = u"lumi\u00E8re.txt".encode(get_argv_encoding())
+ etudes_arg = u"\u00E9tudes".encode(get_io_encoding())
+ lumiere_arg = u"lumi\u00E8re.txt".encode(get_io_encoding())
except UnicodeEncodeError:
raise unittest.SkipTest("A non-ASCII command argument could not be encoded on this platform.")
# tahoe put "\u00E0 trier.txt" "\u00E0 trier.txt"
try:
- a_trier_arg = u"\u00E0 trier.txt".encode(get_argv_encoding())
+ a_trier_arg = u"\u00E0 trier.txt".encode(get_io_encoding())
except UnicodeEncodeError:
raise unittest.SkipTest("A non-ASCII command argument could not be encoded on this platform.")
d = self.do_cli("create-alias", "tahoe")
d.addCallback(lambda res:
- self.do_cli("put", rel_fn.encode(get_argv_encoding()), a_trier_arg))
+ self.do_cli("put", rel_fn.encode(get_io_encoding()), a_trier_arg))
def _uploaded((rc, out, err)):
readcap = out.strip()
self.failUnless(readcap.startswith("URI:LIT:"), readcap)
# u"g\u00F6\u00F6d" might not be representable in the argv and/or output encodings.
# It is initially included in the directory in any case.
try:
- good_arg = u"g\u00F6\u00F6d".encode(get_argv_encoding())
+ good_arg = u"g\u00F6\u00F6d".encode(get_io_encoding())
except UnicodeEncodeError:
good_arg = None
try:
- good_out = u"g\u00F6\u00F6d".encode(get_output_encoding())
+ good_out = u"g\u00F6\u00F6d".encode(get_io_encoding())
except UnicodeEncodeError:
good_out = None
fn1 = os.path.join(unicode(self.basedir), u"\u00C4rtonwall")
try:
- fn1_arg = fn1.encode(get_argv_encoding())
- artonwall_arg = u"\u00C4rtonwall".encode(get_argv_encoding())
+ fn1_arg = fn1.encode(get_io_encoding())
+ artonwall_arg = u"\u00C4rtonwall".encode(get_io_encoding())
except UnicodeEncodeError:
raise unittest.SkipTest("A non-ASCII command argument could not be encoded on this platform.")
self.failUnlessIn("files whose names could not be converted", err)
else:
self.failUnlessReallyEqual(rc, 0)
- self.failUnlessReallyEqual(out.decode(get_output_encoding()), u"Metallica\n\u00C4rtonwall\n")
+ self.failUnlessReallyEqual(out.decode(get_io_encoding()), u"Metallica\n\u00C4rtonwall\n")
self.failUnlessReallyEqual(err, "")
d.addCallback(_check)
fn1 = os.path.join(unicode(self.basedir), u"\u00C4rtonwall")
try:
- fn1_arg = fn1.encode(get_argv_encoding())
+ fn1_arg = fn1.encode(get_io_encoding())
del fn1_arg # hush pyflakes
- artonwall_arg = u"\u00C4rtonwall".encode(get_argv_encoding())
+ artonwall_arg = u"\u00C4rtonwall".encode(get_io_encoding())
except UnicodeEncodeError:
raise unittest.SkipTest("A non-ASCII command argument could not be encoded on this platform.")
self.failUnlessIn("files whose names could not be converted", err)
else:
self.failUnlessReallyEqual(rc, 0)
- self.failUnlessReallyEqual(out.decode(get_output_encoding()), u"\u00C4rtonwall\n")
+ self.failUnlessReallyEqual(out.decode(get_io_encoding()), u"\u00C4rtonwall\n")
self.failUnlessReallyEqual(err, "")
d.addCallback(_check)
def test_exclude_options_unicode(self):
nice_doc = u"nice_d\u00F8c.lyx"
try:
- doc_pattern_arg = u"*d\u00F8c*".encode(get_argv_encoding())
+ doc_pattern_arg = u"*d\u00F8c*".encode(get_io_encoding())
except UnicodeEncodeError:
raise unittest.SkipTest("A non-ASCII command argument could not be encoded on this platform.")
self.set_up_grid()
try:
- motorhead_arg = u"tahoe:Mot\u00F6rhead".encode(get_argv_encoding())
+ motorhead_arg = u"tahoe:Mot\u00F6rhead".encode(get_io_encoding())
except UnicodeEncodeError:
raise unittest.SkipTest("A non-ASCII command argument could not be encoded on this platform.")
print " argv = %s" % repr(sys.argv[1])
print " platform = '%s'" % sys.platform
print " filesystem_encoding = '%s'" % sys.getfilesystemencoding()
- print " output_encoding = '%s'" % sys.stdout.encoding
- print " argv_encoding = '%s'" % sys.stdout.encoding
+ print " io_encoding = '%s'" % sys.stdout.encoding
try:
tmpdir = tempfile.mkdtemp()
for fname in TEST_FILENAMES:
from allmydata.util import encodingutil
from allmydata.util.encodingutil import argv_to_unicode, unicode_to_url, \
unicode_to_output, quote_output, unicode_platform, listdir_unicode, \
- FilenameEncodingError, get_output_encoding, get_filesystem_encoding, _reload
+ FilenameEncodingError, get_io_encoding, get_filesystem_encoding, _reload
from allmydata.dirnode import normalize
from twisted.python import usage
class EncodingUtilErrors(ReallyEqualMixin, unittest.TestCase):
@patch('sys.stdout')
- def test_get_output_encoding(self, mock_stdout):
+ def test_get_io_encoding(self, mock_stdout):
mock_stdout.encoding = 'UTF-8'
_reload()
- self.failUnlessReallyEqual(get_output_encoding(), 'utf-8')
+ self.failUnlessReallyEqual(get_io_encoding(), 'utf-8')
mock_stdout.encoding = 'cp65001'
_reload()
- self.failUnlessReallyEqual(get_output_encoding(), 'utf-8')
+ self.failUnlessReallyEqual(get_io_encoding(), 'utf-8')
mock_stdout.encoding = 'koi8-r'
expected = sys.platform == "win32" and 'utf-8' or 'koi8-r'
_reload()
- self.failUnlessReallyEqual(get_output_encoding(), expected)
+ self.failUnlessReallyEqual(get_io_encoding(), expected)
mock_stdout.encoding = 'nonexistent_encoding'
if sys.platform == "win32":
_reload()
- self.failUnlessReallyEqual(get_output_encoding(), 'utf-8')
+ self.failUnlessReallyEqual(get_io_encoding(), 'utf-8')
else:
self.failUnlessRaises(AssertionError, _reload)
@patch('locale.getpreferredencoding')
- def test_get_output_encoding_not_from_stdout(self, mock_locale_getpreferredencoding):
+ def test_get_io_encoding_not_from_stdout(self, mock_locale_getpreferredencoding):
locale # hush pyflakes
mock_locale_getpreferredencoding.return_value = 'koi8-r'
try:
expected = sys.platform == "win32" and 'utf-8' or 'koi8-r'
_reload()
- self.failUnlessReallyEqual(get_output_encoding(), expected)
+ self.failUnlessReallyEqual(get_io_encoding(), expected)
sys.stdout.encoding = None
_reload()
- self.failUnlessReallyEqual(get_output_encoding(), expected)
+ self.failUnlessReallyEqual(get_io_encoding(), expected)
mock_locale_getpreferredencoding.return_value = None
_reload()
- self.failUnlessReallyEqual(get_output_encoding(), 'utf-8')
+ self.failUnlessReallyEqual(get_io_encoding(), 'utf-8')
finally:
sys.stdout = old_stdout
def test_argv_to_unicode(self):
- encodingutil.argv_encoding = 'utf-8'
+ encodingutil.io_encoding = 'utf-8'
self.failUnlessRaises(usage.UsageError,
argv_to_unicode,
lumiere_nfc.encode('latin1'))
def test_unicode_to_output(self):
- encodingutil.output_encoding = 'koi8-r'
+ encodingutil.io_encoding = 'koi8-r'
self.failUnlessRaises(UnicodeEncodeError, unicode_to_output, lumiere_nfc)
@patch('os.listdir')
if 'argv' not in dir(self):
return
- mock.encoding = self.output_encoding
+ mock.encoding = self.io_encoding
argu = lumiere_nfc
argv = self.argv
_reload()
if 'argv' not in dir(self):
return
- mock.encoding = self.output_encoding
+ mock.encoding = self.io_encoding
_reload()
self.failUnlessReallyEqual(unicode_to_output(lumiere_nfc), self.argv)
check(u"\u2621\"", u"'\u2621\"'", True)
def test_quote_output_default(self):
- encodingutil.output_encoding = 'ascii'
+ encodingutil.io_encoding = 'ascii'
self.test_quote_output_ascii(None)
- encodingutil.output_encoding = 'latin1'
+ encodingutil.io_encoding = 'latin1'
self.test_quote_output_latin1(None)
- encodingutil.output_encoding = 'utf-8'
+ encodingutil.io_encoding = 'utf-8'
self.test_quote_output_utf8(None)
argv = 'lumi\xc3\xa8re'
platform = 'linux2'
filesystem_encoding = 'UTF-8'
- output_encoding = 'UTF-8'
- argv_encoding = 'UTF-8'
+ io_encoding = 'UTF-8'
dirlist = ['test_file', '\xc3\x84rtonwall.mp3', 'Blah blah.txt']
class UbuntuKarmicLatin1(EncodingUtil, unittest.TestCase):
argv = 'lumi\xe8re'
platform = 'linux2'
filesystem_encoding = 'ISO-8859-1'
- output_encoding = 'ISO-8859-1'
- argv_encoding = 'ISO-8859-1'
+ io_encoding = 'ISO-8859-1'
dirlist = ['test_file', 'Blah blah.txt', '\xc4rtonwall.mp3']
class Windows(EncodingUtil, unittest.TestCase):
argv = 'lumi\xc3\xa8re'
platform = 'win32'
filesystem_encoding = 'mbcs'
- output_encoding = 'utf-8'
- argv_encoding = 'utf-8'
+ io_encoding = 'utf-8'
dirlist = [u'Blah blah.txt', u'test_file', u'\xc4rtonwall.mp3']
class MacOSXLeopard(EncodingUtil, unittest.TestCase):
output = 'lumi\xc3\xa8re'
platform = 'darwin'
filesystem_encoding = 'utf-8'
- output_encoding = 'UTF-8'
- argv_encoding = 'UTF-8'
+ io_encoding = 'UTF-8'
dirlist = [u'A\u0308rtonwall.mp3', u'Blah blah.txt', u'test_file']
class MacOSXLeopard7bit(EncodingUtil, unittest.TestCase):
uname = 'Darwin g5.local 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:57:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_PPC Power Macintosh powerpc'
platform = 'darwin'
filesystem_encoding = 'utf-8'
- output_encoding = 'US-ASCII'
- argv_encoding = 'US-ASCII'
+ io_encoding = 'US-ASCII'
dirlist = [u'A\u0308rtonwall.mp3', u'Blah blah.txt', u'test_file']
class OpenBSD(EncodingUtil, unittest.TestCase):
uname = 'OpenBSD 4.1 GENERIC#187 i386 Intel(R) Celeron(R) CPU 2.80GHz ("GenuineIntel" 686-class)'
platform = 'openbsd4'
filesystem_encoding = '646'
- output_encoding = '646'
- argv_encoding = '646'
+ io_encoding = '646'
# Oops, I cannot write filenames containing non-ascii characters
raise AssertionError("The character encoding '%s' is not supported for conversion." % (encoding,))
filesystem_encoding = None
-output_encoding = None
-argv_encoding = None
+io_encoding = None
is_unicode_platform = False
def _reload():
- global filesystem_encoding, output_encoding, argv_encoding, is_unicode_platform
+ global filesystem_encoding, io_encoding, is_unicode_platform
filesystem_encoding = canonical_encoding(sys.getfilesystemencoding())
check_encoding(filesystem_encoding)
if sys.platform == 'win32':
# 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).
- output_encoding = 'utf-8'
+ io_encoding = 'utf-8'
else:
- outenc = None
+ ioenc = None
if hasattr(sys.stdout, 'encoding'):
- outenc = sys.stdout.encoding
- if outenc is None:
+ ioenc = sys.stdout.encoding
+ if ioenc is None:
try:
- outenc = locale.getpreferredencoding()
+ ioenc = locale.getpreferredencoding()
except Exception:
pass # work around <http://bugs.python.org/issue1443504>
- output_encoding = canonical_encoding(outenc)
+ io_encoding = canonical_encoding(ioenc)
- check_encoding(output_encoding)
- argv_encoding = output_encoding
+ check_encoding(io_encoding)
is_unicode_platform = sys.platform in ["win32", "darwin"]
"""
return filesystem_encoding
-def get_output_encoding():
+def get_io_encoding():
"""
- Returns expected encoding for writing to stdout or stderr.
+ Returns expected encoding for writing to stdout or stderr, and for arguments in sys.argv.
"""
- return output_encoding
-
-def get_argv_encoding():
- """
- Returns expected encoding for command-line arguments.
- """
- return argv_encoding
+ return io_encoding
def argv_to_unicode(s):
"""
precondition(isinstance(s, str), s)
try:
- return unicode(s, argv_encoding)
+ return unicode(s, io_encoding)
except UnicodeDecodeError:
raise usage.UsageError("Argument %s cannot be decoded as %s." %
- (quote_output(s), argv_encoding))
+ (quote_output(s), io_encoding))
def argv_to_abspath(s):
"""
# 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:
- return s.encode(argv_encoding)
+ return s.encode(io_encoding)
def unicode_to_url(s):
"""
precondition(isinstance(s, unicode), s)
try:
- out = s.encode(output_encoding)
+ out = s.encode(io_encoding)
except (UnicodeEncodeError, UnicodeDecodeError):
- raise UnicodeEncodeError(output_encoding, s, 0, 0,
+ raise UnicodeEncodeError(io_encoding, s, 0, 0,
"A string could not be encoded as %s for output to the terminal:\n%r" %
- (output_encoding, repr(s)))
+ (io_encoding, repr(s)))
if PRINTABLE_8BIT.search(out) is None:
- raise UnicodeEncodeError(output_encoding, s, 0, 0,
+ raise UnicodeEncodeError(io_encoding, s, 0, 0,
"A string encoded as %s for output to the terminal contained unsafe bytes:\n%r" %
- (output_encoding, repr(s)))
+ (io_encoding, repr(s)))
return out
if MUST_DOUBLE_QUOTE.search(s) is None:
try:
- out = s.encode(encoding or output_encoding)
+ out = s.encode(encoding or io_encoding)
if quotemarks or out.startswith('"'):
return "'%s'" % (out,)
else:
pass
escaped = ESCAPABLE_UNICODE.sub(_unicode_escape, s)
- return '"%s"' % (escaped.encode(encoding or output_encoding, 'backslashreplace'),)
+ return '"%s"' % (escaped.encode(encoding or io_encoding, 'backslashreplace'),)
def quote_path(path, quotemarks=True):
return quote_output("/".join(map(to_str, path)), quotemarks=quotemarks)