mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Merge pull request #4899 from youknowone/debug-friendly-tweaks
debug-friendly tweaks
This commit is contained in:
@@ -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 }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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>(
|
||||
|
||||
@@ -318,8 +318,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 {
|
||||
|
||||
@@ -322,7 +322,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())
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -478,7 +478,6 @@ cfg_if::cfg_if! {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[repr(transparent)]
|
||||
pub struct PyObject(PyInner<Erased>);
|
||||
|
||||
@@ -529,7 +528,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)
|
||||
}
|
||||
@@ -546,6 +545,8 @@ impl PyObjectRef {
|
||||
}
|
||||
}
|
||||
|
||||
/// Force to downcast this reference to a subclass.
|
||||
///
|
||||
/// # Safety
|
||||
/// T must be the exact payload type
|
||||
#[inline(always)]
|
||||
@@ -644,13 +645,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
|
||||
}
|
||||
@@ -861,7 +871,7 @@ 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
|
||||
@@ -869,6 +879,12 @@ impl fmt::Debug for PyObjectRef {
|
||||
}
|
||||
}
|
||||
|
||||
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>);
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>(
|
||||
@@ -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 {
|
||||
|
||||
@@ -475,11 +475,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(
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user