Commits

Thomas Kluyver committed 12e310f

Space out & comment code in cx_Freeze.finder

Comments (0)

Files changed (1)

cx_Freeze/finder.py

                 fileNames = os.listdir(path)
             except os.error:
                 continue
+            
             for fileName in fileNames:
                 fullName = os.path.join(path, fileName)
                 if os.path.isdir(fullName):
             return self._modules[name], False
         except KeyError:
             pass
+        
         if name in self._builtinModules:
             module = self._AddModule(name)
             self._RunHook("load", module.name, module)
             module.inImport = False
             return module, False
+        
         pos = name.rfind(".")
-        if pos < 0:
+        if pos < 0:  # Top-level module
             path = self.path
             searchName = name
             parentModule = None
-        else:
+        else:        # Dotted module name - look up the parent module
             parentName = name[:pos]
             parentModule, returnError = \
                     self._InternalImportModule(parentName, deferredImports,
                 parentModule.ExtendPath()
             path = parentModule.path
             searchName = name[pos + 1:]
+        
         if name in self.aliases:
             actualName = self.aliases[name]
             module, returnError = \
                     self._InternalImportModule(actualName, deferredImports)
             self._modules[name] = module
             return module, returnError
+        
         try:
             fp, path, info = self._FindModule(searchName, path, namespace)
             module = self._LoadModule(name, fp, path, info, deferredImports,
         module = self._AddModule(name)
         module.file = path
         module.parent = parent
+        
         if type == imp.PY_SOURCE:
+            # Load & compile Python source code
             if sys.version_info[0] >= 3:
+                # For Python 3, read the file with the correct encoding
                 import tokenize
                 fp = open(path, "rb")
                 encoding, lines = tokenize.detect_encoding(fp.readline)
             if codeString and codeString[-1] != "\n":
                 codeString = codeString + "\n"
             module.code = compile(codeString, path, "exec")
+        
         elif type == imp.PY_COMPILED:
+            # Load Python bytecode
             if isinstance(fp, str):
                 magic = fp[:4]
             else:
             else:
                 fp.read(4)
                 module.code = marshal.load(fp)
+        
+        # If there's a custom hook for this module, run it.
         self._RunHook("load", module.name, module)
+        
         if module.code is not None:
             if self.replacePaths:
                 topLevelModule = module
                     topLevelModule = topLevelModule.parent
                 module.code = self._ReplacePathsInCode(topLevelModule,
                         module.code)
+            
+            # Scan the module code for import statements
             self._ScanCode(module.code, module, deferredImports)
+        
         module.inImport = False
         return module
 
     def _ReplacePathsInCode(self, topLevelModule, co):
         """Replace paths in the code as directed, returning a new code object
            with the modified paths in place."""
+        # Prepare the new filename.
         origFileName = newFileName = os.path.normpath(co.co_filename)
         for searchValue, replaceValue in self.replacePaths:
             if searchValue == "*":
                 continue
             newFileName = replaceValue + origFileName[len(searchValue):]
             break
+        
+        # Run on subordinate code objects from function & class definitions.
         constants = list(co.co_consts)
         for i, value in enumerate(constants):
             if isinstance(value, type(co)):
                 constants[i] = self._ReplacePathsInCode(topLevelModule, value)
+        
+        # Build the new code object.
         if sys.version_info[0] < 3:
             return types.CodeType(co.co_argcount, co.co_nlocals,
                     co.co_stacksize, co.co_flags, co.co_code, tuple(constants),
                 else:
                     opArg = ord(code[opIndex]) + ord(code[opIndex + 1]) * 256
                 opIndex += 2
+            
             if op == LOAD_CONST:
+                # Store an argument to be used later by an IMPORT_NAME operation.
                 arguments.append(co.co_consts[opArg])
+            
             elif op == IMPORT_NAME:
                 name = co.co_names[opArg]
                 if len(arguments) == 2:
                 else:
                     relativeImportIndex = -1
                     fromList, = arguments
+                
                 if name not in module.excludeNames:
+                    # Load the imported module
                     importedModule = self._ImportModule(name, deferredImports,
                             module, relativeImportIndex)
                     if importedModule is not None:
                                 and importedModule.path is not None:
                             self._EnsureFromList(module, importedModule,
                                     fromList, deferredImports)
+            
             elif op == IMPORT_FROM and topLevel:
                 if is3:
                     op = code[opIndex]
                     storeName = deferredCaller is not module
                 if storeName:
                     module.globalNames[name] = None
+            
             elif op == IMPORT_STAR and topLevel and importedModule is not None:
                 module.globalNames.update(importedModule.globalNames)
                 arguments = []
+            
             elif op not in (BUILD_LIST, INPLACE_ADD):
+                # The stack was used for something else, so we clear it.
                 if topLevel and op in STORE_OPS:
                     name = co.co_names[opArg]
                     module.globalNames[name] = None
                 arguments = []
+        
+        # Scan the code objects from function & class definitions
         for constant in co.co_consts:
             if isinstance(constant, type(co)):
                 self._ScanCode(constant, module, deferredImports,
         return module
 
     def ReportMissingModules(self):
+        """Display a list of modules that weren't found."""
         if self._badModules:
             sys.stdout.write("Missing modules:\n")
             names = list(self._badModules.keys())