Repository: pypa/pip
File: tests/lib/__init__.py (Python)

Code before:
            elif e.code:
                returncode = 1
            else:
                returncode = 0
        finally:
            sys.stdout = orig_stdout
        return InMemoryPipResult(returncode, stdout.getvalue())


class ScriptFactory(Protocol):
    def __call__(
        self,
        tmpdir: pathlib.Path,
        virtualenv: VirtualEnvironment | None = None,
        environ: dict[AnyStr, AnyStr] | None = None,
    ) -> PipTestEnvironment: ...


CertFactory = Callable[[], str]

# -------------------------------------------------------------------------
# Accommodations for Windows path and URL changes in recent Python releases
# -------------------------------------------------------------------------

# versions containing fix/backport from https://github.com/python/cpython/pull/113563
# which changed the behavior of `urllib.parse.urlun{parse,split}`
url = "////path/to/file"
has_new_urlun_behavior = url == urlunparse(urlparse(url))

# the above change seems to only impact tests on Windows, so just add skips for that
skip_needs_new_urlun_behavior_win = pytest.mark.skipif(
    sys.platform != "win32" or not has_new_urlun_behavior,
    reason="testing windows behavior for newer CPython",
)

skip_needs_old_urlun_behavior_win = pytest.mark.skipif(
    sys.platform != "win32" or has_new_urlun_behavior,
    reason="testing windows behavior for older CPython",
)

# Trailing slashes are now preserved on Windows, matching POSIX behaviour.
# BPO: https://github.com/python/cpython/issues/126212
does_pathname2url_preserve_trailing_slash = pathname2url("C:/foo/").endswith("/")
skip_needs_new_pathname2url_trailing_slash_behavior_win = pytest.mark.skipif(
    sys.platform != "win32" or not does_pathname2url_preserve_trailing_slash,
    reason="testing windows (pathname2url) behavior for newer CPython",
)
skip_needs_old_pathname2url_trailing_slash_behavior_win = pytest.mark.skipif(
    sys.platform != "win32" or does_pathname2url_preserve_trailing_slash,
    reason="testing windows (pathname2url) behavior for older CPython",
)

Review comment (suggestion):
This detection didn't work correctly, but I suspect it didn't matter because the `has_new_urlun_behavior` value coincided.

Code after:
                returncode = 1
            else:
                returncode = 0
        finally:
            sys.stdout = orig_stdout
        return InMemoryPipResult(returncode, stdout.getvalue())


class ScriptFactory(Protocol):
    def __call__(
        self,
        tmpdir: pathlib.Path,
        virtualenv: VirtualEnvironment | None = None,
        environ: dict[AnyStr, AnyStr] | None = None,
    ) -> PipTestEnvironment: ...


CertFactory = Callable[[], str]

# -------------------------------------------------------------------------
# Accommodations for Windows path and URL changes in recent Python releases
# -------------------------------------------------------------------------

# Trailing slashes are now preserved on Windows, matching POSIX behaviour.
# BPO: https://github.com/python/cpython/issues/126212
does_pathname2url_preserve_trailing_slash = pathname2url("C:\\foo\\").endswith("/")
skip_needs_new_pathname2url_trailing_slash_behavior_win = pytest.mark.skipif(
    sys.platform != "win32" or not does_pathname2url_preserve_trailing_slash,
    reason="testing windows (pathname2url) behavior for newer CPython",
)
skip_needs_old_pathname2url_trailing_slash_behavior_win = pytest.mark.skipif(
    sys.platform != "win32" or does_pathname2url_preserve_trailing_slash,
    reason="testing windows (pathname2url) behavior for older CPython",
)