Skip to content

Commit 30178ff

Browse files
committed
Handle encoding issue
* Adding `errors=replace` for all the io.open functions Signed-off-by: Chin Yeung Li <[email protected]>
1 parent f499419 commit 30178ff

File tree

7 files changed

+14
-14
lines changed

7 files changed

+14
-14
lines changed

src/attributecode/attrib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def generate_from_file(abouts, is_about_input, license_dict, scancode, min_licen
221221
template_loc = add_unc(DEFAULT_TEMPLATE_FILE)
222222
else:
223223
template_loc = add_unc(template_loc)
224-
with io.open(template_loc, encoding='utf-8') as tplf:
224+
with io.open(template_loc, encoding='utf-8', errors='replace') as tplf:
225225
tpls = tplf.read()
226226
return generate(abouts, is_about_input, license_dict, scancode, min_license_score, template=tpls, variables=variables)
227227

@@ -259,7 +259,7 @@ def generate_and_save(abouts, is_about_input, license_dict, output_location, sca
259259

260260
if rendered:
261261
output_location = add_unc(output_location)
262-
with io.open(output_location, 'w', encoding='utf-8') as of:
262+
with io.open(output_location, 'w', encoding='utf-8', errors='replace') as of:
263263
of.write(rendered)
264264

265265
return errors, rendered

src/attributecode/cmd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def validate_template(ctx, param, value):
285285
if not value:
286286
return None
287287

288-
with io.open(value, encoding='utf-8') as templatef:
288+
with io.open(value, encoding='utf-8', errors='replace') as templatef:
289289
template_error = check_template(templatef.read())
290290

291291
if template_error:
@@ -728,7 +728,7 @@ def report_errors(errors, quiet, verbose, log_file_loc=None):
728728
click.echo(msg)
729729
if log_file_loc and errors:
730730
log_msgs, _ = get_error_messages(errors, quiet=False, verbose=True)
731-
with io.open(log_file_loc, 'w', encoding='utf-8') as lf:
731+
with io.open(log_file_loc, 'w', encoding='utf-8', errors='replace') as lf:
732732
lf.write('\n'.join(log_msgs))
733733
return severe_errors_count
734734

src/attributecode/model.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ def _validate(self, *args, **kwargs):
584584
try:
585585
# TODO: we have lots the location by replacing it with a text
586586
location = add_unc(location)
587-
with io.open(location, encoding='utf-8') as txt:
587+
with io.open(location, encoding='utf-8', errors='replace') as txt:
588588
text = txt.read()
589589
self.value[path] = text
590590
except Exception as e:
@@ -1207,7 +1207,7 @@ def dump(self, location, lic_dict=None):
12071207
if on_windows:
12081208
about_file_path = add_unc(about_file_path)
12091209

1210-
with io.open(about_file_path, mode='w', encoding='utf-8') as dumped:
1210+
with io.open(about_file_path, mode='w', encoding='utf-8', errors='replace') as dumped:
12111211
dumped.write(genereated_tk_version)
12121212
dumped.write(self.dumps(lic_dict))
12131213

@@ -1218,7 +1218,7 @@ def dump_android_notice(self, path, context):
12181218
if on_windows:
12191219
path = add_unc(path)
12201220

1221-
with io.open(path, mode='w', encoding='utf-8') as dumped:
1221+
with io.open(path, mode='w', encoding='utf-8', errors='replace') as dumped:
12221222
dumped.write(context)
12231223

12241224
def android_module_license(self, about_parent_path):
@@ -1285,7 +1285,7 @@ def dump_lic(self, location, license_dict):
12851285
license_name, license_filename, license_context, license_url = license_dict[lic_key]
12861286
license_info = (lic_key, license_name, license_filename, license_context, license_url)
12871287
license_key_name_context_url.append(license_info)
1288-
with io.open(license_path, mode='w', encoding='utf-8', newline='\n') as lic:
1288+
with io.open(license_path, mode='w', encoding='utf-8', newline='\n', errors='replace') as lic:
12891289
lic.write(license_context)
12901290
except Exception as e:
12911291
# TODO: it should return error if exception caught
@@ -1526,7 +1526,7 @@ def save_as_json(location, about_dicts):
15261526
output_file.write(json.dumps(data, indent=2))
15271527

15281528
def save_as_csv(location, about_dicts, field_names):
1529-
with io.open(location, mode='w', encoding='utf-8', newline='') as output_file:
1529+
with io.open(location, mode='w', encoding='utf-8', newline='', errors='replace') as output_file:
15301530
writer = csv.DictWriter(output_file, field_names)
15311531
writer.writeheader()
15321532
csv_formatted_list = util.format_about_dict_output(about_dicts)

src/attributecode/transform.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def from_file(cls, location):
282282
Load and return a Transformer instance from a YAML configuration file at
283283
`location`.
284284
"""
285-
with io.open(location, encoding='utf-8') as conf:
285+
with io.open(location, encoding='utf-8', errors='replace') as conf:
286286
data = saneyaml.load(replace_tab_with_spaces(conf.read()))
287287
return cls(
288288
field_renamings=data.get('field_renamings', {}),
@@ -400,7 +400,7 @@ def write_csv(location, data, field_names): # NOQA
400400
Write a CSV file at `location` the `data` list of ordered dicts using the
401401
`field_names`.
402402
"""
403-
with io.open(location, 'w', encoding='utf-8', newline='\n') as csvfile:
403+
with io.open(location, 'w', encoding='utf-8', newline='\n', errors='replace') as csvfile:
404404
writer = csv.DictWriter(csvfile, fieldnames=field_names)
405405
writer.writeheader()
406406
writer.writerows(data)

tests/test_attrib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_check_template_all_builtin_templates_are_valid(self):
6363
builtin_templates_dir = os.path.dirname(attrib.DEFAULT_TEMPLATE_FILE)
6464
for template in os.listdir(builtin_templates_dir):
6565
template_loc = os.path.join(builtin_templates_dir, template)
66-
with io.open(template_loc, 'r', encoding='utf-8') as tmpl:
66+
with io.open(template_loc, 'r', encoding='utf-8', errors='replace') as tmpl:
6767
template = tmpl.read()
6868
try:
6969
assert None == attrib.check_template(template)

tests/test_cmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def test_report_errors_can_write_to_logfile():
154154
result_file = get_temp_file()
155155
_ec = cmd.report_errors(errors, quiet=False, verbose=True,
156156
log_file_loc=result_file)
157-
with io.open(result_file, 'r', encoding='utf-8') as rf:
157+
with io.open(result_file, 'r', encoding='utf-8', errors='replace') as rf:
158158
result = rf.read()
159159
expected = [
160160
'Command completed with 3 errors or warnings.',

tests/test_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def get_unicode_content(location):
9393
"""
9494
Read file at location and return a unicode string.
9595
"""
96-
with io.open(location, encoding='utf-8') as doc:
96+
with io.open(location, encoding='utf-8', errors='replace') as doc:
9797
return doc.read()
9898

9999

0 commit comments

Comments
 (0)