Skip to content

Commit 6586b06

Browse files
committed
Expose sys._is_interned and order interned-state publication
Query the existing object flag without interning or content lookup. Mark only a successful pool insertion before releasing its write lock so a concurrent lookup cannot return the entry before its flag is set. Assisted-by: OpenAI Codex: model version unavailable
1 parent 3b0dd78 commit 6586b06

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

‎crates/vm/src/intern.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ impl StringPool {
5656
let cache = CachedPyStrRef { inner: s };
5757
let mut inner = zelf.inner.write();
5858
if inner.insert(cache.clone()) {
59-
drop(inner);
6059
let interned = unsafe { cache.as_interned_str() };
6160
// `mark_intern` also makes the object immortal: the pool
6261
// never gives an entry up and its refcount could already never
@@ -65,6 +64,8 @@ impl StringPool {
6564
// read-modify-write off every incref and decref of an
6665
// attribute name, a dict key or a docstring.
6766
unsafe { interned.as_object().mark_intern() };
67+
// Publish the interned flag before another lookup can return this entry.
68+
drop(inner);
6869
interned
6970
} else {
7071
unsafe {

‎crates/vm/src/stdlib/sys.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,11 @@ pub mod sys {
13561356
vm.ctx.intern_str(string).to_owned()
13571357
}
13581358

1359+
#[pyfunction]
1360+
fn _is_interned(string: PyRef<PyStr>) -> bool {
1361+
string.as_object().is_interned()
1362+
}
1363+
13591364
#[pyattr]
13601365
fn int_info(vm: &VirtualMachine) -> PyTupleRef {
13611366
PyIntInfo::from_data(IntInfoData::INFO, vm)

0 commit comments

Comments
 (0)