forked from Rust-related/RustPython
Fix builtins (has,get,set)attr type error messages (#4776)
This commit is contained in:
8
Lib/test/test_builtin.py
vendored
8
Lib/test/test_builtin.py
vendored
@@ -523,8 +523,6 @@ class BuiltinTest(unittest.TestCase):
|
||||
exec(co, glob)
|
||||
self.assertEqual(type(glob['ticker']()), AsyncGeneratorType)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_delattr(self):
|
||||
sys.spam = 1
|
||||
delattr(sys, 'spam')
|
||||
@@ -947,8 +945,6 @@ class BuiltinTest(unittest.TestCase):
|
||||
f2 = filter(filter_char, "abcdeabcde")
|
||||
self.check_iter_pickle(f1, list(f2), proto)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_getattr(self):
|
||||
self.assertTrue(getattr(sys, 'stdout') is sys.stdout)
|
||||
self.assertRaises(TypeError, getattr)
|
||||
@@ -960,8 +956,6 @@ class BuiltinTest(unittest.TestCase):
|
||||
# unicode surrogates are not encodable to the default encoding (utf8)
|
||||
self.assertRaises(AttributeError, getattr, 1, "\uDAD1\uD51E")
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_hasattr(self):
|
||||
self.assertTrue(hasattr(sys, 'stdout'))
|
||||
self.assertRaises(TypeError, hasattr)
|
||||
@@ -1614,8 +1608,6 @@ class BuiltinTest(unittest.TestCase):
|
||||
self.assertEqual(round(x, None), round(x))
|
||||
self.assertEqual(type(round(x, None)), type(round(x)))
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_setattr(self):
|
||||
setattr(sys, 'spam', 1)
|
||||
self.assertEqual(sys.spam, 1)
|
||||
|
||||
@@ -186,8 +186,14 @@ mod builtins {
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn delattr(obj: PyObjectRef, attr: PyStrRef, vm: &VirtualMachine) -> PyResult<()> {
|
||||
obj.del_attr(&attr, vm)
|
||||
fn delattr(obj: PyObjectRef, attr: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
|
||||
let attr = attr.try_to_ref::<PyStr>(vm).map_err(|_e| {
|
||||
vm.new_type_error(format!(
|
||||
"attribute name must be string, not '{}'",
|
||||
attr.class().name()
|
||||
))
|
||||
})?;
|
||||
obj.del_attr(attr, vm)
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
@@ -321,14 +327,21 @@ mod builtins {
|
||||
#[pyfunction]
|
||||
fn getattr(
|
||||
obj: PyObjectRef,
|
||||
attr: PyStrRef,
|
||||
attr: PyObjectRef,
|
||||
default: OptionalArg<PyObjectRef>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult {
|
||||
let attr = attr.try_to_ref::<PyStr>(vm).map_err(|_e| {
|
||||
vm.new_type_error(format!(
|
||||
"attribute name must be string, not '{}'",
|
||||
attr.class().name()
|
||||
))
|
||||
})?;
|
||||
|
||||
if let OptionalArg::Present(default) = default {
|
||||
Ok(vm.get_attribute_opt(obj, &attr)?.unwrap_or(default))
|
||||
Ok(vm.get_attribute_opt(obj, attr)?.unwrap_or(default))
|
||||
} else {
|
||||
obj.get_attr(&attr, vm)
|
||||
obj.get_attr(attr, vm)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -338,8 +351,14 @@ mod builtins {
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn hasattr(obj: PyObjectRef, attr: PyStrRef, vm: &VirtualMachine) -> PyResult<bool> {
|
||||
Ok(vm.get_attribute_opt(obj, &attr)?.is_some())
|
||||
fn hasattr(obj: PyObjectRef, attr: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
|
||||
let attr = attr.try_to_ref::<PyStr>(vm).map_err(|_e| {
|
||||
vm.new_type_error(format!(
|
||||
"attribute name must be string, not '{}'",
|
||||
attr.class().name()
|
||||
))
|
||||
})?;
|
||||
Ok(vm.get_attribute_opt(obj, attr)?.is_some())
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
@@ -742,11 +761,17 @@ mod builtins {
|
||||
#[pyfunction]
|
||||
fn setattr(
|
||||
obj: PyObjectRef,
|
||||
attr: PyStrRef,
|
||||
attr: PyObjectRef,
|
||||
value: PyObjectRef,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<()> {
|
||||
obj.set_attr(&attr, value, vm)?;
|
||||
let attr = attr.try_to_ref::<PyStr>(vm).map_err(|_e| {
|
||||
vm.new_type_error(format!(
|
||||
"attribute name must be string, not '{}'",
|
||||
attr.class().name()
|
||||
))
|
||||
})?;
|
||||
obj.set_attr(attr, value, vm)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user