Skip to content

Commit 3eb98ec

Browse files
committed
Fix WebhookHandler.__get_args_count to enable to corp with functions with
varargs
1 parent 37ce8a3 commit 3eb98ec

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

linebot/webhook.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,22 +261,22 @@ def __add_handler(self, func, event, message=None):
261261

262262
@classmethod
263263
def __invoke_func(cls, func, event, payload):
264-
args_count = cls.__get_args_count(func)
265-
if args_count == 0:
266-
func()
264+
(has_varargs, args_count) = cls.__get_args_count(func)
265+
if has_varargs or args_count == 2:
266+
func(event, payload.destination)
267267
elif args_count == 1:
268268
func(event)
269269
else:
270-
func(event, payload.destination)
270+
func()
271271

272272
@staticmethod
273273
def __get_args_count(func):
274274
if PY3:
275275
arg_spec = inspect.getfullargspec(func)
276-
return len(arg_spec.args)
276+
return (arg_spec.varargs is not None, len(arg_spec.args))
277277
else:
278278
arg_spec = inspect.getargspec(func)
279-
return len(arg_spec.args)
279+
return (arg_spec.varargs is not None, len(arg_spec.args))
280280

281281
@staticmethod
282282
def __get_handler_key(event, message=None):

tests/test_webhook.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818
import unittest
1919
from builtins import open
20+
import inspect
2021

2122
from linebot import (
2223
SignatureValidator, WebhookParser, WebhookHandler
@@ -29,6 +30,7 @@
2930
LocationMessage, StickerMessage, FileMessage,
3031
SourceUser, SourceRoom, SourceGroup,
3132
DeviceLink, DeviceUnlink, ScenarioResult, ActionResult)
33+
from linebot.utils import PY3
3234

3335

3436
class TestSignatureValidator(unittest.TestCase):
@@ -529,6 +531,15 @@ def test_handler(self):
529531

530532
class TestInvokeWebhookHandler(unittest.TestCase):
531533
def setUp(self):
534+
def wrap(func):
535+
def wrapper(*args):
536+
if PY3:
537+
arg_spec = inspect.getfullargspec(func)
538+
else:
539+
arg_spec = inspect.getargspec(func)
540+
return func(*args[0:len(arg_spec.args)])
541+
return wrapper
542+
532543
def func_with_0_args():
533544
assert True
534545

@@ -547,13 +558,44 @@ def func_with_2_args_with_default(arg1=False, arg2=False):
547558
def func_with_1_arg_and_1_arg_with_default(arg1, arg2=False):
548559
assert arg1 and arg2
549560

561+
@wrap
562+
def wrapped_func_with_0_args():
563+
assert True
564+
565+
@wrap
566+
def wrapped_func_with_1_arg(arg):
567+
assert arg
568+
569+
@wrap
570+
def wrapped_func_with_2_args(arg1, arg2):
571+
assert arg1 and arg2
572+
573+
@wrap
574+
def wrapped_func_with_1_arg_with_default(arg=False):
575+
assert arg
576+
577+
@wrap
578+
def wrapped_func_with_2_args_with_default(arg1=False, arg2=False):
579+
assert arg1 and arg2
580+
581+
@wrap
582+
def wrapped_func_with_1_arg_and_1_arg_with_default(
583+
arg1, arg2=False):
584+
assert arg1 and arg2
585+
550586
self.functions = [
551587
func_with_0_args,
552588
func_with_1_arg,
553589
func_with_2_args,
554590
func_with_1_arg_with_default,
555591
func_with_2_args_with_default,
556592
func_with_1_arg_and_1_arg_with_default,
593+
wrapped_func_with_0_args,
594+
wrapped_func_with_1_arg,
595+
wrapped_func_with_2_args,
596+
wrapped_func_with_1_arg_with_default,
597+
wrapped_func_with_2_args_with_default,
598+
wrapped_func_with_1_arg_and_1_arg_with_default,
557599
]
558600

559601
def test_invoke_func(self):

0 commit comments

Comments
 (0)