changeset 8220:818751637b77

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.
author John Rouillard <rouilj@ieee.org>
date Tue, 17 Dec 2024 21:42:45 -0500
parents 9404d56d830f
children 3d7292d222d1
files roundup/rest.py test/rest_common.py
diffstat 2 files changed, 15 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/roundup/rest.py	Tue Dec 17 20:45:28 2024 -0500
+++ b/roundup/rest.py	Tue Dec 17 21:42:45 2024 -0500
@@ -19,6 +19,11 @@
 from hashlib import md5
 
 try:
+    from json import JSONDecodeError
+except ImportError:
+    JSONDecodeError = ValueError
+
+try:
     from urllib.parse import urlparse
 except ImportError:
     from urlparse import urlparse
@@ -2747,7 +2752,7 @@
                                     parse_constant=raise_error_on_constant)
             self.value = [self.FsValue(index, self.json_dict[index])
                           for index in self.json_dict]
-        except (json.decoder.JSONDecodeError, ValueError) as e:
+        except (JSONDecodeError, ValueError) as e:
             raise ValueError(e.args[0] + ". JSON is: " + json_string)
 
 
--- a/test/rest_common.py	Tue Dec 17 20:45:28 2024 -0500
+++ b/test/rest_common.py	Tue Dec 17 21:42:45 2024 -0500
@@ -1,6 +1,7 @@
 import pytest
 import unittest
 import shutil
+import sys
 import errno
 
 from time import sleep
@@ -63,6 +64,13 @@
     skip_jwt = mark_class(pytest.mark.skip(
         reason='Skipping JWT tests: jwt library not available'))
 
+if sys.version_info[0] > 2:
+    skip_on_py2 = lambda func, *args, **kwargs: func
+else:
+    from .pytest_patcher import mark_class
+    skip_on_py2 =mark_class(pytest.mark.skip(
+        reason='Skipping test on Python 2'))
+
 NEEDS_INSTANCE = 1
 
 
@@ -2427,6 +2435,7 @@
         json_dict = json.loads(b2s(results))
         self.assertIn('Unable to parse Accept Header. Invalid param: foo. Acceptable types: */*, application/json', json_dict['error']['msg'])
 
+    @skip_on_py2
     def testBadJson(self):
         '''Run some JSON we don't accept through the wringer
         '''
@@ -2509,7 +2518,6 @@
 
         self.assertEqual(json.loads(results), expected)
 
-
     def testStatsGen(self):
         # check stats being returned by put and get ops
         # using dispatch which parses the @stats query param

Roundup Issue Tracker: http://roundup-tracker.org/