FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpython/Lib/_pyrepl/simple_interact.py at 3.15 · python/cpython · GitHub
python
cpython
Repository navigation
Code
Issues
5k+
(5k+)
Pull requests
2.3k
(2.3k)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpython
/
Lib
/
_pyrepl
/
simple_interact.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
185 lines (159 loc) · 5.97 KB
Breadcrumbs
cpython
/
Lib
/
_pyrepl
/
simple_interact.py
Copy path
File metadata and controls
185 lines (159 loc) · 5.97 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# Copyright 2000-2010 Michael Hudson-Doyle <micahel@gmail.com>
# Armin Rigo
#
# All Rights Reserved
#
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose is hereby granted without fee,
# provided that the above copyright notice appear in all copies and
# that both that copyright notice and this permission notice appear in
# supporting documentation.
#
# THE AUTHOR MICHAEL HUDSON DISCLAIMS ALL WARRANTIES WITH REGARD TO
# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
# INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""This is an alternative to python_reader which tries to emulate
the CPython prompt as closely as possible, with the exception of
allowing multiline input and multiline history entries.
"""
from
__future__
import
annotations
import
_sitebuiltins
import
functools
import
os
import
sys
import
code
import
warnings
from
.
readline
import
_get_reader
,
multiline_input
,
append_history_file
_error
:
tuple
[
type
[
Exception
], ...]
|
type
[
Exception
]
try
:
from
.
unix_console
import
_error
except
ModuleNotFoundError
:
from
.
windows_console
import
_error
def
check
()
->
str
:
"""Returns the error message if there is a problem initializing the state."""
try
:
_get_reader
()
except
_error
as
e
:
if
term
:=
os
.
environ
.
get
(
"TERM"
,
""
):
term
=
f"; TERM=
{
term
}
"
return
str
(
str
(
e
)
or
repr
(
e
)
or
"unknown error"
)
+
term
return
""
def
_strip_final_indent
(
text
:
str
)
->
str
:
# kill spaces and tabs at the end, but only if they follow '\n'.
# meant to remove the auto-indentation only (although it would of
# course also remove explicitly-added indentation).
short
=
text
.
rstrip
(
"
\t
"
)
n
=
len
(
short
)
if
n
>
0
and
text
[
n
-
1
]
==
"
\n
"
:
return
short
return
text
def
_clear_screen
():
reader
=
_get_reader
()
reader
.
scheduled_commands
.
append
(
"clear_screen"
)
REPL_COMMANDS
=
{
"exit"
:
_sitebuiltins
.
Quitter
(
'exit'
,
''
),
"quit"
:
_sitebuiltins
.
Quitter
(
'quit'
,
''
),
"copyright"
:
_sitebuiltins
.
_Printer
(
'copyright'
,
sys
.
copyright
),
"help"
:
_sitebuiltins
.
_Helper
(),
"clear"
:
_clear_screen
,
"
\x1a
"
:
_sitebuiltins
.
Quitter
(
'
\x1a
'
,
''
),
}
def
_more_lines
(
console
:
code
.
InteractiveConsole
,
unicodetext
:
str
)
->
bool
:
# ooh, look at the hack:
src
=
_strip_final_indent
(
unicodetext
)
try
:
code
=
console
.
compile
(
src
,
"<stdin>"
,
"single"
)
except
Exception
:
lines
=
src
.
splitlines
(
keepends
=
True
)
if
len
(
lines
)
==
1
:
return
False
last_line
=
lines
[
-
1
]
was_indented
=
last_line
.
startswith
((
" "
,
"
\t
"
))
not_empty
=
last_line
.
strip
()
!=
""
incomplete
=
not
last_line
.
endswith
(
"
\n
"
)
return
(
was_indented
or
not_empty
)
and
incomplete
else
:
return
code
is
None
def
run_multiline_interactive_console
(
console
:
code
.
InteractiveConsole
,
*
,
future_flags
:
int
=
0
,
)
->
None
:
from
.
readline
import
_setup
_setup
(
console
.
locals
)
if
future_flags
:
console
.
compile
.
compiler
.
flags
|=
future_flags
more_lines
=
functools
.
partial
(
_more_lines
,
console
)
input_n
=
0
_is_x_showrefcount_set
=
sys
.
_xoptions
.
get
(
"showrefcount"
)
_is_pydebug_build
=
hasattr
(
sys
,
"gettotalrefcount"
)
show_ref_count
=
_is_x_showrefcount_set
and
_is_pydebug_build
def
maybe_run_command
(
statement
:
str
)
->
bool
:
statement
=
statement
.
strip
()
if
statement
in
console
.
locals
or
statement
not
in
REPL_COMMANDS
:
return
False
reader
=
_get_reader
()
reader
.
history
.
pop
()
# skip internal commands in history
command
=
REPL_COMMANDS
[
statement
]
if
callable
(
command
):
# Make sure that history does not change because of commands
with
reader
.
suspend_history
(),
reader
.
suspend_colorization
():
command
()
return
True
return
False
while
True
:
try
:
try
:
sys
.
stdout
.
flush
()
except
Exception
:
pass
try
:
ps1
=
str
(
getattr
(
sys
,
"ps1"
,
">>> "
))
except
Exception
:
ps1
=
">>> "
try
:
ps2
=
str
(
getattr
(
sys
,
"ps2"
,
"... "
))
except
Exception
:
ps2
=
"... "
try
:
statement
=
multiline_input
(
more_lines
,
ps1
,
ps2
)
except
EOFError
:
break
if
maybe_run_command
(
statement
):
continue
input_name
=
f"<python-input-
{
input_n
}
>"
more
=
console
.
push
(
_strip_final_indent
(
statement
),
filename
=
input_name
,
_symbol
=
"single"
)
# type: ignore[call-arg]
assert
not
more
try
:
append_history_file
()
except
(
FileNotFoundError
,
PermissionError
,
OSError
)
as
e
:
warnings
.
warn
(
f"failed to open the history file for writing:
{
e
}
"
)
input_n
+=
1
except
KeyboardInterrupt
:
r
=
_get_reader
()
r
.
cmpltn_reset
()
if
r
.
input_trans
is
r
.
isearch_trans
:
r
.
do_cmd
((
"isearch-end"
, [
""
]))
r
.
pos
=
len
(
r
.
get_unicode
())
r
.
invalidate_full
()
r
.
refresh
()
console
.
write
(
"
\n
KeyboardInterrupt
\n
"
)
console
.
resetbuffer
()
except
MemoryError
:
console
.
write
(
"
\n
MemoryError
\n
"
)
console
.
resetbuffer
()
except
SystemExit
:
raise
except
:
console
.
showtraceback
()
console
.
resetbuffer
()
if
show_ref_count
:
console
.
write
(
f"[
{
sys
.
gettotalrefcount
()
}
refs,"
f"
{
sys
.
getallocatedblocks
()
}
blocks]
\n
"
)
Back
|
FazBrowse Home
|
New Git URL