python: create NTSTATUSError, HRESULTError and WERRORError
authorAndrew Bartlett <abartlet@samba.org>
Tue, 1 Nov 2016 02:23:58 +0000 (15:23 +1300)
committerGarming Sam <garming@samba.org>
Fri, 4 Nov 2016 03:41:19 +0000 (04:41 +0100)
The advantage of these over the previous use of just RuntimeError is that we can
catch just the errors we want, without having to catch all possible RuntimeError
cases and assume they decode to a tuple

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Garming Sam <garming@catalyst.net.nz>
BUG: https://bugzilla.samba.org/show_bug.cgi?id=12398

python/pyglue.c
python/samba/__init__.py
source4/libcli/util/pyerrors.h

index 81244a249734aae536964916378550bb36719f3b..938a9f0ecf473c1b94726b3e37636b68e3c6489c 100644 (file)
@@ -24,6 +24,9 @@
 #include "lib/socket/netif.h"
 
 void init_glue(void);
+static PyObject *PyExc_NTSTATUSError;
+static PyObject *PyExc_WERRORError;
+static PyObject *PyExc_HRESULTError;
 
 static PyObject *py_generate_random_str(PyObject *self, PyObject *args)
 {
@@ -294,5 +297,22 @@ void init_glue(void)
 
        PyModule_AddObject(m, "version",
                                           PyString_FromString(SAMBA_VERSION_STRING));
+       PyExc_NTSTATUSError = PyErr_NewException(discard_const_p(char, "samba.NTSTATUSError"), PyExc_RuntimeError, NULL);
+       if (PyExc_NTSTATUSError != NULL) {
+               Py_INCREF(PyExc_NTSTATUSError);
+               PyModule_AddObject(m, "NTSTATUSError", PyExc_NTSTATUSError);
+       }
+
+       PyExc_WERRORError = PyErr_NewException(discard_const_p(char, "samba.WERRORError"), PyExc_RuntimeError, NULL);
+       if (PyExc_WERRORError != NULL) {
+               Py_INCREF(PyExc_WERRORError);
+               PyModule_AddObject(m, "WERRORError", PyExc_WERRORError);
+       }
+
+       PyExc_HRESULTError = PyErr_NewException(discard_const_p(char, "samba.HRESULTError"), PyExc_RuntimeError, NULL);
+       if (PyExc_HRESULTError != NULL) {
+               Py_INCREF(PyExc_HRESULTError);
+               PyModule_AddObject(m, "HRESULTError", PyExc_HRESULTError);
+       }
 }
 
index 7cfbc4c10889f5cca8663c1630fd5dc4d35ee6c3..8c75a48874b36e8b7343a8ed87283da54aee4d46 100644 (file)
@@ -399,3 +399,7 @@ generate_random_password = _glue.generate_random_password
 strcasecmp_m = _glue.strcasecmp_m
 strstr_m = _glue.strstr_m
 is_ntvfs_fileserver_built = _glue.is_ntvfs_fileserver_built
+
+NTSTATUSError = _glue.NTSTATUSError
+HRESULTError = _glue.HRESULTError
+WERRORError = _glue.WERRORError
index ef997131fff5ef0a016c718c42eccdb21aa522f5..9228c340ea72b3d2cc1dcaf8ffc89d8e1681a806 100644 (file)
 
 #define PyErr_FromString(str) Py_BuildValue("(s)", discard_const_p(char, str))
 
-#define PyErr_SetWERROR(err) \
-       PyErr_SetObject(PyExc_RuntimeError, PyErr_FromWERROR(err))
+#define PyErr_SetWERROR(werr) \
+        PyErr_SetObject(PyObject_GetAttrString(PyImport_ImportModule("samba"),\
+                                              "WERRORError"),  \
+                       PyErr_FromWERROR(werr))
+
+#define PyErr_SetHRESULT(hresult) \
+        PyErr_SetObject(PyObject_GetAttrString(PyImport_ImportModule("samba"),\
+                                              "HRESULTError"), \
+                       PyErr_FromHRESULT(hresult))
 
 #define PyErr_SetNTSTATUS(status) \
-        PyErr_SetObject(PyExc_RuntimeError, PyErr_FromNTSTATUS(status))
+        PyErr_SetObject(PyObject_GetAttrString(PyImport_ImportModule("samba"),\
+                                              "NTSTATUSError"),        \
+                       PyErr_FromNTSTATUS(status))
 
 #define PyErr_NTSTATUS_IS_ERR_RAISE(status) \
        if (NT_STATUS_IS_ERR(status)) { \