Wandering Thoughts archives


More on slot wrapper objects

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.

python/SlotWrapperObjectsII written at 23:27:15; Add Comment

Page tools: See As Normal.
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.