我正在阅读 .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());
}
创建测试文件:
- 创建一个 .py 文件
- 在 Python 中导入模块(例如
python(3) -c 'import ...'
) __pycache__
文件夹中应该有一个 .pyc 文件- 将调用中代码中的路径替换为
include_bytes!
实际路径
版本信息
- 生锈 2018
- rustc 1.43.0-nightly (564758c4c 2020-03-08)
- 货物 1.43.0-nightly (bda50510d 2020-03-02)
- CPython 3.7.3
- PyO3 0.9.1