Index: trunk/pymigemo/pymigemo.c
===================================================================
--- trunk/pymigemo/pymigemo.c	(revision 30)
+++ trunk/pymigemo/pymigemo.c	(revision 34)
@@ -9,7 +9,8 @@
 #include <structmember.h>
 #include <migemo.h>
+#include <stdbool.h>
 #include <string.h>
 
-#define PYMIGEMO_VERSION "0.2"
+#define PYMIGEMO_VERSION "0.3"
 
 /* for dereference migemo object members */
@@ -32,59 +33,5 @@
 } Migemo;
 
-static void
-Migemo_dealloc(Migemo *self)
-{
-    if (self->migemo_obj) {
-        migemo_close(self->migemo_obj);
-    }
-
-    self->ob_type->tp_free((PyObject *)self);
-}
-
-static PyObject *
-Migemo_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
-{
-    Migemo *self;
-
-    self = (Migemo *)type->tp_alloc(type, 0);
-
-    if (self != NULL) {
-        self->migemo_obj = NULL;
-    }
-
-    return (PyObject *)self;
-}
-
-static int
-Migemo_init(Migemo *self, PyObject *args, PyObject *kwds)
-{
-    migemo *migemo_obj;
-    char   *dictionary;
-
-    static char *kwlist[] = {"dictionary", NULL};
-
-    if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &dictionary)) {
-        return -1;
-    }
-
-    if (dictionary) {
-        if (self->migemo_obj) {
-            migemo_close(self->migemo_obj);
-        }
-
-        migemo_obj = migemo_open(dictionary);
-
-        if (migemo_obj) {
-            self->migemo_obj = migemo_obj;
-        }
-        else {
-            return -1;
-        }
-    }
-
-    return 0;
-}
-
-static int
+static bool
 get_encoding(char *encoding, size_t size, int charset)
 {
@@ -107,5 +54,60 @@
     if (strlen(enc) < size) {
         strcpy(encoding, enc);
-        return 1;
+        return true;
+    }
+
+    return false;
+}
+
+static void
+Migemo_dealloc(Migemo *self)
+{
+    if (self->migemo_obj) {
+        migemo_close(self->migemo_obj);
+    }
+
+    self->ob_type->tp_free((PyObject *)self);
+}
+
+static PyObject *
+Migemo_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+    Migemo *self;
+
+    self = (Migemo *)type->tp_alloc(type, 0);
+
+    if (self != NULL) {
+        self->migemo_obj = NULL;
+    }
+
+    return (PyObject *)self;
+}
+
+static int
+Migemo_init(Migemo *self, PyObject *args, PyObject *kwds)
+{
+    migemo *migemo_obj;
+    char   *dictionary;
+
+    static char *kwlist[] = {"dictionary", NULL};
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &dictionary)) {
+        return -1;
+    }
+
+    if (dictionary) {
+        if (self->migemo_obj) {
+            migemo_close(self->migemo_obj);
+        }
+
+        migemo_obj = migemo_open(dictionary);
+
+        if (migemo_obj) {
+            self->migemo_obj = migemo_obj;
+        }
+        else {
+            PyErr_SetString(PyExc_AssertionError, "migemo_open() failed");
+            return -1;
+        }
     }
 
@@ -119,4 +121,5 @@
 
     if (!get_encoding(encoding, sizeof(encoding), self->migemo_obj->charset)) {
+        PyErr_SetString(PyExc_AssertionError, "get_encoding() failed");
         return NULL;
     }
@@ -128,5 +131,5 @@
 Migemo_query(Migemo *self, PyObject *args, PyObject *kwds)
 {
-    PyObject      *result, *query_obj, *query_str = NULL, *regex_strobj = NULL;
+    PyObject      *result, *pyquery, *pyrestr;
     char          *query, encoding[7];
     unsigned char *regex;
@@ -134,44 +137,48 @@
     static char *kwlist[] = {"query", NULL};
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &query_obj)) {
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &pyquery)) {
         return NULL;
     }
 
     if (!get_encoding(encoding, sizeof(encoding), self->migemo_obj->charset)) {
-        return NULL;
-    }
-
-    if (PyUnicode_Check(query_obj)) {
-        query_str = PyUnicode_AsEncodedString(query_obj, encoding, "strict");
-        query     = PyString_AS_STRING(query_str);
-    }
-    else if (PyString_Check(query_obj)) {
-        query = PyString_AS_STRING(query_obj);
+        PyErr_SetString(PyExc_AssertionError, "get_encoding() failed");
+        return NULL;
+    }
+
+    if (PyUnicode_Check(pyquery)) {
+        PyObject *q = PyUnicode_AsEncodedString(pyquery, encoding, "strict");
+
+        if (q == NULL) {
+            return NULL;
+        }
+
+        query = PyString_AS_STRING(q);
+        Py_DECREF(q);
+    }
+    else if (PyString_Check(pyquery)) {
+        query = PyString_AS_STRING(pyquery);
     }
     else {
-        return NULL;
-    }
-
-    if (query) {
-        regex = migemo_query(self->migemo_obj, query);
-
-        if (regex) {
-            regex_strobj = PyString_FromString(regex);
-
-            if (regex_strobj) {
-                result = PyUnicode_FromEncodedObject(regex_strobj, encoding, "strict");
-            }
-
-            migemo_release(self->migemo_obj, regex);
-        }
-    }
-
-    Py_XDECREF(regex_strobj);
-    Py_XDECREF(query_str);
-
-    if (!result) {
-        return NULL;
-    }
-
+        PyErr_SetString(PyExc_ValueError, "argument must be string");
+        return NULL;
+    }
+    if (query == NULL) {
+        return NULL;
+    }
+
+    regex = migemo_query(self->migemo_obj, query);
+    if (regex == NULL) {
+        PyErr_SetString(PyExc_AssertionError, "migemo_query() failed");
+        return NULL;
+    }
+
+    pyrestr = PyString_FromString(regex);
+    migemo_release(self->migemo_obj, regex);
+    if (pyrestr == NULL) {
+        return NULL;
+    }
+
+    result = PyUnicode_FromEncodedObject(pyrestr, encoding, "strict");
+    Py_DECREF(pyrestr);
     return result;
 }
@@ -191,5 +198,5 @@
 
     if (op) {
-        result = PyInt_FromLong((long)migemo_set_operator(self->migemo_obj, index, op));
+        result = PyBool_FromLong((long)migemo_set_operator(self->migemo_obj, index, op));
     }
 
@@ -204,7 +211,7 @@
 Migemo_get_operator(Migemo *self, PyObject *args, PyObject *kwds)
 {
-    PyObject      *result;
-    unsigned char *op;
-    int            index;
+    PyObject            *result;
+    const unsigned char *op;
+    int                  index;
   
     static char *kwlist[] = {"index", NULL};
@@ -252,5 +259,5 @@
 Migemo_is_enable(Migemo *self)
 {
-    return PyInt_FromLong((long)migemo_is_enable(self->migemo_obj));
+    return PyBool_FromLong((long)migemo_is_enable(self->migemo_obj));
 }
 
