find: add locale encoding in err msg when none is given (#86641)

When the search fails, as expected, the following error is printed out:

    [...] Failed to read the file FILENAME due to an encoding error. current encoding: None (default determined by the Python built-in function "open")

Add locale encoding in error messages when none is given.

As well, this case is hit for decoding exceptions, not encoding ones.

Change the error message.

Add the corresponding tests, update documentation.

Link: https://docs.python.org/3/library/locale.html#locale.getpreferredencoding
Co-authored-by: Matt Clay <matt@mystile.com>
Signed-off-by: Ariel Otilibili <a.otilibili@instadeep.com>
pull/86706/head
Ariel Otilibili 2 months ago committed by GitHub
parent e65e27c38a
commit 7d2aa2a4c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,3 @@
---
minor_changes:
- find - add locale encoding in err msg when none is given

@ -152,6 +152,7 @@ options:
encoding:
description:
- When doing a O(contains) search, determine the encoding of the files to be searched.
- When no value is provided, the Python default encoding is used.
type: str
version_added: "2.17"
limit:
@ -283,6 +284,7 @@ skipped_paths:
import errno
import fnmatch
import grp
import locale
import os
import pwd
import re
@ -388,8 +390,13 @@ def contentfilter(fsname, pattern, encoding, read_whole_file=False):
raise e
except UnicodeDecodeError as e:
if encoding is None:
encoding = 'None (default determined by the Python built-in function "open")'
msg = f'Failed to read the file {fsname} due to an encoding error. current encoding: {encoding}'
# Get the default encoding for the current locale
# This is the same encoding that the open() function uses by default
# when no encoding is specified. This value is platform dependent.
#
# https://docs.python.org/3/library/functions.html#open
encoding = f"{locale.getpreferredencoding(False).lower()} (Python default)"
msg = f'Failed to decode the file {fsname!r} with encoding: {encoding}'
raise Exception(msg) from e
except Exception:
pass

@ -211,7 +211,23 @@
- fail_to_read_wrong_encoding_file.msg == 'Not all paths examined, check warnings for details'
- >-
fail_to_read_wrong_encoding_file.skipped_paths[remote_tmp_dir_test] ==
("Failed to read the file %s/hello_world.gbk due to an encoding error. current encoding: utf-8" % (remote_tmp_dir_test))
("Failed to decode the file '%s/hello_world.gbk' with encoding: utf-8" % (remote_tmp_dir_test))
- name: read a gbk file with no encoding specified (uses locale default)
find:
paths: "{{ remote_tmp_dir_test }}"
patterns: "*.gbk"
contains: "你好世界"
register: fail_to_read_default_encoding
- name: Verify error message shows locale encoding name
vars:
error_pattern: 'Failed to decode the file .+ with encoding: .+ \(Python default\)'
assert:
that:
- fail_to_read_default_encoding.msg == 'Not all paths examined, check warnings for details'
- fail_to_read_default_encoding.skipped_paths[remote_tmp_dir_test] is search("^" + error_pattern + "$")
- fail_to_read_default_encoding.warnings | select('search', error_pattern) | list | length >= 1
- name: read a gbk file by gbk
find:

Loading…
Cancel
Save