berkerpeksag / astor

Python AST read/write
https://pypi.org/project/astor/
BSD 3-Clause "New" or "Revised" License
803 stars 102 forks source link

Don't use str.find #154

Closed domoritz closed 4 years ago

domoritz commented 5 years ago

It doesn't work for unicodes in Python 2.

domoritz commented 5 years ago

I ran into this issue when I created an ast node in Python 2 but set the name to a unicode.

import astor
import ast
astor.to_source(ast.Name(id=u'copy', ctx=ast.Load()))
berkerpeksag commented 5 years ago

Thanks! PR looks good to me. I'll convert your reproducer to a test case (feel free to do that if you have some spare time) and merge it.

Test failure seems unrelated as I can see it fails in other PRs:

======================================================================
FAIL: test_positional_only_arguments (tests.test_code_gen.CodegenTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/travis/build/berkerpeksag/astor/tests/test_code_gen.py", line 180, in test_positional_only_arguments
    self.assertSrcRoundtrips(source)
  File "/home/travis/build/berkerpeksag/astor/tests/test_code_gen.py", line 72, in assertSrcRoundtrips
    self.assertEqual(self.to_source(ast.parse(srctxt)).rstrip(), srctxt)
AssertionError: 'def [42 chars]s\n\n\ndef test(a=3, b=4, /, c=7):\n    pass\n[40 chars]pass' != 'def [42 chars]s\n\ndef test(a=3, b=4, /, c=7):\n    pass\n\n[36 chars]pass'
  def test(a, b, /, c, *, d, **kwargs):
      pass
- 

  def test(a=3, b=4, /, c=7):
      pass

- 
  def test(a, b=4, /, c=8, d=9):
      pass
berkerpeksag commented 4 years ago

Thank you and sorry for my late response!