summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2013-03-30 15:29:54 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2013-03-30 15:29:54 (GMT)
commit9498839a1a29205d65c798a3a6bca272430b83c1 (patch)
treec4d19e95481f2d72fb86584eabf5c1cb925f8a40
parent912fbff1053c7ebadced3233b18fb951e6d15399 (diff)
parent3fdd4f1935ba1bb4b45d77ed99281b134f3e6d24 (diff)
downloadcpython-9498839a1a29205d65c798a3a6bca272430b83c1.zip
cpython-9498839a1a29205d65c798a3a6bca272430b83c1.tar.gz
cpython-9498839a1a29205d65c798a3a6bca272430b83c1.tar.bz2
Merge
-rw-r--r--Doc/howto/logging-cookbook.rst34
-rw-r--r--Doc/library/unittest.mock-examples.rst6
-rw-r--r--Lib/inspect.py2
-rw-r--r--Lib/test/test_inspect.py6
-rw-r--r--Misc/NEWS3
5 files changed, 47 insertions, 4 deletions
diff --git a/Doc/howto/logging-cookbook.rst b/Doc/howto/logging-cookbook.rst
index e113564..efd8c1c 100644
--- a/Doc/howto/logging-cookbook.rst
+++ b/Doc/howto/logging-cookbook.rst
@@ -1094,6 +1094,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
diff --git a/Doc/library/unittest.mock-examples.rst b/Doc/library/unittest.mock-examples.rst
index f32a282..444c208 100644
--- a/Doc/library/unittest.mock-examples.rst
+++ b/Doc/library/unittest.mock-examples.rst
@@ -338,11 +338,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')
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 3661316..e60a235 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 OSError('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 66ffe91..6bd9bd1 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 ['', '<string>']:
+ 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 5c2879a..2af6102 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -297,6 +297,9 @@ Core and Builtins
Library
-------
+- Issue #17526: fix an IndexError raised while passing code without filename to
+ inspect.findsource(). Initial patch by Tyler Doyle.
+
- Issue #17540: Added style to formatter configuration by dict.
- Issue #16692: The ssl module now supports TLS 1.1 and TLS 1.2. Initial