39 PyConfig_InitPythonConfig(&config);
40 PyStatus status = PyConfig_SetBytesArgv(&config, argc,
const_cast<char *
const *
>(argv));
41 PyObject *py_argv =
nullptr;
42 if (PyStatus_Exception(status)) [[unlikely]] {
43 PyErr_Format(PyExc_ValueError,
"%s", status.err_msg);
47 py_argv = PyList_New(config.argv.length);
48 for (Py_ssize_t i = 0; i < config.argv.length; i++) {
49 PyList_SET_ITEM(py_argv, i, PyUnicode_FromWideChar(config.argv.items[i], -1));
52 PyConfig_Clear(&config);
67 PyGILState_STATE gilstate;
70 int exit_code = EXIT_FAILURE;
78 bool has_error =
false;
81 if (py_argv ==
nullptr) {
85 PyObject *exec_args = PyTuple_New(1);
86 PyTuple_SET_ITEM(exec_args, 0, py_argv);
88 PyObject *
result = PyObject_Call(py_exec_fn, exec_args,
nullptr);
95 if ((
result ==
nullptr) && PyErr_ExceptionMatches(PyExc_SystemExit)) {
96 PyObject *error_type, *error_value, *error_traceback;
97 PyErr_Fetch(&error_type, &error_value, &error_traceback);
98 if (PyObject_TypeCheck(error_value, (PyTypeObject *)PyExc_SystemExit) &&
99 ((
reinterpret_cast<PySystemExitObject *
>(error_value))->code !=
nullptr))
102 result = (
reinterpret_cast<PySystemExitObject *
>(error_value))->code;
109 PyErr_Restore(error_type, error_value, error_traceback);
117 if (!PyLong_Check(
result)) {
118 PyErr_Format(PyExc_TypeError,
119 "Expected an int return value, not a %.200s",
120 Py_TYPE(
result)->tp_name);
124 const int exit_code_test = PyC_Long_AsI32(
result);
125 if ((exit_code_test == -1) && PyErr_Occurred()) {
126 exit_code = EXIT_SUCCESS;
130 exit_code = exit_code_test;
150 PyGILState_STATE gilstate = PyGILState_Ensure();
151 Py_DECREF(py_exec_fn);
152 PyGILState_Release(gilstate);
189 bpy_cli_command_register_doc,
190 ".. function:: register_cli_command(id, execute)\n"
192 " Register a command, accessible via the (``-c`` / ``--command``) command-line argument.\n"
194 " :param id: The command identifier (must pass an ``str.isidentifier`` check).\n"
196 " If the ``id`` is already registered, a warning is printed and "
197 "the command is inaccessible to prevent accidents invoking the wrong command.\n"
199 " :param execute: Callback, taking a single list of strings and returns an int.\n"
200 " The arguments are built from all command-line arguments following the command id.\n"
201 " The return value should be 0 for success, 1 on failure "
202 "(specific error codes from the ``os`` module can also be used).\n"
203 " :type execute: Callable[[list[str]], int]\n"
204 " :return: The command handle which can be passed to :func:`unregister_cli_command`.\n"
206 " This uses Python's capsule type "
207 "however the result should be considered an opaque handle only used for unregistering.\n"
213 PyObject *py_exec_fn;
215 static const char *_keywords[] = {
220 static _PyArg_Parser _parser = {
223 ":register_cli_command",
227 if (!_PyArg_ParseTupleAndKeywordsFast(args, kw, &_parser, &PyUnicode_Type, &py_id, &py_exec_fn))
231 if (!PyUnicode_IsIdentifier(py_id)) {
232 PyErr_SetString(PyExc_ValueError,
"The command id is not a valid identifier");
235 if (!PyCallable_Check(py_exec_fn)) {
236 PyErr_SetString(PyExc_ValueError,
"The execute argument must be callable");
240 const char *
id = PyUnicode_AsUTF8(py_id);
242 std::unique_ptr<CommandHandler> cmd_ptr = std::make_unique<BPyCommandHandler>(
243 std::string(
id), Py_NewRef(py_exec_fn));
244 void *cmd_p = cmd_ptr.get();
253 bpy_cli_command_unregister_doc,
254 ".. function:: unregister_cli_command(handle)\n"
256 " Unregister a CLI command.\n"
258 " :param handle: The return value of :func:`register_cli_command`.\n"
260 " :type handle: Any\n");
263 if (!PyCapsule_CheckExact(value)) {
264 PyErr_Format(PyExc_TypeError,
265 "Expected a capsule returned from register_cli_command(...), found a: %.200s",
266 Py_TYPE(value)->tp_name);
272 if (cmd ==
nullptr) {
273 const char *capsule_name = PyCapsule_GetName(value);
275 PyErr_SetString(PyExc_ValueError,
"The command has already been removed");
278 PyErr_Format(PyExc_ValueError,
279 "Unrecognized capsule ID \"%.200s\"",
280 capsule_name ? capsule_name :
"<null>");
298# pragma clang diagnostic push
299# pragma clang diagnostic ignored "-Wcast-function-type"
301# pragma GCC diagnostic push
302# pragma GCC diagnostic ignored "-Wcast-function-type"
307 "register_cli_command",
309 METH_STATIC | METH_VARARGS | METH_KEYWORDS,
310 bpy_cli_command_register_doc,
313 "unregister_cli_command",
315 METH_STATIC | METH_O,
316 bpy_cli_command_unregister_doc,
321# pragma clang diagnostic pop
323# pragma GCC diagnostic pop
Blender CLI Generic --command Support.
~BPyCommandHandler() override
BPyCommandHandler(const std::string &id, PyObject *py_exec_fn)
int exec(bContext *C, int argc, const char **argv) override
CommandHandler(const std::string &id)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
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")
static PyObject * bpy_cli_command_unregister(PyObject *, PyObject *value)
static int bpy_cli_command_exec(bContext *C, PyObject *py_exec_fn, const int argc, const char **argv)
void BKE_blender_cli_command_register(std::unique_ptr< CommandHandler > cmd)
PyMethodDef BPY_cli_command_register_def
static const char * bpy_cli_command_capsule_name_invalid
static void bpy_cli_command_free(PyObject *py_exec_fn)
bool bpy_context_set(struct bContext *C, PyGILState_STATE *gilstate) ATTR_NONNULL(1)
static const char * bpy_cli_command_capsule_name
static PyObject * bpy_cli_command_register(PyObject *, PyObject *args, PyObject *kw)
void bpy_context_clear(struct bContext *C, const PyGILState_STATE *gilstate)
PyMethodDef BPY_cli_command_unregister_def
static PyObject * py_argv_from_bytes(const int argc, const char **argv)
bool BKE_blender_cli_command_unregister(CommandHandler *cmd)
header-only compatibility defines.