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

Function utime

crates/vm/src/stdlib/os.rs:1730–1780  ·  view source on GitHub ↗
(args: UtimeArgs<'_>, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

1728
1729 #[pyfunction]
1730 fn utime(args: UtimeArgs<'_>, vm: &VirtualMachine) -> PyResult<()> {
1731 let parse_tup = |tup: &Py<PyTuple>| -> Option<(PyObjectRef, PyObjectRef)> {
1732 if tup.len() != 2 {
1733 None
1734 } else {
1735 Some((tup[0].clone(), tup[1].clone()))
1736 }
1737 };
1738 let (acc, modif) = match (args.times, args.ns) {
1739 (Some(t), None) => {
1740 let (a, m) = parse_tup(&t).ok_or_else(|| {
1741 vm.new_type_error("utime: 'times' must be either a tuple of two ints or None")
1742 })?;
1743 (a.try_into_value(vm)?, m.try_into_value(vm)?)
1744 }
1745 (None, Some(ns)) => {
1746 let (a, m) = parse_tup(&ns)
1747 .ok_or_else(|| vm.new_type_error("utime: 'ns' must be a tuple of two ints"))?;
1748 let ns_in_sec: PyObjectRef = vm.ctx.new_int(1_000_000_000).into();
1749 let ns_to_dur = |obj: PyObjectRef| {
1750 let divmod = vm._divmod(&obj, &ns_in_sec)?;
1751 let (div, rem) = divmod
1752 .downcast_ref::<PyTuple>()
1753 .and_then(parse_tup)
1754 .ok_or_else(|| {
1755 vm.new_type_error(format!(
1756 "{}.__divmod__() must return a 2-tuple, not {}",
1757 obj.class().name(),
1758 divmod.class().name()
1759 ))
1760 })?;
1761 let secs = div.try_index(vm)?.try_to_primitive(vm)?;
1762 let ns = rem.try_index(vm)?.try_to_primitive(vm)?;
1763 Ok(Duration::new(secs, ns))
1764 };
1765 // TODO: do validation to make sure this doesn't.. underflow?
1766 (ns_to_dur(a)?, ns_to_dur(m)?)
1767 }
1768 (None, None) => {
1769 let now = SystemTime::now();
1770 let now = now.duration_since(SystemTime::UNIX_EPOCH).unwrap();
1771 (now, now)
1772 }
1773 (Some(_), Some(_)) => {
1774 return Err(vm.new_value_error(
1775 "utime: you may specify either 'times' or 'ns' but not both",
1776 ));
1777 }
1778 };
1779 utime_impl(args.path, acc, modif, args.dir_fd, args.follow_symlinks, vm)
1780 }
1781
1782 fn utime_impl(
1783 path: OsPath,

Callers

nothing calls this directly

Calls 13

newFunction · 0.85
utime_implFunction · 0.85
ok_or_elseMethod · 0.80
try_into_valueMethod · 0.80
_divmodMethod · 0.80
try_to_primitiveMethod · 0.80
try_indexMethod · 0.80
SomeClass · 0.50
ErrClass · 0.50
lenMethod · 0.45
cloneMethod · 0.45
new_intMethod · 0.45

Tested by

no test coverage detected