on master help sign in
- Read entire file and eval if language doesn't support EvalFile

- Add readline support/completion to python repls.

commita3192b90d6251fa4b6de62282a72421f103042f4 authorRob Blanckaert <basicer@basicer.com> dateWed 17 Oct 2018, 16:50 (8 years ago) parent35bc04b
3 files changed+52−1
src/language.go+11−1 src/languages/python2/main.go+8 src/languages/python3/main.go+33
src/language.go+11−1
@@ -2,6 +2,7 @@ package main
22
33import (
44 "fmt"
5+ "io/ioutil"
56 "os"
67 "path/filepath"
78 "plugin"
@@ -126,7 +127,16 @@ func (lang Langauge) REPLLikeEval(code string) {
126127}
127128
128129func (lang Langauge) EvalFile(file string, args []string) {
129− lang.ptr.(PluginEvalFile).EvalFile(file, args)
130+ pef, ok := lang.ptr.(PluginEvalFile)
131+ if ok {
132+ pef.EvalFile(file, args)
133+ } else {
134+ dat, err := ioutil.ReadFile(file)
135+ if err != nil {
136+ panic(err)
137+ }
138+ lang.Eval(string(dat))
139+ }
130140}
131141
132142func (lang Langauge) REPL() {
src/languages/python2/main.go+8
@@ -110,7 +110,15 @@ func (p Python) REPLLikeEval(code string) {
110110 C.pry_eval(ccode, C.Py_single_input)
111111}
112112
113+func (p Python) LoadModule(mod string) {
114+ cmode := C.CString(mod)
115+ defer C.free(unsafe.Pointer(cmode))
116+ C.PyImport_ImportModule(cmode)
117+}
118+
113119func (p Python) REPL() {
120+ p.LoadModule("readline")
121+
114122 fn := C.CString("<stdin>")
115123 defer C.free(unsafe.Pointer(fn))
116124 C.PyRun_InteractiveLoopFlags(C.stdin, fn, nil)
src/languages/python3/main.go+33
@@ -55,6 +55,37 @@ void pry_set_prompts(const char* ps1, const char* ps2) {
5555 Py_DECREF(po2);
5656}
5757
58+//From python3 sourcecode
59+static void
60+pymain_run_interactive_hook(void)
61+{
62+ PyObject *sys, *hook, *result;
63+ sys = PyImport_ImportModule("sys");
64+ if (sys == NULL) {
65+ goto error;
66+ }
67+
68+ hook = PyObject_GetAttrString(sys, "__interactivehook__");
69+ Py_DECREF(sys);
70+ if (hook == NULL) {
71+ PyErr_Clear();
72+ return;
73+ }
74+
75+ result = _PyObject_CallNoArg(hook);
76+ Py_DECREF(hook);
77+ if (result == NULL) {
78+ goto error;
79+ }
80+ Py_DECREF(result);
81+
82+ return;
83+
84+error:
85+ PySys_WriteStderr("Failed calling sys.__interactivehook__\n");
86+ PyErr_Print();
87+}
88+
5889*/
5990import "C"
6091
@@ -111,6 +142,8 @@ func (p Python) REPLLikeEval(code string) {
111142}
112143
113144func (p Python) REPL() {
145+ C.pymain_run_interactive_hook()
146+
114147 fn := C.CString("<stdin>")
115148 defer C.free(unsafe.Pointer(fn))
116149 C.PyRun_InteractiveLoopFlags(C.stdin, fn, nil)