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

Function putenv

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

Source from the content-addressed store, hash-verified

503 #[cfg(windows)]
504 #[pyfunction]
505 fn putenv(key: PyStrRef, value: PyStrRef, vm: &VirtualMachine) -> PyResult<()> {
506 let key_str = key.expect_str();
507 let value_str = value.expect_str();
508 // Search from index 1 because on Windows starting '=' is allowed for
509 // defining hidden environment variables.
510 if key_str.is_empty()
511 || key_str.get(1..).is_some_and(|s| s.contains('='))
512 || key_str.contains('\0')
513 || value_str.contains('\0')
514 {
515 return Err(vm.new_value_error("illegal environment variable name"));
516 }
517 let env_str = format!("{}={}", key_str, value_str);
518 let wide = env_str.to_wide_with_nul();
519 check_env_var_len(wide.len(), vm)?;
520
521 // Use _wputenv like CPython (not SetEnvironmentVariableW) to update CRT environ
522 let result = unsafe { suppress_iph!(_wputenv(wide.as_ptr())) };
523 if result != 0 {
524 return Err(vm.new_last_errno_error());
525 }
526 Ok(())
527 }
528
529 #[cfg(not(windows))]
530 #[pyfunction]

Callers 1

__setitem__Method · 0.85

Calls 11

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
bytes_as_os_strFunction · 0.70
ErrClass · 0.50
is_emptyMethod · 0.45
getMethod · 0.45
containsMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected