# Buggy code
def forModule(cls, name):
    """
    Return an instance of this class representing the module of the given name. If the given
    module name is "__main__", it will be translated to the actual file name of the top-level
    script without the .py or .pyc extension. This method assumes that the module with the
    specified name has already been loaded.
    """
    module = sys.modules[name]
    filePath = os.path.abspath(module.__file__)
    filePath = filePath.split(os.path.sep)
    filePath[-1], extension = os.path.splitext(filePath[-1])
    assert extension in (".py", ".pyc")
    if name == "__main__":
        name = filePath.pop()
        dirPath = os.path.sep.join(filePath)
        cls._check_conflict(dirPath, name)
    else:
        for package in reversed(name.split(".")):
            dirPathTail = filePath.pop()
            assert dirPathTail == package
        dirPath = os.path.sep.join(filePath)

    return cls(dirPath=dirPath, name=name, extension=extension)


# Traceback (ImportError)
INFO:toil.batchSystems.mesos.batchSystem:Preparing to launch Mesos task 0 using offer 20151120-222317-83886090-5050-35148-O5...
INFO:toil.batchSystems.mesos.batchSystem:...launching Mesos task 0
WARNING:toil.leader:The jobWrapper seems to have left a log file, indicating failure: 46c1efec_0849_4ce3_b74d_c74f1f0602a4
WARNING:toil.leader:Reporting file: 46c1efec_0849_4ce3_b74d_c74f1f0602a4
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:       ---TOIL WORKER OUTPUT LOG---
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:       Traceback (most recent call last):
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:         File "/usr/local/lib/python2.7/dist-packages/toil/worker.py", line
266, in main
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:           job = Job._loadJob(jobWrapper.command, jobStore)
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:         File "/usr/local/lib/python2.7/dist-packages/toil/job.py", line 936, in _loadJob
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:           userModule = cls._loadUserModule(userModule)
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:         File "/usr/local/lib/python2.7/dist-packages/toil/job.py", line 914, in _loadUserModule
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:           return importlib.import_module(userModule.name)
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:         File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:           __import__(name)
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:         File "/tmp/tmpflu3Jl/5b3f3d5ba2a95124e308998df2ab7bb2/parallelMappingEvaluation.py", line 16, in <module>
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:           from toillib import *
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:       ImportError: No module named toillib
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:       Exiting the worker because of a failed jobWrapper on host c4agent1
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:       ERROR:toil.worker:Exiting the worker because of a failed jobWrapper on host c4agent1
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:       WARNING:toil.jobWrapper:Due to failure we are reducing the remaining retry count of job 46c1efec_0849_4ce3_b74d_c74f1f0602a4 to 0
WARNING:toil.leader:Job: 46c1efec_0849_4ce3_b74d_c74f1f0602a4 is completely failed

# Fixed code
def forModule(cls, name):
    """
    Return an instance of this class representing the module of the given name. If the given
    module name is "__main__", it will be translated to the actual file name of the top-level
    script without the .py or .pyc extension. This method assumes that the module with the
    specified name has already been loaded.
    """
    module = sys.modules[name]
    filePath = os.path.abspath(module.__file__)
    filePath = filePath.split(os.path.sep)
    filePath[-1], extension = os.path.splitext(filePath[-1])
    assert extension in (".py", ".pyc")
    if name == "__main__":
        if module.__package__:
            # invoked via python -m foo.bar
            name = [filePath.pop()]
            for package in reversed(module.__package__.split(".")):
                dirPathTail = filePath.pop()
                assert dirPathTail == package
                name.append(dirPathTail)
            name = ".".join(reversed(name))
            dirPath = os.path.sep.join(filePath)
        else:
            # invoked via python foo/bar.py
            name = filePath.pop()
            dirPath = os.path.sep.join(filePath)
            cls._check_conflict(dirPath, name)
    else:
        # imported as a module
        for package in reversed(name.split(".")):
            dirPathTail = filePath.pop()
            assert dirPathTail == package
        dirPath = os.path.sep.join(filePath)

    return cls(dirPath=dirPath, name=name)