[Python-Dev] Sharing expat instances
Phillip J. Eby
pje at telecommunity.com
Wed Dec 14 20:44:18 CET 2005
At 08:21 PM 12/14/2005 +0100, Fredrik Lundh wrote:
>Scott David Daniels wrote:
>
> > > > One good reason for this is that the .pyd's or .so's cannot necessarily
> > > > be used from zip files
> > >
> > > When you say "cannot necessarily", are the situations where they can be
> > > imported from zip files? I thought the answer to that was always "no".
> >
> > I thought so too, but was not sure enough to state it that way.
>
>you could of course add them to the zip file, and automagically extract
>them before you start importing things.
The runtime system for Python Eggs does this; extraction is to a
PYTHON_EGG_CACHE directory. See e.g.:
http://peak.telecommunity.com/DevCenter/PkgResources#resource-extraction
The egg builder writes a .py file alongside the .pyd or .so in the .egg
file that contains something like:
def __bootstrap__():
global __bootstrap__, __loader__, __file__
import sys, pkg_resources, imp
__file__ = pkg_resources.resource_filename(__name__,'foobar.so')
del __bootstrap__, __loader__
imp.load_dynamic(__name__,__file__)
__bootstrap__()
So, when you import from the zipfile, the .py file gets loaded (since
zipimport doesn't support .pyd/.so/etc. imports directly) and then it
reloads the module from the extracted file. The other magic there is just
to keep any of the special names from staying behind in the reloaded module.
More information about the Python-Dev
mailing list