ia64/xen-unstable

changeset 7973:87b520c30cb2

As with xs.c, change the module interface to match the current Python/C
tutorial, simplifying the interface here, and making it more conventional.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Mon Nov 21 18:46:09 2005 +0100 (2005-11-21)
parents 52f80621b889
children 11a06dd44a8d
files tools/python/xen/lowlevel/xc/xc.c tools/python/xen/xend/XendCheckpoint.py tools/python/xen/xend/XendDmesg.py tools/python/xen/xend/XendDomain.py tools/python/xen/xend/XendDomainInfo.py tools/python/xen/xend/XendNode.py tools/python/xen/xend/image.py tools/python/xen/xend/server/SrvDaemon.py tools/python/xen/xend/server/iopif.py tools/python/xen/xend/server/pciif.py tools/python/xen/xm/create.py
line diff
     1.1 --- a/tools/python/xen/lowlevel/xc/xc.c	Mon Nov 21 18:34:25 2005 +0100
     1.2 +++ b/tools/python/xen/lowlevel/xc/xc.c	Mon Nov 21 18:46:09 2005 +0100
     1.3 @@ -23,7 +23,8 @@
     1.4  #define PyMODINIT_FUNC DL_EXPORT(void)
     1.5  #endif
     1.6  
     1.7 -#define XENPKG "xen.lowlevel.xc"
     1.8 +#define PKG "xen.lowlevel.xc"
     1.9 +#define CLS "xc"
    1.10  
    1.11  static PyObject *xc_error, *zero;
    1.12  
    1.13 @@ -1055,81 +1056,109 @@ static PyMethodDef pyxc_methods[] = {
    1.14  };
    1.15  
    1.16  
    1.17 -/*
    1.18 - * Definitions for the 'Xc' module wrapper.
    1.19 - */
    1.20 -
    1.21 -staticforward PyTypeObject PyXcType;
    1.22 -
    1.23 -static PyObject *PyXc_new(PyObject *self, PyObject *args)
    1.24 -{
    1.25 -    XcObject *xc;
    1.26 -
    1.27 -    if ( !PyArg_ParseTuple(args, ":new") )
    1.28 -        return NULL;
    1.29 -
    1.30 -    xc = PyObject_New(XcObject, &PyXcType);
    1.31 -
    1.32 -    if ( (xc->xc_handle = xc_interface_open()) == -1 )
    1.33 -    {
    1.34 -        PyObject_Del((PyObject *)xc);
    1.35 -        return PyErr_SetFromErrno(xc_error);
    1.36 -    }
    1.37 -
    1.38 -    return (PyObject *)xc;
    1.39 -}
    1.40 -
    1.41  static PyObject *PyXc_getattr(PyObject *obj, char *name)
    1.42  {
    1.43      return Py_FindMethod(pyxc_methods, obj, name);
    1.44  }
    1.45  
    1.46 -static void PyXc_dealloc(PyObject *self)
    1.47 +static PyObject *PyXc_new(PyTypeObject *type, PyObject *args)
    1.48 +{
    1.49 +    XcObject *self = (XcObject *)type->tp_alloc(type, 0);
    1.50 +
    1.51 +    if (self == NULL)
    1.52 +        return NULL;
    1.53 +
    1.54 +    self->xc_handle = NULL;
    1.55 +
    1.56 +    return (PyObject *)self;
    1.57 +}
    1.58 +
    1.59 +static int
    1.60 +PyXc_init(XcObject *self, PyObject *args, PyObject *kwds)
    1.61  {
    1.62 -    XcObject *xc = (XcObject *)self;
    1.63 -    (void)xc_interface_close(xc->xc_handle);
    1.64 -    PyObject_Del(self);
    1.65 +    if ((self->xc_handle = xc_interface_open()) == -1) {
    1.66 +        PyErr_SetFromErrno(PyExc_RuntimeError);
    1.67 +        return -1;
    1.68 +    }
    1.69 +
    1.70 +    return 0;
    1.71 +}
    1.72 +
    1.73 +static void PyXc_dealloc(XcObject *self)
    1.74 +{
    1.75 +    if (self->xc_handle) {
    1.76 +        xc_interface_close(self->xc_handle);
    1.77 +        self->xc_handle = NULL;
    1.78 +    }
    1.79 +
    1.80 +    self->ob_type->tp_free((PyObject *)self);
    1.81  }
    1.82  
    1.83  static PyTypeObject PyXcType = {
    1.84 -    PyObject_HEAD_INIT(&PyType_Type)
    1.85 +    PyObject_HEAD_INIT(NULL)
    1.86      0,
    1.87 -    "Xc",
    1.88 +    PKG "." CLS,
    1.89      sizeof(XcObject),
    1.90      0,
    1.91 -    PyXc_dealloc,    /* tp_dealloc     */
    1.92 -    NULL,            /* tp_print       */
    1.93 -    PyXc_getattr,    /* tp_getattr     */
    1.94 -    NULL,            /* tp_setattr     */
    1.95 -    NULL,            /* tp_compare     */
    1.96 -    NULL,            /* tp_repr        */
    1.97 -    NULL,            /* tp_as_number   */
    1.98 -    NULL,            /* tp_as_sequence */
    1.99 -    NULL,            /* tp_as_mapping  */
   1.100 -    NULL             /* tp_hash        */
   1.101 +    (destructor)PyXc_dealloc,     /* tp_dealloc        */
   1.102 +    NULL,                         /* tp_print          */
   1.103 +    PyXc_getattr,                 /* tp_getattr        */
   1.104 +    NULL,                         /* tp_setattr        */
   1.105 +    NULL,                         /* tp_compare        */
   1.106 +    NULL,                         /* tp_repr           */
   1.107 +    NULL,                         /* tp_as_number      */
   1.108 +    NULL,                         /* tp_as_sequence    */
   1.109 +    NULL,                         /* tp_as_mapping     */
   1.110 +    NULL,                         /* tp_hash           */
   1.111 +    NULL,                         /* tp_call           */
   1.112 +    NULL,                         /* tp_str            */
   1.113 +    NULL,                         /* tp_getattro       */
   1.114 +    NULL,                         /* tp_setattro       */
   1.115 +    NULL,                         /* tp_as_buffer      */
   1.116 +    Py_TPFLAGS_DEFAULT,           /* tp_flags          */
   1.117 +    "Xen client connections",     /* tp_doc            */
   1.118 +    NULL,                         /* tp_traverse       */
   1.119 +    NULL,                         /* tp_clear          */
   1.120 +    NULL,                         /* tp_richcompare    */
   1.121 +    0,                            /* tp_weaklistoffset */
   1.122 +    NULL,                         /* tp_iter           */
   1.123 +    NULL,                         /* tp_iternext       */
   1.124 +    pyxc_methods,                 /* tp_methods        */
   1.125 +    NULL,                         /* tp_members        */
   1.126 +    NULL,                         /* tp_getset         */
   1.127 +    NULL,                         /* tp_base           */
   1.128 +    NULL,                         /* tp_dict           */
   1.129 +    NULL,                         /* tp_descr_get      */
   1.130 +    NULL,                         /* tp_descr_set      */
   1.131 +    0,                            /* tp_dictoffset     */
   1.132 +    (initproc)PyXc_init,          /* tp_init           */
   1.133 +    NULL,                         /* tp_alloc          */
   1.134 +    PyXc_new,                     /* tp_new            */
   1.135  };
   1.136  
   1.137 -static PyMethodDef PyXc_methods[] = {
   1.138 -    { "new", PyXc_new, METH_VARARGS, "Create a new " XENPKG " object." },
   1.139 -    { NULL, NULL, 0, NULL }
   1.140 -};
   1.141 +static PyMethodDef xc_methods[] = { { NULL } };
   1.142  
   1.143  PyMODINIT_FUNC initxc(void)
   1.144  {
   1.145 -    PyObject *m, *d;
   1.146 +    PyObject *m;
   1.147  
   1.148 -    m = Py_InitModule(XENPKG, PyXc_methods);
   1.149 +    if (PyType_Ready(&PyXcType) < 0)
   1.150 +        return;
   1.151  
   1.152 -    d = PyModule_GetDict(m);
   1.153 -    xc_error = PyErr_NewException(XENPKG ".error", NULL, NULL);
   1.154 -    PyDict_SetItemString(d, "error", xc_error);
   1.155 -    PyDict_SetItemString(d, "VIRQ_DOM_EXC", PyInt_FromLong(VIRQ_DOM_EXC));
   1.156 +    m = Py_InitModule(PKG, PyXc_methods);
   1.157  
   1.158 +    if (m == NULL)
   1.159 +      return;
   1.160 +
   1.161 +    xc_error = PyErr_NewException(PKG ".error", NULL, NULL);
   1.162      zero = PyInt_FromLong(0);
   1.163  
   1.164      /* KAF: This ensures that we get debug output in a timely manner. */
   1.165      setbuf(stdout, NULL);
   1.166      setbuf(stderr, NULL);
   1.167 +
   1.168 +    Py_INCREF(&PyXcType);
   1.169 +    PyModule_AddObject(m, CLS, (PyObject *)&PyXcType);
   1.170  }
   1.171  
   1.172  
     2.1 --- a/tools/python/xen/xend/XendCheckpoint.py	Mon Nov 21 18:34:25 2005 +0100
     2.2 +++ b/tools/python/xen/xend/XendCheckpoint.py	Mon Nov 21 18:46:09 2005 +0100
     2.3 @@ -33,7 +33,7 @@ sizeof_int = calcsize("i")
     2.4  sizeof_unsigned_long = calcsize("L")
     2.5  
     2.6  
     2.7 -xc = xen.lowlevel.xc.new()
     2.8 +xc = xen.lowlevel.xc.xc()
     2.9  
    2.10  
    2.11  def write_exact(fd, buf, errmsg):
     3.1 --- a/tools/python/xen/xend/XendDmesg.py	Mon Nov 21 18:34:25 2005 +0100
     3.2 +++ b/tools/python/xen/xend/XendDmesg.py	Mon Nov 21 18:46:09 2005 +0100
     3.3 @@ -22,7 +22,7 @@ import xen.lowlevel.xc
     3.4  
     3.5  class XendDmesg:
     3.6      def __init__(self):
     3.7 -        self.xc = xen.lowlevel.xc.new()
     3.8 +        self.xc = xen.lowlevel.xc.xc()
     3.9  
    3.10      def info(self):
    3.11          return self.xc.readconsolering()
     4.1 --- a/tools/python/xen/xend/XendDomain.py	Mon Nov 21 18:34:25 2005 +0100
     4.2 +++ b/tools/python/xen/xend/XendDomain.py	Mon Nov 21 18:46:09 2005 +0100
     4.3 @@ -39,7 +39,7 @@ from xen.xend.server import relocate
     4.4  from xen.xend.xenstore.xswatch import xswatch
     4.5  
     4.6  
     4.7 -xc = xen.lowlevel.xc.new()
     4.8 +xc = xen.lowlevel.xc.xc()
     4.9  xroot = XendRoot.instance()
    4.10  
    4.11  
    4.12 @@ -201,7 +201,7 @@ class XendDomain:
    4.13                              "%d.  Destroying it in the hope of "
    4.14                              "recovery.", d)
    4.15                          try:
    4.16 -                            xc.domain_destroy(dom = d)
    4.17 +                            xc.domain_destroy(d)
    4.18                          except:
    4.19                              log.exception('Destruction of %d failed.', d)
    4.20  
    4.21 @@ -378,7 +378,7 @@ class XendDomain:
    4.22              val = dominfo.destroy()
    4.23          else:
    4.24              try:
    4.25 -                val = xc.domain_destroy(dom=domid)
    4.26 +                val = xc.domain_destroy(domid)
    4.27              except Exception, ex:
    4.28                  raise XendError(str(ex))
    4.29          return val       
     5.1 --- a/tools/python/xen/xend/XendDomainInfo.py	Mon Nov 21 18:34:25 2005 +0100
     5.2 +++ b/tools/python/xen/xend/XendDomainInfo.py	Mon Nov 21 18:46:09 2005 +0100
     5.3 @@ -93,7 +93,7 @@ MINIMUM_RESTART_TIME = 20
     5.4  RESTART_IN_PROGRESS = 'xend/restart_in_progress'
     5.5  
     5.6  
     5.7 -xc = xen.lowlevel.xc.new()
     5.8 +xc = xen.lowlevel.xc.xc()
     5.9  xroot = XendRoot.instance()
    5.10  
    5.11  log = logging.getLogger("xend.XendDomainInfo")
     6.1 --- a/tools/python/xen/xend/XendNode.py	Mon Nov 21 18:34:25 2005 +0100
     6.2 +++ b/tools/python/xen/xend/XendNode.py	Mon Nov 21 18:46:09 2005 +0100
     6.3 @@ -28,7 +28,7 @@ import xen.lowlevel.xc
     6.4  class XendNode:
     6.5  
     6.6      def __init__(self):
     6.7 -        self.xc = xen.lowlevel.xc.new()
     6.8 +        self.xc = xen.lowlevel.xc.xc()
     6.9  
    6.10      def shutdown(self):
    6.11          return 0
    6.12 @@ -40,7 +40,7 @@ class XendNode:
    6.13          return 0
    6.14      
    6.15      def cpu_bvt_slice_set(self, ctx_allow):
    6.16 -        return self.xc.bvtsched_global_set(ctx_allow=ctx_allow)
    6.17 +        return self.xc.bvtsched_global_set(ctx_allow)
    6.18  
    6.19      def cpu_bvt_slice_get(self):
    6.20          return self.xc.bvtsched_global_get()
     7.1 --- a/tools/python/xen/xend/image.py	Mon Nov 21 18:34:25 2005 +0100
     7.2 +++ b/tools/python/xen/xend/image.py	Mon Nov 21 18:46:09 2005 +0100
     7.3 @@ -27,7 +27,7 @@ from xen.xend.XendLogging import log
     7.4  from xen.xend.server.netif import randomMAC
     7.5  
     7.6  
     7.7 -xc = xen.lowlevel.xc.new()
     7.8 +xc = xen.lowlevel.xc.xc()
     7.9  
    7.10  
    7.11  MAX_GUEST_CMDLINE = 1024
     8.1 --- a/tools/python/xen/xend/server/SrvDaemon.py	Mon Nov 21 18:34:25 2005 +0100
     8.2 +++ b/tools/python/xen/xend/server/SrvDaemon.py	Mon Nov 21 18:46:09 2005 +0100
     8.3 @@ -267,7 +267,7 @@ class Daemon:
     8.4          try:
     8.5              log.info("Xend Daemon started")
     8.6  
     8.7 -            xc = xen.lowlevel.xc.new()
     8.8 +            xc = xen.lowlevel.xc.xc()
     8.9              xinfo = xc.xeninfo()
    8.10              log.info("Xend changeset: %s.", xinfo['xen_changeset'])
    8.11              del xc
     9.1 --- a/tools/python/xen/xend/server/iopif.py	Mon Nov 21 18:34:25 2005 +0100
     9.2 +++ b/tools/python/xen/xend/server/iopif.py	Mon Nov 21 18:46:09 2005 +0100
     9.3 @@ -28,7 +28,7 @@ from xen.xend.XendError import VmError
     9.4  from xen.xend.server.DevController import DevController
     9.5  
     9.6  
     9.7 -xc = xen.lowlevel.xc.new()
     9.8 +xc = xen.lowlevel.xc.xc()
     9.9  
    9.10  
    9.11  def parse_ioport(val):
    10.1 --- a/tools/python/xen/xend/server/pciif.py	Mon Nov 21 18:34:25 2005 +0100
    10.2 +++ b/tools/python/xen/xend/server/pciif.py	Mon Nov 21 18:46:09 2005 +0100
    10.3 @@ -27,7 +27,7 @@ from xen.xend.XendError import VmError
    10.4  from xen.xend.server.DevController import DevController
    10.5  
    10.6  
    10.7 -xc = xen.lowlevel.xc.new()
    10.8 +xc = xen.lowlevel.xc.xc()
    10.9  
   10.10  
   10.11  def parse_pci(val):
    11.1 --- a/tools/python/xen/xm/create.py	Mon Nov 21 18:34:25 2005 +0100
    11.2 +++ b/tools/python/xen/xm/create.py	Mon Nov 21 18:46:09 2005 +0100
    11.3 @@ -849,7 +849,7 @@ def balloon_out(dom0_min_mem, opts):
    11.4      timeout = 20 # 2s
    11.5      ret = 1
    11.6  
    11.7 -    xc = xen.lowlevel.xc.new()
    11.8 +    xc = xen.lowlevel.xc.xc()
    11.9      free_mem = xc.physinfo()['free_pages'] / 256
   11.10      domU_need_mem = opts.vals.memory + SLACK 
   11.11