Skip to content

Commit 0a1fdb9

Browse files
committed
Be simpler
1 parent 7e46073 commit 0a1fdb9

2 files changed

Lines changed: 5 additions & 41 deletions

File tree

Lib/test/test_call.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,6 @@ def missing_self_no_args():
921921

922922
@cpython_only
923923
class TestErrorMessagesUseQualifiedName(unittest.TestCase):
924-
925924
@contextlib.contextmanager
926925
def check_raises_type_error(self, message):
927926
with self.assertRaises(TypeError) as cm:

Python/ceval.c

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,7 +1575,7 @@ static void
15751575
too_many_positional(PyThreadState *tstate, PyCodeObject *co,
15761576
Py_ssize_t given, PyObject *defaults,
15771577
_PyStackRef *localsplus, PyObject *qualname,
1578-
int suggest_missing_self)
1578+
int should_suggest_missing_self)
15791579
{
15801580
int plural;
15811581
Py_ssize_t kwonly_given = 0;
@@ -1618,7 +1618,7 @@ too_many_positional(PyThreadState *tstate, PyCodeObject *co,
16181618
kwonly_sig = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
16191619
assert(kwonly_sig != NULL);
16201620
}
1621-
if (suggest_missing_self) {
1621+
if (should_suggest_missing_self) {
16221622
self_hint = PyUnicode_FromString(
16231623
". Did you forget the 'self' parameter in the function definition?");
16241624
if (self_hint == NULL) {
@@ -1641,44 +1641,9 @@ too_many_positional(PyThreadState *tstate, PyCodeObject *co,
16411641
}
16421642

16431643
static int
1644-
suggest_missing_self(PyFunctionObject *func, PyCodeObject *co,
1645-
_PyStackRef const *args, Py_ssize_t argcount)
1644+
suggest_missing_self(PyCodeObject *co, Py_ssize_t argcount)
16461645
{
1647-
if (co->co_argcount >= argcount) {
1648-
// When declared count is more than provided, there is nothing to add
1649-
return 0;
1650-
}
1651-
1652-
PyObject *self = PyStackRef_AsPyObjectBorrow(args[0]);
1653-
if (self == NULL) {
1654-
// When first arg is NULL, it's not really about self
1655-
return 0;
1656-
}
1657-
1658-
Py_ssize_t qualname_len;
1659-
const char *qualname = PyUnicode_AsUTF8AndSize(
1660-
func->func_qualname, &qualname_len);
1661-
if (qualname == NULL) {
1662-
PyErr_Clear();
1663-
return 0;
1664-
}
1665-
1666-
const char *method_dot = strrchr(qualname, '.');
1667-
if (method_dot == NULL) {
1668-
return 0;
1669-
}
1670-
1671-
const char *class_start = qualname;
1672-
for (const char *p = qualname; p < method_dot; p++) {
1673-
if (*p == '.') {
1674-
class_start = p + 1;
1675-
}
1676-
}
1677-
Py_ssize_t class_len = method_dot - class_start;
1678-
const char *type_name = Py_TYPE(self)->tp_name;
1679-
1680-
return (strlen(type_name) == (size_t)class_len
1681-
&& strncmp(type_name, class_start, (size_t)class_len) == 0);
1646+
return (co->co_argcount + 1) == argcount
16821647
}
16831648

16841649
static int
@@ -1773,7 +1738,7 @@ initialize_locals(PyThreadState *tstate, PyFunctionObject *func,
17731738

17741739
/* Copy all positional arguments into local variables */
17751740
Py_ssize_t j, n;
1776-
int missing_self_hint = suggest_missing_self(func, co, args, argcount);
1741+
int missing_self_hint = suggest_missing_self(co, argcount);
17771742
if (argcount > co->co_argcount) {
17781743
n = co->co_argcount;
17791744
}

0 commit comments

Comments
 (0)