tracemalloc.py 778 B

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