Skip to content

Commit

Permalink
Print error in Unicode
Browse files Browse the repository at this point in the history
This avoids errors when the source code contains Unicode and the user
runs with "-vvvv" (on some systems). This is related to issue #72.
  • Loading branch information
myint committed May 2, 2013
1 parent 7cfb7ef commit 898c18d
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions autopep8.py
Expand Up @@ -87,6 +87,11 @@
'W6'])


ERROR = codecs.getwriter('utf-8')(sys.stderr.buffer
if sys.version_info[0] >= 3
else sys.stderr)


def open_with_encoding(filename, encoding=None, mode='r'):
"""Return opened file with a specific encoding."""
if not encoding:
Expand Down Expand Up @@ -238,19 +243,19 @@ def _fix_source(self, results):
print(
'---> Not fixing {f} on line {l}'.format(
f=result['id'], l=result['line']),
file=sys.stderr)
file=ERROR)
else: # We assume one-line fix when None
completed_lines.add(result['line'])
else:
if self.options.verbose >= 3:
print("---> '%s' is not defined." % fixed_methodname,
file=sys.stderr)
file=ERROR)
info = result['info'].strip()
print('---> %s:%s:%s:%s' % (self.filename,
result['line'],
result['column'],
info),
file=sys.stderr)
file=ERROR)

def fix(self):
"""Return a version of the source code with PEP 8 violations fixed."""
Expand All @@ -268,7 +273,7 @@ def fix(self):
progress[r['id']] = set()
progress[r['id']].add(r['line'])
print('---> {n} issue(s) to fix {progress}'.format(
n=len(results), progress=progress), file=sys.stderr)
n=len(results), progress=progress), file=ERROR)

self._fix_source(filter_results(source=unicode().join(self.source),
results=results,
Expand Down Expand Up @@ -716,7 +721,7 @@ def fix_e501(self, result):

if self.options.verbose >= 4:
print(('-' * 79 + '\n').join([''] + candidates + ['']),
file=sys.stderr)
file=ERROR)

for _candidate in candidates:
assert _candidate is not None
Expand Down Expand Up @@ -1928,7 +1933,7 @@ def apply_global_fixes(source, options):
if code_match(code, select=options.select, ignore=options.ignore):
if options.verbose:
print('---> Applying global fix for {0}'.format(code.upper()),
file=sys.stderr)
file=ERROR)
source = function(source)

return source
Expand Down Expand Up @@ -2212,11 +2217,11 @@ def find_files(filenames, recursive, exclude):
def _fix_file(parameters):
"""Helper function for optionally running fix_file() in parallel."""
if parameters[1].verbose:
print('[file:{0}]'.format(parameters[0]), file=sys.stderr)
print('[file:{0}]'.format(parameters[0]), file=ERROR)
try:
fix_file(*parameters)
except IOError as error:
print(str(error), file=sys.stderr)
print(str(error), file=ERROR)


def fix_multiple_files(filenames, options, output=None):
Expand Down

0 comments on commit 898c18d

Please sign in to comment.