Author: Matti Picus <[email protected]>
Branch: PyTuple_Type-subclass
Changeset: r85368:1d0e93b54604
Date: 2016-06-24 10:26 +0300
http://bitbucket.org/pypy/pypy/changeset/1d0e93b54604/

Log:    2 to 3 for merging

diff --git a/pypy/module/cpyext/test/foo.c b/pypy/module/cpyext/test/foo.c
--- a/pypy/module/cpyext/test/foo.c
+++ b/pypy/module/cpyext/test/foo.c
@@ -1,6 +1,20 @@
 #include "Python.h"
 #include "structmember.h"
 
+#if PY_MAJOR_VERSION >= 3
+    #define PyInt_FromLong PyLong_FromLong
+    #define PyInt_AsLong PyLong_AsLong
+    #define PyThing_FromStringAndSize PyUnicode_FromStringAndSize
+    #define PyThing_FromString PyUnicode_FromString
+    # defin PyThing_Check PyUnicode_Check
+    #define _PyThing_AsString _PyUnicode_AsString
+#else
+    #define PyThing_FromStringAndSize PyString_FromStringAndSize
+    #define PyThing_FromString PyString_FromString
+    # defin PyThing_Check PyString_Check
+    #define _PyThing_AsString _PyString_AsString
+#endif
+
 typedef struct {
     PyObject_HEAD
     int    foo;        /* the context holder */
@@ -88,7 +102,7 @@
 static PyObject *
 foo_get_name(PyObject *self, void *closure)
 {
-    return PyString_FromStringAndSize("Foo Example", 11);
+    return PyThing_FromStringAndSize("Foo Example", 11);
 }
 
 static PyObject *
@@ -114,7 +128,7 @@
 {
     PyObject *format;
 
-    format = PyString_FromString("<Foo>");
+    format = PyThing_FromString("<Foo>");
     if (format == NULL) return NULL;
     return format;
 }
@@ -130,11 +144,11 @@
 foo_setattro(fooobject *self, PyObject *name, PyObject *value)
 {
     char *name_str;
-    if (!PyString_Check(name)) {
+    if (!PyThing_Check(name)) {
         PyErr_SetObject(PyExc_AttributeError, name);
         return -1;
     }
-    name_str = PyString_AsString(name);
+    name_str = _PyThing_AsString(name);
     if (strcmp(name_str, "set_foo") == 0)
     {
         long v = PyInt_AsLong(value);
@@ -652,6 +666,33 @@
     {NULL,        NULL}    /* Sentinel */
 };
 
+#if PY_MAJOR_VERSION >= 3
+static struct PyModuleDef moduledef = {
+    PyModuleDef_HEAD_INIT,
+    "foo",
+    "Module Doc",
+    -1,
+    foo_functions, 
+    NULL,
+    NULL,
+    NULL,
+    NULL,
+};
+#define INITERROR return NULL
+
+/* Initialize this module. */
+#ifdef __GNUC__
+extern __attribute__((visibility("default")))
+#else
+extern __declspec(dllexport)
+#endif
+
+PyMODINIT_FUNC
+PyInit_foo(void)
+
+#else
+
+#define INITERROR return NULL
 
 /* Initialize this module. */
 #ifdef __GNUC__
@@ -662,8 +703,16 @@
 
 PyMODINIT_FUNC
 initfoo(void)
+#endif
 {
-    PyObject *m, *d;
+    PyObject *d;
+#if PY_MAJOR_VERSION >= 3
+    PyObject *module = PyModule_Create(&moduledef);
+#else
+    PyObject *module = Py_InitModule("foo", foo_functions);
+#endif
+    if (module == NULL)
+        INITERROR;
 
     footype.tp_new = PyType_GenericNew;
 
@@ -672,58 +721,59 @@
     MetaType.tp_base = &PyType_Type;
 
     if (PyType_Ready(&footype) < 0)
-        return;
+        INITERROR;
     if (PyType_Ready(&UnicodeSubtype) < 0)
-        return;
+        INITERROR;
     if (PyType_Ready(&UnicodeSubtype2) < 0)
-        return;
+        INITERROR;
     if (PyType_Ready(&MetaType) < 0)
-        return;
+        INITERROR;
     if (PyType_Ready(&InitErrType) < 0)
-        return;
+        INITERROR;
     if (PyType_Ready(&SimplePropertyType) < 0)
-        return;
+        INITERROR;
 
     SimplePropertyType.tp_new = PyType_GenericNew;
     InitErrType.tp_new = PyType_GenericNew;
 
     CustomType.ob_type = &MetaType;
     if (PyType_Ready(&CustomType) < 0)
-        return;
+        INITERROR;
 
     UnicodeSubtype3.tp_flags = Py_TPFLAGS_DEFAULT;
     UnicodeSubtype3.tp_base = &UnicodeSubtype;
     UnicodeSubtype3.tp_bases = Py_BuildValue("(OO)", &UnicodeSubtype,
                                                     &CustomType);
     if (PyType_Ready(&UnicodeSubtype3) < 0)
-        return;
+        INITERROR;
 
     TupleLike.tp_base = &PyTuple_Type;
     if (PyType_Ready(&TupleLike) < 0)
         return;
 
-    m = Py_InitModule("foo", foo_functions);
-    if (m == NULL)
-        return;
-    d = PyModule_GetDict(m);
+
+    d = PyModule_GetDict(module);
     if (d == NULL)
-        return;
+        INITERROR;
     if (PyDict_SetItemString(d, "fooType", (PyObject *)&footype) < 0)
-        return;
+        INITERROR;
     if (PyDict_SetItemString(d, "UnicodeSubtype", (PyObject *) 
&UnicodeSubtype) < 0)
-        return;
+        INITERROR;
     if (PyDict_SetItemString(d, "UnicodeSubtype2", (PyObject *) 
&UnicodeSubtype2) < 0)
-        return;
+        INITERROR;
     if (PyDict_SetItemString(d, "UnicodeSubtype3", (PyObject *) 
&UnicodeSubtype3) < 0)
-        return;
+        INITERROR;
     if (PyDict_SetItemString(d, "MetaType", (PyObject *) &MetaType) < 0)
-        return;
+        INITERROR;
     if (PyDict_SetItemString(d, "InitErrType", (PyObject *) &InitErrType) < 0)
-        return;
+        INITERROR;
     if (PyDict_SetItemString(d, "Property", (PyObject *) &SimplePropertyType) 
< 0)
-        return;
+        INITERROR;
     if (PyDict_SetItemString(d, "Custom", (PyObject *) &CustomType) < 0)
-        return;
+        INITERROR;
     if (PyDict_SetItemString(d, "TupleLike", (PyObject *) &TupleLike) < 0)
-        return;
+        INITERROR;
+#if PY_MAJOR_VERSION >=3
+    return module;
+#endif
 }
diff --git a/pypy/module/cpyext/unicodeobject.py 
b/pypy/module/cpyext/unicodeobject.py
--- a/pypy/module/cpyext/unicodeobject.py
+++ b/pypy/module/cpyext/unicodeobject.py
@@ -90,6 +90,7 @@
     Py_DecRef(space, py_unicode.c_defenc)
     if py_unicode.c_str:
         lltype.free(py_unicode.c_str, flavor="raw")
+
     from pypy.module.cpyext.object import _dealloc
     _dealloc(space, py_obj)
 
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to