open_file respects "errors" parameter
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Joe Ledger · committed by David Lord · parent 14fa50a · Browse files · 2 files·+69 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Signed-off-by: Joe Ledger <joewledger@gmail.com>
Diff stats
2 files+69 added−3 removed
- tests:+60 added−0 removed
- other:+9 added−3 removed
Provenance
- Commit
fb880d7- Tree
c771bad- Author
Joe Ledger <joewledger@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
14fa50a
2 files changed+69 added−3 removed
modified:click/_compat.py+9 added-3 removed
481 unmodified lines482 return get_text_stdin(encoding=encoding, errors=errors), False483 484 # Non-atomic writes directly go out through the regular open functions.485 if not atomic:486- if encoding is None:487- return open(filename, mode), True488- return io.open(filename, mode, encoding=encoding, errors=errors), True486+ open_kwargs = {}487 488+ if encoding is not None:489+ open_kwargs['encoding'] = encoding490+491+ if errors is not None:492+ open_kwargs['errors'] = errors493+494+ return codecs.open(filename, mode, **open_kwargs), True495+496 # Some usability stuff for atomic writes497 if 'a' in mode:498 raise ValueError(499 'Appending to an existing file is not supported, because that '251 unmodified linesmodified:tests/test_utils.py+60 added
288 unmodified lines289 with click.open_file(filename) as f:290 click.echo(f.read())291 click.echo('meep')292 293+ @click.command()294+ @click.argument('filename')295+ def cli_ignore_errors(filename):296+ with click.open_file(filename, errors="ignore") as f:297+ click.echo(f.read())298+ click.echo('meep')299+300 result = runner.invoke(cli, ['hello.txt'])301 assert result.exception is None302 assert result.output == 'Cool stuff\nmeep\n'303 304 result = runner.invoke(cli, ['-'], input='foobar')305 assert result.exception is None306 assert result.output == 'foobar\nmeep\n'307 308+ # Test that invalid unicode does not throw an exception when we ignore errors,309+ # but does throw an exception when we don't310+ random_input = os.urandom(64)311+ result = runner.invoke(cli_ignore_errors, ['-'], input=random_input)312+ assert result.exception is None313 314+ result = runner.invoke(cli, ['-'], input=random_input)315+ assert isinstance(result.exception, UnicodeDecodeError)316+317+318+def test_click_open_file_respects_ignore(runner):319+ file_name = "hello_world.txt"320+321+ with runner.isolated_filesystem():322+ with open(file_name, "w") as f:323+ f.write("Hello world!")324+325+ click_wrapper = click.utils.open_file(file_name, encoding="UTF-8", errors="ignore")326+ assert click_wrapper.errors == "ignore"327+328+329+@pytest.mark.parametrize("ignore", [330+ True,331+ False332+])333+def test_click_open_file_ignore_invalid_utf8(ignore, runner):334+ file_name = "invalid_utf8.txt"335+336+ with runner.isolated_filesystem():337+ with open(file_name, "wb") as f:338+ f.write(b'\xe2\x28\xa1')339+340+ open_kwargs = {"encoding": "UTF-8"}341+342+ if ignore:343+ # Here we ignore invalid UTF-8, so we should not throw an exception344+ open_kwargs["errors"] = "ignore"345+ click.utils.open_file(file_name, **open_kwargs).read()346+ else:347+ # Here we are not ignoring invalid UTF-8, so we expect a UnicodeDecodeError348+ with pytest.raises(UnicodeDecodeError):349+ click.utils.open_file(file_name, **open_kwargs).read()350+351+352+def test_click_open_file_ignore_no_encoding_provided(runner):353+ file_name = "random_input.bin"354+355+ with runner.isolated_filesystem():356+ with open(file_name, "wb") as f:357+ f.write(os.urandom(64))358+359+ click.utils.open_file(file_name, errors="ignore").read()360+361+362 @pytest.mark.skipif(WIN, reason='os.chmod() is not fully supported on Windows.')363 @pytest.mark.parametrize('permissions', [364 0o400,365 0o444,57 unmodified lines