Skip to content

Commit 6996141

Browse files
committed
debug-friendly tweaks
1 parent a354f7b commit 6996141

File tree

14 files changed

+61
-39
lines changed

14 files changed

+61
-39
lines changed

stdlib/src/json/machinery.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,8 @@ pub struct DecodeError {
105105
}
106106
impl DecodeError {
107107
fn new(msg: impl Into<String>, pos: usize) -> Self {
108-
Self {
109-
msg: msg.into(),
110-
pos,
111-
}
108+
let msg = msg.into();
109+
Self { msg, pos }
112110
}
113111
}
114112

vm/src/builtins/bytearray.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,8 @@ impl PyByteArray {
326326
fn fromhex(cls: PyTypeRef, string: PyStrRef, vm: &VirtualMachine) -> PyResult {
327327
let bytes = PyBytesInner::fromhex(string.as_str(), vm)?;
328328
let bytes = vm.ctx.new_bytes(bytes);
329-
PyType::call(&cls, vec![bytes.into()].into(), vm)
329+
let args = vec![bytes.into()].into();
330+
PyType::call(&cls, args, vm)
330331
}
331332

332333
#[pymethod]

vm/src/builtins/module.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ impl Py<PyModule> {
8080
}
8181

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

8687
pub fn set_attr<'a>(

vm/src/builtins/str.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,9 @@ impl PyStr {
310310
Self::new_str_unchecked(bytes, PyStrKind::Ascii)
311311
}
312312

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

317318
fn new_substr(&self, s: String) -> Self {

vm/src/builtins/type.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ impl PyType {
301301
value: V,
302302
ctx: impl AsRef<Context>,
303303
) {
304-
let attr_name = ctx.as_ref().intern_str(attr_name);
304+
let ctx = ctx.as_ref();
305+
let attr_name = ctx.intern_str(attr_name);
305306
self.set_attr(attr_name, value.into())
306307
}
307308

vm/src/function/protocol.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ pub struct ArgCallable {
1818
impl ArgCallable {
1919
#[inline(always)]
2020
pub fn invoke(&self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult {
21-
(self.call)(&self.obj, args.into_args(vm), vm)
21+
let args = args.into_args(vm);
22+
(self.call)(&self.obj, args, vm)
2223
}
2324
}
2425

vm/src/object/core.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,6 @@ cfg_if::cfg_if! {
471471
}
472472
}
473473

474-
#[derive(Debug)]
475474
#[repr(transparent)]
476475
pub struct PyObject(PyInner<Erased>);
477476

@@ -522,7 +521,7 @@ impl PyObjectRef {
522521
#[inline(always)]
523522
pub fn downcast<T: PyObjectPayload>(self) -> Result<PyRef<T>, Self> {
524523
if self.payload_is::<T>() {
525-
Ok(unsafe { PyRef::from_obj_unchecked(self) })
524+
Ok(unsafe { self.downcast_unchecked() })
526525
} else {
527526
Err(self)
528527
}
@@ -539,6 +538,8 @@ impl PyObjectRef {
539538
}
540539
}
541540

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

641+
/// Force to return payload as T.
642+
///
643+
/// # Safety
644+
/// The actual payload type must be T.
645+
#[inline(always)]
646+
pub unsafe fn payload_unchecked<T: PyObjectPayload>(&self) -> &T {
647+
// we cast to a PyInner<T> first because we don't know T's exact offset because of
648+
// varying alignment, but once we get a PyInner<T> the compiler can get it for us
649+
let inner = unsafe { &*(&self.0 as *const PyInner<Erased> as *const PyInner<T>) };
650+
&inner.payload
651+
}
652+
640653
#[inline(always)]
641654
pub fn payload<T: PyObjectPayload>(&self) -> Option<&T> {
642655
if self.payload_is::<T>() {
643-
// we cast to a PyInner<T> first because we don't know T's exact offset because of
644-
// varying alignment, but once we get a PyInner<T> the compiler can get it for us
645-
let inner = unsafe { &*(&self.0 as *const PyInner<Erased> as *const PyInner<T>) };
646-
Some(&inner.payload)
656+
Some(unsafe { self.payload_unchecked() })
647657
} else {
648658
None
649659
}
@@ -854,14 +864,20 @@ impl Drop for PyObjectRef {
854864
}
855865
}
856866

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

875+
impl fmt::Debug for PyObjectRef {
876+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
877+
self.as_object().fmt(f)
878+
}
879+
}
880+
865881
#[repr(transparent)]
866882
pub struct Py<T: PyObjectPayload>(PyInner<T>);
867883

vm/src/protocol/callable.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ impl PyObject {
1717

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

2324
/// PyObject_Call
@@ -45,8 +46,9 @@ impl<'a> PyCallable<'a> {
4546
}
4647

4748
pub fn invoke(&self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult {
49+
let args = args.into_args(vm);
4850
vm.trace_event(TraceEvent::Call)?;
49-
let result = (self.call)(self.obj, args.into_args(vm), vm);
51+
let result = (self.call)(self.obj, args, vm);
5052
vm.trace_event(TraceEvent::Return)?;
5153
result
5254
}

vm/src/protocol/object.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ impl PyObject {
133133
vm: &VirtualMachine,
134134
) -> PyResult<()> {
135135
let attr_name = attr_name.as_pystr(&vm.ctx);
136-
self.call_set_attr(vm, attr_name, PySetterValue::Assign(attr_value.into()))
136+
let attr_value = attr_value.into();
137+
self.call_set_attr(vm, attr_name, PySetterValue::Assign(attr_value))
137138
}
138139

139140
// int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)

vm/src/stdlib/marshal.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ mod decl {
149149
self.0.ctx.new_int(value).into()
150150
}
151151
fn make_tuple(&self, elements: impl Iterator<Item = Self::Value>) -> Self::Value {
152-
self.0.ctx.new_tuple(elements.collect()).into()
152+
let elements = elements.collect();
153+
self.0.ctx.new_tuple(elements).into()
153154
}
154155
fn make_code(&self, code: CodeObject) -> Self::Value {
155156
self.0.ctx.new_code(code).into()

0 commit comments

Comments
 (0)