raw_input.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. from .info import check_PY2
  2. check_PY2()
  3. import __builtin__ as builtins
  4. import sys
  5. from ctypes import pythonapi, c_char_p, c_void_p, py_object
  6. from .streams import STDIN, STDOUT
  7. from .readline_hook import check_encodings, stdio_readline
  8. original_raw_input = builtins.raw_input
  9. original_input = builtins.input
  10. RETURN_UNICODE = True
  11. PyOS_Readline = pythonapi.PyOS_Readline
  12. PyOS_Readline.restype = c_char_p
  13. PyOS_Readline.argtypes = [c_void_p, c_void_p, c_char_p]
  14. PyFile_AsFile = pythonapi.PyFile_AsFile
  15. PyFile_AsFile.restype = c_void_p
  16. PyFile_AsFile.argtypes = [py_object]
  17. STDIN_FILE_POINTER = PyFile_AsFile(sys.stdin)
  18. STDOUT_FILE_POINTER = PyFile_AsFile(sys.stdout)
  19. def stdout_encode(s):
  20. if isinstance(s, bytes):
  21. return s
  22. encoding = sys.stdout.encoding
  23. errors = sys.stdout.errors
  24. if errors is not None:
  25. return s.encode(encoding, errors)
  26. else:
  27. return s.encode(encoding)
  28. def stdin_encode(s):
  29. if isinstance(s, bytes):
  30. return s
  31. encoding = sys.stdin.encoding
  32. errors = sys.stdin.errors
  33. if errors is not None:
  34. return s.encode(encoding, errors)
  35. else:
  36. return s.encode(encoding)
  37. def stdin_decode(b):
  38. if isinstance(b, unicode):
  39. return b
  40. encoding = sys.stdin.encoding
  41. errors = sys.stdin.errors
  42. if errors is not None:
  43. return b.decode(encoding, errors)
  44. else:
  45. return b.decode(encoding)
  46. def readline(prompt=""):
  47. check_encodings()
  48. prompt_bytes = stdout_encode(prompt)
  49. line_bytes = PyOS_Readline(STDIN_FILE_POINTER, STDOUT_FILE_POINTER, prompt_bytes)
  50. if line_bytes is None:
  51. raise KeyboardInterrupt
  52. else:
  53. return line_bytes
  54. def raw_input(prompt=""):
  55. """raw_input([prompt]) -> string
  56. Read a string from standard input. The trailing newline is stripped.
  57. If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
  58. On Unix, GNU readline is used if enabled. The prompt string, if given,
  59. is printed without a trailing newline before reading."""
  60. sys.stderr.flush()
  61. tty = STDIN.is_a_TTY() and STDOUT.is_a_TTY()
  62. if RETURN_UNICODE:
  63. if tty:
  64. line_bytes = readline(prompt)
  65. line = stdin_decode(line_bytes)
  66. else:
  67. line = stdio_readline(prompt)
  68. else:
  69. if tty:
  70. line = readline(prompt)
  71. else:
  72. line_unicode = stdio_readline(prompt)
  73. line = stdin_encode(line_unicode)
  74. if line:
  75. return line[:-1] # strip strailing "\n"
  76. else:
  77. raise EOFError
  78. def input(prompt=""):
  79. """input([prompt]) -> value
  80. Equivalent to eval(raw_input(prompt))."""
  81. string = stdin_decode(raw_input(prompt))
  82. caller_frame = sys._getframe(1)
  83. globals = caller_frame.f_globals
  84. locals = caller_frame.f_locals
  85. return eval(string, globals, locals)
  86. def enable(return_unicode=RETURN_UNICODE):
  87. global RETURN_UNICODE
  88. RETURN_UNICODE = return_unicode
  89. builtins.raw_input = raw_input
  90. builtins.input = input
  91. def disable():
  92. builtins.raw_input = original_raw_input
  93. builtins.input = original_input