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>2019-09-30 21:09:33 +0300
committerCampbell Barton <ideasman42@gmail.com>2019-09-30 22:18:25 +0300
commit07feb6e81441b084775e7cb7a042fc19c8c126d4 (patch)
treeec5ac3b0bcf6e5e07916e2ec39cca6d81a0f9ffe /source/blender/python
parentddb157999eed6907e3fd8753d61e65491f2f3a6d (diff)
PyC API: utility to parse a string as an RNA like enum
More convenient than comparing strings, with the advantage that errors don't need to repeat expected values.
Diffstat (limited to 'source/blender/python')
-rw-r--r--source/blender/python/generic/py_capi_utils.c31
-rw-r--r--source/blender/python/generic/py_capi_utils.h11
2 files changed, 42 insertions, 0 deletions
diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c
index ca9f15ed01a..a6983c38f0e 100644
--- a/source/blender/python/generic/py_capi_utils.c
+++ b/source/blender/python/generic/py_capi_utils.c
@@ -231,6 +231,37 @@ int PyC_ParseBool(PyObject *o, void *p)
return 1;
}
+/**
+ * Use with PyArg_ParseTuple's "O&" formatting.
+ */
+int PyC_ParseStringEnum(PyObject *o, void *p)
+{
+ struct PyC_StringEnum *e = p;
+ const char *value = _PyUnicode_AsString(o);
+ if (value == NULL) {
+ PyErr_Format(PyExc_ValueError, "expected a string, got %s", Py_TYPE(o)->tp_name);
+ return 0;
+ }
+ int i;
+ for (i = 0; e->items[i].id; i++) {
+ if (STREQ(e->items[i].id, value)) {
+ e->value_found = e->items[i].value;
+ return 1;
+ }
+ }
+
+ /* Set as a precaution. */
+ e->value_found = -1;
+
+ PyObject *enum_items = PyTuple_New(i);
+ for (i = 0; e->items[i].id; i++) {
+ PyTuple_SET_ITEM(enum_items, i, PyUnicode_FromString(e->items[i].id));
+ }
+ PyErr_Format(PyExc_ValueError, "expected a string in %S, got '%s'", enum_items, value);
+ Py_DECREF(enum_items);
+ return 0;
+}
+
/* silly function, we dont use arg. just check its compatible with __deepcopy__ */
int PyC_CheckArgs_DeepCopy(PyObject *args)
{
diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h
index 4454aa11d77..1f552c3d78d 100644
--- a/source/blender/python/generic/py_capi_utils.h
+++ b/source/blender/python/generic/py_capi_utils.h
@@ -126,6 +126,17 @@ bool PyC_RunString_AsString(const char **imports,
int PyC_ParseBool(PyObject *o, void *p);
+struct PyC_StringEnumItems {
+ int value;
+ const char *id;
+};
+struct PyC_StringEnum {
+ const struct PyC_StringEnumItems *items;
+ int value_found;
+};
+
+int PyC_ParseStringEnum(PyObject *o, void *p);
+
int PyC_CheckArgs_DeepCopy(PyObject *args);
/* Integer parsing (with overflow checks), -1 on error. */