Skip to content

Commit 37b92bd

Browse files
committed
honour readline setting for auto_history
1 parent b04647d commit 37b92bd

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/ConsoleHandler.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@
4545
import java.nio.charset.StandardCharsets;
4646
import java.util.List;
4747
import java.util.function.BiConsumer;
48+
import java.util.function.BooleanSupplier;
4849
import java.util.function.Consumer;
4950
import java.util.function.Function;
50-
import java.util.function.Supplier;
51+
import java.util.function.IntConsumer;
52+
import java.util.function.IntFunction;
53+
import java.util.function.IntSupplier;
5154

5255
import org.graalvm.polyglot.Context;
5356

@@ -73,7 +76,8 @@ public void setContext(@SuppressWarnings("unused") Context context) {
7376
}
7477

7578
@SuppressWarnings("unused")
76-
public void setHistory(Supplier<Integer> getSize, Consumer<String> addItem, Function<Integer, String> getItem, BiConsumer<Integer, String> setItem, Consumer<Integer> removeItem, Runnable clear) {
79+
public void setHistory(BooleanSupplier shouldRecord, IntSupplier getSize, Consumer<String> addItem, IntFunction<String> getItem, BiConsumer<Integer, String> setItem, IntConsumer removeItem,
80+
Runnable clear) {
7781
// ignore by default
7882
}
7983

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/GraalPythonMain.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,15 +593,21 @@ public int readEvalPrint(Context context, ConsoleHandler consoleHandler) {
593593
}
594594

595595
private void setupReadline(Context context, ConsoleHandler consoleHandler) {
596-
final Value readline = context.eval(Source.newBuilder(getLanguageId(), "import readline; readline", "setup-interactive").interactive(true).buildLiteral());
596+
// First run nothing to trigger the setup of interactive mode (site import and so on)
597+
context.eval(Source.newBuilder(getLanguageId(), "None", "setup-interactive").interactive(true).buildLiteral());
598+
// Then we can get the readline module and see if any completers were registered and use its
599+
// history feature
600+
final Value readline = context.eval(Source.newBuilder(getLanguageId(), "import readline; readline", "setup-interactive").buildLiteral());
597601
final Value completer = readline.getMember("get_completer").execute();
602+
final Value shouldRecord = readline.getMember("get_auto_history");
598603
final Value addHistory = readline.getMember("add_history");
599604
final Value getHistoryItem = readline.getMember("get_history_item");
600605
final Value setHistoryItem = readline.getMember("replace_history_item");
601606
final Value deleteHistoryItem = readline.getMember("remove_history_item");
602607
final Value clearHistory = readline.getMember("clear_history");
603608
final Value getHistorySize = readline.getMember("get_current_history_length");
604609
consoleHandler.setHistory(
610+
() -> shouldRecord.execute().asBoolean(),
605611
() -> getHistorySize.execute().asInt(),
606612
(item) -> addHistory.execute(item),
607613
(pos) -> getHistoryItem.execute(pos).asString(),

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/JLineConsoleHandler.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@
4747
import java.util.List;
4848
import java.util.ListIterator;
4949
import java.util.function.BiConsumer;
50+
import java.util.function.BooleanSupplier;
5051
import java.util.function.Consumer;
5152
import java.util.function.Function;
52-
import java.util.function.Supplier;
53+
import java.util.function.IntConsumer;
54+
import java.util.function.IntFunction;
55+
import java.util.function.IntSupplier;
5356

5457
import org.graalvm.polyglot.Context;
5558

@@ -95,12 +98,13 @@ public int complete(String buffer, int cursor, List<CharSequence> candidates) {
9598
}
9699

97100
@Override
98-
public void setHistory(Supplier<Integer> getSize, Consumer<String> addItem, Function<Integer, String> getItem, BiConsumer<Integer, String> setItem, Consumer<Integer> removeItem, Runnable clear) {
101+
public void setHistory(BooleanSupplier shouldRecord, IntSupplier getSize, Consumer<String> addItem, IntFunction<String> getItem, BiConsumer<Integer, String> setItem, IntConsumer removeItem,
102+
Runnable clear) {
99103
console.setHistory(new History() {
100-
private int pos = getSize.get();
104+
private int pos = getSize.getAsInt();
101105

102106
public int size() {
103-
return getSize.get();
107+
return getSize.getAsInt();
104108
}
105109

106110
public void set(int arg0, CharSequence arg1) {
@@ -216,8 +220,10 @@ public void clear() {
216220
}
217221

218222
public void add(CharSequence arg0) {
219-
addItem.accept(arg0.toString());
220-
pos = size();
223+
if (shouldRecord.getAsBoolean()) {
224+
addItem.accept(arg0.toString());
225+
pos = size();
226+
}
221227
}
222228
});
223229
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ReadlineModuleBuiltins.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
8383
private static final class LocalData implements TruffleObject {
8484
private final HashMap<String, String> bindings = new HashMap<>();
8585
private final List<String> history = new ArrayList<>();
86-
protected Object completer;
86+
protected Object completer = null;
87+
public boolean autoHistory = true;
8788

8889
public ForeignAccess getForeignAccess() {
8990
return null;
@@ -320,4 +321,27 @@ PNone setCompleter() {
320321
return PNone.NONE;
321322
}
322323
}
324+
325+
@Builtin(name = "get_auto_history", fixedNumOfPositionalArgs = 1, declaresExplicitSelf = true)
326+
@GenerateNodeFactory
327+
abstract static class GetAutoHistoryNode extends PythonUnaryBuiltinNode {
328+
@Specialization
329+
boolean setCompleter(PythonModule self,
330+
@Cached("create()") ReadAttributeFromObjectNode readNode) {
331+
LocalData data = (LocalData) readNode.execute(self, DATA);
332+
return data.autoHistory;
333+
}
334+
}
335+
336+
@Builtin(name = "set_auto_history", fixedNumOfPositionalArgs = 2, declaresExplicitSelf = true)
337+
@GenerateNodeFactory
338+
abstract static class SetAutoHistoryNode extends PythonBinaryBuiltinNode {
339+
@Specialization
340+
PNone setCompleter(PythonModule self, boolean enabled,
341+
@Cached("create()") ReadAttributeFromObjectNode readNode) {
342+
LocalData data = (LocalData) readNode.execute(self, DATA);
343+
data.autoHistory = enabled;
344+
return PNone.NONE;
345+
}
346+
}
323347
}

0 commit comments

Comments
 (0)