Skip to content

Commit 4e8e40a

Browse files
Merge pull request #951 from RonnyPfannschmidt/fix-950-encoding-safe
fix #950 - add encoding args to io interaction
2 parents b877fb8 + 83dd018 commit 4e8e40a

File tree

10 files changed

+36
-21
lines changed

10 files changed

+36
-21
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
### Changed
3+
4+
- fix #950: ensure to pass encodings to io usage

src/setuptools_scm/_run_cmd.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def run(
154154
HGPLAIN="1",
155155
),
156156
text=True,
157+
encoding="utf-8",
157158
timeout=timeout,
158159
)
159160

testing/test_basic_api.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_run_plain(tmp_path: Path) -> None:
3232

3333
def test_data_from_mime(tmp_path: Path) -> None:
3434
tmpfile = tmp_path.joinpath("test.archival")
35-
tmpfile.write_text("name: test\nrevision: 1")
35+
tmpfile.write_bytes(b"name: test\nrevision: 1")
3636

3737
res = data_from_mime(str(tmpfile))
3838
assert res == {"name": "test", "revision": "1"}
@@ -81,7 +81,8 @@ def test_parentdir_prefix(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> No
8181
p.joinpath("setup.py").write_text(
8282
"""from setuptools import setup
8383
setup(use_scm_version={"parentdir_prefix_version": "projectname-"})
84-
"""
84+
""",
85+
encoding="utf-8",
8586
)
8687
res = run([sys.executable, "setup.py", "--version"], p)
8788
assert res.stdout == "12.34"
@@ -94,7 +95,8 @@ def test_fallback(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
9495
p.joinpath("setup.py").write_text(
9596
"""from setuptools import setup
9697
setup(use_scm_version={"fallback_version": "12.34"})
97-
"""
98+
""",
99+
encoding="utf-8",
98100
)
99101
res = run([sys.executable, "setup.py", "--version"], p)
100102
assert res.stdout == "12.34"
@@ -108,7 +110,8 @@ def test_empty_pretend_version(tmp_path: Path, monkeypatch: pytest.MonkeyPatch)
108110
p.joinpath("setup.py").write_text(
109111
"""from setuptools import setup
110112
setup(use_scm_version={"fallback_version": "12.34"})
111-
"""
113+
""",
114+
encoding="utf-8",
112115
)
113116
res = run([sys.executable, "setup.py", "--version"], p)
114117
assert res.stdout == "12.34"
@@ -126,7 +129,8 @@ def test_empty_pretend_version_named(
126129
p.joinpath("setup.py").write_text(
127130
"""from setuptools import setup
128131
setup(name="myscm", use_scm_version={"fallback_version": "12.34"})
129-
"""
132+
""",
133+
encoding="utf-8",
130134
)
131135
res = run([sys.executable, "setup.py", "--version"], p)
132136
assert res.stdout == "12.34"
@@ -171,7 +175,7 @@ def test_dump_version(tmp_path: Path) -> None:
171175
dump_version(tmp_path, version, "first.txt", scm_version=scm_version)
172176

173177
def read(name: str) -> str:
174-
return tmp_path.joinpath(name).read_text()
178+
return tmp_path.joinpath(name).read_text(encoding="utf-8")
175179

176180
assert read("first.txt") == "1.0"
177181

@@ -248,7 +252,7 @@ def __repr__(self) -> str:
248252

249253

250254
def test_internal_get_version_warns_for_version_files(tmp_path: Path) -> None:
251-
tmp_path.joinpath("PKG-INFO").write_text("Version: 0.1")
255+
tmp_path.joinpath("PKG-INFO").write_bytes(b"Version: 0.1")
252256
c = Configuration(root=tmp_path, fallback_root=tmp_path)
253257
with pytest.warns(
254258
DeprecationWarning,

testing/test_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def test_dump_version_works_with_pretend(
9494
name = "VERSION.txt"
9595
target = tmp_path.joinpath(name)
9696
get_version(root=tmp_path, write_to=name)
97-
assert target.read_text() == version
97+
assert target.read_text(encoding="utf-8") == version
9898

9999

100100
def test_dump_version_modern(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
@@ -107,7 +107,7 @@ def test_dump_version_modern(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) ->
107107
project.mkdir()
108108

109109
get_version(root="..", relative_to=target, version_file=name)
110-
assert target.read_text() == version
110+
assert target.read_text(encoding="utf-8") == version
111111

112112

113113
def dump_a_version(tmp_path: Path) -> None:

testing/test_git.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ def test_root_relative_to(wd: WorkDir, monkeypatch: pytest.MonkeyPatch) -> None:
7171
"""from setuptools import setup
7272
setup(use_scm_version={"root": "../..",
7373
"relative_to": __file__})
74-
"""
74+
""",
75+
encoding="utf-8",
7576
)
7677
res = run([sys.executable, "setup.py", "--version"], p)
7778
assert res.stdout == "0.1.dev0+d20090213"
@@ -86,7 +87,8 @@ def test_root_search_parent_directories(
8687
p.joinpath("setup.py").write_text(
8788
"""from setuptools import setup
8889
setup(use_scm_version={"search_parent_directories": True})
89-
"""
90+
""",
91+
encoding="utf-8",
9092
)
9193
res = run([sys.executable, "setup.py", "--version"], p)
9294
assert res.stdout == "0.1.dev0+d20090213"
@@ -249,7 +251,7 @@ def test_git_version_unnormalized_setuptools(
249251
assert res == "17.33.0rc1"
250252

251253
# but the version tag in the file is non-normalized (with the dash)
252-
assert wd.cwd.joinpath("VERSION.txt").read_text() == "17.33.0-rc1"
254+
assert wd.cwd.joinpath("VERSION.txt").read_text(encoding="utf-8") == "17.33.0-rc1"
253255

254256

255257
@pytest.mark.issue(179)

testing/test_integration.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def test_pyproject_support(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> N
5050
),
5151
encoding="utf-8",
5252
)
53-
pkg.joinpath("setup.py").write_text("__import__('setuptools').setup()")
53+
pkg.joinpath("setup.py").write_text(
54+
"__import__('setuptools').setup()", encoding="utf-8"
55+
)
5456
res = run([sys.executable, "setup.py", "--version"], pkg)
5557
assert res.stdout == "12.34"
5658

@@ -238,7 +240,7 @@ def test_setuptools_version_keyword_ensures_regex(
238240
"ep_name", ["setuptools_scm.parse_scm", "setuptools_scm.parse_scm_fallback"]
239241
)
240242
def test_git_archival_plugin_ignored(tmp_path: Path, ep_name: str) -> None:
241-
tmp_path.joinpath(".git_archival.txt").write_text("broken")
243+
tmp_path.joinpath(".git_archival.txt").write_text("broken", encoding="utf-8")
242244
try:
243245
dist = importlib.metadata.distribution("setuptools_scm_git_archive")
244246
except importlib.metadata.PackageNotFoundError:

testing/test_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def test_main() -> None:
1414
os.path.dirname(__file__), "..", "src", "setuptools_scm", "__main__.py"
1515
)
1616
ns = {"__package__": "setuptools_scm"}
17-
with open(mainfile) as f:
17+
with open(mainfile, encoding="utf-8") as f:
1818
code = compile(f.read(), "__main__.py", "exec")
1919
exec(code, ns)
2020

testing/test_regressions.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ def test_pkginfo_noscmroot(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> N
3434

3535
tmp_path.joinpath(".git").mkdir()
3636
p.joinpath("setup.py").write_text(
37-
"from setuptools import setup;" 'setup(use_scm_version={"root": ".."})'
37+
"from setuptools import setup;" 'setup(use_scm_version={"root": ".."})',
38+
encoding="utf-8",
3839
)
3940

4041
res = run([sys.executable, "setup.py", "--version"], p)
4142
assert "setuptools-scm was unable to detect version for" in res.stderr
4243
assert res.returncode == 1
4344

44-
p.joinpath("PKG-INFO").write_text("Version: 1.0")
45+
p.joinpath("PKG-INFO").write_text("Version: 1.0", encoding="utf-8")
4546
res = run([sys.executable, "setup.py", "--version"], p)
4647
assert res.stdout == "1.0"
4748

@@ -76,9 +77,10 @@ def vs(v):
7677
return guess_next_dev_version(v)
7778
return {"version_scheme": vs}
7879
setup(use_scm_version=vcfg)
79-
"""
80+
""",
81+
encoding="utf-8",
8082
)
81-
p.joinpath("PKG-INFO").write_text("Version: 1.0")
83+
p.joinpath("PKG-INFO").write_text("Version: 1.0", encoding="utf-8")
8284

8385
res = run([sys.executable, "setup.py", "--version"], p)
8486
assert res.stdout == "1.0"

testing/wd_wrapper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def write(self, name: str, content: str | bytes) -> Path:
3232
if isinstance(content, bytes):
3333
path.write_bytes(content)
3434
else:
35-
path.write_text(content)
35+
path.write_text(content, encoding="utf-8")
3636
return path
3737

3838
def _reason(self, given_reason: str | None) -> str:

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ deps=
1515
rich
1616
build
1717
commands=
18-
pytest {posargs}
18+
python -X warn_default_encoding -m pytest {posargs}
1919

2020

2121

0 commit comments

Comments
 (0)