diff Parser/node.c @ 78390:aceb975c4832 2.7

Closes #15512: Correct __sizeof__ support for parser
author Jesus Cea <jcea@jcea.es>
date Fri, 03 Aug 2012 14:25:53 +0200
parents fd25030223b0
children 99112b851b25
line wrap: on
line diff
--- a/Parser/node.c	Thu Aug 02 20:37:12 2012 +0200
+++ b/Parser/node.c	Fri Aug 03 14:25:53 2012 +0200
@@ -114,6 +114,7 @@
 
 /* Forward */
 static void freechildren(node *);
+static Py_ssize_t sizeofchildren(node *n);
 
 
 void
@@ -125,6 +126,16 @@
     }
 }
 
+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 @@
     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;
+}