@@ -130,13 +130,18 @@ def test_dict(self):
130130 return custom_dict;
131131 }
132132 """ ,
133- ready_code = """
133+ ready_code = """PyObject* descr;
134134 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);
136139 TestDictType.tp_dict = custom_dict;
137140 """ ,
138141 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);
140145 """ ,
141146 tp_methods = '{"get_dict", get_dict, METH_NOARGS, ""}'
142147 )
@@ -166,6 +171,7 @@ def test_new(self):
166171 typedObj = ((TestNewObject*)obj);
167172 typedObj->none = Py_None;
168173 Py_INCREF(Py_None);
174+ Py_XINCREF(obj);
169175 return obj;
170176 }
171177 static PyObject* get_none(PyObject* self) {
@@ -184,9 +190,10 @@ def test_slots(self):
184190 '' ,
185191 includes = '#include "datetime.h"' ,
186192 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;
189194 PyDateTime_IMPORT;
195+ Py_INCREF(PyDateTimeAPI);
196+ datetime_type = PyDateTimeAPI->DateTimeType;
190197 Py_XINCREF(datetime_type);
191198 TestSlotsType.tp_base = (PyTypeObject*) datetime_type;
192199 TestSlotsType.tp_new = datetime_type->tp_new;
@@ -198,7 +205,7 @@ def test_slots_initialized(self):
198205 TestSlotsInitialized = CPyExtType ("TestSlotsInitialized" ,
199206 '''
200207 static PyTypeObject* datetime_type = NULL;
201-
208+
202209 PyObject* TestSlotsInitialized_new(PyTypeObject* self, PyObject* args, PyObject* kwargs) {
203210 PyObject* result = datetime_type->tp_new(self, args, kwargs);
204211 Py_XINCREF(result);
@@ -207,10 +214,10 @@ def test_slots_initialized(self):
207214 ''' ,
208215 includes = '#include "datetime.h"' ,
209216 cmembers = "PyDateTime_DateTime __pyx_base;" ,
210- ready_code = '''PyObject* datetime_module = PyImport_ImportModule("datetime");
217+ ready_code = '''
211218 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 ;
214221 Py_INCREF(datetime_type);
215222 TestSlotsInitializedType.tp_base = datetime_type;
216223 ''' ,
@@ -222,25 +229,23 @@ def test_float_subclass(self):
222229 TestFloatSubclass = CPyExtType ("TestFloatSubclass" ,
223230 """
224231 static PyTypeObject* testFloatSubclassPtr = NULL;
225-
232+
226233 static PyObject* new_fp(double val) {
227234 PyFloatObject* fp = PyObject_New(PyFloatObject, testFloatSubclassPtr);
228235 fp->ob_fval = val;
229- Py_XINCREF(fp);
230236 return (PyObject*)fp;
231237 }
232-
238+
233239 static PyObject* fp_tpnew(PyTypeObject* type, PyObject* args, PyObject* kwargs) {
234240 double dval = 0.0;
241+ Py_XINCREF(args);
235242 if (!PyArg_ParseTuple(args, "d", &dval)) {{
236243 return NULL;
237244 }}
238245 return new_fp(dval);
239246 }
240-
247+
241248 static PyObject* fp_add(PyObject* l, PyObject* r) {
242- Py_XINCREF(l);
243- Py_XINCREF(r);
244249 if (PyFloat_Check(l)) {
245250 if (PyFloat_Check(r)) {
246251 return new_fp(PyFloat_AS_DOUBLE(l) + PyFloat_AS_DOUBLE(r));
@@ -257,14 +262,16 @@ def test_float_subclass(self):
257262 return Py_NotImplemented;
258263 }
259264 """ ,
265+ cmembers = "PyFloatObject base;" ,
260266 tp_base = "&PyFloat_Type" ,
261267 nb_add = "fp_add" ,
262268 tp_new = "fp_tpnew" ,
263- post_ready_code = "testFloatSubclassPtr = &TestFloatSubclassType;"
269+ post_ready_code = "testFloatSubclassPtr = &TestFloatSubclassType; Py_INCREF(testFloatSubclassPtr); "
264270 )
265271 tester = TestFloatSubclass (41.0 )
266- res = tester + 1
272+ res = tester + 1
267273 assert res == 42.0 , "expected 42.0 but was %s" % res
274+
268275
269276
270277class TestObjectFunctions (CPyExtTestCase ):
0 commit comments