X-Git-Url: https://git.madduck.net/etc/vim.git/blobdiff_plain/6aef6c9d458e8df88f510e94c18f216232b6a786..729f2d8cafd1b8e44d7c0a6bd841453ffac01c8e:/blib2to3/pgen2/literals.py?ds=sidebyside diff --git a/blib2to3/pgen2/literals.py b/blib2to3/pgen2/literals.py index baa17e1..b5fe428 100644 --- a/blib2to3/pgen2/literals.py +++ b/blib2to3/pgen2/literals.py @@ -3,20 +3,26 @@ """Safely evaluate Python string literals without using eval().""" -import regex as re - -simple_escapes = {"a": "\a", - "b": "\b", - "f": "\f", - "n": "\n", - "r": "\r", - "t": "\t", - "v": "\v", - "'": "'", - '"': '"', - "\\": "\\"} - -def escape(m): +import re + +from typing import Dict, Match, Text + + +simple_escapes: Dict[Text, Text] = { + "a": "\a", + "b": "\b", + "f": "\f", + "n": "\n", + "r": "\r", + "t": "\t", + "v": "\v", + "'": "'", + '"': '"', + "\\": "\\", +} + + +def escape(m: Match[Text]) -> Text: all, tail = m.group(0, 1) assert all.startswith("\\") esc = simple_escapes.get(tail) @@ -37,17 +43,19 @@ def escape(m): raise ValueError("invalid octal string escape ('\\%s')" % tail) from None return chr(i) -def evalString(s): + +def evalString(s: Text) -> Text: assert s.startswith("'") or s.startswith('"'), repr(s[:1]) q = s[0] - if s[:3] == q*3: - q = q*3 - assert s.endswith(q), repr(s[-len(q):]) - assert len(s) >= 2*len(q) - s = s[len(q):-len(q)] + if s[:3] == q * 3: + q = q * 3 + assert s.endswith(q), repr(s[-len(q) :]) + assert len(s) >= 2 * len(q) + s = s[len(q) : -len(q)] return re.sub(r"\\(\'|\"|\\|[abfnrtv]|x.{0,2}|[0-7]{1,3})", escape, s) -def test(): + +def test() -> None: for i in range(256): c = chr(i) s = repr(c)