From c51068cd1864d995e81ee5fd248b8a2027e56c35 Mon Sep 17 00:00:00 2001 From: Padraic Harley Date: Fri, 18 Dec 2015 19:33:32 +0000 Subject: Fix unidecode bytes error on python2 `bytes` takes no additional args in python2 unlike it's taking an encoding in python3. Unidecode returns a unicode string in python3 and a bytestring in python2 which, I believe, was the main cause of error #71. Now, regardless of whether unidecode is included, all strings passing through http_quote will be encoded to ascii which should fix both issues. Also included is a fix for a failing test when unidecode is used. Unidecode's `_unidecode` function ignores characters greater than 0xefff, which '\xe2\x99\xa5' (the heart symbol) is. This caused users with unidecode to fail '.pdf' was produced rather than the expected '?.pdf'. --- wkhtmltopdf/utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'wkhtmltopdf/utils.py') diff --git a/wkhtmltopdf/utils.py b/wkhtmltopdf/utils.py index 3d125bb..f002066 100644 --- a/wkhtmltopdf/utils.py +++ b/wkhtmltopdf/utils.py @@ -186,9 +186,11 @@ def http_quote(string): if isinstance(string, six.text_type): try: import unidecode - string = bytes(unidecode.unidecode(string), 'ascii') except ImportError: - string = string.encode('ascii', 'replace') + pass + else: + string = unidecode.unidecode(string) + string = string.encode('ascii', 'replace') # Wrap in double-quotes for ; , and the like string = string.replace(b'\\', b'\\\\').replace(b'"', b'\\"') return '"{0!s}"'.format(string.decode()) -- cgit v1.2.3