Do not fail in python2.7
This commit is contained in:
parent
d9372bd5b5
commit
1f86901f00
1 changed files with 24 additions and 19 deletions
|
@ -4,30 +4,35 @@
|
||||||
import os
|
import os
|
||||||
import atexit
|
import atexit
|
||||||
import readline
|
import readline
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
if readline.get_current_history_length() == 0:
|
try:
|
||||||
state_home = os.environ.get("XDG_STATE_HOME")
|
import pathlib
|
||||||
if state_home is None:
|
except ImportError: # not part of python2
|
||||||
state_home = Path.home() / ".local" / "state"
|
pass
|
||||||
else:
|
else:
|
||||||
state_home = Path(state_home)
|
|
||||||
|
|
||||||
history_path = state_home / "python" / "history"
|
if readline.get_current_history_length() == 0:
|
||||||
if history_path.is_dir():
|
state_home = os.environ.get("XDG_STATE_HOME")
|
||||||
raise OSError(f"'{history_path}' cannot be a directory")
|
if state_home is None:
|
||||||
|
state_home = pathlib.Path.home() / ".local" / "state"
|
||||||
|
else:
|
||||||
|
state_home = pathlib.Path(state_home)
|
||||||
|
|
||||||
history = str(history_path)
|
history_path = state_home / "python" / "history"
|
||||||
|
if history_path.is_dir():
|
||||||
|
raise OSError(history_path + " cannot be a directory")
|
||||||
|
|
||||||
try:
|
history = str(history_path)
|
||||||
readline.read_history_file(history)
|
|
||||||
except OSError: # Non existent
|
|
||||||
pass
|
|
||||||
|
|
||||||
def write_history():
|
|
||||||
try:
|
try:
|
||||||
readline.write_history_file(history)
|
readline.read_history_file(history)
|
||||||
except OSError:
|
except OSError: # Non existent
|
||||||
pass
|
pass
|
||||||
|
|
||||||
atexit.register(write_history)
|
def write_history():
|
||||||
|
try:
|
||||||
|
readline.write_history_file(history)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
atexit.register(write_history)
|
||||||
|
|
Loading…
Reference in a new issue