FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
RustPython/src/interpreter.rs at main · github-roushan/RustPython · GitHub
github-roushan
RustPython
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
RustPython
/
src
/
interpreter.rs
Copy path
More file actions
More file actions
Latest commit
History
History
History
126 lines (114 loc) · 3.86 KB
Breadcrumbs
RustPython
/
src
/
interpreter.rs
Copy path
File metadata and controls
126 lines (114 loc) · 3.86 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
use
rustpython_vm
::
{
Interpreter
,
PyRef
,
Settings
,
VirtualMachine
,
builtins
::
PyModule
}
;
pub
type
InitHook
=
Box
<
dyn
FnOnce
(
&
mut
VirtualMachine
)
>
;
/// The convenient way to create [rustpython_vm::Interpreter] with stdlib and other stuffs.
///
/// Basic usage:
/// ```
/// let interpreter = rustpython::InterpreterConfig::new()
/// .init_stdlib()
/// .interpreter();
/// ```
///
/// To override [rustpython_vm::Settings]:
/// ```
/// use rustpython_vm::Settings;
/// // Override your settings here.
/// let mut settings = Settings::default();
/// settings.debug = 1;
/// // You may want to add paths to `rustpython_vm::Settings::path_list` to allow import python libraries.
/// settings.path_list.push("Lib".to_owned()); // add standard library directory
/// settings.path_list.push("".to_owned()); // add current working directory
/// let interpreter = rustpython::InterpreterConfig::new()
/// .settings(settings)
/// .interpreter();
/// ```
///
/// To add native modules:
/// ```compile_fail
/// let interpreter = rustpython::InterpreterConfig::new()
/// .init_stdlib()
/// .init_hook(Box::new(|vm| {
/// vm.add_native_module(
/// "your_module_name".to_owned(),
/// Box::new(your_module::make_module),
/// );
/// }))
/// .interpreter();
/// ```
#
[
derive
(
Default
)
]
pub
struct
InterpreterConfig
{
settings
:
Option
<
Settings
>
,
init_hooks
:
Vec
<
InitHook
>
,
}
impl
InterpreterConfig
{
pub
fn
new
(
)
->
Self
{
Self
::
default
(
)
}
pub
fn
interpreter
(
self
)
->
Interpreter
{
let
settings =
self
.
settings
.
unwrap_or_default
(
)
;
Interpreter
::
with_init
(
settings
,
|vm|
{
for
hook
in
self
.
init_hooks
{
hook
(
vm
)
;
}
}
)
}
pub
fn
settings
(
mut
self
,
settings
:
Settings
)
->
Self
{
self
.
settings
=
Some
(
settings
)
;
self
}
pub
fn
init_hook
(
mut
self
,
hook
:
InitHook
)
->
Self
{
self
.
init_hooks
.
push
(
hook
)
;
self
}
pub
fn
add_native_module
(
self
,
name
:
String
,
make_module
:
fn
(
&
VirtualMachine
)
->
PyRef
<
PyModule
>
,
)
->
Self
{
self
.
init_hook
(
Box
::
new
(
move
|vm|
{
vm
.
add_native_module
(
name
,
Box
::
new
(
make_module
)
)
}
)
)
}
#
[
cfg
(
feature =
"stdlib"
)
]
pub
fn
init_stdlib
(
self
)
->
Self
{
self
.
init_hook
(
Box
::
new
(
init_stdlib
)
)
}
}
#
[
cfg
(
feature =
"stdlib"
)
]
pub
fn
init_stdlib
(
vm
:
&
mut
VirtualMachine
)
{
vm
.
add_native_modules
(
rustpython_stdlib
::
get_module_inits
(
)
)
;
// if we're on freeze-stdlib, the core stdlib modules will be included anyway
#
[
cfg
(
feature =
"freeze-stdlib"
)
]
{
vm
.
add_frozen
(
rustpython_pylib
::
FROZEN_STDLIB
)
;
// FIXME: Remove this hack once sys._stdlib_dir is properly implemented or _frozen_importlib doesn't depend on it anymore.
assert
!
(
vm
.
sys_module
.
get_attr
(
"_stdlib_dir"
,
vm
)
.
is_err
(
)
)
;
vm
.
sys_module
.
set_attr
(
"_stdlib_dir"
,
vm
.
new_pyobj
(
rustpython_pylib
::
LIB_PATH
.
to_owned
(
)
)
,
vm
,
)
.
unwrap
(
)
;
}
#
[
cfg
(
not
(
feature =
"freeze-stdlib"
)
)
]
{
use
rustpython_vm
::
common
::
rc
::
PyRc
;
let
state =
PyRc
::
get_mut
(
&
mut
vm
.
state
)
.
unwrap
(
)
;
let
settings =
&
mut
state
.
settings
;
let
path_list = std
::
mem
::
take
(
&
mut
settings
.
path_list
)
;
// BUILDTIME_RUSTPYTHONPATH should be set when distributing
if
let
Some
(
paths
)
=
option_env
!
(
"BUILDTIME_RUSTPYTHONPATH"
)
{
settings
.
path_list
.
extend
(
crate
::
settings
::
split_paths
(
paths
)
.
map
(
|path| path
.
into_os_string
(
)
.
into_string
(
)
.
unwrap
(
)
)
,
)
}
else
{
#
[
cfg
(
feature =
"rustpython-pylib"
)
]
settings
.
path_list
.
push
(
rustpython_pylib
::
LIB_PATH
.
to_owned
(
)
)
}
settings
.
path_list
.
extend
(
path_list
)
;
}
}
Back
|
FazBrowse Home
|
New Git URL