~inkscape.dev/inkscape-devlibs64/trunk

« back to all changes in this revision

Viewing changes to python/Lib/lib2to3/pgen2/literals.py

  • Committer: Eduard Braun
  • Date: 2016-10-22 16:51:19 UTC
  • Revision ID: eduard.braun2@gmx.de-20161022165119-9eosgy6lp8j1kzli
Update Python to version 2.7.12

Included modules:
  coverage 4.2
  lxml 3.6.4
  numpy 1.11.2
  scour 0.35
  six 1.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2004-2005 Elemental Security, Inc. All Rights Reserved.
2
 
# Licensed to PSF under a Contributor Agreement.
3
 
 
4
 
"""Safely evaluate Python string literals without using eval()."""
5
 
 
6
 
import re
7
 
 
8
 
simple_escapes = {"a": "\a",
9
 
                  "b": "\b",
10
 
                  "f": "\f",
11
 
                  "n": "\n",
12
 
                  "r": "\r",
13
 
                  "t": "\t",
14
 
                  "v": "\v",
15
 
                  "'": "'",
16
 
                  '"': '"',
17
 
                  "\\": "\\"}
18
 
 
19
 
def escape(m):
20
 
    all, tail = m.group(0, 1)
21
 
    assert all.startswith("\\")
22
 
    esc = simple_escapes.get(tail)
23
 
    if esc is not None:
24
 
        return esc
25
 
    if tail.startswith("x"):
26
 
        hexes = tail[1:]
27
 
        if len(hexes) < 2:
28
 
            raise ValueError("invalid hex string escape ('\\%s')" % tail)
29
 
        try:
30
 
            i = int(hexes, 16)
31
 
        except ValueError:
32
 
            raise ValueError("invalid hex string escape ('\\%s')" % tail)
33
 
    else:
34
 
        try:
35
 
            i = int(tail, 8)
36
 
        except ValueError:
37
 
            raise ValueError("invalid octal string escape ('\\%s')" % tail)
38
 
    return chr(i)
39
 
 
40
 
def evalString(s):
41
 
    assert s.startswith("'") or s.startswith('"'), repr(s[:1])
42
 
    q = s[0]
43
 
    if s[:3] == q*3:
44
 
        q = q*3
45
 
    assert s.endswith(q), repr(s[-len(q):])
46
 
    assert len(s) >= 2*len(q)
47
 
    s = s[len(q):-len(q)]
48
 
    return re.sub(r"\\(\'|\"|\\|[abfnrtv]|x.{0,2}|[0-7]{1,3})", escape, s)
49
 
 
50
 
def test():
51
 
    for i in range(256):
52
 
        c = chr(i)
53
 
        s = repr(c)
54
 
        e = evalString(s)
55
 
        if e != c:
56
 
            print i, c, s, e
57
 
 
58
 
 
59
 
if __name__ == "__main__":
60
 
    test()
 
1
# Copyright 2004-2005 Elemental Security, Inc. All Rights Reserved.
 
2
# Licensed to PSF under a Contributor Agreement.
 
3
 
 
4
"""Safely evaluate Python string literals without using eval()."""
 
5
 
 
6
import re
 
7
 
 
8
simple_escapes = {"a": "\a",
 
9
                  "b": "\b",
 
10
                  "f": "\f",
 
11
                  "n": "\n",
 
12
                  "r": "\r",
 
13
                  "t": "\t",
 
14
                  "v": "\v",
 
15
                  "'": "'",
 
16
                  '"': '"',
 
17
                  "\\": "\\"}
 
18
 
 
19
def escape(m):
 
20
    all, tail = m.group(0, 1)
 
21
    assert all.startswith("\\")
 
22
    esc = simple_escapes.get(tail)
 
23
    if esc is not None:
 
24
        return esc
 
25
    if tail.startswith("x"):
 
26
        hexes = tail[1:]
 
27
        if len(hexes) < 2:
 
28
            raise ValueError("invalid hex string escape ('\\%s')" % tail)
 
29
        try:
 
30
            i = int(hexes, 16)
 
31
        except ValueError:
 
32
            raise ValueError("invalid hex string escape ('\\%s')" % tail)
 
33
    else:
 
34
        try:
 
35
            i = int(tail, 8)
 
36
        except ValueError:
 
37
            raise ValueError("invalid octal string escape ('\\%s')" % tail)
 
38
    return chr(i)
 
39
 
 
40
def evalString(s):
 
41
    assert s.startswith("'") or s.startswith('"'), repr(s[:1])
 
42
    q = s[0]
 
43
    if s[:3] == q*3:
 
44
        q = q*3
 
45
    assert s.endswith(q), repr(s[-len(q):])
 
46
    assert len(s) >= 2*len(q)
 
47
    s = s[len(q):-len(q)]
 
48
    return re.sub(r"\\(\'|\"|\\|[abfnrtv]|x.{0,2}|[0-7]{1,3})", escape, s)
 
49
 
 
50
def test():
 
51
    for i in range(256):
 
52
        c = chr(i)
 
53
        s = repr(c)
 
54
        e = evalString(s)
 
55
        if e != c:
 
56
            print i, c, s, e
 
57
 
 
58
 
 
59
if __name__ == "__main__":
 
60
    test()