berkerpeksag / astor

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

get wrong '\n' by astor #129

Closed junjie666666 closed 4 years ago

junjie666666 commented 5 years ago

I meet a problem. when I read data from test_astor.py,then to_source() by astor, the result get wrong ‘\n’. This problem may affect my data analysis.

source data file test_astor.py is:

# -*- coding:utf-8 -*-
import math3d
import math

class Visual(object):
    __slots__ = ('_status', '_uid', 'cur_anim', 'next_anim', 'cur_anim_tree', 'pos2', 'height',
                 'hori_o', 'vert_o', '_model', '_model_path', '_model_async_task', '_flush_handlers')

I read data by ast and to_source, code is:

    with open('E:\\work_data\static_analyze_ast\\venv\\Scripts\\test_astor.py', 'r') as f:
        root_node = ast.parse(f.read())
        astor_data = code_gen.to_source(root_node)
        print(astor_data)

    with open('E:\\work_data\static_analyze_ast\\venv\\Scripts\\save_astor.py', 'w') as f:
        f.write(astor_data)

but the result is wrong '\n' in save_astor.py:

import math3d
import math

class Visual(object):
    __slots__ = ('_status', '_uid', 'cur_anim', 'next_anim',
        'cur_anim_tree', 'pos2', 'height', 'hori_o', 'vert_o', '_model',
        '_model_path', '_model_async_task', '_flush_handlers')
esupoff commented 5 years ago

Formatting info is not stored in AST, it isn't possible to reconstruct original indentation or extra lines.

Not a bug IMO, just using a wrong tool for the job.

berkerpeksag commented 4 years ago

Thank you for the report! Looking at this issue again, now I agree with what @esupoff said. astor tries to reformat the input to be PEP 8 compatible. So, in theory, it's possible to get the original formatting only if it follows astor's not-so-great PEP 8 formatting rules.