Index: pymigemo/trunk/pymigemo.c
===================================================================
--- pymigemo/trunk/pymigemo.c	(revision 225)
+++ pymigemo/trunk/pymigemo.c	(revision 227)
@@ -48,7 +48,7 @@
 
 static bool
-get_encoding(char *encoding, size_t size, int charset)
-{
-    char *enc;
+get_encoding(unsigned char *encoding, size_t size, int charset)
+{
+    unsigned char *enc;
 
     switch(charset) {
@@ -74,8 +74,43 @@
 }
 
+static unsigned char *
+trans_string(PyObject *pystr, const unsigned char *encoding)
+{
+    unsigned char *str;
+
+    if (PyUnicode_Check(pystr)) {
+        PyObject *tmp = PyUnicode_AsEncodedString(pystr, encoding, "strict");
+
+        if (tmp == NULL) {
+            return NULL;
+        }
+
+        str = strdup(PyBytes_AS_STRING(tmp));
+        Py_DECREF(tmp);
+
+        if (str == NULL) {
+            PyErr_NoMemory();
+            return NULL;
+        }
+        return str;
+    }
+
+    if (PyBytes_Check(pystr)) {
+        if ((str = strdup(PyBytes_AS_STRING(pystr))) == NULL) {
+            PyErr_NoMemory();
+            return NULL;
+        }
+        return str;
+    }
+
+    PyErr_SetString(PyExc_TypeError, "argument must be string");
+    return NULL;
+}
+
 static int
-isloadable(const char *path)
+isloadable(const unsigned char *path)
 {
     struct stat st;
+
     int ret = 0;
     int fd  = open(path, O_RDONLY);
@@ -114,7 +149,5 @@
 Migemo_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-    Migemo *self;
-
-    self = (Migemo *)type->tp_alloc(type, 0);
+    Migemo *self = (Migemo *)type->tp_alloc(type, 0);
 
     if (self != NULL) {
@@ -128,6 +161,6 @@
 Migemo_init(Migemo *self, PyObject *args, PyObject *kwds)
 {
-    migemo *migemo_obj;
-    char   *dictionary;
+    migemo        *migemo_obj;
+    unsigned char *dictionary;
 
     static char *kwlist[] = {"dictionary", NULL};
@@ -141,5 +174,5 @@
 
         if (ret != 0) {
-            PyErr_SetString(PyExc_ValueError, strerror(ret));
+            PyErr_SetString(PyExc_OSError, strerror(ret));
             return -1;
         }
@@ -166,5 +199,5 @@
 Migemo_get_encoding(Migemo *self)
 {
-    char encoding[7];
+    unsigned char encoding[7];
 
     if (!get_encoding(encoding, sizeof(encoding), self->migemo_obj->charset)) {
@@ -173,5 +206,5 @@
     }
 
-    return PyBytes_FromString(encoding);
+    return PyUnicode_FromString(encoding);
 }
 
@@ -180,6 +213,5 @@
 {
     PyObject      *result, *pyquery, *pyrestr;
-    char          encoding[7], *query = NULL;
-    unsigned char *regex;
+    unsigned char *query, *regex, encoding[7];
 
     static char *kwlist[] = {"query", NULL};
@@ -194,27 +226,5 @@
     }
 
-    if (PyUnicode_Check(pyquery)) {
-        PyObject *q = PyUnicode_AsEncodedString(pyquery, encoding, "strict");
-
-        if (q == NULL) {
-            return NULL;
-        }
-
-        query = strdup(PyBytes_AS_STRING(q));
-        Py_DECREF(q);
-
-        if (query == NULL) {
-            return PyErr_NoMemory();
-        }
-    }
-    else if (PyBytes_Check(pyquery)) {
-        query = strdup(PyBytes_AS_STRING(pyquery));
-
-        if (query == NULL) {
-            return PyErr_NoMemory();
-        }
-    }
-    else {
-        PyErr_SetString(PyExc_ValueError, "argument must be string");
+    if ((query = trans_string(pyquery, encoding)) == NULL) {
         return NULL;
     }
@@ -241,18 +251,25 @@
 Migemo_set_operator(Migemo *self, PyObject *args, PyObject *kwds)
 {
-    PyObject *result = NULL;
-    char     *op;
-    int       index;
+    PyObject      *result, *pyop;
+    unsigned char *op, encoding[7];
+    int            index;
   
     static char *kwlist[] = {"index", "op", NULL};
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwds, "is", kwlist, &index, &op)) {
-        return NULL;
-    }
-
-    if (op) {
-        result = PyBool_FromLong((long)migemo_set_operator(self->migemo_obj, index, op));
-    }
-
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "iO", kwlist, &index, &pyop)) {
+        return NULL;
+    }
+
+    if (!get_encoding(encoding, sizeof(encoding), self->migemo_obj->charset)) {
+        PyErr_SetString(PyExc_AssertionError, "get_encoding() failed");
+        return NULL;
+    }
+
+    if ((op = trans_string(pyop, encoding)) == NULL) {
+        return NULL;
+    }
+
+    result = PyBool_FromLong((long)migemo_set_operator(self->migemo_obj, index, op));
+    free(op);
     return result;
 }
@@ -261,6 +278,7 @@
 Migemo_get_operator(Migemo *self, PyObject *args, PyObject *kwds)
 {
-    PyObject            *result = NULL;
+    PyObject            *result;
     const unsigned char *op;
+    unsigned char        encoding[7];
     int                  index;
   
@@ -271,12 +289,23 @@
     }
 
+    if (!get_encoding(encoding, sizeof(encoding), self->migemo_obj->charset)) {
+        PyErr_SetString(PyExc_AssertionError, "get_encoding() failed");
+        return NULL;
+    }
+
     if (op = migemo_get_operator(self->migemo_obj, index)) {
-        result = PyBytes_FromString(op);
-    }
-    else {
-        PyErr_SetString(PyExc_ValueError, "invalid opindex");
-    }
-
-    return result;
+        PyObject *tmp = PyBytes_FromString(op);
+
+        if (tmp == NULL) {
+            return NULL;
+        }
+
+        result = PyUnicode_FromEncodedObject(tmp, encoding, "strict");
+        Py_DECREF(tmp);
+        return result;
+    }
+
+    PyErr_SetString(PyExc_ValueError, "invalid opindex");
+    return NULL;
 }
 
@@ -284,7 +313,6 @@
 Migemo_load(Migemo *self, PyObject *args, PyObject *kwds)
 {
-    PyObject *result = NULL;
-    char     *dict_file;
-    int       dict_id;
+    unsigned char *dict_file;
+    int            dict_id;
   
     static char *kwlist[] = {"dict_id", "dict_file", NULL};
@@ -298,12 +326,13 @@
 
         if (ret != 0) {
-            PyErr_SetString(PyExc_ValueError, strerror(ret));
+            PyErr_SetString(PyExc_OSError, strerror(ret));
             return NULL;
         }
 
-        result = PyLong_FromLong((long)migemo_load(self->migemo_obj, dict_id, dict_file));
-    }
-
-    return result;
+        return PyLong_FromLong((long)migemo_load(self->migemo_obj, dict_id, dict_file));
+    }
+
+    PyErr_SetString(PyExc_AssertionError, "dict_file is NULL");
+    return NULL;
 }
 
@@ -316,10 +345,10 @@
 static PyMethodDef Migemo_methods[] = {
     {"query", (PyCFunction)Migemo_query, METH_VARARGS | METH_KEYWORDS,
-     "return regex from romaji string\n\
+     "return regex from string\n\
 \n\
 def query(query)\n\
-  query: romaji string (str or unicode)\n\
-\n\
-  returns: regex string as Unicode object"},
+  query: string\n\
+\n\
+  returns: regex string"},
     {"set_operator", (PyCFunction)Migemo_set_operator, METH_VARARGS | METH_KEYWORDS,
      "set operator string as the meta character of regex\n\
@@ -328,5 +357,5 @@
   index: (OPINDEX_NEST_IN|OPINDEX_NEST_OUT|OPINDEX_NEWLINE|\n\
           OPINDEX_OR|OPINDEX_SELECT_IN|OPINDEX_SELECT_OUT)\n\
-  op: operator string (str)\n\
+  op: operator string\n\
 \n\
   returns: boolean value"},
@@ -338,5 +367,5 @@
           OPINDEX_OR|OPINDEX_SELECT_IN|OPINDEX_SELECT_OUT)\n\
 \n\
-  returns: operator string (str)"},
+  returns: operator string"},
     {"load", (PyCFunction)Migemo_load, METH_VARARGS | METH_KEYWORDS,
      "add dictionary to Migemo object\n\
@@ -345,5 +374,5 @@
   dict_id: (DICTID_HAN2ZEN|DICTID_HIRA2KATA|DICTID_MIGEMO|\n\
             DICTID_ROMA2HIRA|DICTID_ZEN2HAN)\n\
-  dict_file: path to dictionary file (str)\n\
+  dict_file: path to dictionary file\n\
 \n\
   returns: ID of loaded dictionary"},
@@ -357,5 +386,5 @@
 \n\
 def get_encoding()\n\
-  returns: encoding string (str)"},
+  returns: encoding string"},
     {NULL} /* Sentinel */
 };
