More on slot wrapper objects

June 20, 2007

Following up on the first installment:

Slot wrapper objects do not directly call the C functions that they wrap up. Instead they check that they are being called with a self argument that is an instance of the type that they are wrapping, create a 'method-wrapper' object with the self memorized, and call that.

We're not done yet, because there is another level of indirection: instead of directly calling the C function, the method-wrapper object calls a generic handler function for the particular sort of slot function that you are calling, and that generic handler calls the actual function. The generic handler handles all of the Python-level bookkeeping, because the slot functions themselves are only expecting to be called inside the guts of the interpreter.

(If you want to poke at a method-wrapper object, many of the attributes of a slot wrapper object are method-wrapper objects.)

The C function that corresponds to __new__ must be handled specially because __new__ is not called with an instance as the self argument. This C function is just turned into a generic wrapped method (which is a complicated subject in its own right) that gets the type itself as self when it gets called.

(Just to confuse everyone, a 'built-in method' object is not the same as a 'method-wrapper' object, although they do more or less the same thing.)

People who want to see how this particular sausage is made can look into Objects/typeobject.c and Objects/descrobject.c in the CPython source. Start at the add_operators function.

Written on 20 June 2007.
« Thinking about types of bugs (and static analysis)
A thought on web server capacity measurement »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Wed Jun 20 23:27:15 2007
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.