Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On Windows, spurious ^M characters are added to Unix formatted files #115

Closed
rasa opened this issue Jun 13, 2022 · 3 comments · Fixed by #117
Closed

On Windows, spurious ^M characters are added to Unix formatted files #115

rasa opened this issue Jun 13, 2022 · 3 comments · Fixed by #117
Labels
bug Something isn't working

Comments

@rasa
Copy link

rasa commented Jun 13, 2022

Our team uses a .gitattributes file of

* -text  

This means files checked out on Windows will often contain only Unix line endings: just ^Js (LF), and no ^Ms (CRs).

But when these files are reformatted using pydocstringformatter, the modified lines now end in ^M^J, but only those lines.

Perhaps pydocstringformatter can sense the line ending characters of the lines it's modifying, and use them instead of introducing ^Ms where none were there previously?

I think this a better solution than adding a config option, but a config option would be nice too, to be explicit.

If there's interest, I can try to cobble a PR, that accomplishes this task.

@DanielNoord
Copy link
Owner

I'd certainly be interested in a PR and this probably shouldn't be a config. A little googling shows that it is actually expected behaviour of Python to read CRLF to LF but then output CRLF when writing again, although I think this is only in binary mode. We're breaking that assumption apparently, so let's fix that!

We could add an option to make this a toggleable 'fix', but I think that's probably better dealt with by an other tool.

I'm not sure what the best approach is, but looking at https://peps.python.org/pep-0278/, https://discuss.python.org/t/will-open-function-change-line-endings-in-files/7150/5 and other things I quickly found it seems like opening in binary mode might be the best solution as we then rely on Python's own correct handling of this issue!

@DanielNoord DanielNoord added the bug Something isn't working label Jun 13, 2022
@rasa
Copy link
Author

rasa commented Jun 13, 2022

Ok, I will start working on this now. PEP 278 says it offers "universal newline" support, but only on reads. Perhaps we open the file for reading first, read the entire file, and then use the newline characters returned in the newlines attribute when we output the file (or reopen the file for writing if --write was specified)?

@DanielNoord
Copy link
Owner

https://discuss.python.org/t/will-open-function-change-line-endings-in-files/7150/6 says that writing should fix this as well. But we probably need to double check that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants