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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With