Skip to content

Commit fffab9b

Browse files
committed
fix: make rest.py still load on python2, do not test bad json
It's not worth fixing the test to make it work on python2. But do define the missing JSONDecodeError as it's base class ValueError on python2.
1 parent 4039c14 commit fffab9b

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

roundup/rest.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
from datetime import timedelta
1919
from hashlib import md5
2020

21+
try:
22+
from json import JSONDecodeError
23+
except ImportError:
24+
JSONDecodeError = ValueError
25+
2126
try:
2227
from urllib.parse import urlparse
2328
except ImportError:
@@ -2747,7 +2752,7 @@ def raise_error_on_constant(x):
27472752
parse_constant=raise_error_on_constant)
27482753
self.value = [self.FsValue(index, self.json_dict[index])
27492754
for index in self.json_dict]
2750-
except (json.decoder.JSONDecodeError, ValueError) as e:
2755+
except (JSONDecodeError, ValueError) as e:
27512756
raise ValueError(e.args[0] + ". JSON is: " + json_string)
27522757

27532758

test/rest_common.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
import unittest
33
import shutil
4+
import sys
45
import errno
56

67
from time import sleep
@@ -63,6 +64,13 @@ def dst(self, dt):
6364
skip_jwt = mark_class(pytest.mark.skip(
6465
reason='Skipping JWT tests: jwt library not available'))
6566

67+
if sys.version_info[0] > 2:
68+
skip_on_py2 = lambda func, *args, **kwargs: func
69+
else:
70+
from .pytest_patcher import mark_class
71+
skip_on_py2 =mark_class(pytest.mark.skip(
72+
reason='Skipping test on Python 2'))
73+
6674
NEEDS_INSTANCE = 1
6775

6876

@@ -2427,6 +2435,7 @@ def testDispatchBadAccept(self):
24272435
json_dict = json.loads(b2s(results))
24282436
self.assertIn('Unable to parse Accept Header. Invalid param: foo. Acceptable types: */*, application/json', json_dict['error']['msg'])
24292437

2438+
@skip_on_py2
24302439
def testBadJson(self):
24312440
'''Run some JSON we don't accept through the wringer
24322441
'''
@@ -2509,7 +2518,6 @@ def testBadJson(self):
25092518

25102519
self.assertEqual(json.loads(results), expected)
25112520

2512-
25132521
def testStatsGen(self):
25142522
# check stats being returned by put and get ops
25152523
# using dispatch which parses the @stats query param

0 commit comments

Comments
 (0)