Merge remote-tracking branch 'origin/8.0.x'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parents 5d759e0, b4581b4 · Browse files · 8 files·+71 added−23 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+71 added−23 removed
- tests:+50 added−3 removed
- other:+21 added−20 removed
Provenance
- Commit
c3687bf- Tree
7d8d256- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent 1
5d759e0- Parent 2
b4581b4
8 files changed+71 added−23 removed
modified:src/click/core.py+1 added-12 removed
44 unmodified lines45 F = t.TypeVar("F", bound=t.Callable[..., t.Any])46 V = t.TypeVar("V")47 48 49-def _fast_exit(code: int) -> "te.NoReturn":50- """Low-level exit that skips Python's cleanup but speeds up exit by51- about 10ms for things like shell completion.52-53- :param code: Exit code.54- """55- sys.stdout.flush()56- sys.stderr.flush()57- os._exit(code)58-59-49 def _complete_visible_commands(50 ctx: "Context", incomplete: str51 ) -> t.Iterator[t.Tuple[str, "Command"]]:52 """List all the subcommands of a group that start with the1065 unmodified lines1118 1119 from .shell_completion import shell_complete1120 1121 rv = shell_complete(self, ctx_args, prog_name, complete_var, instruction)1133- _fast_exit(rv)1122+ sys.exit(rv)1123 1124 def __call__(self, *args: t.Any, **kwargs: t.Any) -> t.Any:1125 """Alias for :meth:`main`."""1126 return self.main(*args, **kwargs)1828 unmodified linesmodified:src/click/decorators.py+2 added-3 removed
338 unmodified lines339 message = _("%(prog)s, version %(version)s")340 341 if version is None and package_name is None:342 frame = inspect.currentframe()343- assert frame is not None344- assert frame.f_back is not None345- f_globals = frame.f_back.f_globals if frame is not None else None343+ f_back = frame.f_back if frame is not None else None344+ f_globals = f_back.f_globals if f_back is not None else None345 # break reference cycle346 # https://docs.python.org/3/library/inspect.html#the-interpreter-stack347 del frame348 88 unmodified linesmodified:src/click/types.py+13 added-4 removed
834 unmodified lines835 is_dash = self.file_okay and self.allow_dash and rv in (b"-", "-")836 837 if not is_dash:838 if self.resolve_path:839- # realpath on Windows Python < 3.8 doesn't resolve symlinks839+ # Get the absolute directory containing the path.840+ dir_ = os.path.dirname(os.path.abspath(rv))841+842+ # Resolve a symlink. realpath on Windows Python < 3.9843+ # doesn't resolve symlinks. This might return a relative844+ # path even if the path to the link is absolute.845 if os.path.islink(rv):846 rv = os.readlink(rv)847 848+ # Join dir_ with the resolved symlink. If the resolved849+ # path is relative, this will make it relative to the850+ # original containing directory. If it is absolute, this851+ # has no effect.843- rv = os.path.realpath(rv)852+ rv = os.path.join(dir_, rv)853 854 try:855 st = os.stat(rv)856 except OSError:22 unmodified lines879 ),880 param,881 ctx,882 )874- if self.writable and not os.access(value, os.W_OK):883+ if self.writable and not os.access(rv, os.W_OK):884 self.fail(885 _("{name} {filename!r} is not writable.").format(886 name=self.name.title(), filename=os.fsdecode(value)887 ),888 param,889 ctx,890 )882- if self.readable and not os.access(value, os.R_OK):891+ if self.readable and not os.access(rv, os.R_OK):892 self.fail(893 _("{name} {filename!r} is not readable.").format(894 name=self.name.title(), filename=os.fsdecode(value)895 ),166 unmodified linesmodified:tests/conftest.py+23 added
1+import os2+import shutil3+import tempfile4+5 import pytest6 7 from click.testing import CliRunner8 9 10 @pytest.fixture(scope="function")11 def runner(request):12 return CliRunner()13+14+15+def check_symlink_impl():16+ """This function checks if using symlinks is allowed17+ on the host machine"""18+ tempdir = tempfile.mkdtemp(prefix="click-")19+ test_pth = os.path.join(tempdir, "check_sym_impl")20+ sym_pth = os.path.join(tempdir, "link")21+ open(test_pth, "w").close()22+ rv = True23+ try:24+ os.symlink(test_pth, sym_pth)25+ except (NotImplementedError, OSError):26+ # Creating symlinks on Windows require elevated access.27+ # OSError is thrown if the function is called without it.28+ rv = False29+ finally:30+ shutil.rmtree(tempdir, ignore_errors=True)31+ return rvmodified:tests/test_shell_completion.py-3 removed
1-import sys2-1 import pytest2 3 from click.core import Argument4 from click.core import Command257 unmodified lines262 263 264 @pytest.fixture()265 def _patch_for_completion(monkeypatch):268- monkeypatch.setattr("click.core._fast_exit", sys.exit)266 monkeypatch.setattr(267 "click.shell_completion.BashComplete._check_version", lambda self: True268 )269 50 unmodified linesmodified:tests/test_types.py+27 added
1+import os.path2 import pathlib3 4 import pytest5+from conftest import check_symlink_impl6 7 import click8 9 91 unmodified lines101 )102 result = runner.invoke(cli, ["a/b/c.txt"], standalone_mode=False)103 assert result.exception is None104 assert result.return_value == expect105+106+107+@pytest.mark.skipif(not check_symlink_impl(), reason="symlink not allowed on device")108+@pytest.mark.parametrize(109+ ("sym_file", "abs_fun"),110+ [111+ (("relative_symlink",), os.path.basename),112+ (("test", "absolute_symlink"), lambda x: x),113+ ],114+)115+def test_symlink_resolution(tmpdir, sym_file, abs_fun):116+ """This test ensures symlinks are properly resolved by click"""117+ tempdir = str(tmpdir)118+ real_path = os.path.join(tempdir, "test_file")119+ sym_path = os.path.join(tempdir, *sym_file)120+121+ # create dirs and files122+ os.makedirs(os.path.join(tempdir, "test"), exist_ok=True)123+ open(real_path, "w").close()124+ os.symlink(abs_fun(real_path), sym_path)125+126+ # test127+ ctx = click.Context(click.Command("do_stuff"))128+ rv = click.Path(resolve_path=True).convert(sym_path, None, ctx)129+ assert rv == real_pathmodified:CHANGES.rst+4 added
19 unmodified lines20 - Empty ``default`` value is not shown for ``multiple=True``.21 :issue:`1969`22 - Fix shell completion for arguments that start with a forward slash23 such as absolute file paths. :issue:`1929`24+- ``Path`` type with ``resolve_path=True`` resolves relative symlinks25+ to be relative to the containing directory. :issue:`1921`26+- Completion does not skip Python's resource cleanup when exiting,27+ avoiding some unexpected warning output. :issue:`1738, 2017`28 29 30 Version 8.0.131 -------------889 unmodified linesmodified:README.rst+1 added-1 removed
24 unmodified lines25 .. code-block:: text26 27 $ pip install -U click28 29-.. _pip: https://pip.pypa.io/en/stable/quickstart/29+.. _pip: https://pip.pypa.io/en/stable/getting-started/30 31 32 A Simple Example33 ----------------47 unmodified lines