Skip to content

debug-friendly tweaks #4899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2023
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
6 changes: 2 additions & 4 deletions stdlib/src/json/machinery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ pub struct DecodeError {
}
impl DecodeError {
fn new(msg: impl Into<String>, pos: usize) -> Self {
Self {
msg: msg.into(),
pos,
}
let msg = msg.into();
Self { msg, pos }
}
}

Expand Down
3 changes: 2 additions & 1 deletion vm/src/builtins/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ impl PyByteArray {
fn fromhex(cls: PyTypeRef, string: PyStrRef, vm: &VirtualMachine) -> PyResult {
let bytes = PyBytesInner::fromhex(string.as_str(), vm)?;
let bytes = vm.ctx.new_bytes(bytes);
PyType::call(&cls, vec![bytes.into()].into(), vm)
let args = vec![bytes.into()].into();
PyType::call(&cls, args, vm)
}

#[pymethod]
Expand Down
3 changes: 2 additions & 1 deletion vm/src/builtins/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ impl Py<PyModule> {
}

pub fn get_attr<'a>(&self, attr_name: impl AsPyStr<'a>, vm: &VirtualMachine) -> PyResult {
self.getattr_inner(attr_name.as_pystr(&vm.ctx), vm)
let attr_name = attr_name.as_pystr(&vm.ctx);
self.getattr_inner(attr_name, vm)
}

pub fn set_attr<'a>(
Expand Down
5 changes: 3 additions & 2 deletions vm/src/builtins/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,9 @@ impl PyStr {
Self::new_str_unchecked(bytes, PyStrKind::Ascii)
}

pub fn new_ref(s: impl Into<Self>, ctx: &Context) -> PyRef<Self> {
PyRef::new_ref(s.into(), ctx.types.str_type.to_owned(), None)
pub fn new_ref(zelf: impl Into<Self>, ctx: &Context) -> PyRef<Self> {
let zelf = zelf.into();
PyRef::new_ref(zelf, ctx.types.str_type.to_owned(), None)
}

fn new_substr(&self, s: String) -> Self {
Expand Down
3 changes: 2 additions & 1 deletion vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ impl PyType {
value: V,
ctx: impl AsRef<Context>,
) {
let attr_name = ctx.as_ref().intern_str(attr_name);
let ctx = ctx.as_ref();
let attr_name = ctx.intern_str(attr_name);
self.set_attr(attr_name, value.into())
}

Expand Down
3 changes: 2 additions & 1 deletion vm/src/function/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ pub struct ArgCallable {
impl ArgCallable {
#[inline(always)]
pub fn invoke(&self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult {
(self.call)(&self.obj, args.into_args(vm), vm)
let args = args.into_args(vm);
(self.call)(&self.obj, args, vm)
}
}

Expand Down
30 changes: 23 additions & 7 deletions vm/src/object/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,6 @@ cfg_if::cfg_if! {
}
}

#[derive(Debug)]
#[repr(transparent)]
pub struct PyObject(PyInner<Erased>);

Expand Down Expand Up @@ -522,7 +521,7 @@ impl PyObjectRef {
#[inline(always)]
pub fn downcast<T: PyObjectPayload>(self) -> Result<PyRef<T>, Self> {
if self.payload_is::<T>() {
Ok(unsafe { PyRef::from_obj_unchecked(self) })
Ok(unsafe { self.downcast_unchecked() })
} else {
Err(self)
}
Expand All @@ -539,6 +538,8 @@ impl PyObjectRef {
}
}

/// Force to downcast this reference to a subclass.
///
/// # Safety
/// T must be the exact payload type
#[inline(always)]
Expand Down Expand Up @@ -637,13 +638,22 @@ impl PyObject {
self.0.typeid == TypeId::of::<T>()
}

/// Force to return payload as T.
///
/// # Safety
/// The actual payload type must be T.
#[inline(always)]
pub unsafe fn payload_unchecked<T: PyObjectPayload>(&self) -> &T {
// we cast to a PyInner<T> first because we don't know T's exact offset because of
// varying alignment, but once we get a PyInner<T> the compiler can get it for us
let inner = unsafe { &*(&self.0 as *const PyInner<Erased> as *const PyInner<T>) };
&inner.payload
}

#[inline(always)]
pub fn payload<T: PyObjectPayload>(&self) -> Option<&T> {
if self.payload_is::<T>() {
// we cast to a PyInner<T> first because we don't know T's exact offset because of
// varying alignment, but once we get a PyInner<T> the compiler can get it for us
let inner = unsafe { &*(&self.0 as *const PyInner<Erased> as *const PyInner<T>) };
Some(&inner.payload)
Some(unsafe { self.payload_unchecked() })
} else {
None
}
Expand Down Expand Up @@ -854,14 +864,20 @@ impl Drop for PyObjectRef {
}
}

impl fmt::Debug for PyObjectRef {
impl fmt::Debug for PyObject {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// SAFETY: the vtable contains functions that accept payload types that always match up
// with the payload of the object
unsafe { (self.0.vtable.debug)(self, f) }
}
}

impl fmt::Debug for PyObjectRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_object().fmt(f)
}
}

#[repr(transparent)]
pub struct Py<T: PyObjectPayload>(PyInner<T>);

Expand Down
6 changes: 4 additions & 2 deletions vm/src/protocol/callable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ impl PyObject {

/// PyObject_Call*Arg* series
pub fn call(&self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult {
self.call_with_args(args.into_args(vm), vm)
let args = args.into_args(vm);
self.call_with_args(args, vm)
}

/// PyObject_Call
Expand Down Expand Up @@ -45,8 +46,9 @@ impl<'a> PyCallable<'a> {
}

pub fn invoke(&self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult {
let args = args.into_args(vm);
vm.trace_event(TraceEvent::Call)?;
let result = (self.call)(self.obj, args.into_args(vm), vm);
let result = (self.call)(self.obj, args, vm);
vm.trace_event(TraceEvent::Return)?;
result
}
Expand Down
3 changes: 2 additions & 1 deletion vm/src/protocol/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ impl PyObject {
vm: &VirtualMachine,
) -> PyResult<()> {
let attr_name = attr_name.as_pystr(&vm.ctx);
self.call_set_attr(vm, attr_name, PySetterValue::Assign(attr_value.into()))
let attr_value = attr_value.into();
self.call_set_attr(vm, attr_name, PySetterValue::Assign(attr_value))
}

// int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)
Expand Down
3 changes: 2 additions & 1 deletion vm/src/stdlib/marshal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ mod decl {
self.0.ctx.new_int(value).into()
}
fn make_tuple(&self, elements: impl Iterator<Item = Self::Value>) -> Self::Value {
self.0.ctx.new_tuple(elements.collect()).into()
let elements = elements.collect();
self.0.ctx.new_tuple(elements).into()
}
fn make_code(&self, code: CodeObject) -> Self::Value {
self.0.ctx.new_code(code).into()
Expand Down
9 changes: 6 additions & 3 deletions vm/src/stdlib/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ pub struct OsPath {

impl OsPath {
pub fn new_str(path: impl Into<ffi::OsString>) -> Self {
let path = path.into();
Self {
path: path.into(),
path,
mode: OutputMode::String,
}
}
Expand Down Expand Up @@ -178,11 +179,13 @@ impl IOErrorBuilder {
}
}
pub(crate) fn filename(mut self, filename: impl Into<OsPathOrFd>) -> Self {
self.filename.replace(filename.into());
let filename = filename.into();
self.filename.replace(filename);
self
}
pub(crate) fn filename2(mut self, filename: impl Into<OsPathOrFd>) -> Self {
self.filename2.replace(filename.into());
let filename = filename.into();
self.filename2.replace(filename);
self
}
}
Expand Down
16 changes: 6 additions & 10 deletions vm/src/vm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,11 +558,9 @@ impl Context {
where
F: IntoPyGetterFunc<T>,
{
PyRef::new_ref(
PyGetSet::new(name.into(), class).with_get(f),
self.types.getset_type.to_owned(),
None,
)
let name = name.into();
let getset = PyGetSet::new(name, class).with_get(f);
PyRef::new_ref(getset, self.types.getset_type.to_owned(), None)
}

pub fn new_getset<G, S, T, U>(
Expand All @@ -576,11 +574,9 @@ impl Context {
G: IntoPyGetterFunc<T>,
S: IntoPySetterFunc<U>,
{
PyRef::new_ref(
PyGetSet::new(name.into(), class).with_get(g).with_set(s),
self.types.getset_type.to_owned(),
None,
)
let name = name.into();
let getset = PyGetSet::new(name, class).with_get(g).with_set(s);
PyRef::new_ref(getset, self.types.getset_type.to_owned(), None)
}

pub fn new_base_object(&self, class: PyTypeRef, dict: Option<PyDictRef>) -> PyObjectRef {
Expand Down
5 changes: 3 additions & 2 deletions vm/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,11 +469,12 @@ impl VirtualMachine {
#[inline]
pub fn import<'a>(
&self,
module: impl AsPyStr<'a>,
module_name: impl AsPyStr<'a>,
from_list: Option<PyTupleTyped<PyStrRef>>,
level: usize,
) -> PyResult {
self.import_inner(module.as_pystr(&self.ctx), from_list, level)
let module_name = module_name.as_pystr(&self.ctx);
self.import_inner(module_name, from_list, level)
}

fn import_inner(
Expand Down
5 changes: 2 additions & 3 deletions wasm/lib/src/js_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ mod _js {
impl PyJsValue {
#[inline]
pub fn new(value: impl Into<JsValue>) -> PyJsValue {
PyJsValue {
value: value.into(),
}
let value = value.into();
PyJsValue { value }
}

#[pymethod]
Expand Down