Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged

Conversation

Jesse-Bakker
Copy link
Contributor

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

reversing the order of the frames in the output
lines = []
if limit is not None and limit < 0:
return lines
for frame in self[:limit]:
frame_slice = self[:limit]
Copy link
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
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
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
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

lines = []
if limit is not None and limit < 0:
return lines
for frame in self[:limit]:
frame_slice = self[:limit]
Copy link
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.
"""
__slots__ = ("_frames",)

def __init__(self, frames):
Sequence.__init__(self)
# 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
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
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

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
Contributor Author

I have made the requested changes; please review again.

@bedevere-bot
Copy link

Thanks for making the requested changes!

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

Copy link
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.

@@ -941,10 +941,10 @@ def func():
{fname}:5: ResourceWarning: unclosed file <...>
f = None
Object allocated at (most recent call first):
Copy link
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
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

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
Contributor Author

I have made the requested changes; please review again.

@bedevere-bot
Copy link

Thanks for making the requested changes!

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

Copy link
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