diff --git a/pyxform/instance.py b/pyxform/instance.py index eff6a1cf..98a60087 100644 --- a/pyxform/instance.py +++ b/pyxform/instance.py @@ -78,7 +78,7 @@ def import_from_xml(self, xml_string_or_filename): import os.path if os.path.isfile(xml_string_or_filename): - xml_str = open(xml_string_or_filename).read() + xml_str = open(xml_string_or_filename, encoding="utf-8").read() else: xml_str = xml_string_or_filename key_val_dict = parse_xform_instance(xml_str) diff --git a/pyxform/utils.py b/pyxform/utils.py index b74ae285..70c1bebc 100644 --- a/pyxform/utils.py +++ b/pyxform/utils.py @@ -157,7 +157,7 @@ def xls_sheet_to_csv(workbook_path, csv_path, sheet_name): return False if not sheet or sheet.nrows < 2: return False - with open(csv_path, "w", newline="") as f: + with open(csv_path, mode="w", encoding="utf-8", newline="") as f: writer = csv.writer(f, quoting=csv.QUOTE_ALL) mask = [v and len(v.strip()) > 0 for v in sheet.row_values(0)] for row_idx in range(sheet.nrows): @@ -185,7 +185,7 @@ def xlsx_sheet_to_csv(workbook_path, csv_path, sheet_name): except KeyError: return False - with open(csv_path, "w", newline="") as f: + with open(csv_path, mode="w", encoding="utf-8", newline="") as f: writer = csv.writer(f, quoting=csv.QUOTE_ALL) mask = [not is_empty(cell.value) for cell in sheet[1]] for row in sheet.rows: @@ -225,7 +225,8 @@ def get_languages_with_bad_tags(languages): """ Returns languages with invalid or missing IANA subtags. """ - with open(os.path.join(os.path.dirname(__file__), "iana_subtags.txt")) as f: + path = os.path.join(os.path.dirname(__file__), "iana_subtags.txt") + with open(path, encoding="utf-8") as f: iana_subtags = f.read().splitlines() lang_code_regex = re.compile(r"\((.*)\)$") diff --git a/pyxform/validators/updater.py b/pyxform/validators/updater.py index 732eaa39..6cefc099 100644 --- a/pyxform/validators/updater.py +++ b/pyxform/validators/updater.py @@ -96,7 +96,7 @@ def _read_json(file_path): Read the JSON file to a string. """ _UpdateHandler._check_path(file_path=file_path) - with open(file_path) as in_file: + with open(file_path, encoding="utf-8") as in_file: return json.load(in_file) @staticmethod @@ -104,7 +104,7 @@ def _write_json(file_path, content): """ Save the JSON data to a file. """ - with open(file_path, mode="w", newline="\n") as out_file: + with open(file_path, mode="w", encoding="utf-8", newline="\n") as out_file: data = json.dumps(content, indent=2, sort_keys=True) out_file.write(str(data)) @@ -114,7 +114,7 @@ def _read_last_check(file_path): Read the .last_check file. """ _UpdateHandler._check_path(file_path=file_path) - with open(file_path) as in_file: + with open(file_path, encoding="utf-8") as in_file: first_line = in_file.readline() try: last_check = datetime.strptime(first_line, UTC_FMT) @@ -128,7 +128,7 @@ def _write_last_check(file_path, content): """ Write the .last_check file. """ - with open(file_path, mode="w", newline="\n") as out_file: + with open(file_path, mode="w", encoding="utf-8", newline="\n") as out_file: out_file.write(str(content.strftime(UTC_FMT))) @staticmethod diff --git a/pyxform/xls2json.py b/pyxform/xls2json.py index a46f9364..548cffe7 100644 --- a/pyxform/xls2json.py +++ b/pyxform/xls2json.py @@ -1693,7 +1693,7 @@ def __init__(self, path_or_file, default_name=None): def print_warning_log(self, warn_out_file): # Open file to print warning log to. - warn_out = open(warn_out_file, "w") + warn_out = open(warn_out_file, mode="w", encoding="utf-8") warn_out.write("\n".join(self._warnings)) diff --git a/tests/test_external_instances_for_selects.py b/tests/test_external_instances_for_selects.py index b4291675..32b78b28 100644 --- a/tests/test_external_instances_for_selects.py +++ b/tests/test_external_instances_for_selects.py @@ -441,7 +441,7 @@ def test_itemset_csv_generated_from_external_choices(self): self.assertIn(log_msg, [r.message for r in log.records]) self.assertTrue(os.path.exists(itemsets_path)) - with open(itemsets_path) as csv: + with open(itemsets_path, encoding="utf-8") as csv: rows = csv.readlines() # Should have the non-empty headers in the first row. self.assertEqual('"list_name","name","state","city"\n', rows[0]) diff --git a/tests/test_xform2json.py b/tests/test_xform2json.py index 7e150950..0409a40b 100644 --- a/tests/test_xform2json.py +++ b/tests/test_xform2json.py @@ -77,7 +77,7 @@ def test_try_parse_with_path(self): """Should return root node from XML file path.""" xml_path = os.path.join(test_output.PATH, "test_try_parse.xml") self.tidy_file = xml_path - with open(xml_path, "w") as xml_file: + with open(xml_path, mode="w", encoding="utf-8") as xml_file: xml_file.write(self.xml) root = _try_parse(xml_path) self.assertEqual("a", root.tag) @@ -97,7 +97,7 @@ def test_try_parse_with_bad_file(self): """Should raise XMLSyntaxError: file exists but content is not valid.""" xml_path = os.path.join(test_output.PATH, "test_try_parse.xml") self.tidy_file = xml_path - with open(xml_path, "w") as xml_file: + with open(xml_path, mode="w", encoding="utf-8") as xml_file: xml_file.write("not valid xml :(") with self.assertRaises(ParseError): _try_parse(xml_path) diff --git a/tests/test_xls2json_xls.py b/tests/test_xls2json_xls.py index 51b53da0..1f914968 100644 --- a/tests/test_xls2json_xls.py +++ b/tests/test_xls2json_xls.py @@ -34,7 +34,9 @@ def test_simple_yes_or_no_question(self): with open(output_path, mode="w", encoding="utf-8") as fp: json.dump(x_results, fp=fp, ensure_ascii=False, indent=4) # Compare with the expected output: - with open(expected_output_path) as expected, open(output_path) as observed: + with open(expected_output_path, encoding="utf-8") as expected, open( + output_path, encoding="utf-8" + ) as observed: self.assertEqual(json.load(expected), json.load(observed)) def test_hidden(self): @@ -126,7 +128,9 @@ def test_table(self): with open(output_path, mode="w", encoding="utf-8") as fp: json.dump(x_results, fp=fp, ensure_ascii=False, indent=4) # Compare with the expected output: - with open(expected_output_path) as expected, open(output_path) as observed: + with open(expected_output_path, encoding="utf-8") as expected, open( + output_path, encoding="utf-8" + ) as observed: self.assertEqual(json.load(expected), json.load(observed)) def test_choice_filter_choice_fields(self): diff --git a/tests/utils.py b/tests/utils.py index aec95fe8..b2b2344d 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -95,7 +95,7 @@ def truncate_temp_files(temp_dir): truncate_temp_files(f.path) # Check still in temp directory elif f.path.startswith(temp_root): - with open(f.path, mode="w") as _: + with open(f.path, mode="w", encoding="utf-8") as _: pass diff --git a/tests/xform_test_case/test_attribute_columns.py b/tests/xform_test_case/test_attribute_columns.py index f0e9bcc7..e560ff3b 100644 --- a/tests/xform_test_case/test_attribute_columns.py +++ b/tests/xform_test_case/test_attribute_columns.py @@ -30,5 +30,7 @@ def test_conversion(self): survey.print_xform_to_file(self.output_path, warnings=warnings) # print warnings # Compare with the expected output: - with open(expected_output_path) as expected, open(self.output_path) as observed: + with open(expected_output_path, encoding="utf-8") as expected, open( + self.output_path, encoding="utf-8" + ) as observed: self.assertXFormEqual(expected.read(), observed.read()) diff --git a/tests/xform_test_case/test_bugs.py b/tests/xform_test_case/test_bugs.py index dc7ee407..7d9d6df3 100644 --- a/tests/xform_test_case/test_bugs.py +++ b/tests/xform_test_case/test_bugs.py @@ -93,7 +93,9 @@ def test_conversion(self): survey.print_xform_to_file(self.output_path, warnings=warnings) # print warnings # Compare with the expected output: - with open(expected_output_path) as expected, open(self.output_path) as observed: + with open(expected_output_path, encoding="utf-8") as expected, open( + self.output_path, encoding="utf-8" + ) as observed: self.assertXFormEqual(expected.read(), observed.read()) @@ -116,7 +118,9 @@ def test_conversion(self): survey.print_xform_to_file(self.output_path, warnings=warnings) # print warnings # Compare with the expected output: - with open(expected_output_path) as expected, open(self.output_path) as observed: + with open(expected_output_path, encoding="utf-8") as expected, open( + self.output_path, encoding="utf-8" + ) as observed: self.assertXFormEqual(expected.read(), observed.read()) @@ -141,7 +145,9 @@ def test_conversion(self): survey.print_xform_to_file(output_path, warnings=warnings) # print warnings # Compare with the expected output: - with open(expected_output_path) as expected, open(output_path) as observed: + with open(expected_output_path, encoding="utf-8") as expected, open( + output_path, encoding="utf-8" + ) as observed: self.assertXFormEqual(expected.read(), observed.read())