Skip to content

Commit

Permalink
[build] Copy readline module to line_input so we can fork it.
Browse files Browse the repository at this point in the history
This will aid in correct history expansion (#231) as well as completion
on OS X (#228).

Tested:

- dev build with line_input.so
- Building _bin/oil.ovm{,-dbg} in the repo
- tarball build on my machine (build/test.sh)
- tarball build in Alpine Linux, without readline (test/alpine.sh)
  • Loading branch information
Andy Chu committed Feb 20, 2019
1 parent c014ea7 commit 391297e
Show file tree
Hide file tree
Showing 11 changed files with 1,337 additions and 30 deletions.
8 changes: 0 additions & 8 deletions Python-2.7.13/Modules/main.c
Expand Up @@ -335,14 +335,6 @@ Ovm_Main(int argc, char **argv)

setenv("_OVM_IS_BUNDLE", "1", 1); // for .zip resources

// Communicate C build configuration to Python env.
// TODO: Could rename back to _OVM_HAVE_READLINE.
#ifdef HAVE_READLINE
setenv("_HAVE_READLINE", "1", 1);
#else
setenv("_HAVE_READLINE", "", 1);
#endif

// NOTE: I think we need sys.path to find runpy in the first place. But
// then runpy mutates sys.path again.
if (run_self) {
Expand Down
26 changes: 12 additions & 14 deletions bin/oil.py
Expand Up @@ -80,12 +80,10 @@ def _tlog(msg):
value_e = runtime_asdl.value_e
builtin_e = runtime_asdl.builtin_e

# Set in Modules/main.c.
HAVE_READLINE = posix.environ.get('_HAVE_READLINE') != ''
if HAVE_READLINE:
import readline
else:
readline = None
try:
import line_input
except ImportError:
line_input = None

log = util.log

Expand Down Expand Up @@ -146,10 +144,10 @@ def _InitDefaultCompletions(ex, complete_builtin, comp_lookup):


def _MaybeWriteHistoryFile(history_filename):
if not readline:
if not line_input:
return
try:
readline.write_history_file(history_filename)
line_input.write_history_file(history_filename)
except IOError:
pass

Expand Down Expand Up @@ -283,7 +281,7 @@ def ShellMain(lang, argv0, argv, login_shell):
funcs = {}

fd_state = process.FdState()
exec_opts = state.ExecOpts(mem, readline)
exec_opts = state.ExecOpts(mem, line_input)
builtin.SetExecOpts(exec_opts, opts.opt_changes)
aliases = {} # feedback between runtime and parser

Expand Down Expand Up @@ -367,7 +365,7 @@ def ShellMain(lang, argv0, argv, login_shell):
comp_lookup = completion.Lookup()

builtins = { # Lookup
builtin_e.HISTORY: builtin.History(readline),
builtin_e.HISTORY: builtin.History(line_input),

builtin_e.COMPOPT: builtin_comp.CompOpt(comp_state),
builtin_e.COMPADJUST: builtin_comp.CompAdjust(mem),
Expand Down Expand Up @@ -413,8 +411,8 @@ def ShellMain(lang, argv0, argv, login_shell):
exec_deps.prompt_ev = prompt_ev
word_ev.prompt_ev = prompt_ev # HACK for circular deps

# History evaluation is a no-op if readline is None.
hist_ev = reader.HistoryEvaluator(readline, hist_ctx, debug_f)
# History evaluation is a no-op if line_input is None.
hist_ev = reader.HistoryEvaluator(line_input, hist_ctx, debug_f)

# Calculate ~/.config/oil/oshrc or oilrc
# Use ~/.config/oil to avoid cluttering the user's home directory. Some
Expand Down Expand Up @@ -470,12 +468,12 @@ def ShellMain(lang, argv0, argv, login_shell):
if exec_opts.interactive:
# NOTE: We're using a different evaluator here. The completion system can
# also run functions... it gets the Executor through Executor._Complete.
if readline:
if line_input:
ev = word_eval.CompletionWordEvaluator(mem, exec_opts, exec_deps, arena)
progress_f = ui.StatusLine()
root_comp = completion.RootCompleter(ev, mem, comp_lookup, comp_state,
comp_ctx, progress_f, debug_f)
_InitReadline(readline, history_filename, root_comp, debug_f)
_InitReadline(line_input, history_filename, root_comp, debug_f)
_InitDefaultCompletions(ex, complete_builtin, comp_lookup)

# NOTE: Call this AFTER _InitDefaultCompletions.
Expand Down
8 changes: 4 additions & 4 deletions build/actions.sh
Expand Up @@ -128,18 +128,18 @@ make-dotd() {

extdecls() {
for mod in "$@"; do
test "$mod" = readline && echo "#ifdef HAVE_READLINE"
test "$mod" = line_input && echo "#ifdef HAVE_READLINE"
echo "extern void init$mod(void);"
test "$mod" = readline && echo "#endif"
test "$mod" = line_input && echo "#endif"
done
return 0 # because test can fail
}

initbits() {
for mod in "$@"; do
test "$mod" = readline && echo "#ifdef HAVE_READLINE"
test "$mod" = line_input && echo "#ifdef HAVE_READLINE"
echo " {\"$mod\", init$mod},"
test "$mod" = readline && echo "#endif"
test "$mod" = line_input && echo "#endif"
done
return 0 # because test can fail
}
Expand Down
2 changes: 1 addition & 1 deletion build/c_module_srcs.py
Expand Up @@ -26,7 +26,7 @@ def main(argv):

# Hard-coded special cases for now.

if mod_name in ('libc', 'fastlex'): # Our own modules
if mod_name in ('libc', 'fastlex', 'line_input'): # Our own modules
# Relative to Python-2.7.13 dir
print('../native/%s.c' % mod_name)

Expand Down
2 changes: 1 addition & 1 deletion build/compile.sh
Expand Up @@ -202,7 +202,7 @@ build() {
readline_flags+="-l readline -D HAVE_READLINE"
else
# don't fail
c_module_src_list=$(grep -v '/readline.c' $abs_c_module_srcs || true)
c_module_src_list=$(grep -E -v '/readline.c|/line_input.c' $abs_c_module_srcs || true)
fi

# $PREFIX comes from ./configure and defaults to /usr/local.
Expand Down
1 change: 1 addition & 0 deletions build/cpython_defs.py
Expand Up @@ -222,6 +222,7 @@ def out(msg, *args):
# My Own
'libc.c',
'fastlex.c',
'line_input.c',

'import.c',
'marshal.c', # additional filters below
Expand Down
6 changes: 6 additions & 0 deletions build/dev.sh
Expand Up @@ -148,6 +148,11 @@ fastlex() {
native/fastlex_test.py
}

line-input() {
py-ext line_input build/setup_line_input.py
native/line_input_test.py "$@"
}

clean() {
rm -f --verbose libc.so fastlex.so
rm -r -f --verbose _devbuild/py-ext
Expand Down Expand Up @@ -175,6 +180,7 @@ minimal() {
asdl/run.sh gen-typed-arith-asdl

pylibc
line-input
}

# Prerequisites: build/codegen.sh {download,install}-re2c
Expand Down
13 changes: 13 additions & 0 deletions build/setup_line_input.py
@@ -0,0 +1,13 @@
#!/usr/bin/env python
from distutils.core import setup, Extension

module = Extension('line_input',
sources = ['native/line_input.c'],
undef_macros = ['NDEBUG'],
libraries = ['readline']
)

setup(name = 'line_input',
version = '1.0',
description = 'Our readline/libedit binding',
ext_modules = [module])
4 changes: 2 additions & 2 deletions native/fastlex_test.py
Expand Up @@ -5,10 +5,10 @@
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
from __future__ import print_function
"""
libc_test.py: Tests for libc.py
fastlex_test.py: Tests for fastlex
"""
from __future__ import print_function

import unittest

Expand Down

0 comments on commit 391297e

Please sign in to comment.