mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 03:04:38 +08:00
Borrow exception and dict methods that only read self (#8808)
add_note, __reduce__, and __setstate__ take &Py<BaseException>. dict.__ror__ takes &Py<PyDict>. Callers drop the clone made for add_note. Assisted-by: Grok CLI:grok-4.7
This commit is contained in:
1 parent
4174a77cac
commit
e540b4152d
4 files changed
+24
-24
No files matched your search
@@ -881,7 +881,7 @@ mod _json {
|
||||
fn add_note(exc: PyBaseExceptionRef, note: Wtf8Buf, vm: &VirtualMachine) -> PyBaseExceptionRef {
|
||||
// `PyBaseException::add_note` is `pub`, so call it directly instead
|
||||
// of going through the generic attribute-lookup + call machinery.
|
||||
let _ = exc.clone().add_note(vm.ctx.new_str(note), vm);
|
||||
let _ = exc.add_note(vm.ctx.new_str(note), vm);
|
||||
exc
|
||||
}
|
||||
|
||||
|
||||
@@ -2133,7 +2133,7 @@ mod _pickle {
|
||||
// saving
|
||||
|
||||
fn add_note(err: PyBaseExceptionRef, note: String, vm: &VirtualMachine) -> PyBaseExceptionRef {
|
||||
let _ = err.clone().add_note(vm.ctx.new_str(note), vm);
|
||||
let _ = err.add_note(vm.ctx.new_str(note), vm);
|
||||
err
|
||||
}
|
||||
|
||||
|
||||
@@ -585,6 +585,16 @@ impl Py<PyDict> {
|
||||
fn __getitem__(&self, key: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.inner_getitem(&*key, vm)
|
||||
}
|
||||
|
||||
fn __ror__(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
let other_dict = other.downcast::<PyDict>();
|
||||
if let Ok(other) = other_dict {
|
||||
let other_cp = other.copy();
|
||||
other_cp.merge_dict(self, true, vm)?;
|
||||
return Ok(other_cp.into_pyobject(vm));
|
||||
}
|
||||
Ok(vm.ctx.not_implemented())
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
@@ -613,16 +623,6 @@ impl PyRef<PyDict> {
|
||||
self.merge_object(other, vm)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn __ror__(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
let other_dict: Result<Self, _> = other.downcast();
|
||||
if let Ok(other) = other_dict {
|
||||
let other_cp = other.copy();
|
||||
other_cp.merge_dict(&self, true, vm)?;
|
||||
return Ok(other_cp.into_pyobject(vm));
|
||||
}
|
||||
Ok(vm.ctx.not_implemented())
|
||||
}
|
||||
}
|
||||
|
||||
impl DefaultConstructor for PyDict {}
|
||||
|
||||
+12
-12
@@ -786,18 +786,9 @@ impl Py<PyBaseException> {
|
||||
Err(i) => PyStr::from(format!("({})", i.format(", "))).into_ref(&vm.ctx),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
impl PyRef<PyBaseException> {
|
||||
#[pymethod]
|
||||
fn with_traceback(self, tb: Option<PyTracebackRef>) -> Self {
|
||||
*self.traceback.write() = tb;
|
||||
self
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
pub fn add_note(self, note: PyStrRef, vm: &VirtualMachine) -> PyResult<()> {
|
||||
pub fn add_note(&self, note: PyStrRef, vm: &VirtualMachine) -> PyResult<()> {
|
||||
let dict = crate::builtins::object::object_get_dict(self.as_object().to_owned(), vm)
|
||||
.map_err(|_| vm.new_attribute_error("Exception object has no __dict__"))?;
|
||||
|
||||
@@ -818,7 +809,7 @@ impl PyRef<PyBaseException> {
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn __reduce__(self, vm: &VirtualMachine) -> PyTupleRef {
|
||||
fn __reduce__(&self, vm: &VirtualMachine) -> PyTupleRef {
|
||||
if let Some(dict) = self.as_object().dict().filter(|x| !x.is_empty()) {
|
||||
vm.new_tuple((self.class().to_owned(), self.args(), dict))
|
||||
} else {
|
||||
@@ -827,7 +818,7 @@ impl PyRef<PyBaseException> {
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn __setstate__(self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyObjectRef> {
|
||||
fn __setstate__(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyObjectRef> {
|
||||
if !vm.is_none(&state) {
|
||||
let dict = state
|
||||
.downcast::<crate::builtins::PyDict>()
|
||||
@@ -845,6 +836,15 @@ impl PyRef<PyBaseException> {
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
impl PyRef<PyBaseException> {
|
||||
#[pymethod]
|
||||
fn with_traceback(self, tb: Option<PyTracebackRef>) -> Self {
|
||||
*self.traceback.write() = tb;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Constructor for PyBaseException {
|
||||
type Args = FuncArgs;
|
||||
|
||||
|
||||
Reference in new issue
Block a user