(key: PyStrRef, value: PyStrRef, vm: &VirtualMachine)
| 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] |
no test coverage detected