forked from Rust-related/RustPython
Add _multiprocessing stub
This commit is contained in:
@@ -9,11 +9,12 @@ if sys.platform != 'win32': # pragma: no cover
|
||||
|
||||
import _winapi
|
||||
import itertools
|
||||
import msvcrt
|
||||
# XXX RustPython TODO: msvcrt
|
||||
# import msvcrt
|
||||
import os
|
||||
import socket
|
||||
import subprocess
|
||||
import tempfile
|
||||
# import tempfile
|
||||
import warnings
|
||||
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ import time
|
||||
#import tempfile
|
||||
import itertools
|
||||
|
||||
#import _multiprocessing
|
||||
import _multiprocessing
|
||||
|
||||
from . import util
|
||||
|
||||
|
||||
@@ -39,6 +39,8 @@ use crate::vm::VirtualMachine;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub mod io;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
mod multiprocessing;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
mod os;
|
||||
#[cfg(all(unix, not(any(target_os = "android", target_os = "redox"))))]
|
||||
mod pwd;
|
||||
@@ -109,6 +111,10 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
|
||||
modules.insert("_io".to_string(), Box::new(io::make_module));
|
||||
modules.insert("_os".to_string(), Box::new(os::make_module));
|
||||
modules.insert("_socket".to_string(), Box::new(socket::make_module));
|
||||
modules.insert(
|
||||
"_multiprocessing".to_string(),
|
||||
Box::new(multiprocessing::make_module),
|
||||
);
|
||||
modules.insert("signal".to_string(), Box::new(signal::make_module));
|
||||
modules.insert("select".to_string(), Box::new(select::make_module));
|
||||
modules.insert("_subprocess".to_string(), Box::new(subprocess::make_module));
|
||||
|
||||
80
vm/src/stdlib/multiprocessing.rs
Normal file
80
vm/src/stdlib/multiprocessing.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
#[allow(unused_imports)]
|
||||
use crate::obj::objbyteinner::PyBytesLike;
|
||||
#[allow(unused_imports)]
|
||||
use crate::pyobject::{PyObjectRef, PyResult};
|
||||
use crate::VirtualMachine;
|
||||
|
||||
#[cfg(windows)]
|
||||
use winapi::um::winsock2::{self, SOCKET};
|
||||
|
||||
#[cfg(windows)]
|
||||
fn multiprocessing_closesocket(socket: usize, vm: &VirtualMachine) -> PyResult<()> {
|
||||
let res = unsafe { winsock2::closesocket(socket as SOCKET) };
|
||||
if res == 0 {
|
||||
Err(super::os::convert_io_error(
|
||||
vm,
|
||||
std::io::Error::last_os_error(),
|
||||
))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn multiprocessing_recv(socket: usize, size: usize, vm: &VirtualMachine) -> PyResult<libc::c_int> {
|
||||
let mut buf = vec![0 as libc::c_char; size];
|
||||
let nread =
|
||||
unsafe { winsock2::recv(socket as SOCKET, buf.as_mut_ptr() as *mut _, size as i32, 0) };
|
||||
if nread < 0 {
|
||||
Err(super::os::convert_io_error(
|
||||
vm,
|
||||
std::io::Error::last_os_error(),
|
||||
))
|
||||
} else {
|
||||
Ok(nread)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn multiprocessing_send(
|
||||
socket: usize,
|
||||
buf: PyBytesLike,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<libc::c_int> {
|
||||
let buf = buf.to_cow();
|
||||
let ret = unsafe {
|
||||
winsock2::send(
|
||||
socket as SOCKET,
|
||||
buf.as_ptr() as *const _,
|
||||
buf.len() as i32,
|
||||
0,
|
||||
)
|
||||
};
|
||||
if ret < 0 {
|
||||
Err(super::os::convert_io_error(
|
||||
vm,
|
||||
std::io::Error::last_os_error(),
|
||||
))
|
||||
} else {
|
||||
Ok(ret)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
|
||||
let module = py_module!(vm, "_multiprocessing", {});
|
||||
extend_module_platform_specific(vm, &module);
|
||||
module
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn extend_module_platform_specific(vm: &VirtualMachine, module: &PyObjectRef) {
|
||||
let ctx = &vm.ctx;
|
||||
extend_module!(vm, module, {
|
||||
"closesocket" => ctx.new_rustfunc(multiprocessing_closesocket),
|
||||
"recv" => ctx.new_rustfunc(multiprocessing_recv),
|
||||
"send" => ctx.new_rustfunc(multiprocessing_send),
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
fn extend_module_platform_specific(_vm: &VirtualMachine, _module: &PyObjectRef) {}
|
||||
@@ -1,3 +1,5 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use std::io;
|
||||
use winapi::shared::winerror;
|
||||
use winapi::um::winnt::HANDLE;
|
||||
|
||||
Reference in New Issue
Block a user