Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2to3 - how to keep newline characters from input file?

I'm attempting to run 2to3 on Windows machine where *.py files has Unix-style end-line characters. Running 2to3 modifies newline characters in output file.

MCVE:

print2.py content before

print "Hello, world!"\n

Executed command:

2to3 print2.py -w -n

print2.py content after

print("Hello, world!")\r\n

Expected content:

print("Hello, world!")\n

Is it possible to keep old newline characters when 2to3 conversion is performed?

like image 320
Łukasz Rogalski Avatar asked Nov 09 '22 10:11

Łukasz Rogalski


1 Answers

Since there seems to be no standard way to change this behavior in command line usage, I've prepared very simple Python script, which runs code and patches unwanted behavior.

Here's an example for python modernize, but any 2to3-based tool will do just fine.

# to access function to patch
import lib2to3.refactor
# actual main
import libmodernize.main
# convert str to list of args, not mandatory
import shlex
# patch problematic function, as suggested by @mfripp
lib2to3.refactor._to_system_newlines = lambda input: input 

args = shlex.split("-w -n src")  # prepare args
libmodernize.main.main(args)  # pass args to main, equivalent of running cmdline tool
like image 104
Łukasz Rogalski Avatar answered Nov 14 '22 22:11

Łukasz Rogalski