Cleanup: py-gpu error checks

Move gpu initialization checks to the start of each function instead
of mixing with argument parsing.
This commit is contained in:
2019-01-03 01:08:26 +11:00
parent d07bc44a96
commit 72e2a0cfb6
6 changed files with 20 additions and 13 deletions

View File

@@ -49,7 +49,7 @@
/** \name Utils to invalidate functions /** \name Utils to invalidate functions
* \{ */ * \{ */
bool bpygpu_is_initialized(void) bool bpygpu_is_initialized_or_error(void)
{ {
if (!GPU_is_initialized()) { if (!GPU_is_initialized()) {
PyErr_SetString( PyErr_SetString(

View File

@@ -25,10 +25,13 @@
#ifndef __GPU_PY_API_H__ #ifndef __GPU_PY_API_H__
#define __GPU_PY_API_H__ #define __GPU_PY_API_H__
bool bpygpu_is_initialized(void);
int bpygpu_ParsePrimType(PyObject *o, void *p); int bpygpu_ParsePrimType(PyObject *o, void *p);
PyObject *BPyInit_gpu(void); 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__ */ #endif /* __GPU_PY_API_H__ */

View File

@@ -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) 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)"; const char *exc_str_missing_arg = "GPUBatch.__new__() missing required argument '%s' (pos %d)";
struct { 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 const char *_keywords[] = {"type", "buf", "elem", NULL};
static _PyArg_Parser _parser = {"|$O&O!O!:GPUBatch.__new__", _keywords, 0}; static _PyArg_Parser _parser = {"|$O&O!O!:GPUBatch.__new__", _keywords, 0};
if (!bpygpu_is_initialized() || if (!_PyArg_ParseTupleAndKeywordsFast(
!_PyArg_ParseTupleAndKeywordsFast(
args, kwds, &_parser, args, kwds, &_parser,
bpygpu_ParsePrimType, &params.type_id, bpygpu_ParsePrimType, &params.type_id,
&BPyGPUVertBuf_Type, &params.py_vertbuf, &BPyGPUVertBuf_Type, &params.py_vertbuf,

View File

@@ -47,6 +47,8 @@
static PyObject *bpygpu_IndexBuf_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds) static PyObject *bpygpu_IndexBuf_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
{ {
BPYGPU_IS_INIT_OR_ERROR_OBJ;
const char *error_prefix = "IndexBuf.__new__"; const char *error_prefix = "IndexBuf.__new__";
bool ok = true; 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 const char *_keywords[] = {"type", "seq", NULL};
static _PyArg_Parser _parser = {"$O&O:IndexBuf.__new__", _keywords, 0}; static _PyArg_Parser _parser = {"$O&O:IndexBuf.__new__", _keywords, 0};
if (!bpygpu_is_initialized() || if (!_PyArg_ParseTupleAndKeywordsFast(
!_PyArg_ParseTupleAndKeywordsFast(
args, kwds, &_parser, args, kwds, &_parser,
bpygpu_ParsePrimType, &params.type_id, bpygpu_ParsePrimType, &params.type_id,
&params.seq)) &params.seq))

View File

@@ -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) static PyObject *bpygpu_offscreen_new(PyTypeObject *UNUSED(self), PyObject *args, PyObject *kwds)
{ {
BPYGPU_IS_INIT_OR_ERROR_OBJ;
GPUOffScreen *ofs; GPUOffScreen *ofs;
int width, height, samples = 0; int width, height, samples = 0;
char err_out[256]; char err_out[256];
static const char *_keywords[] = {"width", "height", "samples", NULL}; static const char *_keywords[] = {"width", "height", "samples", NULL};
static _PyArg_Parser _parser = {"ii|i:GPUOffScreen.__new__", _keywords, 0}; static _PyArg_Parser _parser = {"ii|i:GPUOffScreen.__new__", _keywords, 0};
if (!bpygpu_is_initialized() || if (!_PyArg_ParseTupleAndKeywordsFast(
!_PyArg_ParseTupleAndKeywordsFast(
args, kwds, &_parser, args, kwds, &_parser,
&width, &height, &samples)) &width, &height, &samples))
{ {

View File

@@ -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) static PyObject *bpygpu_shader_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
{ {
BPYGPU_IS_INIT_OR_ERROR_OBJ;
struct { struct {
const char *vertexcode; const char *vertexcode;
const char *fragcode; const char *fragcode;
@@ -118,8 +120,7 @@ static PyObject *bpygpu_shader_new(PyTypeObject *UNUSED(type), PyObject *args, P
"libcode", "defines", NULL}; "libcode", "defines", NULL};
static _PyArg_Parser _parser = {"ss|$sss:GPUShader.__new__", _keywords, 0}; static _PyArg_Parser _parser = {"ss|$sss:GPUShader.__new__", _keywords, 0};
if (!bpygpu_is_initialized() || if (!_PyArg_ParseTupleAndKeywordsFast(
!_PyArg_ParseTupleAndKeywordsFast(
args, kwds, &_parser, args, kwds, &_parser,
&params.vertexcode, &params.fragcode, &params.geocode, &params.vertexcode, &params.fragcode, &params.geocode,
&params.libcode, &params.defines)) &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) static PyObject *bpygpu_shader_from_builtin(PyObject *UNUSED(self), PyObject *arg)
{ {
BPYGPU_IS_INIT_OR_ERROR_OBJ;
GPUBuiltinShader shader_id; GPUBuiltinShader shader_id;
if (!bpygpu_is_initialized() || if (!bpygpu_ParseBultinShaderEnum(arg, &shader_id)) {
!bpygpu_ParseBultinShaderEnum(arg, &shader_id))
{
return NULL; return NULL;
} }