From 2104e6b07df393e64e22ccbee21596eb5d270eec Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 4 May 2024 15:48:01 +1000 Subject: [PATCH] Cleanup: require filepath to be non-null for Python name-space creation All callers pass in a non-null filepath, require this as it's often expected to be set and useful in error messages. --- source/blender/python/generic/py_capi_utils.cc | 12 ++++++------ source/blender/python/generic/py_capi_utils.h | 18 ++++++++++-------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/source/blender/python/generic/py_capi_utils.cc b/source/blender/python/generic/py_capi_utils.cc index 20867ee1457..c08412f6732 100644 --- a/source/blender/python/generic/py_capi_utils.cc +++ b/source/blender/python/generic/py_capi_utils.cc @@ -1102,12 +1102,12 @@ PyObject *PyC_DefaultNameSpace(const char *filename) PyDict_SetItemString(modules, "__main__", mod_main); Py_DECREF(mod_main); /* `sys.modules` owns now. */ PyModule_AddStringConstant(mod_main, "__name__", "__main__"); - if (filename) { - /* This won't map to a real file when executing text-blocks and buttons. - * In this case an identifier is typically used that is surrounded by angle-brackets. - * It's mainly helpful for the UI and messages to show *something*. */ - PyModule_AddObject(mod_main, "__file__", PyC_UnicodeFromBytes(filename)); - } + + /* This won't map to a real file when executing text-blocks and buttons. + * In this case an identifier is typically used that is surrounded by angle-brackets. + * It's mainly helpful for the UI and messages to show *something*. */ + PyModule_AddObject(mod_main, "__file__", PyC_UnicodeFromBytes(filename)); + PyModule_AddObject(mod_main, "__builtins__", builtins); Py_INCREF(builtins); /* AddObject steals a reference */ return PyModule_GetDict(mod_main); diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h index 239070c49f4..0c8e993ba2a 100644 --- a/source/blender/python/generic/py_capi_utils.h +++ b/source/blender/python/generic/py_capi_utils.h @@ -172,8 +172,8 @@ int PyC_ParseUnicodeAsBytesAndSize_OrNone(PyObject *o, void *p); * be sure to run PyC_MainModule_Backup & PyC_MainModule_Restore if there is * any chance that python is in the call stack. */ -PyObject *PyC_DefaultNameSpace(const char *filename); -void PyC_RunQuicky(const char *filepath, int n, ...); +PyObject *PyC_DefaultNameSpace(const char *filename) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT; +void PyC_RunQuicky(const char *filepath, int n, ...) ATTR_NONNULL(1); /** * Import `imports` into `py_dict`. * @@ -223,11 +223,11 @@ PyObject *PyC_FlagSet_FromBitfield(PyC_FlagSet *items, int flag); bool PyC_RunString_AsNumber(const char **imports, const char *expr, const char *filename, - double *r_value); + double *r_value) ATTR_NONNULL(2, 3, 4) ATTR_WARN_UNUSED_RESULT; bool PyC_RunString_AsIntPtr(const char **imports, const char *expr, const char *filename, - intptr_t *r_value); + intptr_t *r_value) ATTR_NONNULL(2, 3, 4) ATTR_WARN_UNUSED_RESULT; /** * \param r_value_size: The length of the string assigned: `strlen(*r_value)`. */ @@ -235,11 +235,12 @@ bool PyC_RunString_AsStringAndSize(const char **imports, const char *expr, const char *filename, char **r_value, - size_t *r_value_size); + size_t *r_value_size) + ATTR_NONNULL(2, 3, 4, 5) ATTR_WARN_UNUSED_RESULT; bool PyC_RunString_AsString(const char **imports, const char *expr, const char *filename, - char **r_value); + char **r_value) ATTR_NONNULL(2, 3, 4) ATTR_WARN_UNUSED_RESULT; /** * \param r_value_size: The length of the string assigned: `strlen(*r_value)`. @@ -248,11 +249,12 @@ bool PyC_RunString_AsStringAndSizeOrNone(const char **imports, const char *expr, const char *filename, char **r_value, - size_t *r_value_size); + size_t *r_value_size) + ATTR_NONNULL(2, 3, 4) ATTR_WARN_UNUSED_RESULT; bool PyC_RunString_AsStringOrNone(const char **imports, const char *expr, const char *filename, - char **r_value); + char **r_value) ATTR_NONNULL(2, 3, 4) ATTR_WARN_UNUSED_RESULT; /** * Use with PyArg_ParseTuple's "O&" formatting.