Author: ArcRiley
Date: 2009-01-18 17:52:02 -0500 (Sun, 18 Jan 2009)
New Revision: 1497

Modified:
   trunk/concordance/src/sockets/Socket.c
   trunk/concordance/src/sockets/__init__.h
Log:
work on sockets.Socket


Modified: trunk/concordance/src/sockets/Socket.c
===================================================================
--- trunk/concordance/src/sockets/Socket.c      2009-01-18 16:43:04 UTC (rev 
1496)
+++ trunk/concordance/src/sockets/Socket.c      2009-01-18 22:52:02 UTC (rev 
1497)
@@ -25,6 +25,7 @@
 cdef class Socket :                                                       \*/
   static char socketsSocket_Doc[] = "Test";
 
+
   static PyObject*
   socketsSocket_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { /*\
     cdef :                                                                \*/
@@ -36,22 +37,27 @@
     /* start with closed socket */
     self->sock = -1;
 
+    /* Get a reference to our local module
+    
+       This is to keep our module alive as long as this instance.
+       
+       PyObject*        PyImport_ImportModule    (const char *name);
+    */
+    self->module = PyImport_ImportModule("concordance.sockets");
+    
     /* Get context from global state
 
-       I am so sorry for this.  Here's the natural language for this block:
-         1) get a PyObject* for the concordance.sockets module object
-         2) get the state of sockets module - a PyObject** to concordance._core
-         3) dereference the PyObject** to PyObject*
-         4) get the state of _core - a concordGlobals**
-         5) dereference concordGlobals** to concordGlobals*
-         6) copy the GMainContext* context from the struct to self->context
+       I'm sorry for this.  Here's the natural language for this block:
+         1) get the state of sockets module - a PyObject** to concordance._core
+         2) dereference the PyObject** to PyObject*
+         3) get the state of _core - a concordGlobals**
+         4) dereference concordGlobals** to concordGlobals*
+         5) copy the GMainContext* context from the struct to self->context
 
        void*            PyModule_GetState        (PyObject*);
     */
-    self->cntx = (*(concordGlobals**) PyModule_GetState(
-                  *(PyObject**) PyModule_GetState(
-                   PyImport_ImportModule("concordance.sockets"))
-                  ))->context;
+    self->context = (*(concordGlobals**) PyModule_GetState(
+                     *(PyObject**) PyModule_GetState(self->module)))->context;
 
     return (PyObject*) self;
   }
@@ -68,9 +74,82 @@
 
   static void
   socketsSocket_dealloc(socketsSocket_Object* self) {
+    /* close the socket if open */
     if (self->sock > -1)
       close(self->sock);
+
+    /* decref our module handle, which may in-turn decref _core */
+    if (self->module)
+      Py_DECREF(self->module);
+
     PyObject_Del(self);
   }
 
 
+  static PyObject*
+  socketsSocket_getattro(socketsSocket_Object* self, PyObject* key) {
+    return PyObject_GenericGetAttr((PyObject*) self, key);
+  }
+
+
+  static int
+  socketsSocket_setattro(socketsSocket_Object* self, PyObject* key,
+                         PyObject* value) {
+    return PyObject_GenericSetAttr((PyObject*) self, key, value);
+  }
+  
+  
+  /*
+  #
+  ###########################################################################
+  #
+  # global structs
+  #                                                                        */
+
+  static PyMethodDef socketsSocket_Methods[] = {
+    { NULL, NULL },                              /* sentinel */
+  };
+
+
+  PyTypeObject socketsSocket_Type = {
+    PyVarObject_HEAD_INIT(NULL, 0)
+    "concordance.sockets.Socket",                /*tp_name*/
+    sizeof(socketsSocket_Object),                /*tp_basicsize*/
+    0,                                           /*tp_itemsize*/
+    (destructor) socketsSocket_dealloc,          /*tp_dealloc*/
+    0,                                           /*tp_print*/
+    (getattrfunc) 0,                             /*tp_getattr*/
+    (setattrfunc) 0,                             /*tp_setattr*/
+    0,                                           /*tp_compare*/
+    0,                                           /*tp_repr*/
+    0,                                           /*tp_as_number*/
+    0,                                           /*tp_as_sequence*/
+    0,                                           /*tp_as_mapping*/
+    0,                                           /*tp_hash*/
+    0,                                           /*tp_call*/
+    0,                                           /*tp_str*/
+    (getattrofunc) socketsSocket_getattro,       /*tp_getattro*/
+    (setattrofunc) socketsSocket_setattro,       /*tp_setattro*/
+    0,                                           /*tp_as_buffer*/
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,    /*tp_flags*/
+    socketsSocket_Doc,                           /*tp_doc*/
+    0,                                           /*tp_traverse*/
+    0,                                           /*tp_clear*/
+    0,                                           /*tp_richcompare*/
+    0,                                           /*tp_weaklistoffset*/
+    0,                                           /*tp_iter*/
+    0,                                           /*tp_iternext*/
+    socketsSocket_Methods,                       /*tp_methods*/
+    0,                                           /*tp_members*/
+    0,                                           /*tp_getset*/
+    0,                                           /*tp_base*/
+    0,                                           /*tp_dict*/
+    0,                                           /*tp_descr_get*/
+    0,                                           /*tp_descr_set*/
+    0,                                           /*tp_dictoffset*/
+    socketsSocket_init,                          /*tp_init*/
+    0,                                           /*tp_alloc*/
+    socketsSocket_new,                           /*tp_new*/
+    0,                                           /*tp_free*/
+    0,                                           /*tp_is_gc*/
+  };

Modified: trunk/concordance/src/sockets/__init__.h
===================================================================
--- trunk/concordance/src/sockets/__init__.h    2009-01-18 16:43:04 UTC (rev 
1496)
+++ trunk/concordance/src/sockets/__init__.h    2009-01-18 22:52:02 UTC (rev 
1497)
@@ -30,7 +30,8 @@
 
 typedef struct {
   PyObject_HEAD
-  GMainContext*         cntx;          /* Concordance's Glib context */
+  PyObject*             module;        /* reference to our own module */
+  GMainContext*         context;       /* Concordance's Glib context */
   const gchar*          addr;
   gushort               port;
   int                   sock;
@@ -43,7 +44,8 @@
 
 typedef struct {
   PyObject_HEAD
-  GMainContext*         cntx;          /* Concordance's Glib context */
+  PyObject*             module;        /* reference to our own module */
+  GMainContext*         context;       /* Concordance's Glib context */
   const gchar*          addr;
   gushort               port;
   int                   sock;

_______________________________________________
PySoy-SVN mailing list
PySoy-SVN@pysoy.org
http://www.pysoy.org/mailman/listinfo/pysoy-svn

Reply via email to