Only the first instanceof a dynamically loaded module
gets the __file__ attribute. It is missing from all later
instances.
The problem is in importdl.c in the function
_PyImport_LoadDynamicModule. The first time a
module is loaded a copy of its module dictionary is
made by a call to _PyImport_FixupExtension. After that
copy was made, _PyImport_LoadDynamicModule adds
the _file__ attribute to the original module dictionary.
Thus the first instance of the module has the
__file__attribute.
Later when another instance of the module loads, its
module dictionary is duplicated from the copy of the
dictionary from the first instance. The problem is that
when that copy was made, the __file__ attribute was not
yet there. Thus the later instance of the module does
not have the __file__ attribute.
The required fix is in importdl.c and consists of simply
moving the call of _PyImport_FixupExtension to a point
just after the __file__ attribute is added, instead of just
before it. I have tested this fix and it solves the problem.
|