Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/5444.fixed.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to assess if this is better to class as a "fix" or a breaking change. At the moment I guess the result is that we always include a trailing NUL on the extracted string, only on Windows?

So this is a good consistency fix, to match the other platforms, but it also might cause issues in the worst case? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current case is that it can only succeed when the string is valid utf8. Because it will take the shortcut using utf8 conversion. When the string is WTF16 it will continue and use PyUnicode_AsWideChar. But this currently never succeeds because the assertion assert_eq!(bytes_read, size); always fails (bytes_read does not contain the null byte but size does). So this is not a breaking change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess no one is hitting this assert because valid utf8 is the common case.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix `OsStr` conversion for non-utf8 strings on windows
37 changes: 35 additions & 2 deletions src/conversions/std/osstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ impl FromPyObject<'_, '_> for OsString {
unsafe { ffi::PyUnicode_AsWideChar(pystring.as_ptr(), std::ptr::null_mut(), 0) };
crate::err::error_on_minusone(ob.py(), size)?;

debug_assert!(
size > 0,
"PyUnicode_AsWideChar should return at least 1 for null terminator"
);
let size = size - 1; // exclude null terminator

let mut buffer = vec![0; size as usize];
let bytes_read =
unsafe { ffi::PyUnicode_AsWideChar(pystring.as_ptr(), buffer.as_mut_ptr(), size) };
Expand Down Expand Up @@ -169,7 +175,7 @@ impl<'py> IntoPyObject<'py> for &OsString {

#[cfg(test)]
mod tests {
use crate::types::{PyString, PyStringMethods};
use crate::types::{PyAnyMethods, PyString, PyStringMethods};
use crate::{BoundObject, IntoPyObject, Python};
use std::fmt::Debug;
use std::{
Expand All @@ -181,7 +187,6 @@ mod tests {
#[cfg(not(windows))]
fn test_non_utf8_conversion() {
Python::attach(|py| {
use crate::types::PyAnyMethods;
#[cfg(not(target_os = "wasi"))]
use std::os::unix::ffi::OsStrExt;
#[cfg(target_os = "wasi")]
Expand Down Expand Up @@ -219,4 +224,32 @@ mod tests {
test_roundtrip::<OsString>(py, os_str.to_os_string());
});
}

#[test]
#[cfg(windows)]
fn test_windows_non_utf8_osstring_roundtrip() {
use std::os::windows::ffi::{OsStrExt, OsStringExt};

Python::attach(|py| {
// Example: Unpaired surrogate (0xD800) is not valid UTF-8, but valid in Windows OsString
let wide: &[u16] = &['A' as u16, 0xD800, 'B' as u16]; // 'A', unpaired surrogate, 'B'
let os_str = OsString::from_wide(wide);

assert_eq!(os_str.to_string_lossy(), "A�B");

// This cannot be represented as UTF-8, so .to_str() would return None
assert!(os_str.to_str().is_none());

// Convert to Python and back
let py_str = os_str.as_os_str().into_pyobject(py).unwrap();
let os_str_2 = py_str.extract::<OsString>().unwrap();

// The roundtrip should preserve the original wide data
assert_eq!(os_str, os_str_2);

// Show that encode_wide is necessary: direct UTF-8 conversion would lose information
let encoded: Vec<u16> = os_str.encode_wide().collect();
assert_eq!(encoded, wide);
});
}
}
Loading