fix: handle NaN/undefined variable in integer comparisons
Wrap int(var_value) in __eval_basiccondition() with try/except to prevent ValueError crash when a variable is undefined, empty, or contains non-numeric text. In warn mode the comparison evaluates to false; in stop mode a PPPInterrupt is raised with a descriptive message.
This commit is contained in:
+1
-1
@@ -34,7 +34,7 @@ With this prompt: `__quality__, 1girl, ${head:__eyes__, __hair__, __expression__
|
||||
### General settings
|
||||
|
||||
* **Debug level**: what to write to the console. Note: in *SD.Next* debug messages only show if you launch it with the `--debug` argument.
|
||||
* **What to do on invalid content warnings?**: warn on the console or stop the generation.
|
||||
* **What to do on invalid content warnings?**: warn on the console or stop the generation. This also affects integer comparisons with undefined or non-numeric variables: in *warn* mode the comparison evaluates to false, in *stop* mode the generation is stopped with an error.
|
||||
* **Model variant definitions**: definitions for model variants to be recognized based on strings found in the full filename.
|
||||
|
||||
The format for each line is (with *kind* being one of the base model identifiers or not defined):
|
||||
|
||||
@@ -211,6 +211,8 @@ The `conditionN` can be:
|
||||
|
||||
For a simple value the allowed operations are `eq`, `ne`, `gt`, `lt`, `ge`, `le`, `contains` and the value can be a quoted string or an integer. For a list of values the allowed operations are `contains`, `in` and the value of the variable is checked against all the elements of the list until one matches. The operation can be preceded by `not` for readability, instead of using it in the front.
|
||||
|
||||
When a variable used in an integer comparison is undefined or cannot be converted to an integer (e.g. it contains non-numeric text or is an empty string), the behavior depends on the `on_warning` setting: in `warn` mode the comparison evaluates to false, and in `stop` mode an error is raised.
|
||||
|
||||
You can also build complex conditions joining them with boolean operators and/or/not and parentheses.
|
||||
|
||||
The variable can be one set with the `set` or `add` commands (user variables) or you can use system variables like these (names starting with an underscore are reserved for system variables):
|
||||
|
||||
@@ -1139,19 +1139,20 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
|
||||
)
|
||||
result = False
|
||||
for c in cond_value_adjusted:
|
||||
var_value_adjusted = (
|
||||
var_value
|
||||
if isinstance(c, str)
|
||||
else (
|
||||
True
|
||||
if isinstance(c, bool) and var_value != "false" and var_value != "" and var_value is not False
|
||||
else (
|
||||
False
|
||||
if isinstance(c, bool) and (var_value != "true" or var_value is False)
|
||||
else int(var_value)
|
||||
if isinstance(c, str):
|
||||
var_value_adjusted = var_value
|
||||
elif isinstance(c, bool) and var_value != "false" and var_value != "" and var_value is not False:
|
||||
var_value_adjusted = True
|
||||
elif isinstance(c, bool) and (var_value != "true" or var_value is False):
|
||||
var_value_adjusted = False
|
||||
else:
|
||||
try:
|
||||
var_value_adjusted = int(var_value)
|
||||
except (ValueError, TypeError):
|
||||
self.warn_or_stop(
|
||||
f"Cannot convert variable value '{var_value}' to integer for comparison"
|
||||
)
|
||||
)
|
||||
)
|
||||
return False
|
||||
result = comp_ops[cond_comp](var_value_adjusted, c)
|
||||
if result:
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user