summaryrefslogtreecommitdiff
path: root/Tools/framer/framer/template.py
blob: 41f95371b85b0d7d003ab3bd64892d2aa046dac7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""framer's C code templates.

Templates use the following variables:

FileName: name of the file that contains the C source code
ModuleName: name of the module, as in "import ModuleName"
ModuleDocstring: C string containing the module doc string
"""

module_start = '#include "Python.h"'
member_include = '#include "structmember.h"'

module_doc = """\
PyDoc_STRVAR(%(ModuleName)s_doc,
%(ModuleDocstring)s);
"""

methoddef_start = """\
static struct PyMethodDef %(MethodDefName)s[] = {"""

methoddef_def = """\
        {"%(PythonName)s", (PyCFunction)%(CName)s, %(MethType)s},"""

methoddef_def_doc = """\
        {"%(PythonName)s", (PyCFunction)%(CName)s, %(MethType)s,
         %(DocstringVar)s},"""

methoddef_end = """\
        {NULL, NULL}
};
"""

memberdef_start = """\
#define OFF(X) offsetof(%(StructName)s, X)

static struct PyMemberDef %(MemberDefName)s[] = {"""

memberdef_def_doc = """\
        {"%(PythonName)s", %(Type)s, OFF(%(CName)s), %(Flags)s,
         %(Docstring)s},"""

memberdef_def = """\
        {"%(PythonName)s", %(Type)s, OFF(%(CName)s), %(Flags)s},"""

memberdef_end = """\
        {NULL}
};

#undef OFF
"""

dealloc_func = """static void
%(name)s(PyObject *ob)
{
}
"""

docstring = """\
PyDoc_STRVAR(%(DocstringVar)s,
%(Docstring)s);
"""

funcdef_start = """\
static PyObject *
%(name)s(%(args)s)
{"""

funcdef_end = """\
}
"""

varargs = """\
        if (!PyArg_ParseTuple(args, \"%(ArgParse)s:%(PythonName)s\",
                              %(ArgTargets)s))
                return NULL;"""

module_init_start = """\
PyMODINIT_FUNC
init%(ModuleName)s(void)
{
        PyObject *mod;

        mod = Py_InitModule3("%(ModuleName)s", %(MethodDefName)s,
                             %(ModuleName)s_doc);
        if (mod == NULL)
                return;
"""

type_init_type = "        %(CTypeName)s.ob_type = &PyType_Type;"
module_add_type = """\
        if (!PyObject_SetAttrString(mod, "%(TypeName)s",
                                    (PyObject *)&%(CTypeName)s))
                return;
"""

type_struct_start = """\
static PyTypeObject %(CTypeName)s = {
        PyObject_HEAD_INIT(0)"""

type_struct_end = """\
};
"""