| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
|
| 4 |
def tracemalloc_message(source: object) -> str: |
| 5 |
if source is None: |
| 6 |
return "" |
| 7 |
|
| 8 |
try: |
| 9 |
import tracemalloc |
| 10 |
except ImportError: |
| 11 |
return "" |
| 12 |
|
| 13 |
tb = tracemalloc.get_object_traceback(source) |
| 14 |
if tb is not None: |
| 15 |
formatted_tb = "\n".join(tb.format()) |
| 16 |
# Use a leading new line to better separate the (large) output |
| 17 |
# from the traceback to the previous warning text. |
| 18 |
return f"\nObject allocated at:\n{formatted_tb}" |
| 19 |
# No need for a leading new line. |
| 20 |
url = "https://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warnings" |
| 21 |
return ( |
| 22 |
"Enable tracemalloc to get traceback where the object was allocated.\n" |
| 23 |
f"See {url} for more info." |
| 24 |
) |