PageRenderTime 225ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/documentor/libraries/Sphinx-1.1.3-py3.2/sphinx/pycode/pgen2/literals.py

https://github.com/tictactatic/Superdesk
Python | 96 lines | 86 code | 6 blank | 4 comment | 3 complexity | 77d61b57a3d667686c1101ae2c4484e3 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, GPL-2.0
  1. # Copyright 2004-2005 Elemental Security, Inc. All Rights Reserved.
  2. # Licensed to PSF under a Contributor Agreement.
  3. # Extended to handle raw and unicode literals by Georg Brandl.
  4. """Safely evaluate Python string literals without using eval()."""
  5. import re
  6. simple_escapes = {"a": "\a",
  7. "b": "\b",
  8. "f": "\f",
  9. "n": "\n",
  10. "r": "\r",
  11. "t": "\t",
  12. "v": "\v",
  13. "'": "'",
  14. '"': '"',
  15. "\\": "\\"}
  16. def convert_hex(x, n):
  17. if len(x) < n+1:
  18. raise ValueError("invalid hex string escape ('\\%s')" % x)
  19. try:
  20. return int(x[1:], 16)
  21. except ValueError:
  22. raise ValueError("invalid hex string escape ('\\%s')" % x)
  23. def escape(m):
  24. all, tail = m.group(0, 1)
  25. assert all.startswith("\\")
  26. esc = simple_escapes.get(tail)
  27. if esc is not None:
  28. return esc
  29. elif tail.startswith("x"):
  30. return chr(convert_hex(tail, 2))
  31. elif tail.startswith('u'):
  32. return chr(convert_hex(tail, 4))
  33. elif tail.startswith('U'):
  34. return chr(convert_hex(tail, 8))
  35. elif tail.startswith('N'):
  36. import unicodedata
  37. try:
  38. return unicodedata.lookup(tail[1:-1])
  39. except KeyError:
  40. raise ValueError("undefined character name %r" % tail[1:-1])
  41. else:
  42. try:
  43. return chr(int(tail, 8))
  44. except ValueError:
  45. raise ValueError("invalid octal string escape ('\\%s')" % tail)
  46. def escaperaw(m):
  47. all, tail = m.group(0, 1)
  48. if tail.startswith('u'):
  49. return chr(convert_hex(tail, 4))
  50. elif tail.startswith('U'):
  51. return chr(convert_hex(tail, 8))
  52. else:
  53. return all
  54. escape_re = re.compile(r"\\(\'|\"|\\|[abfnrtv]|x.{0,2}|[0-7]{1,3})")
  55. uni_escape_re = re.compile(r"\\(\'|\"|\\|[abfnrtv]|x.{0,2}|[0-7]{1,3}|"
  56. r"u[0-9a-fA-F]{0,4}|U[0-9a-fA-F]{0,8}|N\{.+?\})")
  57. def evalString(s, encoding=None):
  58. regex = escape_re
  59. repl = escape
  60. if encoding and not isinstance(s, str):
  61. s = s.decode(encoding)
  62. if s.startswith('u') or s.startswith('U'):
  63. regex = uni_escape_re
  64. s = s[1:]
  65. if s.startswith('r') or s.startswith('R'):
  66. repl = escaperaw
  67. s = s[1:]
  68. assert s.startswith("'") or s.startswith('"'), repr(s[:1])
  69. q = s[0]
  70. if s[:3] == q*3:
  71. q = q*3
  72. assert s.endswith(q), repr(s[-len(q):])
  73. assert len(s) >= 2*len(q)
  74. s = s[len(q):-len(q)]
  75. return regex.sub(repl, s)
  76. def test():
  77. for i in range(256):
  78. c = chr(i)
  79. s = repr(c)
  80. e = evalString(s)
  81. if e != c:
  82. print(i, c, s, e)
  83. if __name__ == "__main__":
  84. test()