Skip to content

Commit e4d0a58

Browse files
committed
validate_downcast
1 parent 98fff96 commit e4d0a58

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

crates/vm/src/builtins/builtin_func.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl Representable for PyNativeFunction {
148148
impl Unconstructible for PyNativeFunction {}
149149

150150
// `PyCMethodObject` in CPython
151-
#[pyclass(name = "builtin_method", module = false, base = PyNativeFunction)]
151+
#[pyclass(name = "builtin_method", module = false, base = PyNativeFunction, ctx = "builtin_method_type")]
152152
pub struct PyNativeMethod {
153153
pub(crate) func: PyNativeFunction,
154154
pub(crate) class: &'static Py<PyType>, // TODO: the actual life is &'self
@@ -189,12 +189,6 @@ impl PyNativeMethod {
189189
}
190190
}
191191

192-
impl PyPayload for PyNativeMethod {
193-
fn class(ctx: &Context) -> &'static Py<PyType> {
194-
ctx.types.builtin_method_type
195-
}
196-
}
197-
198192
impl fmt::Debug for PyNativeMethod {
199193
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
200194
write!(

crates/vm/src/builtins/str.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,12 +1932,10 @@ impl PyPayload for PyUtf8Str {
19321932
std::any::TypeId::of::<PyStr>()
19331933
}
19341934

1935-
fn downcastable_from(obj: &PyObject) -> bool {
1936-
obj.typeid() == Self::payload_type_id() && {
1937-
// SAFETY: we know the object is a PyStr in this context
1938-
let wtf8 = unsafe { obj.downcast_unchecked_ref::<PyStr>() };
1939-
wtf8.is_utf8()
1940-
}
1935+
fn validate_downcastable_from(obj: &PyObject) -> bool {
1936+
// SAFETY: we know the object is a PyStr in this context
1937+
let wtf8 = unsafe { obj.downcast_unchecked_ref::<PyStr>() };
1938+
wtf8.is_utf8()
19411939
}
19421940

19431941
fn try_downcast_from(obj: &PyObject, vm: &VirtualMachine) -> PyResult<()> {

crates/vm/src/object/payload.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ pub trait PyPayload: MaybeTraverse + PyThreadingConstraint + Sized + 'static {
2525
/// # Safety: this function should only be called if `payload_type_id` matches the type of `obj`.
2626
#[inline]
2727
fn downcastable_from(obj: &PyObject) -> bool {
28-
obj.typeid() == Self::payload_type_id()
28+
obj.typeid() == Self::payload_type_id() && Self::validate_downcastable_from(obj)
29+
}
30+
31+
#[inline]
32+
fn validate_downcastable_from(_obj: &PyObject) -> bool {
33+
true
2934
}
3035

3136
fn try_downcast_from(obj: &PyObject, vm: &VirtualMachine) -> PyResult<()> {

0 commit comments

Comments
 (0)