Skip to content

Commit

Permalink
Fix TypeError when passing unicode string to to_source() (berkerpeksa…
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz authored and isidentical committed Dec 9, 2019
1 parent 6bb6c4d commit 390a8f8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions astor/source_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def split_lines(source, maxline=79):
line = []
multiline = False
count = 0
find = str.find
for item in source:
index = find(item, '\n')
newline = type(item)('\n')
index = item.find(newline)
if index:
line.append(item)
multiline = index > 0
Expand Down
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ Bug fixes
.. _`Issue 158`: https://github.com/berkerpeksag/astor/issues/158
.. _`PR 164`: https://github.com/berkerpeksag/astor/pull/164

* Fixed :exc:`TypeError` when AST nodes with unicode strings are passed to
:func:`astor.to_source`.
(Reported and fixed by Dominik Moritz in `PR 154`_.)

.. _`PR 154`: https://github.com/berkerpeksag/astor/pull/154

0.8.0 - 2019-05-19
------------------
Expand Down
15 changes: 15 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import astor

from astor.source_repr import split_lines

from .support import import_fresh_module


Expand Down Expand Up @@ -99,5 +101,18 @@ def test_auto_generated_attributes(self):
self.assertEqual(treewalk.__dict__['post_handlers'], {})


class SourceReprTestCase(unittest.TestCase):
"""
Tests for helpers in astor.source_repr module.
Note that these APIs are not public.
"""

@unittest.skipUnless(sys.version_info[0] == 2, 'only applies to Python 2')
def test_split_lines_unicode_support(self):
source = [u'copy', '\n']
self.assertEqual(split_lines(source), source)


if __name__ == '__main__':
unittest.main()

0 comments on commit 390a8f8

Please sign in to comment.