Skip to content

Commit e70bff4

Browse files
committed
fix: make sure encoding is specified in more places
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 4f06e8c commit e70bff4

File tree

14 files changed

+50
-29
lines changed

14 files changed

+50
-29
lines changed

src/scikit_build_core/build/_editable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def editable_redirect(
3636
"""
3737

3838
editable_py = resources / "_editable_redirect.py"
39-
editable_txt: str = editable_py.read_text(encoding="utf-8")
39+
editable_txt: str = editable_py.read_text(encoding="utf-8-sig")
4040

4141
arguments = (
4242
modules,

src/scikit_build_core/build/_file_processor.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ def each_unignored_file(
4040

4141
for gi in [Path(".git/info/exclude"), Path(".gitignore")]:
4242
ignore_errs = [FileNotFoundError, NotADirectoryError]
43-
with contextlib.suppress(*ignore_errs), gi.open(encoding="utf-8") as f:
43+
with contextlib.suppress(*ignore_errs), gi.open(encoding="utf-8-sig") as f:
4444
exclude_lines += f.readlines()
4545

4646
nested_excludes = {
47-
p.parent: pathspec.GitIgnoreSpec.from_lines(p.read_text().splitlines())
47+
p.parent: pathspec.GitIgnoreSpec.from_lines(
48+
p.read_text(encoding="utf-8-sig").splitlines()
49+
)
4850
for p in Path().rglob("**/.gitignore")
4951
if p != Path(".gitignore")
5052
}

src/scikit_build_core/build/_scripts.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ def __dir__() -> list[str]:
2020
def process_script_dir(script_dir: Path) -> None:
2121
for item in script_dir.iterdir():
2222
content = []
23-
with contextlib.suppress(UnicodeDecodeError), item.open(encoding="utf-8") as f:
23+
with contextlib.suppress(UnicodeDecodeError), item.open(
24+
encoding="utf-8-sig"
25+
) as f:
2426
file_iter = iter(f)
2527
try:
2628
# TODO: handle empty files
@@ -31,5 +33,5 @@ def process_script_dir(script_dir: Path) -> None:
3133
if match:
3234
content = [f"#!python{match.group(1) or ''}\n", *file_iter]
3335
if content:
34-
with item.open("w", encoding="utf-8") as f:
36+
with item.open("w", encoding="utf-8-sig") as f:
3537
f.writelines(content)

src/scikit_build_core/build/generate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def generate_file_contents(gen: GenerateSettings, metadata: StandardMetadata) ->
2626
), f"One of template or template-path must be set for {gen.path}"
2727

2828
if gen.template_path:
29-
template = gen.template_path.read_text(encoding="utf-8")
29+
template = gen.template_path.read_text(encoding="utf-8-sig")
3030
else:
3131
template = gen.template
3232

src/scikit_build_core/cmake.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def __post_init__(self) -> None:
100100
skbuild_info = self.build_dir / ".skbuild-info.json"
101101
# If building via SDist, this could be pre-filled, so delete it if it exists
102102
with contextlib.suppress(FileNotFoundError):
103-
with skbuild_info.open("r", encoding="utf-8") as f:
103+
with skbuild_info.open("r", encoding="utf-8-sig") as f:
104104
info = json.load(f)
105105

106106
cached_source_dir = Path(info["source_dir"])
@@ -113,7 +113,7 @@ def __post_init__(self) -> None:
113113
shutil.rmtree(self.build_dir)
114114
self.build_dir.mkdir()
115115

116-
with skbuild_info.open("w", encoding="utf-8") as f:
116+
with skbuild_info.open("w", encoding="utf-8-sig") as f:
117117
json.dump(self._info_dict(), f, indent=2)
118118

119119
def _info_dict(self) -> dict[str, str]:
@@ -132,7 +132,7 @@ def _info_dict(self) -> dict[str, str]:
132132
def init_cache(
133133
self, cache_settings: Mapping[str, str | os.PathLike[str] | bool]
134134
) -> None:
135-
with self.init_cache_file.open("w", encoding="utf-8") as f:
135+
with self.init_cache_file.open("w", encoding="utf-8-sig") as f:
136136
for key, value in cache_settings.items():
137137
if isinstance(value, bool):
138138
str_value = "ON" if value else "OFF"
@@ -162,7 +162,7 @@ def init_cache(
162162
)
163163
f.write('set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE "BOTH" CACHE PATH "")\n')
164164

165-
contents = self.init_cache_file.read_text(encoding="utf-8").strip()
165+
contents = self.init_cache_file.read_text(encoding="utf-8-sig").strip()
166166
logger.debug(
167167
"{}:\n{}",
168168
self.init_cache_file,

src/scikit_build_core/file_api/_cattrs_converter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def from_json_file(with_path: Dict[str, Any], t: Type[T]) -> T:
4040
if with_path["jsonFile"] is None:
4141
return converter.structure_attrs_fromdict({}, t)
4242
path = base_dir / Path(with_path["jsonFile"])
43-
raw = json.loads(path.read_text(encoding="utf-8"))
43+
raw = json.loads(path.read_text(encoding="utf-8-sig"))
4444
return converter.structure_attrs_fromdict(raw, t)
4545

4646
converter.register_structure_hook(CodeModel, from_json_file)
@@ -57,7 +57,7 @@ def load_reply_dir(reply_dir: Path) -> Index:
5757
msg = f"index file not found in {reply_dir}"
5858
raise IndexError(msg)
5959
index_file = indexes[-1]
60-
return converter.loads(index_file.read_text(), Index)
60+
return converter.loads(index_file.read_text("utf-8-sig"), Index)
6161

6262

6363
if __name__ == "__main__":

src/scikit_build_core/file_api/reply.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ def load(self) -> Index:
3434
Load the newest index.json file and return the Index object.
3535
"""
3636
index_file = sorted(self.base_dir.glob("index-*"))[-1]
37-
with index_file.open(encoding="utf-8") as f:
37+
with index_file.open(encoding="utf-8-sig") as f:
3838
data = json.load(f)
3939

4040
return self.make_class(data, Index)
4141

4242
def _load_from_json(self, name: Path, target: Type[T]) -> T:
43-
with self.base_dir.joinpath(name).open(encoding="utf-8") as f:
43+
with self.base_dir.joinpath(name).open(encoding="utf-8-sig") as f:
4444
data = json.load(f)
4545

4646
return self.make_class(data, target)

src/scikit_build_core/metadata/regex.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def dynamic_metadata(
4848
assert isinstance(result, str)
4949
remove = settings.get("result", "")
5050

51-
with Path(input_filename).open(encoding="utf-8") as f:
51+
with Path(input_filename).open(encoding="utf-8-sig") as f:
5252
match = re.search(regex, f.read(), re.MULTILINE)
5353

5454
if not match:

src/scikit_build_core/settings/skbuild_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def get_skbuild_schema(tool_name: str = "scikit-build") -> dict[str, Any]:
212212
"Get the stored complete schema for scikit-build settings."
213213
assert tool_name == "scikit-build", "Only scikit-build is supported."
214214

215-
with resources.joinpath("scikit-build.schema.json").open(encoding="utf-8") as f:
215+
with resources.joinpath("scikit-build.schema.json").open(encoding="utf-8-sig") as f:
216216
return json.load(f) # type: ignore[no-any-return]
217217

218218

tests/packages/navigate_editable/python/shared_pkg/py_module.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ def py_method():
1919
def read_py_data_txt():
2020
root = files("shared_pkg.data")
2121
py_data = root / "py_data.txt"
22-
print(py_data.read_text())
22+
print(py_data.read_text(encoding="utf-8-sig"))
2323

2424

2525
def read_c_generated_txt():
2626
root = files("shared_pkg.data")
2727
c_generated_txt = root / "c_generated.txt"
28-
print(c_generated_txt.read_text())
28+
print(c_generated_txt.read_text(encoding="utf-8-sig"))

tests/test_cmake_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def test_init_cache(
107107
cmake_init = config.build_dir / "CMakeInit.txt"
108108
source_dir_str = str(config.source_dir).replace("\\", "/")
109109
assert (
110-
cmake_init.read_text()
110+
cmake_init.read_text(encoding="utf-8-sig")
111111
== f"""\
112112
set(SKBUILD ON CACHE BOOL "" FORCE)
113113
set(SKBUILD_VERSION [===[1.0.0]===] CACHE STRING "" FORCE)

tests/test_custom_modules.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_ep(isolated):
2020

2121
script = isolated.run("script1", capture=True).strip()
2222
pysys = isolated.execute("import sys; print(sys.executable)").strip()
23-
contents = Path(script).read_text()
23+
contents = Path(script).read_text(encoding="utf-8-sig")
2424
assert contents.startswith(f"#!{pysys}")
2525

2626
if sys.version_info >= (3, 8):

tests/test_editable_unit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def editable_package(
8484
8585
from importlib.resources import files
8686
87-
read_file = files("pkg.resources").joinpath("file.txt").read_text(encoding="utf-8")
87+
read_file = files("pkg.resources").joinpath("file.txt").read_text(encoding="utf-8-sig")
8888
assert read_file == "hello"
8989
"""
9090
)
@@ -98,7 +98,7 @@ def editable_package(
9898
"""\
9999
from importlib.resources import files
100100
101-
read_file = files("pkg.iresources").joinpath("file.txt").read_text(encoding="utf-8")
101+
read_file = files("pkg.iresources").joinpath("file.txt").read_text(encoding="utf-8-sig")
102102
assert read_file == "hi"
103103
"""
104104
)

tests/test_process_scripts.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,35 @@ def test_script_dir(tmp_path: Path) -> None:
3333

3434
process_script_dir(script_dir)
3535

36-
assert script_1.read_text() == "#!python\n\nprint('hello world')"
36+
assert (
37+
script_1.read_text(encoding="utf-8-sig") == "#!python\n\nprint('hello world')"
38+
)
3739
assert script_1.stat().st_mode == orig_mode_1
3840

39-
assert script_2.read_text() == "#!python\n\nprint('hello world')"
41+
assert (
42+
script_2.read_text(encoding="utf-8-sig") == "#!python\n\nprint('hello world')"
43+
)
4044
assert script_2.stat().st_mode == orig_mode_2
4145

42-
assert script_3.read_text() == "#!python\n\nprint('hello world')"
46+
assert (
47+
script_3.read_text(encoding="utf-8-sig") == "#!python\n\nprint('hello world')"
48+
)
4349

44-
assert script_4.read_text() == "#!python\n\nprint('hello world')"
50+
assert (
51+
script_4.read_text(encoding="utf-8-sig") == "#!python\n\nprint('hello world')"
52+
)
4553

46-
assert script_5.read_text() == "#!/usr/bin/other\n\nprint('hello world')"
54+
assert (
55+
script_5.read_text(encoding="utf-8-sig")
56+
== "#!/usr/bin/other\n\nprint('hello world')"
57+
)
4758

48-
assert script_6.read_text() == "#!python other\n\nprint('hello world')"
59+
assert (
60+
script_6.read_text(encoding="utf-8-sig")
61+
== "#!python other\n\nprint('hello world')"
62+
)
4963

50-
assert script_7.read_text() == "#!/usr/bin/env other\n\nprint('hello world')"
64+
assert (
65+
script_7.read_text(encoding="utf-8-sig")
66+
== "#!/usr/bin/env other\n\nprint('hello world')"
67+
)

0 commit comments

Comments
 (0)