Skip to content

Commit

Permalink
Use utf-8 instead of unicode_escape when forcing encoding.
Browse files Browse the repository at this point in the history
unicode_escape messes up escaped chars in strings. We'll assume we're
interacting with valid utf-8-encoded strings.

Closes #70
Closes #79
  • Loading branch information
eliben committed Apr 9, 2015
1 parent 78adc49 commit e30fbf5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion yapf/yapflib/py3compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def unicode(s):
if PY3:
return s
else:
return __builtin__.unicode(s, 'unicode_escape')
return __builtin__.unicode(s, 'utf-8')


# In Python 3.2+, readfp is deprecated in favor of read_file, which doesn't
Expand Down
17 changes: 17 additions & 0 deletions yapftests/yapf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ def foo():
self.assertIsNone(stderrdata)
self.assertEqual(reformatted_code.decode('utf-8'), expected_formatted_code)

def testReadFromStdinWithEscapedStrings(self):
unformatted_code = textwrap.dedent(u"""\
s = "foo\\nbar"
""")
expected_formatted_code = textwrap.dedent(u"""\
s = "foo\\nbar"
""")

p = subprocess.Popen(YAPF_BINARY,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.STDOUT)
reformatted_code, stderrdata = p.communicate(
unformatted_code.encode('utf-8'))
self.assertIsNone(stderrdata)
self.assertEqual(reformatted_code.decode('utf-8'), expected_formatted_code)

def testSetGoogleStyle(self):
unformatted_code = textwrap.dedent(u"""\
def foo(): # trail
Expand Down

0 comments on commit e30fbf5

Please sign in to comment.