Cleanup: use the naming convention in py_capi_utils

And use inline functions instead of preprocessor directives.
This commit is contained in:
Germano Cavalcante 2018-10-06 01:15:15 -03:00
parent 0f55334413
commit 495a7128cb
3 changed files with 75 additions and 17 deletions

View File

@ -480,14 +480,14 @@ int BGL_typeSize(int type)
static int gl_buffer_type_from_py_buffer(Py_buffer *pybuffer)
{
const char format = FORMAT_STR_GET(pybuffer->format);
const char format = PyC_Formatstr_get(pybuffer->format);
Py_ssize_t itemsize = pybuffer->itemsize;
if (FORMAT_STR_IS_FLOAT(format)) {
if (PyC_Formatstr_is_float(format)) {
if (itemsize == 4) return GL_FLOAT;
if (itemsize == 8) return GL_DOUBLE;
}
if (FORMAT_STR_IS_BYTE(format) || FORMAT_STR_IS_INT(format)) {
if (PyC_Formatstr_is_byte(format) || PyC_Formatstr_is_int(format)) {
if (itemsize == 1) return GL_BYTE;
if (itemsize == 2) return GL_SHORT;
if (itemsize == 4) return GL_INT;

View File

@ -132,10 +132,74 @@ Py_LOCAL_INLINE(int64_t) PyC_Long_AsI64(PyObject *value) { return (int64_t)PyLo
Py_LOCAL_INLINE(uint64_t) PyC_Long_AsU64(PyObject *value) { return (uint64_t)PyLong_AsUnsignedLongLong(value); }
/* utils for format string in `struct` module style syntax */
#define FORMAT_STR_GET(typestr) ELEM(typestr[0], '!', '<', '=', '>', '@') ? typestr[1] : typestr[0]
#define FORMAT_STR_IS_FLOAT(format) ELEM(format, 'f', 'd', 'e')
#define FORMAT_STR_IS_INT(format) ELEM(format, 'i', 'I', 'l', 'L', 'h', 'H', 'b', 'B', 'q', 'Q', 'n', 'N', 'P')
#define FORMAT_STR_IS_BYTE(format) ELEM(format, 'c', 's', 'p')
#define FORMAT_STR_IS_BOOL(format) ELEM(format, '?')
Py_LOCAL_INLINE(char) PyC_Formatstr_get(char *typestr)
{
switch (typestr[0]) {
case '!':
case '<':
case '=':
case '>':
case '@':
return typestr[1];
default:
return typestr[0];
}
}
Py_LOCAL_INLINE(bool) PyC_Formatstr_is_float(char format)
{
switch (format) {
case 'f':
case 'd':
case 'e':
return true;
default:
return false;
}
}
Py_LOCAL_INLINE(bool) PyC_Formatstr_is_int(char format)
{
switch (format) {
case 'i':
case 'I':
case 'l':
case 'L':
case 'h':
case 'H':
case 'b':
case 'B':
case 'q':
case 'Q':
case 'n':
case 'N':
case 'P':
return true;
default:
return false;
}
}
Py_LOCAL_INLINE(bool) PyC_Formatstr_is_byte(char format)
{
switch (format) {
case 'c':
case 's':
case 'p':
return true;
default:
return false;
}
}
Py_LOCAL_INLINE(bool) PyC_Formatstr_is_bool(char format)
{
switch (format) {
case '?':
return true;
default:
return false;
}
}
#endif /* __PY_CAPI_UTILS_H__ */

View File

@ -91,17 +91,11 @@ static PyObject *bpygpu_IndexBuf_new(PyTypeObject *UNUSED(type), PyObject *args,
return NULL;
}
bool format_error = pybuffer.itemsize != 4;
if (pybuffer.itemsize != 4 ||
PyC_Formatstr_is_float(PyC_Formatstr_get(pybuffer.format)))
{
char format = FORMAT_STR_GET(pybuffer.format);
if (FORMAT_STR_IS_FLOAT(format)) {
format_error = true;
}
}
if (format_error) {
PyErr_Format(PyExc_ValueError,
"Each index must be an integer value with 4 bytes in size");
"Each index must be an 4-bytes integer value");
return NULL;
}