Skip to content

Commit bff45e1

Browse files
committed
fix
1 parent 194e2a6 commit bff45e1

1 file changed

Lines changed: 63 additions & 7 deletions

File tree

  • crates/vm/src/stdlib

‎crates/vm/src/stdlib/os.rs‎

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,38 @@ pub(super) mod _os {
436436
Ok(())
437437
}
438438

439+
#[cfg(windows)]
440+
#[pyfunction]
441+
fn putenv(key: PyStrRef, value: PyStrRef, vm: &VirtualMachine) -> PyResult<()> {
442+
let key_str = key.as_str();
443+
let value_str = value.as_str();
444+
// Search from index 1 because on Windows starting '=' is allowed for
445+
// defining hidden environment variables.
446+
if key_str.is_empty()
447+
|| key_str.get(1..).is_some_and(|s| s.contains('='))
448+
|| key_str.contains('\0')
449+
|| value_str.contains('\0')
450+
{
451+
return Err(vm.new_value_error("illegal environment variable name"));
452+
}
453+
let env_str = format!("{}={}", key_str, value_str);
454+
let wide = env_str.to_wide_string();
455+
if wide.len() > _MAX_ENV + 1 {
456+
return Err(vm.new_value_error(format!(
457+
"the environment variable is longer than {} characters",
458+
_MAX_ENV
459+
)));
460+
}
461+
462+
// Use _wputenv like CPython (not SetEnvironmentVariableW) to update CRT environ
463+
let result = unsafe { suppress_iph!(libc::wputenv(wide.as_ptr())) };
464+
if result != 0 {
465+
return Err(vm.new_last_errno_error());
466+
}
467+
Ok(())
468+
}
469+
470+
#[cfg(not(windows))]
439471
#[pyfunction]
440472
fn putenv(
441473
key: Either<PyStrRef, PyBytesRef>,
@@ -450,15 +482,44 @@ pub(super) mod _os {
450482
if key.is_empty() || key.contains(&b'=') {
451483
return Err(vm.new_value_error("illegal environment variable name"));
452484
}
453-
#[cfg(windows)]
454-
check_env_var_len(key.len() + value.len() + 2, vm)?;
455485
let key = super::bytes_as_os_str(key, vm)?;
456486
let value = super::bytes_as_os_str(value, vm)?;
457487
// SAFETY: requirements forwarded from the caller
458488
unsafe { env::set_var(key, value) };
459489
Ok(())
460490
}
461491

492+
#[cfg(windows)]
493+
#[pyfunction]
494+
fn unsetenv(key: PyStrRef, vm: &VirtualMachine) -> PyResult<()> {
495+
let key_str = key.as_str();
496+
// Search from index 1 because on Windows starting '=' is allowed for
497+
// defining hidden environment variables.
498+
if key_str.is_empty()
499+
|| key_str.get(1..).is_some_and(|s| s.contains('='))
500+
|| key_str.contains('\0')
501+
{
502+
return Err(vm.new_value_error("illegal environment variable name"));
503+
}
504+
// "key=" to unset (empty value removes the variable)
505+
let env_str = format!("{}=", key_str);
506+
let wide = env_str.to_wide_string();
507+
if wide.len() > _MAX_ENV + 1 {
508+
return Err(vm.new_value_error(format!(
509+
"the environment variable is longer than {} characters",
510+
_MAX_ENV
511+
)));
512+
}
513+
514+
// Use _wputenv like CPython (not SetEnvironmentVariableW) to update CRT environ
515+
let result = unsafe { suppress_iph!(libc::wputenv(wide.as_ptr())) };
516+
if result != 0 {
517+
return Err(vm.new_last_errno_error());
518+
}
519+
Ok(())
520+
}
521+
522+
#[cfg(not(windows))]
462523
#[pyfunction]
463524
fn unsetenv(key: Either<PyStrRef, PyBytesRef>, vm: &VirtualMachine) -> PyResult<()> {
464525
let key = env_bytes_as_bytes(&key);
@@ -474,11 +535,6 @@ pub(super) mod _os {
474535
),
475536
));
476537
}
477-
#[cfg(windows)]
478-
{
479-
// For unsetenv, size is key + '=' (no value, just clearing)
480-
check_env_var_len(key.len() + 1, vm)?;
481-
}
482538
let key = super::bytes_as_os_str(key, vm)?;
483539
// SAFETY: requirements forwarded from the caller
484540
unsafe { env::remove_var(key) };

0 commit comments

Comments
 (0)