]>
Commit | Line | Data |
---|---|---|
397c4fed IB |
1 | import sys as sys_ |
2 | ||
3 | # Adapted from /usr/lib/python3.7/site.py # enablerlcompleter | |
4 | def register_readline(): | |
5 | import os | |
6 | import atexit | |
7 | try: | |
8 | import readline | |
9 | import rlcompleter | |
10 | except ImportError: | |
11 | return | |
12 | ||
13 | # Reading the initialization (config) file may not be enough to set a | |
14 | # completion key, so we set one first and then read the file. | |
15 | readline_doc = getattr(readline, '__doc__', '') | |
16 | if readline_doc is not None and 'libedit' in readline_doc: | |
17 | readline.parse_and_bind('bind ^I rl_complete') | |
18 | else: | |
19 | readline.parse_and_bind('tab: complete') | |
20 | ||
21 | try: | |
22 | readline.read_init_file() | |
23 | except OSError: | |
24 | # An OSError here could have many causes, but the most likely one | |
25 | # is that there's no .inputrc file (or .editrc file in the case of | |
26 | # Mac OS X + libedit) in the expected location. In that case, we | |
27 | # want to ignore the exception. | |
28 | pass | |
29 | ||
30 | if readline.get_current_history_length() == 0: | |
31 | # If no history was loaded, default to .python_history. | |
32 | # The guard is necessary to avoid doubling history size at | |
33 | # each interpreter exit when readline was already configured | |
34 | # through a PYTHONSTARTUP hook, see: | |
35 | # http://bugs.python.org/issue5845#msg198636 | |
36 | environ = os.environ.get("XDG_STATE_HOME") or os.environ.get("XDG_DATA_HOME") | |
37 | if environ is not None: | |
38 | if not os.path.exists(os.path.join(environ, "python")): | |
39 | os.mkdir(os.path.join(environ, "python")) | |
40 | history = os.path.join(environ, "python", "history") | |
41 | else: | |
42 | history = os.path.join(os.path.expanduser('~'), | |
43 | '.python_history') | |
44 | try: | |
45 | readline.read_history_file(history) | |
46 | except OSError: | |
47 | pass | |
48 | atexit.register(readline.write_history_file, history) | |
49 | ||
50 | sys_.__interactivehook__ = register_readline | |
51 | ||
52 | del sys_ |