Ignore:
Timestamp:
11/10/12 01:41:55 (12 years ago)
Author:
atzm
Message:
  • Python3 support
File:
1 edited

Legend:

Unmodified
Added
Removed
  • pymigemo/trunk/pymigemo.c

    r42 r225  
    11/* 
    22 * pymigemo.c - C/Migemo Python binding 
    3  * Copyright(C) 2005-2010, Atzm WATANABE <atzm@atzm.org> 
     3 * Copyright(C) 2005-2012, Atzm WATANABE <atzm@atzm.org> 
    44 * 
    55 * $Id$ 
     
    1818#include <unistd.h> 
    1919 
    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 
    2129 
    2230/* for dereference migemo object members */ 
     
    100108    } 
    101109 
    102     self->ob_type->tp_free((PyObject *)self); 
     110    Py_TYPE(self)->tp_free((PyObject *)self); 
    103111} 
    104112 
     
    165173    } 
    166174 
    167     return PyString_FromString(encoding); 
     175    return PyBytes_FromString(encoding); 
    168176} 
    169177 
     
    193201        } 
    194202 
    195         query = strdup(PyString_AS_STRING(q)); 
     203        query = strdup(PyBytes_AS_STRING(q)); 
    196204        Py_DECREF(q); 
    197205 
     
    200208        } 
    201209    } 
    202     else if (PyString_Check(pyquery)) { 
    203         query = strdup(PyString_AS_STRING(pyquery)); 
     210    else if (PyBytes_Check(pyquery)) { 
     211        query = strdup(PyBytes_AS_STRING(pyquery)); 
    204212 
    205213        if (query == NULL) { 
     
    219227    } 
    220228 
    221     pyrestr = PyString_FromString(regex); 
     229    pyrestr = PyBytes_FromString(regex); 
    222230    migemo_release(self->migemo_obj, regex); 
    223231    if (pyrestr == NULL) { 
     
    264272 
    265273    if (op = migemo_get_operator(self->migemo_obj, index)) { 
    266         result = PyString_FromString(op); 
     274        result = PyBytes_FromString(op); 
    267275    } 
    268276    else { 
     
    294302        } 
    295303 
    296         result = PyInt_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)); 
    297305    } 
    298306 
     
    307315 
    308316static PyMethodDef Migemo_methods[] = { 
    309     {"query", (PyCFunction)Migemo_query, METH_KEYWORDS, 
     317    {"query", (PyCFunction)Migemo_query, METH_VARARGS | METH_KEYWORDS, 
    310318     "return regex from romaji string\n\ 
    311319\n\ 
     
    314322\n\ 
    315323  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, 
    317325     "set operator string as the meta character of regex\n\ 
    318326\n\ 
     
    323331\n\ 
    324332  returns: boolean value"}, 
    325     {"get_operator", (PyCFunction)Migemo_get_operator, METH_KEYWORDS, 
     333    {"get_operator", (PyCFunction)Migemo_get_operator, METH_VARARGS | METH_KEYWORDS, 
    326334     "get operator string as the meta character of regex\n\ 
    327335\n\ 
     
    331339\n\ 
    332340  returns: operator string (str)"}, 
    333     {"load", (PyCFunction)Migemo_load, METH_KEYWORDS, 
     341    {"load", (PyCFunction)Migemo_load, METH_VARARGS | METH_KEYWORDS, 
    334342     "add dictionary to Migemo object\n\ 
    335343\n\ 
     
    357365}; 
    358366 
     367#ifndef PyVarObject_HEAD_INIT 
     368#  define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size, 
     369#endif 
     370 
    359371static PyTypeObject MigemoType = { 
    360     PyObject_HEAD_INIT(NULL) 
    361     0,                          /*ob_size*/ 
     372    PyVarObject_HEAD_INIT(NULL, 0) 
    362373    "migemo.Migemo",            /*tp_name*/ 
    363374    sizeof(Migemo),             /*tp_basicsize*/ 
     
    399410}; 
    400411 
     412#define PYMIGEMO_MODULEDOC "C/Migemo Python binding" 
     413 
    401414static PyMethodDef module_methods[] = { 
    402415    {NULL} /* Sentinel */ 
    403416}; 
    404417 
    405 #ifndef PyMODINIT_FUNC 
    406 #define PyMODINIT_FUNC void 
    407 #endif 
    408 PyMODINIT_FUNC 
    409 initmigemo(void)  
     418#ifdef PYTHON3 
     419static 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 
     438MOD_INIT(migemo) 
    410439{ 
    411440    PyObject* m; 
     
    414443        return; 
    415444 
    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    } 
    417458 
    418459    Py_INCREF(&MigemoType); 
     
    435476    PyModule_AddObject(m, "OPINDEX_SELECT_OUT", Py_BuildValue("i", MIGEMO_OPINDEX_SELECT_OUT)); 
    436477    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.