PyAPI: use None for bpy.app.autoexec_override when it isn't set

Expose `bpy.app.autoexec_override` as as bool or None,
so the boolean can be used to indicate the command line
argument passed in.

Ref !164033
This commit is contained in:
Campbell Barton 2026-09-17 04:26:24 +00:00
parent 93145bafae
commit 3d39eceaee
5 changed files with 23 additions and 10 deletions

View file

@ -3749,7 +3749,7 @@ class WM_OT_drop_blend_file(Operator):
# Excluded paths can't be trusted, unless the command line overrides the preference.
is_untrusted = (
context.preferences.filepaths.use_scripts_auto_execute and
not bpy.app.autoexec_override and
bpy.app.autoexec_override is None and
not self._is_autoexec(filepath, skip_overrides=True)
)

View file

@ -1777,8 +1777,8 @@ class USERPREF_PT_saveload_autorun(FilePathsPanel, Panel):
text = iface_("Auto Run Python Scripts")
if bpy.app.autoexec_override:
if bpy.app.autoexec:
if (autoexec_override := bpy.app.autoexec_override) is not None:
if autoexec_override:
text_warn = iface_("enabled on startup, overriding the preference")
else:
text_warn = iface_("disabled on startup, overriding the preference")

View file

@ -367,10 +367,10 @@ PyDoc_STRVAR(
PyDoc_STRVAR(
/* Wrap. */
bpy_app_autoexec_override_doc,
"Boolean, True when the auto-execution preference is overridden by the command line "
"The auto-execution set by the command line, None when the preference isn't overridden "
"(read-only).\n"
"\n"
":type: bool\n");
":type: bool | None\n");
PyDoc_STRVAR(
/* Wrap. */
bpy_app_autoexec_fail_doc,
@ -391,6 +391,14 @@ static PyObject *bpy_app_global_flag_get(PyObject * /*self*/, void *closure)
return PyBool_FromLong(G.f & flag);
}
static PyObject *bpy_app_autoexec_override_get(PyObject * /*self*/, void * /*closure*/)
{
if ((G.f & G_FLAG_SCRIPT_OVERRIDE_PREF) == 0) {
Py_RETURN_NONE;
}
return PyBool_FromLong(G.f & G_FLAG_SCRIPT_AUTOEXEC);
}
static int bpy_app_global_flag_set(PyObject * /*self*/, PyObject *value, void *closure)
{
const int flag = POINTER_AS_INT(closure);
@ -699,10 +707,10 @@ static PyGetSetDef bpy_app_getsets[] = {
bpy_app_autoexec_doc,
reinterpret_cast<void *>(G_FLAG_SCRIPT_AUTOEXEC)},
{"autoexec_override",
bpy_app_global_flag_get,
bpy_app_autoexec_override_get,
nullptr,
bpy_app_autoexec_override_doc,
reinterpret_cast<void *>(G_FLAG_SCRIPT_OVERRIDE_PREF)},
nullptr},
{"autoexec_fail",
bpy_app_global_flag_get,
nullptr,

View file

@ -327,8 +327,13 @@ class TestAutoExecCLI(unittest.TestCase):
self.assert_blend_file_state(states[0], filename, is_trusted=is_trusted, message=message)
# Opening the same file interactively must default to the same trust.
self.assertEqual(states[0]["trust_source"], is_trusted, message)
is_override = any(arg in {"--enable-autoexec", "--disable-autoexec"} for arg in args)
self.assertEqual(states[0]["autoexec_override"], is_override, message)
if "--enable-autoexec" in args:
autoexec_override = True
elif "--disable-autoexec" in args:
autoexec_override = False
else:
autoexec_override = None
self.assertEqual(states[0]["autoexec_override"], autoexec_override, message)
# -------------------------------------------------------------------------
# Tests with Auto-Execution Preference "Enabled"

View file

@ -175,7 +175,7 @@ def _setup(dirpath_temp, *, use_autoexec, autoexec_override=None):
e = _events_after_load()
t.assertEqual(bpy.data.filepath, "")
t.assertEqual(bpy.app.autoexec_override, autoexec_override is not None)
t.assertEqual(bpy.app.autoexec_override, autoexec_override)
if autoexec_override is None:
t.assertEqual(bpy.app.autoexec, use_autoexec, "Auto-execution follows the preference")
else: