From 4d7795d9df82a23aff768ec8a679d340eb0ce058 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Thu, 20 Jul 2023 17:15:42 +0200 Subject: [PATCH] Update util.formparams to use inspect.signature. --- wrw/util.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) 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 -- 2.11.0