# Buggy code
def qualname_from_frame(frame):
    """Get a qualified name for the code running in `frame`."""
    co = frame.f_code
    fname = co.co_name
    method = None
    if co.co_argcount and co.co_varnames[0] == "self":
        self = frame.f_locals["self"]
        method = getattr(self, fname, None)

    if method is None:
        func = frame.f_globals[fname]
        return func.__module__ + "." + fname

    func = getattr(method, "__func__", None)
    if func is None:
        cls = self.__class__
        return cls.__module__ + "." + cls.__name__ + "." + fname

    if hasattr(func, "__qualname__"):
        qname = func.__module__ + "." + func.__qualname__
    else:
        for cls in getattr(self.__class__, "__mro__", ()):
            f = cls.__dict__.get(fname, None)
            if f is None:
                continue
            if f is func:
                qname = cls.__module__ + "." + cls.__name__ + "." + fname
                break
        else:
            # Support for old-style classes.
            def mro(bases):
                for base in bases:
                    f = base.__dict__.get(fname, None)
                    if f is func:
                        return base.__module__ + "." + base.__name__ + "." + fname
                for base in bases:
                    qname = mro(base.__bases__)
                    if qname is not None:
                        return qname
                return None

            qname = mro([self.__class__])
            if qname is None:
                qname = func.__module__ + "." + fname

    return qname


# Traceback (KeyError)
Traceback (most recent call last):
File "/eggpath/p/pytest-3.1.0-py2.7.egg/_pytest/config.py", line 367, in _importconftest
mod = conftestpath.pyimport()
File "/eggpath/p/py-1.4.31-py2.7.egg/py/_path/local.py", line 650, in pyimport
__import__(modname)
File "/eggpath/p/pytest-3.1.0-py2.7.egg/_pytest/assertion/rewrite.py", line 216, in load_module
py.builtin.exec_(co, mod.__dict__)
File "/eggpath/p/py-1.4.31-py2.7.egg/py/_builtin.py", line 221, in exec_
exec2(obj, globals, locals)
File "<string>", line 7, in exec2
File "/workspace_path//test-path/tests/conftest.py", line 6, in <module>
import xxxx
...
...
File "/eggpath/t/tables-3.2.2-py2.7-linux-x86_64.egg/tables/__init__.py", line 123, in <module>
from tables.file import File, open_file, copy_file, openFile, copyFile
File "/eggpath/t/tables-3.2.2-py2.7-linux-x86_64.egg/tables/file.py", line 31, in <module>
import numexpr
File "/eggpath/n/numexpr-2.6.4-py2.7-linux-x86_64.egg/numexpr/__init__.py", line 42, in <module>
from numexpr.tests import test, print_versions
File "/eggpath/n/numexpr-2.6.4-py2.7-linux-x86_64.egg/numexpr/tests/__init__.py", line 11, in <module>
from numexpr.tests.test_numexpr import test, print_versions
File "/eggpath/p/pytest-3.1.0-py2.7.egg/_pytest/assertion/rewrite.py", line 216, in load_module
py.builtin.exec_(co, mod.__dict__)
File "/eggpath/p/py-1.4.31-py2.7.egg/py/_builtin.py", line 221, in exec_
exec2(obj, globals, locals)
File "<string>", line 7, in exec2
File "/eggpath/n/numexpr-2.6.4-py2.7-linux-x86_64.egg/numexpr/tests/test_numexpr.py", line 44, in <module>
class test_numexpr(TestCase):
File "/workspace_path//coveragepy/coverage/context.py", line 40, in should_start_context_test_function
return qualname_from_frame(frame)
File "/workspace_path//coveragepy/coverage/context.py", line 49, in qualname_from_frame
func = frame.f_globals[fname]
KeyError: 'test_numexpr'
ERROR: could not load /workspace_path//test-path/tests/conftest.py

# Fixed code
def qualname_from_frame(frame):
    """Get a qualified name for the code running in `frame`."""
    co = frame.f_code
    fname = co.co_name
    method = None
    if co.co_argcount and co.co_varnames[0] == "self":
        self = frame.f_locals["self"]
        method = getattr(self, fname, None)

    if method is None:
        func = frame.f_globals.get(fname)
        if func is None:
            return None
        return func.__module__ + "." + fname

    func = getattr(method, "__func__", None)
    if func is None:
        cls = self.__class__
        return cls.__module__ + "." + cls.__name__ + "." + fname

    if hasattr(func, "__qualname__"):
        qname = func.__module__ + "." + func.__qualname__
    else:
        for cls in getattr(self.__class__, "__mro__", ()):
            f = cls.__dict__.get(fname, None)
            if f is None:
                continue
            if f is func:
                qname = cls.__module__ + "." + cls.__name__ + "." + fname
                break
        else:
            # Support for old-style classes.
            def mro(bases):
                for base in bases:
                    f = base.__dict__.get(fname, None)
                    if f is func:
                        return base.__module__ + "." + base.__name__ + "." + fname
                for base in bases:
                    qname = mro(base.__bases__)
                    if qname is not None:
                        return qname
                return None

            qname = mro([self.__class__])
            if qname is None:
                qname = func.__module__ + "." + fname

    return qname