GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Use ast module instead of compiler module for parsing files. · refack/GYP3@f989ef9

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pylib/gyp
    • input.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pylib/gyp/input.py‎

Copy file name to clipboardExpand all lines: pylib/gyp/input.py
+18-29Lines 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 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.