From c38437d5653286ee39cfbe3154d5e96ea6ce2e7e Mon Sep 17 00:00:00 2001 From: Jon Hadfield Date: Sun, 21 Apr 2024 10:47:03 +0100 Subject: [PATCH] adds compatibility with hosts file containing non-ASCII characters. --- python_hosts/hosts.py | 2 +- python_hosts/utils.py | 2 +- test_files/hosts3 | 4 ++++ tests/test_hosts.py | 11 +++++++++++ tests/test_hosts_entry.py | 11 +++++++++++ 5 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 test_files/hosts3 diff --git a/python_hosts/hosts.py b/python_hosts/hosts.py index bbfa507..e0a5bad 100755 --- a/python_hosts/hosts.py +++ b/python_hosts/hosts.py @@ -362,7 +362,7 @@ def import_file(self, import_file_path=None): invalid_count = 0 if is_readable(import_file_path): import_entries = [] - with open(import_file_path, 'r') as infile: + with open(import_file_path, 'r', encoding='utf-8-sig') as infile: for line in infile: stripped_entry = line.strip() if (not stripped_entry) or (stripped_entry.startswith('#')): diff --git a/python_hosts/utils.py b/python_hosts/utils.py index 890c036..32c2506 100644 --- a/python_hosts/utils.py +++ b/python_hosts/utils.py @@ -43,7 +43,7 @@ def valid_hostnames(hostname_list): for entry in hostname_list: if len(entry) > 255: return False - allowed = re.compile(r'(?!-)[A-Z\d-]{1,63}(?