-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
bpo-37645: add new function _PyObject_FunctionStr() #14890
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
Changes from 1 commit
2a55a9f
733158d
4c0c877
2e1b44e
fa20556
b2e56da
21aee80
402ec96
0307ce9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,6 +196,17 @@ Object Protocol | |
This function now includes a debug assertion to help ensure that it | ||
does not silently discard an active exception. | ||
|
||
|
||
.. 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 | ||
``__qualname__`` attribute and ``type(func).__name__ + " object"`` | ||
otherwise. Note that there is no check that *func* is actually callable. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I decided to use |
||
|
||
.. versionadded:: 3.9 | ||
|
||
|
||
.. c:function:: PyObject* PyObject_Bytes(PyObject *o) | ||
|
||
.. index:: builtin: bytes | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1967,7 +1967,7 @@ def test_methods_in_c(self): | |
# different error messages. | ||
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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 argument There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, this is confusing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can change the error message to anything you like, except that it must contain the string
or whatever (surely, this is better than anything mentioning descriptors). Small rant: the bug is really this:
For a Python method, it would correctly note that 2 arguments are required. This is difficult to fix in CPython since |
||
|
||
with self.assertRaises(TypeError) as cm: | ||
set_add() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Add :c:func:`PyObject_FunctionStr` to get a user-friendly string representation | ||
of a function-like object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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__
.