Skip to content

Commit 5389878

Browse files
author
Anthony Yang
committed
add encoding='utf-8' to open
windows may not assume utf-8 when reading/writing to a file
1 parent 5ac92a1 commit 5389878

File tree

9 files changed

+24
-24
lines changed

9 files changed

+24
-24
lines changed

compiler_integration_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def main():
267267
final_results = evaluate_and_compare_test_cases(TEST_CASES, n=1)
268268
print()
269269

270-
with open('compiler_integration_results.md', 'w') as f:
270+
with open('compiler_integration_results.md', 'w', encoding='utf-8') as f:
271271
dump_results(final_results, f)
272272

273273

harmony_model_checker/dfacmp.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from automata.fa.dfa import DFA
44

55
def parse(file):
6-
with open(file) as f:
6+
with open(file, encoding='utf-8') as f:
77
js = json.load(f)
88

99
states = { "__error__": {} }

harmony_model_checker/harmony/behavior.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def find_error_states(transitions, final_states):
2828
return error_states
2929

3030
def read_hfa(file, dfa, nfa):
31-
with open(file) as fd:
31+
with open(file, encoding='utf-8') as fd:
3232
js = json.load(fd)
3333
initial = js["initial"]
3434
states = { "{}" }
@@ -249,7 +249,7 @@ def add_edge(src, val, dst):
249249
dfa_error_states = find_error_states(dfa_transitions, dfa_final_states)
250250

251251
if outputfiles["hfa"] != None:
252-
with open(outputfiles["hfa"], "w") as fd:
252+
with open(outputfiles["hfa"], "w", encoding='utf-8') as fd:
253253
names = {}
254254
for (idx, s) in enumerate(dfa_states):
255255
names[s] = idx
@@ -295,7 +295,7 @@ def add_edge(src, val, dst):
295295
print("}", file=fd)
296296

297297
if outputfiles["gv"] != None:
298-
with open(outputfiles["gv"], "w") as fd:
298+
with open(outputfiles["gv"], "w", encoding='utf-8') as fd:
299299
names = {}
300300
for (idx, s) in enumerate(dfa_states):
301301
names[s] = idx

harmony_model_checker/harmony/brief.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def print_macrostep(self, mas):
125125
self.interrupted = "interrupt" in self.lastmis and self.lastmis["interrupt"] == "True"
126126

127127
def run(self, outputfiles, behavior):
128-
with open(outputfiles["hco"]) as f:
128+
with open(outputfiles["hco"], encoding='utf-8') as f:
129129
print("Phase 5: loading", outputfiles["hco"])
130130
top = json.load(f)
131131
assert isinstance(top, dict)

harmony_model_checker/harmony/genhtml.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self):
2020
self.js = (self_dir / "charm.js").read_text()
2121

2222
def file_include(self, name, f):
23-
with open(name) as g:
23+
with open(name, encoding='utf-8') as g:
2424
print(g.read(), file=f)
2525

2626
def html_megastep(self, step, tid, name, nmicrosteps, width, f):
@@ -335,7 +335,7 @@ def vars_add(self, vardir, shared):
335335
def run(self, outputfiles):
336336
# First figure out how many megasteps there are and how many threads
337337
lasttid = -1
338-
with open(outputfiles["hco"]) as f:
338+
with open(outputfiles["hco"], encoding='utf-8') as f:
339339
self.top = json.load(f)
340340
assert isinstance(self.top, dict)
341341
if "macrosteps" in self.top:
@@ -356,5 +356,5 @@ def run(self, outputfiles):
356356
if tid >= self.nthreads:
357357
self.nthreads = tid + 1
358358

359-
with open(outputfiles["htm"], "w") as out:
359+
with open(outputfiles["htm"], "w", encoding='utf-8') as out:
360360
self.html(out, outputfiles)

harmony_model_checker/harmony/harmony.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def doImport(scope, code, module):
103103
for dir in [ os.path.dirname(namestack[-1]), install_path + "/modules", "." ]:
104104
filename = dir + "/" + modname + ".hny"
105105
if os.path.exists(filename):
106-
with open(filename) as f:
106+
with open(filename, encoding='utf-8') as f:
107107
load(f, filename, scope2, code)
108108
found = True
109109
break
@@ -2523,7 +2523,7 @@ def doImport(self, scope, code, module):
25232523
for dir in [ os.path.dirname(namestack[-1]), install_path + "/modules", "." ]:
25242524
filename = dir + "/" + modname + ".hny"
25252525
if os.path.exists(filename):
2526-
with open(filename) as f:
2526+
with open(filename, encoding='utf-8') as f:
25272527
load(f, filename, scope2, code)
25282528
found = True
25292529
break
@@ -5380,7 +5380,7 @@ def doCompile(filenames, consts, mods, interface):
53805380
code.append(FrameOp(("__init__", None, None, None), []))
53815381
for fname in filenames:
53825382
try:
5383-
with open(fname) as fd:
5383+
with open(fname, encoding='utf-8') as fd:
53845384
load(fd, fname, scope, code)
53855385
except IOError:
53865386
print("harmony: can't open", fname, file=sys.stderr)
@@ -5996,7 +5996,7 @@ def htmlcode(code, scope, f):
59965996
print("</div>", file=f)
59975997

59985998
def htmldump(nodes, code, scope, node, fulldump, verbose):
5999-
with open("harmony.html", "w") as f:
5999+
with open("harmony.html", "w", encoding='utf-8') as f:
60006000
print("""
60016001
<html>
60026002
<head>
@@ -7576,7 +7576,7 @@ def main():
75767576
code, scope = doCompile(args, consts, mods, interface)
75777577
except HarmonyCompilerError as e:
75787578
if parse_code_only:
7579-
with open(outputfiles["hvm"], "w") as f:
7579+
with open(outputfiles["hvm"], "w", encoding='utf-8') as f:
75807580
data = {
75817581
"errors": [e.token._as_dict()],
75827582
"status": "error"
@@ -7586,12 +7586,12 @@ def main():
75867586
sys.exit(1)
75877587

75887588
if parse_code_only:
7589-
with open(outputfiles["hvm"], "w") as f:
7589+
with open(outputfiles["hvm"], "w", encoding='utf-8') as f:
75907590
f.write(json.dumps({"status": "ok"}))
75917591
return
75927592

75937593
if outputfiles["tla"] != None:
7594-
with open(outputfiles["tla"], "w") as fd:
7594+
with open(outputfiles["tla"], "w", encoding='utf-8') as fd:
75957595
tla_translate(fd, code, scope)
75967596

75977597
install_path = os.path.dirname(os.path.realpath(__file__))
@@ -7600,7 +7600,7 @@ def main():
76007600
# see if there is a configuration file
76017601
infile = "%s/charm.c"%install_path
76027602
outfile = "%s/charm.exe"%install_path
7603-
with open(outputfiles["hvm"], "w") as fd:
7603+
with open(outputfiles["hvm"], "w", encoding='utf-8') as fd:
76047604
dumpCode("json", code, scope, f=fd)
76057605
print("Phase 2: run the model checker")
76067606
sys.stdout.flush()

harmony_model_checker/iface.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def main():
272272
usage()
273273

274274
file = args[0]
275-
with open(file) as f:
275+
with open(file, encoding='utf-8') as f:
276276
js = json.load(f)
277277
parse(js, outfmt, minify);
278278

harmony_model_checker/main.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def load_file(filename: str, scope: Scope, code: Code):
7777
if filename in legacy_harmony.files:
7878
return
7979
legacy_harmony.namestack.append(filename)
80-
with open(filename, "r") as f:
80+
with open(filename, "r", encoding='utf-8') as f:
8181
legacy_harmony.files[filename] = f.read().split("\n")
8282

8383
ast = parse(filename)
@@ -363,7 +363,7 @@ def main():
363363

364364
if parse_code_only:
365365
data = dict(errors=[e._asdict() for e in errors], status="error")
366-
with open(output_files["hvm"], "w") as fp:
366+
with open(output_files["hvm"], "w", encoding='utf-8') as fp:
367367
json.dump(data, fp)
368368
else:
369369
for e in errors:
@@ -372,18 +372,18 @@ def main():
372372
return 1
373373

374374
if parse_code_only:
375-
with open(output_files["hvm"], "w") as f:
375+
with open(output_files["hvm"], "w", encoding='utf-8') as f:
376376
f.write(json.dumps({"status": "ok"}))
377377
return
378378

379379
if output_files["tla"] is not None:
380-
with open(output_files["tla"], "w") as f:
380+
with open(output_files["tla"], "w", encoding='utf-8') as f:
381381
legacy_harmony.tla_translate(f, code, scope)
382382

383383
# Analyze liveness of variables
384384
if charm_flag:
385385
# see if there is a configuration file
386-
with open(output_files["hvm"], "w") as fd:
386+
with open(output_files["hvm"], "w", encoding='utf-8') as fd:
387387
legacy_harmony.dumpCode("json", code, scope, f=fd)
388388

389389
if parse_code_only:

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def read(rel_path: str) -> str:
1515
here = os.path.abspath(os.path.dirname(__file__))
1616
# intentionally *not* adding an encoding option to open, See:
1717
# https://github.com/pypa/virtualenv/issues/201#issuecomment-3145690
18-
with open(os.path.join(here, rel_path)) as fp:
18+
with open(os.path.join(here, rel_path), encoding='utf-8') as fp:
1919
return fp.read()
2020

2121
def get_version(rel_path: str) -> str:

0 commit comments

Comments
 (0)