-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Turn SystemError.from_errno
into a macro
#15874
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: master
Are you sure you want to change the base?
Turn SystemError.from_errno
into a macro
#15874
Conversation
The actual method that resolves #15496 is |
This ensures we capture `Errno.value` right away and it cannot be influenced by other code producing the arguments.
Co-authored-by: Quinton Miller <[email protected]>
e7a4962
to
bfe0758
Compare
bfe0758
to
e48f1d1
Compare
src/system_error.cr
Outdated
# The system message corresponding to the OS error value amends the *message*. | ||
# Additional keyword arguments are forwarded to the exception initializer `.new_from_os_error`. | ||
macro from_errno(message, **opts) | ||
# This is a macro in order to retrieve `Errno.value` first before evaluating `message` and `opts`. |
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.
Polish: maybe move this comment outside of the macro, and be more general:
The following are macros so we can get the errno or winerror before
evaluating message and opts that may change the errno or winerror value.
SystemError.from_errno
callsErrno.value
to retrieve the system error value from the last lib call.But this happens in the body of the method which only executes after its arguments are evaluated (for example building the error message). This can lead to a change in
Errno.value
which.from_errno
would then mistake for the original error.In order to fix this, we must query
Errno.value
immediately after the lib call.Changing
.from_errno
into a macro achieves that because it expands into a call toErrno.value
before constructing the actual error object and its arguments.We need to change the deprecated
from_errno
method which receives anErrno
value as well because the macro would shadow the method.One downside of the macro approach is that it won't work with file-private error types (#15496 (comment)). But this is probably not a very relevant use case.
If this is accepted, I'll do the same change to
SystemError.from_winerror
andSystemError.from_wsa_error
.Resolves #15496