Skip to content
Myr

Do not trigger a `BytesWarning` under `python -bb`

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Closes #2877

authored by Kevin Deldycke · committed by Kevin Deldycke · parent df2e5ed · Browse files · 2 files+27 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+27 added−1 removed

  • tests:+25 added−0 removed
  • other:+2 added−1 removed

2 files+27 added−1 removed

modified:src/click/types.py+2 added-1 removed
1102 unmodified lines
1103 ctx: Context | None,1104 ) -> str | bytes | os.PathLike[str]:1105 rv = value11061107 dash = b"-" if isinstance(rv, bytes) else "-"1107 is_dash = self.file_okay and self.allow_dash and rv in (b"-", "-")1108 is_dash = self.file_okay and self.allow_dash and rv == dash11091110 if not is_dash:1111 if self.resolve_path:1112 rv = os.path.realpath(rv)
263 unmodified lines
modified:tests/test_types.py+25 added
1import os.path2import pathlib3import platform4import subprocess5import sys6import tempfile78import pytest9
116 unmodified lines
126 assert result.exception is None127 assert result.return_value == expect128129130def test_path_dash_no_byteswarning():131 """Detecting the ``-`` dash sentinel must not compare ``bytes`` against132 ``str``, which raises a ``BytesWarning`` under ``python -bb``.133134 The warning is only emitted when the interpreter runs with ``-b``, so this135 has to be checked in a subprocess. ``-bb`` turns the warning into an error,136 so a clean exit means no mismatched comparison happened.137 """138 program = (139 "import click\n"140 "convert = click.Path(allow_dash=True).convert\n"141 "for value in ('-', '', b'-', b''):\n"142 " convert(value, None, None)\n"143 )144 result = subprocess.run(145 [sys.executable, "-bb", "-c", program],146 capture_output=True,147 text=True,148 )149 assert result.returncode == 0, result.stderr150 assert "BytesWarning" not in result.stderr151152153def _symlinks_supported():154 with tempfile.TemporaryDirectory(prefix="click-pytest-") as tempdir:155 target = os.path.join(tempdir, "target")156 open(target, "w").close()
154 unmodified lines