Vinzenz Feenstra has uploaded a new change for review.

Change subject: Revert "Remove contra productive _string_check"
......................................................................

Revert "Remove contra productive _string_check"

This reverts commit b06a6c4c8e61013b7461c6fc946c1530ae47584e.
The previous commit breaks the guest agent badly because the
string type is required to be unicode, this code previously was
converting it most of the times to unicode, however in some cases
it won't do so and causes the data to be an ascii string or even
replaces valid characters even though it should not.

The attempt for fixing this issue will be addressed by a new patch.

Change-Id: I47176ac60e069e7d557312d7047d9bc4b15ac768
Signed-off-by: Vinzenz Feenstra <[email protected]>
---
M ovirt-guest-agent/VirtIoChannel.py
1 file changed, 20 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-guest-agent 
refs/changes/99/18999/1

diff --git a/ovirt-guest-agent/VirtIoChannel.py 
b/ovirt-guest-agent/VirtIoChannel.py
index 5383b04..dd68668 100644
--- a/ovirt-guest-agent/VirtIoChannel.py
+++ b/ovirt-guest-agent/VirtIoChannel.py
@@ -19,6 +19,7 @@
 import os
 import platform
 import time
+import locale
 import unicodedata
 
 
@@ -39,6 +40,23 @@
     .union(set(range(0xE, 0x1F + 1)))\
     .union(set(range(0x7F, 0x84 + 1)))\
     .union(set(range(0x86, 0x9F + 1)))
+
+
+def _string_check(str):
+    """
+    This function tries to convert the given string to a valid representable
+    form. Normal and valid unicode strings should not fail this test. Invalid
+    encodings will fail this and might get characters replaced.
+    """
+    try:
+        str.encode(locale.getpreferredencoding(), 'strict')
+    except UnicodeError:
+        try:
+            return str.encode('ascii', 'replace')
+        except UnicodeError:
+            # unrepresentable string
+            return unicode()
+    return unicode(str)
 
 
 def _filter_xml_chars(u):
@@ -76,7 +94,7 @@
 
 def _filter_object(obj):
     """
-    Apply _filter_xml_chars on all strings in the given
+    Apply _filter_xml_chars and _string_check on all strings in the given
     object
     """
     def filt(o):
@@ -87,7 +105,7 @@
         if isinstance(o, tuple):
             return tuple(map(filt, o))
         if isinstance(o, basestring):
-            return _filter_xml_chars(o)
+            return _filter_xml_chars(_string_check(o))
         return o
 
     return filt(obj)


-- 
To view, visit http://gerrit.ovirt.org/18999
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I47176ac60e069e7d557312d7047d9bc4b15ac768
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-guest-agent
Gerrit-Branch: master
Gerrit-Owner: Vinzenz Feenstra <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to