Skip to content

Commit 95b4da2

Browse files
authored
bpo-30378: Fix the problem that SysLogHandler can't handle IPv6 addresses (#1676) (#1903)
1 parent 9d752aa commit 95b4da2

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

Lib/logging/handlers.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -827,11 +827,26 @@ def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
827827
self.unixsocket = False
828828
if socktype is None:
829829
socktype = socket.SOCK_DGRAM
830-
self.socket = socket.socket(socket.AF_INET, socktype)
831-
if socktype == socket.SOCK_STREAM:
832-
self.socket.connect(address)
830+
host, port = address
831+
ress = socket.getaddrinfo(host, port, 0, socktype)
832+
if not ress:
833+
raise OSError("getaddrinfo returns an empty list")
834+
for res in ress:
835+
af, socktype, proto, _, sa = res
836+
err = sock = None
837+
try:
838+
sock = socket.socket(af, socktype, proto)
839+
if socktype == socket.SOCK_STREAM:
840+
sock.connect(sa)
841+
break
842+
except OSError as exc:
843+
err = exc
844+
if sock is not None:
845+
sock.close()
846+
if err is not None:
847+
raise err
848+
self.socket = sock
833849
self.socktype = socktype
834-
self.formatter = None
835850

836851
def _connect_unixsocket(self, address):
837852
use_socktype = self.socktype
@@ -870,7 +885,7 @@ def encodePriority(self, facility, priority):
870885
priority = self.priority_names[priority]
871886
return (facility << 3) | priority
872887

873-
def close (self):
888+
def close(self):
874889
"""
875890
Closes the socket.
876891
"""

Lib/test/test_logging.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,7 +1663,7 @@ def setUp(self):
16631663
server.ready.wait()
16641664
hcls = logging.handlers.SysLogHandler
16651665
if isinstance(server.server_address, tuple):
1666-
self.sl_hdlr = hcls(('localhost', server.port))
1666+
self.sl_hdlr = hcls((server.server_address[0], server.port))
16671667
else:
16681668
self.sl_hdlr = hcls(server.server_address)
16691669
self.log_output = ''
@@ -1723,6 +1723,24 @@ def tearDown(self):
17231723
SysLogHandlerTest.tearDown(self)
17241724
support.unlink(self.address)
17251725

1726+
@unittest.skipUnless(support.IPV6_ENABLED,
1727+
'IPv6 support required for this test.')
1728+
@unittest.skipUnless(threading, 'Threading required for this test.')
1729+
class IPv6SysLogHandlerTest(SysLogHandlerTest):
1730+
1731+
"""Test for SysLogHandler with IPv6 host."""
1732+
1733+
server_class = TestUDPServer
1734+
address = ('::1', 0)
1735+
1736+
def setUp(self):
1737+
self.server_class.address_family = socket.AF_INET6
1738+
super(IPv6SysLogHandlerTest, self).setUp()
1739+
1740+
def tearDown(self):
1741+
self.server_class.address_family = socket.AF_INET
1742+
super(IPv6SysLogHandlerTest, self).tearDown()
1743+
17261744
@unittest.skipUnless(threading, 'Threading required for this test.')
17271745
class HTTPHandlerTest(BaseTest):
17281746
"""Test for HTTPHandler."""
@@ -4378,7 +4396,7 @@ def test_main():
43784396
QueueHandlerTest, ShutdownTest, ModuleLevelMiscTest, BasicConfigTest,
43794397
LoggerAdapterTest, LoggerTest, SMTPHandlerTest, FileHandlerTest,
43804398
RotatingFileHandlerTest, LastResortTest, LogRecordTest,
4381-
ExceptionTest, SysLogHandlerTest, HTTPHandlerTest,
4399+
ExceptionTest, SysLogHandlerTest, IPv6SysLogHandlerTest, HTTPHandlerTest,
43824400
NTEventLogHandlerTest, TimedRotatingFileHandlerTest,
43834401
UnixSocketHandlerTest, UnixDatagramHandlerTest, UnixSysLogHandlerTest,
43844402
MiscTestCase

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ Core and Builtins
4545
Library
4646
-------
4747

48+
- bpo-30378: Fix the problem that logging.handlers.SysLogHandler cannot
49+
handle IPv6 addresses.
50+
4851
- bpo-29960: Preserve generator state when _random.Random.setstate()
4952
raises an exception. Patch by Bryan Olson.
5053

0 commit comments

Comments
 (0)