Movatterモバイル変換
[0]ホーム
[Python-Dev] [Python-checkins] cpython (3.2): logging: Added locking in flush() and close() handler methods. Thanks to Fayaz
Gregory P. Smithgreg at krypto.org
Fri Feb 24 02:52:46 CET 2012
On Thu, Feb 23, 2012 at 5:51 PM, Gregory P. Smith <greg at krypto.org> wrote:> On Thu, Feb 23, 2012 at 12:04 PM, vinay.sajip> <python-checkins at python.org> wrote:>>http://hg.python.org/cpython/rev/b2adcd90e656>> changeset: 75211:b2adcd90e656>> branch: 3.2>> parent: 75200:85d08a1ba74e>> user: Vinay Sajip <vinay_sajip at yahoo.co.uk>>> date: Thu Feb 23 19:45:52 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 | 24 +++++++++------->> Lib/logging/handlers.py | 40 +++++++++++++++------------->> 2 files changed, 35 insertions(+), 29 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,>> @@ -16,9 +16,9 @@>>>> """>> Logging package for Python. Based on PEP 282 and comments thereto in>> -comp.lang.python, and influenced by Apache's log4j system.>> +comp.lang.python.>>>> -Copyright (C) 2001-2011 Vinay Sajip. All Rights Reserved.>> +Copyright (C) 2001-2012 Vinay Sajip. All Rights Reserved.>>>> To use, simply 'import logging' and log away!>> """>> @@ -917,8 +917,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()>> I don't know if anyone actually builds Python without thread support> anymore, but if so, self.lock will be set to None and these "with> self.lock"s will fail.>> Perhaps change lock = None to self.lock = some dummy duck typed lock> that supports use as a context manager and acquire/release calls?whoops. once again there I go reading the commit log in order. Lookslike you already fixed this inhttp://hg.python.org/cpython/rev/2ab3a97d544c. thanks! :)>>>>> def emit(self, record):>> """>> @@ -969,12 +970,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,>> @@ -16,10 +16,9 @@>>>> """>> Additional handlers for the logging package for Python. The core package is>> -based on PEP 282 and comments thereto in comp.lang.python, and influenced by>> -Apache's log4j system.>> +based on PEP 282 and comments thereto in comp.lang.python.>>>> -Copyright (C) 2001-2010 Vinay Sajip. All Rights Reserved.>> +Copyright (C) 2001-2012 Vinay Sajip. All Rights Reserved.>>>> To use, simply 'import logging.handlers' and log away!>> """>> @@ -554,10 +553,11 @@>> """>> Closes the socket.>> """>> - if self.sock:>> - self.sock.close()>> - self.sock = None>> - logging.Handler.close(self)>> + with self.lock:>> + if self.sock:>> + self.sock.close()>> + self.sock = None>> + logging.Handler.close(self)>>>> class DatagramHandler(SocketHandler):>> """>> @@ -752,9 +752,10 @@>> """>> Closes the socket.>> """>> - if self.unixsocket:>> - self.socket.close()>> - logging.Handler.close(self)>> + with self.lock:>> + if self.unixsocket:>> + self.socket.close()>> + logging.Handler.close(self)>>>> def mapPriority(self, levelName):>> """>> @@ -1095,7 +1096,8 @@>>>> This version just zaps the buffer to empty.>> """>> - self.buffer = []>> + with self.lock:>> + self.buffer = []>>>> def close(self):>> """>> @@ -1145,18 +1147,20 @@>>>> The record buffer is also cleared by this operation.>> """>> - 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)>>>>>> class QueueHandler(logging.Handler):>>>> -->> Repository URL:http://hg.python.org/cpython>>>> _______________________________________________>> Python-checkins mailing list>>Python-checkins at python.org>>http://mail.python.org/mailman/listinfo/python-checkins>>
More information about the Python-Devmailing list
[8]ページ先頭