From 768cebe0e12249d1c3e23f06ecf111c2f346283c Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Sat, 27 Feb 2016 20:17:53 +0100 Subject: [PATCH] Fix coerce_native See #344 --- tests/cli/test_utils.py | 26 ++++++++++++++++++++++++++ vdirsyncer/cli/utils.py | 12 ++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 tests/cli/test_utils.py diff --git a/tests/cli/test_utils.py b/tests/cli/test_utils.py new file mode 100644 index 0000000..947fffa --- /dev/null +++ b/tests/cli/test_utils.py @@ -0,0 +1,26 @@ +from hypothesis import given +from hypothesis.strategies import ( + binary, + booleans, + complex_numbers, + floats, + integers, + none, + one_of, + text +) + +from vdirsyncer.cli.utils import coerce_native + + +@given(one_of( + binary(), + booleans(), + complex_numbers(), + floats(), + integers(), + none(), + text() +)) +def test_coerce_native_fuzzing(s): + coerce_native(s) diff --git a/vdirsyncer/cli/utils.py b/vdirsyncer/cli/utils.py index 3ea3ec4..f1dcc48 100644 --- a/vdirsyncer/cli/utils.py +++ b/vdirsyncer/cli/utils.py @@ -503,7 +503,15 @@ def assert_permissions(path, wanted): def coerce_native(x, encoding='utf-8'): + # XXX: Remove with Python 3 only try: - return to_native(x, encoding) + return str(x) except UnicodeError: - return repr(x) + pass + + try: + return to_native(x, encoding=encoding) + except UnicodeError: + pass + + return repr(x)