-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Store SyntaxError members in struct fields instead of the instance dict #8348
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -341,12 +341,11 @@ impl VirtualMachine { | |
| /// [`vm.invoke_exception()`][Self::invoke_exception] or | ||
| /// [`exceptions::ExceptionCtor`][crate::exceptions::ExceptionCtor] instead. | ||
| pub fn new_exception(&self, exc_type: PyTypeRef, args: Vec<PyObjectRef>) -> PyBaseExceptionRef { | ||
| debug_assert_eq!( | ||
| exc_type.slots.basicsize, | ||
| core::mem::size_of::<PyBaseException>(), | ||
| "vm.new_exception() is only for exception types without additional payload. The given type '{}' is not allowed. Use vm.new_os_subtype_error() for OSError subtypes.", | ||
| exc_type.name() | ||
| ); | ||
| if exc_type.slots.basicsize != core::mem::size_of::<PyBaseException>() { | ||
| // If constructing the exception raises (e.g. __init__ rejects the | ||
| // args), surface that exception instead of panicking. | ||
| return self.invoke_exception(&exc_type, args).unwrap_or_else(|e| e); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unwrap_or_else is wrong folding here. in my opinion, new_exception must not call invoke_exception for a few reason Note: it doesn't mean i am justifying the name new_exception and invoke_exception. if anyone can suggest good names that shows this difference, welcome.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed on keeping I prototyped a dedicated payload path that skips pub fn new_payload_exception<T>(&self, cls: PyTypeRef, args: FuncArgs) -> PyResult<PyRef<T>>
where
T: Constructor<Args = FuncArgs> + Initializer,
{
let payload = T::py_new(&cls, args.clone(), self)?;
let exc = payload
.into_ref_with_type_lazy_dict(self, cls)
.expect("new_payload_exception: cls is not a matching subtype of T");
T::slot_init(exc.as_object().to_owned(), args, self)?;
Ok(exc)
}I verified it by routing both If you're on board with this direction, I'd open it as a separate draft to work on further.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds good, let's try it
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| PyBaseException::new(args, self) | ||
| .into_ref_with_type_lazy_dict(self, exc_type) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: setting
msghere is redundant —slot_initsets it again — and diverges slightly from CPython, where__new__leavesmsgunset (SyntaxError.__new__(SyntaxError, "x").msgisNonethere, since only__init__assigns it).msg: None.into()would match.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right about the CPython
__new__behavior, butmsg: None.into()brings back a CI failure from earlier in this PR.Since
TabError(a second-level subclass) never reachesPySyntaxError::slot_init,TabError("error", …)renders asTabError: <no detail available>in tracebacks, which breaks test_doctest'stest_syntax_error_with_note.Setting it
in py_newis a workaround until slot inheritance works properly for second-level subclasses.