Skip to content

Commit e8d7437

Browse files
authored
Update test_types.py to 3.14.5 (RustPython#7912)
1 parent 5394129 commit e8d7437

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

Lib/test/test_types.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,8 +700,7 @@ def test_traceback_and_frame_types(self):
700700
self.assertIsInstance(exc.__traceback__, types.TracebackType)
701701
self.assertIsInstance(exc.__traceback__.tb_frame, types.FrameType)
702702

703-
# XXX: RUSTPYTHON
704-
@unittest.skipUnless(_datetime, "requires _datetime module")
703+
@unittest.expectedFailure # TODO: RUSTPYTHON; AttributeError: 'NoneType' object has no attribute 'datetime_CAPI'
705704
def test_capsule_type(self):
706705
self.assertIsInstance(_datetime.datetime_CAPI, types.CapsuleType)
707706

@@ -2127,6 +2126,21 @@ class Spam(types.SimpleNamespace):
21272126
self.assertIs(type(spam2), Spam)
21282127
self.assertEqual(vars(spam2), {'ham': 5, 'eggs': 9})
21292128

2129+
def test_replace_invalid_subtype(self):
2130+
# See https://github.com/python/cpython/issues/143636.
2131+
class MyNS(types.SimpleNamespace):
2132+
def __new__(cls, *args, **kwargs):
2133+
if created:
2134+
return 12345
2135+
return super().__new__(cls)
2136+
2137+
created = False
2138+
ns = MyNS()
2139+
created = True
2140+
err = (r"^expect types\.SimpleNamespace type, "
2141+
r"but .+\.MyNS\(\) returned 'int' object")
2142+
self.assertRaisesRegex(TypeError, err, copy.replace, ns)
2143+
21302144
def test_fake_namespace_compare(self):
21312145
# Issue #24257: Incorrect use of PyObject_IsInstance() caused
21322146
# SystemError.

crates/vm/src/builtins/namespace.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ impl PyNamespace {
5454
let cls: PyObjectRef = zelf.class().to_owned().into();
5555
let result = cls.call((), vm)?;
5656

57+
if !zelf.class().is(result.class()) {
58+
return Err(vm.new_type_error(format!(
59+
"expect {} type, but {}() returned '{}' object",
60+
Self::class(&vm.ctx).slot_name(),
61+
zelf.class()
62+
.__qualname__(vm)
63+
.downcast_ref::<PyStr>()
64+
.map_or_else(
65+
|| zelf.class().name().to_string(),
66+
|n| n.as_wtf8().to_string()
67+
),
68+
result.class().name(),
69+
)));
70+
}
71+
5772
// Copy the current namespace dict to the new instance
5873
let src_dict = zelf.dict().unwrap();
5974
let dst_dict = result.dict().unwrap();

0 commit comments

Comments
 (0)