MCPcopy Index your code
hub / github.com/RustPython/RustPython / from_stat

Method from_stat

crates/vm/src/stdlib/os.rs:1291–1362  ·  view source on GitHub ↗
(stat: &StatStruct, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

1289
1290 impl StatResultData {
1291 fn from_stat(stat: &StatStruct, vm: &VirtualMachine) -> Self {
1292 let (atime, mtime, ctime);
1293 #[cfg(any(unix, windows))]
1294 #[cfg(not(any(target_os = "netbsd", target_os = "wasi")))]
1295 {
1296 atime = (stat.st_atime, stat.st_atime_nsec);
1297 mtime = (stat.st_mtime, stat.st_mtime_nsec);
1298 ctime = (stat.st_ctime, stat.st_ctime_nsec);
1299 }
1300 #[cfg(target_os = "netbsd")]
1301 {
1302 atime = (stat.st_atime, stat.st_atimensec);
1303 mtime = (stat.st_mtime, stat.st_mtimensec);
1304 ctime = (stat.st_ctime, stat.st_ctimensec);
1305 }
1306 #[cfg(target_os = "wasi")]
1307 {
1308 atime = (stat.st_atim.tv_sec, stat.st_atim.tv_nsec);
1309 mtime = (stat.st_mtim.tv_sec, stat.st_mtim.tv_nsec);
1310 ctime = (stat.st_ctim.tv_sec, stat.st_ctim.tv_nsec);
1311 }
1312
1313 const NANOS_PER_SEC: u32 = 1_000_000_000;
1314 let to_f64 = |(s, ns)| (s as f64) + (ns as f64) / (NANOS_PER_SEC as f64);
1315 let to_ns = |(s, ns)| s as i128 * NANOS_PER_SEC as i128 + ns as i128;
1316
1317 #[cfg(windows)]
1318 let st_reparse_tag = stat.st_reparse_tag;
1319 #[cfg(windows)]
1320 let st_file_attributes = stat.st_file_attributes;
1321
1322 // On Windows, combine st_ino and st_ino_high into a 128-bit value
1323 // like _pystat_l128_from_l64_l64
1324 #[cfg(windows)]
1325 let st_ino: u128 = stat.st_ino as u128 | ((stat.st_ino_high as u128) << 64);
1326 #[cfg(not(windows))]
1327 let st_ino = stat.st_ino;
1328
1329 #[cfg(not(windows))]
1330 #[allow(clippy::useless_conversion, reason = "needed for 32-bit platforms")]
1331 let st_blksize = i64::from(stat.st_blksize);
1332 #[cfg(not(windows))]
1333 #[allow(clippy::useless_conversion, reason = "needed for 32-bit platforms")]
1334 let st_blocks = i64::from(stat.st_blocks);
1335
1336 Self {
1337 st_mode: vm.ctx.new_pyref(stat.st_mode),
1338 st_ino: vm.ctx.new_pyref(st_ino),
1339 st_dev: vm.ctx.new_pyref(stat.st_dev),
1340 st_nlink: vm.ctx.new_pyref(stat.st_nlink),
1341 st_uid: vm.ctx.new_pyref(stat.st_uid),
1342 st_gid: vm.ctx.new_pyref(stat.st_gid),
1343 st_size: vm.ctx.new_pyref(stat.st_size),
1344 st_atime_int: atime.0,
1345 st_mtime_int: mtime.0,
1346 st_ctime_int: ctime.0,
1347 st_atime: to_f64(atime),
1348 st_mtime: to_f64(mtime),

Callers

nothing calls this directly

Calls 1

new_pyrefMethod · 0.80

Tested by

no test coverage detected