Skip to content

bpo-30378: Fix the problem that SysLogHandler can't handle IPv6 addresses #1676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions Lib/logging/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,11 +827,26 @@ def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
self.unixsocket = False
if socktype is None:
socktype = socket.SOCK_DGRAM
self.socket = socket.socket(socket.AF_INET, socktype)
if socktype == socket.SOCK_STREAM:
self.socket.connect(address)
host, port = address
ress = socket.getaddrinfo(host, port, 0, socktype)
if not ress:
raise OSError("getaddrinfo returns an empty list")
for res in ress:
af, socktype, proto, _, sa = res
err = sock = None
try:
sock = socket.socket(af, socktype, proto)
if socktype == socket.SOCK_STREAM:
sock.connect(sa)
break
except OSError as exc:
err = exc
if sock is not None:
sock.close()
if err is not None:
raise err
self.socket = sock
self.socktype = socktype
self.formatter = None

def _connect_unixsocket(self, address):
use_socktype = self.socktype
Expand Down Expand Up @@ -870,7 +885,7 @@ def encodePriority(self, facility, priority):
priority = self.priority_names[priority]
return (facility << 3) | priority

def close (self):
def close(self):
"""
Closes the socket.
"""
Expand Down
22 changes: 20 additions & 2 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@ def setUp(self):
server.ready.wait()
hcls = logging.handlers.SysLogHandler
if isinstance(server.server_address, tuple):
self.sl_hdlr = hcls(('localhost', server.port))
self.sl_hdlr = hcls((server.server_address[0], server.port))
else:
self.sl_hdlr = hcls(server.server_address)
self.log_output = ''
Expand Down Expand Up @@ -1738,6 +1738,24 @@ def tearDown(self):
SysLogHandlerTest.tearDown(self)
support.unlink(self.address)

@unittest.skipUnless(support.IPV6_ENABLED,
'IPv6 support required for this test.')
@unittest.skipUnless(threading, 'Threading required for this test.')
class IPv6SysLogHandlerTest(SysLogHandlerTest):

"""Test for SysLogHandler with IPv6 host."""

server_class = TestUDPServer
address = ('::1', 0)

def setUp(self):
self.server_class.address_family = socket.AF_INET6
super(IPv6SysLogHandlerTest, self).setUp()

def tearDown(self):
self.server_class.address_family = socket.AF_INET
super(IPv6SysLogHandlerTest, self).tearDown()

@unittest.skipUnless(threading, 'Threading required for this test.')
class HTTPHandlerTest(BaseTest):
"""Test for HTTPHandler."""
Expand Down Expand Up @@ -4404,7 +4422,7 @@ def test_main():
QueueHandlerTest, ShutdownTest, ModuleLevelMiscTest, BasicConfigTest,
LoggerAdapterTest, LoggerTest, SMTPHandlerTest, FileHandlerTest,
RotatingFileHandlerTest, LastResortTest, LogRecordTest,
ExceptionTest, SysLogHandlerTest, HTTPHandlerTest,
ExceptionTest, SysLogHandlerTest, IPv6SysLogHandlerTest, HTTPHandlerTest,
NTEventLogHandlerTest, TimedRotatingFileHandlerTest,
UnixSocketHandlerTest, UnixDatagramHandlerTest, UnixSysLogHandlerTest,
MiscTestCase
Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ Extension Modules
Library
-------

- bpo-30378: Fix the problem that logging.handlers.SysLogHandler cannot
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure having this in the patch is a good idea - may cause unnecessary merge conflicts.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, NEWS now is the main source of merge backport conflicts, but we need to log what happened. :-(

handle IPv6 addresses.

- bpo-16500: Allow registering at-fork handlers.

- bpo-30470: Deprecate invalid ctypes call protection on Windows. Patch by
Expand Down