From 6964edfa7a20389c0fcbda5f9b049f9c006e5c2c Mon Sep 17 00:00:00 2001 From: Ali-Akber Saifee Date: Sun, 22 Oct 2017 13:29:08 +0800 Subject: [PATCH] Handle non string types in key_for function --- limits/limits.py | 15 +++++++++++++-- limits/util.py | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/limits/limits.py b/limits/limits.py index a562eb49..7124cb7d 100644 --- a/limits/limits.py +++ b/limits/limits.py @@ -2,11 +2,22 @@ """ from six import add_metaclass + try: from functools import total_ordering except ImportError: # pragma: no cover from .backports.total_ordering import total_ordering # pragma: no cover +def safe_string(value): + """ + consistently converts a value to a string + :param value: + :return: str + """ + if isinstance(value, bytes): + return value.decode() + return str(value) + TIME_TYPES = dict( day=(60 * 60 * 24, "day"), month=(60 * 60 * 24 * 30, "month"), @@ -71,8 +82,8 @@ def key_for(self, *identifiers): each identifier appended with a '/' delimiter. """ remainder = "/".join( - identifiers + - (str(self.amount), str(self.multiples), self.granularity[1]) + [safe_string(k) for k in identifiers] + + [safe_string(self.amount), safe_string(self.multiples), self.granularity[1]] ) return "%s/%s" % (self.namespace, remainder) diff --git a/limits/util.py b/limits/util.py index 56611bfc..d2966879 100644 --- a/limits/util.py +++ b/limits/util.py @@ -6,6 +6,7 @@ from .limits import GRANULARITIES + SEPARATORS = re.compile(r"[,;|]{1}") SINGLE_EXPR = re.compile( r"\s*([0-9]+)\s*(/|\s*per\s*)\s*([0-9]+)*\s*(hour|minute|second|day|month|year)s?\s*",