[Python-checkins] cpython (2.7): logging: Added locking in flush() and close() handler methods. Thanks to Fayaz

vinay.sajip python-checkins at python.org
Thu Feb 23 21:04:46 CET 2012


http://hg.python.org/cpython/rev/1316d2af2331
changeset: 75209:1316d2af2331
branch: 2.7
parent: 75207:51615c55781f
user: Vinay Sajip <vinay_sajip at yahoo.co.uk>
date: Thu Feb 23 19:37:18 2012 +0000
summary:
 logging: Added locking in flush() and close() handler methods. Thanks to Fayaz Yusuf Khan for the suggestion.
files:
 Lib/logging/__init__.py | 20 ++++++++++--------
 Lib/logging/handlers.py | 31 ++++++++++++++++------------
 2 files changed, 29 insertions(+), 22 deletions(-)
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -1,4 +1,4 @@
-# Copyright 2001-2010 by Vinay Sajip. All Rights Reserved.
+# Copyright 2001-2012 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,
@@ -828,8 +828,9 @@
 """
 Flushes the stream.
 """
- if self.stream and hasattr(self.stream, "flush"):
- self.stream.flush()
+ with self.lock:
+ if self.stream and hasattr(self.stream, "flush"):
+ self.stream.flush()
 
 def emit(self, record):
 """
@@ -900,12 +901,13 @@
 """
 Closes the stream.
 """
- if self.stream:
- self.flush()
- if hasattr(self.stream, "close"):
- self.stream.close()
- StreamHandler.close(self)
- self.stream = None
+ with self.lock:
+ if self.stream:
+ self.flush()
+ if hasattr(self.stream, "close"):
+ self.stream.close()
+ StreamHandler.close(self)
+ self.stream = None
 
 def _open(self):
 """
diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py
--- a/Lib/logging/handlers.py
+++ b/Lib/logging/handlers.py
@@ -1,4 +1,4 @@
-# Copyright 2001-2010 by Vinay Sajip. All Rights Reserved.
+# Copyright 2001-2012 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,
@@ -563,9 +563,10 @@
 """
 Closes the socket.
 """
- if self.sock:
- self.sock.close()
- self.sock = None
+ with self.lock:
+ if self.sock:
+ self.sock.close()
+ self.sock = None
 logging.Handler.close(self)
 
 class DatagramHandler(SocketHandler):
@@ -767,8 +768,9 @@
 """
 Closes the socket.
 """
- if self.unixsocket:
- self.socket.close()
+ with self.lock:
+ if self.unixsocket:
+ self.socket.close()
 logging.Handler.close(self)
 
 def mapPriority(self, levelName):
@@ -1096,7 +1098,8 @@
 
 This version just zaps the buffer to empty.
 """
- self.buffer = []
+ with self.lock:
+ self.buffer = []
 
 def close(self):
 """
@@ -1144,15 +1147,17 @@
 records to the target, if there is one. Override if you want
 different behaviour.
 """
- if self.target:
- for record in self.buffer:
- self.target.handle(record)
- self.buffer = []
+ with self.lock:
+ if self.target:
+ for record in self.buffer:
+ self.target.handle(record)
+ self.buffer = []
 
 def close(self):
 """
 Flush, set the target to None and lose the buffer.
 """
 self.flush()
- self.target = None
- BufferingHandler.close(self)
+ with self.lock:
+ self.target = None
+ BufferingHandler.close(self)
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /