- Timestamp:
- 11/10/12 01:41:55 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pymigemo/trunk/pymigemo.c
r42 r225 1 1 /* 2 2 * pymigemo.c - C/Migemo Python binding 3 * Copyright(C) 2005-201 0, Atzm WATANABE <atzm@atzm.org>3 * Copyright(C) 2005-2012, Atzm WATANABE <atzm@atzm.org> 4 4 * 5 5 * $Id$ … … 18 18 #include <unistd.h> 19 19 20 #define PYMIGEMO_VERSION "0.3" 20 #define PYMIGEMO_VERSION "0.4" 21 22 #if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 1) || PY_MAJOR_VERSION > 3 23 # define PYTHON3 24 #endif 25 26 #ifndef Py_TYPE 27 # define Py_TYPE(ob) (((PyObject *)(ob))->ob_type) 28 #endif 21 29 22 30 /* for dereference migemo object members */ … … 100 108 } 101 109 102 self->ob_type->tp_free((PyObject *)self);110 Py_TYPE(self)->tp_free((PyObject *)self); 103 111 } 104 112 … … 165 173 } 166 174 167 return Py String_FromString(encoding);175 return PyBytes_FromString(encoding); 168 176 } 169 177 … … 193 201 } 194 202 195 query = strdup(Py String_AS_STRING(q));203 query = strdup(PyBytes_AS_STRING(q)); 196 204 Py_DECREF(q); 197 205 … … 200 208 } 201 209 } 202 else if (Py String_Check(pyquery)) {203 query = strdup(Py String_AS_STRING(pyquery));210 else if (PyBytes_Check(pyquery)) { 211 query = strdup(PyBytes_AS_STRING(pyquery)); 204 212 205 213 if (query == NULL) { … … 219 227 } 220 228 221 pyrestr = Py String_FromString(regex);229 pyrestr = PyBytes_FromString(regex); 222 230 migemo_release(self->migemo_obj, regex); 223 231 if (pyrestr == NULL) { … … 264 272 265 273 if (op = migemo_get_operator(self->migemo_obj, index)) { 266 result = Py String_FromString(op);274 result = PyBytes_FromString(op); 267 275 } 268 276 else { … … 294 302 } 295 303 296 result = Py Int_FromLong((long)migemo_load(self->migemo_obj, dict_id, dict_file));304 result = PyLong_FromLong((long)migemo_load(self->migemo_obj, dict_id, dict_file)); 297 305 } 298 306 … … 307 315 308 316 static PyMethodDef Migemo_methods[] = { 309 {"query", (PyCFunction)Migemo_query, METH_ KEYWORDS,317 {"query", (PyCFunction)Migemo_query, METH_VARARGS | METH_KEYWORDS, 310 318 "return regex from romaji string\n\ 311 319 \n\ … … 314 322 \n\ 315 323 returns: regex string as Unicode object"}, 316 {"set_operator", (PyCFunction)Migemo_set_operator, METH_ KEYWORDS,324 {"set_operator", (PyCFunction)Migemo_set_operator, METH_VARARGS | METH_KEYWORDS, 317 325 "set operator string as the meta character of regex\n\ 318 326 \n\ … … 323 331 \n\ 324 332 returns: boolean value"}, 325 {"get_operator", (PyCFunction)Migemo_get_operator, METH_ KEYWORDS,333 {"get_operator", (PyCFunction)Migemo_get_operator, METH_VARARGS | METH_KEYWORDS, 326 334 "get operator string as the meta character of regex\n\ 327 335 \n\ … … 331 339 \n\ 332 340 returns: operator string (str)"}, 333 {"load", (PyCFunction)Migemo_load, METH_ KEYWORDS,341 {"load", (PyCFunction)Migemo_load, METH_VARARGS | METH_KEYWORDS, 334 342 "add dictionary to Migemo object\n\ 335 343 \n\ … … 357 365 }; 358 366 367 #ifndef PyVarObject_HEAD_INIT 368 # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size, 369 #endif 370 359 371 static PyTypeObject MigemoType = { 360 PyObject_HEAD_INIT(NULL) 361 0, /*ob_size*/ 372 PyVarObject_HEAD_INIT(NULL, 0) 362 373 "migemo.Migemo", /*tp_name*/ 363 374 sizeof(Migemo), /*tp_basicsize*/ … … 399 410 }; 400 411 412 #define PYMIGEMO_MODULEDOC "C/Migemo Python binding" 413 401 414 static PyMethodDef module_methods[] = { 402 415 {NULL} /* Sentinel */ 403 416 }; 404 417 405 #ifndef PyMODINIT_FUNC 406 #define PyMODINIT_FUNC void 407 #endif 408 PyMODINIT_FUNC 409 initmigemo(void) 418 #ifdef PYTHON3 419 static struct PyModuleDef moduledef = { 420 PyModuleDef_HEAD_INIT, 421 "migemo", /* m_name */ 422 PYMIGEMO_MODULEDOC, /* m_doc */ 423 -1, /* m_size */ 424 module_methods, /* m_methods */ 425 NULL, /* m_reload */ 426 NULL, /* m_traverse */ 427 NULL, /* m_clear */ 428 NULL, /* m_free */ 429 }; 430 #endif 431 432 #ifdef PYTHON3 433 # define MOD_INIT(name) PyObject *PyInit_##name(void) 434 #else 435 # define MOD_INIT(name) void init##name(void) 436 #endif 437 438 MOD_INIT(migemo) 410 439 { 411 440 PyObject* m; … … 414 443 return; 415 444 416 m = Py_InitModule3("migemo", module_methods, "C/Migemo Python binding"); 445 #ifdef PYTHON3 446 m = PyModule_Create(&moduledef); 447 #else 448 m = Py_InitModule3("migemo", module_methods, PYMIGEMO_MODULEDOC); 449 #endif 450 451 if (m == NULL) { 452 #ifdef PYTHON3 453 return NULL; 454 #else 455 return; 456 #endif 457 } 417 458 418 459 Py_INCREF(&MigemoType); … … 435 476 PyModule_AddObject(m, "OPINDEX_SELECT_OUT", Py_BuildValue("i", MIGEMO_OPINDEX_SELECT_OUT)); 436 477 PyModule_AddObject(m, "OPINDEX_NEWLINE", Py_BuildValue("i", MIGEMO_OPINDEX_NEWLINE)); 437 } 478 479 #ifdef PYTHON3 480 return m; 481 #endif 482 }
Note: See TracChangeset
for help on using the changeset viewer.