我正在使用一个包装了名为PyTCC的LibTCC的Python库。
我正在尝试使用Python进行JIT编译代码的方法。问题是,在调用函数时,我可以正确返回正常的C数据类型,但是在返回任何PyObject *
时出现“访问冲突”错误。
我已经确保可以从PyTCC执行代码,如我的代码示例所示。这也意味着该代码示例已成功编译。
import ctypes, pytcc
program = b"""
#include "Python.h"
/* Cannot return 3 due to access violation */
PyObject * pop(PyObject * self, PyObject * args, PyObject * kwargs) {
// Cannot return *any* Python object
return PyLong_FromLong(3);
}
int foobar() { return 3; } // Returns 3 just fine
// Needed to appease TCC:
int main() { }
"""
jit_code = pytcc.TCCState()
jit_code.add_include_path('C:/Python37/include')
jit_code.add_library_path('C:/Python37')
jit_code.add_library('python37')
jit_code.compile_string(program)
jit_code.relocate()
foobar_proto = ctypes.CFUNCTYPE(ctypes.c_int)
foobar = foobar_proto(jit_code.get_symbol('foobar'))
print(f'It works: {foobar()}')
pop_proto = ctypes.CFUNCTYPE(ctypes.c_voidp)
pop = pop_proto(jit_code.get_symbol('pop'))
print('But this does not for some reason:')
print(pop())
print('Never gets here due to access violation :(')
程序的输出应为:
It works: 3
But this does not for some reason:
3
Never gets here due to access violation :(
但是,相反,我得到了这个确切的错误:
It works: 3
But this does not for some reason:
Traceback (most recent call last):
File "fails.py", line 40, in <module>
print(pop())
OSError: exception: access violation writing 0x00000000FFC000E9
答案 0 :(得分:0)
很可能是因为创建对象时没有GIL。您还对返回类型有疑问。 ctypes.c_voidp告诉python将其视为int而不是PyObject,因此,如果不是访问冲突,您将看到的只是值指针本身而不是它指向的对象。
尝试:
PyObject * pop() {
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
PyObject* obj = PyLong_FromLong(10);
PyGILState_Release(gstate);
return obj;
}
并切换
pop_proto = ctypes.CFUNCTYPE(ctypes.c_voidp)
到
pop_proto = ctypes.CFUNCTYPE(ctypes.py_object)
我的运行输出(将pyobject中的值从3更改为10只是为了证明它成功了
It works: 3
But this does not for some reason:
10
Never gets here due to access violation :(
答案 1 :(得分:0)
无法与 PyTCC 一起使用,但是代码中有问题。
根据[Python 3]: class ctypes.PyDLL(name, mode=DEFAULT_MODE, handle=None)(强调是我的):
此类的实例的行为类似于CDLL实例,除了在函数调用期间 not 释放Python GIL,并且在函数执行后检查Python错误标志。如果设置了错误标志,则会引发Python异常。
因此,这仅在直接调用Python C api函数时有用。
注意: CFUNCTYPE 用于 CDLL ,与 PYFUNCTYPE 相同 PyDLL 。
因此,在 pop_proto 中,您应将ctypes.CFUNCTYPE
替换为ctypes.PyFUNCTYPE
(请注意,您在 c_voidp 中有错字)。
接下来,同一页显示对于 PyObject * ( C ),应使用 py_object ( Python )。所以:
pop_proto = ctypes.PyFUNCTYPE(ctypes.py_object)
如果您要严格,则必须在原型中包括参数,这会使代码看起来更加复杂,但是对于这种特殊情况(它们将被忽略),它不是强制性的:>
pop_proto = ctypes.PyFUNCTYPE(ctypes.py_object, ctypes.py_object, ctypes.py_object, ctypes.py_object)
下面是PyObject *PyBytes_Repr(PyObject *obj, int smartquotes)
的示例(以“老式”方式调用 C 函数):
[cfati@CFATI-5510-0:C:\WINDOWS\system32]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> >>> import sys >>> import os >>> import ctypes >>> >>> python_dll_name = os.path.join(os.path.dirname(sys.executable), "python" + str(sys.version_info.major) + str(sys.version_info.minor) + ".dll") >>> python_dll_name 'e:\\Work\\Dev\\VEnvs\\py_064_03.07.03_test0\\Scripts\\python37.dll' >>> >>> python_dll = ctypes.PyDLL(python_dll_name) >>> >>> pybytes_repr_proto = ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.py_object, ctypes.c_int) >>> pybytes_repr = pybytes_repr_proto(("PyBytes_Repr", python_dll)) >>> >>> b = b"abcd" >>> >>> reprb = pybytes_repr(b, 0) >>> reprb "b'abcd'"
您还可以选中[SO]: How to cast a ctypes pointer to an instance of a Python class (@CristiFati's answer)。