[Python-Dev] PEP 362 implementation issue: C callables

Larry Hastings larry at hastings.org
Fri Jun 15 23:24:58 CEST 2012



One wrinkle of PEP 362 in CPython is callables implemented directly in 
C.  Right now there's no good way to generate such signatures 
automatically.  That's why we kept __signature__ in the spec, to allow 
implementors to generate signatures by hand for these callables.

But the wrinkle gets wrinklier.  The problem is, you often can't add 
arbitrary attributes to these objects:

     >>> import os
     >>> os.stat.random_new_attribute = 3
    Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
    AttributeError: 'builtin_function_or_method' object has no attribute
    'random_new_attribute'

If users are to attach precomputed signatures to callables implemented 
in C, those callables may need to *already* have a __signature__ attribute.


I've already added __signature__ to PyCFunctionObject in the PEP 362 
patch.  This takes care of the vast bulk of such callables.  But there 
are a bunch of obscure callable objects in CPython, and I suspect some 
of them may need a __signature__ attribute.

Here's an almost- complete list, listed as their Python type:

    functools.KeyWrapper (result of functools.cmp_to_key)
    weakref.weakref
    method (bound method objects, aka "x = Class(); x.method")
    instancemethod (is this still even used?  it's never used in trunk)
    operator.itemgetter
    operator.attrgetter
    operator.methodcaller
    _json.Scanner (result of _json.make_scanner)
    _json.Encoder (result of _json.make_encoder)
    _ctypes.DictRemover (I'm not even sure how you can get your hands on
    one of these)
    sqlite3.Connection

I produced this by grepping for "/* tp_call */" and ignoring all of them 
that were set to "0", then creating one of those objects and trying (and 
failing) to set a __signature__ attribute.

There are four more candidates I found with the grep but couldn't figure 
out how to instantiate and test.  They have to do with the descriptor 
protocol, aka properties, but the types aren't directly exposed by 
Python.  They're all defined in Object/descrobject.c.  The internal 
class names are:

    method_descriptor
    classmethod_descriptor
    wrapper_descriptor
    method-wrapper (you get one of these out of a "wrapper_descriptor")


I'm happy to do the work, but I don't have a good sense of which 
callables need a __signature__ attribute.  Can someone here tell me 
which ones might need a __signature__ attribute?


//arry/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20120615/788847f3/attachment.html>


More information about the Python-Dev mailing list