]> granicus.if.org Git - python/commitdiff
Issue #17795: Reverted backwards-incompatible change in SysLogHandler with Unix domai...
authorVinay Sajip <vinay_sajip@yahoo.co.uk>
Mon, 22 Apr 2013 09:07:40 +0000 (10:07 +0100)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>
Mon, 22 Apr 2013 09:07:40 +0000 (10:07 +0100)
Lib/logging/handlers.py
Misc/NEWS

index f286cd61d4496c696ed568a91c75c00b7a19c80a..95e23a8b78bdfee32d28be66a2c83314f37c1835 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright 2001-2012 by Vinay Sajip. All Rights Reserved.
+# Copyright 2001-2013 by Vinay Sajip. All Rights Reserved.
 #
 # Permission to use, copy, modify, and distribute this software and its
 # documentation for any purpose and without fee is hereby granted,
@@ -18,7 +18,7 @@
 Additional handlers for the logging package for Python. The core package is
 based on PEP 282 and comments thereto in comp.lang.python.
 
-Copyright (C) 2001-2012 Vinay Sajip. All Rights Reserved.
+Copyright (C) 2001-2013 Vinay Sajip. All Rights Reserved.
 
 To use, simply 'import logging.handlers' and log away!
 """
@@ -767,7 +767,7 @@ class SysLogHandler(logging.Handler):
     }
 
     def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
-                 facility=LOG_USER, socktype=socket.SOCK_DGRAM):
+                 facility=LOG_USER, socktype=None):
         """
         Initialize a handler.
 
@@ -786,18 +786,37 @@ class SysLogHandler(logging.Handler):
             self._connect_unixsocket(address)
         else:
             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)
+            self.socktype = socktype
         self.formatter = None
 
     def _connect_unixsocket(self, address):
-        self.socket = socket.socket(socket.AF_UNIX, self.socktype)
+        use_socktype = self.socktype
+        if use_socktype is None:
+            use_socktype = socket.SOCK_DGRAM
+        self.socket = socket.socket(socket.AF_UNIX, use_socktype)
         try:
             self.socket.connect(address)
+            # it worked, so set self.socktype to the used type
+            self.socktype = use_socktype
         except socket.error:
             self.socket.close()
-            raise
+            if self.socktype is not None:
+                # user didn't specify falling back, so fail
+                raise
+            use_socktype = socket.SOCK_STREAM
+            self.socket = socket.socket(socket.AF_UNIX, use_socktype)
+            try:
+                self.socket.connect(address)
+                # it worked, so set self.socktype to the used type
+                self.socktype = use_socktype
+            except socket.error:
+                self.socket.close()
+                raise
 
     def encodePriority(self, facility, priority):
         """
index 25ad4060ab1586d2b15441f6bceafc08bc49b6de..e71b6ed016abef49c1fde270a0e7d9a91b88b3be 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #17795: Reverted backwards-incompatible change in SysLogHandler with
+  Unix domain sockets.
+
 - Issue #17555: Fix ForkAwareThreadLock so that size of after fork
   registry does not grow exponentially with generation of process.