Skip to content

Commit

Permalink
fix: make sure encoding is specified in more places
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii committed Aug 13, 2024
1 parent 4f06e8c commit e70bff4
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/scikit_build_core/build/_editable.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def editable_redirect(
"""

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

arguments = (
modules,
Expand Down
6 changes: 4 additions & 2 deletions src/scikit_build_core/build/_file_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ def each_unignored_file(

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

nested_excludes = {
p.parent: pathspec.GitIgnoreSpec.from_lines(p.read_text().splitlines())
p.parent: pathspec.GitIgnoreSpec.from_lines(
p.read_text(encoding="utf-8-sig").splitlines()
)
for p in Path().rglob("**/.gitignore")
if p != Path(".gitignore")
}
Expand Down
6 changes: 4 additions & 2 deletions src/scikit_build_core/build/_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def __dir__() -> list[str]:
def process_script_dir(script_dir: Path) -> None:
for item in script_dir.iterdir():
content = []
with contextlib.suppress(UnicodeDecodeError), item.open(encoding="utf-8") as f:
with contextlib.suppress(UnicodeDecodeError), item.open(
encoding="utf-8-sig"
) as f:
file_iter = iter(f)
try:
# TODO: handle empty files
Expand All @@ -31,5 +33,5 @@ def process_script_dir(script_dir: Path) -> None:
if match:
content = [f"#!python{match.group(1) or ''}\n", *file_iter]
if content:
with item.open("w", encoding="utf-8") as f:
with item.open("w", encoding="utf-8-sig") as f:
f.writelines(content)
2 changes: 1 addition & 1 deletion src/scikit_build_core/build/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def generate_file_contents(gen: GenerateSettings, metadata: StandardMetadata) ->
), f"One of template or template-path must be set for {gen.path}"

if gen.template_path:
template = gen.template_path.read_text(encoding="utf-8")
template = gen.template_path.read_text(encoding="utf-8-sig")
else:
template = gen.template

Expand Down
8 changes: 4 additions & 4 deletions src/scikit_build_core/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def __post_init__(self) -> None:
skbuild_info = self.build_dir / ".skbuild-info.json"
# If building via SDist, this could be pre-filled, so delete it if it exists
with contextlib.suppress(FileNotFoundError):
with skbuild_info.open("r", encoding="utf-8") as f:
with skbuild_info.open("r", encoding="utf-8-sig") as f:
info = json.load(f)

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

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

def _info_dict(self) -> dict[str, str]:
Expand All @@ -132,7 +132,7 @@ def _info_dict(self) -> dict[str, str]:
def init_cache(
self, cache_settings: Mapping[str, str | os.PathLike[str] | bool]
) -> None:
with self.init_cache_file.open("w", encoding="utf-8") as f:
with self.init_cache_file.open("w", encoding="utf-8-sig") as f:
for key, value in cache_settings.items():
if isinstance(value, bool):
str_value = "ON" if value else "OFF"
Expand Down Expand Up @@ -162,7 +162,7 @@ def init_cache(
)
f.write('set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE "BOTH" CACHE PATH "")\n')

contents = self.init_cache_file.read_text(encoding="utf-8").strip()
contents = self.init_cache_file.read_text(encoding="utf-8-sig").strip()
logger.debug(
"{}:\n{}",
self.init_cache_file,
Expand Down
4 changes: 2 additions & 2 deletions src/scikit_build_core/file_api/_cattrs_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def from_json_file(with_path: Dict[str, Any], t: Type[T]) -> T:
if with_path["jsonFile"] is None:
return converter.structure_attrs_fromdict({}, t)
path = base_dir / Path(with_path["jsonFile"])
raw = json.loads(path.read_text(encoding="utf-8"))
raw = json.loads(path.read_text(encoding="utf-8-sig"))
return converter.structure_attrs_fromdict(raw, t)

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


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions src/scikit_build_core/file_api/reply.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ def load(self) -> Index:
Load the newest index.json file and return the Index object.
"""
index_file = sorted(self.base_dir.glob("index-*"))[-1]
with index_file.open(encoding="utf-8") as f:
with index_file.open(encoding="utf-8-sig") as f:
data = json.load(f)

return self.make_class(data, Index)

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

return self.make_class(data, target)
Expand Down
2 changes: 1 addition & 1 deletion src/scikit_build_core/metadata/regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def dynamic_metadata(
assert isinstance(result, str)
remove = settings.get("result", "")

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

if not match:
Expand Down
2 changes: 1 addition & 1 deletion src/scikit_build_core/settings/skbuild_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def get_skbuild_schema(tool_name: str = "scikit-build") -> dict[str, Any]:
"Get the stored complete schema for scikit-build settings."
assert tool_name == "scikit-build", "Only scikit-build is supported."

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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def py_method():
def read_py_data_txt():
root = files("shared_pkg.data")
py_data = root / "py_data.txt"
print(py_data.read_text())
print(py_data.read_text(encoding="utf-8-sig"))


def read_c_generated_txt():
root = files("shared_pkg.data")
c_generated_txt = root / "c_generated.txt"
print(c_generated_txt.read_text())
print(c_generated_txt.read_text(encoding="utf-8-sig"))
2 changes: 1 addition & 1 deletion tests/test_cmake_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_init_cache(
cmake_init = config.build_dir / "CMakeInit.txt"
source_dir_str = str(config.source_dir).replace("\\", "/")
assert (
cmake_init.read_text()
cmake_init.read_text(encoding="utf-8-sig")
== f"""\
set(SKBUILD ON CACHE BOOL "" FORCE)
set(SKBUILD_VERSION [===[1.0.0]===] CACHE STRING "" FORCE)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_custom_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_ep(isolated):

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

if sys.version_info >= (3, 8):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_editable_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def editable_package(
from importlib.resources import files
read_file = files("pkg.resources").joinpath("file.txt").read_text(encoding="utf-8")
read_file = files("pkg.resources").joinpath("file.txt").read_text(encoding="utf-8-sig")
assert read_file == "hello"
"""
)
Expand All @@ -98,7 +98,7 @@ def editable_package(
"""\
from importlib.resources import files
read_file = files("pkg.iresources").joinpath("file.txt").read_text(encoding="utf-8")
read_file = files("pkg.iresources").joinpath("file.txt").read_text(encoding="utf-8-sig")
assert read_file == "hi"
"""
)
Expand Down
31 changes: 24 additions & 7 deletions tests/test_process_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,35 @@ def test_script_dir(tmp_path: Path) -> None:

process_script_dir(script_dir)

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

assert script_2.read_text() == "#!python\n\nprint('hello world')"
assert (
script_2.read_text(encoding="utf-8-sig") == "#!python\n\nprint('hello world')"
)
assert script_2.stat().st_mode == orig_mode_2

assert script_3.read_text() == "#!python\n\nprint('hello world')"
assert (
script_3.read_text(encoding="utf-8-sig") == "#!python\n\nprint('hello world')"
)

assert script_4.read_text() == "#!python\n\nprint('hello world')"
assert (
script_4.read_text(encoding="utf-8-sig") == "#!python\n\nprint('hello world')"
)

assert script_5.read_text() == "#!/usr/bin/other\n\nprint('hello world')"
assert (
script_5.read_text(encoding="utf-8-sig")
== "#!/usr/bin/other\n\nprint('hello world')"
)

assert script_6.read_text() == "#!python other\n\nprint('hello world')"
assert (
script_6.read_text(encoding="utf-8-sig")
== "#!python other\n\nprint('hello world')"
)

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

0 comments on commit e70bff4

Please sign in to comment.