Lichen

Annotated pyparser/test/unittest_samples.py

957:c086e5c5db75
2021-11-14 Paul Boddie Added reserved argument count to the invocation generation code.
paul@437 1
"""test module for CPython / PyPy nested tuples comparison"""
paul@437 2
paul@437 3
import os, os.path as osp
paul@437 4
import sys
paul@437 5
from pyparser.pythonutil import python_parse, pypy_parse
paul@437 6
from pprint import pprint
paul@437 7
from pyparser import grammar
paul@437 8
grammar.DEBUG = False
paul@437 9
from symbol import sym_name
paul@437 10
paul@437 11
paul@437 12
def name(elt):
paul@437 13
    return "%s[%s]"% (sym_name.get(elt,elt),elt)
paul@437 14
paul@437 15
def read_samples_dir():
paul@437 16
    return [osp.join('samples', fname) for fname in os.listdir('samples') if fname.endswith('.py')]
paul@437 17
paul@437 18
def print_sym_tuple(nested, level=0, limit=15, names=False, trace=()):
paul@437 19
    buf = []
paul@437 20
    if level <= limit:
paul@437 21
        buf.append("%s(" % (" "*level))
paul@437 22
    else:
paul@437 23
        buf.append("(")
paul@437 24
    for index, elt in enumerate(nested):
paul@437 25
        # Test if debugging and if on last element of error path
paul@437 26
        if trace and not trace[1:] and index == trace[0]:
paul@437 27
            buf.append('\n----> ')
paul@437 28
        if type(elt) is int:
paul@437 29
            if names:
paul@437 30
                buf.append(name(elt))
paul@437 31
            else:
paul@437 32
                buf.append(str(elt))
paul@437 33
            buf.append(', ')
paul@437 34
        elif type(elt) is str:
paul@437 35
            buf.append(repr(elt))
paul@437 36
        else:
paul@437 37
            if level < limit:
paul@437 38
                buf.append('\n')
paul@437 39
            buf.extend(print_sym_tuple(elt, level+1, limit,
paul@437 40
                                       names, trace[1:]))
paul@437 41
    buf.append(')')
paul@437 42
    return buf
paul@437 43
paul@437 44
def assert_tuples_equal(tup1, tup2, curpos = ()):
paul@437 45
    for index, (elt1, elt2) in enumerate(zip(tup1, tup2)):
paul@437 46
        if elt1 != elt2:
paul@437 47
            if type(elt1) is tuple and type(elt2) is tuple:
paul@437 48
                assert_tuples_equal(elt1, elt2, curpos + (index,))
paul@437 49
            raise AssertionError('Found difference at %s : %s != %s' %
paul@437 50
                                 (curpos, name(elt1), name(elt2) ), curpos)
paul@437 51
paul@437 52
from time import time, clock
paul@437 53
def test_samples( samples ):
paul@437 54
    time_reports = {}
paul@437 55
    for sample in samples:
paul@437 56
        print "testing", sample
paul@437 57
        tstart1, cstart1 = time(), clock()
paul@437 58
        pypy_tuples = pypy_parse(sample)
paul@437 59
        tstart2, cstart2 = time(), clock()
paul@437 60
        python_tuples = python_parse(sample)
paul@437 61
        time_reports[sample] = (time() - tstart2, tstart2-tstart1, clock() - cstart2, cstart2-cstart1 )
paul@437 62
        #print "-"*10, "PyPy parse results", "-"*10
paul@437 63
        #print ''.join(print_sym_tuple(pypy_tuples, names=True))
paul@437 64
        #print "-"*10, "CPython parse results", "-"*10
paul@437 65
        #print ''.join(print_sym_tuple(python_tuples, names=True))
paul@437 66
        print
paul@437 67
        try:
paul@437 68
            assert_tuples_equal(pypy_tuples, python_tuples)
paul@437 69
        except AssertionError as e:
paul@437 70
            error_path = e.args[-1]
paul@437 71
            print "ERROR PATH =", error_path
paul@437 72
            print "="*80
paul@437 73
            print file(sample).read()
paul@437 74
            print "="*80
paul@437 75
            print "-"*10, "PyPy parse results", "-"*10
paul@437 76
            print ''.join(print_sym_tuple(pypy_tuples, names=True, trace=error_path))
paul@437 77
            print "-"*10, "CPython parse results", "-"*10
paul@437 78
            print ''.join(print_sym_tuple(python_tuples, names=True, trace=error_path))
paul@437 79
            print "Failed on (%s)" % sample
paul@437 80
            # raise
paul@437 81
    pprint(time_reports)
paul@437 82
paul@437 83
if __name__=="__main__":
paul@437 84
    import getopt
paul@437 85
    opts, args = getopt.getopt( sys.argv[1:], "d:", [] )
paul@437 86
    for opt, val in opts:
paul@437 87
        if opt == "-d":
paul@437 88
            pass
paul@437 89
#            set_debug(int(val))
paul@437 90
    if args:
paul@437 91
        samples = args
paul@437 92
    else:
paul@437 93
        samples = read_samples_dir()
paul@437 94
paul@437 95
    test_samples( samples )