]> git.madduck.net Git - etc/vim.git/blob - src/blib2to3/pgen2/literals.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

grammar: accept open sequences on match subject (GH-2639)
[etc/vim.git] / src / blib2to3 / pgen2 / literals.py
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 from typing import Dict, Match, Text
9
10
11 simple_escapes: Dict[Text, Text] = {
12     "a": "\a",
13     "b": "\b",
14     "f": "\f",
15     "n": "\n",
16     "r": "\r",
17     "t": "\t",
18     "v": "\v",
19     "'": "'",
20     '"': '"',
21     "\\": "\\",
22 }
23
24
25 def escape(m: Match[Text]) -> Text:
26     all, tail = m.group(0, 1)
27     assert all.startswith("\\")
28     esc = simple_escapes.get(tail)
29     if esc is not None:
30         return esc
31     if tail.startswith("x"):
32         hexes = tail[1:]
33         if len(hexes) < 2:
34             raise ValueError("invalid hex string escape ('\\%s')" % tail)
35         try:
36             i = int(hexes, 16)
37         except ValueError:
38             raise ValueError("invalid hex string escape ('\\%s')" % tail) from None
39     else:
40         try:
41             i = int(tail, 8)
42         except ValueError:
43             raise ValueError("invalid octal string escape ('\\%s')" % tail) from None
44     return chr(i)
45
46
47 def evalString(s: Text) -> Text:
48     assert s.startswith("'") or s.startswith('"'), repr(s[:1])
49     q = s[0]
50     if s[:3] == q * 3:
51         q = q * 3
52     assert s.endswith(q), repr(s[-len(q) :])
53     assert len(s) >= 2 * len(q)
54     s = s[len(q) : -len(q)]
55     return re.sub(r"\\(\'|\"|\\|[abfnrtv]|x.{0,2}|[0-7]{1,3})", escape, s)
56
57
58 def test() -> None:
59     for i in range(256):
60         c = chr(i)
61         s = repr(c)
62         e = evalString(s)
63         if e != c:
64             print(i, c, s, e)
65
66
67 if __name__ == "__main__":
68     test()