Commit: 945007b32e9a30252216398413bff88c103d8667
Author: mano-wii
Date:   Wed Jan 2 10:01:46 2019 -0200
Branches: master
https://developer.blender.org/rB945007b32e9a30252216398413bff88c103d8667

Fix T59773: Raise exception if the gpu module is used in backgound mode.

Instead of crashing, an error message is displayed if a function of the gpu 
module is called without a GPU context.

Reviewers: brecht, campbellbarton, JacquesLucke, mont29

Subscribers: abdelmatinboulbayam, amir.shehata

Differential Revision: https://developer.blender.org/D4143

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

M       source/blender/gpu/GPU_init_exit.h
M       source/blender/gpu/intern/gpu_init_exit.c
M       source/blender/python/gpu/CMakeLists.txt
R061    source/blender/python/gpu/gpu_py_api.c  
source/blender/python/gpu/gpu_py.c
R085    source/blender/python/gpu/gpu_py_api.h  
source/blender/python/gpu/gpu_py.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
D       source/blender/python/gpu/gpu_py_primitive.c
D       source/blender/python/gpu/gpu_py_primitive.h
M       source/blender/python/gpu/gpu_py_shader.c
M       source/blender/python/intern/bpy_interface.c

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

diff --git a/source/blender/gpu/GPU_init_exit.h 
b/source/blender/gpu/GPU_init_exit.h
index e89c970b7d9..8f1a42c8795 100644
--- a/source/blender/gpu/GPU_init_exit.h
+++ b/source/blender/gpu/GPU_init_exit.h
@@ -38,6 +38,7 @@ extern "C" {
 
 void GPU_init(void);
 void GPU_exit(void);
+bool GPU_is_initialized(void);
 
 #ifdef __cplusplus
 }
diff --git a/source/blender/gpu/intern/gpu_init_exit.c 
b/source/blender/gpu/intern/gpu_init_exit.c
index 55d0466c929..d21acb188d9 100644
--- a/source/blender/gpu/intern/gpu_init_exit.c
+++ b/source/blender/gpu/intern/gpu_init_exit.c
@@ -73,7 +73,6 @@ void GPU_init(void)
 }
 
 
-
 void GPU_exit(void)
 {
        if (!G.background) {
@@ -92,3 +91,9 @@ void GPU_exit(void)
 
        initialized = false;
 }
+
+
+bool GPU_is_initialized(void)
+{
+       return initialized;
+}
diff --git a/source/blender/python/gpu/CMakeLists.txt 
b/source/blender/python/gpu/CMakeLists.txt
index 0d4dedf7c76..fb5eb07a7d8 100644
--- a/source/blender/python/gpu/CMakeLists.txt
+++ b/source/blender/python/gpu/CMakeLists.txt
@@ -34,24 +34,22 @@ set(INC_SYS
 )
 
 set(SRC
-       gpu_py_api.c
+       gpu_py.c
        gpu_py_batch.c
        gpu_py_element.c
        gpu_py_matrix.c
        gpu_py_offscreen.c
-       gpu_py_primitive.c
        gpu_py_select.c
        gpu_py_shader.c
        gpu_py_types.c
        gpu_py_vertex_buffer.c
        gpu_py_vertex_format.c
 
-       gpu_py_api.h
+       gpu_py.h
        gpu_py_batch.h
        gpu_py_element.h
        gpu_py_matrix.h
        gpu_py_offscreen.h
-       gpu_py_primitive.h
        gpu_py_select.h
        gpu_py_shader.h
        gpu_py_types.h
diff --git a/source/blender/python/gpu/gpu_py_api.c 
b/source/blender/python/gpu/gpu_py.c
similarity index 61%
rename from source/blender/python/gpu/gpu_py_api.c
rename to source/blender/python/gpu/gpu_py.c
index 373bce160b7..5470e791011 100644
--- a/source/blender/python/gpu/gpu_py_api.c
+++ b/source/blender/python/gpu/gpu_py.c
@@ -34,11 +34,89 @@
 
 #include "../generic/python_utildefines.h"
 
+#include "GPU_init_exit.h"
+#include "GPU_primitive.h"
+
 #include "gpu_py_matrix.h"
 #include "gpu_py_select.h"
 #include "gpu_py_types.h"
 
-#include "gpu_py_api.h" /* own include */
+#include "gpu_py.h" /* own include */
+
+
+/* -------------------------------------------------------------------- */
+
+/** \name Utils to invalidate functions
+ * \{ */
+
+bool bpygpu_is_initialized(void)
+{
+       if (!GPU_is_initialized()) {
+               PyErr_SetString(
+                       PyExc_SystemError,
+                       "GPU functions for drawing are not available in 
background mode");
+
+               return false;
+       }
+
+       return true;
+}
+
+/** \} */
+
+
+/* -------------------------------------------------------------------- */
+
+/** \name Primitive Type Utils
+ * \{ */
+
+int bpygpu_ParsePrimType(PyObject *o, void *p)
+{
+       Py_ssize_t mode_id_len;
+       const char *mode_id = _PyUnicode_AsStringAndSize(o, &mode_id_len);
+       if (mode_id == NULL) {
+               PyErr_Format(PyExc_ValueError,
+                            "expected a string, got %s",
+                            Py_TYPE(o)->tp_name);
+               return 0;
+       }
+#define MATCH_ID(id) \
+       if (mode_id_len == strlen(STRINGIFY(id))) { \
+               if (STREQ(mode_id, STRINGIFY(id))) { \
+                       mode = GPU_PRIM_##id; \
+                       goto success; \
+               } \
+       } ((void)0)
+
+       GPUPrimType mode;
+       MATCH_ID(POINTS);
+       MATCH_ID(LINES);
+       MATCH_ID(TRIS);
+       MATCH_ID(LINE_STRIP);
+       MATCH_ID(LINE_LOOP);
+       MATCH_ID(TRI_STRIP);
+       MATCH_ID(TRI_FAN);
+       MATCH_ID(LINE_STRIP_ADJ);
+
+#undef MATCH_ID
+       PyErr_Format(PyExc_ValueError,
+                    "unknown type literal: '%s'",
+                    mode_id);
+       return 0;
+
+success:
+       (*(GPUPrimType *)p) = mode;
+       return 1;
+}
+
+/** \} */
+
+
+/* -------------------------------------------------------------------- */
+
+/** \name GPU Module
+ * \{ */
+
 
 PyDoc_STRVAR(GPU_doc,
 "This module provides Python wrappers for the GPU implementation in Blender. "
@@ -83,3 +161,5 @@ PyObject *BPyInit_gpu(void)
 
        return mod;
 }
+
+/** \} */
diff --git a/source/blender/python/gpu/gpu_py_api.h 
b/source/blender/python/gpu/gpu_py.h
similarity index 85%
rename from source/blender/python/gpu/gpu_py_api.h
rename to source/blender/python/gpu/gpu_py.h
index 20eafb3d140..d3ee7b56def 100644
--- a/source/blender/python/gpu/gpu_py_api.h
+++ b/source/blender/python/gpu/gpu_py.h
@@ -22,9 +22,13 @@
  *  \ingroup bpygpu
  */
 
-#ifndef __GPU_PY_API_H__
-#define __GPU_PY_API_H__
+#ifndef __GPU_PY_H__
+#define __GPU_PY_H__
+
+bool bpygpu_is_initialized(void);
+
+int bpygpu_ParsePrimType(PyObject *o, void *p);
 
 PyObject *BPyInit_gpu(void);
 
-#endif  /* __GPU_PY_API_H__ */
+#endif  /* __GPU_PY_H__ */
diff --git a/source/blender/python/gpu/gpu_py_batch.c 
b/source/blender/python/gpu/gpu_py_batch.c
index ffbd2a17e34..aa687ecaff5 100644
--- a/source/blender/python/gpu/gpu_py_batch.c
+++ b/source/blender/python/gpu/gpu_py_batch.c
@@ -44,7 +44,7 @@
 
 #include "../generic/py_capi_utils.h"
 
-#include "gpu_py_primitive.h"
+#include "gpu_py.h"
 #include "gpu_py_shader.h"
 #include "gpu_py_vertex_buffer.h"
 #include "gpu_py_element.h"
@@ -85,7 +85,8 @@ 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 (!_PyArg_ParseTupleAndKeywordsFast(
+       if (!bpygpu_is_initialized() ||
+           !_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 12027d2e2b8..b996e859bc7 100644
--- a/source/blender/python/gpu/gpu_py_element.c
+++ b/source/blender/python/gpu/gpu_py_element.c
@@ -36,7 +36,7 @@
 #include "../generic/py_capi_utils.h"
 #include "../generic/python_utildefines.h"
 
-#include "gpu_py_primitive.h"
+#include "gpu_py.h"
 #include "gpu_py_element.h" /* own include */
 
 
@@ -61,7 +61,8 @@ 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 (!_PyArg_ParseTupleAndKeywordsFast(
+       if (!bpygpu_is_initialized() ||
+           !_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 c51a356d900..2725b55a5da 100644
--- a/source/blender/python/gpu/gpu_py_offscreen.c
+++ b/source/blender/python/gpu/gpu_py_offscreen.c
@@ -53,6 +53,7 @@
 
 #include "../generic/py_capi_utils.h"
 
+#include "gpu_py.h"
 #include "gpu_py_offscreen.h" /* own include */
 
 
@@ -93,7 +94,8 @@ static PyObject *bpygpu_offscreen_new(PyTypeObject 
*UNUSED(self), PyObject *args
 
        static const char *_keywords[] = {"width", "height", "samples", NULL};
        static _PyArg_Parser _parser = {"ii|i:GPUOffScreen.__new__", _keywords, 
0};
-       if (!_PyArg_ParseTupleAndKeywordsFast(
+       if (!bpygpu_is_initialized() ||
+           !_PyArg_ParseTupleAndKeywordsFast(
                args, kwds, &_parser,
                &width, &height, &samples))
        {
diff --git a/source/blender/python/gpu/gpu_py_primitive.c 
b/source/blender/python/gpu/gpu_py_primitive.c
deleted file mode 100644
index 798dfc050f6..00000000000
--- a/source/blender/python/gpu/gpu_py_primitive.c
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * ***** BEGIN GPL LICENSE BLOCK *****
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- * Copyright 2015, Blender Foundation.
- *
- * ***** END GPL LICENSE BLOCK *****
- */
-
-/** \file blender/python/gpu/gpu_py_primitive.c
- *  \ingroup bpygpu
- *
- * - Use ``bpygpu_`` for local API.
- * - Use ``BPyGPU`` for public API.
- */
-
-#include <Python.h>
-
-#include "BLI_utildefines.h"
-
-#include "GPU_primitive.h"
-
-#include "gpu_py_primitive.h" /* own include */
-
-
-/* -------------------------------------------------------------------- */
-
-/** \name Primitive Utils
- * \{ */
-
-int bpygpu_ParsePrimType(PyObject *o, void *p)
-{
-       Py_ssize_t mode_id_len;
-       const char *mode_id = _PyUnicode_AsStringAndSize(o, &mode_id_len);
-       if (mode_id == NULL) {
-               PyErr_Format(PyExc_ValueError,
-                            "expected a string, got %s",
-                            Py_TYPE(o)->tp_name);
-               return 0;
-       }
-#define MATCH_ID(id) \
-       if (mode_id_len == strlen(STRINGIFY(id))) { \
-               if (STREQ(mode_id, STRINGIFY(id))) { \
-                       mode = GPU_PRIM_##id; \
-                       goto success; \
-               } \
-       } ((void)0)
-
-       GPUPrimType mode;
-       MATCH_ID(POINTS);
-       MATCH_ID(LINES);
-       MATCH_ID(TRIS);
-       MATCH_ID(LINE_STRIP);
-       MATCH_ID(LINE_LOOP);
-       MATCH_ID(TRI_STRIP);
-       MATCH_ID(TRI_FAN);
-       MATCH_ID(LINE_STRIP_ADJ);
-
-#undef MATCH_ID
-       PyErr_Format(PyExc_ValueError,
-                    "unknown type literal: '%s'",
-                    mode_id);
-       return 0;
-
-success:
-       (*(GPUPrimType *)p) = mode;
-       return 1;
-}
diff --git a/source/blender/python/gpu/gpu_py_primitive.h 
b/source/blender/python/gpu/gpu_py_primitive.h
deleted file mode 100644
index d10ee01c8ad..00000000000
--- a/source/blender/python/gpu/gpu_py_primitive.h
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * ***** BEGIN GPL LICENSE BLOCK *****
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPO

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to