Skip to content

Commit f989ef9

Browse files
committed
Use ast module instead of compiler module for parsing files.
This CL is part of the work getting GYP to run under Python 3. The compiler module we used to parse GYP files was removed in Python 3, so this CL switches to use the ast module instead, which exists in both Python 2 and Python 3. This change is not sufficient to run under Python 3; there's more work remaining. Things should still work under Python 2 just fine. This work is derived from [email protected]'s original work in https://codereview.chromium.org/1454433002/. Bug: gyp:36 Change-Id: I9a9835560491c3d8cd5426623484dc4a46af1d86 Reviewed-on: https://chromium-review.googlesource.com/c/1360352 Reviewed-by: Mark Mentovai <[email protected]>
1 parent 9df93ee commit f989ef9

1 file changed

Lines changed: 18 additions & 29 deletions

File tree

pylib/gyp/input.py

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@
22
# Use of this source code is governed by a BSD-style license that can be
33
# found in the LICENSE file.
44

5-
from compiler.ast import Const
6-
from compiler.ast import Dict
7-
from compiler.ast import Discard
8-
from compiler.ast import List
9-
from compiler.ast import Module
10-
from compiler.ast import Node
11-
from compiler.ast import Stmt
12-
import compiler
5+
import ast
6+
137
import gyp.common
148
import gyp.simple_copy
159
import multiprocessing
@@ -183,43 +177,38 @@ def CheckedEval(file_contents):
183177
Note that this is slower than eval() is.
184178
"""
185179

186-
ast = compiler.parse(file_contents)
187-
assert isinstance(ast, Module)
188-
c1 = ast.getChildren()
189-
assert c1[0] is None
190-
assert isinstance(c1[1], Stmt)
191-
c2 = c1[1].getChildren()
192-
assert isinstance(c2[0], Discard)
193-
c3 = c2[0].getChildren()
194-
assert len(c3) == 1
195-
return CheckNode(c3[0], [])
180+
syntax_tree = ast.parse(file_contents)
181+
assert isinstance(syntax_tree, ast.Module)
182+
c1 = syntax_tree.body
183+
assert len(c1) == 1
184+
c2 = c1[0]
185+
assert isinstance(c2, ast.Expr)
186+
return CheckNode(c2.value, [])
196187

197188

198189
def CheckNode(node, keypath):
199-
if isinstance(node, Dict):
200-
c = node.getChildren()
190+
if isinstance(node, ast.Dict):
201191
dict = {}
202-
for n in range(0, len(c), 2):
203-
assert isinstance(c[n], Const)
204-
key = c[n].getChildren()[0]
192+
for key, value in zip(node.keys, node.values):
193+
assert isinstance(key, ast.Str)
194+
key = key.s
205195
if key in dict:
206196
raise GypError("Key '" + key + "' repeated at level " +
207197
repr(len(keypath) + 1) + " with key path '" +
208198
'.'.join(keypath) + "'")
209199
kp = list(keypath) # Make a copy of the list for descending this node.
210200
kp.append(key)
211-
dict[key] = CheckNode(c[n + 1], kp)
201+
dict[key] = CheckNode(value, kp)
212202
return dict
213-
elif isinstance(node, List):
214-
c = node.getChildren()
203+
elif isinstance(node, ast.List):
215204
children = []
216-
for index, child in enumerate(c):
205+
for index, child in enumerate(node.elts):
217206
kp = list(keypath) # Copy list.
218207
kp.append(repr(index))
219208
children.append(CheckNode(child, kp))
220209
return children
221-
elif isinstance(node, Const):
222-
return node.getChildren()[0]
210+
elif isinstance(node, ast.Str):
211+
return node.s
223212
else:
224213
raise TypeError("Unknown AST node at key path '" + '.'.join(keypath) +
225214
"': " + repr(node))

0 commit comments

Comments
 (0)