Skip to content

Commit 9792170

Browse files
committed
Add os.uname
1 parent 438dcec commit 9792170

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ paste = "0.1"
6969
base64 = "0.11"
7070
is-macro = "0.1"
7171
result-like = "^0.2.1"
72+
uname = "0.1.1"
7273

7374
flame = { version = "0.2", optional = true }
7475
flamer = { version = "0.3", optional = true }

vm/src/stdlib/os.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use nix::pty::openpty;
2222
use nix::unistd::{self, Gid, Pid, Uid};
2323
#[cfg(unix)]
2424
use std::os::unix::io::RawFd;
25+
use uname;
2526

2627
use super::errno::errors;
2728
use crate::exceptions::PyBaseExceptionRef;
@@ -1154,6 +1155,36 @@ fn os_urandom(size: usize, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
11541155
}
11551156
}
11561157

1158+
#[pystruct_sequence(name = "os.uname_result")]
1159+
#[derive(Debug)]
1160+
struct UnameResult {
1161+
sysname: String,
1162+
nodename: String,
1163+
release: String,
1164+
version: String,
1165+
machine: String,
1166+
}
1167+
1168+
impl UnameResult {
1169+
fn into_obj(self, vm: &VirtualMachine) -> PyObjectRef {
1170+
self.into_struct_sequence(vm, vm.class("_os", "uname_result"))
1171+
.unwrap()
1172+
.into_object()
1173+
}
1174+
}
1175+
1176+
fn os_uname(vm: &VirtualMachine) -> PyResult {
1177+
let info = uname::uname().map_err(|err| convert_io_error(vm, err))?;
1178+
Ok(UnameResult {
1179+
sysname: info.sysname,
1180+
nodename: info.nodename,
1181+
release: info.release,
1182+
version: info.version,
1183+
machine: info.machine,
1184+
}
1185+
.into_obj(vm))
1186+
}
1187+
11571188
// this is basically what CPython has for Py_off_t; windows uses long long
11581189
// for offsets, other platforms just use off_t
11591190
#[cfg(not(windows))]
@@ -1243,6 +1274,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
12431274
});
12441275

12451276
let stat_result = StatResult::make_class(ctx);
1277+
let uname_result = UnameResult::make_class(ctx);
12461278

12471279
struct SupportFunc<'a> {
12481280
name: &'a str,
@@ -1331,6 +1363,8 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
13311363
"urandom" => ctx.new_function(os_urandom),
13321364
"isatty" => ctx.new_function(os_isatty),
13331365
"lseek" => ctx.new_function(os_lseek),
1366+
"uname" => ctx.new_function(os_uname),
1367+
"uname_result" => uname_result,
13341368

13351369
"O_RDONLY" => ctx.new_int(libc::O_RDONLY),
13361370
"O_WRONLY" => ctx.new_int(libc::O_WRONLY),

0 commit comments

Comments
 (0)