mirror of
https://github.com/python/cpython.git
synced 2025-10-03 21:55:41 +00:00
* Simplify and speed-up list_resize(). Relying on the newly documented
invariants allows the ob_item != NULL check to be replaced with an assertion. * Added assertions to list_init() which document and verify that the tp_new slot establishes the invariants. This may preclude a future bug if a custom tp_new slot is written.
This commit is contained in:
parent
93677f075d
commit
c0aaa2db4f
1 changed files with 7 additions and 3 deletions
|
@ -19,9 +19,8 @@ list_resize(PyListObject *self, int newsize)
|
||||||
current size, then proceed with the realloc() to shrink the list.
|
current size, then proceed with the realloc() to shrink the list.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (self->allocated >= newsize &&
|
if (self->allocated >= newsize && self->ob_size < newsize + 16) {
|
||||||
self->ob_size < newsize + 16 &&
|
assert(self->ob_item != NULL || newsize == 0);
|
||||||
self->ob_item != NULL) {
|
|
||||||
self->ob_size = newsize;
|
self->ob_size = newsize;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -2314,6 +2313,11 @@ list_init(PyListObject *self, PyObject *args, PyObject *kw)
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
/* Verify list invariants established by PyType_GenericAlloc() */
|
||||||
|
assert(0 <= self->ob_size && self->ob_size <= self->allocated);
|
||||||
|
assert(self->ob_item != NULL || self->allocated == 0);
|
||||||
|
|
||||||
/* Empty previous contents */
|
/* Empty previous contents */
|
||||||
if (self->ob_item != NULL) {
|
if (self->ob_item != NULL) {
|
||||||
(void)list_clear(self);
|
(void)list_clear(self);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue