我正在阅读.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`
正确的方法是什么
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__
文件夹中应该有一个.pyc文件include_bytes!
的调用中代码中的路径
我想我已经想出了如何做到这一点:
相关问题 更多 >
编程相关推荐