Skip to content

Commit d096fe4

Browse files
committed
fix: add encoding arguments
Signed-off-by: Henry Schreiner <[email protected]>
1 parent fd8be1d commit d096fe4

37 files changed

+100
-97
lines changed

benchmark/benchmark.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,16 @@ def file_replace(file_name: Path, old_text: str, new_text: str) -> Iterator[None
148148
"""
149149
file_text = ""
150150
if old_text:
151-
file_text = file_name.read_text()
151+
file_text = file_name.read_text(encoding="utf-8")
152152
if old_text not in file_text:
153153
raise Exception("Old text {old_text!r} not found in {file_name}")
154154
updated_text = file_text.replace(old_text, new_text)
155-
file_name.write_text(updated_text)
155+
file_name.write_text(updated_text, encoding="utf-8")
156156
try:
157157
yield
158158
finally:
159159
if old_text:
160-
file_name.write_text(file_text)
160+
file_name.write_text(file_text, encoding="utf-8")
161161

162162

163163
def file_must_exist(file_name: str, kind: str = "file") -> Path:
@@ -624,7 +624,7 @@ def run_no_coverage(self, env: Env) -> float:
624624
def run_with_coverage(self, env: Env, cov_ver: Coverage) -> float:
625625
env.shell.run_command(f"{env.python} -m pip install {cov_ver.pip_args}")
626626
pforce = Path("force.ini")
627-
pforce.write_text("[run]\nbranch=false\n")
627+
pforce.write_text("[run]\nbranch=false\n", encoding="utf-8")
628628
with env.shell.set_env({"COVERAGE_FORCE_CONFIG": str(pforce.resolve())}):
629629
env.shell.run_command(f"{env.python} -m pytest {self.FAST} --cov")
630630
duration = env.shell.last_duration
@@ -907,13 +907,13 @@ def __init__(
907907

908908
def save_results(self) -> None:
909909
"""Save current results to the JSON file."""
910-
with self.results_file.open("w") as f:
910+
with self.results_file.open("w", encoding="utf-8") as f:
911911
json.dump({" ".join(k): v for k, v in self.result_data.items()}, f)
912912

913913
def load_results(self) -> dict[ResultKey, list[float]]:
914914
"""Load results from the JSON file if it exists."""
915915
if self.results_file.exists():
916-
with self.results_file.open("r") as f:
916+
with self.results_file.open("r", encoding="utf-8") as f:
917917
data: dict[str, list[float]] = json.load(f)
918918
return {
919919
(k.split()[0], k.split()[1], k.split()[2]): v for k, v in data.items()

coverage/html.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def data_filename(fname: str) -> str:
5151

5252
def read_data(fname: str) -> str:
5353
"""Return the contents of a data file of ours."""
54-
with open(data_filename(fname)) as data_file:
54+
with open(data_filename(fname), encoding="utf-8") as data_file:
5555
return data_file.read()
5656

5757

@@ -412,7 +412,7 @@ def make_local_static_report_files(self) -> None:
412412
# .gitignore can't be copied from the source tree because if it was in
413413
# the source tree, it would stop the static files from being checked in.
414414
if self.directory_was_empty:
415-
with open(os.path.join(self.directory, ".gitignore"), "w") as fgi:
415+
with open(os.path.join(self.directory, ".gitignore"), "w", encoding="utf-8") as fgi:
416416
fgi.write("# Created by coverage.py\n*\n")
417417

418418
def should_report(self, analysis: Analysis, index_page: IndexPage) -> bool:
@@ -706,7 +706,7 @@ def read(self) -> None:
706706
"""Read the information we stored last time."""
707707
try:
708708
status_file = os.path.join(self.directory, self.STATUS_FILE)
709-
with open(status_file) as fstatus:
709+
with open(status_file, encoding="utf-8") as fstatus:
710710
status = json.load(fstatus)
711711
except (OSError, ValueError):
712712
# Status file is missing or malformed.
@@ -747,7 +747,7 @@ def write(self) -> None:
747747
for fname, finfo in self.files.items()
748748
},
749749
}
750-
with open(status_file, "w") as fout:
750+
with open(status_file, "w", encoding="utf-8") as fout:
751751
json.dump(status_data, fout, separators=(",", ":"))
752752

753753
def check_global_data(self, *data: Any) -> None:

coverage/pytracer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def __repr__(self) -> str:
126126

127127
def log(self, marker: str, *args: Any) -> None:
128128
"""For hard-core logging of what this tracer is doing."""
129-
with open("/tmp/debug_trace.txt", "a") as f:
129+
with open("/tmp/debug_trace.txt", "a", encoding="utf-8") as f:
130130
f.write(f"{marker} {self.id}[{len(self.data_stack)}]")
131131
if 0: # if you want thread ids..
132132
f.write(".{:x}.{:x}".format( # type: ignore[unreachable]

coverage/sysmon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def log(msg: str) -> None:
104104
# f"{root}-{pid}.out",
105105
# f"{root}-{pid}-{tslug}.out",
106106
]:
107-
with open(filename, "a") as f:
107+
with open(filename, "a", encoding="utf-8") as f:
108108
try:
109109
print(f"{pid}:{tslug}: {msg}", file=f, flush=True)
110110
except UnicodeError:

doc/cog_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def _read_config(text, fname):
5252
text = textwrap.dedent(text[1:])
5353

5454
os.makedirs("tmp", exist_ok=True)
55-
with open(f"tmp/{fname}", "w") as f:
55+
with open(f"tmp/{fname}", "w", encoding="utf-8") as f:
5656
f.write(text)
5757

5858
config = read_coverage_config(f"tmp/{fname}", warn=cog.error)

doc/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@
215215
# missing, so only use the extension if we are specifically spell-checking.
216216
extensions += ['sphinxcontrib.spelling']
217217
names_file = tempfile.NamedTemporaryFile(mode='w', prefix="coverage_names_", suffix=".txt")
218-
with open("../CONTRIBUTORS.txt") as contributors:
218+
with open("../CONTRIBUTORS.txt", encoding="utf-8") as contributors:
219219
names = set(re.split(r"[^\w']", contributors.read()))
220220
names = [n for n in names if len(n) >= 2 and n[0].isupper()]
221221
names_file.write("\n".join(names))

igor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def run_tests_with_coverage(core, *runner_args):
196196
# There's an entry in "make clean" to get rid of this file.
197197
pth_dir = sysconfig.get_path("purelib")
198198
pth_path = os.path.join(pth_dir, "zzz_metacov.pth")
199-
with open(pth_path, "w") as pth_file:
199+
with open(pth_path, "w", encoding="utf-8") as pth_file:
200200
pth_file.write("import coverage; coverage.process_startup()\n")
201201

202202
suffix = f"{make_env_id(core)}_{platform.platform()}"
@@ -374,14 +374,14 @@ def get_release_facts():
374374

375375
def update_file(fname, pattern, replacement):
376376
"""Update the contents of a file, replacing pattern with replacement."""
377-
with open(fname) as fobj:
377+
with open(fname, encoding="utf-8") as fobj:
378378
old_text = fobj.read()
379379

380380
new_text = re.sub(pattern, replacement, old_text, count=1)
381381

382382
if new_text != old_text:
383383
print(f"Updating {fname}")
384-
with open(fname, "w") as fobj:
384+
with open(fname, "w", encoding="utf-8") as fobj:
385385
fobj.write(new_text)
386386

387387

lab/branch_trace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def trace(frame, event, arg):
1111
last = this
1212
return trace
1313

14-
code = open(sys.argv[1]).read()
14+
code = open(sys.argv[1], encoding="utf-8").read()
1515
sys.settrace(trace)
1616
exec(code)
1717
print(sorted(pairs))

lab/extract_code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def f(a, b):
5252
fname, lineno = sys.argv[1:]
5353
lineno = int(lineno)
5454

55-
with open(fname) as code_file:
55+
with open(fname, encoding="utf-8") as code_file:
5656
lines = ["", *code_file]
5757

5858
# Find opening triple-quote

lab/goals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def main(argv):
6464
print("Need either --file or --group")
6565
return 1
6666

67-
with open("coverage.json") as j:
67+
with open("coverage.json", encoding="utf-8") as j:
6868
data = json.load(j)
6969
all_files = list(data["files"].keys())
7070
selected = select_files(all_files, args.pattern)

0 commit comments

Comments
 (0)