Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
GH-103899: Provide a hint when accidentally calling a module#103900
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
ca5622d0edff38f97d99506fadfb833144de52c1f7File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Provide a helpful hint in the :exc:`TypeError` message when accidentally | ||
| calling a :term:`module` object that has a callable attribute of the same | ||
| name (such as :func:`dis.dis` or :class:`datetime.datetime`). |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -167,6 +167,42 @@ PyObject_VectorcallDict(PyObject *callable, PyObject *const *args, | ||
| return _PyObject_FastCallDictTstate(tstate, callable, args, nargsf, kwargs); | ||
| } | ||
| static void | ||
| object_is_not_callable(PyThreadState *tstate, PyObject *callable) | ||
| { | ||
| if (Py_IS_TYPE(callable, &PyModule_Type)) { | ||
| // >>> import pprint | ||
| // >>> pprint(thing) | ||
| // Traceback (most recent call last): | ||
| // File "<stdin>", line 1, in <module> | ||
| // TypeError: 'module' object is not callable. Did you mean: 'pprint.pprint(...)'? | ||
| PyObject *name = PyModule_GetNameObject(callable); | ||
| if (name == NULL) { | ||
Member 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. Should we instead suppress the new exception here and go back to the "object is not callable" message? Looks like this can happen if someone deletes the MemberAuthor 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. Yeah, that makes sense.
| ||
| _PyErr_Clear(tstate); | ||
| goto basic_type_error; | ||
| } | ||
| PyObject *attr; | ||
| int res = _PyObject_LookupAttr(callable, name, &attr); | ||
| if (res < 0) { | ||
| _PyErr_Clear(tstate); | ||
| } | ||
| else if (res > 0 && PyCallable_Check(attr)) { | ||
| _PyErr_Format(tstate, PyExc_TypeError, | ||
| "'%.200s' object is not callable. " | ||
| "Did you mean: '%U.%U(...)'?", | ||
| Py_TYPE(callable)->tp_name, name, name); | ||
| Py_DECREF(attr); | ||
| Py_DECREF(name); | ||
| return; | ||
| } | ||
| Py_XDECREF(attr); | ||
| Py_DECREF(name); | ||
| } | ||
| basic_type_error: | ||
| _PyErr_Format(tstate, PyExc_TypeError, "'%.200s' object is not callable", | ||
| Py_TYPE(callable)->tp_name); | ||
| } | ||
| PyObject * | ||
| _PyObject_MakeTpCall(PyThreadState *tstate, PyObject *callable, | ||
| @@ -181,9 +217,7 @@ _PyObject_MakeTpCall(PyThreadState *tstate, PyObject *callable, | ||
| * temporary dictionary for keyword arguments (if any) */ | ||
| ternaryfunc call = Py_TYPE(callable)->tp_call; | ||
| if (call == NULL) { | ||
| _PyErr_Format(tstate, PyExc_TypeError, | ||
| "'%.200s' object is not callable", | ||
| Py_TYPE(callable)->tp_name); | ||
| object_is_not_callable(tstate, callable); | ||
| return NULL; | ||
| } | ||
| @@ -332,9 +366,7 @@ _PyObject_Call(PyThreadState *tstate, PyObject *callable, | ||
| else { | ||
| call = Py_TYPE(callable)->tp_call; | ||
| if (call == NULL) { | ||
| _PyErr_Format(tstate, PyExc_TypeError, | ||
| "'%.200s' object is not callable", | ||
| Py_TYPE(callable)->tp_name); | ||
| object_is_not_callable(tstate, callable); | ||
| return NULL; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.