Debian Codegraph @

pypy/module/__builtin__/app_io.py

raw symbols SCIP
1 # NOT_RPYTHON (but maybe soon)
2 """
3 Plain Python definition of the builtin I/O-related functions.
4 """
5
6 import sys
7
8 def _write_prompt(stdout, prompt):
9 print(prompt, file=stdout, end='')
10 try:
11 flush = stdout.flush
12 except AttributeError:
13 pass
14 else:
15 flush()
16
17 def _is_std_tty(stdin, stdout):
18 try:
19 infileno, outfileno = stdin.fileno(), stdout.fileno()
20 except:
21 return False
22 return infileno == 0 and stdin.isatty() and outfileno == 1
23
24 def input(prompt=''):
25 """input([prompt]) -> string
26
27 Read a string from standard input. The trailing newline is stripped.
28 If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
29 On Unix, GNU readline is used if enabled. The prompt string, if given,
30 is printed without a trailing newline before reading."""
31
32 sys.audit("builtins.input", prompt)
33 try:
34 stdin = sys.stdin
35 except AttributeError:
36 raise RuntimeError("input: lost sys.stdin")
37 try:
38 stdout = sys.stdout
39 except AttributeError:
40 raise RuntimeError("input: lost sys.stdout")
41 try:
42 stderr = sys.stderr
43 except AttributeError:
44 raise RuntimeError("input: lost sys.stderr")
45
46 stderr.flush()
47
48 # hook for the readline module
49 if hasattr(sys, '__raw_input__') and _is_std_tty(stdin, stdout):
50 _write_prompt(stdout, '')
51 res = sys.__raw_input__(str(prompt))
52 sys.audit("builtins.input/result", res)
53 return res
54
55 _write_prompt(stdout, prompt)
56 line = stdin.readline()
57 if not line: # inputting an empty line gives line == '\n'
58 raise EOFError
59 if line[-1] == '\n':
60 res = line[:-1]
61 else:
62 res = line
63 sys.audit("builtins.input/result", res)
64 return res
65
66 def print_(*args, sep=' ', end='\n', file=None, flush=False):
67 r"""print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
68
69 Prints the values to a stream, or to sys.stdout by default.
70 Optional keyword arguments:
71 file: a file-like object (stream); defaults to the current sys.stdout.
72 sep: string inserted between values, default a space.
73 end: string appended after the last value, default a newline.
74 flush: whether to forcibly flush the stream.
75 """
76 fp = file
77 if fp is None:
78 fp = sys.stdout
79 if fp is None:
80 return
81 if sep is None:
82 sep = ' '
83 if not isinstance(sep, str):
84 raise TypeError("sep must be None or a string")
85 if end is None:
86 end = '\n'
87 if not isinstance(end, str):
88 raise TypeError("end must be None or a string")
89 if len(args) == 1:
90 fp.write(str(args[0]))
91 else:
92 for i, arg in enumerate(args):
93 if i:
94 fp.write(sep)
95 fp.write(str(arg))
96 fp.write(end)
97 if flush:
98 fp.flush()