Ticket #494 (closed defect: fixed)
Cython functions not bound as methods
| Reported by: | robertwb | Owned by: | robertwb |
|---|---|---|---|
| Priority: | major | Milestone: | 0.17 |
| Component: | Python Semantics | Keywords: | |
| Cc: | bfroehle |
Description (last modified by scoder) (diff)
In Python, one can do
class A:
pass
def foo(self):
print self
A.foo = foo
print A().foo()
and foo will get bound as expected. However, Cython creates PyCFunctions which do not properly bind when accessed (possibly by design). This is the root issue behind #478, and will become more evident once closures start getting used.
PyCFunction does not allow subclassing. The only reasonable fix I see is to create a new type that wraps PyCFunction but behaves like PyFunction. This will have indirection overhead. To avoid overhead, one alternative is for our new type to have the same struct and slots as PyCFunction (with the exception of tp_name, tp_descr_get, tp_alloc, and tp_dealloc of course). This still may have overhead as the Python compiler optimizes for PyCFunction calls--perhaps the user should be allowed to control this (as part of an "ultra pure, I don't care what cost" mode...)?
