• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""framer's C code templates.
2
3Templates use the following variables:
4
5FileName: name of the file that contains the C source code
6ModuleName: name of the module, as in "import ModuleName"
7ModuleDocstring: C string containing the module doc string
8"""
9
10module_start = '#include "Python.h"'
11member_include = '#include "structmember.h"'
12
13module_doc = """\
14PyDoc_STRVAR(%(ModuleName)s_doc,
15%(ModuleDocstring)s);
16"""
17
18methoddef_start = """\
19static struct PyMethodDef %(MethodDefName)s[] = {"""
20
21methoddef_def = """\
22        {"%(PythonName)s", (PyCFunction)%(CName)s, %(MethType)s},"""
23
24methoddef_def_doc = """\
25        {"%(PythonName)s", (PyCFunction)%(CName)s, %(MethType)s,
26         %(DocstringVar)s},"""
27
28methoddef_end = """\
29        {NULL, NULL}
30};
31"""
32
33memberdef_start = """\
34#define OFF(X) offsetof(%(StructName)s, X)
35
36static struct PyMemberDef %(MemberDefName)s[] = {"""
37
38memberdef_def_doc = """\
39        {"%(PythonName)s", %(Type)s, OFF(%(CName)s), %(Flags)s,
40         %(Docstring)s},"""
41
42memberdef_def = """\
43        {"%(PythonName)s", %(Type)s, OFF(%(CName)s), %(Flags)s},"""
44
45memberdef_end = """\
46        {NULL}
47};
48
49#undef OFF
50"""
51
52dealloc_func = """static void
53%(name)s(PyObject *ob)
54{
55}
56"""
57
58docstring = """\
59PyDoc_STRVAR(%(DocstringVar)s,
60%(Docstring)s);
61"""
62
63funcdef_start = """\
64static PyObject *
65%(name)s(%(args)s)
66{"""
67
68funcdef_end = """\
69}
70"""
71
72varargs = """\
73        if (!PyArg_ParseTuple(args, \"%(ArgParse)s:%(PythonName)s\",
74                              %(ArgTargets)s))
75                return NULL;"""
76
77module_init_start = """\
78PyMODINIT_FUNC
79init%(ModuleName)s(void)
80{
81        PyObject *mod;
82
83        mod = Py_InitModule3("%(ModuleName)s", %(MethodDefName)s,
84                             %(ModuleName)s_doc);
85        if (mod == NULL)
86                return;
87"""
88
89type_init_type = "        %(CTypeName)s.ob_type = &PyType_Type;"
90module_add_type = """\
91        if (!PyObject_SetAttrString(mod, "%(TypeName)s",
92                                    (PyObject *)&%(CTypeName)s))
93                return;
94"""
95
96type_struct_start = """\
97static PyTypeObject %(CTypeName)s = {
98        PyObject_HEAD_INIT(0)"""
99
100type_struct_end = """\
101};
102"""
103