ia64/xen-unstable

changeset 7940:2cfded7de82f

Change the module registration declarations and change the object creation and
deallocation to match those in the Python/C tutorial. This has been done to
make the interface here more conventional, in the hunt for bug #411. One
double deallocation has been fixed, but this would only occur if we failed to
create the connection to Xenstore, and so unfortunately is not the cause of
that bug.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Mon Nov 21 12:40:02 2005 +0100 (2005-11-21)
parents e60115657823
children 5b0a7cac1087
files tools/python/xen/lowlevel/xs/xs.c tools/python/xen/xend/xenstore/xsutil.py
line diff
     1.1 --- a/tools/python/xen/lowlevel/xs/xs.c	Mon Nov 21 12:34:37 2005 +0100
     1.2 +++ b/tools/python/xen/lowlevel/xs/xs.c	Mon Nov 21 12:40:02 2005 +0100
     1.3 @@ -789,84 +789,119 @@ static PyObject *xshandle_getattr(PyObje
     1.4      return Py_FindMethod(xshandle_methods, self, name);
     1.5  }
     1.6  
     1.7 -static void xshandle_dealloc(PyObject *self)
     1.8 +static PyObject *
     1.9 +xshandle_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
    1.10  {
    1.11 -    XsHandle *xh = (XsHandle*)self;
    1.12 -    if (xh->xh) {
    1.13 -        xs_daemon_close(xh->xh);
    1.14 -        xh->xh = NULL;
    1.15 -    }
    1.16 -    PyObject_Del(self);
    1.17 +    XsHandle *self = (XsHandle *)type->tp_alloc(type, 0);
    1.18 +
    1.19 +    if (self == NULL)
    1.20 +        return NULL;
    1.21 +
    1.22 +    self->xh = NULL;
    1.23 +    self->watches = PyList_New(0);
    1.24 +    if (!self->watches)
    1.25 +        goto fail;
    1.26 +
    1.27 +    return (PyObject *)self;
    1.28 +fail:
    1.29 +    /* Decreasing the object's reference to 0 will result in xshandle_dealloc
    1.30 +       being called. */
    1.31 +    Py_DECREF(self);
    1.32 +    return NULL;
    1.33  }
    1.34  
    1.35 -static PyTypeObject xshandle_type = {
    1.36 -    PyObject_HEAD_INIT(&PyType_Type)
    1.37 -    0,
    1.38 -    "xshandle",
    1.39 -    sizeof(XsHandle),
    1.40 -    0,
    1.41 -    xshandle_dealloc,   /* tp_dealloc     */
    1.42 -    NULL,               /* tp_print       */
    1.43 -    xshandle_getattr,   /* tp_getattr     */
    1.44 -    NULL,               /* tp_setattr     */
    1.45 -    NULL,               /* tp_compare     */
    1.46 -    NULL,               /* tp_repr        */
    1.47 -    NULL,               /* tp_as_number   */
    1.48 -    NULL,               /* tp_as_sequence */
    1.49 -    NULL,               /* tp_as_mapping  */
    1.50 -    NULL                /* tp_hash        */
    1.51 -};
    1.52 -
    1.53 -static PyObject *xshandle_open(PyObject *self, PyObject *args, PyObject *kwds)
    1.54 +static int
    1.55 +xshandle_init(XsHandle *self, PyObject *args, PyObject *kwds)
    1.56  {
    1.57      static char *kwd_spec[] = { "readonly", NULL };
    1.58      static char *arg_spec = "|i";
    1.59 -    int readonly = 0;
    1.60 -
    1.61 -    XsHandle *xsh = NULL;
    1.62 -    PyObject *val = NULL;
    1.63 +    int readonly;
    1.64  
    1.65      if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec,
    1.66                                       &readonly))
    1.67 -        return NULL;
    1.68 +        goto fail;
    1.69 +
    1.70 +    self->xh = (readonly ? xs_daemon_open_readonly() : xs_daemon_open());
    1.71 +    if (!self->xh)
    1.72 +        goto fail;
    1.73 +
    1.74 +    return 0;
    1.75  
    1.76 -    xsh = PyObject_New(XsHandle, &xshandle_type);
    1.77 -    if (!xsh)
    1.78 -        return NULL;
    1.79 -    xsh->watches = PyList_New(0);
    1.80 -    if (!xsh->watches)
    1.81 -        goto exit;
    1.82 -    xsh->xh = (readonly ? xs_daemon_open_readonly() : xs_daemon_open());
    1.83 -    if (!xsh->xh) {
    1.84 -        Py_DECREF(xsh->watches);
    1.85 -        PyErr_SetFromErrno(PyExc_RuntimeError);
    1.86 -        goto exit;
    1.87 + fail:
    1.88 +    PyErr_SetFromErrno(PyExc_RuntimeError);
    1.89 +    return -1;
    1.90 +}
    1.91 +
    1.92 +static void xshandle_dealloc(XsHandle *self)
    1.93 +{
    1.94 +    if (self->xh) {
    1.95 +        xs_daemon_close(self->xh);
    1.96 +        self->xh = NULL;
    1.97      }
    1.98 -    val = (PyObject *)xsh;
    1.99 -    return val;
   1.100 - exit:
   1.101 -    PyObject_Del(xsh);
   1.102 -    return NULL;
   1.103 +
   1.104 +    Py_XDECREF(self->watches);
   1.105 +
   1.106 +    self->ob_type->tp_free((PyObject *)self);
   1.107  }
   1.108  
   1.109 -static PyMethodDef xs_methods[] = {
   1.110 -    { .ml_name  = "open",
   1.111 -      .ml_meth  = (PyCFunction)xshandle_open,
   1.112 -      .ml_flags = (METH_VARARGS | METH_KEYWORDS), 
   1.113 -      .ml_doc   = "\n"
   1.114 -      "Open a connection to the xenstore daemon.\n"
   1.115 -      "Returns: xs connection object.\n"
   1.116 -      "Raises RuntimeError on error.\n"
   1.117 -      "\n"
   1.118 -    },
   1.119 -    { /* Terminator. */ }
   1.120 +static PyTypeObject xshandle_type = {
   1.121 +    PyObject_HEAD_INIT(NULL)
   1.122 +    0,
   1.123 +    "xen.lowlevel.xs.xs",
   1.124 +    sizeof(XsHandle),
   1.125 +    0,
   1.126 +    (destructor)xshandle_dealloc, /* tp_dealloc        */
   1.127 +    NULL,                         /* tp_print          */
   1.128 +    xshandle_getattr,             /* tp_getattr        */
   1.129 +    NULL,                         /* tp_setattr        */
   1.130 +    NULL,                         /* tp_compare        */
   1.131 +    NULL,                         /* tp_repr           */
   1.132 +    NULL,                         /* tp_as_number      */
   1.133 +    NULL,                         /* tp_as_sequence    */
   1.134 +    NULL,                         /* tp_as_mapping     */
   1.135 +    NULL,                         /* tp_hash           */
   1.136 +    NULL,                         /* tp_call           */
   1.137 +    NULL,                         /* tp_str            */
   1.138 +    NULL,                         /* tp_getattro       */
   1.139 +    NULL,                         /* tp_setattro       */
   1.140 +    NULL,                         /* tp_as_buffer      */
   1.141 +    Py_TPFLAGS_DEFAULT,           /* tp_flags          */
   1.142 +    "Xenstore connections",       /* tp_doc            */
   1.143 +    NULL,                         /* tp_traverse       */
   1.144 +    NULL,                         /* tp_clear          */
   1.145 +    NULL,                         /* tp_richcompare    */
   1.146 +    0,                            /* tp_weaklistoffset */
   1.147 +    NULL,                         /* tp_iter           */
   1.148 +    NULL,                         /* tp_iternext       */
   1.149 +    xshandle_methods,             /* tp_methods        */
   1.150 +    NULL,                         /* tp_members        */
   1.151 +    NULL,                         /* tp_getset         */
   1.152 +    NULL,                         /* tp_base           */
   1.153 +    NULL,                         /* tp_dict           */
   1.154 +    NULL,                         /* tp_descr_get      */
   1.155 +    NULL,                         /* tp_descr_set      */
   1.156 +    0,                            /* tp_dictoffset     */
   1.157 +    (initproc)xshandle_init,      /* tp_init           */
   1.158 +    NULL,                         /* tp_alloc          */
   1.159 +    xshandle_new,                 /* tp_new            */
   1.160  };
   1.161  
   1.162 -PyMODINIT_FUNC initxs (void)
   1.163 +static PyMethodDef xs_methods[] = { { NULL } };
   1.164 +
   1.165 +PyMODINIT_FUNC initxs(void)
   1.166  {
   1.167 -    PyObject *module;
   1.168 +    PyObject* m;
   1.169 +
   1.170 +    if (PyType_Ready(&xshandle_type) < 0)
   1.171 +        return;
   1.172  
   1.173 -    module = Py_InitModule(PYPKG, xs_methods);
   1.174 +    m = Py_InitModule("xen.lowlevel.xs", xs_methods);
   1.175 +
   1.176 +    if (m == NULL)
   1.177 +      return;
   1.178 +
   1.179 +    Py_INCREF(&xshandle_type);
   1.180 +    PyModule_AddObject(m, "xs", (PyObject *)&xshandle_type);
   1.181  }
   1.182  
   1.183  
     2.1 --- a/tools/python/xen/xend/xenstore/xsutil.py	Mon Nov 21 12:34:37 2005 +0100
     2.2 +++ b/tools/python/xen/xend/xenstore/xsutil.py	Mon Nov 21 12:40:02 2005 +0100
     2.3 @@ -5,7 +5,7 @@
     2.4  # this archive for more details.
     2.5  
     2.6  import threading
     2.7 -from xen.lowlevel import xs
     2.8 +import xen.lowlevel.xs
     2.9  
    2.10  xs_lock = threading.Lock()
    2.11  xs_handle = None
    2.12 @@ -15,7 +15,7 @@ def xshandle():
    2.13      if not xs_handle:
    2.14          xs_lock.acquire()
    2.15          if not xs_handle:
    2.16 -            xs_handle = xs.open()
    2.17 +            xs_handle = xen.lowlevel.xs.xs()
    2.18          xs_lock.release()
    2.19      return xs_handle
    2.20