Skip to content

Commit 9c993a8

Browse files
author
Pietro Albini
committed
Refactored the Bot._call internal method
Before this commit, a bug considered all the hooks as part of the main component, for shared memory. This caused serious issues with shared memory initializers, since they didn't work for external components. Now the Bot._call method accepts the component ID of the hook, and provides shared memory as an argument only if the ID is provided. This is a regression introduced in 350a78a.
1 parent ad23717 commit 9c993a8

File tree

5 files changed

+21
-21
lines changed

5 files changed

+21
-21
lines changed

botogram/frozenbot.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,13 @@ def _get_commands(self):
217217
"""Get all the commands this bot implements"""
218218
return self._commands
219219

220-
def _call(self, func, **available):
220+
def _call(self, func, component=None, **available):
221221
"""Wrapper for calling user-provided functions"""
222-
# Get the function's component
223-
if hasattr(func, "botogram") and func.botogram.component is not None:
224-
component = func.botogram.component._component_id
225-
else:
226-
component = self._main_component_id
227-
shared = self._shared_memory.of(self._bot_id, component)
228-
229222
# Set some default available arguments
230223
available.setdefault("bot", self)
231-
available.setdefault("shared", shared)
224+
if component is not None:
225+
shared = self._shared_memory.of(self._bot_id, component)
226+
available.setdefault("shared", shared)
232227

233228
# Get the correct function signature
234229
# botogram_original_signature is set while using @utils.wraps

botogram/hooks.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def __init__(self, func, component, args=None):
2323
self.func = func
2424
self.name = prefix+func.__name__
2525
self.component = component
26+
self.component_id = component._component_id
2627

2728
self._args = args
2829
self._after_init(args)
@@ -46,7 +47,8 @@ def call(self, bot, update):
4647
def _call(self, bot, update):
4748
"""*Actually* call the hook"""
4849
message = update.message
49-
return bot._call(self.func, chat=message.chat, message=message)
50+
return bot._call(self.func, self.component_id, chat=message.chat,
51+
message=message)
5052

5153

5254
def rebuild(cls, func, component, args):
@@ -98,7 +100,8 @@ def _call(self, bot, update):
98100

99101
if text != self._string:
100102
return
101-
return bot._call(self.func, chat=message.chat, message=message)
103+
return bot._call(self.func, self.component_id, chat=message.chat,
104+
message=message)
102105

103106

104107
class MessageContainsHook(MessageEqualsHook):
@@ -115,7 +118,8 @@ def _call(self, bot, update):
115118
if one != self._string:
116119
continue
117120

118-
result = bot._call(self.func, chat=message.chat, message=message)
121+
result = bot._call(self.func, self.component_id, chat=message.chat,
122+
message=message)
119123
res.append(result)
120124
if not self._args["multiple"]:
121125
break
@@ -139,8 +143,8 @@ def _call(self, bot, update):
139143
for result in results:
140144
found = True
141145

142-
bot._call(self.func, chat=message.chat, message=message,
143-
matches=result.groups())
146+
bot._call(self.func, self.component_id, chat=message.chat,
147+
message=message, matches=result.groups())
144148
if not self._args["multiple"]:
145149
break
146150

@@ -172,12 +176,13 @@ def _call(self, bot, update):
172176
return
173177

174178
args = message.text.split(" ")[1:]
175-
bot._call(self.func, chat=message.chat, message=message, args=args)
179+
bot._call(self.func, self.component_id, chat=message.chat,
180+
message=message, args=args)
176181
return True
177182

178183

179184
class TimerHook(Hook):
180185
"""Underlying hook for a timer"""
181186

182187
def call(self, bot):
183-
return bot._call(self.func)
188+
return bot._call(self.func, self.component_id)

botogram/tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def process(self, bot):
1919
"""Process the task"""
2020
if hasattr(self.hook, "call"):
2121
return self.hook.call(bot)
22-
return bot._call(self.hook)
22+
return self.hook(bot)
2323

2424

2525
class TimerTask(BaseTask):

botogram/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ def format_docstr(docstring):
8989
return "\n".join(result)
9090

9191

92-
def docstring_of(func, bot=None):
92+
def docstring_of(func, bot=None, component_id=None):
9393
"""Get the docstring of a function"""
9494
# Get the correct function from the hook
9595
if hasattr(func, "_botogram_hook"):
9696
func = func.func
9797

9898
if hasattr(func, "_botogram_help_message"):
9999
if bot is not None:
100-
docstring = bot._call(func._botogram_help_message)
100+
docstring = bot._call(func._botogram_help_message, component_id)
101101
else:
102102
docstring = func._botogram_help_message()
103103
elif func.__doc__:

tests/test_frozenbot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ def more(bot, a, c):
4040
assert 2 + 2 == 5
4141

4242
for func in subset, all:
43-
frozenbot._call(func, a="foo", b=42)
43+
frozenbot._call(func, "comptest", a="foo", b=42)
4444

4545
# More should raise a TypeError for the "c" argument
4646
with pytest.raises(TypeError):
47-
frozenbot._call(more, a="foo", b=42)
47+
frozenbot._call(more, "comptest", a="foo", b=42)
4848

4949
assert subset_called
5050
assert all_called

0 commit comments

Comments
 (0)