| # Copyright 2004-2005 Elemental Security, Inc. All Rights Reserved. |
| # Licensed to PSF under a Contributor Agreement. |
| """Safely evaluate Python string literals without using eval().""" |
| simple_escapes = {"a": "\a", |
| all, tail = m.group(0, 1) |
| assert all.startswith("\\") |
| esc = simple_escapes.get(tail) |
| raise ValueError("invalid hex string escape ('\\%s')" % tail) |
| raise ValueError("invalid hex string escape ('\\%s')" % tail) |
| raise ValueError("invalid octal string escape ('\\%s')" % tail) |
| assert s.startswith("'") or s.startswith('"'), repr(s[:1]) |
| assert s.endswith(q), repr(s[-len(q):]) |
| assert len(s) >= 2*len(q) |
| return re.sub(r"\\(\'|\"|\\|[abfnrtv]|x.{0,2}|[0-7]{1,3})", escape, s) |
| if __name__ == "__main__": |