Skip to content

Commit f68a808

Browse files
committed
relocate bytes_as_osstr to common
1 parent 7b0ab17 commit f68a808

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

common/src/os.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// TODO: we can move more os-specific bindings/interfaces from stdlib::{os, posix, nt} to here
22

3-
use std::io;
3+
use std::{io, str::Utf8Error};
44

55
#[cfg(windows)]
66
pub fn errno() -> io::Error {
@@ -21,3 +21,14 @@ pub fn errno() -> io::Error {
2121
pub fn errno() -> io::Error {
2222
io::Error::last_os_error()
2323
}
24+
25+
#[cfg(unix)]
26+
pub fn bytes_as_osstr(b: &[u8]) -> Result<&std::ffi::OsStr, Utf8Error> {
27+
use std::os::unix::ffi::OsStrExt;
28+
Ok(std::ffi::OsStr::from_bytes(b))
29+
}
30+
31+
#[cfg(not(unix))]
32+
pub fn bytes_as_osstr(b: &[u8]) -> Result<&std::ffi::OsStr, Utf8Error> {
33+
Ok(std::str::from_utf8(b)?.as_ref())
34+
}

vm/src/stdlib/os.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,8 @@ pub(super) struct FollowSymlinks(
378378
#[pyarg(named, name = "follow_symlinks", default = "true")] pub bool,
379379
);
380380

381-
#[cfg(unix)]
382-
use platform::bytes_as_osstr;
383-
384-
#[cfg(not(unix))]
385381
fn bytes_as_osstr<'a>(b: &'a [u8], vm: &VirtualMachine) -> PyResult<&'a ffi::OsStr> {
386-
std::str::from_utf8(b)
387-
.map(|s| s.as_ref())
382+
rustpython_common::os::bytes_as_osstr(b)
388383
.map_err(|_| vm.new_unicode_decode_error("can't decode path for utf-8".to_owned()))
389384
}
390385

vm/src/stdlib/posix.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{PyObjectRef, PyResult, VirtualMachine};
1+
use crate::{PyObjectRef, VirtualMachine};
22
use std::os::unix::io::RawFd;
33

44
pub fn raw_set_inheritable(fd: RawFd, inheritable: bool) -> nix::Result<()> {
@@ -12,14 +12,6 @@ pub fn raw_set_inheritable(fd: RawFd, inheritable: bool) -> nix::Result<()> {
1212
Ok(())
1313
}
1414

15-
pub(super) fn bytes_as_osstr<'a>(
16-
b: &'a [u8],
17-
_vm: &VirtualMachine,
18-
) -> PyResult<&'a std::ffi::OsStr> {
19-
use std::os::unix::ffi::OsStrExt;
20-
Ok(std::ffi::OsStr::from_bytes(b))
21-
}
22-
2315
pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef {
2416
let module = module::make_module(vm);
2517
super::os::extend_module(vm, &module);

0 commit comments

Comments
 (0)