Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions crates/stdlib/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ pub(crate) use _csv::make_module;
mod _csv {
use crate::common::lock::PyMutex;
use crate::vm::{
AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
VirtualMachine,
builtins::{PyBaseExceptionRef, PyInt, PyNone, PyStr, PyType, PyTypeRef},
function::{ArgIterable, ArgumentError, FromArgs, FuncArgs, OptionalArg},
protocol::{PyIter, PyIterReturn},
Expand Down Expand Up @@ -130,11 +131,11 @@ mod _csv {
///
/// * If the 'delimiter' attribute is not a single-character string, a type error is returned.
/// * If the 'obj' is not of string type and does not have a 'delimiter' attribute, a type error is returned.
fn parse_delimiter_from_obj(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<u8> {
fn parse_delimiter_from_obj(vm: &VirtualMachine, obj: &PyObject) -> PyResult<u8> {
if let Ok(attr) = obj.get_attr("delimiter", vm) {
parse_delimiter_from_obj(vm, &attr)
} else {
match_class!(match obj.clone() {
match_class!(match obj.to_owned() {
s @ PyStr => {
Ok(s.as_str().bytes().exactly_one().map_err(|_| {
let msg = r#""delimiter" must be a 1-character string"#;
Expand All @@ -148,7 +149,7 @@ mod _csv {
})
}
}
fn parse_quotechar_from_obj(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<Option<u8>> {
fn parse_quotechar_from_obj(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Option<u8>> {
match_class!(match obj.get_attr("quotechar", vm)? {
s @ PyStr => {
Ok(Some(s.as_str().bytes().exactly_one().map_err(|_| {
Expand All @@ -169,7 +170,7 @@ mod _csv {
}
})
}
fn parse_escapechar_from_obj(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<Option<u8>> {
fn parse_escapechar_from_obj(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Option<u8>> {
match_class!(match obj.get_attr("escapechar", vm)? {
s @ PyStr => {
Ok(Some(s.as_str().bytes().exactly_one().map_err(|_| {
Expand All @@ -191,10 +192,7 @@ mod _csv {
}
})
}
fn prase_lineterminator_from_obj(
vm: &VirtualMachine,
obj: &PyObjectRef,
) -> PyResult<Terminator> {
fn prase_lineterminator_from_obj(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Terminator> {
match_class!(match obj.get_attr("lineterminator", vm)? {
s @ PyStr => {
Ok(if s.as_bytes().eq(b"\r\n") {
Expand All @@ -217,7 +215,7 @@ mod _csv {
}
})
}
fn prase_quoting_from_obj(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<QuoteStyle> {
fn prase_quoting_from_obj(vm: &VirtualMachine, obj: &PyObject) -> PyResult<QuoteStyle> {
match_class!(match obj.get_attr("quoting", vm)? {
i @ PyInt => {
Ok(i.try_to_primitive::<isize>(vm)?.try_into().map_err(|_| {
Expand Down
5 changes: 3 additions & 2 deletions crates/stdlib/src/openssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ mod _ssl {
vm::{
AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
builtins::{
PyBaseExceptionRef, PyBytesRef, PyListRef, PyOSError, PyStrRef, PyTypeRef, PyWeak,
PyBaseException, PyBaseExceptionRef, PyBytesRef, PyListRef, PyOSError, PyStrRef,
PyTypeRef, PyWeak,
},
class_or_notimplemented,
convert::ToPyException,
Expand Down Expand Up @@ -3351,7 +3352,7 @@ mod _ssl {

// Helper function to set verify_code and verify_message on SSLCertVerificationError
fn set_verify_error_info(
exc: &PyBaseExceptionRef,
exc: &Py<PyBaseException>,
ssl_ptr: *const sys::SSL,
vm: &VirtualMachine,
) {
Expand Down
6 changes: 3 additions & 3 deletions crates/stdlib/src/scproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ mod _scproxy {
// straight-forward port of Modules/_scproxy.c

use crate::vm::{
PyResult, VirtualMachine,
builtins::{PyDictRef, PyStr},
Py, PyResult, VirtualMachine,
builtins::{PyDict, PyDictRef, PyStr},
convert::ToPyObject,
};
use system_configuration::core_foundation::{
Expand Down Expand Up @@ -74,7 +74,7 @@ mod _scproxy {

let result = vm.ctx.new_dict();

let set_proxy = |result: &PyDictRef,
let set_proxy = |result: &Py<PyDict>,
proto: &str,
enabled_key: CFStringRef,
host_key: CFStringRef,
Expand Down
14 changes: 7 additions & 7 deletions crates/stdlib/src/ssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,7 @@ mod _ssl {

/// Helper: Get path from Python's os.environ
fn get_env_path(
environ: &PyObjectRef,
environ: &PyObject,
var_name: &str,
vm: &VirtualMachine,
) -> PyResult<String> {
Expand Down Expand Up @@ -2101,10 +2101,10 @@ mod _ssl {
// Helper functions (private):

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

/// Helper: Parse cadata argument (str or bytes)
fn parse_cadata_arg(&self, arg: &PyObjectRef, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
if let Ok(s) = PyStrRef::try_from_object(vm, arg.clone()) {
fn parse_cadata_arg(&self, arg: &PyObject, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
if let Ok(s) = PyStrRef::try_from_object(vm, arg.to_owned()) {
Ok(s.as_str().as_bytes().to_vec())
} else if let Ok(b) = ArgBytesLike::try_from_object(vm, arg.clone()) {
} else if let Ok(b) = ArgBytesLike::try_from_object(vm, arg.to_owned()) {
Ok(b.borrow_buf().to_vec())
} else {
Err(vm.new_type_error("cadata should be a str or bytes".to_owned()))
Expand Down
8 changes: 4 additions & 4 deletions crates/stdlib/src/ssl/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ use rustls::server::ResolvesServerCert;
use rustls::server::ServerConfig;
use rustls::server::ServerConnection;
use rustls::sign::CertifiedKey;
use rustpython_vm::builtins::PyBaseExceptionRef;
use rustpython_vm::builtins::{PyBaseException, PyBaseExceptionRef};
use rustpython_vm::convert::IntoPyException;
use rustpython_vm::function::ArgBytesLike;
use rustpython_vm::{AsObject, PyObjectRef, PyPayload, PyResult, TryFromObject};
use rustpython_vm::{AsObject, Py, PyObjectRef, PyPayload, PyResult, TryFromObject};
use std::io::Read;
use std::sync::{Arc, Once};

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

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

Expand Down Expand Up @@ -1534,7 +1534,7 @@ fn ssl_read_tls_records(

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

// Check for ConnectionAbortedError, ConnectionResetError (Python exception types)
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/builtins/builtin_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ impl PyNativeFunction {
}

// PyCFunction_GET_SELF
pub const fn get_self(&self) -> Option<&PyObjectRef> {
pub fn get_self(&self) -> Option<&PyObject> {
if self.value.flags.contains(PyMethodFlags::STATIC) {
return None;
}
self.zelf.as_ref()
self.zelf.as_deref()
}

pub const fn as_func(&self) -> &'static dyn PyNativeFn {
Expand Down
10 changes: 5 additions & 5 deletions crates/vm/src/builtins/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ impl ExactSizeIterator for DictIter<'_> {
trait DictView: PyPayload + PyClassDef + Iterable + Representable {
type ReverseIter: PyPayload + std::fmt::Debug;

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

#[pymethod]
Expand Down Expand Up @@ -785,7 +785,7 @@ macro_rules! dict_view {
impl DictView for $name {
type ReverseIter = $reverse_iter_name;

fn dict(&self) -> &PyDictRef {
fn dict(&self) -> &Py<PyDict> {
&self.dict
}

Expand Down Expand Up @@ -1142,7 +1142,7 @@ impl PyDictKeys {

#[pygetset]
fn mapping(zelf: PyRef<Self>) -> PyMappingProxy {
PyMappingProxy::from(zelf.dict().clone())
PyMappingProxy::from(zelf.dict().to_owned())
}
}

Expand Down Expand Up @@ -1206,7 +1206,7 @@ impl PyDictItems {
}
#[pygetset]
fn mapping(zelf: PyRef<Self>) -> PyMappingProxy {
PyMappingProxy::from(zelf.dict().clone())
PyMappingProxy::from(zelf.dict().to_owned())
}
}

Expand Down Expand Up @@ -1269,7 +1269,7 @@ impl AsNumber for PyDictItems {
impl PyDictValues {
#[pygetset]
fn mapping(zelf: PyRef<Self>) -> PyMappingProxy {
PyMappingProxy::from(zelf.dict().clone())
PyMappingProxy::from(zelf.dict().to_owned())
}
}

Expand Down
6 changes: 4 additions & 2 deletions crates/vm/src/builtins/function/jit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::{
AsObject, Py, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine,
builtins::{PyBaseExceptionRef, PyDictRef, PyFunction, PyStrInterned, bool_, float, int},
builtins::{
PyBaseExceptionRef, PyDict, PyDictRef, PyFunction, PyStrInterned, bool_, float, int,
},
bytecode::CodeFlags,
convert::ToPyObject,
function::FuncArgs,
Expand Down Expand Up @@ -42,7 +44,7 @@ pub fn new_jit_error(msg: String, vm: &VirtualMachine) -> PyBaseExceptionRef {
vm.new_exception_msg(jit_error, msg)
}

fn get_jit_arg_type(dict: &PyDictRef, name: &str, vm: &VirtualMachine) -> PyResult<JitType> {
fn get_jit_arg_type(dict: &Py<PyDict>, name: &str, vm: &VirtualMachine) -> PyResult<JitType> {
if let Some(value) = dict.get_item_opt(name, vm)? {
if value.is(vm.ctx.types.int_type) {
Ok(JitType::Int)
Expand Down
6 changes: 3 additions & 3 deletions crates/vm/src/builtins/genericalias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,11 @@ pub(crate) fn make_parameters(args: &Py<PyTuple>, vm: &VirtualMachine) -> PyTupl
}

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

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

fn subs_tvars(
obj: PyObjectRef,
params: &PyTupleRef,
params: &Py<PyTuple>,
arg_items: &[PyObjectRef],
vm: &VirtualMachine,
) -> PyResult {
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/builtins/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ where
impl MutObjectSequenceOp for PyList {
type Inner = [PyObjectRef];

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

fn do_lock(&self) -> impl std::ops::Deref<Target = [PyObjectRef]> {
Expand Down
6 changes: 3 additions & 3 deletions crates/vm/src/builtins/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl PyProperty {
#[pygetset]
fn __isabstractmethod__(&self, vm: &VirtualMachine) -> PyResult {
// Helper to check if a method is abstract
let is_abstract = |method: &PyObjectRef| -> PyResult<bool> {
let is_abstract = |method: &PyObject| -> PyResult<bool> {
match method.get_attr("__isabstractmethod__", vm) {
Ok(isabstract) => isabstract.try_to_bool(vm),
Err(_) => Ok(false),
Expand Down Expand Up @@ -309,7 +309,7 @@ impl PyProperty {
#[cold]
fn format_property_error(
&self,
obj: &PyObjectRef,
obj: &PyObject,
error_type: &str,
vm: &VirtualMachine,
) -> PyResult<String> {
Expand Down Expand Up @@ -356,7 +356,7 @@ impl Initializer for PyProperty {
let mut getter_doc = false;

// Helper to get doc from getter
let get_getter_doc = |fget: &PyObjectRef| -> Option<PyObjectRef> {
let get_getter_doc = |fget: &PyObject| -> Option<PyObjectRef> {
fget.get_attr("__doc__", vm)
.ok()
.filter(|doc| !vm.is_none(doc))
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ fn is_subtype_with_mro(a_mro: &[PyTypeRef], a: &Py<PyType>, b: &Py<PyType>) -> b
impl PyType {
pub fn new_simple_heap(
name: &str,
base: &PyTypeRef,
base: &Py<PyType>,
ctx: &Context,
) -> Result<PyRef<Self>, String> {
Self::new_heap(
name,
vec![base.clone()],
vec![base.to_owned()],
Default::default(),
Default::default(),
Self::static_type().to_owned(),
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl PyUnion {

/// Direct access to args field, matching CPython's _Py_union_args
#[inline]
pub const fn args(&self) -> &PyTupleRef {
pub fn args(&self) -> &Py<PyTuple> {
&self.args
}

Expand Down
16 changes: 10 additions & 6 deletions crates/vm/src/cformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use crate::common::cformat::*;
use crate::common::wtf8::{CodePoint, Wtf8, Wtf8Buf};
use crate::{
AsObject, PyObjectRef, PyResult, TryFromBorrowedObject, TryFromObject, VirtualMachine,
AsObject, PyObject, PyObjectRef, PyResult, TryFromBorrowedObject, TryFromObject,
VirtualMachine,
builtins::{
PyBaseExceptionRef, PyByteArray, PyBytes, PyFloat, PyInt, PyStr, try_f64_to_bigint, tuple,
},
Expand Down Expand Up @@ -207,7 +208,7 @@ fn spec_format_string(

fn try_update_quantity_from_element(
vm: &VirtualMachine,
element: Option<&PyObjectRef>,
element: Option<&PyObject>,
) -> PyResult<CFormatQuantity> {
match element {
Some(width_obj) => {
Expand All @@ -224,7 +225,7 @@ fn try_update_quantity_from_element(

fn try_conversion_flag_from_tuple(
vm: &VirtualMachine,
element: Option<&PyObjectRef>,
element: Option<&PyObject>,
) -> PyResult<CConversionFlags> {
match element {
Some(width_obj) => {
Expand Down Expand Up @@ -254,8 +255,11 @@ fn try_update_quantity_from_tuple<'a, I: Iterator<Item = &'a PyObjectRef>>(
return Ok(());
};
let element = elements.next();
f.insert(try_conversion_flag_from_tuple(vm, element)?);
let quantity = try_update_quantity_from_element(vm, element)?;
f.insert(try_conversion_flag_from_tuple(
vm,
element.map(|v| v.as_ref()),
)?);
let quantity = try_update_quantity_from_element(vm, element.map(|v| v.as_ref()))?;
*q = Some(quantity);
Ok(())
}
Expand All @@ -268,7 +272,7 @@ fn try_update_precision_from_tuple<'a, I: Iterator<Item = &'a PyObjectRef>>(
let Some(CFormatPrecision::Quantity(CFormatQuantity::FromValuesTuple)) = p else {
return Ok(());
};
let quantity = try_update_quantity_from_element(vm, elements.next())?;
let quantity = try_update_quantity_from_element(vm, elements.next().map(|v| v.as_ref()))?;
*p = Some(CFormatPrecision::Quantity(quantity));
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/codecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl PyCodec {
self.0
}
#[inline]
pub const fn as_tuple(&self) -> &PyTupleRef {
pub fn as_tuple(&self) -> &Py<PyTuple> {
&self.0
}

Expand Down
Loading
Loading