forked from Rust-related/RustPython
Merge pull request #1340 from RustPython/coolreader18/windows-improvements
Improve UX on windows
This commit is contained in:
36
Cargo.lock
generated
36
Cargo.lock
generated
@@ -328,6 +328,26 @@ dependencies = [
|
||||
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs"
|
||||
version = "2.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"dirs-sys 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs-sys"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"redox_users 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "docopt"
|
||||
version = "1.1.0"
|
||||
@@ -354,7 +374,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "env_logger"
|
||||
version = "0.5.13"
|
||||
version = "0.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@@ -1088,7 +1108,8 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"cpython 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"env_logger 0.5.13 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"flame 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"flamescope 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@@ -1096,7 +1117,6 @@ dependencies = [
|
||||
"rustpython-parser 0.1.0",
|
||||
"rustpython-vm 0.1.0",
|
||||
"rustyline 4.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"xdg 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2023,11 +2043,6 @@ name = "wtf8"
|
||||
version = "0.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "xdg"
|
||||
version = "2.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[metadata]
|
||||
"checksum adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7e522997b529f05601e05166c07ed17789691f562762c7f3b987263d2dedee5c"
|
||||
"checksum aho-corasick 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "81ce3d38065e618af2d7b77e10c5ad9a069859b4be3c2250f674af3840d9c8a5"
|
||||
@@ -2071,10 +2086,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum diff 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "3c2b69f912779fbb121ceb775d74d51e915af17aaebc38d28a592843a2dd0a3a"
|
||||
"checksum digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5"
|
||||
"checksum dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3fd78930633bd1c6e35c4b42b1df7b0cbc6bc191146e512bb3bedf243fcc3901"
|
||||
"checksum dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "13aea89a5c93364a98e9b37b2fa237effbb694d5cfe01c5b70941f7eb087d5e3"
|
||||
"checksum dirs-sys 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "afa0b23de8fd801745c471deffa6e12d248f962c9fd4b4c33787b055599bde7b"
|
||||
"checksum docopt 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7f525a586d310c87df72ebcd98009e57f1cc030c8c268305287a476beb653969"
|
||||
"checksum either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5527cfe0d098f36e3f8839852688e63c8fff1c90b2b405aef730615f9a7bcf7b"
|
||||
"checksum ena 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f56c93cc076508c549d9bb747f79aa9b4eb098be7b8cad8830c3137ef52d1e00"
|
||||
"checksum env_logger 0.5.13 (registry+https://github.com/rust-lang/crates.io-index)" = "15b0a4d2e39f8420210be8b27eeda28029729e2fd4291019455016c348240c38"
|
||||
"checksum env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "aafcde04e90a5226a6443b7aabdb016ba2f8307c847d524724bd9b346dd1a2d3"
|
||||
"checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2"
|
||||
"checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1"
|
||||
"checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
|
||||
@@ -2257,4 +2274,3 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
"checksum wincolor 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "96f5016b18804d24db43cebf3c77269e7569b8954a8464501c216cc5e070eaa9"
|
||||
"checksum wtf8 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d6b9309a86639c488a8eb2b5331cb5127cc9feb0a94a0db4b5d1ab5b84977956"
|
||||
"checksum xdg 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d089681aa106a86fade1b0128fb5daf07d5867a509ab036d99988dec80429a57"
|
||||
|
||||
@@ -20,13 +20,13 @@ flame-it = ["rustpython-vm/flame-it", "flame", "flamescope"]
|
||||
freeze-stdlib = ["rustpython-vm/freeze-stdlib"]
|
||||
|
||||
[dependencies]
|
||||
log="0.4.1"
|
||||
env_logger="0.5.10"
|
||||
clap = "2.31.2"
|
||||
log = "0.4"
|
||||
env_logger = "0.6"
|
||||
clap = "2.33"
|
||||
rustpython-compiler = {path = "compiler", version = "0.1.0"}
|
||||
rustpython-parser = {path = "parser", version = "0.1.0"}
|
||||
rustpython-vm = {path = "vm", version = "0.1.0"}
|
||||
xdg = "2.2.0"
|
||||
dirs = "2.0"
|
||||
|
||||
flame = { version = "0.2", optional = true }
|
||||
flamescope = { version = "0.1", optional = true }
|
||||
|
||||
40
src/main.rs
40
src/main.rs
@@ -463,21 +463,6 @@ fn shell_exec(vm: &VirtualMachine, source: &str, scope: Scope) -> ShellExecResul
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn get_history_path() -> PathBuf {
|
||||
PathBuf::from(".repl_history.txt")
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn get_history_path() -> PathBuf {
|
||||
//work around for windows dependent builds. The xdg crate is unix specific
|
||||
//so access to the BaseDirectories struct breaks builds on python.
|
||||
extern crate xdg;
|
||||
|
||||
let xdg_dirs = xdg::BaseDirectories::with_prefix("rustpython").unwrap();
|
||||
xdg_dirs.place_cache_file("repl_history.txt").unwrap()
|
||||
}
|
||||
|
||||
fn get_prompt(vm: &VirtualMachine, prompt_name: &str) -> Option<PyStringRef> {
|
||||
vm.get_attribute(vm.sys_module.clone(), prompt_name)
|
||||
.and_then(|prompt| vm.to_str(&prompt))
|
||||
@@ -498,8 +483,22 @@ fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
|
||||
let mut repl = Editor::<()>::new();
|
||||
|
||||
// Retrieve a `history_path_str` dependent on the OS
|
||||
let repl_history_path_str = &get_history_path();
|
||||
if repl.load_history(repl_history_path_str).is_err() {
|
||||
let repl_history_path = match dirs::config_dir() {
|
||||
Some(mut path) => {
|
||||
path.push("rustpython");
|
||||
path.push("repl_history.txt");
|
||||
path
|
||||
}
|
||||
None => ".repl_history.txt".into(),
|
||||
};
|
||||
|
||||
if !repl_history_path.exists() {
|
||||
if let Some(parent) = repl_history_path.parent() {
|
||||
std::fs::create_dir_all(parent).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
if repl.load_history(&repl_history_path).is_err() {
|
||||
println!("No previous history.");
|
||||
}
|
||||
|
||||
@@ -540,7 +539,7 @@ fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
|
||||
|
||||
match shell_exec(vm, &input, scope.clone()) {
|
||||
ShellExecResult::Ok => {
|
||||
input = String::new();
|
||||
input.clear();
|
||||
Ok(())
|
||||
}
|
||||
ShellExecResult::Continue => {
|
||||
@@ -548,13 +547,14 @@ fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
ShellExecResult::PyErr(err) => {
|
||||
input = String::new();
|
||||
input.clear();
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(ReadlineError::Interrupted) => {
|
||||
continuing = false;
|
||||
input.clear();
|
||||
let keyboard_interrupt = vm
|
||||
.new_empty_exception(vm.ctx.exceptions.keyboard_interrupt.clone())
|
||||
.unwrap();
|
||||
@@ -573,7 +573,7 @@ fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
|
||||
print_exception(vm, &exc);
|
||||
}
|
||||
}
|
||||
repl.save_history(repl_history_path_str).unwrap();
|
||||
repl.save_history(&repl_history_path).unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use num_cpus;
|
||||
use std::cell::RefCell;
|
||||
use std::ffi::CStr;
|
||||
use std::ffi;
|
||||
use std::fs::File;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::{self, Error, ErrorKind, Read, Write};
|
||||
use std::io::{self, ErrorKind, Read, Write};
|
||||
use std::time::{Duration, SystemTime};
|
||||
use std::{env, fs};
|
||||
|
||||
@@ -53,11 +53,10 @@ pub fn raw_file_number(handle: File) -> i64 {
|
||||
|
||||
#[cfg(windows)]
|
||||
pub fn rust_file(raw_fileno: i64) -> File {
|
||||
use std::ffi::c_void;
|
||||
use std::os::windows::io::FromRawHandle;
|
||||
|
||||
//This seems to work as expected but further testing is required.
|
||||
unsafe { File::from_raw_handle(raw_fileno as *mut c_void) }
|
||||
unsafe { File::from_raw_handle(raw_fileno as *mut ffi::c_void) }
|
||||
}
|
||||
|
||||
#[cfg(all(not(unix), not(windows)))]
|
||||
@@ -1021,9 +1020,9 @@ pub fn os_ttyname(fd: PyIntRef, vm: &VirtualMachine) -> PyResult {
|
||||
if let Some(fd) = fd.as_bigint().to_i32() {
|
||||
let name = unsafe { ttyname(fd) };
|
||||
if name.is_null() {
|
||||
Err(vm.new_os_error(Error::last_os_error().to_string()))
|
||||
Err(vm.new_os_error(io::Error::last_os_error().to_string()))
|
||||
} else {
|
||||
let name = unsafe { CStr::from_ptr(name) }.to_str().unwrap();
|
||||
let name = unsafe { ffi::CStr::from_ptr(name) }.to_str().unwrap();
|
||||
Ok(vm.ctx.new_str(name.to_owned()))
|
||||
}
|
||||
} else {
|
||||
@@ -1096,6 +1095,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
|
||||
}
|
||||
}
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
let mut support_funcs = vec![
|
||||
SupportFunc::new(vm, "open", os_open, None, Some(false), None),
|
||||
// access Some Some None
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
use crate::obj::objint::PyIntRef;
|
||||
use crate::pyobject::{PyObjectRef, PyResult, TryFromObject};
|
||||
use crate::vm::{VirtualMachine, NSIG};
|
||||
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
use num_traits::cast::ToPrimitive;
|
||||
|
||||
use arr_macro::arr;
|
||||
|
||||
#[cfg(unix)]
|
||||
@@ -85,8 +82,7 @@ fn getsignal(signalnum: i32, vm: &VirtualMachine) -> PyResult<PyObjectRef> {
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn alarm(time: PyIntRef, _vm: &VirtualMachine) -> u32 {
|
||||
let time = time.as_bigint().to_u32().unwrap();
|
||||
fn alarm(time: u32, _vm: &VirtualMachine) -> u32 {
|
||||
let prev_time = if time == 0 {
|
||||
sig_alarm::cancel()
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user