[Python-Dev] RFC: PEP 454: Add a new tracemalloc module (original) (raw)
Victor Stinner victor.stinner at gmail.com
Wed Sep 4 13:20:11 CEST 2013
- Previous message: [Python-Dev] RFC: PEP 454: Add a new tracemalloc module
- Next message: [Python-Dev] RFC: PEP 454: Add a new tracemalloc module
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
trace
class: This class represents debug information of an allocated memory block.
size
attribute: Size in bytes of the memory block.filename
attribute: Name of the Python script where the memory block was allocated,None
if unknown.lineno
attribute: Line number where the memory block was allocated,None
if unknown. I though twice and it would be posible to store more than 1 frame per trace instance, to be able to rebuild a (partial) Python traceback. The hook on the memory allocator has access to the chain of Python frames. The API should be changed to support such enhancement.
Oh, it was much easier than expected to retrieve the traceback (maximum 10 frames) instead of only the current frame.
I modified the trace class to replace filename and lineno with a new frames attribute which is list of frames.
Script example:
import tracemalloc, linecache
def g(): return object()
def f(): return g()
tracemalloc.enable() obj = f() trace = tracemalloc.get_object_trace(obj)
print("Traceback (most recent first):") for frame in trace.frames: print(' File "%s", line %s' % (frame.filename, frame.lineno)) line = linecache.getline(frame.filename, frame.lineno) if line: print(" " + line.strip())
Output of the script:
Traceback (most recent first): File "x.py", line 4 return object() File "x.py", line 7 return g() File "x.py", line 10 obj = f()
I updated the PEP 454 (add a new frame class, update trace class):
frame class
frame
class:
Trace of a Python frame.
filename
attribute (str
):
Python filename, ``None`` if unknown.
lineno
attribute (int
):
Python line number, ``None`` if unknown.
trace class
trace
class:
This class represents debug information of an allocated memory block.
size
attribute (int
):
Size in bytes of the memory block.
frames
attribute (list
):
Traceback where the memory block was allocated as a list of
``frame`` instances (most recent first).
The list can be empty or incomplete if the tracemalloc module was
unable to retrieve the full traceback.
For efficiency, the traceback is truncated to 10 frames.
Victor
- Previous message: [Python-Dev] RFC: PEP 454: Add a new tracemalloc module
- Next message: [Python-Dev] RFC: PEP 454: Add a new tracemalloc module
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]