Pin text I/O to UTF-8 and fail CI on locale-dependent reads/writes (#3296)
Co-authored-by: ShuQingDollarVoyager <57471784+ShuQingDollarVoyager@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
52ad0a8876
commit
5285e936a9
1 changed files with 76 additions and 57 deletions
|
|
@ -43,15 +43,7 @@ def process_snippet_block(match: re.Match[str], check_mode: bool = False) -> str
|
|||
file_path = match.group(2)
|
||||
|
||||
try:
|
||||
# Read the entire file. A missing source file must be fatal: a "Warning"
|
||||
# that returns the stale block lets --check pass with exit 0, so a
|
||||
# renamed or deleted snippet is invisible to CI. SystemExit deliberately
|
||||
# escapes the `except Exception` below.
|
||||
file = Path(file_path)
|
||||
if not file.exists():
|
||||
sys.exit(f"Error: snippet-source file not found: {file_path}")
|
||||
|
||||
code = file.read_text().rstrip()
|
||||
code = Path(file_path).read_text(encoding="utf-8").rstrip()
|
||||
github_url = get_github_url(file_path)
|
||||
|
||||
# Build the replacement block
|
||||
|
|
@ -88,9 +80,9 @@ def process_snippet_block(match: re.Match[str], check_mode: bool = False) -> str
|
|||
|
||||
return replacement
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error processing {file_path}: {e}")
|
||||
return full_match
|
||||
except (OSError, UnicodeDecodeError) as e:
|
||||
# Fatal, not "warn and keep the stale block": that would let --check pass with exit 0.
|
||||
sys.exit(f"Error processing {file_path}: {e}")
|
||||
|
||||
|
||||
def update_readme_snippets(check_mode: bool = False) -> bool:
|
||||
|
|
@ -107,7 +99,7 @@ def update_readme_snippets(check_mode: bool = False) -> bool:
|
|||
print(f"Error: README file not found: {readme_path}")
|
||||
return False
|
||||
|
||||
content = readme_path.read_text()
|
||||
content = readme_path.read_text(encoding="utf-8")
|
||||
original_content = content
|
||||
|
||||
# Pattern to match snippet-source blocks
|
||||
|
|
@ -129,14 +121,14 @@ def update_readme_snippets(check_mode: bool = False) -> bool:
|
|||
)
|
||||
return False
|
||||
else:
|
||||
print(f"✓ {readme_path} code snippets are up to date")
|
||||
print(f"{readme_path} code snippets are up to date")
|
||||
return True
|
||||
else:
|
||||
if updated_content != original_content:
|
||||
readme_path.write_text(updated_content)
|
||||
print(f"✓ Updated {readme_path}")
|
||||
readme_path.write_text(updated_content, encoding="utf-8")
|
||||
print(f"Updated {readme_path}")
|
||||
else:
|
||||
print(f"✓ {readme_path} already up to date")
|
||||
print(f"{readme_path} already up to date")
|
||||
return True
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue