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