]>
crepu.dev Git - config.git/blob - djavu-asus/emacs/elpy/rpc-venv/lib/python3.11/site-packages/blib2to3/pgen2/literals.py
53c0b8ac2bbb2984a8b78d03261d8c7ebd0eb5ca
1 # Copyright 2004-2005 Elemental Security, Inc. All Rights Reserved.
2 # Licensed to PSF under a Contributor Agreement.
4 """Safely evaluate Python string literals without using eval()."""
7 from typing
import Dict
, Match
9 simple_escapes
: Dict
[str, str] = {
23 def escape(m
: Match
[str]) -> str:
24 all
, tail
= m
.group(0, 1)
25 assert all
.startswith("\\")
26 esc
= simple_escapes
.get(tail
)
29 if tail
.startswith("x"):
32 raise ValueError("invalid hex string escape ('\\%s')" % tail
)
36 raise ValueError("invalid hex string escape ('\\%s')" % tail
) from None
41 raise ValueError("invalid octal string escape ('\\%s')" % tail
) from None
45 def evalString(s
: str) -> str:
46 assert s
.startswith("'") or s
.startswith('"'), repr(s
[:1])
50 assert s
.endswith(q
), repr(s
[-len(q
) :])
51 assert len(s
) >= 2 * len(q
)
52 s
= s
[len(q
) : -len(q
)]
53 return re
.sub(r
"\\(\'|\"|
\\|
[abfnrtv
]|x
.{0,2}|
[0-7]{1,3})", escape, s)
65 if __name__ == "__main__
":