Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
binwalk
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fact-depend
binwalk
Commits
f004d4dd
Commit
f004d4dd
authored
Nov 30, 2015
by
Craig Heffner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed LANG=C unicode display errors in Python3
parent
4f89d514
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
3 deletions
+38
-3
display.py
src/binwalk/core/display.py
+38
-3
No files found.
src/binwalk/core/display.py
View file @
f004d4dd
...
...
@@ -33,6 +33,31 @@ class Display(object):
if
csv
:
self
.
csv
=
pycsv
.
writer
(
self
.
fp
)
def
_fix_unicode
(
self
,
line
):
'''
This is a hack, there must be a better way to handle it.
In Python3, if the environment variable LANG=C is set, indicating
that the terminal is ASCII only, but unicode characters need to be
printed to the screen or to a file (e.g., file path, magic result
format string), then an UnicodeEncodError exception will be raised.
This converts the given line to ASCII, ignoring conversion errors,
and returns a str.
'''
return
bytes2str
(
line
.
encode
(
'ascii'
,
'ignore'
))
def
_fix_unicode_list
(
self
,
columns
):
'''
Convenience wrapper for self.log which is passed a list of format arguments.
'''
if
type
(
columns
)
in
[
list
,
tuple
]:
for
i
in
range
(
0
,
len
(
columns
)):
try
:
columns
[
i
]
=
self
.
_fix_unicode
(
columns
[
i
])
except
AttributeError
:
pass
return
columns
def
format_strings
(
self
,
header
,
result
):
self
.
result_format
=
result
self
.
header_format
=
header
...
...
@@ -43,9 +68,15 @@ class Display(object):
def
log
(
self
,
fmt
,
columns
):
if
self
.
fp
:
if
self
.
csv
:
self
.
csv
.
writerow
(
columns
)
try
:
self
.
csv
.
writerow
(
columns
)
except
UnicodeEncodeError
:
self
.
csv
.
writerow
(
self
.
_fix_unicode_list
(
columns
))
else
:
self
.
fp
.
write
(
fmt
%
tuple
(
columns
))
try
:
self
.
fp
.
write
(
fmt
%
tuple
(
columns
))
except
UnicodeEncodeError
:
self
.
fp
.
write
(
fmt
%
tuple
(
self
.
_fix_unicode_list
(
columns
)))
self
.
fp
.
flush
()
...
...
@@ -100,7 +131,11 @@ class Display(object):
if
not
self
.
quiet
and
stdout
:
try
:
sys
.
stdout
.
write
(
self
.
_format_line
(
line
.
strip
())
+
"
\n
"
)
try
:
sys
.
stdout
.
write
(
self
.
_format_line
(
line
.
strip
())
+
"
\n
"
)
except
UnicodeEncodeError
:
line
=
self
.
_fix_unicode
(
line
)
sys
.
stdout
.
write
(
self
.
_format_line
(
line
.
strip
())
+
"
\n
"
)
sys
.
stdout
.
flush
()
except
IOError
as
e
:
pass
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment