Repository navigation
Replace read_str_coding with tokenize.detect_encoding() - #976
Open
NathanTian wants to merge 1 commit into
Open
NathanTian wants to merge 1 commit into
NathanTian wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Replaces the custom PEP 263 regex +
_find_coding()implementation inrope.base.fscommands.read_str_coding()with the standard librarytokenize.detect_encoding(), enabling proper UTF-8 BOM (utf-8-sig) detection and handling across reads, edits, and undo/redo history.Fixes #975
Summary of Changes
rope/base/fscommands.py:CODING_LINE_PATTERN/_find_coding()withtokenize.detect_encoding()inread_str_coding()._iter_first_two_lines()splitting only on CRLF/LF/CR (preserving PEP 263\fon line 1 and avoiding full-file scans on large files), while sanitizing non-ASCII characters onstrinputs (preserving a leadingU+FEFFBOM) sostrinputs with non-UTF-8 coding cookies work cleanly on Python 3.14.1+."a".encode(encoding)to reject non-text/unusable codecs (e.g.rot_13,undefined) consistently across Python versions, and added a Python < 3.14.1 fallback for valid non-UTF-8 files containing non-ASCII bytes on the coding line itself (e.g.cp1252)._decode_data_with_encoding()and prevented duplicate BOM emission inunicode_to_file_data()whenutf-8-sigis inferred from a leadingU+FEFF.rope/base/resources.py&rope/base/change.py:File._has_bom) and preserved it acrossFile.write(),ChangeContents.do()/undo(), and persisted history (4-tuple serialization inChangeToData/DataToChange).ropetest/projecttest.py&CHANGELOG.md:test_file_encoding_reading_for_notepad_styles(previously disabled asxxx_test_...).U+FEFF, legacy 3-tuple history replay,cp1252non-ASCII coding lines, non-text/undefined codecs, and conflicting BOM + coding cookies.CHANGELOG.mdunder# **Upcoming release**.Note on Conflicting BOM + Coding Cookie Behavior
Regarding the question in #975 about files with both a UTF-8 BOM and a conflicting non-UTF-8 coding cookie (e.g.
\xef\xbb\xbf# -*- coding: latin-1 -*-):read_str_coding()catchesSyntaxError/LookupError/UnicodeErrorand returnsNone, soFile.read()/_decode_data()preserves Rope's existing tolerant fallback (utf-8->latin1) for arbitrary/non-Python files.PyModule/ast.parse()) still raisesModuleSyntaxError, matching CPython's behavior.Happy to adjust this if you prefer
File.read()to raiseModuleDecodeErroron BOM/cookie conflicts instead.Verification
black --check rope/— 96 files unchangedpytest -q— 2,188 passed, 11 skipped, 5 xfailedChecklist (delete if not relevant):