From c2fc67dcf0d590e029774eb8a7e8de8e22cd4801 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Wed, 14 Mar 2012 21:47:31 +0100 Subject: [PATCH] Added a decorator for autoencoding string responses to bytes. --- wrw/__init__.py | 2 +- wrw/util.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/wrw/__init__.py b/wrw/__init__.py index 6458f64..f5a4e91 100644 --- a/wrw/__init__.py +++ b/wrw/__init__.py @@ -1,7 +1,7 @@ __all__ = ["wsgiwrap", "restart", "cookie", "formdata"] from . import proto -from .util import wsgiwrap, formparams, persession, sessiondata, autodirty, manudirty +from .util import wsgiwrap, stringwrap, formparams, persession, sessiondata, autodirty, manudirty from .dispatch import restart from . import cookie from .form import formdata diff --git a/wrw/util.py b/wrw/util.py index 58ead25..b8461d1 100644 --- a/wrw/util.py +++ b/wrw/util.py @@ -6,6 +6,15 @@ def wsgiwrap(callable): return dispatch.handle(req.origrequest(env), startreq, callable) return wrapper +def stringwrap(charset): + def dec(callable): + def wrapper(*args, **kwargs): + bk = callable(*args, **kwargs) + for string in bk: + yield string.encode(charset) + return wrapper + return dec + def formparams(callable): def wrapper(req): data = form.formdata(req) -- 2.11.0