From 212f49df08b4d057d763765b7f0e3e97a39dc3cc Mon Sep 17 00:00:00 2001 From: Jose Manuel Delicado Date: Wed, 27 Jul 2016 23:26:51 +0200 Subject: [PATCH] Patched urllib3 included in requests to avoid encoding audio filenames --- src/fixes/fix_urllib3_warnings.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/fixes/fix_urllib3_warnings.py b/src/fixes/fix_urllib3_warnings.py index 5df77334..71afa28a 100644 --- a/src/fixes/fix_urllib3_warnings.py +++ b/src/fixes/fix_urllib3_warnings.py @@ -1,5 +1,22 @@ # -*- coding: utf-8 -*- from requests.packages import urllib3 +from requests.packages.urllib3 import fields +import six def fix(): - urllib3.disable_warnings() \ No newline at end of file + urllib3.disable_warnings() + fields.format_header_param=patched_format_header_param + +def patched_format_header_param(name, value): + if not any(ch in value for ch in '"\\\r\n'): + result = '%s="%s"' % (name, value) + try: + result.encode('ascii') + except (UnicodeEncodeError, UnicodeDecodeError): + pass + else: + return result + if not six.PY3 and isinstance(value, six.text_type): # Python 2: + value = value.encode('utf-8') + value = '%s=%s' % (name, value) + return value