Skip to content

Commit 8228f01

Browse files
committed
Issue python#136: Add get/set_debug() methods to BaseEventLoopTests. Add also a
PYTHONASYNCIODEBUG environment variable to debug coroutines since Python startup, to be able to debug coroutines defined directly in the asyncio module.
1 parent 9b4bbf7 commit 8228f01

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

asyncio/base_events.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def __init__(self):
123123
self._running = False
124124
self._clock_resolution = time.get_clock_info('monotonic').resolution
125125
self._exception_handler = None
126+
self._debug = False
126127

127128
def _make_socket_transport(self, sock, protocol, waiter=None, *,
128129
extra=None, server=None):
@@ -795,3 +796,9 @@ def _run_once(self):
795796
if not handle._cancelled:
796797
handle._run()
797798
handle = None # Needed to break cycles when an exception occurs.
799+
800+
def get_debug(self):
801+
return self._debug
802+
803+
def set_debug(self, enabled):
804+
self._debug = enabled

asyncio/events.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,14 @@ def default_exception_handler(self, context):
345345
def call_exception_handler(self, context):
346346
raise NotImplementedError
347347

348+
# Debug flag management.
349+
350+
def get_debug(self):
351+
raise NotImplementedError
352+
353+
def set_debug(self, enabled):
354+
raise NotImplementedError
355+
348356

349357
class AbstractEventLoopPolicy:
350358
"""Abstract policy for accessing the event loop."""

asyncio/tasks.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import functools
1313
import inspect
1414
import linecache
15+
import os
16+
import sys
1517
import traceback
1618
import weakref
1719

@@ -28,7 +30,8 @@
2830
# before you define your coroutines. A downside of using this feature
2931
# is that tracebacks show entries for the CoroWrapper.__next__ method
3032
# when _DEBUG is true.
31-
_DEBUG = False
33+
_DEBUG = (not sys.flags.ignore_environment
34+
and bool(os.environ.get('PYTHONASYNCIODEBUG')))
3235

3336

3437
class CoroWrapper:

tests/test_base_events.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ def test__run_once(self):
197197
self.assertEqual([h2], self.loop._scheduled)
198198
self.assertTrue(self.loop._process_events.called)
199199

200+
def test_set_debug(self):
201+
self.loop.set_debug(True)
202+
self.assertTrue(self.loop.get_debug())
203+
self.loop.set_debug(False)
204+
self.assertFalse(self.loop.get_debug())
205+
200206
@unittest.mock.patch('asyncio.base_events.time')
201207
@unittest.mock.patch('asyncio.base_events.logger')
202208
def test__run_once_logging(self, m_logger, m_time):

tests/test_tasks.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Tests for tasks.py."""
22

33
import gc
4+
import os.path
45
import unittest
6+
from test.script_helper import assert_python_ok
57

68
import asyncio
79
from asyncio import test_utils
@@ -1461,6 +1463,32 @@ def test_return_exceptions(self):
14611463
cb.assert_called_once_with(fut)
14621464
self.assertEqual(fut.result(), [3, 1, exc, exc2])
14631465

1466+
def test_env_var_debug(self):
1467+
path = os.path.dirname(asyncio.__file__)
1468+
path = os.path.normpath(os.path.join(path, '..'))
1469+
code = '\n'.join((
1470+
'import sys',
1471+
'sys.path.insert(0, %r)' % path,
1472+
'import asyncio.tasks',
1473+
'print(asyncio.tasks._DEBUG)'))
1474+
1475+
# Test with -E to not fail if the unit test was run with
1476+
# PYTHONASYNCIODEBUG set to a non-empty string
1477+
sts, stdout, stderr = assert_python_ok('-E', '-c', code)
1478+
self.assertEqual(stdout.rstrip(), b'False')
1479+
1480+
sts, stdout, stderr = assert_python_ok('-c', code,
1481+
PYTHONASYNCIODEBUG='')
1482+
self.assertEqual(stdout.rstrip(), b'False')
1483+
1484+
sts, stdout, stderr = assert_python_ok('-c', code,
1485+
PYTHONASYNCIODEBUG='1')
1486+
self.assertEqual(stdout.rstrip(), b'True')
1487+
1488+
sts, stdout, stderr = assert_python_ok('-E', '-c', code,
1489+
PYTHONASYNCIODEBUG='1')
1490+
self.assertEqual(stdout.rstrip(), b'False')
1491+
14641492

14651493
class FutureGatherTests(GatherTestsBase, unittest.TestCase):
14661494

0 commit comments

Comments
 (0)