[Bf-blender-cvs] [72e2a0cfb6f] master: Cleanup: py-gpu error checks

Campbell Barton noreply at git.blender.org
Wed Jan 2 15:13:12 CET 2019


Commit: 72e2a0cfb6f8e4d21b33982bbd572f10c20e947c
Author: Campbell Barton
Date:   Thu Jan 3 01:08:26 2019 +1100
Branches: master
https://developer.blender.org/rB72e2a0cfb6f8e4d21b33982bbd572f10c20e947c

Cleanup: py-gpu error checks

Move gpu initialization checks to the start of each function instead
of mixing with argument parsing.

===================================================================

M	source/blender/python/gpu/gpu_py_api.c
M	source/blender/python/gpu/gpu_py_api.h
M	source/blender/python/gpu/gpu_py_batch.c
M	source/blender/python/gpu/gpu_py_element.c
M	source/blender/python/gpu/gpu_py_offscreen.c
M	source/blender/python/gpu/gpu_py_shader.c

===================================================================

diff --git a/source/blender/python/gpu/gpu_py_api.c b/source/blender/python/gpu/gpu_py_api.c
index f237561246a..7e999bdcc2d 100644
--- a/source/blender/python/gpu/gpu_py_api.c
+++ b/source/blender/python/gpu/gpu_py_api.c
@@ -49,7 +49,7 @@
 /** \name Utils to invalidate functions
  * \{ */
 
-bool bpygpu_is_initialized(void)
+bool bpygpu_is_initialized_or_error(void)
 {
 	if (!GPU_is_initialized()) {
 		PyErr_SetString(
diff --git a/source/blender/python/gpu/gpu_py_api.h b/source/blender/python/gpu/gpu_py_api.h
index 2b4d8fa515c..af08881b723 100644
--- a/source/blender/python/gpu/gpu_py_api.h
+++ b/source/blender/python/gpu/gpu_py_api.h
@@ -25,10 +25,13 @@
 #ifndef __GPU_PY_API_H__
 #define __GPU_PY_API_H__
 
-bool bpygpu_is_initialized(void);
 
 int bpygpu_ParsePrimType(PyObject *o, void *p);
 
 PyObject *BPyInit_gpu(void);
 
+bool bpygpu_is_initialized_or_error(void);
+#define BPYGPU_IS_INIT_OR_ERROR_OBJ if (UNLIKELY(!bpygpu_is_initialized_or_error())) { return NULL; } ((void)0)
+#define BPYGPU_IS_INIT_OR_ERROR_INT if (UNLIKELY(!bpygpu_is_initialized_or_error())) { return -1; } ((void)0)
+
 #endif  /* __GPU_PY_API_H__ */
diff --git a/source/blender/python/gpu/gpu_py_batch.c b/source/blender/python/gpu/gpu_py_batch.c
index 93274e33cc4..a50ea32b5dd 100644
--- a/source/blender/python/gpu/gpu_py_batch.c
+++ b/source/blender/python/gpu/gpu_py_batch.c
@@ -75,6 +75,8 @@ static bool bpygpu_batch_is_program_or_error(BPyGPUBatch *self)
 
 static PyObject *bpygpu_Batch_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
 {
+	BPYGPU_IS_INIT_OR_ERROR_OBJ;
+
 	const char *exc_str_missing_arg = "GPUBatch.__new__() missing required argument '%s' (pos %d)";
 
 	struct {
@@ -85,8 +87,7 @@ static PyObject *bpygpu_Batch_new(PyTypeObject *UNUSED(type), PyObject *args, Py
 
 	static const char *_keywords[] = {"type", "buf", "elem", NULL};
 	static _PyArg_Parser _parser = {"|$O&O!O!:GPUBatch.__new__", _keywords, 0};
-	if (!bpygpu_is_initialized() ||
-	    !_PyArg_ParseTupleAndKeywordsFast(
+	if (!_PyArg_ParseTupleAndKeywordsFast(
 	        args, kwds, &_parser,
 	        bpygpu_ParsePrimType, &params.type_id,
 	        &BPyGPUVertBuf_Type, &params.py_vertbuf,
diff --git a/source/blender/python/gpu/gpu_py_element.c b/source/blender/python/gpu/gpu_py_element.c
index 592c761ffbc..2faf57ac55c 100644
--- a/source/blender/python/gpu/gpu_py_element.c
+++ b/source/blender/python/gpu/gpu_py_element.c
@@ -47,6 +47,8 @@
 
 static PyObject *bpygpu_IndexBuf_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
 {
+	BPYGPU_IS_INIT_OR_ERROR_OBJ;
+
 	const char *error_prefix = "IndexBuf.__new__";
 	bool ok = true;
 
@@ -61,8 +63,7 @@ static PyObject *bpygpu_IndexBuf_new(PyTypeObject *UNUSED(type), PyObject *args,
 
 	static const char *_keywords[] = {"type", "seq", NULL};
 	static _PyArg_Parser _parser = {"$O&O:IndexBuf.__new__", _keywords, 0};
-	if (!bpygpu_is_initialized() ||
-	    !_PyArg_ParseTupleAndKeywordsFast(
+	if (!_PyArg_ParseTupleAndKeywordsFast(
 	        args, kwds, &_parser,
 	        bpygpu_ParsePrimType, &params.type_id,
 	        &params.seq))
diff --git a/source/blender/python/gpu/gpu_py_offscreen.c b/source/blender/python/gpu/gpu_py_offscreen.c
index 3ce90c5d648..5dc6e36ca56 100644
--- a/source/blender/python/gpu/gpu_py_offscreen.c
+++ b/source/blender/python/gpu/gpu_py_offscreen.c
@@ -88,14 +88,15 @@ static int bpygpu_offscreen_valid_check(BPyGPUOffScreen *bpygpu_ofs)
 
 static PyObject *bpygpu_offscreen_new(PyTypeObject *UNUSED(self), PyObject *args, PyObject *kwds)
 {
+	BPYGPU_IS_INIT_OR_ERROR_OBJ;
+
 	GPUOffScreen *ofs;
 	int width, height, samples = 0;
 	char err_out[256];
 
 	static const char *_keywords[] = {"width", "height", "samples", NULL};
 	static _PyArg_Parser _parser = {"ii|i:GPUOffScreen.__new__", _keywords, 0};
-	if (!bpygpu_is_initialized() ||
-	    !_PyArg_ParseTupleAndKeywordsFast(
+	if (!_PyArg_ParseTupleAndKeywordsFast(
 	        args, kwds, &_parser,
 	        &width, &height, &samples))
 	{
diff --git a/source/blender/python/gpu/gpu_py_shader.c b/source/blender/python/gpu/gpu_py_shader.c
index 59a416a0c3a..52fe6f2d912 100644
--- a/source/blender/python/gpu/gpu_py_shader.c
+++ b/source/blender/python/gpu/gpu_py_shader.c
@@ -105,6 +105,8 @@ static int bpygpu_uniform_location_get(GPUShader *shader, const char *name, cons
 
 static PyObject *bpygpu_shader_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
 {
+	BPYGPU_IS_INIT_OR_ERROR_OBJ;
+
 	struct {
 		const char *vertexcode;
 		const char *fragcode;
@@ -118,8 +120,7 @@ static PyObject *bpygpu_shader_new(PyTypeObject *UNUSED(type), PyObject *args, P
 	        "libcode", "defines", NULL};
 
 	static _PyArg_Parser _parser = {"ss|$sss:GPUShader.__new__", _keywords, 0};
-	if (!bpygpu_is_initialized() ||
-	    !_PyArg_ParseTupleAndKeywordsFast(
+	if (!_PyArg_ParseTupleAndKeywordsFast(
 	        args, kwds, &_parser,
 	        &params.vertexcode, &params.fragcode, &params.geocode,
 	        &params.libcode, &params.defines))
@@ -717,11 +718,11 @@ PyDoc_STRVAR(bpygpu_shader_from_builtin_doc,
 );
 static PyObject *bpygpu_shader_from_builtin(PyObject *UNUSED(self), PyObject *arg)
 {
+	BPYGPU_IS_INIT_OR_ERROR_OBJ;
+
 	GPUBuiltinShader shader_id;
 
-	if (!bpygpu_is_initialized() ||
-	    !bpygpu_ParseBultinShaderEnum(arg, &shader_id))
-	{
+	if (!bpygpu_ParseBultinShaderEnum(arg, &shader_id)) {
 		return NULL;
 	}



More information about the Bf-blender-cvs mailing list