Skip to content

Commit 69880ca

Browse files
committed
more intern_static_str adaption
1 parent 69e1f69 commit 69880ca

File tree

7 files changed

+23
-15
lines changed

7 files changed

+23
-15
lines changed

vm/src/builtins/str.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,7 @@ impl PyStr {
12881288
#[pyclass]
12891289
impl PyRef<PyStr> {
12901290
#[pymethod(magic)]
1291-
fn str(self, vm: &VirtualMachine) -> PyRefExact<PyStr> {
1291+
pub fn str(self, vm: &VirtualMachine) -> PyRefExact<PyStr> {
12921292
self.into_exact_or(&vm.ctx, |zelf| unsafe {
12931293
// Creating a copy with same kind is safe
12941294
PyStr::new_str_unchecked(zelf.bytes.to_vec(), zelf.kind.kind()).into_exact_ref(&vm.ctx)

vm/src/builtins/tuple.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ impl Representable for PyTuple {
402402
#[inline]
403403
fn repr(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
404404
let s = if zelf.len() == 0 {
405-
vm.ctx.intern_str("()").to_owned()
405+
vm.ctx.intern_static_str("()").to_owned()
406406
} else if let Some(_guard) = ReprGuard::enter(vm, zelf.as_object()) {
407407
let s = if zelf.len() == 1 {
408408
format!("({},)", zelf.elements[0].repr(vm)?)
@@ -411,7 +411,7 @@ impl Representable for PyTuple {
411411
};
412412
vm.ctx.new_str(s)
413413
} else {
414-
vm.ctx.intern_str("(...)").to_owned()
414+
vm.ctx.intern_static_str("(...)").to_owned()
415415
};
416416
Ok(s)
417417
}

vm/src/builtins/type.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,12 @@ impl PyType {
319319
// This is used for class initialisation where the vm is not yet available.
320320
pub fn set_str_attr<V: Into<PyObjectRef>>(
321321
&self,
322-
attr_name: &str,
322+
attr_name: &'static str,
323323
value: V,
324324
ctx: impl AsRef<Context>,
325325
) {
326326
let ctx = ctx.as_ref();
327-
let attr_name = ctx.intern_str(attr_name);
327+
let attr_name = ctx.intern_static_str(attr_name);
328328
self.set_attr(attr_name, value.into())
329329
}
330330

vm/src/stdlib/builtins.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ mod builtins {
372372
fn breakpoint(args: FuncArgs, vm: &VirtualMachine) -> PyResult {
373373
match vm
374374
.sys_module
375-
.get_attr(vm.ctx.intern_str("breakpointhook"), vm)
375+
.get_attr(vm.ctx.intern_static_str("breakpointhook"), vm)
376376
{
377377
Ok(hook) => hook.as_ref().call(args, vm),
378378
Err(_) => Err(vm.new_runtime_error("lost sys.breakpointhook".to_owned())),

vm/src/stdlib/errno.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
1010
"errorcode" => errorcode.clone(),
1111
});
1212
for (name, code) in ERROR_CODES {
13-
let name = vm.ctx.intern_str(*name);
13+
let name = vm.ctx.intern_static_str(name);
1414
let code = vm.new_pyobj(*code);
1515
errorcode
1616
.set_item(&*code, name.to_owned().into(), vm)

vm/src/stdlib/sys.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ mod sys {
135135
fn byteorder(vm: &VirtualMachine) -> PyStrRef {
136136
// https://doc.rust-lang.org/reference/conditional-compilation.html#target_endian
137137
vm.ctx
138-
.intern_str(if cfg!(target_endian = "little") {
138+
.intern_static_str(if cfg!(target_endian = "little") {
139139
"little"
140140
} else if cfg!(target_endian = "big") {
141141
"big"

vm/src/vm/mod.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,20 @@ impl VirtualMachine {
200200

201201
vm.builtins.init_dict(
202202
vm.ctx.intern_str("builtins"),
203-
Some(vm.ctx.intern_str(stdlib::builtins::DOC.unwrap()).to_owned()),
203+
Some(
204+
vm.ctx
205+
.intern_static_str(stdlib::builtins::DOC.unwrap())
206+
.to_owned(),
207+
),
204208
&vm,
205209
);
206210
vm.sys_module.init_dict(
207211
vm.ctx.intern_str("sys"),
208-
Some(vm.ctx.intern_str(stdlib::sys::DOC.unwrap()).to_owned()),
212+
Some(
213+
vm.ctx
214+
.intern_static_str(stdlib::sys::DOC.unwrap())
215+
.to_owned(),
216+
),
209217
&vm,
210218
);
211219
// let name = vm.sys_module.get_attr("__name__", &vm).unwrap();
@@ -274,14 +282,14 @@ impl VirtualMachine {
274282
// builtins.open to io.OpenWrapper, but this is easier, since it doesn't
275283
// require the Python stdlib to be present
276284
let io = import::import_builtin(self, "_io")?;
277-
let set_stdio = |name, fd, mode: &str| {
285+
let set_stdio = |name, dunder_name, fd, mode: &str| {
278286
let stdio = crate::stdlib::io::open(
279287
self.ctx.new_int(fd).into(),
280288
Some(mode),
281289
Default::default(),
282290
self,
283291
)?;
284-
let dunder_name = self.ctx.intern_str(format!("__{name}__"));
292+
let dunder_name = self.ctx.intern_static_str(dunder_name);
285293
self.sys_module.set_attr(
286294
dunder_name, // e.g. __stdin__
287295
stdio.clone(),
@@ -290,9 +298,9 @@ impl VirtualMachine {
290298
self.sys_module.set_attr(name, stdio, self)?;
291299
Ok(())
292300
};
293-
set_stdio("stdin", 0, "r")?;
294-
set_stdio("stdout", 1, "w")?;
295-
set_stdio("stderr", 2, "w")?;
301+
set_stdio("stdin", "__stdin__", 0, "r")?;
302+
set_stdio("stdout", "__stdout__", 1, "w")?;
303+
set_stdio("stderr", "__stderr__", 2, "w")?;
296304

297305
let io_open = io.get_attr("open", self)?;
298306
self.builtins.set_attr("open", io_open, self)?;

0 commit comments

Comments
 (0)