V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
leoleoasd
V2EX  ›  Python

该如何解决 PyMarshal_ReadObjectFromFile 造成 segmentation fault

  •  
  •   leoleoasd ·
    leoleoasd · 2019-02-18 20:49:26 +08:00 · 1015 次点击
    这是一个创建于 1886 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如题 我的 c++代码:

    #include <Python.h>
    #include <iostream>
    #include <marshal.h>
    using namespace std;
    int main(){
        PyObject *pName, *pModule, *pFunc;
        PyObject *pArgs, *pValue;
        //cout<<233<<endl;
        Py_Initialize();
        FILE *fff = fopen("./a.pyc","br");
        pModule = PyMarshal_ReadObjectFromFile(fff);
        //Py_DECREF(pModule);
        //cout<<PyCode_GetNumFree(pModule);
        //PyImport_ExecCodeModule("asd",pModule);
        pValue = PyLong_FromLong(123);
        cout<<PyCode_Check(pModule);
        
        return 0;
    }
    

    只要运行了 PyCode_Check(pModule) 就会导致段错误 进一步测试发现 只要调用 pModule->ob_type 就会导致段错误 同时 a.pyc 文件在 python 中使用 marshal.load 读取 运行 是完全正常的

    环境: macos 10.13 测试过 homebrew 的 python 以及 miniconda 的 python 均有这个问题

    该如何解决?

    1 条回复    2019-03-01 23:12:32 +08:00
    leoleoasd
        1
    leoleoasd  
    OP
       2019-03-01 23:12:32 +08:00
    docker 内运行就能解决这个问题

    同时还有个坑: 网上都说 pyc 文件前 8 字节是文件头要去掉 但是 python3.7 中是 16 字节
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3585 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 04:35 · PVG 12:35 · LAX 21:35 · JFK 00:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.