我正在读取.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`做这件事的正确方法是什么?
MCVE
use pyo3::{ffi::PyCodeObject, marshal::loads, Python};
fn main() {
let gil_guard = Python::acquire_gil();
let py = gil_guard.python();
let code_buffer = &include_bytes!("__pycache__/test.cpython-37.pyc")[16..];
let code = *(loads(py, &code_buffer)
.unwrap()
.downcast::<PyCodeObject>()
.unwrap());
}要创建测试文件:
python(3) -c 'import ...')
__pycache__include_bytes!的代码中使用实际路径中的路径)
版本信息
2020-03-08)
)
发布于 2020-03-23 02:55:47
我想我已经想好了该怎么做
let code_ptr = loads(py, &code_buffer)?.as_ptr() as *mut PyCodeObject;
// This should be valid, since PyCodeObject is Copy, as long as the refcount is positive
let code = unsafe { *code_ptr };https://stackoverflow.com/questions/60802806
复制相似问题