Blender V5.0
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.h"
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
22/* -------------------------------------------------------------------- */
25
26static char *pyop_poll_message_get_fn(bContext * /*C*/, void *user_data)
27{
28 PyGILState_STATE gilstate = PyGILState_Ensure();
29
30 PyObject *py_args = static_cast<PyObject *>(user_data);
31 PyObject *py_func_or_msg = PyTuple_GET_ITEM(py_args, 0);
32
33 char *msg = nullptr;
34
35 if (PyUnicode_Check(py_func_or_msg)) {
36 Py_ssize_t msg_len;
37 const char *msg_src = PyUnicode_AsUTF8AndSize(py_func_or_msg, &msg_len);
38 msg = BLI_strdupn(msg_src, msg_len);
39 }
40 else {
41 PyObject *py_args_after_first = PyTuple_GetSlice(py_args, 1, PY_SSIZE_T_MAX);
42 PyObject *py_msg = PyObject_CallObject(py_func_or_msg, py_args_after_first);
43 Py_DECREF(py_args_after_first);
44
45 bool error = false;
46
47 /* Null for no string. */
48 if (py_msg == nullptr) {
49 error = true;
50 }
51 else {
52 if (py_msg == Py_None) {
53 /* Pass. */
54 }
55 else if (PyUnicode_Check(py_msg)) {
56 Py_ssize_t msg_src_len;
57 const char *msg_src = PyUnicode_AsUTF8AndSize(py_msg, &msg_src_len);
58 msg = BLI_strdupn(msg_src, msg_src_len);
59 }
60 else {
61 PyErr_Format(PyExc_TypeError,
62 "poll_message_set(function, ...): expected string or None, got %.200s",
63 Py_TYPE(py_msg)->tp_name);
64 error = true;
65 }
66 Py_DECREF(py_msg);
67 }
68
69 if (error) {
70 PyErr_Print();
71 }
72 }
73
74 PyGILState_Release(gilstate);
75 return msg;
76}
77
78static void pyop_poll_message_free_fn(bContext * /*C*/, void *user_data)
79{
80 /* Handles the GIL. */
81 BPY_DECREF(user_data);
82}
83
85 /* Wrap. */
86 BPY_rna_operator_poll_message_set_doc,
87 ".. classmethod:: poll_message_set(message, *args)\n"
88 "\n"
89 " Set the message to show in the tool-tip when poll fails.\n"
90 "\n"
91 " When message is callable, "
92 "additional user defined positional arguments are passed to the message function.\n"
93 "\n"
94 " :arg message: The message or a function that returns the message.\n"
95 " :type message: str | Callable[..., str | None]\n"
96 " :arg args: A sequence of arguments to pass to ``message``, if it's a callable, "
97 "otherwise argument is not available.\n"
98 " :type args: Any\n");
99static PyObject *BPY_rna_operator_poll_message_set(PyObject * /*self*/, PyObject *args)
100{
101 const Py_ssize_t args_len = PyTuple_GET_SIZE(args);
102 if (args_len == 0) {
103 PyErr_SetString(PyExc_ValueError,
104 "poll_message_set(message, ...): requires a message argument");
105 return nullptr;
106 }
107
108 PyObject *py_func_or_msg = PyTuple_GET_ITEM(args, 0);
109
110 if (PyUnicode_Check(py_func_or_msg)) {
111 if (args_len > 1) {
112 PyErr_SetString(PyExc_ValueError,
113 "poll_message_set(message): does not support additional arguments");
114 return nullptr;
115 }
116 }
117 else if (PyCallable_Check(py_func_or_msg)) {
118 /* pass */
119 }
120 else {
121 PyErr_Format(PyExc_TypeError,
122 "poll_message_set(message, ...): "
123 "expected at least 1 string or callable argument, got %.200s",
124 Py_TYPE(py_func_or_msg)->tp_name);
125 return nullptr;
126 }
127
132 params.user_data = Py_NewRef(args);
133
135
136 Py_RETURN_NONE;
137}
138
140 "poll_message_set",
142 METH_VARARGS | METH_STATIC,
143 BPY_rna_operator_poll_message_set_doc,
144};
145
void CTX_wm_operator_poll_msg_set_dynamic(bContext *C, const bContextPollMsgDyn_Params *params)
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition string.cc:30
void BPY_DECREF(void *pyob_ptr)
#define C
Definition RandGen.cpp:29
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)
PyDoc_STRVAR(BPY_rna_operator_poll_message_set_doc, ".. classmethod:: poll_message_set(message, *args)\n" "\n" " Set the message to show in the tool-tip when poll fails.\n" "\n" " When message is callable, " "additional user defined positional arguments are passed to the message function.\n" "\n" " :arg message: The message or a function that returns the message.\n" " :type message: str | Callable[..., str | None]\n" " :arg args: A sequence of arguments to pass to ``message``, if it's a callable, " "otherwise argument is not available.\n" " :type args: Any\n")
PyMethodDef BPY_rna_operator_poll_message_set_method_def
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
static void error(const char *str)
Py_DECREF(oname)