| 891 | |
| 892 | # Private utility function called by _PyErr_WarnUnawaitedCoroutine |
| 893 | def _warn_unawaited_coroutine(coro): |
| 894 | msg_lines = [ |
| 895 | f"coroutine '{coro.__qualname__}' was never awaited\n" |
| 896 | ] |
| 897 | if coro.cr_origin is not None: |
| 898 | import linecache, traceback |
| 899 | def extract(): |
| 900 | for filename, lineno, funcname in reversed(coro.cr_origin): |
| 901 | line = linecache.getline(filename, lineno) |
| 902 | yield (filename, lineno, funcname, line) |
| 903 | msg_lines.append("Coroutine created at (most recent call last)\n") |
| 904 | msg_lines += traceback.format_list(list(extract())) |
| 905 | msg = "".join(msg_lines).rstrip("\n") |
| 906 | # Passing source= here means that if the user happens to have tracemalloc |
| 907 | # enabled and tracking where the coroutine was created, the warning will |
| 908 | # contain that traceback. This does mean that if they have *both* |
| 909 | # coroutine origin tracking *and* tracemalloc enabled, they'll get two |
| 910 | # partially-redundant tracebacks. If we wanted to be clever we could |
| 911 | # probably detect this case and avoid it, but for now we don't bother. |
| 912 | _wm.warn( |
| 913 | msg, category=RuntimeWarning, stacklevel=2, source=coro |
| 914 | ) |
| 915 | |
| 916 | |
| 917 | def _setup_defaults(): |