Skip to content
Myr

Merge pull request #1172 from russkel/bindata

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

click.edit accepts binary data (bytes)

authored by Dan Sully · committed by GitHub · parents 7d19964, 12ebfd3 · Browse files · 1 file+14 added−8 removed
This commit

State

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

Diff stats

1 file+14 added−8 removed

  • other:+14 added−8 removed

1 file+14 added−8 removed

modified:click/_termui_impl.py+14 added-8 removed
434 unmodified lines
435 def edit(self, text):436 import tempfile437438 text = text or ''439 binary_data = type(text) in [bytes, bytearray]440439 if text and not text.endswith('\n'):441 if not binary_data and text and not text.endswith('\n'):442 text += '\n'443444 fd, name = tempfile.mkstemp(prefix='editor-', suffix=self.extension)445 try:446 if not binary_data:444 if WIN:445 encoding = 'utf-8-sig'446 text = text.replace('\n', '\r\n')447 else:448 encoding = 'utf-8'449 text = text.encode(encoding)447 if WIN:448 encoding = 'utf-8-sig'449 text = text.replace('\n', '\r\n')450 else:451 encoding = 'utf-8'452 text = text.encode(encoding)453454 f = os.fdopen(fd, 'wb')455 f.write(text)456 f.close()
9 unmodified lines
466 try:467 rv = f.read()468 finally:469 f.close()470 if binary_data:471 return rv472 else:467 return rv.decode('utf-8-sig').replace('\r\n', '\n')473 return rv.decode('utf-8-sig').replace('\r\n', '\n')474 finally:475 os.unlink(name)476477
150 unmodified lines