-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
Expose capture_locals
parameter in traceback
convenience functions
#77990
Comments
Since 3.5 the internal machinery of the It would be useful to also expose that ability through the various convenience functions. |
Hey, I would like to work on this, where do I start or how can I help? |
I think we should get one or more of the core devs who were involved in the changes that added that parameter to sign off on whether this is a good idea or not (I have no opinion at the moment). You should be able to find them via git blame and looking at the issues related to the changesets you find. Unless someone who remembers comes along and just adds them to nosy :) |
Hahah! Let me try to put this on the IRC channel may be someone can help us there. |
I don't understand this request. If you ask TracebackExceptions to capture_locals then the FrameSummary object gets a dict of the reprs of the locals, which can be introspected and participate in __eq__ checks but nothing else happens with them. What would the convenience functions do with this parameter? Ulrich, can you explain the use case or how you see this working? |
Sorry, I spoke too soon - see now that the locals are use in the StackSummary.format(). |
That said, you can always use TracebackException.from_exception(exc, capture_locals=True).format() which is not much longer than print_exception(exc, capture_locals=True) |
Functionally equivalent code would be: print("".join(TracebackException.from_exception(ex, capture_locals=True).format())) vs. (hypothetically) print_exc(capture_locals=True) Which is quite a significant difference IMO. |
I’m not sure I agree that it’s a big win. |
On the other hand, I think it makes sense to add a print() method to TracebackException so that you can do TracebackException.from_exception(ex, capture_locals=True).print(file) or whatever other combination of current or future params. |
That would make it slightly better, but I still think it's a pretty arcane incantation (esp. for newer people). What makes you hesitant to adding the parameter to the convenience functions? |
Generally speaking, I don't think it's a good idea to create redundant APIs. If we copy all the params from TracebackException (and it will be all params, why only this one?) that means more code, more tests, more documentation and a higher cognitive load for someone reading the documentation to learn the API. And the users don't really get anything tangible in return - they can do exactly the same things, maybe with a slightly smaller number of characters. A good standard library API has a small number of buttons that can be used in combination to get a lot of functionality. In this particular case, I think it's nice that print_exception() gives novices a simple way to get some basic functionality, and more sophisticated users can go to TracebackException for more options. I agree with you that at the moment it's a bit clumsy, and a print() method would make it more usable. Note that a programmer who understands the different parts of a traceback and wants to tweak its representation is not a novice and should be able to use that with ease. |
Why expose the internal machinery at all?
I would agree if the API in question was ergonomic and the documentation easy to read in the first place. I think neither is true for the traceback module and its documentation. Another example from today where I helped a coworker who was unable to figure out on his own how to print the current stack with locals: from traceback import StackSummary, walk_stack
print("".join(StackSummary.extract(walk_stack(None), capture_locals=True).format())) There are multiple things that make this API non-intuitive:
|
Feel free to create a new issue and propose a patch. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: