简体   繁体   English

Python 对象未使用 C API 完全初始化

[英]Python object not fully initialized using C API

In the following scenario, the object is not meant to be instantiated within Python (hence no tp_new or tp_init ).在以下场景中,对象不打算在 Python 中实例化(因此没有tp_newtp_init )。 Calling the ThingType as a function using PyObject_CallObject results in a segfault in _PyObject_FastCallDict , I believe because there are no constructors.使用PyObject_CallObject将 ThingType 作为函数调用会导致 _PyObject_FastCallDict 中的段_PyObject_FastCallDict ,我相信是因为没有构造函数。

After creating the object using CreatePythonThing, the function get_height is not set unless a workaround is applied - to call dir(thing) , which as a side effect initializes the object's properties.使用 CreatePythonThing 创建对象后,除非应用变通方法 - 调用dir(thing) ,否则不会设置函数get_height ,这作为初始化对象的属性的副作用。 The workaround is already present in CreatePythonThing .该解决方法已存在于CreatePythonThing

#include <Python.h>

typedef struct {
    PyObject_HEAD
    /* Type-specific fields go here. */
    Eval *eval;
} Thing;

static PyObject* ThingGetHeight(PyObject* self, PyObject* args)
{
    return PyLong_FromLong(1);
}

static PyMethodDef ThingMethods[] = {
    {"get_height", ThingGetHeight, METH_NOARGS, "Get thing height"},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

static PyTypeObject ThingType = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "thing.Thing",             /* tp_name */
    sizeof(Thing),             /* tp_basicsize */
    0,                         /* tp_itemsize */
    0,                         /* tp_dealloc */
    0,                         /* tp_print */
    0,                         /* tp_getattr */
    0,                         /* tp_setattr */
    0,                         /* tp_reserved */
    0,                         /* tp_repr */
    0,                         /* tp_as_number */
    0,                         /* tp_as_sequence */
    0,                         /* tp_as_mapping */
    0,                         /* tp_hash */
    0,                         /* tp_call */
    0,                         /* tp_str */
    0,                         /* tp_getattro */
    0,                         /* tp_setattro */
    0,                         /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT,        /* tp_flags */
    "Thing",                   /* tp_doc */
    0, 0, 0, 0, 0, 0,
    ThingMethods,              /* tp_methods */
};


Thing* CreatePythonThing(Eval *eval)
{
    Thing* obj = PyObject_New(Thing, &ThingType);
    obj->eval = eval;
    obj = (Thing*) PyObject_Init((PyObject*) obj, &ThingType);
    // I don't understand why, but the above does not fully initialize the object. The method
    // table is not set. Calling `dir` on the object causes it to be initialized, so this is 
    // a workaround.
    PyObject_Dir((PyObject*) obj);
    return obj;
}

As in DavidW's comment, I was missing PyType_Ready(&ThingType);就像 DavidW 的评论一样,我错过了PyType_Ready(&ThingType); in my global initializer.在我的全局初始化程序中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Python 的 C API 创建对象 - Create an object using Python's C API 如何在 Python 中使用 C API 获取对象 ID? - How to get object ID using C API in Python? what is the python C api function that returns a PyObject using the object name - what is the python C api function that returns a PyObject using the object name 使用python C API,可以缩小PyUnicode对象吗? - Using the python C API, is it possible to shrink a PyUnicode object? 使用Python使用API​​(已完全处理)提取多功能数据,并给出错误max_queue_checks - Pulling omniture data using API (fully processed) using Python giving error max_queue_checks reached 在 Python 中获取对象的完全限定类名 - Get fully qualified class name of an object in Python 如何使用纯 Python 扩展 API (python3) 包装 C++ 对象? - How to wrap a C++ object using pure Python Extension API (python3)? 如何使用 C-Python API 从 python 中定义的 class 管理 object - How to manage an object from a class defined in python using the C-Python API object 何时在 Python 构造函数表达式中初始化? - When is an object initialized in a Python constructor expression? Python变量没有用期望的对象初始化,为什么? - Python variable is not getting initialized with desired object, why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM