Integrate OSError creations into OSErrorBuilder by youknowone · Pull Request #6443 · RustPython/RustPython
crates/vm/src/stdlib/io.rs (5)
21-68: Well-structured platform-specific error handling.The implementation correctly handles platform-specific strerror logic with proper null pointer checks for the unsafe blocks. The
#[allow(unused_mut)]annotation on line 57 is necessary sincebuilderis only mutated on Windows.
70-74: Clean delegation to builder pattern.The
ToPyExceptionimplementation correctly delegates to the newToOSErrorBuildertrait, maintaining a consistent error construction flow.
4332-4335: Imports updated appropriately for the new builder pattern.
4531-4531: Consistent error construction with filename context.The
OSErrorBuilder::with_filenameusage correctly propagates the filename to the OSError, improving error messages for users.
4546-4560: Unified error handling across platform-specific branches.The error construction is consistent across Windows and Unix code paths, using
OSErrorBuilder::with_filenameto include the filename in all error cases. This provides better error messages compared to the previous approach.
crates/vm/src/exceptions.rs (1)
1196-1197: Re-exporting OSErrorBuilder/ToOSErrorBuilder fromexceptionslooks goodThis makes the builder and trait conveniently available to the rest of the crate and matches how they’re consumed in other files.
crates/vm/src/vm/vm_new.rs (1)
2-15: UsingOSErrorBuilderinnew_os_subtype_errorkeeps behavior while centralizing constructionThe new
OSErrorBuilder::with_subtype(...).build(self)path preserves the previous semantics (including the basicsize debug‑assert onexc_type) while routing OS error creation through the shared builder. This looks correct for botherrno: None(OSError(msg)) andSome(errno)flows.Also applies to: 110-124
crates/vm/src/stdlib/posix.rs (1)
29-35: Consistent use ofOSErrorBuilder::with_filenamefor POSIX filesystem errorsAll these call sites now route
std::io::Error(or errors converted into it) throughOSErrorBuilder::with_filename, which:
- Preserves the original IO error’s errno and message via
ToOSErrorBuilder.- Attaches the relevant
OsPath/OsPathOrFdasfilename(andfilename2where applicable).- Returns a
PyBaseExceptionRefsuitable forPyResulterror types.The patterns in
access,_chmod,lchmod,rmdir,listdir,scandir,pathconf,truncate, andutime_implare all coherent and should yield better CPython‑styleOSErrorinstances with proper filename context.Also applies to: 415-417, 535-538, 1035-1036, 1097-1098, 360-363, 375-386, 861-871, 2130-2135, 1564-1571, 1339-1344, 1386-1403
crates/vm/src/stdlib/os.rs (2)
153-168: UnifiedOSErrorBuilderusage acrossosfilesystem operationsThe new imports and
map_errsites in this file consistently use:
OSErrorBuilder::with_filename(&err, path_or_fd, vm)for IO errors that originate from a single pathname/FD.vm.new_errno_error(...)(now backed byOSErrorBuilder) when constructing errno‑driven errors.OSErrorBuilder::with_filename(&io::Error::from(Errno::last()), path, vm)for errno‑basedpathconffailures.This centralizes OS error creation and ensures that
errno,strerror, andfilenameare wired the same way acrossopen,remove/unlink,mkdir,rmdir,listdir(path branch),readlink,stat/fstat,chdir,truncate,utime, andpathconf. The patterns are type‑correct and align with howOSErrorBuilder/ToOSErrorBuilderare intended to be used.Also applies to: 235-269, 290-323, 324-352, 359-363, 373-387, 546-553, 861-871, 1088-1092, 1118-1121, 1564-1571, 2130-2135
1128-1137:rename/replaceandlinknow produce fully populatedOSErrorwith both filenamesThe new error handling for
rename/replaceandlink:fs::rename(&src.path, &dst.path).map_err(|err| { let builder = err.to_os_error_builder(vm); let builder = builder.filename(src.filename(vm)); let builder = builder.filename2(dst.filename(vm)); builder.build(vm).upcast() })and similarly for
link, ensures that:
- The underlying IO error is converted via
to_os_error_builder, capturing errno and a platform‑appropriate message.filenameandfilename2are set tosrcanddst(respectingOutputModefor str/bytes).- The final Python exception is of the appropriate OSError subtype as determined by
errno_to_exc_type.This is a nice improvement over ad‑hoc construction and should match CPython’s behavior more closely.
Also applies to: 1221-1229
crates/vm/src/ospath.rs (1)
145-158:OSErrorBuilder::with_filenamehelper is well-factored and matches call sitesThe inherent helper:
pub(crate) fn with_filename<'a>( error: &std::io::Error, filename: impl Into<OsPathOrFd<'a>>, vm: &VirtualMachine, ) -> crate::builtins::PyBaseExceptionRef { use crate::exceptions::ToOSErrorBuilder; let builder = error.to_os_error_builder(vm); let builder = builder.filename(filename.into().filename(vm)); builder.build(vm).upcast() }nicely encapsulates the common “IO error + path →
OSErrorwith filename set” pattern used acrossosandposix. The API surface (taking anyInto<OsPathOrFd<'a>>) keeps call sites clean and avoids duplicating theto_os_error_builder().filename(...).build(vm)sequence everywhere.The TODO about returning
PyRef<PyOSError>instead ofPyBaseExceptionRefcan be addressed later without affecting current users.