Skip to content

Commit

Permalink
Handle UnicodeDecodeError
Browse files Browse the repository at this point in the history
  • Loading branch information
myint committed Apr 5, 2013
1 parent 98057f7 commit 0d7896a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
16 changes: 5 additions & 11 deletions autoflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,11 @@ def unused_import_line_numbers(source):
def check(source):
"""Return messages from pyflakes."""
reporter = ListReporter()
pyflakes.api.check(source, filename='<string>', reporter=reporter)
return reporter.messages


class StubFile(object):

"""Stub for ignoring output."""

def write(_, __):
"""Stub write()."""
try:
pyflakes.api.check(source, filename='<string>', reporter=reporter)
except UnicodeDecodeError:
pass
return reporter.messages


class ListReporter(pyflakes.reporter.Reporter):
Expand All @@ -107,7 +101,7 @@ def __init__(self):
Ignore errors from Reporter.
"""
ignore = StubFile()
ignore = io.StringIO()
pyflakes.reporter.Reporter.__init__(self, ignore, ignore)
self.messages = []

Expand Down
4 changes: 4 additions & 0 deletions test_autoflake.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# coding: utf-8

"""Test suite for autoflake."""

Expand Down Expand Up @@ -439,6 +440,9 @@ def test_check(self):
def test_check_with_bad_syntax(self):
self.assertFalse(autoflake.check('foo('))

def test_check_with_unicode(self):
self.assertFalse(autoflake.check('print("∑"'))


class SystemTests(unittest.TestCase):

Expand Down

0 comments on commit 0d7896a

Please sign in to comment.