/Tools/framer/framer/template.py

http://unladen-swallow.googlecode.com/ · Python · 102 lines · 80 code · 20 blank · 2 comment · 0 complexity · 9e1f249c16afcf279bc38b0a72a30b1e MD5 · raw file

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