Skip to content

Commit 750fcbe

Browse files
committedDec 6, 2024·
refactor: Switch to inspect.signature()
1 parent 8585d8c commit 750fcbe

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed
 

‎bottle.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,7 @@ def _cli_patch(cli_args): # pragma: no coverage
9696
import configparser
9797
from datetime import timezone
9898
UTC = timezone.utc
99-
# getfullargspec was deprecated in 3.5 and un-deprecated in 3.6
100-
# getargspec was deprecated in 3.0 and removed in 3.11
101-
from inspect import getfullargspec
102-
103-
def getargspec(func):
104-
spec = getfullargspec(func)
105-
kwargs = makelist(spec[0]) + makelist(spec.kwonlyargs)
106-
return kwargs, spec[1], spec[2], spec[3]
99+
import inspect
107100

108101
json_loads = lambda s: json_lds(touni(s))
109102
callable = lambda x: hasattr(x, '__call__')
@@ -553,7 +546,10 @@ def get_callback_args(self):
553546
""" Return a list of argument names the callback (most likely) accepts
554547
as keyword arguments. If the callback is a decorated function, try
555548
to recover the original function before inspection. """
556-
return getargspec(self.get_undecorated_callback())[0]
549+
sig = inspect.signature(self.get_undecorated_callback())
550+
return [p.name for p in sig.parameters.values() if p.kind in (
551+
p.POSITIONAL_OR_KEYWORD, p.KEYWORD_ONLY
552+
)]
557553

558554
def get_config(self, key, default=None):
559555
""" Lookup a config field and return its value, first checking the
@@ -3006,13 +3002,15 @@ def yieldroutes(func):
30063002
d(x=5, y=6) -> '/d' and '/d/<x>' and '/d/<x>/<y>'
30073003
"""
30083004
path = '/' + func.__name__.replace('__', '/').lstrip('/')
3009-
spec = getargspec(func)
3010-
argc = len(spec[0]) - len(spec[3] or [])
3011-
path += ('/<%s>' * argc) % tuple(spec[0][:argc])
3005+
sig = inspect.signature(func, follow_wrapped=False)
3006+
for p in sig.parameters.values():
3007+
if p.kind == p.POSITIONAL_ONLY:
3008+
raise ValueError("Invalid signature for yieldroutes: %s" % sig)
3009+
if p.kind in (p.POSITIONAL_OR_KEYWORD, p.KEYWORD_ONLY):
3010+
if p.default != p.empty:
3011+
yield path # Yield path without this (optional) parameter.
3012+
path += "/<%s>" % p.name
30123013
yield path
3013-
for arg in spec[0][argc:]:
3014-
path += '/<%s>' % arg
3015-
yield path
30163014

30173015

30183016
def path_shift(script_name, path_info, shift=1):

0 commit comments

Comments
 (0)
Please sign in to comment.