Skip to content
Myr

fix edit() require_save check for fast editors

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

If a fast editor is used and the filesystem only provides a resolution of 1 second, edit() returns None if the file is edited and saved very quickly.

authored by Giovanni Pizzi · committed by David Lord · parent 77cb257 · Browse files · 3 files+15 added−0 removed
This commit

State

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

Diff stats

3 files+15 added−0 removed

  • tests:+6 added−0 removed
  • other:+9 added−0 removed

3 files+15 added−0 removed

modified:src/click/_termui_impl.py+7 added
491 unmodified lines
492493 f = os.fdopen(fd, "wb")494 f.write(text)495 f.close()496 # If the filesystem resolution is 1 second, like Mac OS497 # 10.12 Extended, or 2 seconds, like FAT32, and the editor498 # closes very fast, require_save can fail. Set the modified499 # time to be 2 seconds in the past to work around this.500 os.utime(name, (os.path.getatime(name), os.path.getmtime(name) - 2))501 # Depending on the resolution, the exact value might not be502 # recorded, so get the new recorded value.503 timestamp = os.path.getmtime(name)504505 self.edit_file(name)506
168 unmodified lines
modified:tests/test_termui.py+6 added
377 unmodified lines
378 monkeypatch.setattr(click.termui, "_getchar", None)379380 with pytest.raises(exc):381 click.getchar()382383384@pytest.mark.skipif(platform.system() == "Windows", reason="No sed on Windows.")385def test_fast_edit(runner):386 result = click.edit("a\nb", editor="sed -i~ 's/$/Test/'")387 assert result == "aTest\nbTest\n"
modified:CHANGES.rst+2 added
39 unmodified lines
40- Help for boolean flags with ``show_default=True`` shows the flag41 name instead of ``True`` or ``False``. :issue:`1538`42- Non-string objects passed to ``style()`` and ``secho()`` will be43 converted to string. :pr:`1146`44- ``edit(require_save=True)`` will detect saves for editors that exit45 very fast on filesystems with 1 second resolution. :pr:`1050`464748Version 7.1.249-------------
635 unmodified lines