Anthony Tuininga avatar Anthony Tuininga committed 8090a1c

Tidy up the code for including base modules; handle the situation where values
are imported from a module and renamed during the import.

Comments (0)

Files changed (2)

cx_Freeze/finder.py

 LOAD_CONST = opcode.opmap["LOAD_CONST"]
 IMPORT_NAME = opcode.opmap["IMPORT_NAME"]
 IMPORT_FROM = opcode.opmap["IMPORT_FROM"]
+STORE_FAST = opcode.opmap["STORE_FAST"]
 STORE_NAME = opcode.opmap["STORE_NAME"]
 STORE_GLOBAL = opcode.opmap["STORE_GLOBAL"]
 STORE_OPS = (STORE_NAME, STORE_GLOBAL)
 class ModuleFinder(object):
 
     def __init__(self, includeFiles = [], excludes = [], path = None,
-            replacePaths = [], copyDependentFiles = True, bootstrap = False):
+            replacePaths = [], copyDependentFiles = True, bootstrap = False,
+            compress = True):
         self.includeFiles = list(includeFiles)
         self.excludes = dict.fromkeys(excludes)
         self.replacePaths = list(replacePaths)
         self.copyDependentFiles = copyDependentFiles
+        self.compress = compress
         self.path = path or sys.path
         self.modules = []
         self.aliases = {}
     def _AddBaseModules(self):
         """Add the base modules to the finder. These are the modules that
            Python imports itself during initialization and, if not found,
-           can result in behavior that differs from running from source."""
+           can result in behavior that differs from running from source;
+           also include modules used within the bootstrap code"""
         self.IncludeModule("traceback")
         self.IncludeModule("warnings")
         self.IncludePackage("encodings")
         if sys.version_info[0] >= 3:
             self.IncludeModule("io")
+        if self.copyDependentFiles:
+            self.IncludeModule("imp")
+            self.IncludeModule("os")
+            self.IncludeModule("sys")
+            if self.compress:
+                self.IncludeModule("zlib")
 
     def _AddModule(self, name):
         """Add a module to the list of modules but if one is already found,
         if method is not None:
             method(self, *args)
 
-    def _ScanCode(self, co, module, deferredImports):
+    def _ScanCode(self, co, module, deferredImports, topLevel = True):
         """Scan code, looking for imported modules and keeping track of the
            constants that have been created in order to better tell which
            modules are truly missing."""
                 else:
                     relativeImportIndex = -1
                     fromList, = arguments
-                if fromList:
-                    for fromName in fromList:
-                        module.globalNames[fromName] = None
                 if name not in module.excludeNames:
                     subModule = self._ImportModule(name, deferredImports,
                             module, relativeImportIndex)
                         if fromList and subModule.path is not None:
                             self._EnsureFromList(module, subModule, fromList,
                                     deferredImports)
-            elif op == IMPORT_FROM:
+            elif op == IMPORT_FROM and topLevel:
+                if is3:
+                    op = code[opIndex]
+                    opArg = code[opIndex + 1] + code[opIndex + 2] * 256
+                else:
+                    op = ord(code[opIndex])
+                    opArg = ord(code[opIndex + 1]) + \
+                            ord(code[opIndex + 2]) * 256
                 opIndex += 3
+                if op == STORE_FAST:
+                    name = co.co_varnames[opArg]
+                else:
+                    name = co.co_names[opArg]
+                module.globalNames[name] = None
             elif op not in (BUILD_LIST, INPLACE_ADD):
-                if op in STORE_OPS:
+                if topLevel and op in STORE_OPS:
                     name = co.co_names[opArg]
                     if name == "__all__":
                         module.allNames.extend(arguments)
                 arguments = []
         for constant in co.co_consts:
             if isinstance(constant, type(co)):
-                self._ScanCode(constant, module, deferredImports)
+                self._ScanCode(constant, module, deferredImports,
+                        topLevel = False)
 
     def AddAlias(self, name, aliasFor):
         """Add an alias for a particular module; when an attempt is made to

cx_Freeze/freezer.py

             argsSource = self
         finder = cx_Freeze.ModuleFinder(self.includeFiles, argsSource.excludes,
                 argsSource.path, argsSource.replacePaths,
-                argsSource.copyDependentFiles)
-        if argsSource.copyDependentFiles:
-            finder.IncludeModule("imp")
-            finder.IncludeModule("os")
-            finder.IncludeModule("sys")
-            if argsSource.compress:
-                finder.IncludeModule("zlib")
+                argsSource.copyDependentFiles, argsSource.compress)
         for name in argsSource.includes:
             finder.IncludeModule(name)
         for name in argsSource.packages:
             self.path = sys.path
         if self.appendScriptToLibrary:
             self._VerifyCanAppendToLibrary()
-        for sourceFileName, targetFileName in self.includeFiles + self.zipIncludes:
+        for sourceFileName, targetFileName in \
+                self.includeFiles + self.zipIncludes:
             if not os.path.exists(sourceFileName):
                 raise ConfigError("cannot find file/directory named %s",
                         sourceFileName)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.