-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Replace PyEval_GetFuncName/PyEval_GetFuncDesc #81826
Comments
PyEval_GetFuncName is bad API because
Since PyEval_GetFuncName and PyEval_GetFuncDesc are always used together, we might as well replace them by a single function with a proper API. |
|
Another solution would be to change the __str__ of various function objects to a prettier output. For example, we currently have >>> def f(): pass
>>> print(f)
<function f at 0x7f9f4bbe5e18> We could change this to >>> def f(): pass
>>> print(f)
f() and then use "%S" to display the functions in error messages. But I have a feeling that this is a more controversial change than PR 14890. |
Maybe repr(func) should be left unchanged, but str(func) can be enhanced? |
Yes, that is what I meant. |
I am not convinced. I'm wary of making error messages depend on the str representation of a function; that would prevent us from changing it later. I train beginners to recognize "<function f at 0x7f9f4bbe5e18>" as a sign of omitted parentheses. The ugliness is useful: it shows you're dealing with an internal object, not a data value. So, I think "<function f>" is much better than just "f()". I wouldn't mind "<function f()>" (maybe even with the full signature), but that doesn't quite help this case. |
Why wouldn't we be able to change anything? Typically, the exact string of an error message is NOT part of the API (the exception *type* is, but we're not talking about that).
I'm not following here. Given that Python is a programming language, the user *is* the programmer. Anyway, you don't have to be convinced. I'm trying to solve a problem here and I have two approaches (PR 14890 and PR 15295). I'm more inclined towards PR 15295, but if you like the idea of PR 14890 better, we can go with that instead. |
Maybe you're misunderstanding something. The goal is not really to change error messages, only the way how they are produced. For example, we currently have >>> def f(): pass
>>> f(**1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: f() argument after ** must be a mapping, not int This is about how the "f()" in the error message is produced. Currently, this uses PyEval_GetFuncName() and PyEval_GetFuncDesc(). For the reasons explained in this issue, I want to replace that. I see two ways of doing this:
|
I like PR 14890 better. I like the separation of representation for error messages (where it's clearer that this is a callable) and for __str__. Also, changing the __str__ of functions would need much wider discussion than on issues/PRs. I left some comments on the PR. |
I don't see anything. Either I'm doing something wrong or GitHub is messed up. |
My bad, I didn't publish the comments. They should be there now. |
With this change, CPython no longer uses PyEval_GetFuncName/PyEval_GetFuncDesc internally. |
I've recently covered these two functions in tests, so this issue caught my eye. |
Before deprecating them, we should provide a good replacement for the use case they served. |
I found 3 projects using PyEval_GetFuncName() in PyPI top 8,000 projects (at 2024-03-16):
Code:
I found no matches for PyEval_GetFuncDesc(). |
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: