[Python-ideas] Fix that broken callable builtin

Ionel Cristian Mărieș contact at ionelmc.ro
Sun Apr 19 02:37:47 CEST 2015


Sorry, I pasted a patch with changes unrelated to this discussion. This is
the gist of the proposed change:

 int
 PyCallable_Check(PyObject *x)
 {
     if (x == NULL)
         return 0;
-    return x->ob_type->tp_call;
+    return x->ob_type->tp_call && _PyObject_HasAttrId(x, &PyId___call__);
 }


Thanks,
-- Ionel Cristian Mărieș, http://blog.ionelmc.ro

On Sun, Apr 19, 2015 at 3:30 AM, Ionel Cristian Mărieș <contact at ionelmc.ro>
wrote:

>
> On Sun, Apr 19, 2015 at 2:09 AM, Paul Moore <p.f.moore at gmail.com> wrote:
>
>> So I'm not clear what "small change" you're referring to here:
>>
>> * A change to CPython to reduce the number of false positives by
>> making this case return False? If that, then no, I don't think a PEP
>> is needed. But note that user code still can't assume that the above
>> behaviour couldn't still happen in *other* cases, so the change would
>> be of limited value (and whether it gets accepted depends on whether
>> the complexity is justified by the benefit).
>> * A change to the definition of callable() to remove the possibility
>> of false positives at all? In that case, yes, a PEP probably *is*
>> needed, as that's going to affect an awful lot of corner cases, and
>> will impact all implementations. It's probably not correct to call
>> this a "small change".
>> * Something else?
>>
>
> ​The discussion was about this small change:
>
>  int
>  PyCallable_Check(PyObject *x)
>  {
> -    if (x == NULL)
> +    if (x == NULL) {
>          return 0;
> -    return x->ob_type->tp_call != NULL;
> +    }
> +
> +    return Py_TYPE(x)->tp_call && _PyObject_HasAttrId(x, &PyId___call__);
>  }
>
> There are more explanations in the thread in case you want to know more.
>
>
>
> Thanks,
> -- Ionel Cristian Mărieș, http://blog.ionelmc.ro
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150419/5dc87991/attachment.html>


More information about the Python-ideas mailing list