<p dir="ltr"><br>
On Jul 26, 2015 4:39 AM, "Petr Viktorin" <<a href="mailto:encukou@gmail.com">encukou@gmail.com</a>> wrote:<br>
> So it seems that extension modules that need per-module state need to<br>
> use heap types. And the heap types need a reference to "their" module.<br>
> And methods of those types need to be called with the class that<br>
> defined them.<br>
> This would be possible with regular methods. But, consider for example<br>
> the tp_iternext signature:<br>
><br>
>     PyObject* myobj_iternext(PyObject *self)<br>
><br>
> There's no good way for this function to get a reference to the class<br>
> it belongs to.<br>
> `Py_TYPE(self)` might be a subclass. The best way I can think of is<br>
> walking the MRO until I get to a class with tp_iter (or a class<br>
> created from "my" known PyType_Spec), but one of the requirements on<br>
> module state is that it needs to be efficient, so I'd rather avoid<br>
> walking a list.</p>
<p dir="ltr">One thing I've considered for several years now, and perhaps even proposed at some point (around PEP 451?), is adding "__origin__" to objects, indicating where the object came from.  "Where" would be the object (or its qualname?) associated with the scope in which the first object was created.  For example, for classes this would be the module (or class/func for nested ones).  Likewise, the class for methods.</p>
<p dir="ltr">Something like __origin__ would help make the actual class/module explicit.   I expect it would be sufficiently efficient.  __qualname__ gets you something similar but less efficiently.  Is __qualname__ set for extension types/functions?  Note that __origin__ provides other non-import benefits as well.</p>
<p dir="ltr">An alternative, could the module intra-dependencies be bound where needed?  For example, with _csv could Error be added to reader.__dict__ (i.e. bound to reader)?</p>
<p dir="ltr">-eric<br>
</p>