FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
RustPython/src/shell.rs at main · RustPython/RustPython · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
RustPython
/
RustPython
Public
Notifications
You must be signed in to change notification settings
Fork
1.5k
Star
22.3k
Code
Issues
295
Pull requests
99
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
RustPython
/
src
/
shell.rs
Copy path
More file actions
More file actions
Latest commit
History
History
History
252 lines (234 loc) · 9.09 KB
Breadcrumbs
RustPython
/
src
/
shell.rs
Copy path
File metadata and controls
252 lines (234 loc) · 9.09 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
mod
helper
;
use
rustpython_compiler
::
{
CompileError
,
ParseError
,
parser
::
InterpolatedStringErrorType
,
parser
::
LexicalErrorType
,
parser
::
ParseErrorType
,
}
;
use
rustpython_vm
::
{
AsObject
,
PyResult
,
VirtualMachine
,
builtins
::
PyBaseExceptionRef
,
bytecode
::
CodeFlags
,
compiler
::
{
self
}
,
readline
::
{
Readline
,
ReadlineResult
}
,
scope
::
Scope
,
vm
::
VmCompileError
,
}
;
enum
ShellExecResult
{
Ok
,
PyErr
(
PyBaseExceptionRef
)
,
ContinueBlock
,
ContinueLine
,
}
fn
shell_exec
(
vm
:
&
VirtualMachine
,
source
:
&
str
,
scope
:
Scope
,
empty_line_given
:
bool
,
continuing_block
:
bool
,
future_features
:
&
mut
CodeFlags
,
)
->
ShellExecResult
{
// compiling expects only UNIX style line endings, and will replace windows line endings
// internally. Since we might need to analyze the source to determine if an error could be
// resolved by future input, we need the location from the error to match the source code that
// was actually compiled.
#
[
cfg
(
windows
)
]
let
source =
&
source
.
replace
(
"
\r
\n
"
,
"
\n
"
)
;
let
opts = compiler
::
CompileOpts
{
future_features
:
*
future_features
,
..vm
.
compile_opts
(
)
}
;
match
vm
.
compile_with_opts
(
source
,
compiler
::
Mode
::
Single
,
"<stdin>"
,
opts
)
{
Ok
(
code
)
=>
{
*
future_features |= code
.
code
.
flags
&
CodeFlags
::
FUTURE_MASK
;
if
empty_line_given || !continuing_block
{
// We want to execute the full code
match
vm
.
run_code_obj
(
code
,
scope
)
{
Ok
(
_val
)
=>
ShellExecResult
::
Ok
,
Err
(
err
)
=>
ShellExecResult
::
PyErr
(
err
)
,
}
}
else
{
// We can just return an ok result
ShellExecResult
::
Ok
}
}
Err
(
VmCompileError
::
Compile
(
CompileError
::
Parse
(
ParseError
{
error
:
ParseErrorType
::
Lexical
(
LexicalErrorType
::
Eof
)
,
..
}
)
)
)
=>
ShellExecResult
::
ContinueLine
,
Err
(
VmCompileError
::
Compile
(
CompileError
::
Parse
(
ParseError
{
error
:
ParseErrorType
::
Lexical
(
LexicalErrorType
::
FStringError
(
InterpolatedStringErrorType
::
UnterminatedTripleQuotedString
,
)
)
,
..
}
)
)
)
=>
ShellExecResult
::
ContinueLine
,
Err
(
err
)
=>
{
// Check if the error is from an unclosed triple quoted string (which should always
// continue)
if
let
VmCompileError
::
Compile
(
CompileError
::
Parse
(
ParseError
{
error
:
ParseErrorType
::
Lexical
(
LexicalErrorType
::
UnclosedStringError
)
,
raw_location
,
..
}
)
)
=
&
err
{
let
loc = raw_location
.
start
(
)
.
to_usize
(
)
;
let
mut
iter = source
.
chars
(
)
;
if
let
Some
(
quote
)
= iter
.
nth
(
loc
)
&& iter
.
next
(
)
==
Some
(
quote
)
&& iter
.
next
(
)
==
Some
(
quote
)
{
return
ShellExecResult
::
ContinueLine
;
}
}
;
// bad_error == true if we are handling an error that should be thrown even if we are continuing
// if its an indentation error, set to true if we are continuing and the error is on column 0,
// since indentations errors on columns other than 0 should be ignored.
// if its an unrecognized token for dedent, set to false
let
bad_error =
match
&
err
{
VmCompileError
::
Compile
(
CompileError
::
Parse
(
p
)
)
=>
{
match
&
p
.
error
{
ParseErrorType
::
Lexical
(
LexicalErrorType
::
IndentationError
)
=>
{
continuing_block
}
// && p.location.is_some()
ParseErrorType
::
OtherError
(
msg
)
=>
{
!msg
.
starts_with
(
"Expected an indented block"
)
}
_ =>
true
,
// !matches!(p, ParseErrorType::UnrecognizedToken(Tok::Dedent, _))
}
}
_ =>
true
,
// It is a bad error for everything else
}
;
// If we are handling an error on an empty line or an error worthy of throwing
if
empty_line_given || bad_error
{
ShellExecResult
::
PyErr
(
err
.
into_pyexception
(
vm
,
Some
(
source
)
)
)
}
else
{
ShellExecResult
::
ContinueBlock
}
}
}
}
/// Enter a repl loop
pub
fn
run_shell
(
vm
:
&
VirtualMachine
,
scope
:
Scope
)
->
PyResult
<
(
)
>
{
let
mut
repl =
Readline
::
new
(
helper
::
ShellHelper
::
new
(
vm
,
scope
.
globals
.
clone
(
)
)
)
;
let
mut
full_input =
String
::
new
(
)
;
// Retrieve a `history_path_str` dependent on the OS
let
repl_history_path =
match
dirs
::
config_dir
(
)
{
Some
(
mut
path
)
=>
{
path
.
push
(
"rustpython"
)
;
path
.
push
(
"repl_history.txt"
)
;
path
}
None
=>
".repl_history.txt"
.
into
(
)
,
}
;
if
repl
.
load_history
(
&
repl_history_path
)
.
is_err
(
)
{
println
!
(
"No previous history."
)
;
}
// We might either be waiting to know if a block is complete, or waiting to know if a multiline
// statement is complete. In the former case, we need to ensure that we read one extra new line
// to know that the block is complete. In the latter, we can execute as soon as the statement is
// valid.
let
mut
continuing_block =
false
;
let
mut
continuing_line =
false
;
let
mut
future_features =
CodeFlags
::
empty
(
)
;
loop
{
let
prompt_name =
if
continuing_block || continuing_line
{
"ps2"
}
else
{
"ps1"
}
;
let
prompt = vm
.
sys_module
.
get_attr
(
prompt_name
,
vm
)
.
and_then
(
|prompt| prompt
.
str
(
vm
)
)
;
let
prompt =
match
prompt
{
Ok
(
ref
s
)
=> s
.
expect_str
(
)
,
Err
(
_
)
=>
""
,
}
;
continuing_line =
false
;
let
result =
match
repl
.
readline
(
prompt
)
{
ReadlineResult
::
Line
(
line
)
=>
{
#
[
cfg
(
debug_assertions
)
]
debug
!
(
"You entered {line:?}"
)
;
repl
.
add_history_entry
(
line
.
trim_end
(
)
)
.
unwrap
(
)
;
let
empty_line_given = line
.
is_empty
(
)
;
if
full_input
.
is_empty
(
)
{
full_input = line
;
}
else
{
full_input
.
push_str
(
&
line
)
;
}
full_input
.
push
(
'\n'
)
;
match
shell_exec
(
vm
,
&
full_input
,
scope
.
clone
(
)
,
empty_line_given
,
continuing_block
,
&
mut
future_features
,
)
{
ShellExecResult
::
Ok
=>
{
if
continuing_block
{
if
empty_line_given
{
// We should exit continue mode since the block successfully executed
continuing_block =
false
;
full_input
.
clear
(
)
;
}
}
else
{
// We aren't in continue mode so proceed normally
full_input
.
clear
(
)
;
}
Ok
(
(
)
)
}
// Continue, but don't change the mode
ShellExecResult
::
ContinueLine
=>
{
continuing_line =
true
;
Ok
(
(
)
)
}
ShellExecResult
::
ContinueBlock
=>
{
continuing_block =
true
;
Ok
(
(
)
)
}
ShellExecResult
::
PyErr
(
err
)
=>
{
continuing_block =
false
;
full_input
.
clear
(
)
;
Err
(
err
)
}
}
}
ReadlineResult
::
Interrupt
=>
{
continuing_block =
false
;
full_input
.
clear
(
)
;
let
keyboard_interrupt =
vm
.
new_exception_empty
(
vm
.
ctx
.
exceptions
.
keyboard_interrupt
.
to_owned
(
)
)
;
Err
(
keyboard_interrupt
)
}
ReadlineResult
::
Eof
=>
{
break
;
}
#
[
cfg
(
unix
)
]
ReadlineResult
::
OsError
(
num
)
=>
{
let
os_error = vm
.
new_exception_msg
(
vm
.
ctx
.
exceptions
.
os_error
.
to_owned
(
)
,
format
!
(
"{num:?}"
)
.
into
(
)
,
)
;
vm
.
print_exception
(
os_error
)
;
break
;
}
ReadlineResult
::
Other
(
err
)
=>
{
eprintln
!
(
"Readline error: {err:?}"
)
;
break
;
}
ReadlineResult
::
Io
(
err
)
=>
{
eprintln
!
(
"IO error: {err:?}"
)
;
break
;
}
}
;
if
let
Err
(
exc
)
= result
{
if
exc
.
fast_isinstance
(
vm
.
ctx
.
exceptions
.
system_exit
)
{
repl
.
save_history
(
&
repl_history_path
)
.
unwrap
(
)
;
return
Err
(
exc
)
;
}
vm
.
print_exception
(
exc
)
;
}
}
repl
.
save_history
(
&
repl_history_path
)
.
unwrap
(
)
;
Ok
(
(
)
)
}
Back
|
FazBrowse Home
|
New Git URL