Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

completing merge of non-ascii error output fix

  • Loading branch information...
commit d824fe7c36439a7a712849efc3a04c0755863115 1 parent ea814ca
Andrew Moffat authored
Showing with 18 additions and 8 deletions.
  1. +1 −0  AUTHORS.md
  2. +5 −1 changelog.md
  3. +6 −6 sh.py
  4. +6 −1 test.py
1  AUTHORS.md
View
@@ -16,3 +16,4 @@
* fruch
* Ralph Bean
* Rory Kirchner
+* ahhentz
6 changelog.md
View
@@ -4,4 +4,8 @@
* Making `Command` class resolve the `path` parameter with `which` by default
instead of expecting it to be resolved before it is passed in. This change
- shouldn't affect backwards compatibility.
+ shouldn't affect backwards compatibility.
+
+* Fixing a bug when an exception is raised from a program, and the error
+ output has non-ascii text. This didn't work in Python < 3.0, because
+ .decode()'s default encoding is typically ascii.
12 sh.py
View
@@ -47,7 +47,7 @@
import time as _time
from locale import getpreferredencoding
-ENCODING = getpreferredencoding()
+DEFAULT_ENCODING = getpreferredencoding()
if IS_PY3:
@@ -137,7 +137,7 @@ def __init__(self, full_cmd, stdout, stderr):
tstderr += ("... (%d more, please see e.stderr)" % err_delta).encode()
msg = "\n\n RAN: %r\n\n STDOUT:\n%s\n\n STDERR:\n%s" %\
- (full_cmd, tstdout.decode(ENCODING), tstderr.decode(ENCODING))
+ (full_cmd, tstdout.decode(DEFAULT_ENCODING), tstderr.decode(DEFAULT_ENCODING))
super(ErrorReturnCode, self).__init__(msg)
class CommandNotFound(Exception): pass
@@ -404,7 +404,7 @@ class Command(object):
"tty_in": False,
"tty_out": True,
- "encoding": "utf8",
+ "encoding": DEFAULT_ENCODING,
# how long the process should run before it is auto-killed
"timeout": 0,
@@ -466,7 +466,7 @@ def _extract_call_args(kwargs, to_override={}):
def _format_arg(self, arg):
if IS_PY3: arg = str(arg)
- else: arg = unicode(arg).encode("utf8")
+ else: arg = unicode(arg).encode(DEFAULT_ENCODING)
return arg
def _compile_args(self, args, kwargs):
@@ -520,7 +520,7 @@ def bake(self, *args, **kwargs):
def __str__(self):
if IS_PY3: return self.__unicode__()
- else: return unicode(self).encode("utf8")
+ else: return unicode(self).encode(DEFAULT_ENCODING)
def __eq__(self, other):
try: return str(self) == str(other)
@@ -1246,7 +1246,7 @@ def read(self):
# come in), OProc will use an instance of this class to chop up the data and
# feed it as lines to be sent down the pipe
class StreamBufferer(object):
- def __init__(self, encoding="utf8", buffer_type=1):
+ def __init__(self, encoding=DEFAULT_ENCODING, buffer_type=1):
# 0 for unbuffered, 1 for line, everything else for that amount
self.type = buffer_type
self.buffer = []
7 test.py
View
@@ -1171,7 +1171,12 @@ def test_failure_with_large_output(self):
# an UnicodeDecodeError
def test_non_ascii_error(self):
from sh import ls, ErrorReturnCode
- self.assertRaises(ErrorReturnCode, ls, u'')
+
+ test = ""
+ if not IS_PY3: test = test.decode("utf8")
+
+ self.assertRaises(ErrorReturnCode, ls, test)
+
if __name__ == "__main__":
if len(sys.argv) > 1:
Please sign in to comment.
Something went wrong with that request. Please try again.