Skip to content
Myr

Merge pull request #1050 from giovannipizzi/fix-fast-edit

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

fix edit(require_save=True) for fast editors

authored by David Lord · committed by GitHub · parents 77cb257, 9d4fc53 · Browse files · 3 files+35 added−21 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+35 added−21 removed

  • tests:+6 added−0 removed
  • other:+29 added−21 removed

3 files+35 added−21 removed

modified:src/click/_termui_impl.py+27 added-21 removed
472 unmodified lines
473474 def edit(self, text):475 import tempfile476477 text = text or ""478 binary_data = type(text) in [bytes, bytearray]477 if not text:478 text = ""479480 if not binary_data and text and not text.endswith("\n"):481 text += "\n"480 is_bytes = isinstance(text, (bytes, bytearray))481482 if not is_bytes:483 if text and not text.endswith("\n"):484 text += "\n"485486 if WIN:487 text = text.replace("\n", "\r\n").encode("utf-8-sig")488 else:489 text = text.encode("utf-8")490491 fd, name = tempfile.mkstemp(prefix="editor-", suffix=self.extension)492493 try:485 if not binary_data:486 if WIN:487 encoding = "utf-8-sig"488 text = text.replace("\n", "\r\n")489 else:490 encoding = "utf-8"491 text = text.encode(encoding)494 with os.fdopen(fd, "wb") as f:495 f.write(text)496493 f = os.fdopen(fd, "wb")494 f.write(text)495 f.close()497 # If the filesystem resolution is 1 second, like Mac OS498 # 10.12 Extended, or 2 seconds, like FAT32, and the editor499 # closes very fast, require_save can fail. Set the modified500 # time to be 2 seconds in the past to work around this.501 os.utime(name, (os.path.getatime(name), os.path.getmtime(name) - 2))502 # Depending on the resolution, the exact value might not be503 # recorded, so get the new recorded value.504 timestamp = os.path.getmtime(name)505506 self.edit_file(name)507508 if self.require_save and os.path.getmtime(name) == timestamp:509 return None510503 f = open(name, "rb")504 try:511 with open(name, "rb") as f:512 rv = f.read()506 finally:507 f.close()508 if binary_data:513514 if is_bytes:515 return rv510 else:511 return rv.decode("utf-8-sig").replace("\r\n", "\n")516517 return rv.decode("utf-8-sig").replace("\r\n", "\n")518 finally:519 os.unlink(name)520521
152 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