Skip to content

Commit 096a3dc

Browse files
authored
Fix builtins (has,get,set)attr type error messages (RustPython#4776)
1 parent 11aefa3 commit 096a3dc

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

Lib/test/test_builtin.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,6 @@ def test_compile_async_generator(self):
523523
exec(co, glob)
524524
self.assertEqual(type(glob['ticker']()), AsyncGeneratorType)
525525

526-
# TODO: RUSTPYTHON
527-
@unittest.expectedFailure
528526
def test_delattr(self):
529527
sys.spam = 1
530528
delattr(sys, 'spam')
@@ -947,8 +945,6 @@ def test_filter_pickle(self):
947945
f2 = filter(filter_char, "abcdeabcde")
948946
self.check_iter_pickle(f1, list(f2), proto)
949947

950-
# TODO: RUSTPYTHON
951-
@unittest.expectedFailure
952948
def test_getattr(self):
953949
self.assertTrue(getattr(sys, 'stdout') is sys.stdout)
954950
self.assertRaises(TypeError, getattr)
@@ -960,8 +956,6 @@ def test_getattr(self):
960956
# unicode surrogates are not encodable to the default encoding (utf8)
961957
self.assertRaises(AttributeError, getattr, 1, "\uDAD1\uD51E")
962958

963-
# TODO: RUSTPYTHON
964-
@unittest.expectedFailure
965959
def test_hasattr(self):
966960
self.assertTrue(hasattr(sys, 'stdout'))
967961
self.assertRaises(TypeError, hasattr)
@@ -1614,8 +1608,6 @@ def test_bug_27936(self):
16141608
self.assertEqual(round(x, None), round(x))
16151609
self.assertEqual(type(round(x, None)), type(round(x)))
16161610

1617-
# TODO: RUSTPYTHON
1618-
@unittest.expectedFailure
16191611
def test_setattr(self):
16201612
setattr(sys, 'spam', 1)
16211613
self.assertEqual(sys.spam, 1)

vm/src/stdlib/builtins.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,14 @@ mod builtins {
186186
}
187187

188188
#[pyfunction]
189-
fn delattr(obj: PyObjectRef, attr: PyStrRef, vm: &VirtualMachine) -> PyResult<()> {
190-
obj.del_attr(&attr, vm)
189+
fn delattr(obj: PyObjectRef, attr: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
190+
let attr = attr.try_to_ref::<PyStr>(vm).map_err(|_e| {
191+
vm.new_type_error(format!(
192+
"attribute name must be string, not '{}'",
193+
attr.class().name()
194+
))
195+
})?;
196+
obj.del_attr(attr, vm)
191197
}
192198

193199
#[pyfunction]
@@ -321,14 +327,21 @@ mod builtins {
321327
#[pyfunction]
322328
fn getattr(
323329
obj: PyObjectRef,
324-
attr: PyStrRef,
330+
attr: PyObjectRef,
325331
default: OptionalArg<PyObjectRef>,
326332
vm: &VirtualMachine,
327333
) -> PyResult {
334+
let attr = attr.try_to_ref::<PyStr>(vm).map_err(|_e| {
335+
vm.new_type_error(format!(
336+
"attribute name must be string, not '{}'",
337+
attr.class().name()
338+
))
339+
})?;
340+
328341
if let OptionalArg::Present(default) = default {
329-
Ok(vm.get_attribute_opt(obj, &attr)?.unwrap_or(default))
342+
Ok(vm.get_attribute_opt(obj, attr)?.unwrap_or(default))
330343
} else {
331-
obj.get_attr(&attr, vm)
344+
obj.get_attr(attr, vm)
332345
}
333346
}
334347

@@ -338,8 +351,14 @@ mod builtins {
338351
}
339352

340353
#[pyfunction]
341-
fn hasattr(obj: PyObjectRef, attr: PyStrRef, vm: &VirtualMachine) -> PyResult<bool> {
342-
Ok(vm.get_attribute_opt(obj, &attr)?.is_some())
354+
fn hasattr(obj: PyObjectRef, attr: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
355+
let attr = attr.try_to_ref::<PyStr>(vm).map_err(|_e| {
356+
vm.new_type_error(format!(
357+
"attribute name must be string, not '{}'",
358+
attr.class().name()
359+
))
360+
})?;
361+
Ok(vm.get_attribute_opt(obj, attr)?.is_some())
343362
}
344363

345364
#[pyfunction]
@@ -742,11 +761,17 @@ mod builtins {
742761
#[pyfunction]
743762
fn setattr(
744763
obj: PyObjectRef,
745-
attr: PyStrRef,
764+
attr: PyObjectRef,
746765
value: PyObjectRef,
747766
vm: &VirtualMachine,
748767
) -> PyResult<()> {
749-
obj.set_attr(&attr, value, vm)?;
768+
let attr = attr.try_to_ref::<PyStr>(vm).map_err(|_e| {
769+
vm.new_type_error(format!(
770+
"attribute name must be string, not '{}'",
771+
attr.class().name()
772+
))
773+
})?;
774+
obj.set_attr(attr, value, vm)?;
750775
Ok(())
751776
}
752777

0 commit comments

Comments
 (0)