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
16 changes: 16 additions & 0 deletions Lib/test/test_type_annotations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import annotationlib
import inspect
import itertools
import textwrap
import types
import unittest
Expand Down Expand Up @@ -782,6 +783,21 @@ def test_non_name_annotations(self):


class RegressionTests(unittest.TestCase):
# gh-154902
def test_conditional_annotations_rebound(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this test originated from CPython? otherwise do not directly edit Lib/ files

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This is copied verbatim from CPython's official 3.14 maintenance backport c21e7c45, including the accompanying import itertools. The test body, input values, and assertions are unchanged. The only difference is placement within RegressionTests: this PR inserts it before the existing tests, while upstream appended it after test_complex_comprehension_inlining.

# user code can rebind __conditional_annotations__ to any object
lefts = ("__conditional_annotations__",
'globals()["__conditional_annotations__"]')
values = ("0", "{}", "[]", "''", "object()", "frozenset()")
for left, value in itertools.product(lefts, values):
with self.subTest(left=left, value=value):
code = f"""
{left} = {value}
x: int
"""
with self.assertRaises(TypeError):
run_code(code)

# gh-132479
def test_complex_comprehension_inlining(self):
# Test that the various repro cases from the issue don't crash
Expand Down
8 changes: 4 additions & 4 deletions crates/vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5587,10 +5587,10 @@ impl ExecutingFrame<'_> {
Instruction::SetAdd { i } => {
let item = self.pop_value();
let obj = self.nth_value(i.get(arg) - 1);
let set: &Py<PySet> = unsafe {
// SAFETY: trust compiler
obj.downcast_unchecked_ref()
};
// Annotation tracking can load a user-rebound global here.
let set = obj.downcast_ref_if_exact::<PySet>(vm).ok_or_else(|| {
vm.new_type_error(format!("'{}' object is not a set", obj.class().name()))
})?;
set.add(item, vm)?;
Ok(None)
}
Expand Down
24 changes: 21 additions & 3 deletions crates/vm/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1514,9 +1514,7 @@ impl VirtualMachine {

// No stdio: set to None (embedding use case)
#[cfg(not(feature = "stdio"))]
let make_stdio = |_name: &str, _fd: i32, _write: bool| {
Ok(crate::builtins::PyNone.into_pyobject(self))
};
let make_stdio = |_name: &str, _fd: i32, _write: bool| Ok(self.ctx.none());

let set_stdio = |name, fd, write| {
let stdio: PyObjectRef = make_stdio(name, fd, write)?;
Expand Down Expand Up @@ -3925,6 +3923,26 @@ pub fn resolve_frozen_alias(name: &str) -> &str {
mod tests {
use super::*;

#[cfg(not(feature = "stdio"))]
#[test]
fn no_stdio_streams_are_none_singleton() {
Interpreter::builder(Default::default())
.build()
.enter(|vm| {
for name in [
"stdin",
"stdout",
"stderr",
"__stdin__",
"__stdout__",
"__stderr__",
] {
let stream = vm.sys_module.get_attr(name, vm).unwrap();
assert!(vm.is_none(&stream), "sys.{name} must be the None singleton");
}
});
}

#[test]
fn nested_frozen() {
use rustpython_vm as vm;
Expand Down
Loading