Merge lp:~cjwatson/storm/py3-build-extensions into lp:storm

Proposed by Colin Watson
Status: Merged
Merged at revision: 505
Proposed branch: lp:~cjwatson/storm/py3-build-extensions
Merge into: lp:storm
Diff against target: 203 lines (+63/-26)
1 file modified
storm/cextensions.c (+63/-26)
To merge this branch: bzr merge lp:~cjwatson/storm/py3-build-extensions
Reviewer Review Type Date Requested Status
Colin Watson Approve
Review via email: mp+368556@code.launchpad.net

Commit message

Make cextensions module build with 3.x.

Description of the change

To post a comment you must log in.
Revision history for this message
Colin Watson (cjwatson) wrote :

I'll review this myself since I just extracted it from somebody else's MP.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'storm/cextensions.c'
2--- storm/cextensions.c 2019-06-03 08:45:24 +0000
3+++ storm/cextensions.c 2019-06-07 16:06:01 +0000
4@@ -24,6 +24,15 @@
5 #include <structmember.h>
6
7
8+#if PY_VERSION_HEX >= 0x03000000
9+#define PyInt_FromLong PyLong_FromLong
10+#define PyText_AsString _PyUnicode_AsString
11+#define PyString_CheckExact(o) 0
12+#else
13+/* 2.x */
14+#define PyText_AsString PyString_AsString
15+#endif
16+
17 #define CATCH(error_value, expression) \
18 do { \
19 if ((expression) == error_value) {\
20@@ -248,7 +257,7 @@
21 EventSystem_dealloc(EventSystemObject *self)
22 {
23 EventSystem_clear(self);
24- self->ob_type->tp_free((PyObject *)self);
25+ Py_TYPE(self)->tp_free((PyObject *)self);
26 }
27
28 static PyObject *
29@@ -469,9 +478,8 @@
30 };
31 #undef OFFSETOF
32
33-statichere PyTypeObject EventSystem_Type = {
34- PyObject_HEAD_INIT(NULL)
35- 0, /*ob_size*/
36+static PyTypeObject EventSystem_Type = {
37+ PyVarObject_HEAD_INIT(NULL, 0)
38 "storm.variables.EventSystem", /*tp_name*/
39 sizeof(EventSystemObject), /*tp_basicsize*/
40 0, /*tp_itemsize*/
41@@ -663,7 +671,7 @@
42 Variable_dealloc(VariableObject *self)
43 {
44 Variable_clear(self);
45- self->ob_type->tp_free((PyObject *)self);
46+ Py_TYPE(self)->tp_free((PyObject *)self);
47 }
48
49 static PyObject *
50@@ -1021,7 +1029,7 @@
51
52 /* variable = self.__class__.__new__(self.__class__) */
53 noargs = PyTuple_New(0);
54- CATCH(NULL, variable = self->ob_type->tp_new(self->ob_type, noargs, NULL));
55+ CATCH(NULL, variable = Py_TYPE(self)->tp_new(Py_TYPE(self), noargs, NULL));
56
57 /* variable.set_state(self.get_state()) */
58 CATCH(NULL,
59@@ -1072,9 +1080,8 @@
60 };
61 #undef OFFSETOF
62
63-statichere PyTypeObject Variable_Type = {
64- PyObject_HEAD_INIT(NULL)
65- 0, /*ob_size*/
66+static PyTypeObject Variable_Type = {
67+ PyVarObject_HEAD_INIT(NULL, 0)
68 "storm.variables.Variable", /*tp_name*/
69 sizeof(VariableObject), /*tp_basicsize*/
70 0, /*tp_itemsize*/
71@@ -1223,7 +1230,7 @@
72 Compile_dealloc(CompileObject *self)
73 {
74 Compile_clear(self);
75- self->ob_type->tp_free((PyObject *)self);
76+ Py_TYPE(self)->tp_free((PyObject *)self);
77 }
78
79 static PyObject *
80@@ -1390,13 +1397,13 @@
81 return NULL;
82 }
83
84-staticforward PyTypeObject Compile_Type;
85+static PyTypeObject Compile_Type;
86
87 static PyObject *
88 Compile_create_child(CompileObject *self, PyObject *args)
89 {
90 /* return self.__class__(self) */
91- return PyObject_CallFunctionObjArgs((PyObject *)self->ob_type, self, NULL);
92+ return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(self), self, NULL);
93 }
94
95 static PyObject *
96@@ -1494,7 +1501,7 @@
97 if (repr) {
98 PyErr_Format(CompileError,
99 "Don't know how to compile type %s of %s",
100- expr->ob_type->tp_name, PyString_AS_STRING(repr));
101+ expr->ob_type->tp_name, PyText_AsString(repr));
102 Py_DECREF(repr);
103 }
104 goto error;
105@@ -1513,7 +1520,7 @@
106 state, NULL));
107
108 /* if inner_precedence < outer_precedence: */
109- if (PyObject_Compare(inner_precedence, outer_precedence) == -1) {
110+ if (PyObject_RichCompareBool(inner_precedence, outer_precedence, Py_LT)) {
111 PyObject *args, *tmp;
112
113 if (PyErr_Occurred())
114@@ -1736,9 +1743,8 @@
115 };
116 #undef OFFSETOF
117
118-statichere PyTypeObject Compile_Type = {
119- PyObject_HEAD_INIT(NULL)
120- 0, /*ob_size*/
121+static PyTypeObject Compile_Type = {
122+ PyVarObject_HEAD_INIT(NULL, 0)
123 "storm.variables.Compile", /*tp_name*/
124 sizeof(CompileObject), /*tp_basicsize*/
125 0, /*tp_itemsize*/
126@@ -2027,9 +2033,8 @@
127 {NULL}
128 };
129
130-statichere PyTypeObject ObjectInfo_Type = {
131- PyObject_HEAD_INIT(NULL)
132- 0, /*ob_size*/
133+static PyTypeObject ObjectInfo_Type = {
134+ PyVarObject_HEAD_INIT(NULL, 0)
135 "storm.info.ObjectInfo", /*tp_name*/
136 sizeof(ObjectInfoObject), /*tp_basicsize*/
137 0, /*tp_itemsize*/
138@@ -2132,11 +2137,9 @@
139 return PyType_Ready(type);
140 }
141
142-DL_EXPORT(void)
143-initcextensions(void)
144+static int
145+do_init(PyObject *module)
146 {
147- PyObject *module;
148-
149 prepare_type(&EventSystem_Type);
150 prepare_type(&Compile_Type);
151 ObjectInfo_Type.tp_base = &PyDict_Type;
152@@ -2144,7 +2147,6 @@
153 prepare_type(&ObjectInfo_Type);
154 prepare_type(&Variable_Type);
155
156- module = Py_InitModule3("cextensions", cextensions_methods, "");
157 Py_INCREF(&Variable_Type);
158
159 #define REGISTER_TYPE(name) \
160@@ -2157,7 +2159,42 @@
161 REGISTER_TYPE(ObjectInfo);
162 REGISTER_TYPE(Compile);
163 REGISTER_TYPE(EventSystem);
164-}
165+ return 0;
166+}
167+
168+#if PY_VERSION_HEX < 0x03000000
169+DL_EXPORT(void)
170+initcextensions(void)
171+{
172+ PyObject *module;
173+
174+ module = Py_InitModule3("cextensions", cextensions_methods, "");
175+ do_init(module);
176+}
177+#else
178+static struct PyModuleDef cextensionsmodule = {
179+ PyModuleDef_HEAD_INIT,
180+ "cextensions",
181+ NULL,
182+ -1,
183+ cextensions_methods,
184+ NULL,
185+ NULL,
186+ NULL,
187+ NULL
188+};
189+
190+PyMODINIT_FUNC
191+PyInit_cextensions(void)
192+{
193+ PyObject *module = PyModule_Create(&cextensionsmodule);
194+ if (module == NULL)
195+ return NULL;
196+ do_init(module);
197+ return module;
198+}
199+#endif
200+
201
202 /* vim:ts=4:sw=4:et
203 */

Subscribers

People subscribed via source and target branches

to status/vote changes: