diff options
author | R. Tyler Ballance <tyler@slide.com> | 2009-06-28 10:15:39 +0800 |
---|---|---|
committer | James Abbatiello <abbeyj@gmail.com> | 2009-06-28 12:17:41 +0800 |
commit | d4fc5f9aa2540997da7c767a1e525c4bf3fe499a (patch) | |
tree | 336b2661a0f5caaedac434e37a4417bbfe55707d | |
parent | a8539369bb2a55f6c9ac38ef50f99ba8d0ae1660 (diff) | |
download | python-cheetah-d4fc5f9aa2540997da7c767a1e525c4bf3fe499a.tar.gz |
_template.c should grow to include minor utility methods that benefit from moving into C
Signed-off-by: James Abbatiello <abbeyj@gmail.com>
-rw-r--r-- | src/c/_template.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/c/_template.c b/src/c/_template.c new file mode 100644 index 0000000..146871c --- /dev/null +++ b/src/c/_template.c @@ -0,0 +1,49 @@ +/* + * 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, *res; + + if (!PyArg_ParseTuple(args, "OO", &value, &def)) + return NULL; + + if (value == unspecified) + return def; + return value; +} + +static const char _template_doc[] = "\ +\n\ +"; +static struct PyMethodDef _template_methods[] = { + {"valOrDefault", 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 NULL; + unspecified = PyObject_GetAttrString(unspecifiedModule, "Unspecified"); + if (PyErr_Occurred()) + return NULL; +} + +#ifdef __cplusplus +} +#endif |