Uh oh!
There was an error while loading. Please reload this page.
bpo-37645: add new function _PyObject_FunctionStr() - #14890
Conversation
Uh oh!
There was an error while loading. Please reload this page.
vstinner
commented
Jul 29, 2019
I would prefer to make it private at the beginning: replace Py with _Py. |
jdemeyer
commented
Aug 4, 2019
Done. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Why not using the qualified name for type(func)?
Would it be possible to also add the module, except for builtin functions?
There was a problem hiding this comment.
Why not using the qualified name for type(func)?
I decided to use str(func) as fallback, which looks more useful than f"{type(func)} object".
Before changing this PR, could you reply on my last comment on bpo-37645? It might be a better solution to just use |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| set_add = set.add | ||
| expected_errmsg = "descriptor 'add' of 'set' object needs an argument" | ||
| expected_errmsg = "set.add() needs an argument" |
There was a problem hiding this comment.
I consider this a regression: the message is now too similar to calling the method on an instance, but missing an argument.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: set.add() takes exactly one argument (0 given)
>>> set.add()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: set.add() needs an argumentThere was a problem hiding this comment.
I can change the error message to anything you like, except that it must contain the string set.add (the function name). So it could be
TypeError: unbound method set.add() needs an argument
or whatever (surely, this is better than anything mentioning descriptors).
Small rant: the bug is really this:
>>> set().add()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: add() takes exactly one argument (0 given)
For a Python method, it would correctly note that 2 arguments are required. This is difficult to fix in CPython since builtin_function_or_method doesn't really know whether it's a function or method. This was one of the things that the rejected PEP 580 would have addressed.
| .. c:function:: PyObject* _PyObject_FunctionStr(PyObject *func) | ||
| Return a user-friendly string representation of the function-like object | ||
| *func*. This returns ``func.__qualname__ + "()"`` if there is a |
There was a problem hiding this comment.
Returning __qualname__ without module is not particularly useful.
If you want to return a full qualified name (long but unambiguous), return __module__ + '.' + __qualname__. __module__ can be omitted if it is 'builtins'.
If you want to return a short name (it is enough in many times), return __name__.
if __qualname__ is not available, you can use __name__ instead.
There was a problem hiding this comment.
if
__qualname__is not available, you can use__name__instead.
This is mainly meant as replacement for PyEval_GetFuncName and all classes supported by that function implement __qualname__. So I see little reason for the additional complexity of supporting __name__.
Uh oh!
There was an error while loading. Please reload this page.
| set_add = set.add | ||
| expected_errmsg = "descriptor 'add' of 'set' object needs an argument" | ||
| expected_errmsg = "set.add() needs an argument" |
Uh oh!
There was an error while loading. Please reload this page.
jdemeyer
commented
Sep 13, 2019
I tried to address all comments. |
Uh oh!
There was an error while loading. Please reload this page.
encukou
commented
Oct 8, 2019
One more nitpick above. Otherwise, this does look like an improvement in the error messages – not only in custom extension callables! |
miss-islington
commented
Oct 22, 2019
@jdemeyer: Status check is done, and it's a success ✅ . |
miss-islington
commented
Oct 22, 2019
Sorry, I can't merge this PR. Reason: |
miss-islington
commented
Oct 22, 2019
@jdemeyer: Status check is done, and it's a success ✅ . |
miss-islington
commented
Oct 22, 2019
Sorry, I can't merge this PR. Reason: |
miss-islington
commented
Oct 22, 2019
@jdemeyer: Status check is done, and it's a success ✅ . |
miss-islington
commented
Oct 22, 2019
Sorry, I can't merge this PR. Reason: |
encukou
commented
Oct 22, 2019
@zooba What's the best way to get Azure Pipelines to run on this PR? |
jdemeyer
commented
Oct 22, 2019
Thanks for going through with this. |
rlamy
commented
Oct 31, 2019
Shouldn't |
encukou
commented
Nov 5, 2019
Good catch, thank you! |
The conflicting change in master is to use _PyErr_Format (with explicit thread state argument) instead of PyErr_Format
miss-islington
commented
Nov 5, 2019
@jdemeyer: Status check is done, and it's a success ✅ . |
Additional note: the `method_check_args` function in `Objects/descrobject.c` is written in such a way that it applies to all kinds of descriptors. In particular, a future re-implementation of `wrapper_descriptor` could use that code. CC @vstinner@encukouhttps://bugs.python.org/issue37645 Automerge-Triggered-By: @encukou
Additional note: the `method_check_args` function in `Objects/descrobject.c` is written in such a way that it applies to all kinds of descriptors. In particular, a future re-implementation of `wrapper_descriptor` could use that code. CC @vstinner@encukouhttps://bugs.python.org/issue37645 Automerge-Triggered-By: @encukou
Additional note: the
method_check_argsfunction inObjects/descrobject.cis written in such a way that it applies to all kinds of descriptors. In particular, a future re-implementation ofwrapper_descriptorcould use that code.CC @vstinner@encukou
https://bugs.python.org/issue37645
Automerge-Triggered-By: @encukou