Skip to content

bpo-32121: Add reverse argument to tracemalloc.Traceback.format#4534

Merged
vstinner merged 10 commits intopython:masterfrom
Jesse-Bakker:add-tracemalloc-traceback-format-reverse
Nov 29, 2017
Merged

bpo-32121: Add reverse argument to tracemalloc.Traceback.format#4534
vstinner merged 10 commits intopython:masterfrom
Jesse-Bakker:add-tracemalloc-traceback-format-reverse

Conversation

@Jesse-Bakker
Copy link
Copy Markdown
Contributor

@Jesse-Bakker Jesse-Bakker commented Nov 24, 2017

reversing the order of the frames in the output
Comment thread Lib/tracemalloc.py Outdated
if limit is not None and limit < 0:
return lines
for frame in self[:limit]:
frame_slice = self[:limit]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, I'm not sure that it's useful to truncate before reversing the frames. I would suggest to truncate after reversing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rationale was that the function is designed to format the frames of the limit last calls. If truncating after reversing, it will format the first limit calls, which I think would be less intuitive and less useful.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, you are right :-)

Copy link
Copy Markdown
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I proposed to change the default parameter to reverse=True: see discussion at https://bugs.python.org/issue32121

Comment thread Lib/tracemalloc.py Outdated
if limit is not None and limit < 0:
return lines
for frame in self[:limit]:
frame_slice = self[:limit]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, you are right :-)

Jesse Bakker added 3 commits November 29, 2017 15:05
Reversed default sorting of tracemalloc.Traceback. Renamed argument.
Adjusted docs. Allowed negative limit, truncating from the other side.
Comment thread Lib/tracemalloc.py
# frames is a tuple of frame tuples: see Frame constructor for the
# format of a frame tuple
self._frames = frames
self._frames = tuple(reversed(frames))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extend the comment to explain that the C module gives frames from the most recent to the oldest, but that ther reverse order is expected in the Python API.

@@ -663,13 +663,14 @@ Traceback
The :attr:`Trace.traceback` attribute is an instance of :class:`Traceback`
instance.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reversing the order of frames is a major backward incompatible change for this API, please mention it:

.. versionchanged:: 3.7
   Frames are now sorted from the oldest to the most recent, instead of most recent to oldest.

Moreover, since it's a backward incompatible change, it must be documented in the "Porting" section of Doc/whatsnew/3.7.rst:

https://docs.python.org/dev/whatsnew/3.7.html#changes-in-the-python-api

@bedevere-bot
Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@Jesse-Bakker
Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again.

@bedevere-bot
Copy link
Copy Markdown

Thanks for making the requested changes!

@vstinner: please review the changes made to this pull request.

Copy link
Copy Markdown
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the warnings code displaying a traceback must be updated for the "most recent frame first" line.

Comment thread Lib/test/test_warnings/__init__.py Outdated
@@ -941,10 +941,10 @@ def func():
{fname}:5: ResourceWarning: unclosed file <...>
f = None
Object allocated at (most recent call first):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, the message stil says "most recent call first". It seems like you have to patch Lib/warnings.py too.

sorting the frames from oldest to most recent. ``Traceback.format()``
now accepts negative *limit*, truncating the result to the ``abs(limit)``
oldest frames. To get the old behaviour, one can use the new
*most_recent_first* argument to ``Traceback.format()``.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind to credit yourself? Add "Patch by Jesse Bakker."

@bedevere-bot
Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be poked with soft cushions!

@Jesse-Bakker
Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again.

@bedevere-bot
Copy link
Copy Markdown

Thanks for making the requested changes!

@vstinner: please review the changes made to this pull request.

Copy link
Copy Markdown
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants