Fix windows socket (#6408)

* fix _path_splitroot

* Fix windows socket
This commit is contained in:
Jeong, YunWon authored and GitHub committed 2025-12-12 10:37:40 +09:00
1 parent 2ff1fba6ed
commit 772b92edde
5 files changed
+24 -27

No files matched your search

-2
View File
@@ -131,8 +131,6 @@ class BaseSelectorTestCase:
s.unregister(r)
s.unregister(w)
# TODO: RUSTPYTHON
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
def test_unregister_after_socket_close(self):
s = self.SELECTOR()
self.addCleanup(s.close)
-5
View File
@@ -178,13 +178,11 @@ class SocketServerTest(unittest.TestCase):
buf += data
self.assertEqual(buf, TEST_STR)
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; AssertionError: -1 != 18446744073709551615")
def test_TCPServer(self):
self.run_server(socketserver.TCPServer,
socketserver.StreamRequestHandler,
self.stream_examine)
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; AssertionError: -1 != 18446744073709551615")
def test_ThreadingTCPServer(self):
self.run_server(socketserver.ThreadingTCPServer,
socketserver.StreamRequestHandler,
@@ -217,13 +215,11 @@ class SocketServerTest(unittest.TestCase):
socketserver.StreamRequestHandler,
self.stream_examine)
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; AssertionError: -1 != 18446744073709551615")
def test_UDPServer(self):
self.run_server(socketserver.UDPServer,
socketserver.DatagramRequestHandler,
self.dgram_examine)
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; AssertionError: -1 != 18446744073709551615")
def test_ThreadingUDPServer(self):
self.run_server(socketserver.ThreadingUDPServer,
socketserver.DatagramRequestHandler,
@@ -298,7 +294,6 @@ class SocketServerTest(unittest.TestCase):
socketserver.TCPServer((HOST, -1),
socketserver.StreamRequestHandler)
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; AssertionError: -1 != 18446744073709551615")
def test_context_manager(self):
with socketserver.TCPServer((HOST, 0),
socketserver.StreamRequestHandler) as server:
-1
View File
@@ -730,7 +730,6 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
@unittest.skipIf(os_helper.TESTFN_UNENCODABLE is None,
"need an unencodable filename")
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
def testUnencodable(self):
filename = os_helper.TESTFN_UNENCODABLE + ".zip"
self.addCleanup(os_helper.unlink, filename)
+7 -8
View File
@@ -1065,8 +1065,7 @@ mod _socket {
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
Ok(format!(
"<socket object, fd={}, family={}, type={}, proto={}>",
// cast because INVALID_SOCKET is unsigned, so would show usize::MAX instead of -1
zelf.fileno() as i64,
zelf.fileno(),
zelf.family.load(),
zelf.kind.load(),
zelf.proto.load(),
@@ -1462,25 +1461,25 @@ mod _socket {
#[pymethod]
fn close(&self) -> io::Result<()> {
let sock = self.detach();
if sock != INVALID_SOCKET {
close_inner(sock)?;
if sock != INVALID_SOCKET as i64 {
close_inner(sock as RawSocket)?;
}
Ok(())
}
#[pymethod]
#[inline]
fn detach(&self) -> RawSocket {
fn detach(&self) -> i64 {
let sock = self.sock.write().take();
sock.map_or(INVALID_SOCKET, into_sock_fileno)
sock.map_or(INVALID_SOCKET as i64, |s| into_sock_fileno(s) as i64)
}
#[pymethod]
fn fileno(&self) -> RawSocket {
fn fileno(&self) -> i64 {
self.sock
.read()
.as_ref()
.map_or(INVALID_SOCKET, sock_fileno)
.map_or(INVALID_SOCKET as i64, |s| sock_fileno(s) as i64)
}
#[pymethod]
+17 -11
View File
@@ -1122,10 +1122,18 @@ pub(crate) mod module {
}
#[pyfunction]
fn _path_splitroot(path: OsPath, vm: &VirtualMachine) -> PyResult<(String, String)> {
fn _path_splitroot(
path: OsPath,
_vm: &VirtualMachine,
) -> (
rustpython_common::wtf8::Wtf8Buf,
rustpython_common::wtf8::Wtf8Buf,
) {
use rustpython_common::wtf8::Wtf8Buf;
let orig: Vec<_> = path.path.to_wide();
if orig.is_empty() {
return Ok(("".to_owned(), "".to_owned()));
return (Wtf8Buf::new(), Wtf8Buf::new());
}
let backslashed: Vec<_> = orig
.iter()
@@ -1134,15 +1142,11 @@ pub(crate) mod module {
.chain(std::iter::once(0)) // null-terminated
.collect();
fn from_utf16(wstr: &[u16], vm: &VirtualMachine) -> PyResult<String> {
String::from_utf16(wstr).map_err(|e| vm.new_unicode_decode_error(e.to_string()))
}
let mut end: *const u16 = std::ptr::null();
let hr = unsafe {
windows_sys::Win32::UI::Shell::PathCchSkipRoot(backslashed.as_ptr(), &mut end)
};
let (root, path) = if hr == 0 {
if hr == 0 {
// S_OK
assert!(!end.is_null());
let len: usize = unsafe { end.offset_from(backslashed.as_ptr()) }
@@ -1155,11 +1159,13 @@ pub(crate) mod module {
len,
backslashed.len()
);
(from_utf16(&orig[..len], vm)?, from_utf16(&orig[len..], vm)?)
(
Wtf8Buf::from_wide(&orig[..len]),
Wtf8Buf::from_wide(&orig[len..]),
)
} else {
("".to_owned(), from_utf16(&orig, vm)?)
};
Ok((root, path))
(Wtf8Buf::new(), Wtf8Buf::from_wide(&orig))
}
}
#[pyfunction]