@@ -157,6 +157,42 @@ PyObject_VectorcallDict(PyObject *callable, PyObject *const *args,
157157 return _PyObject_FastCallDictTstate (tstate , callable , args , nargsf , kwargs );
158158}
159159
160+ static void
161+ object_is_not_callable (PyThreadState * tstate , PyObject * callable )
162+ {
163+ if (Py_IS_TYPE (callable , & PyModule_Type )) {
164+ // >>> import pprint
165+ // >>> pprint(thing)
166+ // Traceback (most recent call last):
167+ // File "<stdin>", line 1, in <module>
168+ // TypeError: 'module' object is not callable. Did you mean: 'pprint.pprint(...)'?
169+ PyObject * name = PyModule_GetNameObject (callable );
170+ if (name == NULL ) {
171+ _PyErr_Clear (tstate );
172+ goto basic_type_error ;
173+ }
174+ PyObject * attr ;
175+ int res = _PyObject_LookupAttr (callable , name , & attr );
176+ if (res < 0 ) {
177+ _PyErr_Clear (tstate );
178+ }
179+ else if (res > 0 && PyCallable_Check (attr )) {
180+ _PyErr_Format (tstate , PyExc_TypeError ,
181+ "'%.200s' object is not callable. "
182+ "Did you mean: '%U.%U(...)'?" ,
183+ Py_TYPE (callable )-> tp_name , name , name );
184+ Py_DECREF (attr );
185+ Py_DECREF (name );
186+ return ;
187+ }
188+ Py_XDECREF (attr );
189+ Py_DECREF (name );
190+ }
191+ basic_type_error :
192+ _PyErr_Format (tstate , PyExc_TypeError , "'%.200s' object is not callable" ,
193+ Py_TYPE (callable )-> tp_name );
194+ }
195+
160196
161197PyObject *
162198_PyObject_MakeTpCall (PyThreadState * tstate , PyObject * callable ,
@@ -171,9 +207,7 @@ _PyObject_MakeTpCall(PyThreadState *tstate, PyObject *callable,
171207 * temporary dictionary for keyword arguments (if any) */
172208 ternaryfunc call = Py_TYPE (callable )-> tp_call ;
173209 if (call == NULL ) {
174- _PyErr_Format (tstate , PyExc_TypeError ,
175- "'%.200s' object is not callable" ,
176- Py_TYPE (callable )-> tp_name );
210+ object_is_not_callable (tstate , callable );
177211 return NULL ;
178212 }
179213
@@ -322,9 +356,7 @@ _PyObject_Call(PyThreadState *tstate, PyObject *callable,
322356 else {
323357 call = Py_TYPE (callable )-> tp_call ;
324358 if (call == NULL ) {
325- _PyErr_Format (tstate , PyExc_TypeError ,
326- "'%.200s' object is not callable" ,
327- Py_TYPE (callable )-> tp_name );
359+ object_is_not_callable (tstate , callable );
328360 return NULL ;
329361 }
330362
0 commit comments