Blender  V3.3
bpy_rna_operator.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
9 #include <Python.h>
10 
11 #include "BLI_string.h"
12 
13 #include "BKE_context.h"
14 
15 #include "../generic/python_utildefines.h"
16 
17 #include "BPY_extern.h"
18 #include "bpy_capi_utils.h"
19 
20 #include "bpy_rna_operator.h" /* Own include. */
21 
22 /* -------------------------------------------------------------------- */
27 {
28  PyGILState_STATE gilstate = PyGILState_Ensure();
29 
30  PyObject *py_args = user_data;
31  PyObject *py_func_or_msg = PyTuple_GET_ITEM(py_args, 0);
32 
33  if (PyUnicode_Check(py_func_or_msg)) {
34  return BLI_strdup(PyUnicode_AsUTF8(py_func_or_msg));
35  }
36 
37  PyObject *py_args_after_first = PyTuple_GetSlice(py_args, 1, PY_SSIZE_T_MAX);
38  PyObject *py_msg = PyObject_CallObject(py_func_or_msg, py_args_after_first);
39  Py_DECREF(py_args_after_first);
40 
41  char *msg = NULL;
42  bool error = false;
43 
44  /* NULL for no string. */
45  if (py_msg == NULL) {
46  error = true;
47  }
48  else {
49  if (py_msg == Py_None) {
50  /* pass */
51  }
52  else if (PyUnicode_Check(py_msg)) {
53  msg = BLI_strdup(PyUnicode_AsUTF8(py_msg));
54  }
55  else {
56  PyErr_Format(PyExc_TypeError,
57  "poll_message_set(function, ...): expected string or None, got %.200s",
58  Py_TYPE(py_msg)->tp_name);
59  error = true;
60  }
61  Py_DECREF(py_msg);
62  }
63 
64  if (error) {
65  PyErr_Print();
66  PyErr_Clear();
67  }
68 
69  PyGILState_Release(gilstate);
70  return msg;
71 }
72 
74 {
75  /* Handles the GIL. */
77 }
78 
79 PyDoc_STRVAR(BPY_rna_operator_poll_message_set_doc,
80  ".. method:: poll_message_set(message, *args)\n"
81  "\n"
82  " Set the message to show in the tool-tip when poll fails.\n"
83  "\n"
84  " When message is callable, "
85  "additional user defined positional arguments are passed to the message function.\n"
86  "\n"
87  " :param message: The message or a function that returns the message.\n"
88  " :type message: string or a callable that returns a string or None.\n");
89 
90 static PyObject *BPY_rna_operator_poll_message_set(PyObject *UNUSED(self), PyObject *args)
91 {
92  const Py_ssize_t args_len = PyTuple_GET_SIZE(args);
93  if (args_len == 0) {
94  PyErr_SetString(PyExc_ValueError,
95  "poll_message_set(message, ...): requires a message argument");
96  return NULL;
97  }
98 
99  PyObject *py_func_or_msg = PyTuple_GET_ITEM(args, 0);
100 
101  if (PyUnicode_Check(py_func_or_msg)) {
102  if (args_len > 1) {
103  PyErr_SetString(PyExc_ValueError,
104  "poll_message_set(message): does not support additional arguments");
105  return NULL;
106  }
107  }
108  else if (PyCallable_Check(py_func_or_msg)) {
109  /* pass */
110  }
111  else {
112  PyErr_Format(PyExc_TypeError,
113  "poll_message_set(message, ...): "
114  "expected at least 1 string or callable argument, got %.200s",
115  Py_TYPE(py_func_or_msg)->tp_name);
116  return NULL;
117  }
118 
121  .get_fn = pyop_poll_message_get_fn,
122  .free_fn = pyop_poll_message_free_fn,
123  .user_data = Py_INCREF_RET(args),
124  };
125 
127 
128  Py_RETURN_NONE;
129 }
130 
132  "poll_message_set",
134  METH_VARARGS | METH_STATIC,
135  BPY_rna_operator_poll_message_set_doc,
136 };
137 
void CTX_wm_operator_poll_msg_set_dynamic(bContext *C, const struct bContextPollMsgDyn_Params *params)
Definition: context.c:1049
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:42
#define UNUSED(x)
void BPY_DECREF(void *pyob_ptr)
#define C
Definition: RandGen.cpp:25
struct bContext * BPY_context_get(void)
static void pyop_poll_message_free_fn(bContext *UNUSED(C), void *user_data)
static PyObject * BPY_rna_operator_poll_message_set(PyObject *UNUSED(self), PyObject *args)
PyDoc_STRVAR(BPY_rna_operator_poll_message_set_doc, ".. method:: 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" " :param message: The message or a function that returns the message.\n" " :type message: string or a callable that returns a string or None.\n")
PyMethodDef BPY_rna_operator_poll_message_set_method_def
static char * pyop_poll_message_get_fn(bContext *UNUSED(C), void *user_data)
void * user_data
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
static void error(const char *str)
Definition: meshlaplacian.c:51