del a
expect_nonzero(gc.collect(), "instance")
+def test_newinstance():
+ class A(object):
+ pass
+ a = A()
+ a.a = a
+ gc.collect()
+ del a
+ expect_nonzero(gc.collect(), "newinstance")
+ class B(list):
+ pass
+ class C(B, A):
+ pass
+ a = C()
+ a.a = a
+ gc.collect()
+ del a
+ expect_nonzero(gc.collect(), "newinstance(2)")
+
def test_method():
# Tricky: self.__init__ is a bound method, it references the instance.
class A:
run_test("static classes", test_staticclass)
run_test("dynamic classes", test_dynamicclass)
run_test("instances", test_instance)
+ run_test("new instances", test_newinstance)
run_test("methods", test_method)
run_test("functions", test_function)
run_test("frames", test_frame)
static void
complex_dealloc(PyObject *op)
{
- PyObject_DEL(op);
+ op->ob_type->tp_free(op);
}
0, /* tp_init */
0, /* tp_alloc */
complex_new, /* tp_new */
+ _PyObject_Del, /* tp_free */
};
#endif
}
if (mp->ma_table != mp->ma_smalltable)
PyMem_DEL(mp->ma_table);
- PyObject_GC_Del(mp);
+ mp->ob_type->tp_free((PyObject *)mp);
Py_TRASHCAN_SAFE_END(mp)
}
(initproc)dict_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
dict_new, /* tp_new */
+ _PyObject_GC_Del, /* tp_free */
};
/* For backward compatibility with old dictionary interface */
}
Py_XDECREF(f->f_name);
Py_XDECREF(f->f_mode);
- PyObject_DEL(f);
+ f->ob_type->tp_free((PyObject *)f);
}
static PyObject *
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
file_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
(initproc)file_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
file_new, /* tp_new */
+ _PyObject_Del, /* tp_free */
};
/* Interface for the 'soft space' between print items. */
static void
float_dealloc(PyFloatObject *op)
{
- op->ob_type = (struct _typeobject *)free_list;
- free_list = op;
+ if (PyFloat_CheckExact(op)) {
+ op->ob_type = (struct _typeobject *)free_list;
+ free_list = op;
+ }
+ else
+ op->ob_type->tp_free((PyObject *)op);
}
double
cm_dealloc(classmethod *cm)
{
Py_XDECREF(cm->cm_callable);
- PyObject_DEL(cm);
+ cm->ob_type->tp_free((PyObject *)cm);
}
static PyObject *
cm_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
PyType_GenericNew, /* tp_new */
+ _PyObject_Del, /* tp_free */
};
PyObject *
sm_dealloc(staticmethod *sm)
{
Py_XDECREF(sm->sm_callable);
- PyObject_DEL(sm);
+ sm->ob_type->tp_free((PyObject *)sm);
}
static PyObject *
sm_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
PyType_GenericNew, /* tp_new */
+ _PyObject_Del, /* tp_free */
};
PyObject *
}
PyMem_FREE(op->ob_item);
}
- PyObject_GC_Del(op);
+ op->ob_type->tp_free((PyObject *)op);
Py_TRASHCAN_SAFE_END(op)
}
(initproc)list_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
PyType_GenericNew, /* tp_new */
+ _PyObject_GC_Del, /* tp_free */
};
static void
long_dealloc(PyObject *v)
{
- PyObject_DEL(v);
+ v->ob_type->tp_free(v);
}
static PyObject *
0, /* tp_init */
0, /* tp_alloc */
long_new, /* tp_new */
+ _PyObject_Del, /* tp_free */
};
_PyModule_Clear((PyObject *)m);
Py_DECREF(m->md_dict);
}
- PyObject_GC_Del(m);
+ m->ob_type->tp_free((PyObject *)m);
}
static PyObject *
(initproc)module_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
PyType_GenericNew, /* tp_new */
+ _PyObject_GC_Del, /* tp_free */
};
static void
string_dealloc(PyObject *op)
{
- PyObject_DEL(op);
+ op->ob_type->tp_free(op);
}
static int
0, /* tp_init */
0, /* tp_alloc */
string_new, /* tp_new */
+ _PyObject_Del, /* tp_free */
};
void
}
#endif
}
- PyObject_GC_Del(op);
+ op->ob_type->tp_free((PyObject *)op);
done:
Py_TRASHCAN_SAFE_END(op)
}
0, /* tp_init */
0, /* tp_alloc */
tuple_new, /* tp_new */
+ _PyObject_GC_Del, /* tp_free */
};
/* The following function breaks the notion that tuples are immutable:
return type->tp_alloc(type, 0);
}
-/* Helper for subtyping */
+/* Helpers for subtyping */
+
+static int
+subtype_traverse(PyObject *self, visitproc visit, void *arg)
+{
+ PyTypeObject *type, *base;
+ traverseproc f;
+ int err;
+
+ /* Find the nearest base with a different tp_traverse */
+ type = self->ob_type;
+ base = type->tp_base;
+ while ((f = base->tp_traverse) == subtype_traverse) {
+ base = base->tp_base;
+ assert(base);
+ }
+
+ if (type->tp_dictoffset != base->tp_dictoffset) {
+ PyObject **dictptr = _PyObject_GetDictPtr(self);
+ if (dictptr && *dictptr) {
+ err = visit(*dictptr, arg);
+ if (err)
+ return err;
+ }
+ }
+
+ if (f)
+ return f(self, visit, arg);
+ return 0;
+}
static void
subtype_dealloc(PyObject *self)
}
type->tp_dealloc = subtype_dealloc;
+ /* Enable GC unless there are really no instance variables possible */
+ if (!(type->tp_basicsize == sizeof(PyObject) &&
+ type->tp_itemsize == 0))
+ type->tp_flags |= Py_TPFLAGS_HAVE_GC;
+
/* Always override allocation strategy to use regular heap */
type->tp_alloc = PyType_GenericAlloc;
if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
type->tp_free = _PyObject_GC_Del;
- type->tp_traverse = base->tp_traverse;
+ type->tp_traverse = subtype_traverse;
type->tp_clear = base->tp_clear;
}
else
Py_XDECREF(type->tp_bases);
Py_XDECREF(type->tp_mro);
Py_XDECREF(type->tp_defined);
- /* XXX more? */
Py_XDECREF(et->name);
Py_XDECREF(et->slots);
type->ob_type->tp_free((PyObject *)type);
return _Py_HashPointer(self);
}
-static void
-object_free(PyObject *self)
-{
- PyObject_Del(self);
-}
-
static PyObject *
object_get_class(PyObject *self, void *closure)
{
object_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
PyType_GenericNew, /* tp_new */
- object_free, /* tp_free */
+ _PyObject_Del, /* tp_free */
};
}
static
-void _PyUnicode_Free(register PyUnicodeObject *unicode)
+void unicode_dealloc(register PyUnicodeObject *unicode)
{
+ if (!PyUnicode_CheckExact(unicode)) {
+ unicode->ob_type->tp_free((PyObject *)unicode);
+ return;
+ }
if (unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) {
/* Keep-Alive optimization */
if (unicode->length >= KEEPALIVE_SIZE_LIMIT) {
sizeof(PyUnicodeObject), /* tp_size */
0, /* tp_itemsize */
/* Slots */
- (destructor)_PyUnicode_Free, /* tp_dealloc */
+ (destructor)unicode_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_init */
0, /* tp_alloc */
unicode_new, /* tp_new */
+ _PyObject_Del, /* tp_free */
};
/* Initialize the Unicode implementation */