Skip to content
Myr

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

2 files+69 added−3 removed

modified:click/_compat.py+9 added-3 removed
481 unmodified lines
482 return get_text_stdin(encoding=encoding, errors=errors), False483484 # 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 = {}487488 if encoding is not None:489 open_kwargs['encoding'] = encoding490491 if errors is not None:492 open_kwargs['errors'] = errors493494 return codecs.open(filename, mode, **open_kwargs), True495496 # 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 lines
modified:tests/test_utils.py+60 added
288 unmodified lines
289 with click.open_file(filename) as f:290 click.echo(f.read())291 click.echo('meep')292293 @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')299300 result = runner.invoke(cli, ['hello.txt'])301 assert result.exception is None302 assert result.output == 'Cool stuff\nmeep\n'303304 result = runner.invoke(cli, ['-'], input='foobar')305 assert result.exception is None306 assert result.output == 'foobar\nmeep\n'307308 # 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 None313314 result = runner.invoke(cli, ['-'], input=random_input)315 assert isinstance(result.exception, UnicodeDecodeError)316317318def test_click_open_file_respects_ignore(runner):319 file_name = "hello_world.txt"320321 with runner.isolated_filesystem():322 with open(file_name, "w") as f:323 f.write("Hello world!")324325 click_wrapper = click.utils.open_file(file_name, encoding="UTF-8", errors="ignore")326 assert click_wrapper.errors == "ignore"327328329@pytest.mark.parametrize("ignore", [330 True,331 False332])333def test_click_open_file_ignore_invalid_utf8(ignore, runner):334 file_name = "invalid_utf8.txt"335336 with runner.isolated_filesystem():337 with open(file_name, "wb") as f:338 f.write(b'\xe2\x28\xa1')339340 open_kwargs = {"encoding": "UTF-8"}341342 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()350351352def test_click_open_file_ignore_no_encoding_provided(runner):353 file_name = "random_input.bin"354355 with runner.isolated_filesystem():356 with open(file_name, "wb") as f:357 f.write(os.urandom(64))358359 click.utils.open_file(file_name, errors="ignore").read()360361362@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