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