pythoncapi:PyDateTime\u FromTimestamp导致分段fau

2024-10-02 02:40:05 发布

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

i遵循this answer调用^ {< CD1>},以在C++中创建^ {< CD2>}对象。但是当调用PyDateTime_FromTimestamp时,我得到了一个Segmentation fault。你知道吗

这是我的C++代码:

#include <python3.6/Python.h>
#include <stdio.h>

#include <python3.6/datetime.h>
#include <sys/time.h>

static PyObject *iGetDateTime_PyFn(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) {
    static double doubleValue = 1314761451;
    PyObject *floatObj = NULL;
    PyObject *timeTuple = NULL;
    PyObject *dateTime = NULL;
    floatObj = PyFloat_FromDouble(doubleValue);
    timeTuple = Py_BuildValue("(O)", floatObj);
    printf("timeTuple = %08x\n", (unsigned int)(long long)timeTuple);
    printf("PyTuple_Check(timeTuple) = %d\n", PyTuple_Check(timeTuple));
    dateTime = PyDateTime_FromTimestamp(timeTuple);
    printf("ready to return\n");
    return dateTime;
}

static PyMethodDef all_methods[] = {
    { "get_datetime", iGetDateTime_PyFn, METH_VARARGS, NULL },
    { NULL, NULL, 0, NULL }
};

static struct PyModuleDef main_module = {
    PyModuleDef_HEAD_INIT,
    "cpp",
    NULL,
    -1,
    all_methods
};

PyMODINIT_FUNC PyInit_cpp(void) {
    return PyModule_Create(&main_module);
}

我用这个命令编译:

g++ --shared -fPIC -o cpp.so t1.cpp

我的g++版本是7.3.0。你知道吗

在python中,我执行:

import cpp
print(cpp.get_datetime())

我打印了以下内容:

timeTuple = a7934358
PyTuple_Check(timeTuple) = 1
Segmentation fault (core dumped)

正如我们所看到的,timeTuple被成功构造,并被检查为tuple。但是我们无法理解return这个句子。你知道吗


Tags: pydatetimereturnincludecheckstaticnullcpp
1条回答
网友
1楼 · 发布于 2024-10-02 02:40:05

根据[GitHub]: python/cpython - (3.6) cpython/Include/datetime.h${PYTHON\u SRC\u DIR}/Include/datetime.h):

  1. PyDateTime\u FromTimestamp是一个预处理器

    #define PyDateTime_FromTimestamp(args) \
        PyDateTimeAPI->DateTime_FromTimestamp( \
            (PyObject*) (PyDateTimeAPI->DateTimeType), args, NULL)
    
  2. PyDateTimeAPI初始化为NULL(文件前面)

    static PyDateTime_CAPI *PyDateTimeAPI = NULL;
    

在调用宏时导致segfault访问冲突)。你知道吗

修复需要通过PyDateTime\u IMPORT宏初始化PyDateTimeAPI。你知道吗

#define PyDateTime_IMPORT \
    PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)

起初,我在浏览代码时发现了这个问题(我在getDateTimePyFn函数中发现了这个问题),然后我遇到了[Python 3.Docs]: DateTime Objectsemphasis是我的)

Before using any of these functions, the header file datetime.h must be included in your source (note that this is not included by Python.h), and the macro PyDateTime_IMPORT must be invoked, usually as part of the module initialization function.

我修改了你的代码,我将在Win上举例说明(因为这对我来说更容易,而且行为是可复制的)。你知道吗

cpp.c公司:

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

#define MOD_NAME "cpp"


static double doubleValue = 1314761451;

static PyObject *getDateTimePyFn(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) {
    PyObject *floatObj = NULL,
        *timeTuple = NULL,
        *dateTime = NULL;
    floatObj = PyFloat_FromDouble(doubleValue);
    if (!floatObj)
    {
        return NULL;
    }
    timeTuple = Py_BuildValue("(O)", floatObj);
    Py_XDECREF(floatObj);
    if (!timeTuple)
    {
        return NULL;
    }
    dateTime = PyDateTime_FromTimestamp(timeTuple);
    Py_XDECREF(timeTuple);
    return dateTime;
}


static PyMethodDef all_methods[] = {
    { "get_datetime", getDateTimePyFn, METH_VARARGS, NULL },
    { NULL, NULL, 0, NULL }
};


static struct PyModuleDef main_module = {
    PyModuleDef_HEAD_INIT,
    MOD_NAME,
    NULL,
    -1,
    all_methods
};


PyMODINIT_FUNC PyInit_cpp(void) {
    PyDateTime_IMPORT;  // @TODO - cfati: !!! This initializes the struct containing the function pointer !!!
    return PyModule_Create(&main_module);
}

代码.py

#!/usr/bin/env python3

import sys
import cpp


def main():
    print("cpp.get_datetime returned: {:}".format(cpp.get_datetime()))


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

输出

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q055903897]> sopr.bat
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***

[prompt]> "c:\Install\x86\Microsoft\Visual Studio Community\2015\vc\vcvarsall.bat" x64

[prompt]> dir /b
code.py
cpp.c

[prompt]> cl /nologo /DDLL /MD /I"c:\Install\x64\Python\Python\03.06.08\include" cpp.c  /link /NOLOGO /DLL /LIBPATH:"c:\Install\x64\Python\Python\03.06.08\libs" /OUT:cpp.pyd
cpp.c
   Creating library cpp.lib and object cpp.exp

[prompt]> dir /b
code.py
cpp.c
cpp.exp
cpp.lib
cpp.obj
cpp.pyd

[prompt]> "e:\Work\Dev\VEnvs\py_064_03.06.08_test0\Scripts\python.exe" code.py
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32

cpp.get_datetime returned: 2011-08-31 06:30:51
Done.

相关问题 更多 >

    热门问题