Changeset 34 for trunk


Ignore:
Timestamp:
11/06/10 03:15:51 (13 years ago)
Author:
atzm
Message:
  • fixed error handing
  • returns bool instead of int (set_operator(), is_enable())
Location:
trunk/pymigemo
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/pymigemo/README.ja_JP.UTF-8

    r30 r34  
    108108       * なし 
    109109     * 返り倀 
    110        * 真停倀 (int) : 正垞に読み蟌めおいる堎合に真 
     110       * 真停倀 : 正垞に読み蟌めおいる堎合に真 
    111111   * load(dict_id, dict_file) 
    112112     * Migemo オブゞェクトに蟞曞を远加で読み蟌む 
     
    131131       * op: メタ文字列 (str) 
    132132     * 返り倀 
    133        * 真停倀 (int) : 成功時に真 
     133       * 真停倀 : 成功時に真 
    134134 
    135135 
  • trunk/pymigemo/pymigemo.c

    r30 r34  
    99#include <structmember.h> 
    1010#include <migemo.h> 
     11#include <stdbool.h> 
    1112#include <string.h> 
    1213 
    13 #define PYMIGEMO_VERSION "0.2" 
     14#define PYMIGEMO_VERSION "0.3" 
    1415 
    1516/* for dereference migemo object members */ 
     
    3233} Migemo; 
    3334 
    34 static void 
    35 Migemo_dealloc(Migemo *self) 
    36 { 
    37     if (self->migemo_obj) { 
    38         migemo_close(self->migemo_obj); 
    39     } 
    40  
    41     self->ob_type->tp_free((PyObject *)self); 
    42 } 
    43  
    44 static PyObject * 
    45 Migemo_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 
    46 { 
    47     Migemo *self; 
    48  
    49     self = (Migemo *)type->tp_alloc(type, 0); 
    50  
    51     if (self != NULL) { 
    52         self->migemo_obj = NULL; 
    53     } 
    54  
    55     return (PyObject *)self; 
    56 } 
    57  
    58 static int 
    59 Migemo_init(Migemo *self, PyObject *args, PyObject *kwds) 
    60 { 
    61     migemo *migemo_obj; 
    62     char   *dictionary; 
    63  
    64     static char *kwlist[] = {"dictionary", NULL}; 
    65  
    66     if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &dictionary)) { 
    67         return -1; 
    68     } 
    69  
    70     if (dictionary) { 
    71         if (self->migemo_obj) { 
    72             migemo_close(self->migemo_obj); 
    73         } 
    74  
    75         migemo_obj = migemo_open(dictionary); 
    76  
    77         if (migemo_obj) { 
    78             self->migemo_obj = migemo_obj; 
    79         } 
    80         else { 
    81             return -1; 
    82         } 
    83     } 
    84  
    85     return 0; 
    86 } 
    87  
    88 static int 
     35static bool 
    8936get_encoding(char *encoding, size_t size, int charset) 
    9037{ 
     
    10754    if (strlen(enc) < size) { 
    10855        strcpy(encoding, enc); 
    109         return 1; 
     56        return true; 
     57    } 
     58 
     59    return false; 
     60} 
     61 
     62static void 
     63Migemo_dealloc(Migemo *self) 
     64{ 
     65    if (self->migemo_obj) { 
     66        migemo_close(self->migemo_obj); 
     67    } 
     68 
     69    self->ob_type->tp_free((PyObject *)self); 
     70} 
     71 
     72static PyObject * 
     73Migemo_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 
     74{ 
     75    Migemo *self; 
     76 
     77    self = (Migemo *)type->tp_alloc(type, 0); 
     78 
     79    if (self != NULL) { 
     80        self->migemo_obj = NULL; 
     81    } 
     82 
     83    return (PyObject *)self; 
     84} 
     85 
     86static int 
     87Migemo_init(Migemo *self, PyObject *args, PyObject *kwds) 
     88{ 
     89    migemo *migemo_obj; 
     90    char   *dictionary; 
     91 
     92    static char *kwlist[] = {"dictionary", NULL}; 
     93 
     94    if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &dictionary)) { 
     95        return -1; 
     96    } 
     97 
     98    if (dictionary) { 
     99        if (self->migemo_obj) { 
     100            migemo_close(self->migemo_obj); 
     101        } 
     102 
     103        migemo_obj = migemo_open(dictionary); 
     104 
     105        if (migemo_obj) { 
     106            self->migemo_obj = migemo_obj; 
     107        } 
     108        else { 
     109            PyErr_SetString(PyExc_AssertionError, "migemo_open() failed"); 
     110            return -1; 
     111        } 
    110112    } 
    111113 
     
    119121 
    120122    if (!get_encoding(encoding, sizeof(encoding), self->migemo_obj->charset)) { 
     123        PyErr_SetString(PyExc_AssertionError, "get_encoding() failed"); 
    121124        return NULL; 
    122125    } 
     
    128131Migemo_query(Migemo *self, PyObject *args, PyObject *kwds) 
    129132{ 
    130     PyObject      *result, *query_obj, *query_str = NULL, *regex_strobj = NULL; 
     133    PyObject      *result, *pyquery, *pyrestr; 
    131134    char          *query, encoding[7]; 
    132135    unsigned char *regex; 
     
    134137    static char *kwlist[] = {"query", NULL}; 
    135138 
    136     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &query_obj)) { 
     139    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &pyquery)) { 
    137140        return NULL; 
    138141    } 
    139142 
    140143    if (!get_encoding(encoding, sizeof(encoding), self->migemo_obj->charset)) { 
    141         return NULL; 
    142     } 
    143  
    144     if (PyUnicode_Check(query_obj)) { 
    145         query_str = PyUnicode_AsEncodedString(query_obj, encoding, "strict"); 
    146         query     = PyString_AS_STRING(query_str); 
    147     } 
    148     else if (PyString_Check(query_obj)) { 
    149         query = PyString_AS_STRING(query_obj); 
     144        PyErr_SetString(PyExc_AssertionError, "get_encoding() failed"); 
     145        return NULL; 
     146    } 
     147 
     148    if (PyUnicode_Check(pyquery)) { 
     149        PyObject *q = PyUnicode_AsEncodedString(pyquery, encoding, "strict"); 
     150 
     151        if (q == NULL) { 
     152            return NULL; 
     153        } 
     154 
     155        query = PyString_AS_STRING(q); 
     156        Py_DECREF(q); 
     157    } 
     158    else if (PyString_Check(pyquery)) { 
     159        query = PyString_AS_STRING(pyquery); 
    150160    } 
    151161    else { 
    152         return NULL; 
    153     } 
    154  
    155     if (query) { 
    156         regex = migemo_query(self->migemo_obj, query); 
    157  
    158         if (regex) { 
    159             regex_strobj = PyString_FromString(regex); 
    160  
    161             if (regex_strobj) { 
    162                 result = PyUnicode_FromEncodedObject(regex_strobj, encoding, "strict"); 
    163             } 
    164  
    165             migemo_release(self->migemo_obj, regex); 
    166         } 
    167     } 
    168  
    169     Py_XDECREF(regex_strobj); 
    170     Py_XDECREF(query_str); 
    171  
    172     if (!result) { 
    173         return NULL; 
    174     } 
    175  
     162        PyErr_SetString(PyExc_ValueError, "argument must be string"); 
     163        return NULL; 
     164    } 
     165    if (query == NULL) { 
     166        return NULL; 
     167    } 
     168 
     169    regex = migemo_query(self->migemo_obj, query); 
     170    if (regex == NULL) { 
     171        PyErr_SetString(PyExc_AssertionError, "migemo_query() failed"); 
     172        return NULL; 
     173    } 
     174 
     175    pyrestr = PyString_FromString(regex); 
     176    migemo_release(self->migemo_obj, regex); 
     177    if (pyrestr == NULL) { 
     178        return NULL; 
     179    } 
     180 
     181    result = PyUnicode_FromEncodedObject(pyrestr, encoding, "strict"); 
     182    Py_DECREF(pyrestr); 
    176183    return result; 
    177184} 
     
    191198 
    192199    if (op) { 
    193         result = PyInt_FromLong((long)migemo_set_operator(self->migemo_obj, index, op)); 
     200        result = PyBool_FromLong((long)migemo_set_operator(self->migemo_obj, index, op)); 
    194201    } 
    195202 
     
    204211Migemo_get_operator(Migemo *self, PyObject *args, PyObject *kwds) 
    205212{ 
    206     PyObject      *result; 
    207     unsigned char *op; 
    208     int            index; 
     213    PyObject            *result; 
     214    const unsigned char *op; 
     215    int                  index; 
    209216   
    210217    static char *kwlist[] = {"index", NULL}; 
     
    252259Migemo_is_enable(Migemo *self) 
    253260{ 
    254     return PyInt_FromLong((long)migemo_is_enable(self->migemo_obj)); 
     261    return PyBool_FromLong((long)migemo_is_enable(self->migemo_obj)); 
    255262} 
    256263 
Note: See TracChangeset for help on using the changeset viewer.