|
|
38c882 |
From 3303a8a621467dd7be67cec211fe417e9c81946f Mon Sep 17 00:00:00 2001
|
|
|
38c882 |
From: Simon McVittie <smcv@debian.org>
|
|
|
38c882 |
Date: Fri, 27 Apr 2018 11:09:07 +0100
|
|
|
38c882 |
Subject: [PATCH] avahi-python: Encode unicode strings as UTF-8
|
|
|
38c882 |
MIME-Version: 1.0
|
|
|
38c882 |
Content-Type: text/plain; charset=UTF-8
|
|
|
38c882 |
Content-Transfer-Encoding: 8bit
|
|
|
38c882 |
|
|
|
38c882 |
Previously, we would effectively encode anything representable in
|
|
|
38c882 |
Latin-1 as Latin-1, and crash on anything not representable in Latin-1:
|
|
|
38c882 |
|
|
|
38c882 |
>>> import avahi
|
|
|
38c882 |
>>> avahi.string_to_byte_array(u'©')
|
|
|
38c882 |
[dbus.Byte(169)]
|
|
|
38c882 |
>>> avahi.string_to_byte_array(u'\ufeff')
|
|
|
38c882 |
Traceback (most recent call last):
|
|
|
38c882 |
File "<stdin>", line 1, in <module>
|
|
|
38c882 |
File "/usr/lib/python2.7/dist-packages/avahi/__init__.py", line 94, in string_to_byte_array
|
|
|
38c882 |
r.append(dbus.Byte(ord(c)))
|
|
|
38c882 |
ValueError: Integer outside range 0-255
|
|
|
38c882 |
|
|
|
38c882 |
This is particularly important for Python 3, where the str type
|
|
|
38c882 |
is a Unicode string.
|
|
|
38c882 |
|
|
|
38c882 |
The b'' syntax for bytestrings is supported since at least Python 2.7.
|
|
|
38c882 |
|
|
|
38c882 |
These functions now accept either Unicode strings (Python 2 unicode,
|
|
|
38c882 |
Python 3 str), which are encoded in UTF-8, or bytestrings
|
|
|
38c882 |
(Python 2 str, Python 3 bytes) which are taken as-is.
|
|
|
38c882 |
|
|
|
38c882 |
Signed-off-by: Simon McVittie <smcv@debian.org>
|
|
|
38c882 |
(cherry picked from commit 169e85dbc13dcaae8a699618883e512614f540b7)
|
|
|
38c882 |
|
|
|
38c882 |
Related: #1561019
|
|
|
38c882 |
---
|
|
|
38c882 |
avahi-python/avahi/__init__.py | 24 +++++++++++++++++++++---
|
|
|
38c882 |
1 file changed, 21 insertions(+), 3 deletions(-)
|
|
|
38c882 |
|
|
|
38c882 |
diff --git a/avahi-python/avahi/__init__.py b/avahi-python/avahi/__init__.py
|
|
|
38c882 |
index 7b45029..02305b0 100644
|
|
|
38c882 |
--- a/avahi-python/avahi/__init__.py
|
|
|
38c882 |
+++ b/avahi-python/avahi/__init__.py
|
|
|
38c882 |
@@ -17,6 +17,8 @@
|
|
|
38c882 |
|
|
|
38c882 |
# Some definitions matching those in avahi-common/defs.h
|
|
|
38c882 |
|
|
|
38c882 |
+import sys
|
|
|
38c882 |
+
|
|
|
38c882 |
import dbus
|
|
|
38c882 |
|
|
|
38c882 |
SERVER_INVALID, SERVER_REGISTERING, SERVER_RUNNING, SERVER_COLLISION, SERVER_FAILURE = range(0, 5)
|
|
|
38c882 |
@@ -66,6 +68,9 @@ DBUS_INTERFACE_HOST_NAME_RESOLVER = DBUS_NAME + ".HostNameResolver"
|
|
|
38c882 |
DBUS_INTERFACE_SERVICE_RESOLVER = DBUS_NAME + ".ServiceResolver"
|
|
|
38c882 |
DBUS_INTERFACE_RECORD_BROWSER = DBUS_NAME + ".RecordBrowser"
|
|
|
38c882 |
|
|
|
38c882 |
+if sys.version_info[0] >= 3:
|
|
|
38c882 |
+ unicode = str
|
|
|
38c882 |
+
|
|
|
38c882 |
def byte_array_to_string(s):
|
|
|
38c882 |
r = ""
|
|
|
38c882 |
|
|
|
38c882 |
@@ -86,12 +91,19 @@ def txt_array_to_string_array(t):
|
|
|
38c882 |
|
|
|
38c882 |
return l
|
|
|
38c882 |
|
|
|
38c882 |
-
|
|
|
38c882 |
def string_to_byte_array(s):
|
|
|
38c882 |
+ if isinstance(s, unicode):
|
|
|
38c882 |
+ s = s.encode('utf-8')
|
|
|
38c882 |
+
|
|
|
38c882 |
r = []
|
|
|
38c882 |
|
|
|
38c882 |
for c in s:
|
|
|
38c882 |
- r.append(dbus.Byte(ord(c)))
|
|
|
38c882 |
+ if isinstance(c, int):
|
|
|
38c882 |
+ # Python 3: iterating over bytes yields ints
|
|
|
38c882 |
+ r.append(dbus.Byte(c))
|
|
|
38c882 |
+ else:
|
|
|
38c882 |
+ # Python 2: iterating over str yields str
|
|
|
38c882 |
+ r.append(dbus.Byte(ord(c)))
|
|
|
38c882 |
|
|
|
38c882 |
return r
|
|
|
38c882 |
|
|
|
38c882 |
@@ -107,6 +119,12 @@ def dict_to_txt_array(txt_dict):
|
|
|
38c882 |
l = []
|
|
|
38c882 |
|
|
|
38c882 |
for k,v in txt_dict.items():
|
|
|
38c882 |
- l.append(string_to_byte_array("%s=%s" % (k,v)))
|
|
|
38c882 |
+ if isinstance(k, unicode):
|
|
|
38c882 |
+ k = k.encode('utf-8')
|
|
|
38c882 |
+
|
|
|
38c882 |
+ if isinstance(v, unicode):
|
|
|
38c882 |
+ v = v.encode('utf-8')
|
|
|
38c882 |
+
|
|
|
38c882 |
+ l.append(string_to_byte_array(b"%s=%s" % (k,v)))
|
|
|
38c882 |
|
|
|
38c882 |
return l
|
|
|
38c882 |
--
|
|
|
38c882 |
2.14.3
|
|
|
38c882 |
|