Skip to content

Commit 3c3fc84

Browse files
authored
Move istr_canonical() into istr_object.h (#1754)
<!-- Thank you for your contribution! --> ## What do these changes do? Moves `istr_canonical()` from `htkeys.h` to `istr_object.h`, next to the `istrobject` struct it reads, and uses it in `identity.h` in place of the three direct `((istrobject*)key)->canonical` reads. The only remaining direct accesses are in `istr.h`, where the field is set and released. C extension only; the pure-Python backend has no equivalent. ## Are there changes in behavior for the user? No. ## Is it a substantial burden for the maintainers to support this? No, it is a header reshuffle with no code generation change expected. ## Related issue number N/A ## Checklist - [x] I think the code is well written - [ ] Unit tests for the changes exist: N/A, no behaviour change; covered by the existing suite - [ ] Documentation reflects the changes: N/A - [ ] If you provide code modification, please add yourself to `CONTRIBUTORS.txt`: N/A - [x] Add a new news fragment into the `CHANGES/` folder - [x] `make doc-spelling` passes and any new technical words are added to `docs/spelling_wordlist.txt` Drafted with Claude Code (Claude Opus 5.5); reviewed by @asvetlov. <details> <summary>Agent run details (optional, for reviewers)</summary> Tests, C extension: `pytest -q` on 3.14.7 (4198 passed, 182 skipped) and 3.10.17 (4202 passed, 178 skipped) Tests, pure Python: `MULTIDICT_NO_EXTENSIONS=1 pytest -q --no-c-extensions` on 3.14.7 (1664 passed, 24 skipped) Inlining: `tools/check_inlining.py` on 3.14.7 and 3.14.7t, ok (12 and 16 rules) Docs: `make doc-spelling`, build succeeded </details>
1 parent ccdd836 commit 3c3fc84

4 files changed

Lines changed: 11 additions & 9 deletions

File tree

‎CHANGES/1754.misc.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Moved ``istr_canonical()`` next to the ``istrobject`` struct and used it
2+
for every read of an istr's canonical form -- by :user:`asvetlov`.

‎multidict/_multilib/htkeys.h‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,6 @@ kind_is_compact(uint8_t kind)
212212
return kind == KIND_COMPACT;
213213
}
214214

215-
static inline PyObject*
216-
istr_canonical(PyObject* key)
217-
{
218-
return ((istrobject*)key)->canonical;
219-
}
220-
221215
/* The identity of a compact entry's key: the key itself in a MultiDict's
222216
table (_cs), the istr's canonical in a CIMultiDict's (_ci). */
223217
static inline PyObject*

‎multidict/_multilib/identity.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ static inline PyObject*
180180
_key_to_identity_ci(mod_state* state, PyObject* key)
181181
{
182182
if (IStr_CheckExact(state, key)) {
183-
return Py_NewRef(((istrobject*)key)->canonical);
183+
return Py_NewRef(istr_canonical(key));
184184
}
185185
return _str_to_identity_ci(state, key);
186186
}
@@ -241,7 +241,7 @@ md_borrow_identity(MultiDictObject* md, PyObject* key)
241241
{
242242
if (md->is_ci) {
243243
if (IStr_CheckExact(md->state, key)) {
244-
return ((istrobject*)key)->canonical;
244+
return istr_canonical(key);
245245
}
246246
return _str_borrow_identity_ci(key);
247247
}
@@ -260,7 +260,7 @@ md_calc_identity_fits(MultiDictObject* md, PyObject* key, bool* pfits)
260260
if (md->is_ci) {
261261
if (IStr_CheckExact(md->state, key)) {
262262
*pfits = true;
263-
return Py_NewRef(((istrobject*)key)->canonical);
263+
return Py_NewRef(istr_canonical(key));
264264
}
265265
*pfits = false;
266266
return _str_to_identity_ci(md->state, key);

‎multidict/_multilib/istr_object.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ typedef struct {
1717
Py_hash_t canonical_hash;
1818
} istrobject;
1919

20+
static inline PyObject*
21+
istr_canonical(PyObject* key)
22+
{
23+
return ((istrobject*)key)->canonical;
24+
}
25+
2026
#ifdef __cplusplus
2127
}
2228
#endif

0 commit comments

Comments
 (0)