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
This commit is contained in:
Tim Burke 2020-05-04 21:35:58 -07:00
parent 948289151b
commit fe74ec0489
2 changed files with 18 additions and 8 deletions
swift/common
test/unit/common

@ -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:

@ -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 = ''