From 4ba2f444ff92651350d5ba1f502f44dca1715a98 Mon Sep 17 00:00:00 2001 From: Griatch Date: Tue, 1 Sep 2020 22:58:54 +0200 Subject: [PATCH] Make at_post_cmd() run after func() also for delayed commands. Resolve #2179 --- CHANGELOG.md | 2 ++ evennia/commands/cmdhandler.py | 31 +++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b889f0598..471aec0b1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -70,6 +70,8 @@ without arguments starts a full interactive Python console. - Make `INLINEFUNC_STACK_MAXSIZE` default visible in `settings_default.py`. - Change how `ic` finds puppets; non-priveleged users will use `_playable_characters` list as candidates, Builders+ will use list, local search and only global search if no match found. +- Make `cmd.at_post_cmd()` always run after `cmd.func()`, even when the latter uses delays + with yield. ## Evennia 0.9 (2018-2019) diff --git a/evennia/commands/cmdhandler.py b/evennia/commands/cmdhandler.py index dd0f84e872..b22719ca35 100644 --- a/evennia/commands/cmdhandler.py +++ b/evennia/commands/cmdhandler.py @@ -206,7 +206,7 @@ def _progressive_cmd_run(cmd, generator, response=None): else: value = generator.send(response) except StopIteration: - pass + raise else: if isinstance(value, (int, float)): utils.delay(value, _progressive_cmd_run, cmd, generator) @@ -631,20 +631,31 @@ def cmdhandler( ret = cmd.func() if isinstance(ret, types.GeneratorType): # cmd.func() is a generator, execute progressively - _progressive_cmd_run(cmd, ret) + in_generator = True + try: + _progressive_cmd_run(cmd, ret) + except StopIteration: + # this means func() has run its course + in_generator = False yield None else: + in_generator = False ret = yield ret - # post-command hook - yield cmd.at_post_cmd() + if not in_generator: + # this will only run if we are out of the generator for this + # cmd, otherwise we would have at_post_cmd run before a delayed + # func() finished - if cmd.save_for_next: - # store a reference to this command, possibly - # accessible by the next command. - caller.ndb.last_cmd = yield copy(cmd) - else: - caller.ndb.last_cmd = None + # post-command hook + yield cmd.at_post_cmd() + + if cmd.save_for_next: + # store a reference to this command, possibly + # accessible by the next command. + caller.ndb.last_cmd = yield copy(cmd) + else: + caller.ndb.last_cmd = None # return result to the deferred returnValue(ret)