aboutsummaryrefslogtreecommitdiffhomepage
path: root/bindings/python/notmuch/globals.py
diff options
context:
space:
mode:
authorGravatar Martin Owens <doctormo@gmail.com>2011-09-16 13:19:14 +0200
committerGravatar Sebastian Spaeth <Sebastian@SSpaeth.de>2011-09-16 13:19:14 +0200
commit8e7a108363574ad3a342ed33a7c61c7dea65dc8a (patch)
tree752628cd22ae12e2adf12a4f9449fba4a01a3c06 /bindings/python/notmuch/globals.py
parentbdaee77e1b0853b0dab00565e4c5b6164248f85a (diff)
python: Ensure that we pass utf-8 encoded string to libnotmuch
If we use unicode objects, libnotmuch would not cope with null bytes in the byte array, so we need to make sure they are nicely formatted as utf-8. Introduce a helper function _str which does this throughout the code. Patch slightly modified by Sebastian Spaeth. Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
Diffstat (limited to 'bindings/python/notmuch/globals.py')
-rw-r--r--bindings/python/notmuch/globals.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/bindings/python/notmuch/globals.py b/bindings/python/notmuch/globals.py
index 77f2905b..05b9962a 100644
--- a/bindings/python/notmuch/globals.py
+++ b/bindings/python/notmuch/globals.py
@@ -98,3 +98,15 @@ class NotmuchError(Exception):
return self.args[0]
else:
return STATUS.status2str(self.args[1])
+
+def _str(value):
+ """Ensure a nicely utf-8 encoded string to pass to libnotmuch
+
+ C++ code expects strings to be well formatted and
+ unicode strings to have no null bytes."""
+ if not isinstance(value, basestring):
+ raise TypeError("Expected str or unicode, got %s" % str(type(value)))
+ if isinstance(value, unicode):
+ return value.encode('UTF-8')
+ return value
+