Skip to content

Commit c16f813

Browse files
authored
Merge pull request RustPython#4899 from youknowone/debug-friendly-tweaks
debug-friendly tweaks
2 parents 6e9e15b + 6996141 commit c16f813

File tree

13 files changed

+57
-37
lines changed

13 files changed

+57
-37
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
@@ -318,8 +318,9 @@ impl PyStr {
318318
Self::new_str_unchecked(bytes, PyStrKind::Ascii)
319319
}
320320

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

325326
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
@@ -322,7 +322,8 @@ impl PyType {
322322
value: V,
323323
ctx: impl AsRef<Context>,
324324
) {
325-
let attr_name = ctx.as_ref().intern_str(attr_name);
325+
let ctx = ctx.as_ref();
326+
let attr_name = ctx.intern_str(attr_name);
326327
self.set_attr(attr_name, value.into())
327328
}
328329

vm/src/function/protocol.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ pub struct ArgCallable {
2020
impl ArgCallable {
2121
#[inline(always)]
2222
pub fn invoke(&self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult {
23-
(self.call)(&self.obj, args.into_args(vm), vm)
23+
let args = args.into_args(vm);
24+
(self.call)(&self.obj, args, vm)
2425
}
2526
}
2627

vm/src/object/core.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,6 @@ cfg_if::cfg_if! {
478478
}
479479
}
480480

481-
#[derive(Debug)]
482481
#[repr(transparent)]
483482
pub struct PyObject(PyInner<Erased>);
484483

@@ -529,7 +528,7 @@ impl PyObjectRef {
529528
#[inline(always)]
530529
pub fn downcast<T: PyObjectPayload>(self) -> Result<PyRef<T>, Self> {
531530
if self.payload_is::<T>() {
532-
Ok(unsafe { PyRef::from_obj_unchecked(self) })
531+
Ok(unsafe { self.downcast_unchecked() })
533532
} else {
534533
Err(self)
535534
}
@@ -546,6 +545,8 @@ impl PyObjectRef {
546545
}
547546
}
548547

548+
/// Force to downcast this reference to a subclass.
549+
///
549550
/// # Safety
550551
/// T must be the exact payload type
551552
#[inline(always)]
@@ -644,13 +645,22 @@ impl PyObject {
644645
self.0.typeid == TypeId::of::<T>()
645646
}
646647

648+
/// Force to return payload as T.
649+
///
650+
/// # Safety
651+
/// The actual payload type must be T.
652+
#[inline(always)]
653+
pub unsafe fn payload_unchecked<T: PyObjectPayload>(&self) -> &T {
654+
// we cast to a PyInner<T> first because we don't know T's exact offset because of
655+
// varying alignment, but once we get a PyInner<T> the compiler can get it for us
656+
let inner = unsafe { &*(&self.0 as *const PyInner<Erased> as *const PyInner<T>) };
657+
&inner.payload
658+
}
659+
647660
#[inline(always)]
648661
pub fn payload<T: PyObjectPayload>(&self) -> Option<&T> {
649662
if self.payload_is::<T>() {
650-
// we cast to a PyInner<T> first because we don't know T's exact offset because of
651-
// varying alignment, but once we get a PyInner<T> the compiler can get it for us
652-
let inner = unsafe { &*(&self.0 as *const PyInner<Erased> as *const PyInner<T>) };
653-
Some(&inner.payload)
663+
Some(unsafe { self.payload_unchecked() })
654664
} else {
655665
None
656666
}
@@ -861,14 +871,20 @@ impl Drop for PyObjectRef {
861871
}
862872
}
863873

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

882+
impl fmt::Debug for PyObjectRef {
883+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
884+
self.as_object().fmt(f)
885+
}
886+
}
887+
872888
#[repr(transparent)]
873889
pub struct Py<T: PyObjectPayload>(PyInner<T>);
874890

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()

vm/src/stdlib/os.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ pub struct OsPath {
5454

5555
impl OsPath {
5656
pub fn new_str(path: impl Into<ffi::OsString>) -> Self {
57+
let path = path.into();
5758
Self {
58-
path: path.into(),
59+
path,
5960
mode: OutputMode::String,
6061
}
6162
}
@@ -178,11 +179,13 @@ impl IOErrorBuilder {
178179
}
179180
}
180181
pub(crate) fn filename(mut self, filename: impl Into<OsPathOrFd>) -> Self {
181-
self.filename.replace(filename.into());
182+
let filename = filename.into();
183+
self.filename.replace(filename);
182184
self
183185
}
184186
pub(crate) fn filename2(mut self, filename: impl Into<OsPathOrFd>) -> Self {
185-
self.filename2.replace(filename.into());
187+
let filename = filename.into();
188+
self.filename2.replace(filename);
186189
self
187190
}
188191
}

vm/src/vm/context.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -558,11 +558,9 @@ impl Context {
558558
where
559559
F: IntoPyGetterFunc<T>,
560560
{
561-
PyRef::new_ref(
562-
PyGetSet::new(name.into(), class).with_get(f),
563-
self.types.getset_type.to_owned(),
564-
None,
565-
)
561+
let name = name.into();
562+
let getset = PyGetSet::new(name, class).with_get(f);
563+
PyRef::new_ref(getset, self.types.getset_type.to_owned(), None)
566564
}
567565

568566
pub fn new_getset<G, S, T, U>(
@@ -576,11 +574,9 @@ impl Context {
576574
G: IntoPyGetterFunc<T>,
577575
S: IntoPySetterFunc<U>,
578576
{
579-
PyRef::new_ref(
580-
PyGetSet::new(name.into(), class).with_get(g).with_set(s),
581-
self.types.getset_type.to_owned(),
582-
None,
583-
)
577+
let name = name.into();
578+
let getset = PyGetSet::new(name, class).with_get(g).with_set(s);
579+
PyRef::new_ref(getset, self.types.getset_type.to_owned(), None)
584580
}
585581

586582
pub fn new_base_object(&self, class: PyTypeRef, dict: Option<PyDictRef>) -> PyObjectRef {

vm/src/vm/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,12 @@ impl VirtualMachine {
475475
#[inline]
476476
pub fn import<'a>(
477477
&self,
478-
module: impl AsPyStr<'a>,
478+
module_name: impl AsPyStr<'a>,
479479
from_list: Option<PyTupleTyped<PyStrRef>>,
480480
level: usize,
481481
) -> PyResult {
482-
self.import_inner(module.as_pystr(&self.ctx), from_list, level)
482+
let module_name = module_name.as_pystr(&self.ctx);
483+
self.import_inner(module_name, from_list, level)
483484
}
484485

485486
fn import_inner(

wasm/lib/src/js_module.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ mod _js {
9494
impl PyJsValue {
9595
#[inline]
9696
pub fn new(value: impl Into<JsValue>) -> PyJsValue {
97-
PyJsValue {
98-
value: value.into(),
99-
}
97+
let value = value.into();
98+
PyJsValue { value }
10099
}
101100

102101
#[pymethod]

0 commit comments

Comments
 (0)