# Buggy code
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."""
    opIndex = 0
    arguments = []
    code = co.co_code
    numOps = len(code)
    is3 = sys.version_info[0] >= 3
    while opIndex < numOps:
        if is3:
            op = code[opIndex]
        else:
            op = ord(code[opIndex])
        opIndex += 1
        if op >= dis.HAVE_ARGUMENT:
            if is3:
                opArg = code[opIndex] + code[opIndex + 1] * 256
            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:
                relativeImportIndex, fromList = 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:
                    if (
                        fromList
                        and fromList != ("*",)
                        and importedModule.path is not None
                    ):
                        self._EnsureFromList(
                            module, importedModule, fromList, deferredImports
                        )

        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]
            storeName = True
            if deferredImports:
                deferredCaller, deferredPackage, deferredFromList = deferredImports[-1]
                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, topLevel=False)


# Traceback (IndexError)
#!python

[vagrant@localhost curator_source]$ python3.6 setup.py build_exe
running build_exe
Traceback (most recent call last):
File "setup.py", line 124, in <module>
executables = [curator_exe,curator_cli_exe,repomgr_exe]
File "/home/vagrant/.local/lib/python3.6/site-packages/cx_Freeze/dist.py", line 337, in setup
distutils.core.setup(**attrs)
File "/usr/local/lib/python3.6/distutils/core.py", line 148, in setup
dist.run_commands()
File "/usr/local/lib/python3.6/distutils/dist.py", line 955, in run_commands
self.run_command(cmd)
File "/usr/local/lib/python3.6/distutils/dist.py", line 974, in run_command
cmd_obj.run()
File "/home/vagrant/.local/lib/python3.6/site-packages/cx_Freeze/dist.py", line 207, in run
freezer.Freeze()
File "/home/vagrant/.local/lib/python3.6/site-packages/cx_Freeze/freezer.py", line 544, in Freeze
self.finder = self._GetModuleFinder()
File "/home/vagrant/.local/lib/python3.6/site-packages/cx_Freeze/freezer.py", line 322, in _GetModuleFinder
self.path, self.replacePaths)
File "/home/vagrant/.local/lib/python3.6/site-packages/cx_Freeze/finder.py", line 150, in __init__
self._AddBaseModules()
File "/home/vagrant/.local/lib/python3.6/site-packages/cx_Freeze/finder.py", line 161, in _AddBaseModules
self.IncludeModule("traceback")
File "/home/vagrant/.local/lib/python3.6/site-packages/cx_Freeze/finder.py", line 650, in IncludeModule
namespace = namespace)
File "/home/vagrant/.local/lib/python3.6/site-packages/cx_Freeze/finder.py", line 309, in _ImportModule
deferredImports, namespace = namespace)
File "/home/vagrant/.local/lib/python3.6/site-packages/cx_Freeze/finder.py", line 402, in _InternalImportModule
parentModule, namespace)
File "/home/vagrant/.local/lib/python3.6/site-packages/cx_Freeze/finder.py", line 473, in _LoadModule
self._ScanCode(module.code, module, deferredImports)
File "/home/vagrant/.local/lib/python3.6/site-packages/cx_Freeze/finder.py", line 561, in _ScanCode
arguments.append(co.co_consts[opArg])
IndexError: tuple index out of range

# Fixed code
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."""
    arguments = []
    importedModule = None
    method = (
        dis._unpack_opargs if sys.version_info[:2] >= (3, 5) else self._UnpackOpArgs
    )
    for opIndex, op, opArg in method(co.co_code):
        # keep track of constants (these are used for importing)
        # immediately restart loop so arguments are retained
        if op == LOAD_CONST:
            arguments.append(co.co_consts[opArg])
            continue

        # import statement: attempt to import module
        elif op == IMPORT_NAME:
            name = co.co_names[opArg]
            if len(arguments) >= 2:
                relativeImportIndex, fromList = arguments[-2:]
            else:
                relativeImportIndex = -1
                (fromList,) = arguments
            if name not in module.excludeNames:
                importedModule = self._ImportModule(
                    name, deferredImports, module, relativeImportIndex
                )
                if importedModule is not None:
                    if (
                        fromList
                        and fromList != ("*",)
                        and importedModule.path is not None
                    ):
                        self._EnsureFromList(
                            module, importedModule, fromList, deferredImports
                        )

        # import * statement: copy all global names
        elif op == IMPORT_STAR and topLevel and importedModule is not None:
            module.globalNames.update(importedModule.globalNames)

        # store operation: track only top level
        elif topLevel and op in STORE_OPS:
            name = co.co_names[opArg]
            module.globalNames[name] = None

        # reset arguments; these are only needed for import statements so
        # ignore them in all other cases!
        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, topLevel=False)