summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon McVittie <smcv@debian.org>2020-04-28 12:52:41 +0100
committerSimon McVittie <smcv@debian.org>2020-04-28 13:09:17 +0100
commit227f4cedc2cbbaae8bcb3b421c0da8512993409f (patch)
treeede99692f02268abd983ea3eedb301a02e56509e
parent68776320108215228552fe93f5b1a0b828a656b4 (diff)
downloadlibproxy-git-227f4cedc2cbbaae8bcb3b421c0da8512993409f.tar.gz
python: Convert URLs to/from UTF-8 under Python 3
In Python 3, strings are Unicode (UTF-16 or UTF-32 depending on build options), and are passed to ctypes function wrappers as such. Convert the input URL to a bytestring for processing by libproxy, and convert the results back into (Unicode) strings by decoding them. From the pacrunner_webkit module, it appears that libproxy expects strings to be in UTF-8 (like GLib/GTK), so use that encoding for bytestrings. Fixes: #65
-rw-r--r--bindings/python/libproxy.py26
1 files changed, 23 insertions, 3 deletions
diff --git a/bindings/python/libproxy.py b/bindings/python/libproxy.py
index c9b87c7..a29b33a 100644
--- a/bindings/python/libproxy.py
+++ b/bindings/python/libproxy.py
@@ -1,3 +1,4 @@
+# encoding: utf-8
###############################################################################
# libproxy - A library for proxy configuration
# Copyright (C) 2006 Nathaniel McCallum <nathaniel@natemccallum.com>
@@ -108,9 +109,22 @@ class ProxyFactory(object):
"""
if type(url) != str:
raise TypeError("url must be a string!")
-
+
+ if type(url) is bytes:
+ # Python 2: str is bytes
+ url_bytes = url
+ else:
+ # Python 3: str is unicode
+ # TODO: Does this need to be encoded from IRI to ASCII (ACE) URI,
+ # for example http://кц.рф/пример ->
+ # http://xn--j1ay.xn--p1ai/%D0%BF%D1%80%D0%B8%D0%BC%D0%B5%D1%80?
+ # Or is libproxy designed to accept IRIs like
+ # http://кц.рф/пример? Passing in an IRI does seem to work
+ # acceptably in practice, so do that for now.
+ url_bytes = url.encode('utf-8')
+
proxies = []
- array = _libproxy.px_proxy_factory_get_proxies(self._pf, url)
+ array = _libproxy.px_proxy_factory_get_proxies(self._pf, url_bytes)
if not bool(array):
raise ProxyFactory.ProxyResolutionError(
@@ -118,7 +132,13 @@ class ProxyFactory(object):
i=0
while array[i]:
- proxies.append(str(ctypes.cast(array[i], ctypes.c_char_p).value))
+ proxy_bytes = ctypes.cast(array[i], ctypes.c_char_p).value
+ if type(proxy_bytes) is str:
+ # Python 2
+ proxies.append(proxy_bytes)
+ else:
+ # Python 3
+ proxies.append(proxy_bytes.decode('utf-8', errors='replace'))
i += 1
_libproxy.px_proxy_factory_free_proxies(proxies)