Skip to content

Commit 4bf5644

Browse files
committed
Improve code quality
1 parent 9d9f9d3 commit 4bf5644

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

vm/src/builtins/classmethod.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,20 @@ impl Constructor for PyClassMethod {
6767
type Args = PyObjectRef;
6868

6969
fn py_new(cls: PyTypeRef, callable: Self::Args, vm: &VirtualMachine) -> PyResult {
70-
let result: PyResult<PyObjectRef> = PyClassMethod {
71-
callable: PyMutex::new(callable.clone()),
70+
let doc = callable.get_attr("__doc__", vm);
71+
72+
let result = PyClassMethod {
73+
callable: PyMutex::new(callable),
7274
}
73-
.into_ref_with_type(vm, cls)
74-
.map(Into::into);
75+
.into_ref_with_type(vm, cls)?;
76+
let obj = PyObjectRef::from(result);
7577

76-
let doc: PyResult<PyObjectRef> = callable.get_attr("__doc__", vm);
77-
let doc = vm.unwrap_pyresult(doc);
78-
let obj = vm.unwrap_pyresult(result.clone());
78+
match doc {
79+
Err(_) => None,
80+
Ok(doc) => Some(obj.set_attr("__doc__", doc, vm)),
81+
};
7982

80-
obj.set_attr("__doc__", doc, vm)?;
81-
result
83+
Ok(obj)
8284
}
8385
}
8486

vm/src/builtins/staticmethod.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,17 @@ impl Constructor for PyStaticMethod {
4141
type Args = PyObjectRef;
4242

4343
fn py_new(cls: PyTypeRef, callable: Self::Args, vm: &VirtualMachine) -> PyResult {
44-
let result: PyResult<PyObjectRef> = PyStaticMethod {
45-
callable: callable.clone(),
46-
}
47-
.into_ref_with_type(vm, cls)
48-
.map(Into::into);
44+
let doc = callable.get_attr("__doc__", vm);
45+
46+
let result = PyStaticMethod { callable }.into_ref_with_type(vm, cls)?;
47+
let obj = PyObjectRef::from(result);
4948

50-
let doc: PyResult<PyObjectRef> = callable.get_attr("__doc__", vm);
51-
let doc = vm.unwrap_pyresult(doc);
52-
let obj = vm.unwrap_pyresult(result.clone());
49+
match doc {
50+
Err(_) => None,
51+
Ok(doc) => Some(obj.set_attr("__doc__", doc, vm)),
52+
};
5353

54-
obj.set_attr("__doc__", doc, vm)?;
55-
result
54+
Ok(obj)
5655
}
5756
}
5857

0 commit comments

Comments
 (0)