Blender V5.3
bpy_rna_operator.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <Python.h>
12
13#include "BLI_string.hh"
14
15#include "BKE_context.hh"
16
17#include "BPY_extern.hh"
18#include "bpy_capi_utils.hh"
19
20#include "bpy_rna_operator.hh" /* Own include, #BPY_rna_operator_poll_message_set_method_def. */
21
22namespace blender {
23
24/* -------------------------------------------------------------------- */
27
28static char *pyop_poll_message_get_fn(bContext * /*C*/, void *user_data)
29{
30 PyGILState_STATE gilstate = PyGILState_Ensure();
31
32 PyObject *py_args = static_cast<PyObject *>(user_data);
33 PyObject *py_func_or_msg = PyTuple_GET_ITEM(py_args, 0);
34
35 char *msg = nullptr;
36
37 if (PyUnicode_Check(py_func_or_msg)) {
38 Py_ssize_t msg_len;
39 const char *msg_src = PyUnicode_AsUTF8AndSize(py_func_or_msg, &msg_len);
40 msg = BLI_strdupn(msg_src, msg_len);
41 }
42 else {
43 PyObject *py_args_after_first = PyTuple_GetSlice(py_args, 1, PY_SSIZE_T_MAX);
44 PyObject *py_msg = PyObject_CallObject(py_func_or_msg, py_args_after_first);
45 Py_DECREF(py_args_after_first);
46
47 bool error = false;
48
49 /* Null for no string. */
50 if (py_msg == nullptr) {
51 error = true;
52 }
53 else {
54 if (py_msg == Py_None) {
55 /* Pass. */
56 }
57 else if (PyUnicode_Check(py_msg)) {
58 Py_ssize_t msg_src_len;
59 const char *msg_src = PyUnicode_AsUTF8AndSize(py_msg, &msg_src_len);
60 msg = BLI_strdupn(msg_src, msg_src_len);
61 }
62 else {
63 PyErr_Format(PyExc_TypeError,
64 "poll_message_set(function, ...): expected string or None, got %.200s",
65 Py_TYPE(py_msg)->tp_name);
66 error = true;
67 }
68 Py_DECREF(py_msg);
69 }
70
71 if (error) {
72 PyErr_Print();
73 }
74 }
75
76 PyGILState_Release(gilstate);
77 return msg;
78}
79
80static void pyop_poll_message_free_fn(bContext * /*C*/, void *user_data)
81{
82 /* Handles the GIL. */
83 BPY_DECREF(user_data);
84}
85
87 /* Wrap. */
88 BPY_rna_operator_poll_message_set_doc,
89 ".. classmethod:: poll_message_set(message, *args)\n"
90 "\n"
91 " Set the message to show in the tool-tip when poll fails.\n"
92 "\n"
93 " When message is callable, "
94 "additional user defined positional arguments are passed to the message function.\n"
95 "\n"
96 " :param message: The message or a function that returns the message.\n"
97 " :type message: str | Callable[..., str | None]\n"
98 " :param args: A sequence of arguments to pass to ``message``, if it's a callable, "
99 "otherwise argument is not available.\n"
100 " :type args: Any\n");
101static PyObject *BPY_rna_operator_poll_message_set(PyObject * /*self*/, PyObject *args)
102{
103 const Py_ssize_t args_len = PyTuple_GET_SIZE(args);
104 if (args_len == 0) {
105 PyErr_SetString(PyExc_ValueError,
106 "poll_message_set(message, ...): requires a message argument");
107 return nullptr;
108 }
109
110 PyObject *py_func_or_msg = PyTuple_GET_ITEM(args, 0);
111
112 if (PyUnicode_Check(py_func_or_msg)) {
113 if (args_len > 1) {
114 PyErr_SetString(PyExc_ValueError,
115 "poll_message_set(message): does not support additional arguments");
116 return nullptr;
117 }
118 }
119 else if (PyCallable_Check(py_func_or_msg)) {
120 /* pass */
121 }
122 else {
123 PyErr_Format(PyExc_TypeError,
124 "poll_message_set(message, ...): "
125 "expected at least 1 string or callable argument, got %.200s",
126 Py_TYPE(py_func_or_msg)->tp_name);
127 return nullptr;
128 }
129
134 params.user_data = Py_NewRef(args);
135
137
138 Py_RETURN_NONE;
139}
140
142 "poll_message_set",
143 static_cast<PyCFunction>(BPY_rna_operator_poll_message_set),
144 METH_VARARGS | METH_STATIC,
145 BPY_rna_operator_poll_message_set_doc,
146};
147
149
150} // namespace blender
#define C
Definition RandGen.cpp:29
BMesh Iterators.
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
PyMethodDef BPY_rna_operator_poll_message_set_method_def
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BPY_DECREF(void *pyob_ptr)
struct bContext * BPY_context_get()
static char * pyop_poll_message_get_fn(bContext *, void *user_data)
static PyObject * BPY_rna_operator_poll_message_set(PyObject *, PyObject *args)
static void pyop_poll_message_free_fn(bContext *, void *user_data)
void CTX_wm_operator_poll_msg_set_dynamic(bContext *C, const bContextPollMsgDyn_Params *params)
char *(*) get_fn(bContext *C, void *user_data)
void(*) free_fn(bContext *C, void *user_data)