Jason R. Coombs <jar...@jaraco.com> added the comment:

This issue affected me today. I'm editing a codebase that has mixed line 
endings in different files. I'd like to patch for Python 3 support without 
changing line endings. Even invoking a single fixer (print), the line endings 
mutate. Since I'm running on macOS, the files with CRLF get LF line endings.

Answers [in this 
question](https://stackoverflow.com/questions/39028517/2to3-how-to-keep-newline-characters-from-input-file)
 suggest the mutation can be suppressed by altering the _to_system_newlines 
function, but the proposed fix has no effect on the Python 3.6 version of this 
routine.

I thought I'd encountered this issue before, but I realized after searching 
that I was thinking of issue10639, which may present a model for retaining the 
newlines when refactoring the code.

I found I was able to work around the issue by putting lib2to3-clean.py in my 
current directory:

import lib2to3.refactor
import runpy
import functools


if __name__ == '__main__':
        lib2to3.refactor._open_with_encoding = functools.partial(open, 
newline='')
        runpy.run_module('lib2to3')


And invoking `python -m lib2to3-clean` instead of `-m lib2to3`. The addition of 
newline='' follows the [guidance in the 
docs](https://docs.python.org/release/3.2/library/functions.html#open) on how 
to avoid mutating newlines.

I've released this functionality in [jaraco.develop 
4.0](https://pypi.org/project/jaraco.develop) so others can readily invoke it 
with `rwt jaraco.develop -- -m jaraco.develop.lib2to3`.

----------
nosy: +jason.coombs
versions: +Python 3.6 -Python 2.7, Python 3.2, Python 3.3

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue11594>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to