Skip to content

Commit 4cf2672

Browse files
committed
Add _multiprocessing stub
1 parent 928a404 commit 4cf2672

File tree

5 files changed

+92
-3
lines changed

5 files changed

+92
-3
lines changed

Lib/asyncio/windows_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99

1010
import _winapi
1111
import itertools
12-
import msvcrt
12+
# XXX RustPython TODO: msvcrt
13+
# import msvcrt
1314
import os
1415
import socket
1516
import subprocess
16-
import tempfile
17+
# import tempfile
1718
import warnings
1819

1920

Lib/multiprocessing/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#import tempfile
1919
import itertools
2020

21-
#import _multiprocessing
21+
import _multiprocessing
2222

2323
from . import util
2424

vm/src/stdlib/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ use crate::vm::VirtualMachine;
3939
#[cfg(not(target_arch = "wasm32"))]
4040
pub mod io;
4141
#[cfg(not(target_arch = "wasm32"))]
42+
mod multiprocessing;
43+
#[cfg(not(target_arch = "wasm32"))]
4244
mod os;
4345
#[cfg(all(unix, not(any(target_os = "android", target_os = "redox"))))]
4446
mod pwd;
@@ -109,6 +111,10 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
109111
modules.insert("_io".to_string(), Box::new(io::make_module));
110112
modules.insert("_os".to_string(), Box::new(os::make_module));
111113
modules.insert("_socket".to_string(), Box::new(socket::make_module));
114+
modules.insert(
115+
"_multiprocessing".to_string(),
116+
Box::new(multiprocessing::make_module),
117+
);
112118
modules.insert("signal".to_string(), Box::new(signal::make_module));
113119
modules.insert("select".to_string(), Box::new(select::make_module));
114120
modules.insert("_subprocess".to_string(), Box::new(subprocess::make_module));

vm/src/stdlib/multiprocessing.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#[allow(unused_imports)]
2+
use crate::obj::objbyteinner::PyBytesLike;
3+
#[allow(unused_imports)]
4+
use crate::pyobject::{PyObjectRef, PyResult};
5+
use crate::VirtualMachine;
6+
7+
#[cfg(windows)]
8+
use winapi::um::winsock2::{self, SOCKET};
9+
10+
#[cfg(windows)]
11+
fn multiprocessing_closesocket(socket: usize, vm: &VirtualMachine) -> PyResult<()> {
12+
let res = unsafe { winsock2::closesocket(socket as SOCKET) };
13+
if res == 0 {
14+
Err(super::os::convert_io_error(
15+
vm,
16+
std::io::Error::last_os_error(),
17+
))
18+
} else {
19+
Ok(())
20+
}
21+
}
22+
23+
#[cfg(windows)]
24+
fn multiprocessing_recv(socket: usize, size: usize, vm: &VirtualMachine) -> PyResult<libc::c_int> {
25+
let mut buf = vec![0 as libc::c_char; size];
26+
let nread =
27+
unsafe { winsock2::recv(socket as SOCKET, buf.as_mut_ptr() as *mut _, size as i32, 0) };
28+
if nread < 0 {
29+
Err(super::os::convert_io_error(
30+
vm,
31+
std::io::Error::last_os_error(),
32+
))
33+
} else {
34+
Ok(nread)
35+
}
36+
}
37+
38+
#[cfg(windows)]
39+
fn multiprocessing_send(
40+
socket: usize,
41+
buf: PyBytesLike,
42+
vm: &VirtualMachine,
43+
) -> PyResult<libc::c_int> {
44+
let buf = buf.to_cow();
45+
let ret = unsafe {
46+
winsock2::send(
47+
socket as SOCKET,
48+
buf.as_ptr() as *const _,
49+
buf.len() as i32,
50+
0,
51+
)
52+
};
53+
if ret < 0 {
54+
Err(super::os::convert_io_error(
55+
vm,
56+
std::io::Error::last_os_error(),
57+
))
58+
} else {
59+
Ok(ret)
60+
}
61+
}
62+
63+
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
64+
let module = py_module!(vm, "_multiprocessing", {});
65+
extend_module_platform_specific(vm, &module);
66+
module
67+
}
68+
69+
#[cfg(windows)]
70+
fn extend_module_platform_specific(vm: &VirtualMachine, module: &PyObjectRef) {
71+
let ctx = &vm.ctx;
72+
extend_module!(vm, module, {
73+
"closesocket" => ctx.new_rustfunc(multiprocessing_closesocket),
74+
"recv" => ctx.new_rustfunc(multiprocessing_recv),
75+
"send" => ctx.new_rustfunc(multiprocessing_send),
76+
})
77+
}
78+
79+
#[cfg(not(windows))]
80+
fn extend_module_platform_specific(_vm: &VirtualMachine, _module: &PyObjectRef) {}

vm/src/stdlib/winapi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(non_snake_case)]
2+
13
use std::io;
24
use winapi::shared::winerror;
35
use winapi::um::winnt::HANDLE;

0 commit comments

Comments
 (0)