Skip to content

Fix type() error message and BaseException.__str__ #1730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2020
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
2 changes: 0 additions & 2 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,8 +1168,6 @@ def __prepare__(mcls, name, bases):
with self.assertRaises(TypeError):
X = types.new_class("X", (int(), C))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_one_argument_type(self):
expected_message = 'type.__new__() takes exactly 3 arguments (1 given)'

Expand Down
2 changes: 2 additions & 0 deletions tests/snippets/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def round_trip_repr(e):
assert str(exc) == "'message'"
assert round_trip_repr(exc)

assert LookupError.__str__(exc) == "message"

exc = KeyError('message', 'another message')
assert str(exc) == "('message', 'another message')"
assert round_trip_repr(exc)
Expand Down
18 changes: 17 additions & 1 deletion vm/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl PyBaseException {

#[pymethod(name = "__str__")]
fn str(&self, vm: &VirtualMachine) -> PyStringRef {
let str_args = exception_args_as_string(vm, self.args(), false);
let str_args = exception_args_as_string(vm, self.args(), true);
match str_args.into_iter().exactly_one() {
Err(i) if i.len() == 0 => PyString::from("").into_ref(vm),
Ok(s) => s,
Expand Down Expand Up @@ -616,6 +616,18 @@ fn make_arg_getter(idx: usize) -> impl Fn(PyBaseExceptionRef, &VirtualMachine) -
}
}

fn key_error_str(exc: PyBaseExceptionRef, vm: &VirtualMachine) -> PyStringRef {
let args = exc.args();
if args.as_slice().len() == 1 {
exception_args_as_string(vm, args, false)
.into_iter()
.exactly_one()
.unwrap()
} else {
exc.str(vm)
}
}

pub fn init(ctx: &PyContext) {
let excs = &ctx.exceptions;

Expand All @@ -638,6 +650,10 @@ pub fn init(ctx: &PyContext) {
"value" => ctx.new_property(make_arg_getter(0)),
});

extend_class!(ctx, &excs.key_error, {
"__str__" => ctx.new_method(key_error_str),
});

extend_class!(ctx, &excs.unicode_decode_error, {
"encoding" => ctx.new_property(make_arg_getter(0)),
"object" => ctx.new_property(make_arg_getter(1)),
Expand Down
21 changes: 14 additions & 7 deletions vm/src/obj/objtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,20 @@ impl PyClassRef {
fn tp_new(metatype: PyClassRef, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
vm_trace!("type.__new__ {:?}", args);

if metatype.is(&vm.ctx.types.type_type) {
if args.args.len() == 1 && args.kwargs.is_empty() {
return Ok(args.args[0].class().into_object());
}
if args.args.len() != 3 {
return Err(vm.new_type_error("type() takes 1 or 3 arguments".to_string()));
}
let is_type_type = metatype.is(&vm.ctx.types.type_type);
if is_type_type && args.args.len() == 1 && args.kwargs.is_empty() {
return Ok(args.args[0].class().into_object());
}

if args.args.len() != 3 {
return Err(vm.new_type_error(if is_type_type {
"type() takes 1 or 3 arguments".to_string()
} else {
format!(
"type.__new__() takes exactly 3 arguments ({} given)",
args.args.len()
)
}));
}

let (name, bases, dict): (PyStringRef, PyIterable<PyClassRef>, PyDictRef) =
Expand Down