diff options
author | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2010-09-10 08:25:13 (GMT) |
---|---|---|
committer | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2010-09-10 08:25:13 (GMT) |
commit | 2314fc729b7192a041ae7630febdc376519b8b9a (patch) | |
tree | 89ad57716fcc027ee32b6f955fd1b1b10a35b5f8 | |
parent | c7937791a177a340b153dacd3140ddf8d838e170 (diff) | |
download | cpython-2314fc729b7192a041ae7630febdc376519b8b9a.zip cpython-2314fc729b7192a041ae7630febdc376519b8b9a.tar.gz cpython-2314fc729b7192a041ae7630febdc376519b8b9a.tar.bz2 |
logging: Added threading interlock in basicConfig().
-rw-r--r-- | Lib/logging/__init__.py | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index ddbe8ab..0e01356 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -1459,22 +1459,28 @@ def basicConfig(**kwargs): using sys.stdout or sys.stderr), whereas FileHandler closes its stream when the handler is closed. """ - if len(root.handlers) == 0: - filename = kwargs.get("filename") - if filename: - mode = kwargs.get("filemode", 'a') - hdlr = FileHandler(filename, mode) - else: - stream = kwargs.get("stream") - hdlr = StreamHandler(stream) - fs = kwargs.get("format", BASIC_FORMAT) - dfs = kwargs.get("datefmt", None) - fmt = Formatter(fs, dfs) - hdlr.setFormatter(fmt) - root.addHandler(hdlr) - level = kwargs.get("level") - if level is not None: - root.setLevel(level) + # Add thread safety in case someone mistakenly calls + # basicConfig() from multiple threads + _acquireLock() + try: + if len(root.handlers) == 0: + filename = kwargs.get("filename") + if filename: + mode = kwargs.get("filemode", 'a') + hdlr = FileHandler(filename, mode) + else: + stream = kwargs.get("stream") + hdlr = StreamHandler(stream) + fs = kwargs.get("format", BASIC_FORMAT) + dfs = kwargs.get("datefmt", None) + fmt = Formatter(fs, dfs) + hdlr.setFormatter(fmt) + root.addHandler(hdlr) + level = kwargs.get("level") + if level is not None: + root.setLevel(level) + finally: + _releaseLock() #--------------------------------------------------------------------------- # Utility functions at module level. |