Skip to content

Commit 7067119

Browse files
authored
Merge pull request RustPython#2375 from RustPython/coolreader18/coros-have-names-too
Give generators, coroutines, async generators __name__s
2 parents 57805f7 + 82c8e54 commit 7067119

File tree

5 files changed

+71
-30
lines changed

5 files changed

+71
-30
lines changed

vm/src/builtins/asyncgenerator.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use super::code::PyCodeRef;
2+
use super::pystr::PyStrRef;
23
use super::pytype::PyTypeRef;
34
use crate::coroutine::{Coro, Variant};
45
use crate::exceptions::PyBaseExceptionRef;
56
use crate::frame::FrameRef;
67
use crate::function::OptionalArg;
78
use crate::pyobject::{
8-
PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
9+
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
910
};
1011
use crate::slots::PyIter;
1112
use crate::vm::VirtualMachine;
@@ -32,17 +33,27 @@ impl PyAsyncGen {
3233
&self.inner
3334
}
3435

35-
pub fn new(frame: FrameRef, vm: &VirtualMachine) -> PyRef<Self> {
36+
pub fn new(frame: FrameRef, name: PyStrRef) -> Self {
3637
PyAsyncGen {
37-
inner: Coro::new(frame, Variant::AsyncGen),
38+
inner: Coro::new(frame, Variant::AsyncGen, name),
3839
running_async: AtomicCell::new(false),
3940
}
40-
.into_ref(vm)
4141
}
4242

43-
// TODO: fix function names situation
4443
#[pyproperty(magic)]
45-
fn name(&self) {}
44+
fn name(&self) -> PyStrRef {
45+
self.inner.name()
46+
}
47+
48+
#[pyproperty(magic, setter)]
49+
fn set_name(&self, name: PyStrRef) {
50+
self.inner.set_name(name)
51+
}
52+
53+
#[pymethod(magic)]
54+
fn repr(zelf: PyRef<Self>) -> String {
55+
zelf.inner.repr(zelf.get_id())
56+
}
4657

4758
#[pymethod(name = "__aiter__")]
4859
fn aiter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {

vm/src/builtins/coroutine.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::pytype::PyTypeRef;
44
use crate::coroutine::{Coro, Variant};
55
use crate::frame::FrameRef;
66
use crate::function::OptionalArg;
7-
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
7+
use crate::pyobject::{IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
88
use crate::slots::PyIter;
99
use crate::vm::VirtualMachine;
1010

@@ -28,17 +28,25 @@ impl PyCoroutine {
2828
&self.inner
2929
}
3030

31-
pub fn new(frame: FrameRef, vm: &VirtualMachine) -> PyRef<Self> {
31+
pub fn new(frame: FrameRef, name: PyStrRef) -> Self {
3232
PyCoroutine {
33-
inner: Coro::new(frame, Variant::Coroutine),
33+
inner: Coro::new(frame, Variant::Coroutine, name),
3434
}
35-
.into_ref(vm)
3635
}
3736

38-
// TODO: fix function names situation
3937
#[pyproperty(magic)]
40-
fn name(&self, vm: &VirtualMachine) -> PyObjectRef {
41-
vm.ctx.none()
38+
fn name(&self) -> PyStrRef {
39+
self.inner.name()
40+
}
41+
42+
#[pyproperty(magic, setter)]
43+
fn set_name(&self, name: PyStrRef) {
44+
self.inner.set_name(name)
45+
}
46+
47+
#[pymethod(magic)]
48+
fn repr(zelf: PyRef<Self>) -> String {
49+
zelf.inner.repr(zelf.get_id())
4250
}
4351

4452
#[pymethod]

vm/src/builtins/function.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,9 @@ impl PyFunction {
284284
let is_gen = code.flags.contains(bytecode::CodeFlags::IS_GENERATOR);
285285
let is_coro = code.flags.contains(bytecode::CodeFlags::IS_COROUTINE);
286286
match (is_gen, is_coro) {
287-
(true, false) => Ok(PyGenerator::new(frame, vm).into_object()),
288-
(false, true) => Ok(PyCoroutine::new(frame, vm).into_object()),
289-
(true, true) => Ok(PyAsyncGen::new(frame, vm).into_object()),
287+
(true, false) => Ok(PyGenerator::new(frame, self.name()).into_object(vm)),
288+
(false, true) => Ok(PyCoroutine::new(frame, self.name()).into_object(vm)),
289+
(true, true) => Ok(PyAsyncGen::new(frame, self.name()).into_object(vm)),
290290
(false, false) => vm.run_frame_full(frame),
291291
}
292292
}

vm/src/builtins/generator.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
*/
44

55
use super::code::PyCodeRef;
6+
use super::pystr::PyStrRef;
67
use super::pytype::PyTypeRef;
78
use crate::coroutine::{Coro, Variant};
89
use crate::frame::FrameRef;
910
use crate::function::OptionalArg;
10-
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
11+
use crate::pyobject::{IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
1112
use crate::slots::PyIter;
1213
use crate::vm::VirtualMachine;
1314

@@ -29,17 +30,25 @@ impl PyGenerator {
2930
&self.inner
3031
}
3132

32-
pub fn new(frame: FrameRef, vm: &VirtualMachine) -> PyRef<Self> {
33+
pub fn new(frame: FrameRef, name: PyStrRef) -> Self {
3334
PyGenerator {
34-
inner: Coro::new(frame, Variant::Gen),
35+
inner: Coro::new(frame, Variant::Gen, name),
3536
}
36-
.into_ref(vm)
3737
}
3838

39-
// TODO: fix function names situation
4039
#[pyproperty(magic)]
41-
fn name(&self, vm: &VirtualMachine) -> PyObjectRef {
42-
vm.ctx.none()
40+
fn name(&self) -> PyStrRef {
41+
self.inner.name()
42+
}
43+
44+
#[pyproperty(magic, setter)]
45+
fn set_name(&self, name: PyStrRef) {
46+
self.inner.set_name(name)
47+
}
48+
49+
#[pymethod(magic)]
50+
fn repr(zelf: PyRef<Self>) -> String {
51+
zelf.inner.repr(zelf.get_id())
4352
}
4453

4554
#[pymethod]

vm/src/coroutine.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::frame::{ExecutionResult, FrameRef};
44
use crate::pyobject::{PyObjectRef, PyResult, TypeProtocol};
55
use crate::vm::VirtualMachine;
66

7-
use crate::common::lock::PyRwLock;
7+
use crate::common::lock::PyMutex;
88
use crossbeam_utils::atomic::AtomicCell;
99

1010
#[derive(Debug, PartialEq, Clone, Copy)]
@@ -37,20 +37,22 @@ pub struct Coro {
3737
frame: FrameRef,
3838
pub closed: AtomicCell<bool>,
3939
running: AtomicCell<bool>,
40-
exceptions: PyRwLock<Vec<PyBaseExceptionRef>>,
40+
exceptions: PyMutex<Vec<PyBaseExceptionRef>>,
4141
started: AtomicCell<bool>,
4242
variant: Variant,
43+
name: PyMutex<PyStrRef>,
4344
}
4445

4546
impl Coro {
46-
pub fn new(frame: FrameRef, variant: Variant) -> Self {
47+
pub fn new(frame: FrameRef, variant: Variant, name: PyStrRef) -> Self {
4748
Coro {
4849
frame,
4950
closed: AtomicCell::new(false),
5051
running: AtomicCell::new(false),
51-
exceptions: PyRwLock::new(vec![]),
52+
exceptions: PyMutex::new(vec![]),
5253
started: AtomicCell::new(false),
5354
variant,
55+
name: PyMutex::new(name),
5456
}
5557
}
5658

@@ -69,10 +71,10 @@ impl Coro {
6971
let curr_exception_stack_len = vm.exceptions.borrow().len();
7072
vm.exceptions
7173
.borrow_mut()
72-
.append(&mut self.exceptions.write());
74+
.append(&mut self.exceptions.lock());
7375
let result = vm.with_frame(self.frame.clone(), func);
7476
std::mem::swap(
75-
&mut *self.exceptions.write(),
77+
&mut *self.exceptions.lock(),
7678
&mut vm
7779
.exceptions
7880
.borrow_mut()
@@ -166,7 +168,18 @@ impl Coro {
166168
self.frame.clone()
167169
}
168170
pub fn name(&self) -> PyStrRef {
169-
self.frame.code.obj_name.clone()
171+
self.name.lock().clone()
172+
}
173+
pub fn set_name(&self, name: PyStrRef) {
174+
*self.name.lock() = name;
175+
}
176+
pub fn repr(&self, id: usize) -> String {
177+
format!(
178+
"<{} object {} at {:#x}>",
179+
self.variant.name(),
180+
self.name.lock(),
181+
id
182+
)
170183
}
171184
}
172185

0 commit comments

Comments
 (0)