graemefox / esmre

Automatically exported from code.google.com/p/esmre
GNU Lesser General Public License v2.1
1 stars 0 forks source link

python-3 compatible package? #13

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hi Folks, in my effort to get the package in debian I noticed the package is 
almost ready for python3 but not fully ready.

I'm not a python programmer, I tried to make a patch that makes it build 
correctly 
--- python-esmre-0.3.1.orig/src/esm.c
+++ python-esmre-0.3.1/src/esm.c
@@ -21,6 +21,12 @@ Foundation, Inc., 51 Franklin Street, Fi

 #include "aho_corasick.h"

+
+// to support python 2.5 and earlier
+#ifndef Py_TYPE
+    #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
+#endif
+
 typedef struct {
     PyObject_HEAD
     ac_index*   index;
@@ -35,7 +41,7 @@ decref_result_object(void* item, void* d
 static void
 esm_Index_dealloc(esm_IndexObject* self) {
     ac_index_free(self->index, decref_result_object);
-    self->ob_type->tp_free((PyObject*) self);
+    Py_TYPE(self)->tp_free ((PyObject*) self);
 }

 static PyObject*

something seems still needing to be addressed
src/esm.c:191:5: warning: missing braces around initializer [-Wmissing-braces]
     PyObject_HEAD_INIT(NULL)
     ^
src/esm.c:191:5: warning: (near initialization for 
'esm_IndexType.ob_base.ob_base') [-Wmissing-braces]
src/esm.c:193:5: warning: initialization makes integer from pointer without a 
cast
     "esm.Index",                        /*tp_name*/
     ^
src/esm.c:193:5: warning: (near initialization for 'esm_IndexType.tp_basicsize')
src/esm.c:196:5: warning: initialization from incompatible pointer type
     (destructor) esm_Index_dealloc,     /*tp_dealloc*/
     ^
src/esm.c:196:5: warning: (near initialization for 'esm_IndexType.tp_print')
src/esm.c:211:5: warning: initialization makes pointer from integer without a 
cast
     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
     ^
src/esm.c:211:5: warning: (near initialization for 'esm_IndexType.tp_doc')
src/esm.c:212:5: warning: initialization from incompatible pointer type
     "Index() -> new efficient string matching index",  /* tp_doc */
     ^
src/esm.c:212:5: warning: (near initialization for 'esm_IndexType.tp_traverse')
src/esm.c:219:5: warning: initialization from incompatible pointer type
     esm_Index_methods,             /* tp_methods */
     ^
src/esm.c:219:5: warning: (near initialization for 'esm_IndexType.tp_members')
src/esm.c:220:5: warning: initialization from incompatible pointer type
     esm_Index_members,             /* tp_members */
     ^
src/esm.c:220:5: warning: (near initialization for 'esm_IndexType.tp_getset')
src/esm.c:227:5: warning: initialization from incompatible pointer type
     (initproc) esm_Index_init,      /* tp_init */
     ^
src/esm.c:227:5: warning: (near initialization for 'esm_IndexType.tp_alloc')
src/esm.c:229:5: warning: initialization from incompatible pointer type
     esm_Index_new,                 /* tp_new */
     ^
src/esm.c:229:5: warning: (near initialization for 'esm_IndexType.tp_free')
src/esm.c: In function 'initesm':
src/esm.c:245:9: warning: 'return' with no value, in function returning 
non-void [-Wreturn-type]
         return;
         ^
src/esm.c:247:5: warning: implicit declaration of function 'Py_InitModule3' 
[-Wimplicit-function-declaration]
     m = Py_InitModule3("esm", esm_methods,
     ^
src/esm.c:247:7: warning: assignment makes pointer from integer without a cast
     m = Py_InitModule3("esm", esm_methods,
       ^
src/esm.c:251:9: warning: 'return' with no value, in function returning 
non-void [-Wreturn-type]
         return;
         ^
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes 
-g -O2 -fstack-protector-strong -Wformat -Werror=format-security 
-D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.4m -c src/aho_corasick.c -o 
build/temp.linux-x86_64-3.4/src/aho_corasick.o

--------------

running install_scripts
  File "/usr/lib/python3.4/dist-packages/esmre.py", line 249
    raise TypeError, "enter() cannot be called after query()"
                   ^
SyntaxError: invalid syntax

Original issue reported on code.google.com by costamag...@gmail.com on 19 Feb 2015 at 10:16

GoogleCodeExporter commented 9 years ago
With a great thanks to Thomas Goirand I can write the second part of the patch:

Index: python-esmre/src/esmre.py
===================================================================
--- python-esmre.orig/src/esmre.py      2015-02-19 17:06:52.718795534 +0100
+++ python-esmre/src/esmre.py   2015-02-19 17:06:52.714795526 +0100
@@ -246,7 +246,7 @@
         try:

             if self.fixed:
-                raise TypeError, "enter() cannot be called after query()"
+                raise TypeError("enter() cannot be called after query()")

             keywords = shortlist(hints(regex))

Original comment by costamag...@gmail.com on 19 Feb 2015 at 4:09

GoogleCodeExporter commented 9 years ago
with great help from Thomas again I can write the full patch now:
Index: python-esmre/src/esm.c
===================================================================
--- python-esmre.orig/src/esm.c 2015-02-20 13:15:48.597454617 +0100
+++ python-esmre/src/esm.c      2015-02-20 13:15:48.593454596 +0100
@@ -21,6 +21,10 @@

 #include "aho_corasick.h"

+#ifndef Py_TYPE
+    #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
+#endif
+
 typedef struct {
     PyObject_HEAD
     ac_index*   index;
@@ -35,7 +39,7 @@
 static void
 esm_Index_dealloc(esm_IndexObject* self) {
     ac_index_free(self->index, decref_result_object);
-    self->ob_type->tp_free((PyObject*) self);
+    Py_TYPE(self)->tp_free ((PyObject*) self);
 }

 static PyObject*
@@ -180,10 +184,14 @@
     {NULL}  /* Sentinel */
 };

+// support old python 2.5
+#ifndef PyVarObject_HEAD_INIT
+    #define PyVarObject_HEAD_INIT(type, size) \
+        PyObject_HEAD_INIT(type) size,
+#endif

 static PyTypeObject esm_IndexType = {
-    PyObject_HEAD_INIT(NULL)
-    0,                                  /*ob_size*/
+    PyVarObject_HEAD_INIT(NULL, 0)
     "esm.Index",                        /*tp_name*/
     sizeof(esm_IndexObject),            /*tp_basicsize*/
     0,                                  /*tp_itemsize*/
@@ -231,21 +239,51 @@
 #define PyMODINIT_FUNC void
 #endif
 PyMODINIT_FUNC
-initesm(void) 
+#if PY_MAJOR_VERSION >= 3
+PyInit_esm(void)
+#else
+initesm(void)
+#endif
 {
     PyObject* m;

     if (PyType_Ready(&esm_IndexType) < 0)
+#if PY_MAJOR_VERSION >= 3
+        return NULL;
+#else
         return;
+#endif

+#if PY_MAJOR_VERSION >= 3
+    static struct PyModuleDef moduledef = {
+        PyModuleDef_HEAD_INIT,
+        "esm",     /* m_name */
+        "Support for efficient string matching.",  /* m_doc */
+        -1,                  /* m_size */
+        esm_methods,    /* m_methods */
+        NULL,                /* m_reload */
+        NULL,                /* m_traverse */
+        NULL,                /* m_clear */
+        NULL,                /* m_free */
+    };
+    m = PyModule_Create(&moduledef);
+#else
     m = Py_InitModule3("esm", esm_methods,
                        "Support for efficient string matching.");
+#endif

     if (m == NULL) {
+#if PY_MAJOR_VERSION >= 3
+        return NULL;
+#else
         return;
+#endif
     }

     Py_INCREF(&esm_IndexType);
     PyModule_AddObject(m, "Index", (PyObject *)&esm_IndexType);
+#if PY_MAJOR_VERSION >= 3
+    return m;
+#endif
 }

Index: python-esmre/src/esmre.py
===================================================================
--- python-esmre.orig/src/esmre.py      2015-02-20 13:15:48.597454617 +0100
+++ python-esmre/src/esmre.py   2015-02-20 13:15:48.593454596 +0100
@@ -246,7 +246,7 @@
         try:

             if self.fixed:
-                raise TypeError, "enter() cannot be called after query()"
+                raise TypeError("enter() cannot be called after query()")

             keywords = shortlist(hints(regex))

Original comment by costamag...@gmail.com on 20 Feb 2015 at 12:20

Attachments: