Closes #15512: Correct __sizeof__ support for parser

This commit is contained in:
Jesus Cea 2012-08-03 14:28:37 +02:00
parent a9a53c7dc0
commit e9c5318967
5 changed files with 85 additions and 1 deletions

View file

@ -114,6 +114,7 @@ PyNode_AddChild(register node *n1, int type, char *str, int lineno, int col_offs
/* Forward */
static void freechildren(node *);
static Py_ssize_t sizeofchildren(node *n);
void
@ -125,6 +126,16 @@ PyNode_Free(node *n)
}
}
Py_ssize_t
_PyNode_SizeOf(node *n)
{
Py_ssize_t res = 0;
if (n != NULL)
res = sizeof(node) + sizeofchildren(n);
return res;
}
static void
freechildren(node *n)
{
@ -136,3 +147,18 @@ freechildren(node *n)
if (STR(n) != NULL)
PyObject_FREE(STR(n));
}
static Py_ssize_t
sizeofchildren(node *n)
{
Py_ssize_t res = 0;
int i;
for (i = NCH(n); --i >= 0; )
res += sizeofchildren(CHILD(n, i));
if (n->n_child != NULL)
/* allocated size of n->n_child array */
res += XXXROUNDUP(NCH(n)) * sizeof(node);
if (STR(n) != NULL)
res += strlen(STR(n)) + 1;
return res;
}