Files
test2/source/blender/python/intern/bpy_rna_text.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

208 lines
5.8 KiB
C++
Raw Normal View History

/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup pythonintern
*
* This file extends the text editor with C/Python API methods and attributes.
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "MEM_guardedalloc.h"
#include "WM_api.hh"
#include "BKE_text.h"
#include "../generic/python_compat.hh"
#include "bpy_rna.hh"
#include "bpy_rna_text.hh" /* Declare #BPY_rna_region_as_string_method_def. */
/* -------------------------------------------------------------------- */
/** \name Data structures.
* \{ */
/**
* Struct representing a selection which is extracted from Python arguments.
*/
struct TextRegion {
int curl;
int curc;
int sell;
int selc;
};
/** \} */
/* -------------------------------------------------------------------- */
/** \name Text Editor Get / Set region text API
* \{ */
PyDoc_STRVAR(
/* Wrap. */
bpy_rna_region_as_string_doc,
".. method:: region_as_string(range=None)\n"
"\n"
" :arg range: The region of text to be returned, "
"defaulting to the selection when no range is passed.\n"
" Each int pair represents a line and column: "
"((start_line, start_column), (end_line, end_column))\n"
" The values match Python's slicing logic "
"(negative values count backwards from the end, the end value is not inclusive).\n"
" :type range: tuple[tuple[int, int], tuple[int, int]]\n"
" :return: The specified region as a string.\n"
" :rtype: str.\n");
/* Receive a Python Tuple as parameter to represent the region range. */
static PyObject *bpy_rna_region_as_string(PyObject *self, PyObject *args, PyObject *kwds)
{
BPy_StructRNA *pyrna = (BPy_StructRNA *)self;
RNA: Make the `PointerRNA` struct non-trivial. For now, PointerRNA is made non-trivial by giving explicit default values to its members. Besides of BPY python binding code, the change is relatively trivial. The main change (besides the creation/deletion part) is the replacement of `memset` by zero-initialized assignment (using `{}`). makesrna required changes are quite small too. The big piece of this PR is the refactor of the BPY RNA code. It essentially brings back allocation and deletion of the BPy_StructRNA, BPy_Pointer etc. python objects into 'cannonical process', using `__new__`, and `__init__` callbacks (and there matching CAPI functions). Existing code was doing very low-level manipulations to create these data, which is not really easy to understand, and AFAICT incompatible with handling C++ data that needs to be constructed and destructed. Unfortunately, similar change in destruction code (using `__del__` and matching `tp_finalize` CAPI callback) is not possible, because of technical low-level implementation details in CPython (see [1] for details). `std::optional` pointer management is used to encapsulate PointerRNA data. This allows to keep control on _when_ actual RNA creation is done, and to have a safe destruction in `tp_dealloc` callbacks. Note that a critical change in Blender's Python API will be that classes inherinting from `bpy_struct` etc. will now have to properly call the base class `__new__` and/or `__init__`if they define them. Implements #122431. [1] https://discuss.python.org/t/cpython-usage-of-tp-finalize-in-c-defined-static-types-with-no-custom-tp-dealloc/64100
2024-10-30 15:08:37 +01:00
Text *text = static_cast<Text *>(pyrna->ptr->data);
/* Parse the region range. */
TextRegion region;
static const char *_keywords[] = {"range", nullptr};
static _PyArg_Parser _parser = {
PY_ARG_PARSER_HEAD_COMPAT()
"|$" /* Optional keyword only arguments. */
"((ii)(ii))" /* `range` */
":region_as_string",
_keywords,
2023-08-03 19:14:53 +10:00
nullptr,
};
if (!_PyArg_ParseTupleAndKeywordsFast(
args, kwds, &_parser, &region.curl, &region.curc, &region.sell, &region.selc))
{
return nullptr;
}
if (kwds && PyDict_GET_SIZE(kwds) > 0) {
txt_sel_set(text, region.curl, region.curc, region.sell, region.selc);
}
/* Return an empty string if there is no selection. */
if (!txt_has_sel(text)) {
return PyUnicode_FromString("");
}
char *buf = txt_sel_to_buf(text, nullptr);
PyObject *sel_text = PyUnicode_FromString(buf);
MEM_freeN(buf);
/* Return the selected text. */
return sel_text;
}
#ifdef __GNUC__
# ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wcast-function-type"
# else
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wcast-function-type"
# endif
#endif
PyMethodDef BPY_rna_region_as_string_method_def = {
"region_as_string",
(PyCFunction)bpy_rna_region_as_string,
METH_VARARGS | METH_KEYWORDS,
bpy_rna_region_as_string_doc,
};
#ifdef __GNUC__
# ifdef __clang__
# pragma clang diagnostic pop
# else
# pragma GCC diagnostic pop
# endif
#endif
PyDoc_STRVAR(
/* Wrap. */
bpy_rna_region_from_string_doc,
".. method:: region_from_string(body, range=None)\n"
"\n"
" :arg body: The text to be inserted.\n"
" :type body: str\n"
" :arg range: The region of text to be returned, "
"defaulting to the selection when no range is passed.\n"
" Each int pair represents a line and column: "
"((start_line, start_column), (end_line, end_column))\n"
" The values match Python's slicing logic "
"(negative values count backwards from the end, the end value is not inclusive).\n"
" :type range: tuple[tuple[int, int], tuple[int, int]]\n");
static PyObject *bpy_rna_region_from_string(PyObject *self, PyObject *args, PyObject *kwds)
{
BPy_StructRNA *pyrna = (BPy_StructRNA *)self;
RNA: Make the `PointerRNA` struct non-trivial. For now, PointerRNA is made non-trivial by giving explicit default values to its members. Besides of BPY python binding code, the change is relatively trivial. The main change (besides the creation/deletion part) is the replacement of `memset` by zero-initialized assignment (using `{}`). makesrna required changes are quite small too. The big piece of this PR is the refactor of the BPY RNA code. It essentially brings back allocation and deletion of the BPy_StructRNA, BPy_Pointer etc. python objects into 'cannonical process', using `__new__`, and `__init__` callbacks (and there matching CAPI functions). Existing code was doing very low-level manipulations to create these data, which is not really easy to understand, and AFAICT incompatible with handling C++ data that needs to be constructed and destructed. Unfortunately, similar change in destruction code (using `__del__` and matching `tp_finalize` CAPI callback) is not possible, because of technical low-level implementation details in CPython (see [1] for details). `std::optional` pointer management is used to encapsulate PointerRNA data. This allows to keep control on _when_ actual RNA creation is done, and to have a safe destruction in `tp_dealloc` callbacks. Note that a critical change in Blender's Python API will be that classes inherinting from `bpy_struct` etc. will now have to properly call the base class `__new__` and/or `__init__`if they define them. Implements #122431. [1] https://discuss.python.org/t/cpython-usage-of-tp-finalize-in-c-defined-static-types-with-no-custom-tp-dealloc/64100
2024-10-30 15:08:37 +01:00
Text *text = static_cast<Text *>(pyrna->ptr->data);
/* Parse the region range. */
const char *buf;
Py_ssize_t buf_len;
TextRegion region;
static const char *_keywords[] = {"", "range", nullptr};
static _PyArg_Parser _parser = {
PY_ARG_PARSER_HEAD_COMPAT()
"s#" /* `buf` (positional). */
"|$" /* Optional keyword only arguments. */
"((ii)(ii))" /* `range` */
":region_from_string",
_keywords,
2023-08-03 19:14:53 +10:00
nullptr,
};
if (!_PyArg_ParseTupleAndKeywordsFast(args,
kwds,
&_parser,
&buf,
&buf_len,
&region.curl,
&region.curc,
&region.sell,
&region.selc))
{
return nullptr;
}
if (kwds && PyDict_GET_SIZE(kwds) > 0) {
txt_sel_set(text, region.curl, region.curc, region.sell, region.selc);
}
/* Set the selected text. */
txt_insert_buf(text, buf, buf_len);
/* Update the text editor. */
WM_main_add_notifier(NC_TEXT | NA_EDITED, text);
Py_RETURN_NONE;
}
#ifdef __GNUC__
# ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wcast-function-type"
# else
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wcast-function-type"
# endif
#endif
PyMethodDef BPY_rna_region_from_string_method_def = {
"region_from_string",
(PyCFunction)bpy_rna_region_from_string,
METH_VARARGS | METH_KEYWORDS,
bpy_rna_region_from_string_doc,
};
#ifdef __GNUC__
# ifdef __clang__
# pragma clang diagnostic pop
# else
# pragma GCC diagnostic pop
# endif
#endif
/** \} */