Changeset 227 for pymigemo/trunk/pymigemo.c
- Timestamp:
- 11/10/12 21:20:12 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 };
Note: See TracChangeset
for help on using the changeset viewer.