Skip to content

Commit

Permalink
Fix Python scripts for Windows: UTF-8 encoding (datacarpentry#485)
Browse files Browse the repository at this point in the history
To avoid problems with various symbols, we have to specify the encoding
when we read files.
The actual codec name is `utf_8` but aliases like `utf8`, `utf-8`, etc
are accepted. Here, I'm using `utf-8` alias.
https://docs.python.org/3.8/library/codecs.html#standard-encodings

This fixes `make lesson-check` when running under 'Git for Windows' for
lessons that have non-cp1252 characters.
  • Loading branch information
maxim-belkin authored Aug 11, 2020
1 parent 95221b1 commit 52597a5
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bin/lesson_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def read_references(reporter, ref_path):
result = {}
urls_seen = set()

with open(ref_path, 'r') as reader:
with open(ref_path, 'r', encoding='utf-8') as reader:
for (num, line) in enumerate(reader, 1):

if P_INTERNAL_INCLUDE_LINK.search(line): continue
Expand Down
4 changes: 2 additions & 2 deletions bin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def read_markdown(parser, path):
"""

# Split and extract YAML (if present).
with open(path, 'r') as reader:
with open(path, 'r', encoding='utf-8') as reader:
body = reader.read()
metadata_raw, metadata_yaml, body = split_metadata(path, body)

Expand Down Expand Up @@ -160,7 +160,7 @@ def load_yaml(filename):
"""

try:
with open(filename, 'r') as reader:
with open(filename, 'r', encoding='utf-8') as reader:
return yaml.load(reader, Loader=yaml.SafeLoader)
except (yaml.YAMLError, IOError) as e:
print('Unable to load YAML file {0}:\n{1}'.format(
Expand Down
2 changes: 1 addition & 1 deletion bin/workshop_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def main():
reporter = Reporter()
check_config(reporter, config_file)
check_unwanted_files(root_dir, reporter)
with open(index_file) as reader:
with open(index_file, encoding='utf-8') as reader:
data = reader.read()
check_file(reporter, index_file, data)
reporter.report()
Expand Down

0 comments on commit 52597a5

Please sign in to comment.