Re: [feature request] emacs: use `notmuch insert` for FCC
[notmuch-archives.git] / 4d / 7db1b4648dd005f5c023451680e55b4fcfce10
1 Return-Path: <doctormo@gmail.com>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id B87E4431FD0\r
6         for <notmuch@notmuchmail.org>; Thu, 15 Sep 2011 10:41:16 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.799\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5\r
12         tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
13         FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id KyW6qRfeKQJb for <notmuch@notmuchmail.org>;\r
17         Thu, 15 Sep 2011 10:41:15 -0700 (PDT)\r
18 Received: from mail-qy0-f174.google.com (mail-qy0-f174.google.com\r
19         [209.85.216.174]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 02724431FB6\r
22         for <notmuch@notmuchmail.org>; Thu, 15 Sep 2011 10:41:14 -0700 (PDT)\r
23 Received: by qyk30 with SMTP id 30so5566718qyk.5\r
24         for <notmuch@notmuchmail.org>; Thu, 15 Sep 2011 10:41:14 -0700 (PDT)\r
25 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma;\r
26         h=subject:from:to:cc:in-reply-to:references:content-type:date\r
27         :message-id:mime-version:x-mailer;\r
28         bh=juxIYkA40P8ffh4j3FmgXR/aKSAEVBudgWnVX2PzXKw=;\r
29         b=wd2jTzBokDPLh7bvXThGYgnMJIZ7QmHnE8b6CL4V7fBgI0U/it/w2yKzFfJo4wzu3o\r
30         QcgLnRdyA+gfyDCJ0OorsgaA1OvMHKB2oO4ACtSQ/ZJKzri62JXW3FXa+uqpxjBiopzd\r
31         52/O2w4sGkTG1zJDziOvCF4G1rmyi1ovNPe7I=\r
32 Received: by 10.224.212.129 with SMTP id gs1mr1123495qab.104.1316108474355;\r
33         Thu, 15 Sep 2011 10:41:14 -0700 (PDT)\r
34 Received: from [192.168.1.190] (pool-68-163-190-45.bos.east.verizon.net.\r
35         [68.163.190.45])\r
36         by mx.google.com with ESMTPS id gz3sm8015793qab.18.2011.09.15.10.41.12\r
37         (version=SSLv3 cipher=OTHER); Thu, 15 Sep 2011 10:41:13 -0700 (PDT)\r
38 Subject: Python Unicode Was: Not much database creation error\r
39 From: Martin Owens <doctormo@gmail.com>\r
40 To: Sebastian Spaeth <Sebastian@SSpaeth.de>\r
41 In-Reply-To: <8739g7gnb4.fsf@SSpaeth.de>\r
42 References: <1315110898.32058.13.camel@delen>  <87vct7t7m4.fsf@SSpaeth.de>\r
43         <1315373219.32058.157.camel@delen>  <8739g7gnb4.fsf@SSpaeth.de>\r
44 Content-Type: multipart/mixed; boundary="=-0VlEcVbZa7gbgZ+lT5kJ"\r
45 Date: Thu, 15 Sep 2011 13:41:11 -0400\r
46 Message-ID: <1316108471.2201.24.camel@delen>\r
47 Mime-Version: 1.0\r
48 X-Mailer: Evolution 2.30.3 \r
49 Cc: Notmuch developer list <notmuch@notmuchmail.org>,\r
50         Paul Tagliamonte <tag@pault.ag>\r
51 X-BeenThere: notmuch@notmuchmail.org\r
52 X-Mailman-Version: 2.1.13\r
53 Precedence: list\r
54 List-Id: "Use and development of the notmuch mail system."\r
55         <notmuch.notmuchmail.org>\r
56 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
57         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
58 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
59 List-Post: <mailto:notmuch@notmuchmail.org>\r
60 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
61 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
62         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
63 X-List-Received-Date: Thu, 15 Sep 2011 17:41:16 -0000\r
64 \r
65 \r
66 --=-0VlEcVbZa7gbgZ+lT5kJ\r
67 Content-Type: text/plain; charset="UTF-8"\r
68 Content-Transfer-Encoding: 7bit\r
69 \r
70 Hello Sebastian,\r
71 \r
72 I've attached a diff for some proposed changes to help make dealing with\r
73 unicode and strings in the bindings more regular. I noticed some of the\r
74 methods were protected and others were not.\r
75 \r
76 Let me know.\r
77 \r
78 Best Regards, Martin Owens\r
79 \r
80 On Thu, 2011-09-08 at 15:45 +0200, Sebastian Spaeth wrote:\r
81 > I have to admit, I am not deep into the libnotmuch/xapian internals,\r
82 > so\r
83 > I might be the wrong person to ask (I just do some python bindings to\r
84 > whatever libnotmuch provides). \r
85 \r
86 --=-0VlEcVbZa7gbgZ+lT5kJ\r
87 Content-Disposition: attachment; filename="unicode.patch"\r
88 Content-Type: text/x-patch; name="unicode.patch"; charset="UTF-8"\r
89 Content-Transfer-Encoding: 7bit\r
90 \r
91 diff --git a/bindings/python/notmuch/database.py b/bindings/python/notmuch/database.py\r
92 index b070019..4f44de0 100644\r
93 --- a/bindings/python/notmuch/database.py\r
94 +++ b/bindings/python/notmuch/database.py\r
95 @@ -19,12 +19,11 @@ Copyright 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>'\r
96  \r
97  import os\r
98  from ctypes import c_int, c_char_p, c_void_p, c_uint, c_long, byref\r
99 -from notmuch.globals import nmlib, STATUS, NotmuchError, Enum\r
100 +from notmuch.globals import nmlib, STATUS, NotmuchError, Enum, _str\r
101  from notmuch.thread import Threads\r
102  from notmuch.message import Messages, Message\r
103  from notmuch.tag import Tags\r
104  \r
105 -\r
106  class Database(object):\r
107      """Represents a notmuch database (wraps notmuch_database_t)\r
108  \r
109 @@ -105,7 +104,6 @@ class Database(object):\r
110                  Database._std_db_path = self._get_user_default_db()\r
111              path = Database._std_db_path\r
112  \r
113 -        assert isinstance(path, basestring), 'Path must be a string or None.'\r
114          if create == False:\r
115              self.open(path, mode)\r
116          else:\r
117 @@ -136,7 +134,7 @@ class Database(object):\r
118              raise NotmuchError(message="Cannot create db, this Database() "\r
119                                         "already has an open one.")\r
120  \r
121 -        res = Database._create(path, Database.MODE.READ_WRITE)\r
122 +        res = Database._create(_str(path), Database.MODE.READ_WRITE)\r
123  \r
124          if res is None:\r
125              raise NotmuchError(\r
126 @@ -156,9 +154,7 @@ class Database(object):\r
127          :exception: Raises :exc:`NotmuchError` in case\r
128                      of any failure (after printing an error message on stderr).\r
129          """\r
130 -        if isinstance(path, unicode):\r
131 -            path = path.encode('utf-8') \r
132 -        res = Database._open(path, mode)\r
133 +        res = Database._open(_str(path), mode)\r
134  \r
135          if res is None:\r
136              raise NotmuchError(\r
137 @@ -263,12 +259,10 @@ class Database(object):\r
138              #we got a relative path, make it absolute\r
139              abs_dirpath = os.path.abspath(os.path.join(self.get_path(), path))\r
140  \r
141 -        if isinstance(path, unicode):\r
142 -            path = path.encode('UTF-8')\r
143 -        dir_p = Database._get_directory(self._db, path)\r
144 +        dir_p = Database._get_directory(self._db, _str(path))\r
145  \r
146          # return the Directory, init it with the absolute path\r
147 -        return Directory(abs_dirpath, dir_p, self)\r
148 +        return Directory(_str(abs_dirpath), dir_p, self)\r
149  \r
150      def add_message(self, filename, sync_maildir_flags=False):\r
151          """Adds a new message to the database\r
152 @@ -325,7 +319,7 @@ class Database(object):\r
153  \r
154          msg_p = c_void_p()\r
155          status = nmlib.notmuch_database_add_message(self._db,\r
156 -                                                  filename,\r
157 +                                                  _str(filename),\r
158                                                    byref(msg_p))\r
159  \r
160          if not status in [STATUS.SUCCESS, STATUS.DUPLICATE_MESSAGE_ID]:\r
161 @@ -394,7 +388,7 @@ class Database(object):\r
162          # Raise a NotmuchError if not initialized\r
163          self._verify_initialized_db()\r
164  \r
165 -        msg_p = Database._find_message(self._db, msgid)\r
166 +        msg_p = Database._find_message(self._db, _str(msgid))\r
167          if msg_p is None:\r
168              return None\r
169          return Message(msg_p, self)\r
170 @@ -404,8 +398,7 @@ class Database(object):\r
171             with this filename.\r
172          """\r
173          self._verify_initialized_db()\r
174 -\r
175 -        msg_p = Database._find_message_by_filename(self._db, filename)\r
176 +        msg_p = Database._find_message_by_filename(self._db, _str(filename))\r
177          return msg_p and Message(msg_p, self) or None\r
178  \r
179      def get_all_tags(self):\r
180 @@ -558,11 +551,8 @@ class Query(object):\r
181              raise NotmuchError(STATUS.NOT_INITIALIZED)\r
182          # create reference to parent db to keep it alive\r
183          self._db = db\r
184 -        if isinstance(querystr, unicode):\r
185 -            # xapian takes utf-8 encoded byte arrays\r
186 -            querystr = querystr.encode('utf-8')\r
187          # create query, return None if too little mem available\r
188 -        query_p = Query._create(db.db_p, querystr)\r
189 +        query_p = Query._create(db.db_p, _str(querystr))\r
190          if query_p is None:\r
191              NotmuchError(STATUS.NULL_POINTER)\r
192          self._query = query_p\r
193 diff --git a/bindings/python/notmuch/globals.py b/bindings/python/notmuch/globals.py\r
194 index 77f2905..5c81076 100644\r
195 --- a/bindings/python/notmuch/globals.py\r
196 +++ b/bindings/python/notmuch/globals.py\r
197 @@ -98,3 +98,13 @@ class NotmuchError(Exception):\r
198              return self.args[0]\r
199          else:\r
200              return STATUS.status2str(self.args[1])\r
201 +\r
202 +# C++ code expects strings to be well formatted and\r
203 +# unicode strings to have no null bytes.\r
204 +def _str(value):\r
205 +    if not isinstance(value, basestring):\r
206 +        raise TypeError("Expected str or unicode, got %s" % str(type(value)))\r
207 +    if isinstance(value, unicode):\r
208 +        return value.encode('UTF-8')\r
209 +    return str(value)\r
210 +\r
211 diff --git a/bindings/python/notmuch/message.py b/bindings/python/notmuch/message.py\r
212 index ae6ae1b..4f93a2a 100644\r
213 --- a/bindings/python/notmuch/message.py\r
214 +++ b/bindings/python/notmuch/message.py\r
215 @@ -21,7 +21,7 @@ Copyright 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>'\r
216  \r
217  from ctypes import c_char_p, c_void_p, c_long, c_uint, c_int\r
218  from datetime import date\r
219 -from notmuch.globals import nmlib, STATUS, NotmuchError, Enum\r
220 +from notmuch.globals import nmlib, STATUS, NotmuchError, Enum, _str\r
221  from notmuch.tag import Tags\r
222  from notmuch.filename import Filenames\r
223  import sys\r
224 @@ -505,7 +505,7 @@ class Message(object):\r
225          if self._msg is None:\r
226              raise NotmuchError(STATUS.NOT_INITIALIZED)\r
227  \r
228 -        status = nmlib.notmuch_message_add_tag(self._msg, tag)\r
229 +        status = nmlib.notmuch_message_add_tag(self._msg, _str(tag))\r
230  \r
231          # bail out on failure\r
232          if status != STATUS.SUCCESS:\r
233 @@ -549,7 +549,7 @@ class Message(object):\r
234          if self._msg is None:\r
235              raise NotmuchError(STATUS.NOT_INITIALIZED)\r
236  \r
237 -        status = nmlib.notmuch_message_remove_tag(self._msg, tag)\r
238 +        status = nmlib.notmuch_message_remove_tag(self._msg, _str(tag))\r
239          # bail out on error\r
240          if status != STATUS.SUCCESS:\r
241              raise NotmuchError(status)\r
242 \r
243 --=-0VlEcVbZa7gbgZ+lT5kJ--\r
244 \r