Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
62 changes: 39 additions & 23 deletions crates/vm/src/builtins/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,46 @@ pub(crate) fn key_hash<K: DictKey + ?Sized>(
mapping_name: &str,
vm: &VirtualMachine,
) -> PyResult<PyHash> {
match key.key_hash(vm) {
// Exact TypeError only: exception subclasses retain their identity.
Err(exc) if exc.class().is(vm.ctx.exceptions.type_error) => {
let name = key.to_pyobject(vm).class().fully_qualified_name(vm)?;
// Match BaseException_str without the diagnostic formatter's
// fallback: argument __str__/__repr__ failures must propagate.
let args = exc.args();
let message = match args.as_slice() {
[] => vm.ctx.empty_str.to_owned(),
[arg] => arg.str(vm)?,
_ => args.as_object().str(vm)?,
};
Err(vm.new_type_error(wtf8_concat!(
"cannot use '",
name,
"' as a ",
mapping_name,
" key (",
message.as_wtf8(),
")"
)))
key.key_hash(vm)
.map_err(|exc| contextualize_hash_error(key, mapping_name, "key", exc, vm))
}

pub(crate) fn contextualize_hash_error<K: DictKey + ?Sized>(
key: &K,
container: &str,
role: &str,
exc: PyBaseExceptionRef,
vm: &VirtualMachine,
) -> PyBaseExceptionRef {
let format_error = || -> PyResult<PyBaseExceptionRef> {
match exc {
// Exact TypeError only: exception subclasses retain their identity.
exc if exc.class().is(vm.ctx.exceptions.type_error) => {
let name = key.to_pyobject(vm).class().fully_qualified_name(vm)?;
// Match BaseException_str without the diagnostic formatter's
// fallback: argument __str__/__repr__ failures must propagate.
let args = exc.args();
let message = match args.as_slice() {
[] => vm.ctx.empty_str.to_owned(),
[arg] => arg.str(vm)?,
_ => args.as_object().str(vm)?,
};
Ok(vm.new_type_error(wtf8_concat!(
"cannot use '",
name,
"' as a ",
container,
" ",
role,
" (",
message.as_wtf8(),
")"
)))
}
exc => Ok(exc),
}
result => result,
}
};
format_error().unwrap_or_else(|exc| exc)
}

#[pyclass(module = false, name = "dict", unhashable = true, traverse = "manual")]
Expand Down
26 changes: 23 additions & 3 deletions crates/vm/src/builtins/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ unsafe impl Traverse for PySet {
}

impl PySet {
/// Freeze a working set without hashing or comparing its keys again.
pub(super) fn into_frozen(self) -> PyFrozenSet {
PyFrozenSet {
// Working set tables can have mutable aliases; copy the stored
// entries so the result remains immutable after those aliases run.
inner: self.inner.copy(),
..Default::default()
}
}

#[deprecated(note = "Use `PySet::default().into_ref(ctx)` instead")]
pub fn new_ref(ctx: &Context) -> PyRef<Self> {
Self::default().into_ref(ctx)
Expand Down Expand Up @@ -350,7 +360,7 @@ impl PySetInner {
}

/// Look up a key whose hash is already known, without a frozenset retry.
fn contains_known_hash(
pub(super) fn contains_known_hash(
&self,
needle: &PyObject,
hash: PyHash,
Expand Down Expand Up @@ -562,17 +572,27 @@ impl PySetInner {
fn add(&self, item: &PyObject, vm: &VirtualMachine) -> PyResult<()> {
let result = (|| {
let hash = item.hash(vm)?;
self.content.insert(vm, item, hash, ())
self.insert_known_hash(item, hash, vm)
})();
Self::wrap_unhashable_error(result, item, vm)
}

/// [`Self::add`] with a known hash.
fn add_known_hash(&self, item: &PyObject, hash: PyHash, vm: &VirtualMachine) -> PyResult<()> {
let result = self.content.insert(vm, item, hash, ());
let result = self.insert_known_hash(item, hash, vm);
Self::wrap_unhashable_error(result, item, vm)
}

/// Insert a key without translating exceptions from its equality callback.
pub(super) fn insert_known_hash(
&self,
item: &PyObject,
hash: PyHash,
vm: &VirtualMachine,
) -> PyResult<()> {
self.content.insert(vm, item, hash, ())
}

fn remove(&self, item: &PyObject, vm: &VirtualMachine) -> PyResult<()> {
let result = self.retry_op_with_frozenset(item, vm, |item, vm| {
let hash = item.hash(vm)?;
Expand Down
Loading
Loading