]> granicus.if.org Git - python/commitdiff
Merge 3.4 (Issue #24867)
authorYury Selivanov <yselivanov@sprymix.com>
Fri, 14 Aug 2015 19:32:37 +0000 (15:32 -0400)
committerYury Selivanov <yselivanov@sprymix.com>
Fri, 14 Aug 2015 19:32:37 +0000 (15:32 -0400)
1  2 
Lib/test/test_asyncio/test_pep492.py
Misc/NEWS

index b702efcdfda2d38f3533106d05b9193b4656012e,0000000000000000000000000000000000000000..41e1b8a9e0cc97c6d26710a92c90b23fb879aee0
mode 100644,000000..100644
--- /dev/null
@@@ -1,191 -1,0 +1,208 @@@
 +"""Tests support for new syntax introduced by PEP 492."""
 +
 +import collections.abc
 +import types
 +import unittest
 +
 +from test import support
 +from unittest import mock
 +
 +import asyncio
 +from asyncio import test_utils
 +
 +
 +class BaseTest(test_utils.TestCase):
 +
 +    def setUp(self):
 +        self.loop = asyncio.BaseEventLoop()
 +        self.loop._process_events = mock.Mock()
 +        self.loop._selector = mock.Mock()
 +        self.loop._selector.select.return_value = ()
 +        self.set_event_loop(self.loop)
 +
 +
 +class LockTests(BaseTest):
 +
 +    def test_context_manager_async_with(self):
 +        primitives = [
 +            asyncio.Lock(loop=self.loop),
 +            asyncio.Condition(loop=self.loop),
 +            asyncio.Semaphore(loop=self.loop),
 +            asyncio.BoundedSemaphore(loop=self.loop),
 +        ]
 +
 +        async def test(lock):
 +            await asyncio.sleep(0.01, loop=self.loop)
 +            self.assertFalse(lock.locked())
 +            async with lock as _lock:
 +                self.assertIs(_lock, None)
 +                self.assertTrue(lock.locked())
 +                await asyncio.sleep(0.01, loop=self.loop)
 +                self.assertTrue(lock.locked())
 +            self.assertFalse(lock.locked())
 +
 +        for primitive in primitives:
 +            self.loop.run_until_complete(test(primitive))
 +            self.assertFalse(primitive.locked())
 +
 +    def test_context_manager_with_await(self):
 +        primitives = [
 +            asyncio.Lock(loop=self.loop),
 +            asyncio.Condition(loop=self.loop),
 +            asyncio.Semaphore(loop=self.loop),
 +            asyncio.BoundedSemaphore(loop=self.loop),
 +        ]
 +
 +        async def test(lock):
 +            await asyncio.sleep(0.01, loop=self.loop)
 +            self.assertFalse(lock.locked())
 +            with await lock as _lock:
 +                self.assertIs(_lock, None)
 +                self.assertTrue(lock.locked())
 +                await asyncio.sleep(0.01, loop=self.loop)
 +                self.assertTrue(lock.locked())
 +            self.assertFalse(lock.locked())
 +
 +        for primitive in primitives:
 +            self.loop.run_until_complete(test(primitive))
 +            self.assertFalse(primitive.locked())
 +
 +
 +class StreamReaderTests(BaseTest):
 +
 +    def test_readline(self):
 +        DATA = b'line1\nline2\nline3'
 +
 +        stream = asyncio.StreamReader(loop=self.loop)
 +        stream.feed_data(DATA)
 +        stream.feed_eof()
 +
 +        async def reader():
 +            data = []
 +            async for line in stream:
 +                data.append(line)
 +            return data
 +
 +        data = self.loop.run_until_complete(reader())
 +        self.assertEqual(data, [b'line1\n', b'line2\n', b'line3'])
 +
 +
 +class CoroutineTests(BaseTest):
 +
 +    def test_iscoroutine(self):
 +        async def foo(): pass
 +
 +        f = foo()
 +        try:
 +            self.assertTrue(asyncio.iscoroutine(f))
 +        finally:
 +            f.close() # silence warning
 +
 +        # Test that asyncio.iscoroutine() uses collections.abc.Coroutine
 +        class FakeCoro:
 +            def send(self, value): pass
 +            def throw(self, typ, val=None, tb=None): pass
 +            def close(self): pass
 +            def __await__(self): yield
 +
 +        self.assertTrue(asyncio.iscoroutine(FakeCoro()))
 +
 +    def test_iscoroutinefunction(self):
 +        async def foo(): pass
 +        self.assertTrue(asyncio.iscoroutinefunction(foo))
 +
 +    def test_function_returning_awaitable(self):
 +        class Awaitable:
 +            def __await__(self):
 +                return ('spam',)
 +
 +        @asyncio.coroutine
 +        def func():
 +            return Awaitable()
 +
 +        coro = func()
 +        self.assertEqual(coro.send(None), 'spam')
 +        coro.close()
 +
 +    def test_async_def_coroutines(self):
 +        async def bar():
 +            return 'spam'
 +        async def foo():
 +            return await bar()
 +
 +        # production mode
 +        data = self.loop.run_until_complete(foo())
 +        self.assertEqual(data, 'spam')
 +
 +        # debug mode
 +        self.loop.set_debug(True)
 +        data = self.loop.run_until_complete(foo())
 +        self.assertEqual(data, 'spam')
 +
 +    @mock.patch('asyncio.coroutines.logger')
 +    def test_async_def_wrapped(self, m_log):
 +        async def foo():
 +            pass
 +        async def start():
 +            foo_coro = foo()
 +            self.assertRegex(
 +                repr(foo_coro),
 +                r'<CoroWrapper .*\.foo\(\) running at .*pep492.*>')
 +
 +            with support.check_warnings((r'.*foo.*was never',
 +                                         RuntimeWarning)):
 +                foo_coro = None
 +                support.gc_collect()
 +                self.assertTrue(m_log.error.called)
 +                message = m_log.error.call_args[0][0]
 +                self.assertRegex(message,
 +                                 r'CoroWrapper.*foo.*was never')
 +
 +        self.loop.set_debug(True)
 +        self.loop.run_until_complete(start())
 +
 +        async def start():
 +            foo_coro = foo()
 +            task = asyncio.ensure_future(foo_coro, loop=self.loop)
 +            self.assertRegex(repr(task), r'Task.*foo.*running')
 +
 +        self.loop.run_until_complete(start())
 +
 +
 +    def test_types_coroutine(self):
 +        def gen():
 +            yield from ()
 +            return 'spam'
 +
 +        @types.coroutine
 +        def func():
 +            return gen()
 +
 +        async def coro():
 +            wrapper = func()
 +            self.assertIsInstance(wrapper, types._GeneratorWrapper)
 +            return await wrapper
 +
 +        data = self.loop.run_until_complete(coro())
 +        self.assertEqual(data, 'spam')
 +
++    def test_task_print_stack(self):
++        T = None
++
++        async def foo():
++            f = T.get_stack(limit=1)
++            try:
++                self.assertEqual(f[0].f_code.co_name, 'foo')
++            finally:
++                f = None
++
++        async def runner():
++            nonlocal T
++            T = asyncio.ensure_future(foo(), loop=self.loop)
++            await T
++
++        self.loop.run_until_complete(runner())
++
 +
 +if __name__ == '__main__':
 +    unittest.main()
diff --cc Misc/NEWS
index 7488834eda301f5ef3e560b2ea8640414d24560b,9c4ea332cf8605ded94ae6bf52b2b2805a8d1cf6..0500cd2ab1c53fdcaca3a6d38dcbc3cb7afd9b68
+++ b/Misc/NEWS
@@@ -13,50 -13,72 +13,52 @@@ Core and Builtin
  - Issue #21167: NAN operations are now handled correctly when python is
    compiled with ICC even if -fp-model strict is not specified.
  
 -- Issue #4395: Better testing and documentation of binary operators.
 -  Patch by Martin Panter.
 -
 -- Issue #24467: Fixed possible buffer over-read in bytearray. The bytearray
 -  object now always allocates place for trailing null byte and it's buffer now
 -  is always null-terminated.
 -
 -- Issue #24115: Update uses of PyObject_IsTrue(), PyObject_Not(),
 -  PyObject_IsInstance(), PyObject_RichCompareBool() and _PyDict_Contains()
 -  to check for and handle errors correctly.
 -
 -- Issue #24257: Fixed system error in the comparison of faked
 -  types.SimpleNamespace.
 -
 -- Issue #22939: Fixed integer overflow in iterator object.  Patch by
 -  Clement Rouault.
 -
 -- Issue #23985: Fix a possible buffer overrun when deleting a slice from
 -  the front of a bytearray and then appending some other bytes data.
 +Library
 +-------
  
 -- Issue #24102: Fixed exception type checking in standard error handlers.
 +- Issue #21159: Improve message in configparser.InterpolationMissingOptionError.
 +  Patch from Łukasz Langa.
  
 -- Issue #23757: PySequence_Tuple() incorrectly called the concrete list API
 -  when the data was a list subclass.
 +- Issue #24847: Fixes tcltk installer layout of VC runtime DLL
  
 -- Issue #24407: Fix crash when dict is mutated while being updated.
 +- Issue #24839: platform._syscmd_ver raises DeprecationWarning
  
 -- Issue #24096: Make warnings.warn_explicit more robust against mutation of the
 -  warnings.filters list.
++- Issue #24867: Fix Task.get_stack() for 'async def' coroutines
 -- Issue #23996: Avoid a crash when a delegated generator raises an
 -  unnormalized StopIteration exception.  Patch by Stefan Behnel.
 +Documentation
 +-------------
  
 -- Issue #24022: Fix tokenizer crash when processing undecodable source code.
 +- Issue #23725: Overhaul tempfile docs. Note deprecated status of mktemp.
 +  Patch from Zbigniew Jędrzejewski-Szmek.
  
 -- Issue #23309: Avoid a deadlock at shutdown if a daemon thread is aborted
 -  while it is holding a lock to a buffered I/O object, and the main thread
 -  tries to use the same I/O object (typically stdout or stderr).  A fatal
 -  error is emitted instead.
 +What's New in Python 3.5.0 release candidate 1?
 +===============================================
  
 -- Issue #22977: Fixed formatting Windows error messages on Wine.
 -  Patch by Martin Panter.
 +Release date: 2015-08-09
  
 -- Issue #23803: Fixed str.partition() and str.rpartition() when a separator
 -  is wider then partitioned string.
 +Core and Builtins
 +-----------------
  
 -- Issue #23192: Fixed generator lambdas.  Patch by Bruno Cauet.
 +- Issue #24492: A "package" lacking a __name__ attribute when trying to perform
 +  a ``from .. import ...`` statement will trigger an ImportError instead of an
 +  AttributeError.
  
 -- Issue #23629: Fix the default __sizeof__ implementation for variable-sized
 -  objects.
 +- Issue #24667: Resize odict in all cases that the underlying dict resizes.
  
 -- Issue #24044: Fix possible null pointer dereference in list.sort in out of
 -  memory conditions.
 +Library
 +-------
  
 -- Issue #21354: PyCFunction_New function is exposed by python DLL again.
 +- Issue #24824: Signatures of codecs.encode() and codecs.decode() now are
 +  compatible with pydoc.
  
 -- Issue #23812: Fix asyncio.Queue.get() to avoid loosing items on cancellation.
 -  Patch by Gustavo J. A. M. Carneiro.
 +- Issue #24634: Importing uuid should not try to load libc on Windows
  
 -Library
 --------
 +- Issue #24798: _msvccompiler.py doesn't properly support manifests
  
 -- Issue #21159: Improve message in configparser.InterpolationMissingOptionError.
 -  Patch from Łukasz Langa.
 +- Issue #4395: Better testing and documentation of binary operators.
 +  Patch by Martin Panter.
  
 -- Issue #23888: Handle fractional time in cookie expiry. Patch by ssh.
 +- Issue #23973: Update typing.py from GitHub repo.
  
  - Issue #23004: mock_open() now reads binary data correctly when the type of
    read_data is bytes.  Initial patch by Aaron Hill.