Commits

Naoki INADA  committed d1cbc33 Draft

Support Python 3

  • Participants
  • Parent commits 0e4ad38

Comments (0)

Files changed (1)

File porterstemmer.c

     struct stemmer stem;
 } Stemmer;
 
+#if PY_MAJOR_VERSION < 3
+#define PyBytes_Check PyString_Check
+#define PyBytes_Size  PyString_Size
+#define PyBytes_FromSTring PyString_FromString
+#define PyBytes_AS_STRING PyString_AS_STRING
+#define _PyBytes_Resize _PyString_Resize
+#endif
+
 static PyObject* Stemmer_call(Stemmer *self, PyObject *args, PyObject *kw)
 {
     PyObject *from_stem;
             return NULL;
         }
     } else {
-        if (!PyString_Check(from_stem)) {
+        if (!PyBytes_Check(from_stem)) {
             PyErr_BadArgument();
             return NULL;
         }
-        if (PyString_Size(from_stem) < 2) {
+        if (PyBytes_Size(from_stem) < 2) {
             Py_INCREF(from_stem);
             return from_stem;
         }
         /* Make a copy of ``from_stem``.
          * ``to_stem`` can be modified by the stemmer. */
-        to_stem = PyString_FromString(PyString_AS_STRING(from_stem));
+        to_stem = PyBytes_FromString(PyBytes_AS_STRING(from_stem));
         if (!to_stem) {
             return NULL;
         }
     }
 
     res = stem(&self->stem,
-        PyString_AS_STRING(to_stem), PyString_Size(to_stem)-1);
+        PyBytes_AS_STRING(to_stem), PyBytes_Size(to_stem)-1);
 
     if (unicode) {
-        to_stem_u = PyUnicode_DecodeUTF8(PyString_AsString(to_stem), res+1, NULL);
+        to_stem_u = PyUnicode_DecodeUTF8(PyBytes_AsString(to_stem), res+1, NULL);
         Py_DECREF(to_stem);
         if (!to_stem_u) {
             return NULL;
         return to_stem_u;
     }
     else {
-        _PyString_Resize(&to_stem, res+1);
+        _PyBytes_Resize(&to_stem, res+1);
         return to_stem;
     }
 }
 
 
 static PyTypeObject StemmerType = {
+#if PY_MAJOR_VERSION < 3
     PyObject_HEAD_INIT(NULL)
     0,                         /*tp_size*/
+#else
+    PyVarObject_HEAD_INIT(NULL, 0)
+#endif
     "porterstemmer.Stemmer",   /*tp_name*/
     sizeof(Stemmer),           /*tp_basicsize*/
     0,                         /*tp_itemsize*/
 };
 
 
+#if PY_MAJOR_VERSION < 3
 static PyMethodDef porterstemmer_methods[] = {
     {NULL}
 };
+#else
+static PyModuleDef stemmermodule = {
+    PyModuleDef_HEAD_INIT,
+    "porterstemmer",
+    0,
+    -1,
+    NULL, NULL, NULL, NULL, NULL
+};
+#endif
+
 
 #if PY_MAJOR_VERSION < 3
+#define RETURN(X) return
 PyMODINIT_FUNC
 initporterstemmer(void)
 #else
+#define RETURN(X) return X
 PyMODINIT_FUNC
 PyInit_porterstemmer(void)
 #endif
     StemmerType.tp_new = PyType_GenericNew;
     StemmerType.tp_doc = type_doc;
     if (PyType_Ready(&StemmerType) < 0)
-        return;
+        RETURN(NULL);
 
+#if PY_MAJOR_VERSION < 3
     m = Py_InitModule3("porterstemmer", porterstemmer_methods,
             module_doc);
+#else
+    stemmermodule.m_doc = module_doc;
+    m = PyModule_Create(&stemmermodule);
+#endif
     if (!m) {
-        return;
+        RETURN(NULL);
     }
 
     Py_INCREF(&StemmerType);
     PyModule_AddObject(m, "Stemmer", (PyObject*)&StemmerType);
+    RETURN(m);
 }