<div dir="ltr">Nathaniel, did you look at Brett's LazyLoader? It overcomes the subclass issue by using a module loader that makes all modules instances of a (trivial) Module subclass. I'm sure this approach can be backported as far as you need to go.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Nov 30, 2014 at 5:02 PM, Nathaniel Smith <span dir="ltr"><<a href="mailto:njs@pobox.com" target="_blank">njs@pobox.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Mon, Dec 1, 2014 at 12:59 AM, Nathaniel Smith <<a href="mailto:njs@pobox.com">njs@pobox.com</a>> wrote:<br>
> On Sun, Nov 30, 2014 at 10:14 PM, Mark Shannon <<a href="mailto:mark@hotpy.org">mark@hotpy.org</a>> wrote:<br>
>> Hi,<br>
>><br>
>> This discussion has been going on for a while, but no one has questioned the<br>
>> basic premise. Does this needs any change to the language or interpreter?<br>
>><br>
>> I believe it does not. I'm modified your original metamodule.py to not use<br>
>> ctypes and support reloading:<br>
>> <a href="https://gist.github.com/markshannon/1868e7e6115d70ce6e76" target="_blank">https://gist.github.com/markshannon/1868e7e6115d70ce6e76</a><br>
><br>
> Interesting approach!<br>
><br>
> As written, your code will blow up on any python < 3.4, because when<br>
> old_module gets deallocated it'll wipe the module dict clean. And I<br>
> guess even on >=3.4, this might still happen if old_module somehow<br>
> manages to get itself into a reference loop before getting<br>
> deallocated. (Hopefully not, but what a nightmare to debug if it did.)<br>
> However, both of these issues can be fixed by stashing a reference to<br>
> old_module somewhere in new_module.<br>
><br>
> The __class__ = ModuleType trick is super-clever but makes me<br>
> irrationally uncomfortable. I know that this is documented as a valid<br>
> method of fooling isinstance(), but I didn't know that until your<br>
> yesterday, and the idea of objects where type(foo) is not<br>
> foo.__class__ strikes me as somewhat blasphemous. Maybe this is all<br>
> fine though.<br>
><br>
> The pseudo-module objects generated this way will still won't pass<br>
> PyModule_Check, so in theory this could produce behavioural<br>
> differences. I can't name any specific places where this will break<br>
> things, though. From a quick skim of the CPython source, a few<br>
> observations: It means the PyModule_* API functions won't work (e.g.<br>
> PyModule_GetDict); maybe these aren't used enough to matter. It looks<br>
> like the __reduce__ methods on "method objects"<br>
> (Objects/methodobject.c) have a special check for ->m_self being a<br>
> module object, and won't pickle correctly if ->m_self ends up pointing<br>
> to one of these pseudo-modules. I have no idea how one ends up with a<br>
> method whose ->m_self points to a module object, though -- maybe it<br>
> never actually happens. PyImport_Cleanup treats module objects<br>
> differently from non-module objects during shutdown.<br>
<br>
</div></div>Actually, there is one showstopper here -- in the first version where<br>
reload() uses isinstance() is actually 3.4. Before that you need a<br>
real module subtype for reload to work. But this is in principle<br>
workaroundable by using subclassing + ctypes on old versions of python<br>
and the __class__ = hack on new versions.<br>
<div class="HOEnZb"><div class="h5"><br>
> I guess it also has the mild limitation that it doesn't work with<br>
> extension modules, but eh. Mostly I'd be nervous about the two points<br>
> above.<br>
><br>
> -n<br>
><br>
> --<br>
> Nathaniel J. Smith<br>
> Postdoctoral researcher - Informatics - University of Edinburgh<br>
> <a href="http://vorpus.org" target="_blank">http://vorpus.org</a><br>
<br>
<br>
<br>
--<br>
Nathaniel J. Smith<br>
Postdoctoral researcher - Informatics - University of Edinburgh<br>
<a href="http://vorpus.org" target="_blank">http://vorpus.org</a><br>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/guido%40python.org" target="_blank">https://mail.python.org/mailman/options/python-dev/guido%40python.org</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido">python.org/~guido</a>)</div>
</div>