From 471d46d67e770ab06ab4f573cc9c2745b3a63a76 Mon Sep 17 00:00:00 2001 From: Mark Stenglein Date: Tue, 2 May 2017 23:41:31 -0400 Subject: [PATCH 1/3] web: __init__: _rep: Filter all bytes for serializer This commit fixes issue #2532 by filtering out any byte values added by any other extensions and decoding them to strings for the JSON serializer. It does not remove any of the keys, instead opting to just convert them. Signed-off-by: Mark Stenglein --- beetsplug/web/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/beetsplug/web/__init__.py b/beetsplug/web/__init__.py index 3c18ebd5d..1bea628f8 100644 --- a/beetsplug/web/__init__.py +++ b/beetsplug/web/__init__.py @@ -42,6 +42,10 @@ def _rep(obj, expand=False): else: del out['path'] + # Filter all bytes attributes and convert them to strings + for key in filter(lambda key: isinstance(out[key], bytes), out): + out[key] = out[key].decode('utf-8') + # Get the size (in bytes) of the backing file. This is useful # for the Tomahawk resolver API. try: From 9394e0ac63786b7f0296eb39d0885960e677ff23 Mon Sep 17 00:00:00 2001 From: Mark Stenglein Date: Wed, 3 May 2017 16:34:30 -0400 Subject: [PATCH 2/3] web: __init__: _rep: change to base64 encoding As suggested, this commit adds base64 encoding for the bytes encoding. Signed-off-by: Mark Stenglein --- beetsplug/web/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/beetsplug/web/__init__.py b/beetsplug/web/__init__.py index 1bea628f8..2afef7786 100644 --- a/beetsplug/web/__init__.py +++ b/beetsplug/web/__init__.py @@ -25,6 +25,7 @@ from flask import g from werkzeug.routing import BaseConverter, PathConverter import os import json +import base64 # Utilities. @@ -44,7 +45,7 @@ def _rep(obj, expand=False): # Filter all bytes attributes and convert them to strings for key in filter(lambda key: isinstance(out[key], bytes), out): - out[key] = out[key].decode('utf-8') + out[key] = base64.b64encode(out[key]).decode('ascii') # Get the size (in bytes) of the backing file. This is useful # for the Tomahawk resolver API. From 22f07b91e93267520870f55114841c886c7d057d Mon Sep 17 00:00:00 2001 From: Mark Stenglein Date: Wed, 3 May 2017 16:36:40 -0400 Subject: [PATCH 3/3] web: __init__: _rep: Make the looping more pythonic As suggested, changes around the for loop to make it a bit more pythonic by using an if loop inside a normal for loop. Signed-off-by: Mark Stenglein --- beetsplug/web/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/beetsplug/web/__init__.py b/beetsplug/web/__init__.py index 2afef7786..c154b0cf4 100644 --- a/beetsplug/web/__init__.py +++ b/beetsplug/web/__init__.py @@ -44,7 +44,8 @@ def _rep(obj, expand=False): del out['path'] # Filter all bytes attributes and convert them to strings - for key in filter(lambda key: isinstance(out[key], bytes), out): + for key, value in out.items(): + if isinstance(out[key], bytes): out[key] = base64.b64encode(out[key]).decode('ascii') # Get the size (in bytes) of the backing file. This is useful