#!/usr/bin/env python3

def load_history():
    import os, atexit
    try:
        import readline
    except ImportError:
        return

    histfile = os.getenv("PYTHON_REPL_HISTORY")
    if histfile is None:
        return

    try:
        readline.read_history_file(histfile)
    except IOError:
        pass

    if readline.get_current_history_length() == 0:
        # We absolutely don't want to see ~/.python_history be created, which
        # python3 will do if the history is empty.  So we create some fake
        # history.
        readline.add_history("# I hate ~/.python_history with a passion")

    atexit.register(readline.write_history_file, histfile)

load_history()
