Skip to content

Fix creating CIMultiDict from MultiDict not making keys case-insentive #1112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/1112.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed keys not becoming case-insensitive when :class:`multidict.CIMultiDict` is created by passing in a :class:`multidict.MultiDict` -- by :user:`bdraco`.
11 changes: 9 additions & 2 deletions multidict/_multidict_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ def _title(self, key: str) -> str:


class _CIMixin:
_ci: bool = True

def _key(self, key: str) -> str:
if type(key) is istr:
return key
Expand All @@ -474,6 +476,7 @@ def _title(self, key: str) -> str:

class _Base(MultiMapping[_V]):
_impl: _Impl[_V]
_ci: bool = False

@abstractmethod
def _key(self, key: str) -> str: ...
Expand Down Expand Up @@ -628,9 +631,13 @@ def _extend(
) -> None:
if arg:
if isinstance(arg, (MultiDict, MultiDictProxy)):
items = arg._impl._items
if self._ci != arg._ci:
items = [(self._title(k), k, v) for _, k, v in arg._impl._items]
else:
items = arg._impl._items
if kwargs:
items = items.copy()
if kwargs:
items = items.copy()
for key, value in kwargs.items():
items.append((self._title(key), key, value))
else:
Expand Down
28 changes: 25 additions & 3 deletions multidict/_multilib/pair_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -1132,23 +1132,45 @@ static inline int
pair_list_update_from_pair_list(pair_list_t *list, PyObject* used, pair_list_t *other)
{
Py_ssize_t pos;
Py_hash_t hash;
PyObject *identity = NULL;
bool recalc_identity = list->calc_ci_indentity != other->calc_ci_indentity;

for (pos = 0; pos < other->size; pos++) {
pair_t *pair = other->pairs + pos;
if (recalc_identity) {
identity = pair_list_calc_identity(list, pair->key);
if (identity == NULL) {
goto fail;
}
hash = PyObject_Hash(identity);
if (hash == -1) {
goto fail;
}
} else {
identity = pair->identity;
hash = pair->hash;
}
if (used != NULL) {
if (_pair_list_update(list, pair->key, pair->value, used,
pair->identity, pair->hash) < 0) {
identity, hash) < 0) {
goto fail;
}
} else {
if (_pair_list_add_with_hash(list, pair->identity, pair->key,
pair->value, pair->hash) < 0) {
if (_pair_list_add_with_hash(list, identity, pair->key,
pair->value, hash) < 0) {
goto fail;
}
}
if (recalc_identity) {
Py_CLEAR(identity);
}
}
return 0;
fail:
if (recalc_identity) {
Py_CLEAR(identity);
}
return -1;
}

Expand Down
42 changes: 42 additions & 0 deletions tests/test_multidict.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
MultiMapping,
MutableMultiMapping,
)
from multidict._multidict_py import CIMultiDict as PyCIMultiDict
from multidict._multidict_py import MultiDict as PyMultiDict

_T = TypeVar("_T")

Expand Down Expand Up @@ -1197,3 +1199,43 @@ def test_create_multidict_from_existing_multidict_new_pairs() -> None:
new = MultiDict(original, h4="header4")
assert "h4" in new
assert "h4" not in original


def test_convert_multidict_to_cimultidict_and_back() -> None:
"""Test conversion from MultiDict to CIMultiDict."""
start_as_md = MultiDict([("KEY", "value1"), ("key2", "value2")])
assert start_as_md.get("KEY") == "value1"
assert start_as_md["KEY"] == "value1"
assert start_as_md.get("key2") == "value2"
assert start_as_md["key2"] == "value2"
start_as_cimd = CIMultiDict([("KEY", "value1"), ("key2", "value2")])
assert start_as_cimd.get("key") == "value1"
assert start_as_cimd["key"] == "value1"
assert start_as_cimd.get("key2") == "value2"
assert start_as_cimd["key2"] == "value2"
converted_to_ci = CIMultiDict(start_as_md)
assert converted_to_ci.get("key") == "value1"
assert converted_to_ci["key"] == "value1"
assert converted_to_ci.get("key2") == "value2"
assert converted_to_ci["key2"] == "value2"
converted_to_md = MultiDict(converted_to_ci)
assert converted_to_md.get("KEY") == "value1"
assert converted_to_md["KEY"] == "value1"
assert converted_to_md.get("key2") == "value2"
assert converted_to_md["key2"] == "value2"


def test_pure_python_convert_multidict_to_cimultidict_eq() -> None:
"""Test compare after conversion from MultiDict to CIMultiDict."""
original = PyMultiDict([("h1", "header1"), ("h2", "header2"), ("h3", "header3")])
assert PyCIMultiDict(original) == PyCIMultiDict(
[("H1", "header1"), ("H2", "header2"), ("H3", "header3")]
)


def test_convert_multidict_to_cimultidict_eq() -> None:
"""Test compare after conversion from MultiDict to CIMultiDict."""
original = MultiDict([("h1", "header1"), ("h2", "header2"), ("h3", "header3")])
assert CIMultiDict(original) == CIMultiDict(
[("H1", "header1"), ("H2", "header2"), ("H3", "header3")]
)
Loading