Changeset 227 for pymigemo/trunk
- Timestamp:
- 11/10/12 21:20:12 (12 years ago)
- Location:
- pymigemo/trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
pymigemo/trunk/ChangeLog
r226 r227 1 *pymigemo-0.4 (10 Nov 2012) 1 2 2012-11-10 Atzm WATANABE <atzm@atzm.org> 2 3 3 * pymigemo.c: Python3 APIsupport4 * pymigemo.c: Python3 support 4 5 5 6 *pymigemo-0.3 (07 Nov 2010) -
pymigemo/trunk/README.rst
r226 r227 34 34 |ãã²) 35 35 36 :meth:`Migemo.query` ã¡ãœããã«æååãæž¡ããšïŒæ£èŠè¡šçŸã unicode ã§è¿ããŸãïŒ 36 migemo-dict èŸæžãã¡ã€ã«ãšå 37 ±ã« :class:`Migemo` ã¯ã©ã¹ã®ãªããžã§ã¯ããçæãïŒ 38 :meth:`Migemo.query` ã¡ãœããã«æååãæž¡ããšïŒæ£èŠè¡šçŸãè¿ããŸãïŒ 37 39 ãã®æ£èŠè¡šçŸã :mod:`re` ãšçµã¿åãããã°æ€çŽ¢çã®çšéã«å©çšããããšãã§ããŸãïŒ 38 40 … … 55 57 .. method:: query(query) 56 58 57 *query* ããæ£èŠè¡šçŸã unicode ã§è¿ããŸãïŒ59 *query* ããæ£èŠè¡šçŸãè¿ããŸãïŒ 58 60 *query* 㯠unicode ã§ããšã³ã³ãŒããããæååã§ãæ§ããŸããïŒ 59 ãã ã *query* ã æååã®å ŽåïŒ61 ãã ã *query* ããšã³ã³ãŒããããæååã®å ŽåïŒ 60 62 ãã®ãšã³ã³ãŒãã£ã³ã°ãèŸæžã®ãšã³ã³ãŒãã£ã³ã°ãšç°ãªããšïŒ 61 63 ãšã©ãŒãšãªãå¯èœæ§ããããŸãïŒ … … 90 92 .. method:: get_encoding() 91 93 92 èªã¿èŸŒãã èŸæžã®ãšã³ã³ãŒãã£ã³ã°ã æååã§è¿ããŸãïŒ94 èªã¿èŸŒãã èŸæžã®ãšã³ã³ãŒãã£ã³ã°ãè¿ããŸãïŒ 93 95 äŸãã° EUC-JP ã§æžãããèŸæžã§ããã° ``'euc_jp'`` ãè¿ããŸãïŒ 94 96 ``'cp932'`` ã ``'euc_jp'`` ã ``'utf8'`` ã ``'ascii'`` -
pymigemo/trunk/pymigemo.c
r225 r227 48 48 49 49 static bool 50 get_encoding( char *encoding, size_t size, int charset)51 { 52 char *enc;50 get_encoding(unsigned char *encoding, size_t size, int charset) 51 { 52 unsigned char *enc; 53 53 54 54 switch(charset) { … … 74 74 } 75 75 76 static unsigned char * 77 trans_string(PyObject *pystr, const unsigned char *encoding) 78 { 79 unsigned char *str; 80 81 if (PyUnicode_Check(pystr)) { 82 PyObject *tmp = PyUnicode_AsEncodedString(pystr, encoding, "strict"); 83 84 if (tmp == NULL) { 85 return NULL; 86 } 87 88 str = strdup(PyBytes_AS_STRING(tmp)); 89 Py_DECREF(tmp); 90 91 if (str == NULL) { 92 PyErr_NoMemory(); 93 return NULL; 94 } 95 return str; 96 } 97 98 if (PyBytes_Check(pystr)) { 99 if ((str = strdup(PyBytes_AS_STRING(pystr))) == NULL) { 100 PyErr_NoMemory(); 101 return NULL; 102 } 103 return str; 104 } 105 106 PyErr_SetString(PyExc_TypeError, "argument must be string"); 107 return NULL; 108 } 109 76 110 static int 77 isloadable(const char *path)111 isloadable(const unsigned char *path) 78 112 { 79 113 struct stat st; 114 80 115 int ret = 0; 81 116 int fd = open(path, O_RDONLY); … … 114 149 Migemo_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 115 150 { 116 Migemo *self; 117 118 self = (Migemo *)type->tp_alloc(type, 0); 151 Migemo *self = (Migemo *)type->tp_alloc(type, 0); 119 152 120 153 if (self != NULL) { … … 128 161 Migemo_init(Migemo *self, PyObject *args, PyObject *kwds) 129 162 { 130 migemo *migemo_obj;131 char*dictionary;163 migemo *migemo_obj; 164 unsigned char *dictionary; 132 165 133 166 static char *kwlist[] = {"dictionary", NULL}; … … 141 174 142 175 if (ret != 0) { 143 PyErr_SetString(PyExc_ ValueError, strerror(ret));176 PyErr_SetString(PyExc_OSError, strerror(ret)); 144 177 return -1; 145 178 } … … 166 199 Migemo_get_encoding(Migemo *self) 167 200 { 168 char encoding[7];201 unsigned char encoding[7]; 169 202 170 203 if (!get_encoding(encoding, sizeof(encoding), self->migemo_obj->charset)) { … … 173 206 } 174 207 175 return Py Bytes_FromString(encoding);208 return PyUnicode_FromString(encoding); 176 209 } 177 210 … … 180 213 { 181 214 PyObject *result, *pyquery, *pyrestr; 182 char encoding[7], *query = NULL; 183 unsigned char *regex; 215 unsigned char *query, *regex, encoding[7]; 184 216 185 217 static char *kwlist[] = {"query", NULL}; … … 194 226 } 195 227 196 if (PyUnicode_Check(pyquery)) { 197 PyObject *q = PyUnicode_AsEncodedString(pyquery, encoding, "strict"); 198 199 if (q == NULL) { 200 return NULL; 201 } 202 203 query = strdup(PyBytes_AS_STRING(q)); 204 Py_DECREF(q); 205 206 if (query == NULL) { 207 return PyErr_NoMemory(); 208 } 209 } 210 else if (PyBytes_Check(pyquery)) { 211 query = strdup(PyBytes_AS_STRING(pyquery)); 212 213 if (query == NULL) { 214 return PyErr_NoMemory(); 215 } 216 } 217 else { 218 PyErr_SetString(PyExc_ValueError, "argument must be string"); 228 if ((query = trans_string(pyquery, encoding)) == NULL) { 219 229 return NULL; 220 230 } … … 241 251 Migemo_set_operator(Migemo *self, PyObject *args, PyObject *kwds) 242 252 { 243 PyObject *result = NULL;244 char *op;245 int index;253 PyObject *result, *pyop; 254 unsigned char *op, encoding[7]; 255 int index; 246 256 247 257 static char *kwlist[] = {"index", "op", NULL}; 248 258 249 if (!PyArg_ParseTupleAndKeywords(args, kwds, "is", kwlist, &index, &op)) { 250 return NULL; 251 } 252 253 if (op) { 254 result = PyBool_FromLong((long)migemo_set_operator(self->migemo_obj, index, op)); 255 } 256 259 if (!PyArg_ParseTupleAndKeywords(args, kwds, "iO", kwlist, &index, &pyop)) { 260 return NULL; 261 } 262 263 if (!get_encoding(encoding, sizeof(encoding), self->migemo_obj->charset)) { 264 PyErr_SetString(PyExc_AssertionError, "get_encoding() failed"); 265 return NULL; 266 } 267 268 if ((op = trans_string(pyop, encoding)) == NULL) { 269 return NULL; 270 } 271 272 result = PyBool_FromLong((long)migemo_set_operator(self->migemo_obj, index, op)); 273 free(op); 257 274 return result; 258 275 } … … 261 278 Migemo_get_operator(Migemo *self, PyObject *args, PyObject *kwds) 262 279 { 263 PyObject *result = NULL;280 PyObject *result; 264 281 const unsigned char *op; 282 unsigned char encoding[7]; 265 283 int index; 266 284 … … 271 289 } 272 290 291 if (!get_encoding(encoding, sizeof(encoding), self->migemo_obj->charset)) { 292 PyErr_SetString(PyExc_AssertionError, "get_encoding() failed"); 293 return NULL; 294 } 295 273 296 if (op = migemo_get_operator(self->migemo_obj, index)) { 274 result = PyBytes_FromString(op); 275 } 276 else { 277 PyErr_SetString(PyExc_ValueError, "invalid opindex"); 278 } 279 280 return result; 297 PyObject *tmp = PyBytes_FromString(op); 298 299 if (tmp == NULL) { 300 return NULL; 301 } 302 303 result = PyUnicode_FromEncodedObject(tmp, encoding, "strict"); 304 Py_DECREF(tmp); 305 return result; 306 } 307 308 PyErr_SetString(PyExc_ValueError, "invalid opindex"); 309 return NULL; 281 310 } 282 311 … … 284 313 Migemo_load(Migemo *self, PyObject *args, PyObject *kwds) 285 314 { 286 PyObject *result = NULL; 287 char *dict_file; 288 int dict_id; 315 unsigned char *dict_file; 316 int dict_id; 289 317 290 318 static char *kwlist[] = {"dict_id", "dict_file", NULL}; … … 298 326 299 327 if (ret != 0) { 300 PyErr_SetString(PyExc_ ValueError, strerror(ret));328 PyErr_SetString(PyExc_OSError, strerror(ret)); 301 329 return NULL; 302 330 } 303 331 304 result = PyLong_FromLong((long)migemo_load(self->migemo_obj, dict_id, dict_file)); 305 } 306 307 return result; 332 return PyLong_FromLong((long)migemo_load(self->migemo_obj, dict_id, dict_file)); 333 } 334 335 PyErr_SetString(PyExc_AssertionError, "dict_file is NULL"); 336 return NULL; 308 337 } 309 338 … … 316 345 static PyMethodDef Migemo_methods[] = { 317 346 {"query", (PyCFunction)Migemo_query, METH_VARARGS | METH_KEYWORDS, 318 "return regex from romajistring\n\347 "return regex from string\n\ 319 348 \n\ 320 349 def query(query)\n\ 321 query: romaji string (str or unicode)\n\322 \n\ 323 returns: regex string as Unicode object"},350 query: string\n\ 351 \n\ 352 returns: regex string"}, 324 353 {"set_operator", (PyCFunction)Migemo_set_operator, METH_VARARGS | METH_KEYWORDS, 325 354 "set operator string as the meta character of regex\n\ … … 328 357 index: (OPINDEX_NEST_IN|OPINDEX_NEST_OUT|OPINDEX_NEWLINE|\n\ 329 358 OPINDEX_OR|OPINDEX_SELECT_IN|OPINDEX_SELECT_OUT)\n\ 330 op: operator string (str)\n\359 op: operator string\n\ 331 360 \n\ 332 361 returns: boolean value"}, … … 338 367 OPINDEX_OR|OPINDEX_SELECT_IN|OPINDEX_SELECT_OUT)\n\ 339 368 \n\ 340 returns: operator string (str)"},369 returns: operator string"}, 341 370 {"load", (PyCFunction)Migemo_load, METH_VARARGS | METH_KEYWORDS, 342 371 "add dictionary to Migemo object\n\ … … 345 374 dict_id: (DICTID_HAN2ZEN|DICTID_HIRA2KATA|DICTID_MIGEMO|\n\ 346 375 DICTID_ROMA2HIRA|DICTID_ZEN2HAN)\n\ 347 dict_file: path to dictionary file (str)\n\376 dict_file: path to dictionary file\n\ 348 377 \n\ 349 378 returns: ID of loaded dictionary"}, … … 357 386 \n\ 358 387 def get_encoding()\n\ 359 returns: encoding string (str)"},388 returns: encoding string"}, 360 389 {NULL} /* Sentinel */ 361 390 }; -
pymigemo/trunk/setup.py
r42 r227 5 5 from distutils.core import setup, Extension 6 6 setup(name="migemo", 7 version="0. 3",7 version="0.4", 8 8 description="C/Migemo Python binding", 9 9 author="Atzm WATANABE",
Note: See TracChangeset
for help on using the changeset viewer.