Skip to content

bpo-43656: Introduce format_locals in traceback #29299

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

Closed
wants to merge 7 commits into from

Conversation

moi90
Copy link
Contributor

@moi90 moi90 commented Oct 28, 2021

This allows customization of the string representation of the locals of stack frames.

Also, remind users that repr shouldn't raise.

https://bugs.python.org/issue43656

This allows customization of the string representation of the locals of stack frames.

Also, remind users that __repr__ shouldn't raise.
Comment on lines +1600 to +1625
def f():
1/0

def g():
try:
f()
except:
return sys.exc_info()
def g():
try:
f()
except:
return sys.exc_info()

exc_info = g()
exc_info = g()

class Skip_G(traceback.StackSummary):
def format_frame_summary(self, frame_summary):
if frame_summary.name == 'g':
return None
return super().format_frame_summary(frame_summary)
class Skip_G(traceback.StackSummary):
def format_frame_summary(self, frame_summary):
if frame_summary.name == 'g':
return None
return super().format_frame_summary(frame_summary)

stack = Skip_G.extract(
traceback.walk_tb(exc_info[2])).format()
stack = Skip_G.extract(
traceback.walk_tb(exc_info[2])).format()

self.assertEqual(len(stack), 1)
lno = f.__code__.co_firstlineno + 1
self.assertEqual(
stack[0],
f' File "{__file__}", line {lno}, in f\n 1/0\n'
)
self.assertEqual(len(stack), 1)
lno = f.__code__.co_firstlineno + 1
self.assertEqual(
stack[0],
f' File "{__file__}", line {lno}, in f\n 1/0\n'
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This cosmetic change was done by make patchcheck

@moi90
Copy link
Contributor Author

moi90 commented Nov 25, 2021

Irit Katriel:

While I do think this module should be more customisable than it is, I think it should be done via support for subclassing rather than injecting functions that get forwarded on as you do here.

There are other issues on bpo related to customising this module, with more convincing use cases than suppressing errors. I think it’s more likely that a patch will be accepted which solves the general problem of this module being inflexible (while being backwards compatible).

The patch you propose here solves a small part of the problem while making the api clunkier and it commits us to supporting this new parameter when we try to solve the general problem.

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

Successfully merging this pull request may close these issues.

3 participants