Skip to content

Commit

Permalink
greendns: full comment lines were not skipped; Thanks to nat-goodspeed
Browse files Browse the repository at this point in the history
`greendns.HostsResolver.LINES_RE` doesn't admit the possibility of a
comment starting at the beginning of a line. This has been ignored since `is_ipv4_addr()` and `is_ipv6_addr()` catch `dns.exception.SyntaxError` and return `False`. But in a runtime environment that encounters eventlet#413, `dns.exception.SyntaxError` is not caught, and the
import fails.

Changing '+' to '*' allows `HostsResolver._readlines()` to recognize and skip
comment lines.

greendns.HostsResolver._readlines(), a purely internal method, now returns an
itertools generator rather than a list. Change relevant asserts to build a
list before comparing.
  • Loading branch information
nat-goodspeed authored and temoto committed May 7, 2018
1 parent b40e18d commit 705809f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions eventlet/support/greendns.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class HostsResolver(object):

LINES_RE = re.compile(r"""
\s* # Leading space
([^\r\n#]+?) # The actual match, non-greedy so as not to include trailing space
([^\r\n#]*?) # The actual match, non-greedy so as not to include trailing space
\s* # Trailing space
(?:[#][^\r\n]+)? # Comments
(?:$|[\r\n]+) # EOF or newline
Expand Down Expand Up @@ -196,7 +196,7 @@ def _readlines(self):

udata = fdata.decode(errors='ignore')

return self.LINES_RE.findall(udata)
return six.moves.filter(None, self.LINES_RE.findall(udata))

def _load(self):
"""Load hosts file
Expand Down
18 changes: 12 additions & 6 deletions tests/greendns_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,37 @@ def test_readlines_lines(self):
hr = _make_host_resolver()
hr.hosts.write(b'line0\n')
hr.hosts.flush()
assert hr._readlines() == ['line0']
assert list(hr._readlines()) == ['line0']
hr._last_stat = 0
hr.hosts.write(b'line1\n')
hr.hosts.flush()
assert hr._readlines() == ['line0', 'line1']
assert list(hr._readlines()) == ['line0', 'line1']
# Test reading of varied newline styles
hr._last_stat = 0
hr.hosts.seek(0)
hr.hosts.truncate()
hr.hosts.write(b'\naa\r\nbb\r cc \n\n\tdd ee')
hr.hosts.flush()
assert hr._readlines() == ['aa', 'bb', 'cc', 'dd ee']
assert list(hr._readlines()) == ['aa', 'bb', 'cc', 'dd ee']
# Test comments, including inline comments
hr._last_stat = 0
hr.hosts.seek(0)
hr.hosts.truncate()
hr.hosts.write(b' line1\n#comment\nline2 # inline comment\n')
hr.hosts.write(b'''\
# First couple lines
# are comments.
line1
#comment
line2 # inline comment
''')
hr.hosts.flush()
assert hr._readlines() == ['line1', 'line2']
assert list(hr._readlines()) == ['line1', 'line2']

def test_readlines_missing_file(self):
hr = _make_host_resolver()
hr.hosts.close()
hr._last_stat = 0
assert hr._readlines() == []
assert list(hr._readlines()) == []

def test_load_no_contents(self):
hr = _make_host_resolver()
Expand Down

0 comments on commit 705809f

Please sign in to comment.