ia64/xen-unstable

changeset 10320:da81a493c8e5

Expose the exception thrown by xen.lowlevel.xs.
author Anthony Liguori <anthony@codemonkey.ws>
date Thu Jun 01 12:13:11 2006 +0100 (2006-06-01)
parents 647d012b9a36
children deabbf60bb46
files tools/python/xen/lowlevel/xs/xs.c
line diff
     1.1 --- a/tools/python/xen/lowlevel/xs/xs.c	Thu Jun 01 12:11:59 2006 +0100
     1.2 +++ b/tools/python/xen/lowlevel/xs/xs.c	Thu Jun 01 12:13:11 2006 +0100
     1.3 @@ -28,6 +28,7 @@
     1.4  #include <sys/types.h>
     1.5  #include <sys/stat.h>
     1.6  #include <fcntl.h>
     1.7 +#include <errno.h>
     1.8  
     1.9  #include <xenctrl.h>
    1.10  #include "xs.h"
    1.11 @@ -44,6 +45,8 @@
    1.12  #define PKG "xen.lowlevel.xs"
    1.13  #define CLS "xs"
    1.14  
    1.15 +static PyObject *xs_error;
    1.16 +
    1.17  /** Python wrapper round an xs handle.
    1.18   */
    1.19  typedef struct XsHandle {
    1.20 @@ -52,11 +55,17 @@ typedef struct XsHandle {
    1.21      PyObject *watches;
    1.22  } XsHandle;
    1.23  
    1.24 +static void xs_set_error(int value)
    1.25 +{
    1.26 +	errno = value;
    1.27 +	PyErr_SetFromErrno(xs_error);
    1.28 +}
    1.29 +
    1.30  static inline struct xs_handle *xshandle(XsHandle *self)
    1.31  {
    1.32      struct xs_handle *xh = self->xh;
    1.33      if (!xh)
    1.34 -        PyErr_SetString(PyExc_RuntimeError, "invalid xenstore daemon handle");
    1.35 +	xs_set_error(EINVAL);
    1.36      return xh;
    1.37  }
    1.38  
    1.39 @@ -77,7 +86,7 @@ static int parse_transaction_path(XsHand
    1.40  	"\n"                                            \
    1.41  	"Returns: [string] data read.\n"                \
    1.42  	"         None if key doesn't exist.\n"         \
    1.43 -	"Raises RuntimeError on error.\n"               \
    1.44 +	"Raises xen.lowlevel.xs.Error on error.\n"               \
    1.45  	"\n"
    1.46  
    1.47  static PyObject *xspy_read(XsHandle *self, PyObject *args)
    1.48 @@ -113,7 +122,7 @@ static PyObject *xspy_read(XsHandle *sel
    1.49  	" data   [string] : data to write.\n"			\
    1.50  	"\n"							\
    1.51  	"Returns None on success.\n"				\
    1.52 -	"Raises RuntimeError on error.\n"			\
    1.53 +	"Raises xen.lowlevel.xs.Error on error.\n"			\
    1.54  	"\n"
    1.55  
    1.56  static PyObject *xspy_write(XsHandle *self, PyObject *args)
    1.57 @@ -149,7 +158,7 @@ static PyObject *xspy_write(XsHandle *se
    1.58  	"\n"							\
    1.59  	"Returns: [string array] list of subdirectory names.\n"	\
    1.60  	"         None if key doesn't exist.\n"			\
    1.61 -	"Raises RuntimeError on error.\n"			\
    1.62 +	"Raises xen.lowlevel.xs.Error on error.\n"			\
    1.63  	"\n"
    1.64  
    1.65  static PyObject *xspy_ls(XsHandle *self, PyObject *args)
    1.66 @@ -187,7 +196,7 @@ static PyObject *xspy_ls(XsHandle *self,
    1.67  	" path [string]: path to directory to create.\n"	\
    1.68  	"\n"							\
    1.69  	"Returns None on success.\n"				\
    1.70 -	"Raises RuntimeError on error.\n"			\
    1.71 +	"Raises xen.lowlevel.xs.Error on error.\n"			\
    1.72  	"\n"
    1.73  
    1.74  static PyObject *xspy_mkdir(XsHandle *self, PyObject *args)
    1.75 @@ -215,7 +224,7 @@ static PyObject *xspy_mkdir(XsHandle *se
    1.76  	" path [string] : path to remove\n"             \
    1.77  	"\n"                                            \
    1.78  	"Returns None on success.\n"                    \
    1.79 -	"Raises RuntimeError on error.\n"               \
    1.80 +	"Raises xen.lowlevel.xs.Error on error.\n"               \
    1.81  	"\n"
    1.82  
    1.83  static PyObject *xspy_rm(XsHandle *self, PyObject *args)
    1.84 @@ -243,7 +252,7 @@ static PyObject *xspy_rm(XsHandle *self,
    1.85  	" path [string]:        xenstore path.\n"       \
    1.86  	"\n"                                            \
    1.87  	"Returns: permissions array.\n"                 \
    1.88 -	"Raises RuntimeError on error.\n"               \
    1.89 +	"Raises xen.lowlevel.xs.Error on error.\n"               \
    1.90  	"\n"
    1.91  
    1.92  static PyObject *xspy_get_permissions(XsHandle *self, PyObject *args)
    1.93 @@ -285,7 +294,7 @@ static PyObject *xspy_get_permissions(Xs
    1.94          return val;
    1.95      }
    1.96      else {
    1.97 -        PyErr_SetFromErrno(PyExc_RuntimeError);
    1.98 +        PyErr_SetFromErrno(xs_error);
    1.99          return NULL;
   1.100      }
   1.101  }
   1.102 @@ -297,7 +306,7 @@ static PyObject *xspy_get_permissions(Xs
   1.103  	" perms               : permissions.\n"         \
   1.104  	"\n"                                            \
   1.105  	"Returns None on success.\n"                    \
   1.106 -	"Raises RuntimeError on error.\n"               \
   1.107 +	"Raises xen.lowlevel.xs.Error on error.\n"               \
   1.108  	"\n"
   1.109  
   1.110  static PyObject *xspy_set_permissions(XsHandle *self, PyObject *args)
   1.111 @@ -324,13 +333,13 @@ static PyObject *xspy_set_permissions(Xs
   1.112      th = strtoul(thstr, NULL, 16);
   1.113  
   1.114      if (!PyList_Check(perms)) {
   1.115 -        PyErr_SetString(PyExc_RuntimeError, "perms must be a list");
   1.116 +	xs_set_error(EINVAL);
   1.117          goto exit;
   1.118      }
   1.119      xsperms_n = PyList_Size(perms);
   1.120      xsperms = calloc(xsperms_n, sizeof(struct xs_permissions));
   1.121      if (!xsperms) {
   1.122 -        PyErr_SetString(PyExc_RuntimeError, "out of memory");
   1.123 +	xs_set_error(ENOMEM);
   1.124          goto exit;
   1.125      }
   1.126      tuple0 = PyTuple_New(0);
   1.127 @@ -352,7 +361,7 @@ static PyObject *xspy_set_permissions(Xs
   1.128      result = xs_set_permissions(xh, th, path, xsperms, xsperms_n);
   1.129      Py_END_ALLOW_THREADS
   1.130      if (!result) {
   1.131 -        PyErr_SetFromErrno(PyExc_RuntimeError);
   1.132 +        PyErr_SetFromErrno(xs_error);
   1.133          goto exit;
   1.134      }
   1.135  
   1.136 @@ -371,7 +380,7 @@ static PyObject *xspy_set_permissions(Xs
   1.137  	" token    [string] : returned in watch notification.\n"	\
   1.138  	"\n"								\
   1.139  	"Returns None on success.\n"					\
   1.140 -	"Raises RuntimeError on error.\n"				\
   1.141 +	"Raises xen.lowlevel.xs.Error on error.\n"				\
   1.142  	"\n"
   1.143  
   1.144  /* Each 10 bits takes ~ 3 digits, plus one, plus one for nul terminator. */
   1.145 @@ -421,7 +430,7 @@ static PyObject *xspy_watch(XsHandle *se
   1.146  	"Read a watch notification.\n"				\
   1.147  	"\n"							\
   1.148  	"Returns: [tuple] (path, token).\n"			\
   1.149 -	"Raises RuntimeError on error.\n"			\
   1.150 +	"Raises xen.lowlevel.xs.Error on error.\n"			\
   1.151  	"\n"
   1.152  
   1.153  static PyObject *xspy_read_watch(XsHandle *self, PyObject *args)
   1.154 @@ -441,11 +450,11 @@ again:
   1.155      xsval = xs_read_watch(xh, &num);
   1.156      Py_END_ALLOW_THREADS
   1.157      if (!xsval) {
   1.158 -        PyErr_SetFromErrno(PyExc_RuntimeError);
   1.159 +        PyErr_SetFromErrno(xs_error);
   1.160          goto exit;
   1.161      }
   1.162      if (sscanf(xsval[XS_WATCH_TOKEN], "%li", (unsigned long *)&token) != 1) {
   1.163 -        PyErr_SetString(PyExc_RuntimeError, "invalid token");
   1.164 +	xs_set_error(EINVAL);
   1.165          goto exit;
   1.166      }
   1.167      for (i = 0; i < PyList_Size(self->watches); i++) {
   1.168 @@ -474,7 +483,7 @@ again:
   1.169  	" token [string] : token from the watch.\n"	\
   1.170  	"\n"						\
   1.171  	"Returns None on success.\n"			\
   1.172 -	"Raises RuntimeError on error.\n"		\
   1.173 +	"Raises xen.lowlevel.xs.Error on error.\n"		\
   1.174  	"\n"
   1.175  
   1.176  static PyObject *xspy_unwatch(XsHandle *self, PyObject *args)
   1.177 @@ -504,7 +513,7 @@ static PyObject *xspy_unwatch(XsHandle *
   1.178  	"Start a transaction.\n"				\
   1.179  	"\n"							\
   1.180  	"Returns transaction handle on success.\n"		\
   1.181 -	"Raises RuntimeError on error.\n"			\
   1.182 +	"Raises xen.lowlevel.xs.Error on error.\n"			\
   1.183  	"\n"
   1.184  
   1.185  static PyObject *xspy_transaction_start(XsHandle *self)
   1.186 @@ -521,7 +530,7 @@ static PyObject *xspy_transaction_start(
   1.187      Py_END_ALLOW_THREADS
   1.188  
   1.189      if (th == XBT_NULL) {
   1.190 -        PyErr_SetFromErrno(PyExc_RuntimeError);
   1.191 +        PyErr_SetFromErrno(xs_error);
   1.192          return NULL;
   1.193      }
   1.194  
   1.195 @@ -535,7 +544,7 @@ static PyObject *xspy_transaction_start(
   1.196  	" abort [int]: abort flag (default 0).\n"			\
   1.197  	"\n"								\
   1.198  	"Returns True on success, False if you need to try again.\n"	\
   1.199 -	"Raises RuntimeError on error.\n"				\
   1.200 +	"Raises xen.lowlevel.xs.Error on error.\n"				\
   1.201  	"\n"
   1.202  
   1.203  static PyObject *xspy_transaction_end(XsHandle *self, PyObject *args,
   1.204 @@ -572,7 +581,7 @@ static PyObject *xspy_transaction_end(Xs
   1.205          return Py_False;
   1.206      }
   1.207      else {
   1.208 -        PyErr_SetFromErrno(PyExc_RuntimeError);
   1.209 +        PyErr_SetFromErrno(xs_error);
   1.210          return NULL;
   1.211      }
   1.212  }
   1.213 @@ -585,7 +594,7 @@ static PyObject *xspy_transaction_end(Xs
   1.214  	" port [int]   : port the domain is using for xenstore\n"	\
   1.215  	"\n"								\
   1.216  	"Returns None on success.\n"					\
   1.217 -	"Raises RuntimeError on error.\n"				\
   1.218 +	"Raises xen.lowlevel.xs.Error on error.\n"				\
   1.219  	"\n"
   1.220  
   1.221  static PyObject *xspy_introduce_domain(XsHandle *self, PyObject *args)
   1.222 @@ -616,7 +625,7 @@ static PyObject *xspy_introduce_domain(X
   1.223  	" dom [int]: domain id\n"					\
   1.224  	"\n"								\
   1.225  	"Returns None on success.\n"					\
   1.226 -	"Raises RuntimeError on error.\n"				\
   1.227 +	"Raises xen.lowlevel.xs.Error on error.\n"				\
   1.228  	"\n"
   1.229  
   1.230  static PyObject *xspy_release_domain(XsHandle *self, PyObject *args)
   1.231 @@ -643,7 +652,7 @@ static PyObject *xspy_release_domain(XsH
   1.232  	"Close the connection to xenstore.\n"	\
   1.233  	"\n"					\
   1.234  	"Returns None on success.\n"		\
   1.235 -	"Raises RuntimeError on error.\n"	\
   1.236 +	"Raises xen.lowlevel.xs.Error on error.\n"	\
   1.237  	"\n"
   1.238  
   1.239  static PyObject *xspy_close(XsHandle *self)
   1.240 @@ -672,7 +681,7 @@ static PyObject *xspy_close(XsHandle *se
   1.241  	" domid [int]: domain id\n"			\
   1.242  	"\n"						\
   1.243  	"Returns: [string] domain store path.\n"	\
   1.244 -	"Raises RuntimeError on error.\n"		\
   1.245 +	"Raises xen.lowlevel.xs.Error on error.\n"		\
   1.246  	"\n"
   1.247  
   1.248  static PyObject *xspy_get_domain_path(XsHandle *self, PyObject *args)
   1.249 @@ -754,7 +763,7 @@ static PyObject *none(bool result)
   1.250          return Py_None;
   1.251      }
   1.252      else {
   1.253 -        PyErr_SetFromErrno(PyExc_RuntimeError);
   1.254 +        PyErr_SetFromErrno(xs_error);
   1.255          return NULL;
   1.256      }
   1.257  }
   1.258 @@ -830,7 +839,7 @@ xshandle_init(XsHandle *self, PyObject *
   1.259      return 0;
   1.260  
   1.261   fail:
   1.262 -    PyErr_SetFromErrno(PyExc_RuntimeError);
   1.263 +    PyErr_SetFromErrno(xs_error);
   1.264      return -1;
   1.265  }
   1.266  
   1.267 @@ -902,8 +911,13 @@ PyMODINIT_FUNC initxs(void)
   1.268      if (m == NULL)
   1.269        return;
   1.270  
   1.271 +    xs_error = PyErr_NewException(PKG ".Error", PyExc_RuntimeError, NULL);
   1.272 +
   1.273      Py_INCREF(&xshandle_type);
   1.274      PyModule_AddObject(m, CLS, (PyObject *)&xshandle_type);
   1.275 +
   1.276 +    Py_INCREF(xs_error);
   1.277 +    PyModule_AddObject(m, "Error", xs_error);
   1.278  }
   1.279  
   1.280