From 853fce2521e84e10f2dee10c386913c04b3c348d Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 12 Sep 2024 02:14:40 +0200 Subject: [PATCH] [procedures] Fixed `if` queue flushing logic. Any pending `if`s in the parsing queue of a procedure should also be cleared if the current statement in the procedure is a break/continue/return. In such case we should terminate the current branch, and that involves ensuring that any `if`s branches that are still being parsed are inserted before the branch-terminating statement. --- platypush/procedure/__init__.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/platypush/procedure/__init__.py b/platypush/procedure/__init__.py index 36c57c4dfa..e2d5641496 100644 --- a/platypush/procedure/__init__.py +++ b/platypush/procedure/__init__.py @@ -5,7 +5,7 @@ from dataclasses import dataclass from functools import wraps from queue import LifoQueue -from typing import Any, Optional +from typing import Any, List, Optional from ..common import exec_wrapper from ..config import Config @@ -110,6 +110,7 @@ class Procedure: for request_config in requests: # Check if it's a break/continue/return statement if isinstance(request_config, str): + cls._flush_if_statements(reqs, if_config) reqs.append(Statement.build(request_config)) continue @@ -118,6 +119,7 @@ class Procedure: len(request_config.keys()) == 1 and list(request_config.keys())[0] == 'return' ): + cls._flush_if_statements(reqs, if_config) reqs.append(ReturnStatement(argument=request_config['return'])) continue @@ -214,9 +216,7 @@ class Procedure: request = Request.build(request_config) reqs.append(request) - while not if_config.empty(): - pending_if = if_config.get() - reqs.append(IfProcedure.build(**pending_if)) + cls._flush_if_statements(reqs, if_config) return procedure_class( name=name, @@ -227,6 +227,12 @@ class Procedure: **kwargs, ) + @staticmethod + def _flush_if_statements(requests: List, if_config: LifoQueue): + while not if_config.empty(): + pending_if = if_config.get() + requests.append(IfProcedure.build(**pending_if)) + @staticmethod def _find_nearest_loop(stack): for proc in stack[::-1]: