使用ctypes加载时,将Python解释器嵌入C会导致段错误

时间:2018-12-04 09:45:28

标签: python c ctypes

我尝试将Python解释器嵌入C。 为了对此进行测试,我创建了一个共享库并 尝试使用ctypes在Python中加载此代码。不幸的是,这不是 工作,我想了解原因。

以下是示例c-代码:

#ifdef __cplusplus
extern "C" {
#endif

#include <Python.h>


int run_py(void);
int run_py2(void);

int
run_py(void)
{
    printf("hello from run_py\n");
    return 42;
}

int
run_py2(void)
{
    printf("entering c-function: run_py()\n");
    Py_Initialize();
    PyRun_SimpleString("print('hello world')");
    return 0;
}

#ifdef __cplusplus
}
#endif

因此,我使用gcc将其编译为“ mylib.so”,并使用python3.7-config --cflags和--ldflags进行链接等,以此类推。

这是我用来加载它的Python代码。

import ctypes as c
import os
import sys


if __name__ == '__main__':
    print("running shared-lib integration test with python:\n{}".format(sys.version))

    path = os.path.dirname(os.path.realpath(__file__))
    dllfile = os.path.join(path, 'mylib.so')
    dll = c.CDLL(str(dllfile))

    print("loaded CDLL")
    dll.run_py.restype  = c.c_int
    dll.run_py2.restype  = c.c_int

    print("now calling dll.run_py()...")
    rv = dll.run_py()
    print("called dll.run_py: rv={}".format(rv))

    print("now calling dll.run_py2()...")
    rv2 = dll.run_py2()
    print("called dll.run_py2: rv={}".format(rv2))

因此,这仅加载了run_py和run_py2这两个函数 并执行它们。这是输出...

running shared-lib integration test with python:
3.7.1 (default, Oct 22 2018, 10:41:28) 
[GCC 8.2.1 20180831]
loaded CDLL
now calling dll.run_py()...
hello from run_py
called dll.run_py: rv=42
now calling dll.run_py2()...
entering c-function: run_py()
Segmentation fault (core dumped)

因此,从根本上讲,这在调用run_py2时会导致段错误。 原因是调用PyRun_SimpleString。 但是,如果我将其编译为独立的C程序 一切似乎都正常。我真的 想了解为什么会这样...但是目前我 提出想法,因此在此非常感谢您提供任何反馈。

BR jrsm

1 个答案:

答案 0 :(得分:2)

我稍微更改了您的代码。另外,我正在测试 Win (因为这对我来说更方便),但是我确定 Nix 中的情况是相同的。

dll.c

#include <stdio.h>
#include <Python.h>

#define PRINT_MSG_0() printf("From C - [%s] (%d) - [%s]\n", __FILE__, __LINE__, __FUNCTION__)

#if defined(_WIN32)
#define DLL_EXPORT_API __declspec(dllexport)
#else
#define DLL_EXPORT_API
#endif

#if defined(__cplusplus)
extern "C" {
#endif

DLL_EXPORT_API int test0(void);
DLL_EXPORT_API int test1(void);

#if defined(__cplusplus)
}
#endif


int test0(void) {
    PRINT_MSG_0();
    return 42;
}


int test1(void) {
    PRINT_MSG_0();
    Py_Initialize();
    PRINT_MSG_0();
    PyRun_SimpleString("print(\"Hello world!!!\")");
    PRINT_MSG_0();
    return 0;
}

code.py

#!/usr/bin/env python3

import sys
from ctypes import CDLL,\
    c_int


DLL = "./dll.so"


def main():
    dll_dll = CDLL(DLL)
    test0_func = dll_dll.test0
    test0_func.argtypes = None
    test0_func.restype = c_int
    test1_func = dll_dll.test1
    test1_func.argtypes = None
    test1_func.restype = c_int

    print("Calling {:}...".format(test0_func.__name__))
    res = test0_func()
    print("{:} returned {:d}".format(test0_func.__name__, res))
    print("Calling {:}...".format(test1_func.__name__))
    res = test1_func()
    print("{:} returned {:d}".format(test1_func.__name__, res))


if __name__ == "__main__":
    print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
    main()

输出

(py35x64_test) e:\Work\Dev\StackOverflow\q053609932>"c:\Install\x86\Microsoft\Visual Studio Community\2015\vc\vcvarsall.bat" x64

(py35x64_test) e:\Work\Dev\StackOverflow\q053609932>dir /b
code.py
dll.c

(py35x64_test) e:\Work\Dev\StackOverflow\q053609932>cl /nologo /DDLL /MD /Ic:\Install\x64\Python\Python\3.5\include dll.c  /link /NOLOGO /DLL /OUT:dll.so /LIBPATH:c:\Install\x64\Python\Python\3.5\libs
dll.c
   Creating library dll.lib and object dll.exp

(py35x64_test) e:\Work\Dev\StackOverflow\q053609932>dir /b
code.py
dll.c
dll.exp
dll.lib
dll.obj
dll.so

(py35x64_test) e:\Work\Dev\StackOverflow\q053609932>"e:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe" code.py
Python 3.5.4 (v3.5.4:3f56838, Aug  8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32

Calling test0...
From C - [dll.c] (26) - [test0]
test0 returned 42
Calling test1...
From C - [dll.c] (32) - [test1]
From C - [dll.c] (34) - [test1]
Traceback (most recent call last):
  File "code.py", line 30, in <module>
    main()
  File "code.py", line 24, in main
    res = test1_func()
OSError: exception: access violation reading 0x0000000000000010

问题重现。首先,我认为是[Python 3]: void Py_Initialize()通话。但是后来我想起了[Python 3]: class ctypes.PyDLL(name, mode=DEFAULT_MODE, handle=None)强调是我的),其中指出:

  

此类的实例的行为类似于CDLL实例,除了在函数调用期间 not 释放Python GIL,并且在函数执行后检查Python错误标志。如果设置了错误标志,则会引发Python异常。

     

因此,这仅对直接调用Python C api函数有用。

code.py 中用 PyDLL 替换 CDLL ,结果:

(py35x64_test) e:\Work\Dev\StackOverflow\q053609932>"e:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe" code.py
Python 3.5.4 (v3.5.4:3f56838, Aug  8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32

Calling test0...
From C - [dll.c] (26) - [test0]
test0 returned 42
Calling test1...
From C - [dll.c] (32) - [test1]
From C - [dll.c] (34) - [test1]
Hello world!!!
From C - [dll.c] (36) - [test1]
test1 returned 0