Skip to content

Commit

Permalink
Add most robust handling of message output
Browse files Browse the repository at this point in the history
I'll admit to being completely stymied by ASCII vs. UTF vs. Unicode
handling in Python2 and Python3.  I got a report of issues writing
the output, so put in some code to handle exceptions and try different
techniques.  I thought I had put this issue to rest.

If writing the output in the encoding of sys.stdout doesn't work,
try writing the output in UTF8, and then with no encoding.
One of these should work...(shouldn't it?)

I'm basically just taking stabs in the dark here.
  • Loading branch information
tbird20d committed Dec 17, 2018
1 parent 0cf365c commit 6ff85d0
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions grabserial
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,14 @@ def grab(arglist, outputfd=sys.stdout):
if outputfd:
outputfd.write(msg)
if out:
out.write(msg.encode(sys.stdout.encoding))
try:
out.write(msg.encode(sys.stdout.encoding))
except:
try:
out.write(msg.encode("utf8"))
except:
out.write(msg)

prev1 = elapsed
newline = 0

Expand All @@ -497,7 +504,14 @@ def grab(arglist, outputfd=sys.stdout):
if not quiet:
outputfd.write(msg)
if out:
out.write(msg.encode(sys.stdout.encoding))
try:
out.write(msg.encode(sys.stdout.encoding))
except:
try:
out.write(msg.encode("utf8"))
except:
out.write(msg)

prev1 = elapsed
newline = 0

Expand Down Expand Up @@ -575,7 +589,13 @@ def grab(arglist, outputfd=sys.stdout):
outputfd.write(msg)
outputfd.flush()
if out:
out.write(msg.encode(sys.stdout.encoding))
try:
out.write(msg.encode(sys.stdout.encoding))
except:
try:
out.write(msg.encode("utf8"))
except:
out.write(msg)
out.flush()

if out:
Expand Down

0 comments on commit 6ff85d0

Please sign in to comment.