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

Function unsetenv

crates/vm/src/stdlib/os.rs:553–574  ·  view source on GitHub ↗
(key: PyStrRef, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

551 #[cfg(windows)]
552 #[pyfunction]
553 fn unsetenv(key: PyStrRef, vm: &VirtualMachine) -> PyResult<()> {
554 let key_str = key.expect_str();
555 // Search from index 1 because on Windows starting '=' is allowed for
556 // defining hidden environment variables.
557 if key_str.is_empty()
558 || key_str.get(1..).is_some_and(|s| s.contains('='))
559 || key_str.contains('\0')
560 {
561 return Err(vm.new_value_error("illegal environment variable name"));
562 }
563 // "key=" to unset (empty value removes the variable)
564 let env_str = format!("{}=", key_str);
565 let wide = env_str.to_wide_with_nul();
566 check_env_var_len(wide.len(), vm)?;
567
568 // Use _wputenv like CPython (not SetEnvironmentVariableW) to update CRT environ
569 let result = unsafe { suppress_iph!(_wputenv(wide.as_ptr())) };
570 if result != 0 {
571 return Err(vm.new_last_errno_error());
572 }
573 Ok(())
574 }
575
576 #[cfg(not(windows))]
577 #[pyfunction]

Callers 1

__delitem__Method · 0.85

Calls 13

check_env_var_lenFunction · 0.85
env_bytes_as_bytesFunction · 0.85
expect_strMethod · 0.80
to_wide_with_nulMethod · 0.80
new_last_errno_errorMethod · 0.80
new_errno_errorMethod · 0.80
upcastMethod · 0.80
bytes_as_os_strFunction · 0.70
ErrClass · 0.50
is_emptyMethod · 0.45
getMethod · 0.45
containsMethod · 0.45

Tested by

no test coverage detected