使用pythoncapi时的奇怪内存行为

2024-10-03 04:31:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在C++库上使用Python C API实现Python包装器。我需要实现转换,以便在Python和C++中使用对象。我过去已经做过了,但我有一个错误,我真的很难处理。你知道吗

我有一个非常基本的测试功能:

PyObject* convert_to_python() {
    std::cout << "Convert to PyObject" << std::endl;
    long int a = 20;
    PyObject* py_a = PyInt_FromLong(a);
    std::cout << "Convert to PyObject ok" << std::endl;
    return py_a;
}

我在GoogleTest宏中调用这个函数:

TEST(Wrapper, ConvertTest) {
    PyObject *py_m = convert_to_python();
}

我的输出是:

Convert to PyObject
Segmentation fault (core dumped)

我还查了valgrind:

valgrind --tool=memcheck --track-origins=yes --leak-check=full ./my_convert

但这并没有给我多少信息:

Invalid read of size 8
==19030==    at 0x4F70A7B: PyInt_FromLong (in /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0)
==19030==    by 0x541E6BF: _object* pysmud_from<float>(smu::Matrix<float, 0, 0>&) (smu_type_conversions.cpp:308)
==19030==    by 0x43A144: (anonymous namespace)::Wrapper_ConvertMatrix_Test::Body() (test_wrapper.cpp:12)
==19030==    by 0x43A0C6: (anonymous namespace)::Wrapper_ConvertMatrix_Test::TestBody() (test_wrapper.cpp:10)
==19030==    by 0x465B4D: void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) (gtest.cc:2078)
==19030==    by 0x460684: void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) (gtest.cc:2114)
==19030==    by 0x444C05: testing::Test::Run() (gtest.cc:2151)
==19030==    by 0x4454C9: testing::TestInfo::Run() (gtest.cc:2326)
==19030==    by 0x445BEA: testing::TestCase::Run() (gtest.cc:2444)
==19030==    by 0x44CF41: testing::internal::UnitTestImpl::RunAllTests() (gtest.cc:4315)
==19030==    by 0x46712C: bool testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) (gtest.cc:2078)
==19030==    by 0x461532: bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) (gtest.cc:2114)
==19030==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

我认为这段代码应该有用,但我不知道我写的有什么问题。我是否错误地包含或链接了Python文件和库?你知道吗

编辑:不提供错误

#include <Python.h>

PyObject* convert_long_int(long int a) {
  PyObject *ret = PyInt_FromLong(a);
  return ret;
}

int main(void) {
  long int a = 65454984;
  PyObject *pya = convert_long_int(a);
  return 0;
}

如果用gcc -o wraptest -I/usr/include/python2.7 wraptest.c -L/usr/lib/x86_64-linux-gnu/ -lpython2.7编译

初始化做什么?你知道吗


Tags: totestconvertbytestinglongintcc
1条回答
网友
1楼 · 发布于 2024-10-03 04:31:41

我可以确认ubuntu16.04和python2.7上的分段错误,如果省略了初始化。你知道吗

看看Embedding Python in Another Application,这里有一个例子

#include <Python.h>

int
main(int argc, char *argv[])
{
  Py_SetProgramName(argv[0]);  /* optional but recommended */
  Py_Initialize();
  PyRun_SimpleString("from time import time,ctime\n"
                     "print 'Today is',ctime(time())\n");
  Py_Finalize();
  return 0;
}

所以当我做一个等价的最小主

int main()
{
    Py_Initialize();
    PyObject *p = convert_to_python();
    Py_Finalize();
    return 0;
}

它能正常工作。你知道吗


这两个例子的区别是

long int a = 20;

以及

long int a = 65454984;

我想,这和PyInt_FromLong(long ival)有关

The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object.

可能Python试图在没有初始化的情况下访问未初始化的指针或内存范围。你知道吗

当我使用a = 256更改示例时,它崩溃了。使用a = 257,它不会


查看cpython/Objects/intobject.c:79,可以看到指针数组

static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];

就在PyInt_FromLong(long ival)的正下方

v = small_ints[ival + NSMALLNEGINTS];
Py_INCREF(v);

但是没有从_PyInt_Init(void)初始化

for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
    if (!free_list && (free_list = fill_free_list()) == NULL)
        return 0;
    /* PyObject_New is inlined */
    v = free_list;
    free_list = (PyIntObject *)Py_TYPE(v);
    (void)PyObject_INIT(v, &PyInt_Type);
    v->ob_ival = ival;
    small_ints[ival + NSMALLNEGINTS] = v;
}

这些指针都是NULL,导致崩溃。你知道吗

相关问题 更多 >