Python / C API:创建" __ all __"对象

时间:2015-11-22 02:09:51

标签: python c api python-3.x

使用普通Python3制作Python模块/库时,我可以轻松创建__all__列表。然后,我可以通过" MODULE。__all__"导入模块后。

然而,当通过Python / C API创建模块时,我无法找到一个简单的或者#34;正确的"制作__all__的方法。我查看了官方文档但未找到答案。

在使用Python / C API创建的模块中是否有简单的方法或标准方法来创建__all__

我能够使用下面的代码创建类似的对象(例如__author__)。

static const char __author__[32] = "Devyn Collier Johnson";
static const char __copyright__[8] = "LGPLv3";
static const char __version__[16] = "2015.11.21";
PyModule_AddStringConstant(m, "__author__", __author__);
PyModule_AddStringConstant(m, "__copyright__", __copyright__);
PyModule_AddStringConstant(m, "__version__", __version__);

下面是我的特定模块的代码(我删除了函数和大块注释以便于阅读)

#include <Python.h>

#include "geometry.h"


static const char __author__[32] = "Devyn Collier Johnson";

static const char __copyright__[8] = "LGPLv3";

static const char __version__[16] = "2015.11.21";

static const char __all__[16][32] = {
    // Distance
    "distance",
    "linelength",
    // Area
    "areacircle",
    "areasquare",
    "areasquare_int",
    "areasquare_shape"
};


// Docstrings

static char module_docstring[32] =
    "Geometry Equations";
PyDoc_STRVAR(geometry_distance_docstring,
    "distance(location1: tuple, location2: tuple) -> float\nReturn the distance between two points");
PyDoc_STRVAR(geometry_linelength_docstring,
    "linelength(location1: tuple, location2: tuple) -> float\nReturn the length of a line (given two points as tuples with two floats each)");
PyDoc_STRVAR(geometry_areacircle_docstring,
    "areacircle(radius: float) -> float\nReturn the area of a circle (float)");
PyDoc_STRVAR(geometry_areasquare_docstring,
    "areasquare(length: float) -> float\nReturn the area of a square (float)");
PyDoc_STRVAR(geometry_areasquare_int_docstring,
    "areasquare_int(length: int) -> int\nReturn the area of a square (int)");
PyDoc_STRVAR(geometry_areasquare_shape_docstring,
    "areasquare_shape(square: float) -> float\nReturn the area of a square (float)");


// Function Definitions

static PyObject *geometry_distance(PyObject *self, PyObject *args);
static PyObject *geometry_areacircle(PyObject *self, PyObject *args);
static PyObject *geometry_areasquare(PyObject *self, PyObject *args);
static PyObject *geometry_areasquare_int(PyObject *self, PyObject *args);
static PyObject *geometry_areasquare_shape(PyObject *self, PyObject *args);


// Module Specification

static PyMethodDef module_methods[] = {
    {"distance", (PyCFunction) geometry_distance, METH_VARARGS, geometry_distance_docstring},
    {"linelength", (PyCFunction) geometry_distance, METH_VARARGS, geometry_linelength_docstring},
    {"areacircle", (PyCFunction) geometry_areacircle, METH_VARARGS, geometry_areacircle_docstring},
    {"areasquare", (PyCFunction) geometry_areasquare, METH_VARARGS, geometry_areasquare_docstring},
    {"areasquare_int", (PyCFunction) geometry_areasquare_int, METH_VARARGS, geometry_areasquare_int_docstring},
    {"areasquare_shape", (PyCFunction) geometry_areasquare_shape, METH_VARARGS, geometry_areasquare_shape_docstring},
    {NULL, NULL, 0, NULL}
};


static struct PyModuleDef module = {
    PyModuleDef_HEAD_INIT,
    "geometry",
    module_docstring,
    -1,
    module_methods
};


PyMODINIT_FUNC PyInit_geometry(void) {
    PyObject *m;
    m = PyModule_Create(&module);
    PyModule_AddStringConstant(m, "__author__", __author__);
    PyModule_AddStringConstant(m, "__copyright__", __copyright__);
    PyModule_AddStringConstant(m, "__version__", __version__);
    if (m == NULL)
        return NULL;
    return m;
}

0 个答案:

没有答案