Fix encoding in _open() and clean() functions - #548
Conversation
PNHD
left a comment
There was a problem hiding this comment.
The new-output path and the in-place clean path need different encoding treatment.
"
"Using an explicit encoding when creating a new requirements file is reasonable, but clean(file_, imports) opens an existing user file with r+. Hard-coding UTF-8 there changes current behavior and can make an existing non-UTF8 requirements file unreadable on a system where it currently works with the locale default. Since clean rewrites an existing file, it should preserve/detect that file's encoding (or accept the configured encoding) and have a regression covering a non-UTF8 existing requirements file.
"
"The issue references are also mismatched: #241, #271, and #469 fail while scanning Python source, whereas this diff only changes the output helper and clean(). Please do not claim those source-scanning issues are fixed by this patch.
Continuation of #542. Adds explicit UTF-8 encoding to two remaining open() calls:
_open()context manager (line ~92): used when writing requirements.txtclean()function (line ~459): used when cleaning requirements.txtWithout these, pipreqs can produce garbled output or crash on systems with non-UTF-8 default encoding.
Fixes part of #241, #271, #469.