Skip to content

Commit a4c93df

Browse files
authored
Remove useless &PyRef patterns (#6455)
* fix &PyTypeRef * Remove useless &PyObjectRef * Remove useless &PyRef
1 parent 0412dfd commit a4c93df

37 files changed

+146
-137
lines changed

crates/stdlib/src/csv.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ pub(crate) use _csv::make_module;
44
mod _csv {
55
use crate::common::lock::PyMutex;
66
use crate::vm::{
7-
AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
7+
AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
8+
VirtualMachine,
89
builtins::{PyBaseExceptionRef, PyInt, PyNone, PyStr, PyType, PyTypeRef},
910
function::{ArgIterable, ArgumentError, FromArgs, FuncArgs, OptionalArg},
1011
protocol::{PyIter, PyIterReturn},
@@ -130,11 +131,11 @@ mod _csv {
130131
///
131132
/// * If the 'delimiter' attribute is not a single-character string, a type error is returned.
132133
/// * If the 'obj' is not of string type and does not have a 'delimiter' attribute, a type error is returned.
133-
fn parse_delimiter_from_obj(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<u8> {
134+
fn parse_delimiter_from_obj(vm: &VirtualMachine, obj: &PyObject) -> PyResult<u8> {
134135
if let Ok(attr) = obj.get_attr("delimiter", vm) {
135136
parse_delimiter_from_obj(vm, &attr)
136137
} else {
137-
match_class!(match obj.clone() {
138+
match_class!(match obj.to_owned() {
138139
s @ PyStr => {
139140
Ok(s.as_str().bytes().exactly_one().map_err(|_| {
140141
let msg = r#""delimiter" must be a 1-character string"#;
@@ -148,7 +149,7 @@ mod _csv {
148149
})
149150
}
150151
}
151-
fn parse_quotechar_from_obj(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<Option<u8>> {
152+
fn parse_quotechar_from_obj(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Option<u8>> {
152153
match_class!(match obj.get_attr("quotechar", vm)? {
153154
s @ PyStr => {
154155
Ok(Some(s.as_str().bytes().exactly_one().map_err(|_| {
@@ -169,7 +170,7 @@ mod _csv {
169170
}
170171
})
171172
}
172-
fn parse_escapechar_from_obj(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<Option<u8>> {
173+
fn parse_escapechar_from_obj(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Option<u8>> {
173174
match_class!(match obj.get_attr("escapechar", vm)? {
174175
s @ PyStr => {
175176
Ok(Some(s.as_str().bytes().exactly_one().map_err(|_| {
@@ -191,10 +192,7 @@ mod _csv {
191192
}
192193
})
193194
}
194-
fn prase_lineterminator_from_obj(
195-
vm: &VirtualMachine,
196-
obj: &PyObjectRef,
197-
) -> PyResult<Terminator> {
195+
fn prase_lineterminator_from_obj(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Terminator> {
198196
match_class!(match obj.get_attr("lineterminator", vm)? {
199197
s @ PyStr => {
200198
Ok(if s.as_bytes().eq(b"\r\n") {
@@ -217,7 +215,7 @@ mod _csv {
217215
}
218216
})
219217
}
220-
fn prase_quoting_from_obj(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<QuoteStyle> {
218+
fn prase_quoting_from_obj(vm: &VirtualMachine, obj: &PyObject) -> PyResult<QuoteStyle> {
221219
match_class!(match obj.get_attr("quoting", vm)? {
222220
i @ PyInt => {
223221
Ok(i.try_to_primitive::<isize>(vm)?.try_into().map_err(|_| {

crates/stdlib/src/openssl.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ mod _ssl {
5656
vm::{
5757
AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
5858
builtins::{
59-
PyBaseExceptionRef, PyBytesRef, PyListRef, PyOSError, PyStrRef, PyTypeRef, PyWeak,
59+
PyBaseException, PyBaseExceptionRef, PyBytesRef, PyListRef, PyOSError, PyStrRef,
60+
PyTypeRef, PyWeak,
6061
},
6162
class_or_notimplemented,
6263
convert::ToPyException,
@@ -3351,7 +3352,7 @@ mod _ssl {
33513352

33523353
// Helper function to set verify_code and verify_message on SSLCertVerificationError
33533354
fn set_verify_error_info(
3354-
exc: &PyBaseExceptionRef,
3355+
exc: &Py<PyBaseException>,
33553356
ssl_ptr: *const sys::SSL,
33563357
vm: &VirtualMachine,
33573358
) {

crates/stdlib/src/scproxy.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ mod _scproxy {
55
// straight-forward port of Modules/_scproxy.c
66

77
use crate::vm::{
8-
PyResult, VirtualMachine,
9-
builtins::{PyDictRef, PyStr},
8+
Py, PyResult, VirtualMachine,
9+
builtins::{PyDict, PyDictRef, PyStr},
1010
convert::ToPyObject,
1111
};
1212
use system_configuration::core_foundation::{
@@ -74,7 +74,7 @@ mod _scproxy {
7474

7575
let result = vm.ctx.new_dict();
7676

77-
let set_proxy = |result: &PyDictRef,
77+
let set_proxy = |result: &Py<PyDict>,
7878
proto: &str,
7979
enabled_key: CFStringRef,
8080
host_key: CFStringRef,

crates/stdlib/src/ssl.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,7 @@ mod _ssl {
14231423

14241424
/// Helper: Get path from Python's os.environ
14251425
fn get_env_path(
1426-
environ: &PyObjectRef,
1426+
environ: &PyObject,
14271427
var_name: &str,
14281428
vm: &VirtualMachine,
14291429
) -> PyResult<String> {
@@ -2101,10 +2101,10 @@ mod _ssl {
21012101
// Helper functions (private):
21022102

21032103
/// Parse path argument (str or bytes) to string
2104-
fn parse_path_arg(arg: &PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
2105-
if let Ok(s) = PyStrRef::try_from_object(vm, arg.clone()) {
2104+
fn parse_path_arg(arg: &PyObject, vm: &VirtualMachine) -> PyResult<String> {
2105+
if let Ok(s) = PyStrRef::try_from_object(vm, arg.to_owned()) {
21062106
Ok(s.as_str().to_owned())
2107-
} else if let Ok(b) = ArgBytesLike::try_from_object(vm, arg.clone()) {
2107+
} else if let Ok(b) = ArgBytesLike::try_from_object(vm, arg.to_owned()) {
21082108
String::from_utf8(b.borrow_buf().to_vec())
21092109
.map_err(|_| vm.new_value_error("path contains invalid UTF-8".to_owned()))
21102110
} else {
@@ -2279,10 +2279,10 @@ mod _ssl {
22792279
}
22802280

22812281
/// Helper: Parse cadata argument (str or bytes)
2282-
fn parse_cadata_arg(&self, arg: &PyObjectRef, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
2283-
if let Ok(s) = PyStrRef::try_from_object(vm, arg.clone()) {
2282+
fn parse_cadata_arg(&self, arg: &PyObject, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
2283+
if let Ok(s) = PyStrRef::try_from_object(vm, arg.to_owned()) {
22842284
Ok(s.as_str().as_bytes().to_vec())
2285-
} else if let Ok(b) = ArgBytesLike::try_from_object(vm, arg.clone()) {
2285+
} else if let Ok(b) = ArgBytesLike::try_from_object(vm, arg.to_owned()) {
22862286
Ok(b.borrow_buf().to_vec())
22872287
} else {
22882288
Err(vm.new_type_error("cadata should be a str or bytes".to_owned()))

crates/stdlib/src/ssl/compat.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ use rustls::server::ResolvesServerCert;
2323
use rustls::server::ServerConfig;
2424
use rustls::server::ServerConnection;
2525
use rustls::sign::CertifiedKey;
26-
use rustpython_vm::builtins::PyBaseExceptionRef;
26+
use rustpython_vm::builtins::{PyBaseException, PyBaseExceptionRef};
2727
use rustpython_vm::convert::IntoPyException;
2828
use rustpython_vm::function::ArgBytesLike;
29-
use rustpython_vm::{AsObject, PyObjectRef, PyPayload, PyResult, TryFromObject};
29+
use rustpython_vm::{AsObject, Py, PyObjectRef, PyPayload, PyResult, TryFromObject};
3030
use std::io::Read;
3131
use std::sync::{Arc, Once};
3232

@@ -984,7 +984,7 @@ pub(super) fn create_client_config(options: ClientConfigOptions) -> Result<Clien
984984
}
985985

986986
/// Helper function - check if error is BlockingIOError
987-
pub(super) fn is_blocking_io_error(err: &PyBaseExceptionRef, vm: &VirtualMachine) -> bool {
987+
pub(super) fn is_blocking_io_error(err: &Py<PyBaseException>, vm: &VirtualMachine) -> bool {
988988
err.fast_isinstance(vm.ctx.exceptions.blocking_io_error)
989989
}
990990

@@ -1534,7 +1534,7 @@ fn ssl_read_tls_records(
15341534

15351535
/// Check if an exception is a connection closed error
15361536
/// In SSL context, these errors indicate unexpected connection termination without proper TLS shutdown
1537-
fn is_connection_closed_error(exc: &PyBaseExceptionRef, vm: &VirtualMachine) -> bool {
1537+
fn is_connection_closed_error(exc: &Py<PyBaseException>, vm: &VirtualMachine) -> bool {
15381538
use rustpython_vm::stdlib::errno::errors;
15391539

15401540
// Check for ConnectionAbortedError, ConnectionResetError (Python exception types)

crates/vm/src/builtins/builtin_func.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ impl PyNativeFunction {
5151
}
5252

5353
// PyCFunction_GET_SELF
54-
pub const fn get_self(&self) -> Option<&PyObjectRef> {
54+
pub fn get_self(&self) -> Option<&PyObject> {
5555
if self.value.flags.contains(PyMethodFlags::STATIC) {
5656
return None;
5757
}
58-
self.zelf.as_ref()
58+
self.zelf.as_deref()
5959
}
6060

6161
pub const fn as_func(&self) -> &'static dyn PyNativeFn {

crates/vm/src/builtins/dict.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ impl ExactSizeIterator for DictIter<'_> {
753753
trait DictView: PyPayload + PyClassDef + Iterable + Representable {
754754
type ReverseIter: PyPayload + std::fmt::Debug;
755755

756-
fn dict(&self) -> &PyDictRef;
756+
fn dict(&self) -> &Py<PyDict>;
757757
fn item(vm: &VirtualMachine, key: PyObjectRef, value: PyObjectRef) -> PyObjectRef;
758758

759759
#[pymethod]
@@ -785,7 +785,7 @@ macro_rules! dict_view {
785785
impl DictView for $name {
786786
type ReverseIter = $reverse_iter_name;
787787

788-
fn dict(&self) -> &PyDictRef {
788+
fn dict(&self) -> &Py<PyDict> {
789789
&self.dict
790790
}
791791

@@ -1142,7 +1142,7 @@ impl PyDictKeys {
11421142

11431143
#[pygetset]
11441144
fn mapping(zelf: PyRef<Self>) -> PyMappingProxy {
1145-
PyMappingProxy::from(zelf.dict().clone())
1145+
PyMappingProxy::from(zelf.dict().to_owned())
11461146
}
11471147
}
11481148

@@ -1206,7 +1206,7 @@ impl PyDictItems {
12061206
}
12071207
#[pygetset]
12081208
fn mapping(zelf: PyRef<Self>) -> PyMappingProxy {
1209-
PyMappingProxy::from(zelf.dict().clone())
1209+
PyMappingProxy::from(zelf.dict().to_owned())
12101210
}
12111211
}
12121212

@@ -1269,7 +1269,7 @@ impl AsNumber for PyDictItems {
12691269
impl PyDictValues {
12701270
#[pygetset]
12711271
fn mapping(zelf: PyRef<Self>) -> PyMappingProxy {
1272-
PyMappingProxy::from(zelf.dict().clone())
1272+
PyMappingProxy::from(zelf.dict().to_owned())
12731273
}
12741274
}
12751275

crates/vm/src/builtins/function/jit.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::{
22
AsObject, Py, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine,
3-
builtins::{PyBaseExceptionRef, PyDictRef, PyFunction, PyStrInterned, bool_, float, int},
3+
builtins::{
4+
PyBaseExceptionRef, PyDict, PyDictRef, PyFunction, PyStrInterned, bool_, float, int,
5+
},
46
bytecode::CodeFlags,
57
convert::ToPyObject,
68
function::FuncArgs,
@@ -42,7 +44,7 @@ pub fn new_jit_error(msg: String, vm: &VirtualMachine) -> PyBaseExceptionRef {
4244
vm.new_exception_msg(jit_error, msg)
4345
}
4446

45-
fn get_jit_arg_type(dict: &PyDictRef, name: &str, vm: &VirtualMachine) -> PyResult<JitType> {
47+
fn get_jit_arg_type(dict: &Py<PyDict>, name: &str, vm: &VirtualMachine) -> PyResult<JitType> {
4648
if let Some(value) = dict.get_item_opt(name, vm)? {
4749
if value.is(vm.ctx.types.int_type) {
4850
Ok(JitType::Int)

crates/vm/src/builtins/genericalias.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,11 @@ pub(crate) fn make_parameters(args: &Py<PyTuple>, vm: &VirtualMachine) -> PyTupl
294294
}
295295

296296
#[inline]
297-
fn tuple_index(vec: &[PyObjectRef], item: &PyObjectRef) -> Option<usize> {
297+
fn tuple_index(vec: &[PyObjectRef], item: &PyObject) -> Option<usize> {
298298
vec.iter().position(|element| element.is(item))
299299
}
300300

301-
fn is_unpacked_typevartuple(arg: &PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
301+
fn is_unpacked_typevartuple(arg: &PyObject, vm: &VirtualMachine) -> PyResult<bool> {
302302
if arg.class().is(vm.ctx.types.type_type) {
303303
return Ok(false);
304304
}
@@ -312,7 +312,7 @@ fn is_unpacked_typevartuple(arg: &PyObjectRef, vm: &VirtualMachine) -> PyResult<
312312

313313
fn subs_tvars(
314314
obj: PyObjectRef,
315-
params: &PyTupleRef,
315+
params: &Py<PyTuple>,
316316
arg_items: &[PyObjectRef],
317317
vm: &VirtualMachine,
318318
) -> PyResult {

crates/vm/src/builtins/list.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,8 @@ where
367367
impl MutObjectSequenceOp for PyList {
368368
type Inner = [PyObjectRef];
369369

370-
fn do_get(index: usize, inner: &[PyObjectRef]) -> Option<&PyObjectRef> {
371-
inner.get(index)
370+
fn do_get(index: usize, inner: &[PyObjectRef]) -> Option<&PyObject> {
371+
inner.get(index).map(|r| r.as_ref())
372372
}
373373

374374
fn do_lock(&self) -> impl std::ops::Deref<Target = [PyObjectRef]> {

0 commit comments

Comments
 (0)