annotate Parser/node.c @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents aceb975c4832
children 99112b851b25
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14391
1fd64b3af760 Move down the INT_MAX logic, because HAVE_LIMITS_H was always undefined
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 14375
diff changeset
1 /* Parse tree node implementation */
1fd64b3af760 Move down the INT_MAX logic, because HAVE_LIMITS_H was always undefined
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 14375
diff changeset
2
14871
fb6b51033c3b More limits.h stuff in node.c.
Tim Peters <tim.peters@gmail.com>
parents: 14391
diff changeset
3 #include "Python.h"
14391
1fd64b3af760 Move down the INT_MAX logic, because HAVE_LIMITS_H was always undefined
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 14375
diff changeset
4 #include "node.h"
1fd64b3af760 Move down the INT_MAX logic, because HAVE_LIMITS_H was always undefined
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 14375
diff changeset
5 #include "errcode.h"
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
6
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 node *
13659
e78153e179fb Mass ANSIfication.
Thomas Wouters <thomas@python.org>
parents: 13447
diff changeset
8 PyNode_New(int type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
10 node *n = (node *) PyObject_MALLOC(1 * sizeof(node));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
11 if (n == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
12 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
13 n->n_type = type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
14 n->n_str = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
15 n->n_lineno = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
16 n->n_nchildren = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
17 n->n_child = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
18 return n;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
24141
40bda3e090f6 PyNode_AddChild() and fancy_roundup(): Be paranoid about int overflow.
Tim Peters <tim.peters@gmail.com>
parents: 24128
diff changeset
21 /* See comments at XXXROUNDUP below. Returns -1 on overflow. */
24128
76e9a14fb7c5 PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents: 14871
diff changeset
22 static int
76e9a14fb7c5 PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents: 14871
diff changeset
23 fancy_roundup(int n)
76e9a14fb7c5 PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents: 14871
diff changeset
24 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
25 /* Round up to the closest power of 2 >= n. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
26 int result = 256;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
27 assert(n > 128);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
28 while (result < n) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
29 result <<= 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
30 if (result <= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
31 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
32 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
33 return result;
24128
76e9a14fb7c5 PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents: 14871
diff changeset
34 }
76e9a14fb7c5 PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents: 14871
diff changeset
35
76e9a14fb7c5 PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents: 14871
diff changeset
36 /* A gimmick to make massive numbers of reallocs quicker. The result is
24241
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
37 * a number >= the input. In PyNode_AddChild, it's used like so, when
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
38 * we're about to add child number current_size + 1:
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
39 *
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
40 * if XXXROUNDUP(current_size) < XXXROUNDUP(current_size + 1):
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
41 * allocate space for XXXROUNDUP(current_size + 1) total children
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
42 * else:
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
43 * we already have enough space
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
44 *
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
45 * Since a node starts out empty, we must have
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
46 *
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
47 * XXXROUNDUP(0) < XXXROUNDUP(1)
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
48 *
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
49 * so that we allocate space for the first child. One-child nodes are very
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
50 * common (presumably that would change if we used a more abstract form
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
51 * of syntax tree), so to avoid wasting memory it's desirable that
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
52 * XXXROUNDUP(1) == 1. That in turn forces XXXROUNDUP(0) == 0.
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
53 *
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
54 * Else for 2 <= n <= 128, we round up to the closest multiple of 4. Why 4?
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
55 * Rounding up to a multiple of an exact power of 2 is very efficient, and
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
56 * most nodes with more than one child have <= 4 kids.
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
57 *
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
58 * Else we call fancy_roundup() to grow proportionately to n. We've got an
24128
76e9a14fb7c5 PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents: 14871
diff changeset
59 * extreme case then (like test_longexp.py), and on many platforms doing
76e9a14fb7c5 PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents: 14871
diff changeset
60 * anything less than proportional growth leads to exorbitant runtime
76e9a14fb7c5 PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents: 14871
diff changeset
61 * (e.g., MacPython), or extreme fragmentation of user address space (e.g.,
76e9a14fb7c5 PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents: 14871
diff changeset
62 * Win98).
24241
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
63 *
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
64 * In a run of compileall across the 2.3a0 Lib directory, Andrew MacIntyre
37557
6ae4adefc573 SF patch #1467512, fix double free with triple quoted string in standard build.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36786
diff changeset
65 * reported that, with this scheme, 89% of PyObject_REALLOC calls in
24241
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
66 * PyNode_AddChild passed 1 for the size, and 9% passed 4. So this usually
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
67 * wastes very little memory, but is very effective at sidestepping
38631
cb3b439c29cc Fix comment typo
Andrew M. Kuchling <amk@amk.ca>
parents: 37557
diff changeset
68 * platform-realloc disasters on vulnerable platforms.
24241
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
69 *
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
70 * Note that this would be straightforward if a node stored its current
fc27ffe2036a XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents: 24141
diff changeset
71 * capacity. The code is tricky to avoid that.
24128
76e9a14fb7c5 PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents: 14871
diff changeset
72 */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
73 #define XXXROUNDUP(n) ((n) <= 1 ? (n) : \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
74 (n) <= 128 ? (((n) + 3) & ~3) : \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
75 fancy_roundup(n))
24128
76e9a14fb7c5 PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents: 14871
diff changeset
76
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
12970
63436719d12e Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 5379
diff changeset
78 int
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
79 PyNode_AddChild(register node *n1, int type, char *str, int lineno, int col_offset)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
81 const int nch = n1->n_nchildren;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
82 int current_capacity;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
83 int required_capacity;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
84 node *n;
24128
76e9a14fb7c5 PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents: 14871
diff changeset
85
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
86 if (nch == INT_MAX || nch < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
87 return E_OVERFLOW;
24128
76e9a14fb7c5 PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents: 14871
diff changeset
88
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
89 current_capacity = XXXROUNDUP(nch);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
90 required_capacity = XXXROUNDUP(nch + 1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
91 if (current_capacity < 0 || required_capacity < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
92 return E_OVERFLOW;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
93 if (current_capacity < required_capacity) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
94 if (required_capacity > PY_SIZE_MAX / sizeof(node)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
95 return E_NOMEM;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
96 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
97 n = n1->n_child;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
98 n = (node *) PyObject_REALLOC(n,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
99 required_capacity * sizeof(node));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
100 if (n == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
101 return E_NOMEM;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
102 n1->n_child = n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
103 }
24128
76e9a14fb7c5 PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents: 14871
diff changeset
104
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
105 n = &n1->n_child[n1->n_nchildren++];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
106 n->n_type = type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
107 n->n_str = str;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
108 n->n_lineno = lineno;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
109 n->n_col_offset = col_offset;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
110 n->n_nchildren = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
111 n->n_child = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
112 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 }
76
ede86799705b Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
114
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 76
diff changeset
115 /* Forward */
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
116 static void freechildren(node *);
78390
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
117 static Py_ssize_t sizeofchildren(node *n);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 76
diff changeset
118
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 76
diff changeset
119
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 76
diff changeset
120 void
13659
e78153e179fb Mass ANSIfication.
Thomas Wouters <thomas@python.org>
parents: 13447
diff changeset
121 PyNode_Free(node *n)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 76
diff changeset
122 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
123 if (n != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
124 freechildren(n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
125 PyObject_FREE(n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
126 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 76
diff changeset
127 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 76
diff changeset
128
78390
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
129 Py_ssize_t
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
130 _PyNode_SizeOf(node *n)
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
131 {
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
132 Py_ssize_t res = 0;
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
133
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
134 if (n != NULL)
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
135 res = sizeof(node) + sizeofchildren(n);
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
136 return res;
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
137 }
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
138
76
ede86799705b Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
139 static void
13659
e78153e179fb Mass ANSIfication.
Thomas Wouters <thomas@python.org>
parents: 13447
diff changeset
140 freechildren(node *n)
76
ede86799705b Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
141 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
142 int i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
143 for (i = NCH(n); --i >= 0; )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
144 freechildren(CHILD(n, i));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
145 if (n->n_child != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
146 PyObject_FREE(n->n_child);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
147 if (STR(n) != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45337
diff changeset
148 PyObject_FREE(STR(n));
76
ede86799705b Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
149 }
78390
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
150
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
151 static Py_ssize_t
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
152 sizeofchildren(node *n)
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
153 {
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
154 Py_ssize_t res = 0;
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
155 int i;
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
156 for (i = NCH(n); --i >= 0; )
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
157 res += sizeofchildren(CHILD(n, i));
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
158 if (n->n_child != NULL)
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
159 /* allocated size of n->n_child array */
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
160 res += XXXROUNDUP(NCH(n)) * sizeof(node);
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
161 if (STR(n) != NULL)
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
162 res += strlen(STR(n)) + 1;
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
163 return res;
aceb975c4832 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 61463
diff changeset
164 }