PR#4334: work around parse_qs behavior in python < 3.11

Merges #4334
https://pagure.io/koji/pull-request/4334

Fixes: #4332
https://pagure.io/koji/issue/4332
parse_qs fails on python 3.9
This commit is contained in:
Tomas Kopecek 2025-03-03 16:34:21 +01:00
commit 30cc20652d
3 changed files with 25 additions and 3 deletions

View file

@ -17,4 +17,4 @@ if importlib:
sys.modules[INDEX_MOD] = webidx
spec.loader.exec_module(webidx)
else:
cli = imp.load_source(INDEX_MOD, INDEX_FILENAME)
webidx = imp.load_source(INDEX_MOD, INDEX_FILENAME)

View file

@ -19,6 +19,7 @@ class TestSearch(unittest.TestCase):
}
urlencode_data = "terms=test&type=package&match=testmatch"
self.fs = FieldStorageCompat({'QUERY_STRING': urlencode_data})
self.gen_html = mock.patch.object(webidx, '_genHTML').start()
def __get_server(env):
env['koji.form'] = self.fs
@ -29,6 +30,18 @@ class TestSearch(unittest.TestCase):
def tearDown(self):
mock.patch.stopall()
def test_no_args(self):
self.fs = FieldStorageCompat({'QUERY_STRING': ''})
webidx.search(self.environ)
self.gen_html.assert_called_once()
# extract values
# called as _genHTML(environ, 'search.chtml')
args = self.gen_html.call_args_list[0][0] # no kwargs passed here
environ = args[0]
self.assertEqual(environ['koji.values']['terms'], '')
def test_search_exception_match(self):
"""Test taskinfo function raises exception"""
self.server.getBuildTarget.return_info = None
@ -37,3 +50,7 @@ class TestSearch(unittest.TestCase):
with self.assertRaises(koji.GenericError) as cm:
webidx.search(self.environ)
self.assertEqual(str(cm.exception), "No such match type: 'testmatch'")
self.gen_html.assert_not_called()
# the end

View file

@ -211,8 +211,13 @@ class FieldStorageCompat(Mapping):
"""Emulate the parts of cgi.FieldStorage that we need"""
def __init__(self, environ):
data = parse_qs(environ.get('QUERY_STRING', ''), strict_parsing=True,
keep_blank_values=True)
qs = environ.get('QUERY_STRING', '')
if not qs:
# for python < 3.11, parse_qs will error on a blank string
self.data = {}
return
data = parse_qs(qs, strict_parsing=True, keep_blank_values=True)
# replace singleton lists with single values
for arg in data:
val = data[arg]