PyO3解组PyCodeObject

问题描述 投票:0回答:1

我正在制作一个涉及读取.pyc文件的应用程序,因此,我需要能够解组代码对象。但是,当我尝试将未编组的PyAny转换为PyCodeObject时,出现以下错误消息:

error[E0277]: the trait bound `pyo3::ffi::code::PyCodeObject: pyo3::type_object::PyTypeInfo` is not satisfied
   --> src/lib.rs:179:47
    |
179 |         let code = *(loads(py, &code_buffer)?.downcast::<PyCodeObject>()?);
    |                                               ^^^^^^^^ the trait `pyo3::type_object::PyTypeInfo` is not implemented for `pyo3::ffi::code::PyCodeObject`
    |
    = note: required because of the requirements on the impl of `for<'py> pyo3::conversion::PyTryFrom<'py>` for `pyo3::ffi::code::PyCodeObject`

error[E0277]: the trait bound `pyo3::ffi::code::PyCodeObject: pyo3::instance::PyNativeType` is not satisfied
   --> src/lib.rs:179:47
    |
179 |         let code = *(loads(py, &code_buffer)?.downcast::<PyCodeObject>()?);
    |                                               ^^^^^^^^ the trait `pyo3::instance::PyNativeType` is not implemented for `pyo3::ffi::code::PyCodeObject`
    |
    = note: required because of the requirements on the impl of `for<'py> pyo3::conversion::PyTryFrom<'py>` for `pyo3::ffi::code::PyCodeObject`

任何想法,什么是正确的方法?

python rust unmarshalling ffi pyo3
1个回答
0
投票

我想我已经知道如何做到这一点:

let code_ptr = loads(py, &code_buffer)?.as_ptr() as *mut PyCodeObject;
let code = unsafe { *code_ptr }; // I hope this is valid...
© www.soinside.com 2019 - 2024. All rights reserved.