home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / pythonwin / python.exe / PYTHON23.CHM / ext / noddy4.txt < prev    next >
Encoding:
Text File  |  2003-10-02  |  5.2 KB  |  210 lines

  1. #include <Python.h>
  2. #include "structmember.h"
  3.  
  4. typedef struct {
  5.     PyObject_HEAD
  6.     PyObject *first;
  7.     PyObject *last;
  8.     int number;
  9. } Noddy;
  10.  
  11. static int
  12. Noddy_traverse(Noddy *self, visitproc visit, void *arg)
  13. {
  14.     if (self->first && visit(self->first, arg) < 0)
  15.         return -1;
  16.     if (self->last && visit(self->last, arg) < 0)
  17.         return -1;
  18.  
  19.     return 0;
  20. }
  21.  
  22. static int 
  23. Noddy_clear(Noddy *self)
  24. {
  25.     Py_XDECREF(self->first);
  26.     self->first = NULL;
  27.     Py_XDECREF(self->last);
  28.     self->last = NULL;
  29.  
  30.     return 0;
  31. }
  32.  
  33. static void
  34. Noddy_dealloc(Noddy* self)
  35. {
  36.     Noddy_clear(self);
  37.     self->ob_type->tp_free((PyObject*)self);
  38. }
  39.  
  40. static PyObject *
  41. Noddy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  42. {
  43.     Noddy *self;
  44.  
  45.     self = (Noddy *)type->tp_alloc(type, 0);
  46.     if (self != NULL) {
  47.         self->first = PyString_FromString("");
  48.         if (self->first == NULL)
  49.           {
  50.             Py_DECREF(self);
  51.             return NULL;
  52.           }
  53.         
  54.         self->last = PyString_FromString("");
  55.         if (self->last == NULL)
  56.           {
  57.             Py_DECREF(self);
  58.             return NULL;
  59.           }
  60.  
  61.         self->number = 0;
  62.     }
  63.  
  64.     return (PyObject *)self;
  65. }
  66.  
  67. static int
  68. Noddy_init(Noddy *self, PyObject *args, PyObject *kwds)
  69. {
  70.     PyObject *first=NULL, *last=NULL;
  71.  
  72.     static char *kwlist[] = {"first", "last", "number", NULL};
  73.  
  74.     if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OOi", kwlist, 
  75.                                       &first, &last, 
  76.                                       &self->number))
  77.         return -1; 
  78.  
  79.     if (first) {
  80.         Py_XDECREF(self->first);
  81.         Py_INCREF(first);
  82.         self->first = first;
  83.     }
  84.  
  85.     if (last) {
  86.         Py_XDECREF(self->last);
  87.         Py_INCREF(last);
  88.         self->last = last;
  89.     }
  90.  
  91.     return 0;
  92. }
  93.  
  94.  
  95. static PyMemberDef Noddy_members[] = {
  96.     {"first", T_OBJECT_EX, offsetof(Noddy, first), 0,
  97.      "first name"},
  98.     {"last", T_OBJECT_EX, offsetof(Noddy, last), 0,
  99.      "last name"},
  100.     {"number", T_INT, offsetof(Noddy, number), 0,
  101.      "noddy number"},
  102.     {NULL}  /* Sentinel */
  103. };
  104.  
  105. static PyObject *
  106. Noddy_name(Noddy* self)
  107. {
  108.     static PyObject *format = NULL;
  109.     PyObject *args, *result;
  110.  
  111.     if (format == NULL) {
  112.         format = PyString_FromString("%s %s");
  113.         if (format == NULL)
  114.             return NULL;
  115.     }
  116.  
  117.     if (self->first == NULL) {
  118.         PyErr_SetString(PyExc_AttributeError, "first");
  119.         return NULL;
  120.     }
  121.  
  122.     if (self->last == NULL) {
  123.         PyErr_SetString(PyExc_AttributeError, "last");
  124.         return NULL;
  125.     }
  126.  
  127.     args = Py_BuildValue("OO", self->first, self->last);
  128.     if (args == NULL)
  129.         return NULL;
  130.  
  131.     result = PyString_Format(format, args);
  132.     Py_DECREF(args);
  133.     
  134.     return result;
  135. }
  136.  
  137. static PyMethodDef Noddy_methods[] = {
  138.     {"name", (PyCFunction)Noddy_name, METH_NOARGS,
  139.      "Return the name, combining the first and last name"
  140.     },
  141.     {NULL}  /* Sentinel */
  142. };
  143.  
  144. static PyTypeObject NoddyType = {
  145.     PyObject_HEAD_INIT(NULL)
  146.     0,                         /*ob_size*/
  147.     "noddy.Noddy",             /*tp_name*/
  148.     sizeof(Noddy),             /*tp_basicsize*/
  149.     0,                         /*tp_itemsize*/
  150.     (destructor)Noddy_dealloc, /*tp_dealloc*/
  151.     0,                         /*tp_print*/
  152.     0,                         /*tp_getattr*/
  153.     0,                         /*tp_setattr*/
  154.     0,                         /*tp_compare*/
  155.     0,                         /*tp_repr*/
  156.     0,                         /*tp_as_number*/
  157.     0,                         /*tp_as_sequence*/
  158.     0,                         /*tp_as_mapping*/
  159.     0,                         /*tp_hash */
  160.     0,                         /*tp_call*/
  161.     0,                         /*tp_str*/
  162.     0,                         /*tp_getattro*/
  163.     0,                         /*tp_setattro*/
  164.     0,                         /*tp_as_buffer*/
  165.     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
  166.     "Noddy objects",           /* tp_doc */
  167.     (traverseproc)Noddy_traverse,   /* tp_traverse */
  168.     (inquiry)Noddy_clear,           /* tp_clear */
  169.     0,                       /* tp_richcompare */
  170.     0,                       /* tp_weaklistoffset */
  171.     0,                       /* tp_iter */
  172.     0,                       /* tp_iternext */
  173.     Noddy_methods,             /* tp_methods */
  174.     Noddy_members,             /* tp_members */
  175.     0,                         /* tp_getset */
  176.     0,                         /* tp_base */
  177.     0,                         /* tp_dict */
  178.     0,                         /* tp_descr_get */
  179.     0,                         /* tp_descr_set */
  180.     0,                         /* tp_dictoffset */
  181.     (initproc)Noddy_init,      /* tp_init */
  182.     0,                         /* tp_alloc */
  183.     Noddy_new,                 /* tp_new */
  184. };
  185.  
  186. static PyMethodDef module_methods[] = {
  187.     {NULL}  /* Sentinel */
  188. };
  189.  
  190. #ifndef PyMODINIT_FUNC    /* declarations for DLL import/export */
  191. #define PyMODINIT_FUNC void
  192. #endif
  193. PyMODINIT_FUNC
  194. initnoddy4(void) 
  195. {
  196.     PyObject* m;
  197.  
  198.     if (PyType_Ready(&NoddyType) < 0)
  199.         return;
  200.  
  201.     m = Py_InitModule3("noddy4", module_methods,
  202.                        "Example module that creates an extension type.");
  203.  
  204.     if (m == NULL)
  205.       return;
  206.  
  207.     Py_INCREF(&NoddyType);
  208.     PyModule_AddObject(m, "Noddy", (PyObject *)&NoddyType);
  209. }
  210.