summaryrefslogtreecommitdiff
path: root/cheetah/c/_template.c
blob: 234533621abd604daa4664f6301d1a4e441ba7dd (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
/*
 * Implementing a few of the functions needed for Template.py in C
 *
 * (c) 2009, R. Tyler Ballance <tyler@slide.com>
 */
#include <Python.h>

#ifdef __cplusplus
extern "C" {
#endif

static PyObject *unspecifiedModule = NULL;
static PyObject *unspecified = NULL;

static PyObject *py_valordefault(PyObject *self, PyObject *args, PyObject *kwargs)
{
    PyObject *value, *def;

    if (!PyArg_ParseTuple(args, "OO", &value, &def))
        return NULL;

    if (value == unspecified) {
        Py_XINCREF(def);
        return def;
    }
    Py_XINCREF(value);
    return value;
}

static const char _template_doc[] = "\
\n\
";
static struct PyMethodDef _template_methods[] = {
    {"valOrDefault", (PyCFunction)py_valordefault, METH_VARARGS, NULL},
    {NULL}
};

PyMODINIT_FUNC init_template()
{
    PyObject *module = Py_InitModule3("_template", _template_methods, 
            _template_doc);
    unspecifiedModule = PyImport_ImportModule("Cheetah.Unspecified");
    if ( (PyErr_Occurred()) || (!unspecifiedModule) )
        return;
    unspecified = PyObject_GetAttrString(unspecifiedModule, "Unspecified");
    if (PyErr_Occurred())
        return;
}

#ifdef __cplusplus
}
#endif