From 1b145927d725b136b987df220dff0ec7529b6f29 Mon Sep 17 00:00:00 2001 From: Ezio Melotti Date: Sat, 30 Mar 2013 05:17:24 +0200 Subject: [PATCH 1/3] #17526: fix an IndexError raised while passing code without filename to inspect.findsource(). Initial patch by Tyler Doyle. --- Lib/inspect.py | 2 +- Lib/test/test_inspect.py | 6 ++++++ Misc/NEWS | 11 +++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 7a7bb91b1be..7834d12ea7d 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -550,7 +550,7 @@ def findsource(object): file = getfile(object) sourcefile = getsourcefile(object) - if not sourcefile and file[0] + file[-1] != '<>': + if not sourcefile and file[:1] + file[-1:] != '<>': raise IOError('source code not available') file = sourcefile if sourcefile else file diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 6e3f04e68a0..9f5e93b0c7f 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -416,6 +416,12 @@ class TestBuggyCases(GetSourceBase): finally: del linecache.cache[co.co_filename] + def test_findsource_without_filename(self): + for fname in ['', '']: + co = compile('x=1', fname, "exec") + self.assertRaises(IOError, inspect.findsource, co) + self.assertRaises(IOError, inspect.getsource, co) + class TestNoEOL(GetSourceBase): def __init__(self, *args, **kwargs): self.tempdir = TESTFN + '_dir' diff --git a/Misc/NEWS b/Misc/NEWS index 8c016a4c80d..c3e9ef2fa74 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -5,8 +5,19 @@ Python News What's New in Python 3.3.2? =========================== +*Release date: XXXX-XX-XX* + *Not yet released, see sections below for changes released in 3.3.0* +Core and Builtins +----------------- + +Library +------- + +- Issue #17526: fix an IndexError raised while passing code without filename to + inspect.findsource(). Initial patch by Tyler Doyle. + What's New in Python 3.3.1? =========================== From b40a2203adff6511afc65fe885816636e135c935 Mon Sep 17 00:00:00 2001 From: Ezio Melotti Date: Sat, 30 Mar 2013 05:55:52 +0200 Subject: [PATCH 2/3] #17539: fix MagicMock example. Patch by Berker Peksag. --- Doc/library/unittest.mock-examples.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/library/unittest.mock-examples.rst b/Doc/library/unittest.mock-examples.rst index 0d136eb4484..d7d697d6509 100644 --- a/Doc/library/unittest.mock-examples.rst +++ b/Doc/library/unittest.mock-examples.rst @@ -324,11 +324,11 @@ with. ... >>> test() -If you are patching a module (including `__builtin__`) then use `patch` +If you are patching a module (including :mod:`builtins`) then use `patch` instead of `patch.object`: - >>> mock = MagicMock(return_value = sentinel.file_handle) - >>> with patch('__builtin__.open', mock): + >>> mock = MagicMock(return_value=sentinel.file_handle) + >>> with patch('builtins.open', mock): ... handle = open('filename', 'r') ... >>> mock.assert_called_with('filename', 'r') From 8028a5cf15753c283b7308a32ecc4651db33c6c9 Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Sat, 30 Mar 2013 11:56:18 +0000 Subject: [PATCH 3/3] Updated logging cookbook with additional example for output using str.format(). --- Doc/howto/logging-cookbook.rst | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Doc/howto/logging-cookbook.rst b/Doc/howto/logging-cookbook.rst index 8d58c8486a7..47406aa9eb7 100644 --- a/Doc/howto/logging-cookbook.rst +++ b/Doc/howto/logging-cookbook.rst @@ -1096,6 +1096,40 @@ parentheses go around the format string and the arguments, not just the format string. That's because the __ notation is just syntax sugar for a constructor call to one of the XXXMessage classes. +If you prefer, you can use a :class:`LoggerAdapter` to achieve a similar effect +to the above, as in the following example:: + + import logging + + class Message(object): + def __init__(self, fmt, args): + self.fmt = fmt + self.args = args + + def __str__(self): + return self.fmt.format(*self.args) + + class StyleAdapter(logging.LoggerAdapter): + def __init__(self, logger, extra=None): + super(StyleAdapter, self).__init__(logger, extra or {}) + + def log(self, level, msg, *args, **kwargs): + if self.isEnabledFor(level): + msg, kwargs = self.process(msg, kwargs) + self.logger._log(level, Message(msg, args), (), **kwargs) + + logger = StyleAdapter(logging.getLogger(__name__)) + + def main(): + logger.debug('Hello, {}', 'world!') + + if __name__ == '__main__': + logging.basicConfig(level=logging.DEBUG) + main() + +The above script should log the message ``Hello, world!`` when run with +Python 3.2 or later. + .. currentmodule:: logging