@@ -509,35 +509,35 @@ def register_readline():
509
509
pass
510
510
511
511
if readline .get_current_history_length () == 0 :
512
+ try :
513
+ from _pyrepl .main import CAN_USE_PYREPL
514
+ except ImportError :
515
+ CAN_USE_PYREPL = False
512
516
# If no history was loaded, default to .python_history,
513
517
# or PYTHON_HISTORY.
514
518
# The guard is necessary to avoid doubling history size at
515
519
# each interpreter exit when readline was already configured
516
520
# through a PYTHONSTARTUP hook, see:
517
521
# http://bugs.python.org/issue5845#msg198636
518
522
history = gethistoryfile ()
523
+ if os .getenv ("PYTHON_BASIC_REPL" ) or not CAN_USE_PYREPL :
524
+ my_readline = readline
525
+ else :
526
+ my_readline = _pyrepl .readline
519
527
try :
520
- if os .getenv ("PYTHON_BASIC_REPL" ):
521
- readline .read_history_file (history )
522
- else :
523
- _pyrepl .readline .read_history_file (history )
528
+ my_readline .read_history_file (history )
524
529
except (OSError ,* _pyrepl .unix_console ._error ):
525
530
pass
526
531
527
532
def write_history ():
528
533
try :
529
- from _pyrepl .main import CAN_USE_PYREPL
530
- except ImportError :
531
- CAN_USE_PYREPL = False
532
-
533
- try :
534
- if os .getenv ("PYTHON_BASIC_REPL" ) or not CAN_USE_PYREPL :
535
- readline .write_history_file (history )
536
- else :
537
- _pyrepl .readline .write_history_file (history )
538
- except (FileNotFoundError , PermissionError ):
534
+ if my_readline .get_current_history_length () > 0 :
535
+ my_readline .write_history_file (history )
536
+ except (FileNotFoundError , PermissionError ,
537
+ _pyrepl .unix_console .InvalidTerminal ):
539
538
# home directory does not exist or is not writable
540
539
# https://bugs.python.org/issue19891
540
+ # or terminal doesn't have the required capability
541
541
pass
542
542
543
543
atexit .register (write_history )
0 commit comments