# Buggy code
def find_module(modulename):
    """Find the module named `modulename`.

    Returns the file path of the module, and the name of the enclosing
    package.
    """
    openfile = None
    glo, loc = globals(), locals()
    try:
        # Search for the module - inside its parent package, if any - using
        # standard import mechanics.
        if "." in modulename:
            packagename, name = modulename.rsplit(".", 1)
            package = __import__(packagename, glo, loc, ["__path__"])
            searchpath = package.__path__
        else:
            packagename, name = None, modulename
            searchpath = None  # "top-level search" in imp.find_module()
        openfile, pathname, _ = imp.find_module(name, searchpath)

        # Complain if this is a magic non-file module.
        if openfile is None and pathname is None:
            raise NoSource("module does not live in a file: %r" % modulename)

        # If `modulename` is actually a package, not a mere module, then we
        # pretend to be Python 2.7 and try running its __main__.py script.
        if openfile is None:
            packagename = modulename
            name = "__main__"
            package = __import__(packagename, glo, loc, ["__path__"])
            searchpath = package.__path__
            openfile, pathname, _ = imp.find_module(name, searchpath)
    except ImportError as err:
        raise NoSource(str(err))
    finally:
        if openfile:
            openfile.close()

    return pathname, packagename


# Traceback (AttributeError)
$ python --version
Python 3.5.6 :: Anaconda, Inc.

$ coverage --version
Coverage.py, version 4.5.1 with C extension
Documentation at https://coverage.readthedocs.io

$ cat foo.py
print(__name__)
print(__spec__.name)

$ python -m foo  # Behaves as expected
__main__
foo

$ coverage run -m foo  # Observe __spec__ is None
__main__
Traceback (most recent call last):
File "/nas/dft/ire/rhys/Build/liquidmetal-debt/foo.py", line 2, in <module>
print(__spec__.name)
AttributeError: 'NoneType' object has no attribute 'name'
Coverage.py warning: No data was collected. (no-data-collected)

# Fixed code
def find_module(modulename):
    """Find the module named `modulename`.

    Returns the file path of the module, the name of the enclosing
    package, and None (where a spec would have been).
    """
    openfile = None
    glo, loc = globals(), locals()
    try:
        # Search for the module - inside its parent package, if any - using
        # standard import mechanics.
        if "." in modulename:
            packagename, name = modulename.rsplit(".", 1)
            package = __import__(packagename, glo, loc, ["__path__"])
            searchpath = package.__path__
        else:
            packagename, name = None, modulename
            searchpath = None  # "top-level search" in imp.find_module()
        openfile, pathname, _ = imp.find_module(name, searchpath)

        # Complain if this is a magic non-file module.
        if openfile is None and pathname is None:
            raise NoSource("module does not live in a file: %r" % modulename)

        # If `modulename` is actually a package, not a mere module, then we
        # pretend to be Python 2.7 and try running its __main__.py script.
        if openfile is None:
            packagename = modulename
            name = "__main__"
            package = __import__(packagename, glo, loc, ["__path__"])
            searchpath = package.__path__
            openfile, pathname, _ = imp.find_module(name, searchpath)
    except ImportError as err:
        raise NoSource(str(err))
    finally:
        if openfile:
            openfile.close()

    return pathname, packagename, None