我看了一个关于用Python对C代码进行单元测试的非常有说服力的讲座,我一直在尝试使用这个策略编写一些测试代码。当我试图运行一个非常简单的测试时,我收到错误:
cffi.api.CDefError: cannot parse "extern void *memcpy (void *__restrict __dest, const void *__restrict __src,":5:39: before: __dest
这是我可以解决的cffi的一个限制,还是我用错了?在
作为参考,讲座在这里:https://www.youtube.com/watch?v=zW_HyDTPjO0 下面是带有代码片段的幻灯片:https://ep2016.europython.eu/media/conference/slides/writing-unit-tests-for-c-code-in-python.html
我的头文件:
^{2}$我的实现:
#include <stringutils.h>
int cmp(char* a, char* b) {
return (strcmp(a, b) == 0);
}
包含测试的python文件:
#!/usr/bin/python3
import unittest
import cffi
import importlib
import pycparser
import subprocess
def preprocess(source):
return subprocess.run(['gcc', '-E', '-P', '-'],
input=source, stdout=subprocess.PIPE,
universal_newlines=True, check=True).stdout
def load(filename):
print('Loading ' + filename)
source = open(filename + '.c').read()
includes = preprocess(open(filename + '.h').read())
ffibuilder = cffi.FFI()
ffibuilder.cdef(includes)
print('21')
ffibuilder.set_source(filename + '_', source)
ffibuilder.compile()
module = importlib.import_module(filename + '_')
return module.lib
class StringUtilsTest(unittest.TestCase):
def test_cmp(self):
module = load('stringutils')
self.assertTrue(module.cmp('howdy', 'howdy'))
self.assertFalse(module.cmp('howdy', 'haudy'))
#run(StringUtilsTest)
if __name__ == '__main__':
unittest.main()
这和它的*__restrict
声明有关吗?在
我以前从未尝试过以这种源代码感知的方式混合Python和C。提前谢谢。在
目前没有回答
相关问题 更多 >
编程相关推荐