Re: [Python-Dev] RFC: PEP 454: Add a new tracemalloc module

2013-09-04 Thread Victor Stinner
>> ``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
___
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] RFC: PEP 454: Add a new tracemalloc module

2013-09-04 Thread Alexander Belopolsky
On Tue, Sep 3, 2013 at 7:27 PM, Victor Stinner wrote:

> API
> ===
>
> To trace the most Python memory allocations, the module should be
> enabled as early as possible in your application by calling
> ``tracemalloc.enable()`` function, by setting the ``PYTHONTRACEMALLOC``
> environment variable to ``1``, or  by using ``-X tracemalloc`` command
> line option.
>
>
> Functions
> -
>
> ``enable()`` function:
>
> Start tracing Python memory allocations.
>
> ``disable()`` function:
>
> Stop tracing Python memory allocations and stop the timer started by
> ``start_timer()``.
>
> ``is_enabled()`` function:
>
> Get the status of the module: ``True`` if it is enabled, ``False``
> otherwise.
>

Please mention that this API is similar to that of faulthandler and add a
link to faulthandler docs.
___
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com