Skip to content

Commit 772b92e

Browse files
authored
Fix windows socket (#6408)
* fix _path_splitroot * Fix windows socket
1 parent 2ff1fba commit 772b92e

File tree

5 files changed

+24
-27
lines changed

5 files changed

+24
-27
lines changed

Lib/test/test_selectors.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ def test_unregister_after_fd_close_and_reuse(self):
131131
s.unregister(r)
132132
s.unregister(w)
133133

134-
# TODO: RUSTPYTHON
135-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
136134
def test_unregister_after_socket_close(self):
137135
s = self.SELECTOR()
138136
self.addCleanup(s.close)

Lib/test/test_socketserver.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,11 @@ def dgram_examine(self, proto, addr):
178178
buf += data
179179
self.assertEqual(buf, TEST_STR)
180180

181-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; AssertionError: -1 != 18446744073709551615")
182181
def test_TCPServer(self):
183182
self.run_server(socketserver.TCPServer,
184183
socketserver.StreamRequestHandler,
185184
self.stream_examine)
186185

187-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; AssertionError: -1 != 18446744073709551615")
188186
def test_ThreadingTCPServer(self):
189187
self.run_server(socketserver.ThreadingTCPServer,
190188
socketserver.StreamRequestHandler,
@@ -217,13 +215,11 @@ def test_ForkingUnixStreamServer(self):
217215
socketserver.StreamRequestHandler,
218216
self.stream_examine)
219217

220-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; AssertionError: -1 != 18446744073709551615")
221218
def test_UDPServer(self):
222219
self.run_server(socketserver.UDPServer,
223220
socketserver.DatagramRequestHandler,
224221
self.dgram_examine)
225222

226-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; AssertionError: -1 != 18446744073709551615")
227223
def test_ThreadingUDPServer(self):
228224
self.run_server(socketserver.ThreadingUDPServer,
229225
socketserver.DatagramRequestHandler,
@@ -298,7 +294,6 @@ def test_tcpserver_bind_leak(self):
298294
socketserver.TCPServer((HOST, -1),
299295
socketserver.StreamRequestHandler)
300296

301-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; AssertionError: -1 != 18446744073709551615")
302297
def test_context_manager(self):
303298
with socketserver.TCPServer((HOST, 0),
304299
socketserver.StreamRequestHandler) as server:

Lib/test/test_zipimport.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,6 @@ def testTraceback(self):
730730

731731
@unittest.skipIf(os_helper.TESTFN_UNENCODABLE is None,
732732
"need an unencodable filename")
733-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
734733
def testUnencodable(self):
735734
filename = os_helper.TESTFN_UNENCODABLE + ".zip"
736735
self.addCleanup(os_helper.unlink, filename)

crates/stdlib/src/socket.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,8 +1065,7 @@ mod _socket {
10651065
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
10661066
Ok(format!(
10671067
"<socket object, fd={}, family={}, type={}, proto={}>",
1068-
// cast because INVALID_SOCKET is unsigned, so would show usize::MAX instead of -1
1069-
zelf.fileno() as i64,
1068+
zelf.fileno(),
10701069
zelf.family.load(),
10711070
zelf.kind.load(),
10721071
zelf.proto.load(),
@@ -1462,25 +1461,25 @@ mod _socket {
14621461
#[pymethod]
14631462
fn close(&self) -> io::Result<()> {
14641463
let sock = self.detach();
1465-
if sock != INVALID_SOCKET {
1466-
close_inner(sock)?;
1464+
if sock != INVALID_SOCKET as i64 {
1465+
close_inner(sock as RawSocket)?;
14671466
}
14681467
Ok(())
14691468
}
14701469

14711470
#[pymethod]
14721471
#[inline]
1473-
fn detach(&self) -> RawSocket {
1472+
fn detach(&self) -> i64 {
14741473
let sock = self.sock.write().take();
1475-
sock.map_or(INVALID_SOCKET, into_sock_fileno)
1474+
sock.map_or(INVALID_SOCKET as i64, |s| into_sock_fileno(s) as i64)
14761475
}
14771476

14781477
#[pymethod]
1479-
fn fileno(&self) -> RawSocket {
1478+
fn fileno(&self) -> i64 {
14801479
self.sock
14811480
.read()
14821481
.as_ref()
1483-
.map_or(INVALID_SOCKET, sock_fileno)
1482+
.map_or(INVALID_SOCKET as i64, |s| sock_fileno(s) as i64)
14841483
}
14851484

14861485
#[pymethod]

crates/vm/src/stdlib/nt.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,10 +1122,18 @@ pub(crate) mod module {
11221122
}
11231123

11241124
#[pyfunction]
1125-
fn _path_splitroot(path: OsPath, vm: &VirtualMachine) -> PyResult<(String, String)> {
1125+
fn _path_splitroot(
1126+
path: OsPath,
1127+
_vm: &VirtualMachine,
1128+
) -> (
1129+
rustpython_common::wtf8::Wtf8Buf,
1130+
rustpython_common::wtf8::Wtf8Buf,
1131+
) {
1132+
use rustpython_common::wtf8::Wtf8Buf;
1133+
11261134
let orig: Vec<_> = path.path.to_wide();
11271135
if orig.is_empty() {
1128-
return Ok(("".to_owned(), "".to_owned()));
1136+
return (Wtf8Buf::new(), Wtf8Buf::new());
11291137
}
11301138
let backslashed: Vec<_> = orig
11311139
.iter()
@@ -1134,15 +1142,11 @@ pub(crate) mod module {
11341142
.chain(std::iter::once(0)) // null-terminated
11351143
.collect();
11361144

1137-
fn from_utf16(wstr: &[u16], vm: &VirtualMachine) -> PyResult<String> {
1138-
String::from_utf16(wstr).map_err(|e| vm.new_unicode_decode_error(e.to_string()))
1139-
}
1140-
11411145
let mut end: *const u16 = std::ptr::null();
11421146
let hr = unsafe {
11431147
windows_sys::Win32::UI::Shell::PathCchSkipRoot(backslashed.as_ptr(), &mut end)
11441148
};
1145-
let (root, path) = if hr == 0 {
1149+
if hr == 0 {
11461150
// S_OK
11471151
assert!(!end.is_null());
11481152
let len: usize = unsafe { end.offset_from(backslashed.as_ptr()) }
@@ -1155,11 +1159,13 @@ pub(crate) mod module {
11551159
len,
11561160
backslashed.len()
11571161
);
1158-
(from_utf16(&orig[..len], vm)?, from_utf16(&orig[len..], vm)?)
1162+
(
1163+
Wtf8Buf::from_wide(&orig[..len]),
1164+
Wtf8Buf::from_wide(&orig[len..]),
1165+
)
11591166
} else {
1160-
("".to_owned(), from_utf16(&orig, vm)?)
1161-
};
1162-
Ok((root, path))
1167+
(Wtf8Buf::new(), Wtf8Buf::from_wide(&orig))
1168+
}
11631169
}
11641170

11651171
#[pyfunction]

0 commit comments

Comments
 (0)