Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2017-08-20 08:39:08 +0300
committerCampbell Barton <ideasman42@gmail.com>2017-08-20 08:39:08 +0300
commita10a7f42de29d251e27f6b85e7f3cac26e6843ed (patch)
treed831f64eac4e372ced2bbbbc448f0b06f3d360c6 /source/blender/python/generic/py_capi_utils.h
parent2ff9c8a3bc51142a1d7ad19d8b772b58e4dcc890 (diff)
PyAPI: Integer conversion functions
Python's C-API doesn't provide functions to get int's at specific integer sizes. Leaving the caller to check for overflow, which ended up being ignored in practice. Add API functions that convert int/uint 8/16/32/64, also bool. Raising overflow exception for unsupported ranges.
Diffstat (limited to 'source/blender/python/generic/py_capi_utils.h')
-rw-r--r--source/blender/python/generic/py_capi_utils.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h
index 3f89e1d82a0..322e67f486d 100644
--- a/source/blender/python/generic/py_capi_utils.h
+++ b/source/blender/python/generic/py_capi_utils.h
@@ -85,4 +85,26 @@ bool PyC_RunString_AsString(const char *expr, const char *filename, char **r_val
int PyC_ParseBool(PyObject *o, void *p);
+
+/* Integer parsing (with overflow checks), -1 on error. */
+int PyC_Long_AsBool(PyObject *value);
+int8_t PyC_Long_AsI8(PyObject *value);
+int16_t PyC_Long_AsI16(PyObject *value);
+#if 0 /* inline */
+int32_t PyC_Long_AsI32(PyObject *value);
+int64_t PyC_Long_AsI64(PyObject *value);
+#endif
+
+uint8_t PyC_Long_AsU8(PyObject *value);
+uint16_t PyC_Long_AsU16(PyObject *value);
+uint32_t PyC_Long_AsU32(PyObject *value);
+#if 0 /* inline */
+uint64_t PyC_Long_AsU64(PyObject *value);
+#endif
+
+/* inline so type signatures match as expected */
+BLI_INLINE int32_t PyC_Long_AsI32(PyObject *value) { return (int32_t)_PyLong_AsInt(value); }
+BLI_INLINE int64_t PyC_Long_AsI64(PyObject *value) { return (int64_t)PyLong_AsLongLong(value); }
+BLI_INLINE uint64_t PyC_Long_AsU64(PyObject *value) { return (uint64_t)PyLong_AsUnsignedLongLong(value); }
+
#endif /* __PY_CAPI_UTILS_H__ */