Ignore:
Timestamp:
Mar 19, 2014, 11:11:30 AM (11 years ago)
Author:
dmik
Message:

python: Update vendor to 2.7.6.

Location:
python/vendor/current/Demo/parser
Files:
1 added
1 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • python/vendor/current/Demo/parser/README

    r2 r388  
    77------
    88
    9         FILES        -- list of files associated with the parser module.
     9    FILES        -- list of files associated with the parser module.
    1010
    11         README       -- this file.
     11    README       -- this file.
    1212
    13         example.py   -- module that uses the `parser' module to extract
    14                         information from the parse tree of Python source
    15                         code.
     13    docstring.py -- sample source file containing only a module docstring.
    1614
    17         docstring.py -- sample source file containing only a module docstring.
     15    example.py   -- module that uses the `parser' module to extract
     16                    information from the parse tree of Python source
     17                    code.
    1818
    19         simple.py    -- sample source containing a "short form" definition.
     19    simple.py    -- sample source containing a "short form" definition.
    2020
    21         source.py    -- sample source code used to demonstrate ability to
    22                         handle nested constructs easily using the functions
    23                         and classes in example.py.
     21    source.py    -- sample source code used to demonstrate ability to
     22                    handle nested constructs easily using the functions
     23                    and classes in example.py.
    2424
    25         test_parser.py  program to put the parser module through its paces.
     25    test_parser.py  program to put the parser module through its paces.
    2626
    27         unparse.py      AST (2.5) based example to recreate source code
    28                         from an AST. This is incomplete; contributions
    29                         are welcome.
     27    test_unparse.py tests for the unparse module
     28
     29    unparse.py      AST (2.7) based example to recreate source code
     30                    from an AST.
    3031
    3132Enjoy!
  • python/vendor/current/Demo/parser/test_parser.py

    r2 r388  
    1212    print '----', fileName,
    1313    try:
    14         ast = parser.suite(t)
    15         tup = parser.ast2tuple(ast)
    16         # this discards the first AST; a huge memory savings when running
     14        st = parser.suite(t)
     15        tup = parser.st2tuple(st)
     16        # this discards the first ST; a huge memory savings when running
    1717        # against a large source file like Tkinter.py.
    18         ast = None
    19         new = parser.tuple2ast(tup)
     18        st = None
     19        new = parser.tuple2st(tup)
    2020    except parser.ParserError, err:
    2121        print
     
    2424        _numFailed = _numFailed + 1
    2525    else:
    26         if tup != parser.ast2tuple(new):
     26        if tup != parser.st2tuple(new):
    2727            print
    2828            print 'parser module failed on input file', fileName
  • python/vendor/current/Demo/parser/unparse.py

    r2 r388  
    11"Usage: unparse.py <path to source file>"
    22import sys
    3 import _ast
     3import ast
    44import cStringIO
    55import os
     6
     7# Large float and imaginary literals get turned into infinities in the AST.
     8# We unparse those infinities to INFSTR.
     9INFSTR = "1e" + repr(sys.float_info.max_10_exp + 1)
    610
    711def interleave(inter, f, seq):
     
    1014    seq = iter(seq)
    1115    try:
    12         f(seq.next())
     16        f(next(seq))
    1317    except StopIteration:
    1418        pass
     
    2125    """Methods in this class recursively traverse an AST and
    2226    output source code for the abstract syntax; original formatting
    23     is disregarged. """
     27    is disregarded. """
    2428
    2529    def __init__(self, tree, file = sys.stdout):
     
    2731         Print the source for tree to file."""
    2832        self.f = file
     33        self.future_imports = []
    2934        self._indent = 0
    3035        self.dispatch(tree)
    31         print >>self.f,""
     36        self.f.write("")
    3237        self.f.flush()
    3338
     
    8085
    8186    def _ImportFrom(self, t):
     87        # A from __future__ import may affect unparsing, so record it.
     88        if t.module and t.module == '__future__':
     89            self.future_imports.extend(n.name for n in t.names)
     90
    8291        self.fill("from ")
    83         self.write(t.module)
     92        self.write("." * t.level)
     93        if t.module:
     94            self.write(t.module)
    8495        self.write(" import ")
    8596        interleave(lambda: self.write(", "), self.dispatch, t.names)
    86         # XXX(jpe) what is level for?
    8797
    8898    def _Assign(self, t):
     
    116126    def _Delete(self, t):
    117127        self.fill("del ")
    118         self.dispatch(t.targets)
     128        interleave(lambda: self.write(", "), self.dispatch, t.targets)
    119129
    120130    def _Assert(self, t):
     
    187197
    188198    def _TryFinally(self, t):
    189         self.fill("try")
    190         self.enter()
    191         self.dispatch(t.body)
    192         self.leave()
     199        if len(t.body) == 1 and isinstance(t.body[0], ast.TryExcept):
     200            # try-except-finally
     201            self.dispatch(t.body)
     202        else:
     203            self.fill("try")
     204            self.enter()
     205            self.dispatch(t.body)
     206            self.leave()
    193207
    194208        self.fill("finally")
     
    203217            self.dispatch(t.type)
    204218        if t.name:
    205             self.write(", ")
     219            self.write(" as ")
    206220            self.dispatch(t.name)
    207221        self.enter()
     
    211225    def _ClassDef(self, t):
    212226        self.write("\n")
     227        for deco in t.decorator_list:
     228            self.fill("@")
     229            self.dispatch(deco)
    213230        self.fill("class "+t.name)
    214231        if t.bases:
     
    246263            self.enter()
    247264            self.dispatch(t.orelse)
    248             self.leave
     265            self.leave()
    249266
    250267    def _If(self, t):
     
    252269        self.dispatch(t.test)
    253270        self.enter()
    254         # XXX elif?
    255         self.dispatch(t.body)
    256         self.leave()
     271        self.dispatch(t.body)
     272        self.leave()
     273        # collapse nested ifs into equivalent elifs.
     274        while (t.orelse and len(t.orelse) == 1 and
     275               isinstance(t.orelse[0], ast.If)):
     276            t = t.orelse[0]
     277            self.fill("elif ")
     278            self.dispatch(t.test)
     279            self.enter()
     280            self.dispatch(t.body)
     281            self.leave()
     282        # final else
    257283        if t.orelse:
    258284            self.fill("else")
     
    271297            self.enter()
    272298            self.dispatch(t.orelse)
    273             self.leave
     299            self.leave()
    274300
    275301    def _With(self, t):
     
    285311    # expr
    286312    def _Str(self, tree):
    287         self.write(repr(tree.s))
     313        # if from __future__ import unicode_literals is in effect,
     314        # then we want to output string literals using a 'b' prefix
     315        # and unicode literals with no prefix.
     316        if "unicode_literals" not in self.future_imports:
     317            self.write(repr(tree.s))
     318        elif isinstance(tree.s, str):
     319            self.write("b" + repr(tree.s))
     320        elif isinstance(tree.s, unicode):
     321            self.write(repr(tree.s).lstrip("u"))
     322        else:
     323            assert False, "shouldn't get here"
    288324
    289325    def _Name(self, t):
     
    296332
    297333    def _Num(self, t):
    298         self.write(repr(t.n))
     334        repr_n = repr(t.n)
     335        # Parenthesize negative numbers, to avoid turning (-1)**2 into -1**2.
     336        if repr_n.startswith("-"):
     337            self.write("(")
     338        # Substitute overflowing decimal literal for AST infinities.
     339        self.write(repr_n.replace("inf", INFSTR))
     340        if repr_n.startswith("-"):
     341            self.write(")")
    299342
    300343    def _List(self, t):
     
    316359            self.dispatch(gen)
    317360        self.write(")")
     361
     362    def _SetComp(self, t):
     363        self.write("{")
     364        self.dispatch(t.elt)
     365        for gen in t.generators:
     366            self.dispatch(gen)
     367        self.write("}")
     368
     369    def _DictComp(self, t):
     370        self.write("{")
     371        self.dispatch(t.key)
     372        self.write(": ")
     373        self.dispatch(t.value)
     374        for gen in t.generators:
     375            self.dispatch(gen)
     376        self.write("}")
    318377
    319378    def _comprehension(self, t):
     
    335394        self.write(")")
    336395
     396    def _Set(self, t):
     397        assert(t.elts) # should be at least one element
     398        self.write("{")
     399        interleave(lambda: self.write(", "), self.dispatch, t.elts)
     400        self.write("}")
     401
    337402    def _Dict(self, t):
    338403        self.write("{")
    339         def writem((k, v)):
     404        def write_pair(pair):
     405            (k, v) = pair
    340406            self.dispatch(k)
    341407            self.write(": ")
    342408            self.dispatch(v)
    343         interleave(lambda: self.write(", "), writem, zip(t.keys, t.values))
     409        interleave(lambda: self.write(", "), write_pair, zip(t.keys, t.values))
    344410        self.write("}")
    345411
     
    356422    unop = {"Invert":"~", "Not": "not", "UAdd":"+", "USub":"-"}
    357423    def _UnaryOp(self, t):
     424        self.write("(")
    358425        self.write(self.unop[t.op.__class__.__name__])
    359         self.write("(")
    360         self.dispatch(t.operand)
     426        self.write(" ")
     427        # If we're applying unary minus to a number, parenthesize the number.
     428        # This is necessary: -2147483648 is different from -(2147483648) on
     429        # a 32-bit machine (the first is an int, the second a long), and
     430        # -7j is different from -(7j).  (The first has real part 0.0, the second
     431        # has real part -0.0.)
     432        if isinstance(t.op, ast.USub) and isinstance(t.operand, ast.Num):
     433            self.write("(")
     434            self.dispatch(t.operand)
     435            self.write(")")
     436        else:
     437            self.dispatch(t.operand)
    361438        self.write(")")
    362439
    363440    binop = { "Add":"+", "Sub":"-", "Mult":"*", "Div":"/", "Mod":"%",
    364                     "LShift":">>", "RShift":"<<", "BitOr":"|", "BitXor":"^", "BitAnd":"&",
     441                    "LShift":"<<", "RShift":">>", "BitOr":"|", "BitXor":"^", "BitAnd":"&",
    365442                    "FloorDiv":"//", "Pow": "**"}
    366443    def _BinOp(self, t):
     
    379456            self.write(" " + self.cmpops[o.__class__.__name__] + " ")
    380457            self.dispatch(e)
    381             self.write(")")
    382 
    383     boolops = {_ast.And: 'and', _ast.Or: 'or'}
     458        self.write(")")
     459
     460    boolops = {ast.And: 'and', ast.Or: 'or'}
    384461    def _BoolOp(self, t):
    385462        self.write("(")
     
    390467    def _Attribute(self,t):
    391468        self.dispatch(t.value)
     469        # Special case: 3.__abs__() is a syntax error, so if t.value
     470        # is an integer literal then we need to either parenthesize
     471        # it or add an extra space to get 3 .__abs__().
     472        if isinstance(t.value, ast.Num) and isinstance(t.value.n, int):
     473            self.write(" ")
    392474        self.write(".")
    393475        self.write(t.attr)
     
    446528    def _arguments(self, t):
    447529        first = True
    448         nonDef = len(t.args)-len(t.defaults)
    449         for a in t.args[0:nonDef]:
    450             if first:first = False
    451             else: self.write(", ")
    452             self.dispatch(a)
    453         for a,d in zip(t.args[nonDef:], t.defaults):
     530        # normal arguments
     531        defaults = [None] * (len(t.args) - len(t.defaults)) + t.defaults
     532        for a,d in zip(t.args, defaults):
    454533            if first:first = False
    455534            else: self.write(", ")
    456535            self.dispatch(a),
    457             self.write("=")
    458             self.dispatch(d)
     536            if d:
     537                self.write("=")
     538                self.dispatch(d)
     539
     540        # varargs
    459541        if t.vararg:
    460542            if first:first = False
    461543            else: self.write(", ")
    462             self.write("*"+t.vararg)
     544            self.write("*")
     545            self.write(t.vararg)
     546
     547        # kwargs
    463548        if t.kwarg:
    464549            if first:first = False
     
    472557
    473558    def _Lambda(self, t):
     559        self.write("(")
    474560        self.write("lambda ")
    475561        self.dispatch(t.args)
    476562        self.write(": ")
    477563        self.dispatch(t.body)
     564        self.write(")")
    478565
    479566    def _alias(self, t):
     
    483570
    484571def roundtrip(filename, output=sys.stdout):
    485     source = open(filename).read()
    486     tree = compile(source, filename, "exec", _ast.PyCF_ONLY_AST)
     572    with open(filename, "r") as pyfile:
     573        source = pyfile.read()
     574    tree = compile(source, filename, "exec", ast.PyCF_ONLY_AST)
    487575    Unparser(tree, output)
    488576
     
    493581        names = [n for n in os.listdir(a) if n.endswith('.py')]
    494582    except OSError:
    495         print >> sys.stderr, "Directory not readable: %s" % a
     583        sys.stderr.write("Directory not readable: %s" % a)
    496584    else:
    497585        for n in names:
     
    502590                try:
    503591                    roundtrip(fullname, output)
    504                 except Exception, e:
     592                except Exception as e:
    505593                    print '  Failed to compile, exception is %s' % repr(e)
    506594            elif os.path.isdir(fullname):
Note: See TracChangeset for help on using the changeset viewer.