@@ -130,13 +130,18 @@ def test_dict(self):
130
130
return custom_dict;
131
131
}
132
132
""" ,
133
- ready_code = """
133
+ ready_code = """PyObject* descr;
134
134
custom_dict = PyDict_New();
135
- PyDict_SetItemString(custom_dict, "hello", PyUnicode_FromString("first custom property"));
135
+ Py_XINCREF(custom_dict);
136
+ descr = PyUnicode_FromString("first custom property");
137
+ Py_INCREF(descr);
138
+ PyDict_SetItemString(custom_dict, "hello", descr);
136
139
TestDictType.tp_dict = custom_dict;
137
140
""" ,
138
141
post_ready_code = """
139
- PyDict_SetItemString(TestDictType.tp_dict, "world", PyUnicode_FromString("second custom property"));
142
+ descr = PyUnicode_FromString("second custom property");
143
+ Py_INCREF(descr);
144
+ PyDict_SetItemString(TestDictType.tp_dict, "world", descr);
140
145
""" ,
141
146
tp_methods = '{"get_dict", get_dict, METH_NOARGS, ""}'
142
147
)
@@ -166,6 +171,7 @@ def test_new(self):
166
171
typedObj = ((TestNewObject*)obj);
167
172
typedObj->none = Py_None;
168
173
Py_INCREF(Py_None);
174
+ Py_XINCREF(obj);
169
175
return obj;
170
176
}
171
177
static PyObject* get_none(PyObject* self) {
@@ -184,9 +190,10 @@ def test_slots(self):
184
190
'' ,
185
191
includes = '#include "datetime.h"' ,
186
192
cmembers = "PyDateTime_DateTime __pyx_base;" ,
187
- ready_code = '''PyObject* datetime_module = PyImport_ImportModule("datetime");
188
- PyTypeObject* datetime_type = (PyTypeObject*)PyObject_GetAttrString(datetime_module, "datetime");
193
+ ready_code = '''PyTypeObject* datetime_type = NULL;
189
194
PyDateTime_IMPORT;
195
+ Py_INCREF(PyDateTimeAPI);
196
+ datetime_type = PyDateTimeAPI->DateTimeType;
190
197
Py_XINCREF(datetime_type);
191
198
TestSlotsType.tp_base = (PyTypeObject*) datetime_type;
192
199
TestSlotsType.tp_new = datetime_type->tp_new;
@@ -198,7 +205,7 @@ def test_slots_initialized(self):
198
205
TestSlotsInitialized = CPyExtType ("TestSlotsInitialized" ,
199
206
'''
200
207
static PyTypeObject* datetime_type = NULL;
201
-
208
+
202
209
PyObject* TestSlotsInitialized_new(PyTypeObject* self, PyObject* args, PyObject* kwargs) {
203
210
PyObject* result = datetime_type->tp_new(self, args, kwargs);
204
211
Py_XINCREF(result);
@@ -207,10 +214,10 @@ def test_slots_initialized(self):
207
214
''' ,
208
215
includes = '#include "datetime.h"' ,
209
216
cmembers = "PyDateTime_DateTime __pyx_base;" ,
210
- ready_code = '''PyObject* datetime_module = PyImport_ImportModule("datetime");
217
+ ready_code = '''
211
218
PyDateTime_IMPORT;
212
- Py_INCREF(datetime_module );
213
- datetime_type = (PyTypeObject*)PyObject_GetAttrString(datetime_module, "datetime") ;
219
+ Py_INCREF(PyDateTimeAPI );
220
+ datetime_type = PyDateTimeAPI->DateTimeType ;
214
221
Py_INCREF(datetime_type);
215
222
TestSlotsInitializedType.tp_base = datetime_type;
216
223
''' ,
@@ -222,25 +229,23 @@ def test_float_subclass(self):
222
229
TestFloatSubclass = CPyExtType ("TestFloatSubclass" ,
223
230
"""
224
231
static PyTypeObject* testFloatSubclassPtr = NULL;
225
-
232
+
226
233
static PyObject* new_fp(double val) {
227
234
PyFloatObject* fp = PyObject_New(PyFloatObject, testFloatSubclassPtr);
228
235
fp->ob_fval = val;
229
- Py_XINCREF(fp);
230
236
return (PyObject*)fp;
231
237
}
232
-
238
+
233
239
static PyObject* fp_tpnew(PyTypeObject* type, PyObject* args, PyObject* kwargs) {
234
240
double dval = 0.0;
241
+ Py_XINCREF(args);
235
242
if (!PyArg_ParseTuple(args, "d", &dval)) {{
236
243
return NULL;
237
244
}}
238
245
return new_fp(dval);
239
246
}
240
-
247
+
241
248
static PyObject* fp_add(PyObject* l, PyObject* r) {
242
- Py_XINCREF(l);
243
- Py_XINCREF(r);
244
249
if (PyFloat_Check(l)) {
245
250
if (PyFloat_Check(r)) {
246
251
return new_fp(PyFloat_AS_DOUBLE(l) + PyFloat_AS_DOUBLE(r));
@@ -257,14 +262,16 @@ def test_float_subclass(self):
257
262
return Py_NotImplemented;
258
263
}
259
264
""" ,
265
+ cmembers = "PyFloatObject base;" ,
260
266
tp_base = "&PyFloat_Type" ,
261
267
nb_add = "fp_add" ,
262
268
tp_new = "fp_tpnew" ,
263
- post_ready_code = "testFloatSubclassPtr = &TestFloatSubclassType;"
269
+ post_ready_code = "testFloatSubclassPtr = &TestFloatSubclassType; Py_INCREF(testFloatSubclassPtr); "
264
270
)
265
271
tester = TestFloatSubclass (41.0 )
266
- res = tester + 1
272
+ res = tester + 1
267
273
assert res == 42.0 , "expected 42.0 but was %s" % res
274
+
268
275
269
276
270
277
class TestObjectFunctions (CPyExtTestCase ):
0 commit comments