Repository navigation
Hash a str subclass attribute name through its own __hash__
#9921
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,15 +3,16 @@ | |
| use crate::{ | ||
| AsObject, Py, PyObject, PyObjectRef, PyRef, PyResult, TryFromObject, VirtualMachine, | ||
| builtins::{ | ||
| PyBaseObject, PyBytes, PyDict, PyDictRef, PyGenericAlias, PyInt, PyListRef, PyStr, PyTuple, | ||
| PyTupleRef, PyType, PyTypeRef, PyUtf8Str, int::check_int_to_str_digits, pystr::AsPyStr, | ||
| PyBaseExceptionRef, PyBaseObject, PyBytes, PyDict, PyDictRef, PyGenericAlias, PyInt, | ||
| PyListRef, PyStr, PyTuple, PyTupleRef, PyType, PyTypeRef, PyUtf8Str, | ||
| int::check_int_to_str_digits, pystr::AsPyStr, | ||
| }, | ||
| common::{hash::PyHash, str::to_ascii}, | ||
| convert::ToPyObject, | ||
| dict_inner::DictKey, | ||
| function::{Either, FuncArgs, PyArithmeticValue, PySetterValue}, | ||
| protocol::PyIter, | ||
| types::{Constructor, PyComparisonOp}, | ||
| types::{Constructor, PyComparisonOp, PyTypeFlags}, | ||
| }; | ||
|
|
||
| /// Converts an index key the way a sequence does before it refuses the | ||
|
|
@@ -231,19 +232,33 @@ impl PyObject { | |
| return Err(exc); | ||
| }; | ||
|
|
||
| // Only the object key runs a `str` subclass's `__hash__`. | ||
| let exact_str = attr_name.class().is(vm.ctx.types.str_type); | ||
| // A `KeyError` raised by that `__hash__` reads as a missing attribute | ||
| // too, on assignment as well as on deletion. | ||
| let key_error_to_attribute_error = |e: PyBaseExceptionRef| { | ||
| if e.fast_isinstance(vm.ctx.exceptions.key_error) { | ||
| vm.new_no_attribute_error(self.to_owned(), attr_name.to_owned()) | ||
| } else { | ||
| e | ||
| } | ||
| }; | ||
| if let PySetterValue::Assign(value) = value { | ||
| instance_dict | ||
| .get_or_insert(vm) | ||
| .set_item(attr_name, value, vm)?; | ||
| let dict = instance_dict.get_or_insert(vm); | ||
| if exact_str { | ||
| dict.set_item(attr_name, value, vm) | ||
| } else { | ||
| dict.set_item(attr_name.as_object(), value, vm) | ||
| } | ||
| .map_err(key_error_to_attribute_error)?; | ||
| instance_dict.maybe_materialize_inline_values(); | ||
| } else if let Some(dict) = instance_dict.get() { | ||
| dict.del_item(attr_name, vm).map_err(|e| { | ||
| if e.fast_isinstance(vm.ctx.exceptions.key_error) { | ||
| vm.new_no_attribute_error(self.to_owned(), attr_name.to_owned()) | ||
| } else { | ||
| e | ||
| } | ||
| })?; | ||
| if exact_str { | ||
| dict.del_item(attr_name, vm) | ||
| } else { | ||
| dict.del_item(attr_name.as_object(), vm) | ||
| } | ||
| .map_err(key_error_to_attribute_error)?; | ||
| } else { | ||
| return Err(vm.new_no_attribute_error(self.to_owned(), attr_name.to_owned())); | ||
| } | ||
|
|
@@ -284,7 +299,19 @@ impl PyObject { | |
| let attr = if let Some(dict) = dict { | ||
| // `Py<PyStr>` rather than its `&Wtf8`: the key type carries the | ||
| // cached hash and compares interned keys by pointer. | ||
| dict.get_item_opt(name_str, vm)? | ||
| if name_str.class().is(vm.ctx.types.str_type) { | ||
| dict.get_item_opt(name_str, vm)? | ||
| } else if dict.is_empty() | ||
| && !(obj_cls.slots.flags.has_feature(PyTypeFlags::MANAGED_DICT) | ||
| && obj_cls.slots.itemsize == 0) | ||
| { | ||
| // An empty dict that is not a managed one stands for a dict | ||
| // not created yet: skip the lookup. | ||
| None | ||
|
jseop-lim marked this conversation as resolved.
Resolved
|
||
| } else { | ||
| // Only the object key runs a subclass's `__hash__`. | ||
| dict.get_item_opt(name_str.as_object(), vm)? | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For Useful? React with 👍 / 👎. |
||
| } | ||
| } else { | ||
| None | ||
| }; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.