libsoup3: update to 3.6.0; fix GTK2/3 app startup
[oi-userland.git] / components / network / avahi / patches / 21-py3-unicode-strings.patch
bloba4bb34029964b77f53f8922796ad46573f1fc63f
1 From 169e85dbc13dcaae8a699618883e512614f540b7 Mon Sep 17 00:00:00 2001
2 From: Simon McVittie <smcv@debian.org>
3 Date: Fri, 27 Apr 2018 11:09:07 +0100
4 Subject: [PATCH] avahi-python: Encode unicode strings as UTF-8
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 Previously, we would effectively encode anything representable in
10 Latin-1 as Latin-1, and crash on anything not representable in Latin-1:
12 >>> import avahi
13 >>> avahi.string_to_byte_array(u'©')
14 [dbus.Byte(169)]
15 >>> avahi.string_to_byte_array(u'\ufeff')
16 Traceback (most recent call last):
17 File "<stdin>", line 1, in <module>
18 File "/usr/lib/python2.7/dist-packages/avahi/__init__.py", line 94, in string_to_byte_array
19 r.append(dbus.Byte(ord(c)))
20 ValueError: Integer outside range 0-255
22 This is particularly important for Python 3, where the str type
23 is a Unicode string.
25 The b'' syntax for bytestrings is supported since at least Python 2.7.
27 These functions now accept either Unicode strings (Python 2 unicode,
28 Python 3 str), which are encoded in UTF-8, or bytestrings
29 (Python 2 str, Python 3 bytes) which are taken as-is.
31 Signed-off-by: Simon McVittie <smcv@debian.org>
32 ---
33 avahi-python/avahi/__init__.py | 24 +++++++++++++++++++++---
34 1 file changed, 21 insertions(+), 3 deletions(-)
36 diff --git a/avahi-python/avahi/__init__.py b/avahi-python/avahi/__init__.py
37 index 7b450293..02305b02 100644
38 --- a/avahi-python/avahi/__init__.py
39 +++ b/avahi-python/avahi/__init__.py
40 @@ -17,6 +17,8 @@
42 # Some definitions matching those in avahi-common/defs.h
44 +import sys
46 import dbus
48 SERVER_INVALID, SERVER_REGISTERING, SERVER_RUNNING, SERVER_COLLISION, SERVER_FAILURE = range(0, 5)
49 @@ -66,6 +68,9 @@
50 DBUS_INTERFACE_SERVICE_RESOLVER = DBUS_NAME + ".ServiceResolver"
51 DBUS_INTERFACE_RECORD_BROWSER = DBUS_NAME + ".RecordBrowser"
53 +if sys.version_info[0] >= 3:
54 + unicode = str
56 def byte_array_to_string(s):
57 r = ""
59 @@ -86,12 +91,19 @@ def txt_array_to_string_array(t):
61 return l
64 def string_to_byte_array(s):
65 + if isinstance(s, unicode):
66 + s = s.encode('utf-8')
68 r = []
70 for c in s:
71 - r.append(dbus.Byte(ord(c)))
72 + if isinstance(c, int):
73 + # Python 3: iterating over bytes yields ints
74 + r.append(dbus.Byte(c))
75 + else:
76 + # Python 2: iterating over str yields str
77 + r.append(dbus.Byte(ord(c)))
79 return r
81 @@ -107,6 +119,12 @@ def dict_to_txt_array(txt_dict):
82 l = []
84 for k,v in txt_dict.items():
85 - l.append(string_to_byte_array("%s=%s" % (k,v)))
86 + if isinstance(k, unicode):
87 + k = k.encode('utf-8')
89 + if isinstance(v, unicode):
90 + v = v.encode('utf-8')
92 + l.append(string_to_byte_array(b"%s=%s" % (k,v)))
94 return l