From fe74ec04897d1ffae6e2808ed54a2f0761187d5e Mon Sep 17 00:00:00 2001 From: Tim Burke Date: Mon, 4 May 2020 21:35:58 -0700 Subject: [PATCH] py27: Suppress UnicodeWarnings in ShardRange setters Previously, we'd see warnings like UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal when setting lower/upper bounds with non-ascii byte strings. Change-Id: I328f297a5403d7e59db95bc726428a3f92df88e1 --- swift/common/utils.py | 13 +++++++++---- test/unit/common/test_utils.py | 13 +++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/swift/common/utils.py b/swift/common/utils.py index 458652840b..df8713d3a8 100644 --- a/swift/common/utils.py +++ b/swift/common/utils.py @@ -48,6 +48,7 @@ import ctypes.util from copy import deepcopy from optparse import OptionParser import traceback +import warnings from tempfile import gettempdir, mkstemp, NamedTemporaryFile import glob @@ -4985,8 +4986,10 @@ class ShardRange(object): @lower.setter def lower(self, value): - if value in (None, b'', u''): - value = ShardRange.MIN + with warnings.catch_warnings(): + warnings.simplefilter('ignore', UnicodeWarning) + if value in (None, b'', u''): + value = ShardRange.MIN try: value = self._encode_bound(value) except TypeError as err: @@ -5011,8 +5014,10 @@ class ShardRange(object): @upper.setter def upper(self, value): - if value in (None, b'', u''): - value = ShardRange.MAX + with warnings.catch_warnings(): + warnings.simplefilter('ignore', UnicodeWarning) + if value in (None, b'', u''): + value = ShardRange.MAX try: value = self._encode_bound(value) except TypeError as err: diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py index b3a1c9728e..b91be13299 100644 --- a/test/unit/common/test_utils.py +++ b/test/unit/common/test_utils.py @@ -44,6 +44,7 @@ import sys import json import math import inspect +import warnings import six from six import StringIO @@ -7585,8 +7586,10 @@ class TestShardRange(unittest.TestCase): expected = u'\N{SNOWMAN}' if six.PY2: expected = expected.encode('utf-8') - do_test(u'\N{SNOWMAN}', expected) - do_test(u'\N{SNOWMAN}'.encode('utf-8'), expected) + with warnings.catch_warnings(record=True) as captured_warnings: + do_test(u'\N{SNOWMAN}', expected) + do_test(u'\N{SNOWMAN}'.encode('utf-8'), expected) + self.assertFalse(captured_warnings) sr = utils.ShardRange('a/c', utils.Timestamp.now(), 'b', 'y') sr.lower = '' @@ -7633,8 +7636,10 @@ class TestShardRange(unittest.TestCase): expected = u'\N{SNOWMAN}' if six.PY2: expected = expected.encode('utf-8') - do_test(u'\N{SNOWMAN}', expected) - do_test(u'\N{SNOWMAN}'.encode('utf-8'), expected) + with warnings.catch_warnings(record=True) as captured_warnings: + do_test(u'\N{SNOWMAN}', expected) + do_test(u'\N{SNOWMAN}'.encode('utf-8'), expected) + self.assertFalse(captured_warnings) sr = utils.ShardRange('a/c', utils.Timestamp.now(), 'b', 'y') sr.upper = ''