【问题标题】:Strange memory behaviour when using Python C API使用 Python C API 时出现奇怪的内存行为
【发布时间】:2018-02-22 09:30:39
【问题描述】:

我正在尝试在 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编译

初始化是做什么的?

【问题讨论】:

  • 您知道返回后的 cout 语句永远不会显示对吗?是错字吗:p?
  • 返回之前是我的错误。我在写这个问题时放错了位置。

标签: python c++ wrapper


【解决方案1】:

我可以在 Ubuntu 16.04 和 Python 2.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)有关

当前的实现为 -5 到 256 之间的所有整数保留一个整数对象数组,当您在该范围内创建一个 int 时,实际上您只是取回对现有对象的引用。

也许 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,导致崩溃。

【讨论】:

  • 我尝试了一个简单的 main 并使用 gcc -o wraptest -I/usr/include/python2.7 wraptest.c -L/usr/lib/x86_64-linux-gnu/ -lpython2.7 编译也没有错误。但在这种情况下,我也没有初​​始化。
猜你喜欢
  • 2013-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多