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.
This commit is contained in:
Campbell Barton 2024-05-04 15:48:01 +10:00
parent d3c6eeb03e
commit 2104e6b07d
2 changed files with 16 additions and 14 deletions

@ -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);

@ -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.