From eb2cc80cd03d39c156e800c6cc72a028c1f5e895 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 10 Aug 2025 11:43:23 +1000 Subject: [PATCH] PyAPI: move bpy_types.py to a private module Use an underscore prefix as this module should not be accessed directly. --- doc/python_api/sphinx_doc_gen.py | 2 +- .../modules/{bpy_types.py => _bpy_types.py} | 0 .../bl_i18n_utils/bl_extract_messages.py | 2 +- scripts/modules/bl_i18n_utils/settings.py | 2 +- scripts/modules/bpy/utils/__init__.py | 2 +- scripts/modules/rna_info.py | 2 +- source/blender/python/intern/bpy.cc | 10 +++++----- source/blender/python/intern/bpy_rna.cc | 18 +++++++++--------- .../python/intern/bpy_rna_types_capi.cc | 2 +- 9 files changed, 20 insertions(+), 20 deletions(-) rename scripts/modules/{bpy_types.py => _bpy_types.py} (100%) diff --git a/doc/python_api/sphinx_doc_gen.py b/doc/python_api/sphinx_doc_gen.py index 5b41109d3d8..906a0b05e2a 100644 --- a/doc/python_api/sphinx_doc_gen.py +++ b/doc/python_api/sphinx_doc_gen.py @@ -791,7 +791,7 @@ def pyfunc2sphinx(ident, fw, module_name, type_name, identifier, py_func, is_cla # would be listed in documentation which isn't useful. # # However, excluding all of them is also incorrect as it means class methods defined - # in `bpy_types.py` for example are excluded, making some utility functions entirely hidden. + # in `_bpy_types.py` for example are excluded, making some utility functions entirely hidden. if (bl_rna := getattr(py_func.__self__, "bl_rna", None)) is not None: if bl_rna.functions.get(identifier) is not None: return diff --git a/scripts/modules/bpy_types.py b/scripts/modules/_bpy_types.py similarity index 100% rename from scripts/modules/bpy_types.py rename to scripts/modules/_bpy_types.py diff --git a/scripts/modules/bl_i18n_utils/bl_extract_messages.py b/scripts/modules/bl_i18n_utils/bl_extract_messages.py index 3931f6551bc..94815fadcc7 100644 --- a/scripts/modules/bl_i18n_utils/bl_extract_messages.py +++ b/scripts/modules/bl_i18n_utils/bl_extract_messages.py @@ -405,7 +405,7 @@ def dump_rna_messages(msgs, reports, settings, verbose=False): return cls.__name__ cls_id = "" bl_rna = getattr(cls, "bl_rna", None) - # It seems that py-defined 'wrappers' RNA classes (like `MeshEdge` in `bpy_types.py`) need to be accessed + # It seems that py-defined 'wrappers' RNA classes (like `MeshEdge` in `_bpy_types.py`) need to be accessed # once from `bpy.types` before they have a valid `bl_rna` member. # Weirdly enough, this is only triggered on release builds, debug builds somehow do not have that issue. if bl_rna is None: diff --git a/scripts/modules/bl_i18n_utils/settings.py b/scripts/modules/bl_i18n_utils/settings.py index e1fa44130cb..73acc6cf0d3 100644 --- a/scripts/modules/bl_i18n_utils/settings.py +++ b/scripts/modules/bl_i18n_utils/settings.py @@ -649,7 +649,7 @@ CUSTOM_PY_UI_FILES = [ os.path.join("scripts", "startup", "bl_operators"), os.path.join("scripts", "modules", "rna_prop_ui.py"), os.path.join("scripts", "modules", "rna_keymap_ui.py"), - os.path.join("scripts", "modules", "bpy_types.py"), + os.path.join("scripts", "modules", "_bpy_types.py"), os.path.join("scripts", "presets", "keyconfig"), ] diff --git a/scripts/modules/bpy/utils/__init__.py b/scripts/modules/bpy/utils/__init__.py index 336b8b64d3f..bf9b4898431 100644 --- a/scripts/modules/bpy/utils/__init__.py +++ b/scripts/modules/bpy/utils/__init__.py @@ -200,7 +200,7 @@ def modules_from_path(path, loaded_modules): # Currently used for "startup" modules. _registered_module_names = [] # Keep for comparisons, never ever reload this. -import bpy_types as _bpy_types +import _bpy_types def _register_module_call(mod): diff --git a/scripts/modules/rna_info.py b/scripts/modules/rna_info.py index 79f948e2995..615f0dc5c1e 100644 --- a/scripts/modules/rna_info.py +++ b/scripts/modules/rna_info.py @@ -144,7 +144,7 @@ class InfoStructRNA: if (self.py_class and not hasattr(bpy.types, self.identifier)) else "bpy.types" ) - if self.module_name == "bpy_types": + if self.module_name == "_bpy_types": self.module_name = "bpy.types" def build(self): diff --git a/source/blender/python/intern/bpy.cc b/source/blender/python/intern/bpy.cc index 49086d6f97c..be739d6217a 100644 --- a/source/blender/python/intern/bpy.cc +++ b/source/blender/python/intern/bpy.cc @@ -745,21 +745,21 @@ void BPy_init_modules(bContext *C) PyDict_SetItemString(PyImport_GetModuleDict(), "_bpy", mod); Py_DECREF(mod); - /* needs to be first so bpy_types can run */ + /* Needs to be first so `_bpy_types` can run. */ PyObject *bpy_types = BPY_rna_types(); PyModule_AddObject(bpy_types, "GeometrySet", BPyInit_geometry_set_type()); PyModule_AddObject(mod, "types", bpy_types); - /* needs to be first so bpy_types can run */ + /* Needs to be first so `_bpy_types` can run. */ BPY_library_load_type_ready(); BPY_rna_data_context_type_ready(); BPY_rna_gizmo_module(mod); - bpy_import_test("bpy_types"); - PyModule_AddObject(mod, "data", BPY_rna_module()); /* imports bpy_types by running this */ - bpy_import_test("bpy_types"); + bpy_import_test("_bpy_types"); + PyModule_AddObject(mod, "data", BPY_rna_module()); /* Imports `_bpy_types` by running this. */ + bpy_import_test("_bpy_types"); BPY_rna_types_finalize_external_types(bpy_types); PyModule_AddObject(mod, "props", BPY_rna_props()); diff --git a/source/blender/python/intern/bpy_rna.cc b/source/blender/python/intern/bpy_rna.cc index a4ae708cb7e..73cbb57476c 100644 --- a/source/blender/python/intern/bpy_rna.cc +++ b/source/blender/python/intern/bpy_rna.cc @@ -8168,11 +8168,11 @@ static PyObject *pyrna_srna_ExternalType(StructRNA *srna) PyObject *newclass; if (bpy_types_dict == nullptr) { - PyObject *bpy_types = PyImport_ImportModuleLevel("bpy_types", nullptr, nullptr, nullptr, 0); + PyObject *bpy_types = PyImport_ImportModuleLevel("_bpy_types", nullptr, nullptr, nullptr, 0); if (bpy_types == nullptr) { PyErr_Print(); - CLOG_ERROR(BPY_LOG_RNA, "failed to find 'bpy_types' module"); + CLOG_ERROR(BPY_LOG_RNA, "failed to find '_bpy_types' module"); return nullptr; } bpy_types_dict = PyModule_GetDict(bpy_types); /* Borrow. */ @@ -8194,7 +8194,7 @@ static PyObject *pyrna_srna_ExternalType(StructRNA *srna) if (tp_slots == nullptr) { CLOG_ERROR( - BPY_LOG_RNA, "expected class '%s' to have __slots__ defined, see bpy_types.py", idname); + BPY_LOG_RNA, "expected class '%s' to have __slots__ defined, see _bpy_types.py", idname); newclass = nullptr; } else if (PyTuple_GET_SIZE(tp_bases)) { @@ -8204,7 +8204,7 @@ static PyObject *pyrna_srna_ExternalType(StructRNA *srna) char pyob_info[256]; PyC_ObSpitStr(pyob_info, sizeof(pyob_info), base_compare); CLOG_ERROR(BPY_LOG_RNA, - "incorrect subclassing of SRNA '%s', expected '%s', see bpy_types.py", + "incorrect subclassing of SRNA '%s', expected '%s', see _bpy_types.py", idname, pyob_info); newclass = nullptr; @@ -8234,7 +8234,7 @@ static PyObject *pyrna_srna_Subtype(StructRNA *srna) else if ((newclass = static_cast(RNA_struct_py_type_get(srna)))) { /* Add a reference for the return value. */ Py_INCREF(newclass); - } /* Check if `bpy_types.py` module has the class defined in it. */ + } /* Check if `_bpy_types.py` module has the class defined in it. */ else if ((newclass = pyrna_srna_ExternalType(srna))) { pyrna_subtype_set_rna(newclass, srna); /* Add a reference for the return value. */ @@ -8851,13 +8851,13 @@ void BPY_rna_types_finalize_external_types(PyObject *submodule) BLI_assert_msg( PyObject_IsSubclass(arg_value, (PyObject *)&pyrna_struct_Type), - "Members of bpy_types.py which are not StructRNA sub-classes must use a \"_\" prefix!"); + "Members of _bpy_types.py which are not StructRNA sub-classes must use a \"_\" prefix!"); PointerRNA newptr; if (RNA_property_collection_lookup_string(&state->ptr.value(), state->prop, key_str, &newptr)) { StructRNA *srna = srna_from_ptr(&newptr); - /* Within the Python logic of `./scripts/modules/bpy_types.py` + /* Within the Python logic of `./scripts/modules/_bpy_types.py` * it's possible this was already initialized. */ if (RNA_struct_py_type_get(srna) == nullptr) { pyrna_subtype_set_rna(arg_value, srna); @@ -8872,7 +8872,7 @@ void BPY_rna_types_finalize_external_types(PyObject *submodule) } # endif CLOG_WARN( - BPY_LOG_RNA, "bpy_types.py defines \"%.200s\" which is not a known RNA type!", key_str); + BPY_LOG_RNA, "_bpy_types.py defines \"%.200s\" which is not a known RNA type!", key_str); } #endif } @@ -9977,7 +9977,7 @@ void pyrna_alloc_types() { /* NOTE: This isn't essential to run on startup, since sub-types will lazy initialize. * But keep running in debug mode so we get immediate notification of bad class hierarchy - * or any errors in "bpy_types.py" at load time, so errors don't go unnoticed. */ + * or any errors in `_bpy_types.py` at load time, so errors don't go unnoticed. */ #ifndef NDEBUG PyGILState_STATE gilstate = PyGILState_Ensure(); diff --git a/source/blender/python/intern/bpy_rna_types_capi.cc b/source/blender/python/intern/bpy_rna_types_capi.cc index 2b44f7c4e45..d82ef8e4b49 100644 --- a/source/blender/python/intern/bpy_rna_types_capi.cc +++ b/source/blender/python/intern/bpy_rna_types_capi.cc @@ -9,7 +9,7 @@ * * We should avoid adding code here, and prefer: * - `source/blender/makesrna/intern/rna_context.cc` using the RNA C API. - * - `scripts/modules/bpy_types.py` when additions c an be written in Python. + * - `scripts/modules/_bpy_types.py` when additions c an be written in Python. * * Otherwise functions can be added here as a last resort. */