ยปCore Development>Code coverage>Lib/lib2to3/tests/pytree_idempotency.py

Python code coverage for Lib/lib2to3/tests/pytree_idempotency.py

#countcontent
1n/a#!/usr/bin/env python3
2n/a# Copyright 2006 Google, Inc. All Rights Reserved.
3n/a# Licensed to PSF under a Contributor Agreement.
4n/a
5n/a"""Main program for testing the infrastructure."""
6n/a
7n/afrom __future__ import print_function
8n/a
9n/a__author__ = "Guido van Rossum <guido@python.org>"
10n/a
11n/a# Support imports (need to be imported first)
12n/afrom . import support
13n/a
14n/a# Python imports
15n/aimport os
16n/aimport sys
17n/aimport logging
18n/a
19n/a# Local imports
20n/afrom .. import pytree
21n/afrom .. import pgen2
22n/afrom ..pgen2 import driver
23n/a
24n/alogging.basicConfig()
25n/a
26n/adef main():
27n/a gr = driver.load_grammar("Grammar.txt")
28n/a dr = driver.Driver(gr, convert=pytree.convert)
29n/a
30n/a fn = "example.py"
31n/a tree = dr.parse_file(fn, debug=True)
32n/a if not diff(fn, tree):
33n/a print("No diffs.")
34n/a if not sys.argv[1:]:
35n/a return # Pass a dummy argument to run the complete test suite below
36n/a
37n/a problems = []
38n/a
39n/a # Process every imported module
40n/a for name in sys.modules:
41n/a mod = sys.modules[name]
42n/a if mod is None or not hasattr(mod, "__file__"):
43n/a continue
44n/a fn = mod.__file__
45n/a if fn.endswith(".pyc"):
46n/a fn = fn[:-1]
47n/a if not fn.endswith(".py"):
48n/a continue
49n/a print("Parsing", fn, file=sys.stderr)
50n/a tree = dr.parse_file(fn, debug=True)
51n/a if diff(fn, tree):
52n/a problems.append(fn)
53n/a
54n/a # Process every single module on sys.path (but not in packages)
55n/a for dir in sys.path:
56n/a try:
57n/a names = os.listdir(dir)
58n/a except OSError:
59n/a continue
60n/a print("Scanning", dir, "...", file=sys.stderr)
61n/a for name in names:
62n/a if not name.endswith(".py"):
63n/a continue
64n/a print("Parsing", name, file=sys.stderr)
65n/a fn = os.path.join(dir, name)
66n/a try:
67n/a tree = dr.parse_file(fn, debug=True)
68n/a except pgen2.parse.ParseError as err:
69n/a print("ParseError:", err)
70n/a else:
71n/a if diff(fn, tree):
72n/a problems.append(fn)
73n/a
74n/a # Show summary of problem files
75n/a if not problems:
76n/a print("No problems. Congratulations!")
77n/a else:
78n/a print("Problems in following files:")
79n/a for fn in problems:
80n/a print("***", fn)
81n/a
82n/adef diff(fn, tree):
83n/a f = open("@", "w")
84n/a try:
85n/a f.write(str(tree))
86n/a finally:
87n/a f.close()
88n/a try:
89n/a return os.system("diff -u %s @" % fn)
90n/a finally:
91n/a os.remove("@")
92n/a
93n/aif __name__ == "__main__":
94n/a main()