Skip to content

Delete a key equal to a string key from a DynamicObjectStorage - #1396

Open
taxor03 wants to merge 1 commit into
oracle:masterfrom
taxor03:GH-dom-delitem-non-str-key
Open

taxor03 wants to merge 1 commit into
oracle:masterfrom
taxor03:GH-dom-delitem-non-str-key

Conversation

@taxor03

@taxor03 taxor03 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #1393

HashingStorageDelItem.domStringKey hashed a key that is not a built-in str and then reported it as not found. On the dicts that use a DynamicObjectStorage (a module's __dict__, globals(), the dict locals() returns), del d[k] and d.pop(k) therefore raised KeyError for a key equal to one of the string keys, although d[k] found it. Getting an item already compares such a key with the string keys in DynamicObjectStorage.GetItemNoStringKeyNode.

AS-IS

>>> import types
>>> class Key:
...     def __init__(self, v): self.v = v
...     def __hash__(self): return hash(self.v)
...     def __eq__(self, o): return self.v == o
>>> m = types.ModuleType("m"); m.y = 2
>>> vars(m).pop(Key("y"))
KeyError: <__main__.Key object at 0x...>

TO-BE

2

(matching CPython)

Changes

  • In HashingStorageDelItem.domStringKey, for a key that is not a built-in str, look for a string key with the same hash that compares equal, as GetItemNoStringKeyNode does, and delete that key.
  • Add test_module_dict_del_non_str_key_equal_to_str_key to tests/test_dict.py: pop and del with such a key on a module's __dict__ and on locals(), pop returning the default once the key is gone, and KeyError from del for a missing key. It passes on CPython 3.12, 3.13 and 3.14.

Testing

mx build on darwin-aarch64 (master 3176851), then mx graalpytest test_dict (85 passed) and --tagged test_dict (101), test_module (36) and test_scope (37). Without the change, the new test fails at the first pop. mx checkstyle --primary and mx eclipseformat --primary report nothing.

Signed-off-by: Sungmin Park <ryan040@korea.ac.kr>
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Dec 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: del and pop() raise KeyError on a module's __dict__, globals() and locals() for a key that equals a string key

1 participant