From: Fredrik Tolf Date: Thu, 20 Jul 2023 15:15:42 +0000 (+0200) Subject: Update util.formparams to use inspect.signature. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=4d7795d9df82a23aff768ec8a679d340eb0ce058;hp=dff902d7223a92f2e7e33e5561cca7a6d39c7dbb;p=wrw.git Update util.formparams to use inspect.signature. --- diff --git a/wrw/util.py b/wrw/util.py index 7209ae4..b4e5b28 100644 --- a/wrw/util.py +++ b/wrw/util.py @@ -8,20 +8,24 @@ def wsgiwrap(callable): return wrapper def formparams(callable): - spec = inspect.getargspec(callable) + sig = inspect.signature(callable) + haskw = inspect.Parameter.VAR_KEYWORD in (par.kind for par in sig.parameters.values()) def wrapper(req): try: - data = form.formdata(req) + data = dict(form.formdata(req).items()) except IOError: raise resp.httperror(400, "Invalid request", "Form data was incomplete") - args = dict(data.items()) - args["req"] = req - if not spec.keywords: - for arg in list(args): - if arg not in spec.args: - del args[arg] - for i in range(len(spec.args) - (len(spec.defaults) if spec.defaults else 0)): - if spec.args[i] not in args: + + data["req"] = req + if haskw: + args = data + else: + args = {} + for par in sig.parameters.values(): + if par.name in data: + args[par.name] = data[par.name] + for par in sig.parameters.values(): + if par.default is inspect.Parameter.empty and par.name not in args: raise resp.httperror(400, "Missing parameter", ("The query parameter `", resp.h.code(spec.args[i]), "' is required but not supplied.")) return callable(**args) wrapper.__wrapped__ = callable