Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: always use parameter UTF-8 fallback #1468

Merged
merged 2 commits into from Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions click/types.py
Expand Up @@ -119,6 +119,8 @@ def convert(self, value, param, ctx):
value = value.decode(fs_enc)
except UnicodeError:
value = value.decode('utf-8', 'replace')
else:
value = value.decode('utf-8', 'replace')
davidism marked this conversation as resolved.
Show resolved Hide resolved
return value
return value

Expand Down
23 changes: 22 additions & 1 deletion tests/test_arguments.py
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
import pytest
import click
from click._compat import PY2
import sys
from click._compat import PY2, text_type


def test_nargs_star(runner):
Expand Down Expand Up @@ -84,6 +85,26 @@ def copy(x):
assert 'Got unexpected extra argument (bar)' in result.output


def test_bytes_args(runner, monkeypatch):
@click.command()
@click.argument('arg')
def from_bytes(arg):
assert isinstance(arg, text_type), "UTF-8 encoded argument should be implicitly converted to Unicode"

# Simulate empty locale environment variables
if PY2:
monkeypatch.setattr(sys.stdin, 'encoding', 'ANSI_X3.4-1968')
monkeypatch.setattr(sys, 'getfilesystemencoding', lambda: 'ANSI_X3.4-1968')
monkeypatch.setattr(sys, 'getdefaultencoding', lambda: 'ascii')
else:
monkeypatch.setattr(sys.stdin, 'encoding', 'utf-8')
monkeypatch.setattr(sys, 'getfilesystemencoding', lambda: 'utf-8')
monkeypatch.setattr(sys, 'getdefaultencoding', lambda: 'utf-8')

runner.invoke(from_bytes, [u'Something outside of ASCII range: 林'.encode('UTF-8')],
catch_exceptions=False)


def test_file_args(runner):
@click.command()
@click.argument('input', type=click.File('rb'))
Expand Down