Skip to content

Commit 75c2e4a

Browse files
committed
Remove lazy_static, just store as a CodeObject
1 parent 241f519 commit 75c2e4a

File tree

5 files changed

+12
-46
lines changed

5 files changed

+12
-46
lines changed

derive/src/compile_bytecode.rs

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,11 @@ struct PyCompileInput {
5151
metas: Vec<Meta>,
5252
}
5353

54-
struct PyCompileResult {
55-
code_obj: CodeObject,
56-
lazy_static: bool,
57-
}
58-
5954
impl PyCompileInput {
60-
fn compile(&self) -> Result<PyCompileResult, Diagnostic> {
55+
fn compile(&self) -> Result<CodeObject, Diagnostic> {
6156
let mut source_path = None;
6257
let mut mode = None;
6358
let mut source: Option<CompilationSource> = None;
64-
let mut lazy_static = false;
6559

6660
fn assert_source_empty(source: &Option<CompilationSource>) -> Result<(), Diagnostic> {
6761
if let Some(source) = source {
@@ -114,16 +108,11 @@ impl PyCompileInput {
114108
});
115109
}
116110
}
117-
Meta::Word(ident) => {
118-
if ident == "lazy_static" {
119-
lazy_static = true;
120-
}
121-
}
122111
_ => {}
123112
}
124113
}
125114

126-
let code_obj = source
115+
source
127116
.ok_or_else(|| {
128117
Diagnostic::span_error(
129118
self.span.clone(),
@@ -133,12 +122,7 @@ impl PyCompileInput {
133122
.compile(
134123
&mode.unwrap_or(compile::Mode::Exec),
135124
source_path.unwrap_or_else(|| "frozen".to_string()),
136-
)?;
137-
138-
Ok(PyCompileResult {
139-
code_obj,
140-
lazy_static,
141-
})
125+
)
142126
}
143127
}
144128

@@ -156,10 +140,7 @@ impl Parse for PyCompileInput {
156140
pub fn impl_py_compile_bytecode(input: TokenStream2) -> Result<TokenStream2, Diagnostic> {
157141
let input: PyCompileInput = parse2(input)?;
158142

159-
let PyCompileResult {
160-
code_obj,
161-
lazy_static,
162-
} = input.compile()?;
143+
let code_obj = input.compile()?;
163144

164145
let bytes = bincode::serialize(&code_obj).expect("Failed to serialize");
165146
let bytes = LitByteStr::new(&bytes, Span::call_site());
@@ -172,17 +153,5 @@ pub fn impl_py_compile_bytecode(input: TokenStream2) -> Result<TokenStream2, Dia
172153
})
173154
};
174155

175-
if lazy_static {
176-
Ok(quote! {
177-
({
178-
use ::lazy_static::lazy_static;
179-
lazy_static! {
180-
static ref STATIC: ::rustpython_vm::bytecode::CodeObject = #output;
181-
}
182-
&*STATIC
183-
})
184-
})
185-
} else {
186-
Ok(output)
187-
}
156+
Ok(output)
188157
}

vm/src/frozen.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
use crate::bytecode::CodeObject;
22
use std::collections::HashMap;
33

4-
pub fn get_module_inits() -> HashMap<&'static str, &'static CodeObject> {
4+
pub fn get_module_inits() -> HashMap<String, CodeObject> {
55
hashmap! {
6-
"__hello__" => py_compile_bytecode!(
7-
lazy_static,
6+
"__hello__".into() => py_compile_bytecode!(
87
source = "initialized = True; print(\"Hello world!\")\n",
98
),
10-
"_frozen_importlib" => py_compile_bytecode!(
11-
lazy_static,
9+
"_frozen_importlib".into() => py_compile_bytecode!(
1210
file = "../Lib/importlib/_bootstrap.py",
1311
),
14-
"_frozen_importlib_external" => py_compile_bytecode!(
15-
lazy_static,
12+
"_frozen_importlib_external".into() => py_compile_bytecode!(
1613
file = "../Lib/importlib/_bootstrap_external.py",
1714
),
1815
}

vm/src/import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn init_importlib(vm: &VirtualMachine) -> PyResult {
2525
}
2626

2727
pub fn import_frozen(vm: &VirtualMachine, module_name: &str) -> PyResult {
28-
if let Some(&frozen) = vm.frozen.borrow().get(module_name) {
28+
if let Some(frozen) = vm.frozen.borrow().get(module_name) {
2929
let mut frozen = frozen.clone();
3030
frozen.source_path = format!("frozen {}", module_name);
3131
import_codeobj(vm, module_name, frozen)

vm/src/stdlib/imp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn imp_get_frozen_object(name: PyStringRef, vm: &VirtualMachine) -> PyResult<PyC
5858
vm.frozen
5959
.borrow()
6060
.get(name.as_str())
61-
.map(|&frozen| {
61+
.map(|frozen| {
6262
let mut frozen = frozen.clone();
6363
frozen.source_path = format!("frozen {}", name.as_str());
6464
PyCode::new(frozen)

vm/src/vm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub struct VirtualMachine {
5353
pub frames: RefCell<Vec<FrameRef>>,
5454
pub wasm_id: Option<String>,
5555
pub exceptions: RefCell<Vec<PyObjectRef>>,
56-
pub frozen: RefCell<HashMap<&'static str, &'static bytecode::CodeObject>>,
56+
pub frozen: RefCell<HashMap<String, bytecode::CodeObject>>,
5757
pub import_func: RefCell<PyObjectRef>,
5858
}
5959

0 commit comments

Comments
 (0)