home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / pythonwin / python.exe / PYTHON23.CHM / ext / noddy3.txt < prev    next >
Encoding:
Text File  |  2003-10-02  |  5.8 KB  |  242 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 void
  12. Noddy_dealloc(Noddy* self)
  13. {
  14.     Py_XDECREF(self->first);
  15.     Py_XDECREF(self->last);
  16.     self->ob_type->tp_free((PyObject*)self);
  17. }
  18.  
  19. static PyObject *
  20. Noddy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  21. {
  22.     Noddy *self;
  23.  
  24.     self = (Noddy *)type->tp_alloc(type, 0);
  25.     if (self != NULL) {
  26.         self->first = PyString_FromString("");
  27.         if (self->first == NULL)
  28.           {
  29.             Py_DECREF(self);
  30.             return NULL;
  31.           }
  32.         
  33.         self->last = PyString_FromString("");
  34.         if (self->last == NULL)
  35.           {
  36.             Py_DECREF(self);
  37.             return NULL;
  38.           }
  39.  
  40.         self->number = 0;
  41.     }
  42.  
  43.     return (PyObject *)self;
  44. }
  45.  
  46. static int
  47. Noddy_init(Noddy *self, PyObject *args, PyObject *kwds)
  48. {
  49.     PyObject *first=NULL, *last=NULL;
  50.  
  51.     static char *kwlist[] = {"first", "last", "number", NULL};
  52.  
  53.     if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OOi", kwlist, 
  54.                                       &first, &last, 
  55.                                       &self->number))
  56.         return -1; 
  57.  
  58.     if (first) {
  59.         Py_DECREF(self->first);
  60.         Py_INCREF(first);
  61.         self->first = first;
  62.     }
  63.  
  64.     if (last) {
  65.         Py_DECREF(self->last);
  66.         Py_INCREF(last);
  67.         self->last = last;
  68.     }
  69.  
  70.     return 0;
  71. }
  72.  
  73. static PyMemberDef Noddy_members[] = {
  74.     {"number", T_INT, offsetof(Noddy, number), 0,
  75.      "noddy number"},
  76.     {NULL}  /* Sentinel */
  77. };
  78.  
  79. static PyObject *
  80. Noddy_getfirst(Noddy *self, void *closure)
  81. {
  82.     Py_INCREF(self->first);
  83.     return self->first;
  84. }
  85.  
  86. static int
  87. Noddy_setfirst(Noddy *self, PyObject *value, void *closure)
  88. {
  89.   if (value == NULL) {
  90.     PyErr_SetString(PyExc_TypeError, "Cannot delete the first attribute");
  91.     return -1;
  92.   }
  93.   
  94.   if (! PyString_Check(value)) {
  95.     PyErr_SetString(PyExc_TypeError, 
  96.                     "The first attribute value must be a string");
  97.     return -1;
  98.   }
  99.       
  100.   Py_DECREF(self->first);
  101.   Py_INCREF(value);
  102.   self->first = value;    
  103.  
  104.   return 0;
  105. }
  106.  
  107. static PyObject *
  108. Noddy_getlast(Noddy *self, void *closure)
  109. {
  110.     Py_INCREF(self->last);
  111.     return self->last;
  112. }
  113.  
  114. static int
  115. Noddy_setlast(Noddy *self, PyObject *value, void *closure)
  116. {
  117.   if (value == NULL) {
  118.     PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute");
  119.     return -1;
  120.   }
  121.   
  122.   if (! PyString_Check(value)) {
  123.     PyErr_SetString(PyExc_TypeError, 
  124.                     "The last attribute value must be a string");
  125.     return -1;
  126.   }
  127.       
  128.   Py_DECREF(self->last);
  129.   Py_INCREF(value);
  130.   self->last = value;    
  131.  
  132.   return 0;
  133. }
  134.  
  135. static PyGetSetDef Noddy_getseters[] = {
  136.     {"first", 
  137.      (getter)Noddy_getfirst, (setter)Noddy_setfirst,
  138.      "first name",
  139.      NULL},
  140.     {"last", 
  141.      (getter)Noddy_getlast, (setter)Noddy_setlast,
  142.      "last name",
  143.      NULL},
  144.     {NULL}  /* Sentinel */
  145. };
  146.  
  147. static PyObject *
  148. Noddy_name(Noddy* self)
  149. {
  150.     static PyObject *format = NULL;
  151.     PyObject *args, *result;
  152.  
  153.     if (format == NULL) {
  154.         format = PyString_FromString("%s %s");
  155.         if (format == NULL)
  156.             return NULL;
  157.     }
  158.  
  159.     args = Py_BuildValue("OO", self->first, self->last);
  160.     if (args == NULL)
  161.         return NULL;
  162.  
  163.     result = PyString_Format(format, args);
  164.     Py_DECREF(args);
  165.     
  166.     return result;
  167. }
  168.  
  169. static PyMethodDef Noddy_methods[] = {
  170.     {"name", (PyCFunction)Noddy_name, METH_NOARGS,
  171.      "Return the name, combining the first and last name"
  172.     },
  173.     {NULL}  /* Sentinel */
  174. };
  175.  
  176. static PyTypeObject NoddyType = {
  177.     PyObject_HEAD_INIT(NULL)
  178.     0,                         /*ob_size*/
  179.     "noddy.Noddy",             /*tp_name*/
  180.     sizeof(Noddy),             /*tp_basicsize*/
  181.     0,                         /*tp_itemsize*/
  182.     (destructor)Noddy_dealloc, /*tp_dealloc*/
  183.     0,                         /*tp_print*/
  184.     0,                         /*tp_getattr*/
  185.     0,                         /*tp_setattr*/
  186.     0,                         /*tp_compare*/
  187.     0,                         /*tp_repr*/
  188.     0,                         /*tp_as_number*/
  189.     0,                         /*tp_as_sequence*/
  190.     0,                         /*tp_as_mapping*/
  191.     0,                         /*tp_hash */
  192.     0,                         /*tp_call*/
  193.     0,                         /*tp_str*/
  194.     0,                         /*tp_getattro*/
  195.     0,                         /*tp_setattro*/
  196.     0,                         /*tp_as_buffer*/
  197.     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
  198.     "Noddy objects",           /* tp_doc */
  199.     0,                       /* tp_traverse */
  200.     0,                       /* tp_clear */
  201.     0,                       /* tp_richcompare */
  202.     0,                       /* tp_weaklistoffset */
  203.     0,                       /* tp_iter */
  204.     0,                       /* tp_iternext */
  205.     Noddy_methods,             /* tp_methods */
  206.     Noddy_members,             /* tp_members */
  207.     Noddy_getseters,           /* tp_getset */
  208.     0,                         /* tp_base */
  209.     0,                         /* tp_dict */
  210.     0,                         /* tp_descr_get */
  211.     0,                         /* tp_descr_set */
  212.     0,                         /* tp_dictoffset */
  213.     (initproc)Noddy_init,      /* tp_init */
  214.     0,                         /* tp_alloc */
  215.     Noddy_new,                 /* tp_new */
  216. };
  217.  
  218. static PyMethodDef module_methods[] = {
  219.     {NULL}  /* Sentinel */
  220. };
  221.  
  222. #ifndef PyMODINIT_FUNC    /* declarations for DLL import/export */
  223. #define PyMODINIT_FUNC void
  224. #endif
  225. PyMODINIT_FUNC
  226. initnoddy3(void) 
  227. {
  228.     PyObject* m;
  229.  
  230.     if (PyType_Ready(&NoddyType) < 0)
  231.         return;
  232.  
  233.     m = Py_InitModule3("noddy3", module_methods,
  234.                        "Example module that creates an extension type.");
  235.  
  236.     if (m == NULL)
  237.       return;
  238.  
  239.     Py_INCREF(&NoddyType);
  240.     PyModule_AddObject(m, "Noddy", (PyObject *)&NoddyType);
  241. }
  242.