from random
import random
class CompilerTest(unittest
.TestCase
):
def testCompileLibrary(self
):
# A simple but large test. Compile all the code in the
# standard library and its test suite. This doesn't verify
# that any of the code is correct, merely the compiler is able
# to generate some kind of code for it.
libdir
= os
.path
.dirname(unittest
.__file
__)
testdir
= os
.path
.dirname(test
.test_support
.__file
__)
for dir in [libdir
, testdir
]:
for basename
in os
.listdir(dir):
if not basename
.endswith(".py"):
if not TEST_ALL
and random() < 0.98:
path
= os
.path
.join(dir, basename
)
if test
.test_support
.verbose
:
if "badsyntax" in basename
:
self
.assertRaises(SyntaxError, compiler
.compile,
compiler
.compile(buf
, basename
, "exec")
# Test that all nodes except Module have a correct lineno attribute.
if filename
.endswith(".pyc") or filename
.endswith(".pyo"):
tree
= compiler
.parseFile(filename
)
def check_lineno(self
, node
):
print node
.__class
__, node
.lineno
def _check_lineno(self
, node
):
if not node
.__class
__ in NOLINENO
:
self
.assert_(isinstance(node
.lineno
, int),
"lineno=%s on %s" % (node
.lineno
, node
.__class
__))
self
.assert_(node
.lineno
> 0,
"lineno=%s on %s" % (node
.lineno
, node
.__class
__))
for child
in node
.getChildNodes():
NOLINENO
= (compiler
.ast
.Module
, compiler
.ast
.Stmt
, compiler
.ast
.Discard
)
###############################################################################
# code below is just used to trigger some possible errors, for the benefit of
###############################################################################
l
= [(x
, y
) for x
, y
in zip(range(5), range(5,10))]
###############################################################################
TEST_ALL
= test
.test_support
.is_resource_enabled("compiler")
test
.test_support
.run_unittest(CompilerTest
)
if __name__
== "__main__":