Skip to content

fix: add explicit encoding to open() calls for cross-platform portability#14658

Closed
KeloYuan wants to merge 1 commit intoTheAlgorithms:masterfrom
KeloYuan:fix/add-encoding-to-open
Closed

fix: add explicit encoding to open() calls for cross-platform portability#14658
KeloYuan wants to merge 1 commit intoTheAlgorithms:masterfrom
KeloYuan:fix/add-encoding-to-open

Conversation

@KeloYuan
Copy link
Copy Markdown

@KeloYuan KeloYuan commented May 8, 2026

Problem

Python's open() uses locale-dependent encoding by default, which varies across platforms:

  • macOS/Linux: typically UTF-8
  • Windows: typically cp1252 or other locale-specific encoding

This means code that works perfectly on macOS/Linux can silently corrupt data on Windows (and vice versa). Since Python 3.15, the default will change to UTF-8, but for now it's best practice to be explicit.

PEP 686 (Python 3.15+) will make UTF-8 the default, but explicit encoding is recommended for all current Python versions.

Changes

Added encoding="utf-8" to text-mode open() calls in 5 files:

File Line Purpose
searches/tabu_search.py 52, 91 Reading graph adjacency data
data_structures/arrays/sudoku_solver.py 209 Parsing Sudoku puzzles
strings/word_patterns.py 49 Loading dictionary file
data_compression/huffman.py 29, 81 Reading/writing compressed text
strings/detecting_english_programmatically.py 10 Loading English dictionary

Testing

All existing tests pass — no behavioral change for correct UTF-8 files.

…lity

Python's open() uses locale-dependent encoding by default, which varies
across platforms (UTF-8 on macOS/Linux, cp1252 on Windows). This causes
files to be read/written incorrectly on some systems.

Added encoding='utf-8' to all text-mode open() calls in:
- searches/tabu_search.py
- data_structures/arrays/sudoku_solver.py
- strings/word_patterns.py
- data_compression/huffman.py
- strings/detecting_english_programmatically.py
Copilot AI review requested due to automatic review settings May 8, 2026 16:57
@algorithms-keeper
Copy link
Copy Markdown

Closing this pull request as invalid

@KeloYuan, this pull request is being closed as none of the checkboxes have been marked. It is important that you go through the checklist and mark the ones relevant to this pull request. Please read the Contributing guidelines.

If you're facing any problem on how to mark a checkbox, please read the following instructions:

  • Read a point one at a time and think if it is relevant to the pull request or not.
  • If it is, then mark it by putting a x between the square bracket like so: [x]

NOTE: Only [x] is supported so if you have put any other letter or symbol between the brackets, that will be marked as invalid. If that is the case then please open a new pull request with the appropriate changes.

@algorithms-keeper algorithms-keeper Bot closed this May 8, 2026
@algorithms-keeper algorithms-keeper Bot review requested due to automatic review settings May 8, 2026 16:59
@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed invalid

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant