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

View File

@ -48,6 +48,7 @@ import ctypes.util
from copy import deepcopy from copy import deepcopy
from optparse import OptionParser from optparse import OptionParser
import traceback import traceback
import warnings
from tempfile import gettempdir, mkstemp, NamedTemporaryFile from tempfile import gettempdir, mkstemp, NamedTemporaryFile
import glob import glob
@ -4985,6 +4986,8 @@ class ShardRange(object):
@lower.setter @lower.setter
def lower(self, value): def lower(self, value):
with warnings.catch_warnings():
warnings.simplefilter('ignore', UnicodeWarning)
if value in (None, b'', u''): if value in (None, b'', u''):
value = ShardRange.MIN value = ShardRange.MIN
try: try:
@ -5011,6 +5014,8 @@ class ShardRange(object):
@upper.setter @upper.setter
def upper(self, value): def upper(self, value):
with warnings.catch_warnings():
warnings.simplefilter('ignore', UnicodeWarning)
if value in (None, b'', u''): if value in (None, b'', u''):
value = ShardRange.MAX value = ShardRange.MAX
try: try:

View File

@ -44,6 +44,7 @@ import sys
import json import json
import math import math
import inspect import inspect
import warnings
import six import six
from six import StringIO from six import StringIO
@ -7585,8 +7586,10 @@ class TestShardRange(unittest.TestCase):
expected = u'\N{SNOWMAN}' expected = u'\N{SNOWMAN}'
if six.PY2: if six.PY2:
expected = expected.encode('utf-8') expected = expected.encode('utf-8')
with warnings.catch_warnings(record=True) as captured_warnings:
do_test(u'\N{SNOWMAN}', expected) do_test(u'\N{SNOWMAN}', expected)
do_test(u'\N{SNOWMAN}'.encode('utf-8'), 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 = utils.ShardRange('a/c', utils.Timestamp.now(), 'b', 'y')
sr.lower = '' sr.lower = ''
@ -7633,8 +7636,10 @@ class TestShardRange(unittest.TestCase):
expected = u'\N{SNOWMAN}' expected = u'\N{SNOWMAN}'
if six.PY2: if six.PY2:
expected = expected.encode('utf-8') expected = expected.encode('utf-8')
with warnings.catch_warnings(record=True) as captured_warnings:
do_test(u'\N{SNOWMAN}', expected) do_test(u'\N{SNOWMAN}', expected)
do_test(u'\N{SNOWMAN}'.encode('utf-8'), 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 = utils.ShardRange('a/c', utils.Timestamp.now(), 'b', 'y')
sr.upper = '' sr.upper = ''