mirror of
https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
synced 2026-03-22 14:20:39 -07:00
- Add modules/runtime_utils.py with temporary_opts context manager - Refactor process_images() to use temporary_opts for override application - Preserve opts.set(is_api=True, run_callbacks=False) and setattr restore - Add test/quality/test_opts_override.py (samples_save, restore_afterwards) - Model/VAE reload and token merging remain in process_images per decisions Made-with: Cursor
41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
"""Runtime utilities for the generation pipeline.
|
|
|
|
M05: Temporary opts override seam — isolates option mutation during generation.
|
|
"""
|
|
from contextlib import contextmanager
|
|
|
|
from modules import shared
|
|
|
|
|
|
@contextmanager
|
|
def temporary_opts(overrides: dict, restore_afterwards: bool = True):
|
|
"""Context manager for temporary option overrides during generation.
|
|
|
|
Applies overrides via opts.set(..., is_api=True, run_callbacks=False).
|
|
Restores original values on exit via setattr (no callbacks).
|
|
Only mutates keys present in opts.data.
|
|
|
|
Args:
|
|
overrides: Dict of option key -> value to apply.
|
|
restore_afterwards: If True, restore original values on exit.
|
|
"""
|
|
opts = shared.opts
|
|
if not overrides:
|
|
yield
|
|
return
|
|
|
|
original = {
|
|
k: opts.data[k] if k in opts.data else opts.get_default(k)
|
|
for k in overrides.keys()
|
|
if k in opts.data
|
|
}
|
|
|
|
try:
|
|
for k, v in overrides.items():
|
|
if k in opts.data:
|
|
opts.set(k, v, is_api=True, run_callbacks=False)
|
|
yield
|
|
finally:
|
|
if restore_afterwards:
|
|
for k, v in original.items():
|
|
setattr(opts, k, v)
|