Skip to content

Commit 3ef3bcb

Browse files
authored
bpo-30378: Fix the problem that SysLogHandler can't handle IPv6 addresses (#1904) (#1676)
1 parent 662d856 commit 3ef3bcb

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

Lib/logging/handlers.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -760,14 +760,29 @@ def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
760760
self.unixsocket = 1
761761
self._connect_unixsocket(address)
762762
else:
763-
self.unixsocket = 0
763+
self.unixsocket = False
764764
if socktype is None:
765765
socktype = socket.SOCK_DGRAM
766-
self.socket = socket.socket(socket.AF_INET, socktype)
767-
if socktype == socket.SOCK_STREAM:
768-
self.socket.connect(address)
766+
host, port = address
767+
ress = socket.getaddrinfo(host, port, 0, socktype)
768+
if not ress:
769+
raise socket.error("getaddrinfo returns an empty list")
770+
for res in ress:
771+
af, socktype, proto, _, sa = res
772+
err = sock = None
773+
try:
774+
sock = socket.socket(af, socktype, proto)
775+
if socktype == socket.SOCK_STREAM:
776+
sock.connect(sa)
777+
break
778+
except socket.error as exc:
779+
err = exc
780+
if sock is not None:
781+
sock.close()
782+
if err is not None:
783+
raise err
784+
self.socket = sock
769785
self.socktype = socktype
770-
self.formatter = None
771786

772787
def _connect_unixsocket(self, address):
773788
use_socktype = self.socktype
@@ -812,7 +827,7 @@ def encodePriority(self, facility, priority):
812827
priority = self.priority_names[priority]
813828
return (facility << 3) | priority
814829

815-
def close (self):
830+
def close(self):
816831
"""
817832
Closes the socket.
818833
"""

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ Extension Modules
4949
Library
5050
-------
5151

52+
- bpo-30378: Fix the problem that logging.handlers.SysLogHandler cannot
53+
handle IPv6 addresses.
54+
5255
- bpo-29960: Preserve generator state when _random.Random.setstate()
5356
raises an exception. Patch by Bryan Olson.
5457

0 commit comments

Comments
 (0)