# Buggy code
def _resolve_pyvenv_canonical_python_binary(
    cls,
    real_binary,  # type: str
    maybe_venv_python_binary,  # type: str
):
    # type: (...) -> Optional[str]
    maybe_venv_python_binary = os.path.abspath(maybe_venv_python_binary)
    if not os.path.islink(maybe_venv_python_binary):
        return None

    home_dir = cls._find_pyvenv_home(maybe_venv_python_binary)
    if os.path.dirname(real_binary) != home_dir:
        return None

    while os.path.islink(maybe_venv_python_binary):
        resolved = os.readlink(maybe_venv_python_binary)
        if not os.path.isabs(resolved):
            resolved = os.path.abspath(
                os.path.join(os.path.dirname(maybe_venv_python_binary), resolved)
            )
        if os.path.dirname(resolved) == os.path.dirname(maybe_venv_python_binary):
            maybe_venv_python_binary = resolved
        else:
            # We've escaped the venv bin dir; so the last resolved link was the
            # canonical venv Python binary.
            #
            # For example, for:
            #   ./venv/bin/
            #     python -> python3.8
            #     python3 -> python3.8
            #     python3.8 -> /usr/bin/python3.8
            #
            # We want to resolve each of ./venv/bin/python{,3{,.8}} to the canonical
            # ./venv/bin/python3.8 which is the symlink that points to the home binary.
            break
    return maybe_venv_python_binary


# Traceback (ModuleNotFoundError)
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-req-build-kh23prp9/setup.py", line 59, in <module>
'Programming Language :: Python :: 3.8',
File "/root/.pex/pip.pex/aef609891d42d65c887d1aeee58c46f6713a7e49/.deps/setuptools/setuptools/__init__.py", line 169, in setup
return distutils.core.setup(**attrs)
File "/usr/lib/python3.7/distutils/core.py", line 148, in setup
dist.run_commands()
File "/usr/lib/python3.7/distutils/dist.py", line 966, in run_commands
self.run_command(cmd)
File "/usr/lib/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/root/.pex/pip.pex/aef609891d42d65c887d1aeee58c46f6713a7e49/.deps/wheel/wheel/bdist_wheel.py", line 228, in run
self.run_command('install')
File "/usr/lib/python3.7/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/usr/lib/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/root/.pex/pip.pex/aef609891d42d65c887d1aeee58c46f6713a7e49/.deps/setuptools/setuptools/command/install.py", line 65, in run
return orig.install.run(self)
File "/usr/lib/python3.7/distutils/command/install.py", line 601, in run
self.run_command(cmd_name)
File "/usr/lib/python3.7/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/usr/lib/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/root/.pex/pip.pex/aef609891d42d65c887d1aeee58c46f6713a7e49/.deps/setuptools/setuptools/command/install_scripts.py", line 22, in run
import setuptools.command.easy_install as ei  # vendor:skip
File "/root/.pex/pip.pex/aef609891d42d65c887d1aeee58c46f6713a7e49/.deps/setuptools/setuptools/command/easy_install.py", line 69, in <module>
from setuptools.sandbox import run_setup  # vendor:skip
File "/root/.pex/pip.pex/aef609891d42d65c887d1aeee58c46f6713a7e49/.deps/setuptools/setuptools/sandbox.py", line 24, in <module>
import pkg_resources.py31compat  # vendor:skip
ModuleNotFoundError: No module named 'pkg_resources.py31compat'
----------------------------------------
ERROR: Failed building wheel for boto3

# Fixed code
def _resolve_pyvenv_canonical_python_binary(
    cls,
    real_binary,  # type: str
    maybe_venv_python_binary,  # type: str
):
    # type: (...) -> Optional[str]
    maybe_venv_python_binary = os.path.abspath(maybe_venv_python_binary)
    if not os.path.islink(maybe_venv_python_binary):
        return None

    pyvenv_cfg = cls._find_pyvenv_cfg(maybe_venv_python_binary)
    if pyvenv_cfg is None:
        return None

    while os.path.islink(maybe_venv_python_binary):
        resolved = os.readlink(maybe_venv_python_binary)
        if not os.path.isabs(resolved):
            resolved = os.path.abspath(
                os.path.join(os.path.dirname(maybe_venv_python_binary), resolved)
            )
        if os.path.dirname(resolved) == os.path.dirname(maybe_venv_python_binary):
            maybe_venv_python_binary = resolved
        else:
            # We've escaped the venv bin dir; so the last resolved link was the
            # canonical venv Python binary.
            #
            # For example, for:
            #   ./venv/bin/
            #     python -> python3.8
            #     python3 -> python3.8
            #     python3.8 -> /usr/bin/python3.8
            #
            # We want to resolve each of ./venv/bin/python{,3{,.8}} to the canonical
            # ./venv/bin/python3.8 which is the symlink that points to the home binary.
            break
    return maybe_venv_python_binary