-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-98284: better error message for undefined abstractmethod #97971
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 2 commits
c268395
3603740
4073b1f
6a762e4
6ae052e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Improved :class:`TypeError` message for undefined abstract methods of a | ||
:class:`abc.ABC` instance. The names of the missing methods are surrounded | ||
by single-quotes to highlight them. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4861,9 +4861,10 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |
PyObject *abstract_methods; | ||
PyObject *sorted_methods; | ||
PyObject *joined; | ||
PyObject* comma_w_quotes_sep; | ||
Py_ssize_t method_count; | ||
|
||
/* Compute ", ".join(sorted(type.__abstractmethods__)) | ||
/* Compute "', '".join(sorted(type.__abstractmethods__)) | ||
into joined. */ | ||
abstract_methods = type_abstractmethods(type, NULL); | ||
if (abstract_methods == NULL) | ||
|
@@ -4876,8 +4877,8 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |
Py_DECREF(sorted_methods); | ||
return NULL; | ||
} | ||
_Py_DECLARE_STR(comma_sep, ", "); | ||
joined = PyUnicode_Join(&_Py_STR(comma_sep), sorted_methods); | ||
comma_w_quotes_sep = PyUnicode_FromString("', '"); | ||
joined = PyUnicode_Join(comma_w_quotes_sep, sorted_methods); | ||
method_count = PyObject_Length(sorted_methods); | ||
Py_DECREF(sorted_methods); | ||
if (joined == NULL) | ||
|
@@ -4887,11 +4888,12 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |
|
||
PyErr_Format(PyExc_TypeError, | ||
"Can't instantiate abstract class %s " | ||
"without an implementation for abstract method%s %U", | ||
"without an implementation for abstract method%s '%U'", | ||
type->tp_name, | ||
method_count > 1 ? "s" : "", | ||
joined); | ||
Py_DECREF(joined); | ||
Py_DECREF(comma_w_quotes_sep); | ||
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. You also need to DECREF this in the two Come to think of it, the second of those already leaks 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. Aah, yep! Fixed in 6ae052e. |
||
return NULL; | ||
} | ||
PyObject *obj = type->tp_alloc(type, 0); | ||
|
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.
The NEWS entry must refer to an issue, not a PR. You'll have to create an issue and rename the file to have your issue number in the file name.
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.
Thanks, see 4073b1f.