This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author jaraco
Recipients benjamin.peterson, bialix, eric.araujo, jaraco
Date 2017-12-08.19:25:24
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1512761124.13.0.213398074469.issue11594@psf.upfronthosting.co.za>
In-reply-to
Content
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`.
History
Date User Action Args
2017-12-08 19:25:24jaracosetrecipients: + jaraco, bialix, benjamin.peterson, eric.araujo
2017-12-08 19:25:24jaracosetmessageid: <1512761124.13.0.213398074469.issue11594@psf.upfronthosting.co.za>
2017-12-08 19:25:24jaracolinkissue11594 messages
2017-12-08 19:25:24jaracocreate