| 835 | |
| 836 | # Private utility function called by _PyErr_WarnUnawaitedCoroutine |
| 837 | def _warn_unawaited_coroutine(coro): |
| 838 | msg_lines = [ |
| 839 | f"coroutine '{coro.__qualname__}' was never awaited\n" |
| 840 | ] |
| 841 | if coro.cr_origin is not None: |
| 842 | import linecache, traceback |
| 843 | def extract(): |
| 844 | for filename, lineno, funcname in reversed(coro.cr_origin): |
| 845 | line = linecache.getline(filename, lineno) |
| 846 | yield (filename, lineno, funcname, line) |
| 847 | msg_lines.append("Coroutine created at (most recent call last)\n") |
| 848 | msg_lines += traceback.format_list(list(extract())) |
| 849 | msg = "".join(msg_lines).rstrip("\n") |
| 850 | # Passing source= here means that if the user happens to have tracemalloc |
| 851 | # enabled and tracking where the coroutine was created, the warning will |
| 852 | # contain that traceback. This does mean that if they have *both* |
| 853 | # coroutine origin tracking *and* tracemalloc enabled, they'll get two |
| 854 | # partially-redundant tracebacks. If we wanted to be clever we could |
| 855 | # probably detect this case and avoid it, but for now we don't bother. |
| 856 | _wm.warn( |
| 857 | msg, category=RuntimeWarning, stacklevel=2, source=coro |
| 858 | ) |
| 859 | |
| 860 | |
| 861 | def _setup_defaults(): |