Skip to content

Improve UX on windows #1340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
40 changes: 20 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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.");
}

Expand Down Expand Up @@ -540,21 +539,22 @@ 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 => {
continuing = true;
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();
Expand All @@ -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(())
}
Expand Down
12 changes: 6 additions & 6 deletions vm/src/stdlib/os.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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)))]
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions vm/src/stdlib/signal.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down