def test_default_exc_handler_coro(self): self.loop._process_events = mock.Mock() self.loop.set_debug(True) asyncio.set_event_loop(self.loop) @asyncio.coroutine def zero_error_coro(): yield From(asyncio.sleep(0.01, loop=self.loop)) 1 / 0 # Test Future.__del__ with mock.patch("trollius.base_events.logger") as log: fut = asyncio.async(zero_error_coro(), loop=self.loop) fut.add_done_callback(lambda *args: self.loop.stop()) self.loop.run_forever() fut = None # Trigger Future.__del__ or futures._TracebackLogger support.gc_collect() if PY34: # Future.__del__ in Python 3.4 logs error with # an actual exception context log.error.assert_called_with( test_utils.MockPattern(".*exception was never retrieved"), exc_info=(ZeroDivisionError, MOCK_ANY, MOCK_ANY), ) else: # futures._TracebackLogger logs only textual traceback log.error.assert_called_with( test_utils.MockPattern(".*exception was never retrieved.*ZeroDiv"), exc_info=False )
def test_default_exc_handler_coro(self): self.loop._process_events = mock.Mock() self.loop.set_debug(True) asyncio.set_event_loop(self.loop) @asyncio.coroutine def zero_error_coro(): yield From(asyncio.sleep(0.01, loop=self.loop)) 1 / 0 # Test Future.__del__ with mock.patch('trollius.base_events.logger') as log: fut = asyncio. async (zero_error_coro(), loop=self.loop) fut.add_done_callback(lambda *args: self.loop.stop()) self.loop.run_forever() fut = None # Trigger Future.__del__ or futures._TracebackLogger support.gc_collect() if PY34: # Future.__del__ in Python 3.4 logs error with # an actual exception context log.error.assert_called_with( test_utils.MockPattern('.*exception was never retrieved'), exc_info=(ZeroDivisionError, MOCK_ANY, MOCK_ANY)) else: # futures._TracebackLogger logs only textual traceback log.error.assert_called_with(test_utils.MockPattern( '.*exception was never retrieved.*ZeroDiv'), exc_info=False)
def test_tb_logger_exception_unretrieved(self, m_log): self.loop.set_debug(True) asyncio.set_event_loop(self.loop) fut = asyncio.Future(loop=self.loop) fut.set_exception(RuntimeError('boom')) del fut test_utils.run_briefly(self.loop) support.gc_collect() self.assertTrue(m_log.error.called)
def test_tb_logger_exception_unretrieved(self, m_log): self.loop.set_debug(True) asyncio.set_event_loop(self.loop) fut = asyncio.Future(loop=self.loop) fut.set_exception(RuntimeError("boom")) del fut test_utils.run_briefly(self.loop) support.gc_collect() self.assertTrue(m_log.error.called)
def test_pipe_handle(self): h, _ = windows_utils.pipe(overlapped=(True, True)) _winapi.CloseHandle(_) p = windows_utils.PipeHandle(h) self.assertEqual(p.fileno(), h) self.assertEqual(p.handle, h) # check garbage collection of p closes handle del p support.gc_collect() try: _winapi.CloseHandle(h) except OSError as e: self.assertEqual(e.winerror, 6) # ERROR_INVALID_HANDLE else: raise RuntimeError("expected ERROR_INVALID_HANDLE")
def test_run_forever_keyboard_interrupt(self): # Python issue #22601: ensure that the temporary task created by # run_forever() consumes the KeyboardInterrupt and so don't log # a warning @asyncio.coroutine def raise_keyboard_interrupt(): raise KeyboardInterrupt self.loop._process_events = mock.Mock() self.loop.call_exception_handler = mock.Mock() try: self.loop.run_until_complete(raise_keyboard_interrupt()) except KeyboardInterrupt: pass self.loop.close() support.gc_collect() self.assertFalse(self.loop.call_exception_handler.called)
def test_pipe_handle(self): h, _ = windows_utils.pipe(overlapped=(True, True)) _winapi.CloseHandle(_) p = windows_utils.PipeHandle(h) self.assertEqual(p.fileno(), h) self.assertEqual(p.handle, h) # check garbage collection of p closes handle with warnings.catch_warnings(): if sys.version_info >= (3, 4): warnings.filterwarnings("ignore", "", ResourceWarning) del p support.gc_collect() try: _winapi.CloseHandle(h) except OSError as e: self.assertEqual(e.winerror, 6) # ERROR_INVALID_HANDLE else: raise RuntimeError('expected ERROR_INVALID_HANDLE')
def check_future_exception_never_retrieved(self, debug, m_log): self.loop.set_debug(debug) def memory_error(): try: raise MemoryError() except BaseException as exc: return exc exc = memory_error() future = asyncio.Future(loop=self.loop) if debug: source_traceback = future._source_traceback future.set_exception(exc) future = None test_utils.run_briefly(self.loop) support.gc_collect() if sys.version_info >= (3, 4): if debug: frame = source_traceback[-1] regex = (r'^Future exception was never retrieved\n' r'future: <Future finished exception=MemoryError\(\) ' r'created at {filename}:{lineno}>\n' r'source_traceback: Object ' r'created at \(most recent call last\):\n' r' File' r'.*\n' r' File "{filename}", line {lineno}, ' r'in check_future_exception_never_retrieved\n' r' future = asyncio\.Future\(loop=self\.loop\)$' ).format(filename=re.escape(frame[0]), lineno=frame[1]) else: regex = (r'^Future exception was never retrieved\n' r'future: ' r'<Future finished exception=MemoryError\(\)>$') exc_info = (type(exc), exc, exc.__traceback__) m_log.error.assert_called_once_with(mock.ANY, exc_info=exc_info) else: if debug: frame = source_traceback[-1] regex = (r'^Future/Task exception was never retrieved\n' r'Future/Task created at \(most recent call last\):\n' r' File' r'.*\n' r' File "{filename}", line {lineno}, ' r'in check_future_exception_never_retrieved\n' r' future = asyncio\.Future\(loop=self\.loop\)\n' r'Traceback \(most recent call last\):\n' r'.*\n' r'MemoryError$').format(filename=re.escape(frame[0]), lineno=frame[1]) elif compat.PY3: regex = (r'^Future/Task exception was never retrieved\n' r'Traceback \(most recent call last\):\n' r'.*\n' r'MemoryError$') else: regex = (r'^Future/Task exception was never retrieved\n' r'MemoryError$') m_log.error.assert_called_once_with(mock.ANY, exc_info=False) message = m_log.error.call_args[0][0] self.assertRegex(message, re.compile(regex, re.DOTALL))
def check_future_exception_never_retrieved(self, debug, m_log): self.loop.set_debug(debug) def memory_error(): try: raise MemoryError() except BaseException as exc: return exc exc = memory_error() future = asyncio.Future(loop=self.loop) if debug: source_traceback = future._source_traceback future.set_exception(exc) future = None test_utils.run_briefly(self.loop) support.gc_collect() if sys.version_info >= (3, 4): if debug: frame = source_traceback[-1] regex = ( r"^Future exception was never retrieved\n" r"future: <Future finished exception=MemoryError\(\) created at {filename}:{lineno}>\n" r"source_traceback: Object created at \(most recent call last\):\n" r" File" r".*\n" r' File "{filename}", line {lineno}, in check_future_exception_never_retrieved\n' r" future = asyncio\.Future\(loop=self\.loop\)$" ).format(filename=re.escape(frame[0]), lineno=frame[1]) else: regex = ( r"^Future exception was never retrieved\n" r"future: <Future finished exception=MemoryError\(\)>$" ) exc_info = (type(exc), exc, exc.__traceback__) m_log.error.assert_called_once_with(mock.ANY, exc_info=exc_info) else: if debug: frame = source_traceback[-1] regex = ( r"^Future/Task exception was never retrieved\n" r"Future/Task created at \(most recent call last\):\n" r" File" r".*\n" r' File "{filename}", line {lineno}, in check_future_exception_never_retrieved\n' r" future = asyncio\.Future\(loop=self\.loop\)\n" r"Traceback \(most recent call last\):\n" r".*\n" r"MemoryError$" ).format(filename=re.escape(frame[0]), lineno=frame[1]) elif compat.PY3: regex = ( r"^Future/Task exception was never retrieved\n" r"Traceback \(most recent call last\):\n" r".*\n" r"MemoryError$" ) else: regex = r"^Future/Task exception was never retrieved\n" r"MemoryError$" m_log.error.assert_called_once_with(mock.ANY, exc_info=False) message = m_log.error.call_args[0][0] self.assertRegex(message, re.compile(regex, re.DOTALL))