This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add a test which uses the ast module to compile the stdlib
Type: Stage: test needed
Components: Tests Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, iritkatriel, rhettinger, terry.reedy, yselivanov
Priority: normal Keywords:

Created on 2015-09-01 20:49 by brett.cannon, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
temast.py terry.reedy, 2015-09-04 19:54
tem_ast2.py terry.reedy, 2015-09-04 21:58
Messages (8)
msg249516 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2015-09-01 20:49
Issue #24975 shows that we can easily end up with holes in support from the ast module. We should probably make sure we have a test that goes through the entire stdlib, Does an ast.parse(), and then passes the result to compile() to verify no exceptions are raised. We should either avoid the test/ directory or blacklist specific files which will fail because they are designed to be syntactically incorrect. The test can also be behind a -u flag if it turns out to be an expensive test.
msg249526 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-09-02 04:07
+1 This is a great idea.
msg249817 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-09-04 19:54
The attached recurses one level deep in Lib. Catching UnicodeDecodeError on file read is needed at least for "test/test_source_encoding.py 'utf-8' codec can't decode byte 0xf0 in position 267: invalid continuation byte".  Catching "badxxx.py" is needed for several test/ modules.

This test may have found another hole:  It prints

 "C:/programs/Python35/Lib\distutils _msvccompiler.py None disallowed in expression list"

whereas

>>> compile(open('C:/programs/Python35/Lib\distutils\_msvccompiler.py').read(), '', 'exec')
<code object <module> at 0x00000000034A3F60, file "", line 8>
msg249818 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-09-04 19:55
Forgot to mention: runtime 4 seconds.
msg249819 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-09-04 19:56
Ran with installed 3.5.0rc1.
msg249826 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2015-09-04 20:30
Two things about the UTF-8 decode issue. One is you don't need to decode the source for it to be compiled; ast.parse()/compile() will work from bytes objects and thus handle the decoded for you. Two, if you want to decode source files into text, use importlib.util.decode_source().

Since Terry's script shows a complete compilation of the stdlib is very fast, we should make this a proper test. Any resulting test should probably should go into test_compile(). We can have it read all files and those that raise a a SyntaxError or some such exception can simply be skipped as the test is about AST -> code and not source -> AST (although we could theoretically have the test validate the failure with a source -> code compilation as well and verify the same exception is raised). We can also double-check that any AST -> code failure passes under source -> code and then report a failure (basically I'm trying to avoid blacklisting files that are test cases for the compiler overall and are known to fail).
msg249839 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-09-04 21:58
The distutils error is a ValueError, which was printed as such before I added 'Exception' to the except statement.

I did not initially catch SyntaxError separately because some are caught during the compile phase. Revised file corrects 'pass' to 'continue' and continues with parse errors also.

Many test/badsyntax files have compile-time syntax errors.

SyntaxError 'await' outside function (badsyntax_async1.py, line 1)
SyntaxError 'yield' inside async function (badsyntax_async6.py, line 2)
SyntaxError from __future__ imports must occur at the beginning of the file (badsyntax_future10.py, line 3)
SyntaxError future feature rested_snopes is not defined (badsyntax_future3.py, line 3)
SyntaxError not a chance (badsyntax_future9.py, line 3)

Attached is a fully recursive version that only report compile errors for non-'badsyntax' files.  It takes about 2 seconds more. Output:

C:/programs/Python35/Lib\distutils _msvccompiler.py ValueError None disallowed in expression list
1776 (files compiled)

I excluded site-packages after testing on 3.4 because pylint has bad syntax examples giving a page of error messages.
msg400775 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-08-31 20:12
I think there is something like this here: 

https://github.com/python/cpython/blob/22fe0eb13c3441b71b60aaea0e7fe289a29783da/Lib/test/test_ast.py#L1498
History
Date User Action Args
2022-04-11 14:58:20adminsetgithub: 69169
2021-08-31 20:12:25iritkatrielsetnosy: + iritkatriel
messages: + msg400775
2015-09-04 21:58:22terry.reedysetfiles: + tem_ast2.py

messages: + msg249839
2015-09-04 20:30:34brett.cannonsetmessages: + msg249826
2015-09-04 19:56:28terry.reedysetmessages: + msg249819
2015-09-04 19:55:30terry.reedysetmessages: + msg249818
2015-09-04 19:54:15terry.reedysetfiles: + temast.py
nosy: + terry.reedy
messages: + msg249817

2015-09-02 04:07:12rhettingersetnosy: + rhettinger
messages: + msg249526
2015-09-01 21:18:29yselivanovsetnosy: + yselivanov
2015-09-01 20:49:15brett.cannoncreate