Skip to content

Commit 5ae1fca

Browse files
committed
Improve UX on windows
Use the `dirs` crate to find the config dir on non-unix; fix warnings
1 parent a3081c5 commit 5ae1fca

File tree

5 files changed

+57
-45
lines changed

5 files changed

+57
-45
lines changed

Cargo.lock

Lines changed: 26 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ flame-it = ["rustpython-vm/flame-it", "flame", "flamescope"]
2020
freeze-stdlib = ["rustpython-vm/freeze-stdlib"]
2121

2222
[dependencies]
23-
log="0.4.1"
24-
env_logger="0.5.10"
25-
clap = "2.31.2"
23+
log = "0.4"
24+
env_logger = "0.6"
25+
clap = "2.33"
2626
rustpython-compiler = {path = "compiler", version = "0.1.0"}
2727
rustpython-parser = {path = "parser", version = "0.1.0"}
2828
rustpython-vm = {path = "vm", version = "0.1.0"}
29-
xdg = "2.2.0"
29+
dirs = "2.0"
3030

3131
flame = { version = "0.2", optional = true }
3232
flamescope = { version = "0.1", optional = true }

src/main.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -463,21 +463,6 @@ fn shell_exec(vm: &VirtualMachine, source: &str, scope: Scope) -> ShellExecResul
463463
}
464464
}
465465

466-
#[cfg(not(unix))]
467-
fn get_history_path() -> PathBuf {
468-
PathBuf::from(".repl_history.txt")
469-
}
470-
471-
#[cfg(unix)]
472-
fn get_history_path() -> PathBuf {
473-
//work around for windows dependent builds. The xdg crate is unix specific
474-
//so access to the BaseDirectories struct breaks builds on python.
475-
extern crate xdg;
476-
477-
let xdg_dirs = xdg::BaseDirectories::with_prefix("rustpython").unwrap();
478-
xdg_dirs.place_cache_file("repl_history.txt").unwrap()
479-
}
480-
481466
fn get_prompt(vm: &VirtualMachine, prompt_name: &str) -> Option<PyStringRef> {
482467
vm.get_attribute(vm.sys_module.clone(), prompt_name)
483468
.and_then(|prompt| vm.to_str(&prompt))
@@ -498,8 +483,22 @@ fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
498483
let mut repl = Editor::<()>::new();
499484

500485
// Retrieve a `history_path_str` dependent on the OS
501-
let repl_history_path_str = &get_history_path();
502-
if repl.load_history(repl_history_path_str).is_err() {
486+
let repl_history_path = match dirs::config_dir() {
487+
Some(mut path) => {
488+
path.push("rustpython");
489+
path.push("repl_history.txt");
490+
path
491+
}
492+
None => ".repl_history.txt".into(),
493+
};
494+
495+
if !repl_history_path.exists() {
496+
if let Some(parent) = repl_history_path.parent() {
497+
std::fs::create_dir_all(parent).unwrap();
498+
}
499+
}
500+
501+
if repl.load_history(&repl_history_path).is_err() {
503502
println!("No previous history.");
504503
}
505504

@@ -540,21 +539,22 @@ fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
540539

541540
match shell_exec(vm, &input, scope.clone()) {
542541
ShellExecResult::Ok => {
543-
input = String::new();
542+
input.clear();
544543
Ok(())
545544
}
546545
ShellExecResult::Continue => {
547546
continuing = true;
548547
Ok(())
549548
}
550549
ShellExecResult::PyErr(err) => {
551-
input = String::new();
550+
input.clear();
552551
Err(err)
553552
}
554553
}
555554
}
556555
Err(ReadlineError::Interrupted) => {
557556
continuing = false;
557+
input.clear();
558558
let keyboard_interrupt = vm
559559
.new_empty_exception(vm.ctx.exceptions.keyboard_interrupt.clone())
560560
.unwrap();
@@ -573,7 +573,7 @@ fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
573573
print_exception(vm, &exc);
574574
}
575575
}
576-
repl.save_history(repl_history_path_str).unwrap();
576+
repl.save_history(&repl_history_path).unwrap();
577577

578578
Ok(())
579579
}

vm/src/stdlib/os.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use num_cpus;
22
use std::cell::RefCell;
3-
use std::ffi::CStr;
3+
use std::ffi;
44
use std::fs::File;
55
use std::fs::OpenOptions;
6-
use std::io::{self, Error, ErrorKind, Read, Write};
6+
use std::io::{self, ErrorKind, Read, Write};
77
use std::time::{Duration, SystemTime};
88
use std::{env, fs};
99

@@ -53,11 +53,10 @@ pub fn raw_file_number(handle: File) -> i64 {
5353

5454
#[cfg(windows)]
5555
pub fn rust_file(raw_fileno: i64) -> File {
56-
use std::ffi::c_void;
5756
use std::os::windows::io::FromRawHandle;
5857

5958
//This seems to work as expected but further testing is required.
60-
unsafe { File::from_raw_handle(raw_fileno as *mut c_void) }
59+
unsafe { File::from_raw_handle(raw_fileno as *mut ffi::c_void) }
6160
}
6261

6362
#[cfg(all(not(unix), not(windows)))]
@@ -1021,9 +1020,9 @@ pub fn os_ttyname(fd: PyIntRef, vm: &VirtualMachine) -> PyResult {
10211020
if let Some(fd) = fd.as_bigint().to_i32() {
10221021
let name = unsafe { ttyname(fd) };
10231022
if name.is_null() {
1024-
Err(vm.new_os_error(Error::last_os_error().to_string()))
1023+
Err(vm.new_os_error(io::Error::last_os_error().to_string()))
10251024
} else {
1026-
let name = unsafe { CStr::from_ptr(name) }.to_str().unwrap();
1025+
let name = unsafe { ffi::CStr::from_ptr(name) }.to_str().unwrap();
10271026
Ok(vm.ctx.new_str(name.to_owned()))
10281027
}
10291028
} else {
@@ -1096,6 +1095,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
10961095
}
10971096
}
10981097
}
1098+
#[allow(unused_mut)]
10991099
let mut support_funcs = vec![
11001100
SupportFunc::new(vm, "open", os_open, None, Some(false), None),
11011101
// access Some Some None

vm/src/stdlib/signal.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
use crate::obj::objint::PyIntRef;
21
use crate::pyobject::{PyObjectRef, PyResult, TryFromObject};
32
use crate::vm::{VirtualMachine, NSIG};
43

54
use std::sync::atomic::{AtomicBool, Ordering};
65

7-
use num_traits::cast::ToPrimitive;
8-
96
use arr_macro::arr;
107

118
#[cfg(unix)]
@@ -85,8 +82,7 @@ fn getsignal(signalnum: i32, vm: &VirtualMachine) -> PyResult<PyObjectRef> {
8582
}
8683

8784
#[cfg(unix)]
88-
fn alarm(time: PyIntRef, _vm: &VirtualMachine) -> u32 {
89-
let time = time.as_bigint().to_u32().unwrap();
85+
fn alarm(time: u32, _vm: &VirtualMachine) -> u32 {
9086
let prev_time = if time == 0 {
9187
sig_alarm::cancel()
9288
} else {

0 commit comments

Comments
 (0)