Skip to content

Commit 8700273

Browse files
authored
allow async await debug, fix #3 (#4)
1 parent fa8a8ce commit 8700273

5 files changed

Lines changed: 37 additions & 10 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea/
2+
env/
23
env35/
34
env36/
45
*.py[cod]

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
.DEFAULT_GOAL := all
22

3-
.PHONY: install
3+
.PHONY: install-dev
44
install-dev:
55
pip install -U setuptools pip
66
pip install -r dev-requirements.txt
7-
pip install -U .
7+
pip install -U -e .[pygments]
88

9-
.PHONY: install
9+
.PHONY: install-test
1010
install-test:
1111
pip install -U setuptools pip
1212
pip install -r tests/requirements.txt

dev-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#-r docs/requirements.txt
22
-r tests/requirements.txt
33

4+
astpretty # pyup: ignore
45
ipython # pyup: ignore

devtools/debug.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import re
66
import warnings
77
from pathlib import Path
8-
from textwrap import dedent
8+
from textwrap import dedent, indent
99
from typing import Generator, List, Optional, Tuple, Type
1010

1111
from .ansi import isatty, sformat
@@ -222,7 +222,7 @@ def _parse_code(self, call_frame, func_regex, filename) -> Tuple[Optional[ast.AS
222222
func_ast = None
223223
tail_index = call_frame.index
224224
try:
225-
func_ast = ast.parse(code, filename=filename).body[0].value
225+
func_ast = self._wrap_parse(code, filename)
226226
except SyntaxError as e1:
227227
# if the trailing bracket(s) of the function is/are on a new line eg.
228228
# debug(
@@ -233,7 +233,7 @@ def _parse_code(self, call_frame, func_regex, filename) -> Tuple[Optional[ast.AS
233233
extra_lines = call_frame.code_context[tail_index + 1:tail_index + extra]
234234
code = dedent(''.join(call_lines + extra_lines))
235235
try:
236-
func_ast = ast.parse(code).body[0].value
236+
func_ast = self._wrap_parse(code, filename)
237237
except SyntaxError:
238238
pass
239239
else:
@@ -253,17 +253,25 @@ def _parse_code(self, call_frame, func_regex, filename) -> Tuple[Optional[ast.AS
253253
code_lines[-1] = code_lines[-1][:-1]
254254
return func_ast, code_lines, lineno
255255

256-
@classmethod
257-
def _get_offsets(cls, func_ast):
256+
@staticmethod
257+
def _wrap_parse(code, filename):
258+
"""
259+
async wrapper is required to avoid await calls raising a SyntaxError
260+
"""
261+
code = 'async def wrapper():\n' + indent(code, ' ')
262+
return ast.parse(code, filename=filename).body[0].body[0].value
263+
264+
@staticmethod
265+
def _get_offsets(func_ast):
258266
for arg in func_ast.args:
259-
start_line, start_col = arg.lineno - 1, arg.col_offset
267+
start_line, start_col = arg.lineno - 2, arg.col_offset - 1
260268

261269
# horrible hack for http://bugs.python.org/issue31241
262270
if isinstance(arg, (ast.ListComp, ast.GeneratorExp)):
263271
start_col -= 1
264272
yield start_line, start_col
265273
for kw in func_ast.keywords:
266-
yield kw.value.lineno - 1, kw.value.col_offset - len(kw.arg) - 1
274+
yield kw.value.lineno - 2, kw.value.col_offset - len(kw.arg) - 2
267275

268276
def _warn(self, msg, category: Type[Warning]=RuntimeWarning):
269277
if self._show_warnings:

tests/test_expr_render.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import re
23
import sys
34

@@ -199,3 +200,19 @@ def test_no_syntax_warning():
199200
)
200201
assert 'test_no_syntax_warning\n 1 (int)' in str(v)
201202
assert len(warning_checker) == 0
203+
204+
205+
def test_await():
206+
async def foo():
207+
return 1
208+
209+
async def bar():
210+
return debug.format(await foo())
211+
212+
loop = asyncio.get_event_loop()
213+
v = loop.run_until_complete(bar())
214+
s = re.sub(':\d{2,}', ':<line no>', str(v))
215+
assert (
216+
'tests/test_expr_render.py:<line no> bar\n'
217+
' 1 (int)'
218+
) == s

0 commit comments

Comments
 (0)