Skip to content

Commit

Permalink
adds compatibility with hosts file containing non-ASCII characters.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhadfield committed Apr 21, 2024
1 parent 01974b2 commit c38437d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion python_hosts/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('#')):
Expand Down
2 changes: 1 addition & 1 deletion python_hosts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}(?<!-)$', re.IGNORECASE)
allowed = re.compile(r'(?!-)[A-Z\w-]{1,63}(?<!-)$', re.UNICODE | re.IGNORECASE)
if not all(allowed.match(x) for x in entry.split(".")):
return False
return True
Expand Down
4 changes: 4 additions & 0 deletions test_files/hosts3
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
5.5.5.5 test.com 测试
0.0.0.0 bob.com
0.0.0.0 jane.com
0.0.0.0 simon.com asif.com
11 changes: 11 additions & 0 deletions tests/test_hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,17 @@ def test_import_file_returns_duplicate_correctly(tmpdir):
assert write_result.get('ipv4_entries_written') == 2


def test_import_file_with_chinese_simplified(tmpdir):
"""
Test that importing of a file with simplified Chinese characters succeeds
"""
hosts_file = tmpdir.mkdir("etc").join("hosts")
hosts_file.write("6.6.6.6\texample.com\n")
hosts = Hosts(path=hosts_file.strpath)
hosts.import_file(import_file_path=os.path.join("test_files", "hosts3"))
assert hosts.exists(address='5.5.5.5', names=['test.com', '测试'])


def test_addition_of_ipv6_entry_where_matching_name_exists_and_force_false(tmpdir):
"""
Test no replacement of an ipv6 entry where the address is different
Expand Down
11 changes: 11 additions & 0 deletions tests/test_hosts_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ def test_create_ipv4_instance():
assert hosts_entry.comment == 'this is a comment'


def test_create_ipv4_instance_with_chinese_simplified():
""" add an ipv4 type entry """
hosts_entry = HostsEntry(entry_type='ipv4', address='1.2.3.4',
names=['example.com', 'example', '例'],
comment='这是一个评论')
assert hosts_entry.entry_type == 'ipv4'
assert hosts_entry.address == '1.2.3.4'
assert hosts_entry.names == ['example.com', 'example', '例']
assert hosts_entry.comment == '这是一个评论'


def test_str_to_hostentry_ipv4():
str_entry = HostsEntry.str_to_hostentry(
'10.10.10.10 example.com example.org example # another comment')
Expand Down

0 comments on commit c38437d

Please sign in to comment.