Permalink
Browse files

bootstrap correctly even if we're not a real module

  • Loading branch information...
1 parent 0096c58 commit 542c146302c156bb2ae7593d8ade7b117972149c @rfk rfk committed Nov 25, 2010
Showing with 23 additions and 5 deletions.
  1. +5 −0 ChangeLog.txt
  2. +18 −5 zipimportx/__init__.py
View
@@ -1,4 +1,9 @@
+v0.3.1:
+
+ * Make bootstrapping be more robust if it's not running from within a
+ module (e.g. when run by the cxFreeze init script)
+
v0.3.0:
* Always name the index <archivepath>.idx regardless of the platform; if
@@ -121,6 +121,9 @@
SEP = "/"
BADSEP = "\\"
+ZipImportError = zipimport.ZipImportError
+_zip_directory_cache = zipimport._zip_directory_cache
+_zip_directory_preload = {}
class zipimporter(zipimport.zipimporter):
@@ -148,7 +151,7 @@ def __init__(self,archivepath):
# we do it by looking in the directory cache.
archive = archivepath; prefix = ""
while archive:
- cached_files = zipimport._zip_directory_cache.get(archive)
+ cached_files = _zip_directory_cache.get(archive)
if cached_files is not None:
archivepath = archive
break
@@ -176,7 +179,7 @@ def __init__(self,archivepath):
cached_files = None
break
if cached_files is not None:
- zipimport._zip_directory_cache[archivepath] = cached_files
+ _zip_directory_cache[archivepath] = cached_files
# If the archive is in the cache, we bypass the default implementation
# since it wants to keep checking the filesystem for things we know
# (well, OK, *assume*) are still there.
@@ -515,7 +518,7 @@ def write_index(self,platform=None,preload=[]):
current platform; pass platform="win32" or platform="posix" to make
an index for a specific platform.
"""
- index = zipimport._zip_directory_cache[self.archive].copy()
+ index = _zip_directory_cache[self.archive].copy()
# Don't store the __file__ field, it won't be correct.
# Besides, we can re-create it as needed.
for (key,info) in index.iteritems():
@@ -564,7 +567,7 @@ def get_inline_code(self,platform=None,bootstrap_zipimportx=True):
import zipimportx
ilid = os.urandom(8).encode("hex")
name = "<zipimportx-%s>" % (ilid,)
- index = zipimport._zip_directory_cache[self.archive].copy()
+ index = _zip_directory_cache[self.archive].copy()
# The only field we need to keep is the "compressed" field
# Don't store the __file__ field, it won't be correct.
# Besides, we can re-create it as needed.
@@ -661,6 +664,16 @@ def install(cls):
if __name__ == "__main__":
if not sys.modules.get("zipimportx"):
zipimporter.install()
- sys.modules["zipimportx"] = sys.modules["__main__"]
+ thismodule = sys.modules.get(__name__)
+ if getattr(thismodule,"zipimporter",None) is not zipimporter:
+ # Uh-oh! We're running as some sort of bizarre script and
+ # not as a module. Create a fake one.
+ thismodule = imp.new_module("zipimportx")
+ thismodule.zipimporter = zipimporter
+ thismodule.SEP = SEP
+ thismodule.BADSEP = BADSEP
+ thismodule._zip_directory_cache = _zip_directory_cache
+ thismodule.ZipImportError = ZipImportError
+ sys.modules["zipimportx"] = thismodule

0 comments on commit 542c146

Please sign in to comment.