[PATCH] Create assertion function to return PyErrors.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



---
 dm.c        |    8 ++++++++
 dmraid.c    |   14 ++++++++++++++
 pyhelpers.h |   39 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+), 0 deletions(-)

diff --git a/dm.c b/dm.c
index a1572f0..e1ddb90 100644
--- a/dm.c
+++ b/dm.c
@@ -297,6 +297,8 @@ pydm_dev_hash(PyObject *self)
 static PyObject *
 pydm_dev_get(PyObject *self, void *data)
 {
+	PB_DM_ASSERT_DEV(((PydmDeviceObject *)self), return NULL);
+
 	PydmDeviceObject *dev = (PydmDeviceObject *)self;
 	const char *attr = (const char *)data;
 
@@ -529,6 +531,8 @@ pydm_table_compare(PydmTableObject *self, PydmTableObject *other)
 static PyObject *
 pydm_table_get(PyObject *self, void *data)
 {
+	PB_DM_ASSERT_TABLE(((PydmTableObject *)self), return NULL);
+
 	PydmTableObject *table = (PydmTableObject *)self;
 	const char *attr = (const char *)data;
 
@@ -1272,6 +1276,8 @@ pydm_map_get_deps(PydmMapObject *map)
 PyObject *
 pydm_map_get(PyObject *self, void *data)
 {
+	PB_DM_ASSERT_MAP(((PydmMapObject *)self), return NULL);
+
 	PydmMapObject *map = (PydmMapObject *)self;
 	const char *attr = (const char *)data;
 
@@ -1457,6 +1463,8 @@ pydm_target_str_method(PyObject *self)
 static PyObject *
 pydm_target_get(PyObject *self, void *data)
 {
+	PB_DM_ASSERT_TARGET(((PydmTargetObject *)self), return NULL);
+
 	PydmTargetObject *target = (PydmTargetObject *)self;
 	const char *attr = (const char *)data;
 
diff --git a/dmraid.c b/dmraid.c
index b9121f4..6d4e7b7 100644
--- a/dmraid.c
+++ b/dmraid.c
@@ -187,6 +187,8 @@ pydmraid_dev_str_method(PyObject *self)
 static PyObject *
 pydmraid_dev_get(PyObject *self, void *data)
 {
+	PB_DMRAID_ASSERT_DEVCTX(((PydmraidDeviceObject *)self), return NULL);
+
 	PydmraidDeviceObject *dev = (PydmraidDeviceObject *)self;
 	const char *attr = (const char *)data;
 
@@ -424,6 +426,8 @@ pydmraid_raiddev_repr_method(PyObject *self)
 static PyObject *
 pydmraid_raiddev_get(PyObject *self, void *data)
 {
+	PB_DMRAID_ASSERT_DEVCTX(((PydmraidRaidDevObject *)self), return NULL);
+
 	PydmraidRaidDevObject *dev = (PydmraidRaidDevObject *)self;
 	const char *attr = (const char *)data;
 
@@ -577,6 +581,8 @@ pydmraid_raidset_repr_method(PyObject *self)
 static PyObject *
 pydmraid_raidset_get_children(PyObject *self, void *data)
 {
+	PB_DMRAID_ASSERT_RSCTX(((PydmraidRaidSetObject *)self), return NULL);
+
 	PydmraidRaidSetObject *set = (PydmraidRaidSetObject *)self;
 	struct lib_context *lc = set->ctx->lc;
 	PyObject *t;
@@ -645,6 +651,8 @@ pydmraid_raidset_get_spares(PyObject *self, void *data)
 static PyObject *
 pydmraid_raidset_get_table(PyObject *self, void *data)
 {
+	PB_DMRAID_ASSERT_RSCTX(((PydmraidRaidSetObject *)self), return NULL);
+
 	PydmraidRaidSetObject *set = (PydmraidRaidSetObject *)self;
 	struct lib_context *lc = set->ctx->lc;
 	struct raid_set *rs = set->rs;
@@ -699,6 +707,8 @@ pydmraid_raidset_get_table(PyObject *self, void *data)
 static PyObject *
 pydmraid_raidset_get_dm_table(PyObject *self, void *data)
 {
+	PB_DMRAID_ASSERT_RSCTX(((PydmraidRaidSetObject *)self), return NULL);
+
 	PydmraidRaidSetObject *set = (PydmraidRaidSetObject *)self;
 	struct lib_context *lc = set->ctx->lc;
 	struct raid_set *rs = set->rs;
@@ -899,6 +909,8 @@ out:
 static PyObject *
 pydmraid_raidset_get(PyObject *self, void *data)
 {
+	PB_DMRAID_ASSERT_RSCTX(((PydmraidRaidSetObject *)self), return NULL);
+
 	PydmraidRaidSetObject *set = (PydmraidRaidSetObject *)self;
 	struct lib_context *lc = set->ctx->lc;
 	struct raid_set *rs = set->rs;
@@ -1108,6 +1120,8 @@ pydmraid_ctx_init_method(PyObject *self, PyObject *args, PyObject *kwds)
 static PyObject *
 pydmraid_ctx_get(PyObject *self, void *data)
 {
+	PB_DMRAID_ASSERT_CTX(((PydmraidContextObject *)self), return NULL);
+
 	PydmraidContextObject *ctx = (PydmraidContextObject *)self;
 	const char *attr = (const char *)data;
 
diff --git a/pyhelpers.h b/pyhelpers.h
index 7822981..de4cedf 100644
--- a/pyhelpers.h
+++ b/pyhelpers.h
@@ -19,6 +19,45 @@
 #ifndef _PYBLOCK_PYHELPERS_H
 #define _PYBLOCK_PYHELPERS_H 1
 
+#define PYBLOCK_ASSERT(cond, message, action)\
+	if(!(cond)){\
+		PyErr_SetString(PyExc_AssertionError, message);\
+		action;\
+	}
+
+/* ASSERT functions for dmraid.c */
+#define PB_DMRAID_ASSERT_CTX(a_ctx, action)\
+	PYBLOCK_ASSERT(a_ctx != NULL, "The pyblock context is NULL.", action);\
+	PYBLOCK_ASSERT(a_ctx->lc != NULL, "The dmraid context is NULL.", action)
+
+#define PB_DMRAID_ASSERT_RS(a_rs, action)\
+	PYBLOCK_ASSERT(a_rs != NULL, "The pyblock raidset is NULL.", action);\
+	PYBLOCK_ASSERT(a_rs->rs != NULL, "The dmraid raidset is NULL.", action)
+
+#define PB_DMRAID_ASSERT_DEV(a_dev, action)\
+	PYBLOCK_ASSERT(a_dev != NULL, "The pyblock device is NULL.", action)
+
+#define PB_DMRAID_ASSERT_RSCTX(pb_rs, action)\
+	PB_DMRAID_ASSERT_RS(pb_rs, action);\
+	PB_DMRAID_ASSERT_CTX(pb_rs->ctx, action)
+
+#define PB_DMRAID_ASSERT_DEVCTX(pb_dev, action)\
+	PB_DMRAID_ASSERT_DEV(pb_dev, action);\
+	PB_DMRAID_ASSERT_CTX(pb_dev->ctx, action)
+
+/* ASSERT functions for dm.c */
+#define PB_DM_ASSERT_DEV(a_dev, action)\
+	PYBLOCK_ASSERT(a_dev != NULL, "The pyblock device is NULL.", action)
+
+#define PB_DM_ASSERT_TABLE(a_table, action)\
+	PYBLOCK_ASSERT(a_table != NULL, "The pyblock table is NULL.", action)
+
+#define PB_DM_ASSERT_MAP(a_map, action)\
+	PYBLOCK_ASSERT(a_map != NULL, "The pyblock map is NULL.", action)
+
+#define PB_DM_ASSERT_TARGET(a_target, action)\
+	PYBLOCK_ASSERT(a_target != NULL, "The pyblock target is NULL.", action)
+
 extern PyObject *
 pyblock_PyString_FromFormatV(const char *format, va_list)
 	__attribute__ ((format(printf, 1, 0)));
-- 
1.6.0.6

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/anaconda-devel-list

[Index of Archives]     [Kickstart]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]
  Powered by Linux