Skip to content

Fix encoding in _open() and clean() functions - #548

Open
Johnnnmai wants to merge 1 commit into
bndr:masterfrom
Johnnnmai:fix/encoding-open-context-manager
Open

Fix encoding in _open() and clean() functions#548
Johnnnmai wants to merge 1 commit into
bndr:masterfrom
Johnnnmai:fix/encoding-open-context-manager

Conversation

@Johnnnmai

Copy link
Copy Markdown

Continuation of #542. Adds explicit UTF-8 encoding to two remaining open() calls:

  • _open() context manager (line ~92): used when writing requirements.txt
  • clean() function (line ~459): used when cleaning requirements.txt

Without these, pipreqs can produce garbled output or crash on systems with non-UTF-8 default encoding.

Fixes part of #241, #271, #469.

@PNHD PNHD left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants