summaryrefslogtreecommitdiff
path: root/python/src/com/jetbrains/python/console/PydevConsoleCommunication.java
blob: ce3a00a34465571c6358ff8e1044342e82435f17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.jetbrains.python.console;

import com.intellij.application.options.RegistryManager;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ex.ApplicationUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.Function;
import com.intellij.util.concurrency.FutureResult;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.xdebugger.XSourcePosition;
import com.intellij.xdebugger.frame.*;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.console.actions.CommandQueueForPythonConsoleService;
import com.jetbrains.python.console.protocol.*;
import com.jetbrains.python.console.pydev.AbstractConsoleCommunication;
import com.jetbrains.python.console.pydev.InterpreterResponse;
import com.jetbrains.python.console.pydev.PydevCompletionVariant;
import com.jetbrains.python.debugger.*;
import com.jetbrains.python.debugger.containerview.PyViewNumericContainerAction;
import com.jetbrains.python.debugger.pydev.GetVariableCommand;
import com.jetbrains.python.debugger.pydev.SetUserTypeRenderersCommand;
import com.jetbrains.python.debugger.pydev.TableCommandType;
import com.jetbrains.python.debugger.pydev.dataviewer.DataViewerCommandBuilder;
import com.jetbrains.python.debugger.pydev.dataviewer.DataViewerCommandResult;
import com.jetbrains.python.debugger.settings.PyDebuggerSettings;
import com.jetbrains.python.debugger.variablesview.usertyperenderers.ConfigureTypeRenderersHyperLink;
import com.jetbrains.python.debugger.variablesview.usertyperenderers.PyUserNodeRenderer;
import com.jetbrains.python.debugger.variablesview.usertyperenderers.PyUserTypeRenderersSettings;
import com.jetbrains.python.parsing.console.PythonConsoleData;
import org.apache.thrift.TException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import static com.jetbrains.python.console.PydevConsoleCommunicationUtil.*;
import static com.jetbrains.python.debugger.pydev.dataviewer.DataViewerCommandResult.ResultType.UNHANDLED_ERROR;
import static com.jetbrains.python.debugger.variablesview.usertyperenderers.ConfigureTypeRenderersActionKt.getTypeRenderer;
import static com.jetbrains.python.debugger.variablesview.usertyperenderers.ConfigureTypeRenderersActionKt.loadTypeRendererChildren;

/**
 * Communication with Python console backend using Thrift services.
 *
 * @author Fabio
 */
public abstract class PydevConsoleCommunication extends AbstractConsoleCommunication implements PyFrameAccessor {
  private static final Logger LOG = Logger.getInstance(PydevConsoleCommunication.class);

  protected volatile boolean keyboardInterruption;
  /**
   * Input that should be sent to the server (waiting for raw_input)
   */
  protected volatile String inputReceived;
  /**
   * Response that should be sent back to the shell.
   */
  @Nullable protected volatile InterpreterResponse nextResponse;

  private boolean myExecuting;
  private PythonDebugConsoleCommunication myDebugCommunication;
  private boolean myNeedsMore = false;
  /**
   * UI sends a lot of repeated requests for the same expression
   * Store result of the previous request to avoid sending the same command several times
   */
  @Nullable private Pair<String, String> myPrevNameToDescription = null;

  private int myFullValueSeq = 0;
  private final Map<Integer, List<PyFrameAccessor.PyAsyncValue<String>>> myCallbackHashMap = new ConcurrentHashMap<>();

  @Nullable private PythonConsoleView myConsoleView;
  private final List<PyFrameListener> myFrameListeners = ContainerUtil.createLockFreeCopyOnWriteList();

  @Nullable private XCompositeNode myCurrentRootNode;

  /**
   * Initializes the bidirectional RPC communication.
   */
  public PydevConsoleCommunication(Project project) {
    super(project);
  }

  /**
   * Returns thread safe, Python Console process-aware and disposable
   * {@link PythonConsoleBackendService.Iface}. Requests to the returned
   * {@link PythonConsoleBackendService.Iface} will be processed sequentially.
   * If Python Console process is detected to be finished the current request
   * will be interrupted and {@link PyConsoleProcessFinishedException} is
   * thrown.
   *
   * @return thread safe and related Python Console process-aware
   * {@link PythonConsoleBackendService.Iface}
   * @throws CommunicationClosedException if transport is closed
   */
  @NotNull
  protected abstract PythonConsoleBackendServiceDisposable getPythonConsoleBackendClient();

  /**
   * Sends <i>handshake</i> message to Python Console backend. Returns
   * {@code true} if Python Console backend replies with <i>PyCharm</i> string.
   * Returns {@code false} if Python Console backend replies with unexpected
   * message or Python Console process is finished or Python Console is closed.
   *
   * @return whether <i>handshake</i> with Python Console backend succeeded
   * @throws RuntimeException if transport (protocol) error occurs
   */
  public boolean handshake() {
    if (!isCommunicationClosed()) {
      try {
        return "PyCharm".equals(getPythonConsoleBackendClient().handshake());
      }
      catch (CommunicationClosedException | PyConsoleProcessFinishedException e) {
        return false;
      }
      catch (TException e) {
        throw new RuntimeException(e);
      }
    }
    else {
      return false;
    }
  }

  /**
   * Stops the communication with the client (passes message for it to quit).
   */
  public void close() {
    PyDebugValueExecutionService.getInstance(myProject).sessionStopped(this);
    notifySessionStopped();
    myCallbackHashMap.clear();

    new Task.Backgroundable(myProject, PyBundle.message("console.close.console.communication"), false) {
      @Override
      public void run(@NotNull ProgressIndicator indicator) {
        try {
          closeCommunication().get();
        }
        catch (InterruptedException e) {
          Thread.currentThread().interrupt();
        }
        catch (ExecutionException e) {
          // it could help us diagnose some intricate cases
          LOG.debug(e);
        }
      }
    }.queue();
  }

  /**
   * Stops the communication with the client (passes message for it to quit).
   *
   * @return {@link Future} that allows to wait for Python Console transport
   * thread(s) to finish its execution
   */
  @NotNull
  public Future<?> closeAsync() {
    PyDebugValueExecutionService.getInstance(myProject).sessionStopped(this);
    myCallbackHashMap.clear();

    return closeCommunication();
  }

  /**
   * Closes the communication with Python Console backend gracefully. Returns
   * {@link Future} that allows to wait for communication resources
   * (corresponding {@link java.util.concurrent.ExecutorService} and threads)
   * to be finished.
   * <p>
   * The method is not expected to throw any exception as well as the returned
   * {@link Future}.
   *
   * @return {@link Future}
   */
  @NotNull
  protected abstract Future<?> closeCommunication();

  public abstract boolean isCommunicationClosed();

  /*
   * Variables that control when we're expecting to give some input to the server or when we're
   * adding some line to be executed
   */

  @Override
  public @Nullable Project getProject() {
    return myProject;
  }

  /**
   * Helper to keep on busy loop.
   */
  private final Object lock = new Object();

  private void execNotifyAboutMagic(List<String> commands, boolean isAutoMagic) {
    if (getConsoleFile() != null) {
      PythonConsoleData consoleData = PyConsoleUtil.getOrCreateIPythonData(getConsoleFile());
      consoleData.setIPythonAutomagic(isAutoMagic);
      consoleData.setIPythonMagicCommands(commands);
    }
  }

  private boolean execIPythonEditor(String path) {
    final VirtualFile file = StringUtil.isEmpty(path) ? null : LocalFileSystem.getInstance().findFileByPath(path);
    if (file != null) {
      ApplicationManager.getApplication().invokeLater(() -> FileEditorManager.getInstance(myProject).openFile(file, true));

      return true;
    }

    return false;
  }

  private void execNotifyFinished(boolean more) {
    myNeedsMore = more;
    setExecuting(false);
    notifyCommandExecuted(more);
  }

  private void setExecuting(boolean executing) {
    myExecuting = executing;
  }

  private Object execRequestInput() throws KeyboardInterruptException {
    waitingForInput = true;
    inputReceived = null;
    keyboardInterruption = false;
    boolean needInput = true;

    //let the busy loop from execInterpreter free and enter a busy loop
    //in this function until execInterpreter gives us an input
    nextResponse = new InterpreterResponse(false, needInput);

    notifyInputRequested();

    //busy loop until we have an input
    while (inputReceived == null) {
      if (keyboardInterruption) {
        waitingForInput = false;

        throw new KeyboardInterruptException();
      }
      synchronized (lock) {
        try {
          lock.wait(10);
        }
        catch (InterruptedException e) {
          //pass
        }
      }
    }
    return inputReceived;
  }

  /**
   * Executes the needed command
   *
   * @param command
   * @return a Pair with (null, more) or (error, false)
   */
  protected Pair<String, Boolean> exec(final ConsoleCodeFragment command) throws PythonUnhandledException {
    setExecuting(true);

    boolean more;
    try {
      if (command.isSingleLine()) {
        more = getPythonConsoleBackendClient().execLine(command.getText());
      }
      else {
        more = getPythonConsoleBackendClient().execMultipleLines(command.getText());
      }
    }
    catch (PythonUnhandledException e) {
      setExecuting(false);
      throw e;
    }
    catch (TException e) {
      setExecuting(false);
      throw new RuntimeException(e);
    }

    if (more) {
      setExecuting(false);
    }

    return Pair.create(null, more);
  }

  private static String createRuntimeMessage(String taskName) {
    return PyBundle.message("console.getting.from.runtime", taskName);
  }

  /**
   * @return completions from the client
   */
  @Override
  @NotNull
  public List<PydevCompletionVariant> getCompletions(String text, String actTok) throws Exception {
    if (waitingForInput || isExecuting()) {
      return Collections.emptyList();
    }
    ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
    indicator.setText(createRuntimeMessage(PyBundle.message("console.getting.completion")));
    return ApplicationUtil.runWithCheckCanceled(
      () -> {
        return doGetCompletions(text, actTok);
      },
      indicator);
  }

  private List<PydevCompletionVariant> doGetCompletions(String text, String actTok) throws Exception {
    try {
      if (myDebugCommunication != null && myDebugCommunication.isSuspended()) {
        return myDebugCommunication.getCompletions(text, actTok);
      }
      else {
        List<CompletionOption> fromServer = getPythonConsoleBackendClient().getCompletions(text, actTok);
        return ContainerUtil.map(fromServer, option -> toPydevCompletionVariant(option));
      }
    }
    catch (PythonUnhandledException e) {
      LOG.warn("Completion error in Python Console: " + e.traceback);
      return Collections.emptyList();
    }
  }

  @Override
  public String execTableCommand(String command, TableCommandType commandType) throws PyDebuggerException {
    if (!isCommunicationClosed()) {
      return executeBackgroundTask(
        () -> {
          try {
            return getPythonConsoleBackendClient().execTableCommand(command, commandType.name());
          }
          catch (PythonTableException e) {
            throw new PyDebuggerException(e.message);
          }
        },
        true,
        createRuntimeMessage(PyBundle.message("console.getting.table.data")),
        PyBundle.message("console.table.failed.to.load")
      );
    }
    else {
      return null;
    }
  }

  @TestOnly
  public List<PydevCompletionVariant> gerCompletionVariants(String text, String actTok) throws Exception {
    return doGetCompletions(text, actTok);
  }

  @NotNull
  private static PydevCompletionVariant toPydevCompletionVariant(@NotNull CompletionOption option) {
    String args = String.join(" ", option.arguments);
    return new PydevCompletionVariant(option.name, option.documentation, args, option.type.getValue());
  }

  private void executeBackgroundTaskSuppressException(Callable<?> task,
                                                      @NlsContexts.ProgressTitle String userVisibleMessage,
                                                      String errorLogMessage) {
    try {
      executeBackgroundTask(task, false, userVisibleMessage, errorLogMessage);
    }
    catch (Exception e) {
      LOG.error(e);
    }
  }

  @Nullable
  private <T> T executeBackgroundTask(Callable<T> task,
                                      boolean waitForResult,
                                      @NlsContexts.ProgressTitle String userVisibleMessage,
                                      String errorLogMessage)
    throws PyDebuggerException {
    final FutureResult<T> future = new FutureResult<>();
    new Task.Backgroundable(myProject, userVisibleMessage, false) {
      @Override
      public void run(@NotNull ProgressIndicator indicator) {
        try {
          T result = task.call();
          future.set(result);
        }
        catch (PythonUnhandledException e) {
          LOG.error(errorLogMessage + e.traceback);
          future.set(null);
        }
        catch (Exception e) {
          future.setException(e);
        }
      }
    }.queue();
    if (waitForResult) {
      try {
        return future.get();
      }
      catch (InterruptedException | ExecutionException e) {
        throw new PyDebuggerException(errorLogMessage + e.getMessage(), e);
      }
    }
    return null;
  }

  /**
   * @return the description of the given attribute in the shell
   */
  @Override
  public String getDescription(String text) throws Exception {
    if (myDebugCommunication != null && myDebugCommunication.isSuspended()) {
      return myDebugCommunication.getDescription(text);
    }
    if (waitingForInput) {
      return "Unable to get description: waiting for input.";
    }
    if (myPrevNameToDescription != null) {
      if (myPrevNameToDescription.first.equals(text)) return myPrevNameToDescription.second;
    }
    else {
      // add temporary value to avoid repeated requests for the same expression
      myPrevNameToDescription = Pair.create(text, "");
    }
    if (ApplicationManager.getApplication().isDispatchThread() && !ApplicationManager.getApplication().isUnitTestMode()) {
      throw new PyDebuggerException("Documentation in Python Console shouldn't be called from Dispatch Thread!");
    }

    return executeBackgroundTask(
      () ->
      {
        final String resultDescription = getPythonConsoleBackendClient().getDescription(text);
        myPrevNameToDescription = Pair.create(text, resultDescription);
        return resultDescription;
      },
      true,
      createRuntimeMessage(PyBundle.message("console.getting.documentation")),
      "Error when Getting Description in Python Console: ");
  }

  /**
   * Executes a given line in the interpreter.
   *
   * @param command the command to be executed in the client
   */
  @Override
  public void execInterpreter(final ConsoleCodeFragment command, final Function<InterpreterResponse, Object> onResponseReceived) {
    if (myDebugCommunication != null && myDebugCommunication.isSuspended()) {
      myDebugCommunication.execInterpreter(command, onResponseReceived);
      return; //TODO: handle text input and other cases
    }
    nextResponse = null;
    if (waitingForInput && myConsoleView != null && myConsoleView.isInitialized()) {
      inputReceived = command.getText();
      waitingForInput = false;
      //the thread that we started in the last exec is still alive if we were waiting for an input.
    }
    else {
      //create a thread that'll keep locked until an answer is received from the server.
      new Task.Backgroundable(myProject, PyBundle.message("console.waiting.execution.result"), false) {
        @Override
        public void run(@NotNull ProgressIndicator indicator) {
          try {
            if (indicator.isCanceled()) return;
            Pair<String, Boolean> executed = exec(command);
            nextResponse = new InterpreterResponse(executed.second, false);
          }
          catch (ProcessCanceledException e) {
            //ignore
          }
          catch (PythonUnhandledException e) {
            LOG.error("Error in execInterpreter():" + e.traceback);
            nextResponse = new InterpreterResponse(false, false);
            notifyCommandExecuted(true);
          }
          catch (Exception e) {
            nextResponse = new InterpreterResponse(false, false);
            notifyCommandExecuted(true);
          }
          finally {
            InterpreterResponse response = nextResponse;
            if (response != null && response.more) {
              myNeedsMore = true;
            }
            onResponseReceived.fun(response);
          }
        }
      }.queue();
    }
  }

  @Override
  public void interrupt() {
    if (waitingForInput) {
      // we do not want to forcibly `interrupt()` the `requestInput()` on the
      // Python side otherwise the message queue to the IDE will be broken
      keyboardInterruption = true;
      return;
    }
    executeBackgroundTaskSuppressException(
      () ->
      {
        getPythonConsoleBackendClient().interrupt();
        return null;
      },
      PyBundle.message("console.interrupting.execution"),
      "");
  }

  @Override
  public boolean isExecuting() {
    return myExecuting;
  }

  @Override
  public boolean needsMore() {
    return myNeedsMore;
  }

  @Override
  public PyDebugValue evaluate(String expression, boolean execute, boolean doTrunc) throws PyDebuggerException {
    if (!isCommunicationClosed()) {
      return executeBackgroundTask(
        () -> {
          List<DebugValue> debugValues = getPythonConsoleBackendClient().evaluate(expression, doTrunc);
          return createPyDebugValue(debugValues.iterator().next(), this);
        },
        true,
        PyBundle.message("console.evaluating.expression.in.console"),
        "Error in evaluate():"
      );
    }
    else {
      return null;
    }
  }

  @Nullable
  @Override
  public XValueChildrenList loadFrame(@Nullable XStackFrame contextFrame) throws PyDebuggerException {
    if (!isCommunicationClosed()) {
      return executeBackgroundTask(
        () -> {
          List<DebugValue> frame = getPythonConsoleBackendClient().getFrame();
          return parseVars(frame, null, this);
        },
        true,
        createRuntimeMessage(PyBundle.message("console.getting.frame.variables")),
        "Error in loadFrame():"
      );
    }
    else {
      return new XValueChildrenList();
    }
  }

  public synchronized int getNextFullValueSeq() {
    myFullValueSeq++;
    return myFullValueSeq;
  }

  @Override
  public void loadAsyncVariablesValues(@Nullable XStackFrame frame, @NotNull List<PyAsyncValue<String>> pyAsyncValues) {
    PyDebugValueExecutionService.getInstance(myProject).submitTask(this, () -> {
      try {
        List<String> evaluationExpressions = new ArrayList<>();
        for (PyAsyncValue<String> asyncValue : pyAsyncValues) {
          evaluationExpressions.add(GetVariableCommand.composeName(asyncValue.getDebugValue()));
        }
        final int seq = getNextFullValueSeq();
        myCallbackHashMap.put(seq, pyAsyncValues);

        getPythonConsoleBackendClient().loadFullValue(seq, evaluationExpressions);

        // previously `loadFullValue()` might return `List<PyDebugValue>` but this is no longer true
      }
      catch (CommunicationClosedException | PyConsoleProcessFinishedException | TException e) {
        for (PyAsyncValue<String> asyncValue : pyAsyncValues) {
          PyDebugValue value = asyncValue.getDebugValue();
          for (XValueNode node : value.getValueNodes()) {
            if (node != null && !node.isObsolete()) {
              if (e.getMessage().startsWith("Timeout") || e.getMessage().startsWith("Console already exited")) {
                value.updateNodeValueAfterLoading(node, " ", "", PyBundle.message("debugger.variables.view.loading.timed.out"));
              }
              else {
                LOG.error(e);
              }
            }
          }
        }
      }
    });
  }

  @Override
  public @Nullable XValueChildrenList loadVariableDefaultView(PyDebugValue variable) throws PyDebuggerException {
    if (!isCommunicationClosed()) {
      return executeBackgroundTask(
        () -> {
          final String name = variable.getOffset() == 0 ? GetVariableCommand.composeName(variable)
                                                        : variable.getOffset() + "\t" + GetVariableCommand.composeName(variable);
          List<DebugValue> ret = getPythonConsoleBackendClient().getVariable(name);
          return parseVars(ret, variable, this);
        },
        true,
        createRuntimeMessage(PyBundle.message("console.getting.variable.value")),
        "Error in loadVariable():"
      );
    }
    else {
      return new XValueChildrenList();
    }
  }

  @Override
  public @Nullable XValueChildrenList loadVariable(PyDebugValue var) throws PyDebuggerException {
    PyUserNodeRenderer typeRenderer = getTypeRenderer(var);
    if (typeRenderer != null) {
      return executeBackgroundTask(
        () -> {
          return loadTypeRendererChildren(this, var, typeRenderer);
        },
        true,
        createRuntimeMessage(PyBundle.message("console.getting.variable.value")),
        "Error in loadVariable():"
      );
    }
    else {
      return loadVariableDefaultView(var);
    }
  }

  @Override
  public void setCurrentRootNode(@NotNull XCompositeNode node) {
    myCurrentRootNode = node;
  }

  @Override
  public boolean isSimplifiedView() {
    return PyDebuggerSettings.getInstance().isSimplifiedView();
  }

  @Override
  @Nullable
  public XCompositeNode getCurrentRootNode() {
    return myCurrentRootNode;
  }

  @Override
  public void changeVariable(PyDebugValue variable, String value) {
    if (!isCommunicationClosed()) {
      executeBackgroundTaskSuppressException(
        () -> {
          // NOTE: The actual change is being scheduled in the exec_queue in main thread
          // This method is async now
          getPythonConsoleBackendClient().changeVariable(variable.getEvaluationExpression(), value);
          return null;
        },
        PyBundle.message("console.changing.variable"),
        "Error in changeVariable():"
      );
    }
  }

  @Nullable
  @Override
  public PyReferrersLoader getReferrersLoader() {
    return null;
  }

  @Override
  public ArrayChunk getArrayItems(PyDebugValue var, int rowOffset, int colOffset, int rows, int cols, String format)
    throws PyDebuggerException {
    if (!isCommunicationClosed()) {
      return executeBackgroundTask(
        () -> {
          GetArrayResponse ret = getPythonConsoleBackendClient().getArray(var.getName(), rowOffset, colOffset, rows, cols, format);
          return createArrayChunk(ret, this);
        },
        true,
        createRuntimeMessage(PyBundle.message("console.getting.array")),
        "Error in getArrayItems():"
      );
    }
    else {
      return null;
    }
  }

  @Override
  public DataViewerCommandResult executeDataViewerCommand(DataViewerCommandBuilder builder) throws PyDebuggerException {
    if (!isCommunicationClosed()) {
      return executeBackgroundTask(
        () -> {
          try {
            getPythonConsoleBackendClient().execDataViewerAction(
              builder.getVar().getName(),
              builder.getAction().name(),
              builder.getArgs() == null ? "" : String.join("\t", builder.getArgs())
            );

            return DataViewerCommandResult.makeSuccessResult("Export successful");
          }
          catch (PythonUnhandledException e) {
            return DataViewerCommandResult.errorFromExportTraceback(e.getTraceback());
          }
        },
        true,
        PyBundle.message("console.executing.dataviewer.command"),
        "Error in DataViewer command:"
      );
    }
    return DataViewerCommandResult.makeErrorResult(UNHANDLED_ERROR, "Console communication is closed");
  }

  @Nullable
  @Override
  public XSourcePosition getSourcePositionForName(String name, String parentType) {
    return null;
  }

  @Nullable
  @Override
  public XSourcePosition getSourcePositionForType(String type) {
    return null;
  }

  /**
   * Request that pydevconsole connect (with pydevd) to the specified port
   *
   * @param localPort port for pydevd to connect to.
   * @param dbgOpts   additional debugger options (that are normally passed via command line) to apply
   * @param extraEnvs
   * @throws Exception if connection fails
   */
  public void connectToDebugger(int localPort,
                                @Nullable String debuggerHost,
                                @NotNull Map<String, Boolean> dbgOpts,
                                @NotNull Map<String, String> extraEnvs)
    throws Exception {
    if (waitingForInput) {
      throw new Exception("Can't connect debugger now, waiting for input");
    }
    executeBackgroundTask(
      () -> {
        // though `connectToDebugger` returns "connect complete" string, let us just ignore it
        getPythonConsoleBackendClient().connectToDebugger(localPort, debuggerHost, dbgOpts, extraEnvs);
        return null;
      },
      true,
      PyBundle.message("console.connecting.to.debugger"),
      "Error in connectToDebugger():"
    );
  }

  @Override
  public void notifyCommandExecuted(boolean more) {
    super.notifyCommandExecuted(more);
    for (PyFrameListener listener : myFrameListeners) {
      listener.frameChanged();
    }
  }

  private void notifySessionStopped() {
    for (PyFrameListener listener : myFrameListeners) {
      listener.sessionStopped();
    }
  }

  @Override
  public void setUserTypeRenderersSettings() {
    if (!isCommunicationClosed()) {
      try {
        executeBackgroundTask(
          () -> {
            PyUserTypeRenderersSettings settings = PyUserTypeRenderersSettings.getInstance();
            if (settings == null) {
              return false;
            }
            List<PyUserTypeRenderer> renderers = settings.getApplicableRenderers();
            if (renderers.isEmpty()) {
              return false;
            }
            final String renderersMessage = SetUserTypeRenderersCommand.createMessage(renderers);
            return getPythonConsoleBackendClient().setUserTypeRenderers(renderersMessage);
          },
          true,
          createRuntimeMessage(PyBundle.message("console.setting.user.type.renderers")),
          "Error in setUserTypeRenderersSettings():"
        );
      }
      catch (PyDebuggerException e) {
        LOG.warn("Failed to send Type Renderers", e);
      }
    }
  }

  @Override
  public @Nullable XDebuggerTreeNodeHyperlink getUserTypeRenderersLink(@NotNull String typeRendererId) {
    return new ConfigureTypeRenderersHyperLink(typeRendererId, getProject(), null);
  }

  public void setDebugCommunication(PythonDebugConsoleCommunication debugCommunication) {
    myDebugCommunication = debugCommunication;
  }

  public PythonDebugConsoleCommunication getDebugCommunication() {
    return myDebugCommunication;
  }

  public void setConsoleView(@Nullable PythonConsoleView consoleView) {
    myConsoleView = consoleView;
  }

  @Override
  public void showNumericContainer(@NotNull PyDebugValue value) {
    PyViewNumericContainerAction.showNumericViewer(myProject, value);
  }

  @Override
  public void addFrameListener(@NotNull PyFrameListener listener) {
    myFrameListeners.add(listener);
  }

  @NotNull
  protected final PythonConsoleFrontendService.Iface createPythonConsoleFrontendHandler() {
    return new PythonConsoleFrontendHandler();
  }

  private class PythonConsoleFrontendHandler implements PythonConsoleFrontendService.Iface {

    @Override
    public void notifyFinished(boolean needsMoreInput, boolean exceptionOccurred) {
      if (RegistryManager.getInstance().is("python.console.CommandQueue")) {
        // notify the CommandQueue service that the command has been completed without exceptions
        // and it must be removed from the queue
        // or clear queue if exception occurred
        ApplicationManager.getApplication().getService(CommandQueueForPythonConsoleService.class)
          .removeCommand(PydevConsoleCommunication.this, exceptionOccurred);
      }
      execNotifyFinished(needsMoreInput);
    }

    @Override
    public String requestInput(String path) throws KeyboardInterruptException {
      return (String)execRequestInput();
    }

    @Override
    public void notifyAboutMagic(List<String> commands, boolean isAutoMagic) {
      execNotifyAboutMagic(commands, isAutoMagic);
    }

    @Override
    public void showConsole() {
      if (myConsoleView != null) {
        myConsoleView.setConsoleEnabled(true);
      }
    }

    @Override
    public void returnFullValue(int requestSeq, List<DebugValue> response) {
      final List<PyAsyncValue<String>> values = myCallbackHashMap.remove(requestSeq);
      try {
        List<PyDebugValue> debugValues = ContainerUtil.map(response, value -> createPyDebugValue(value, PydevConsoleCommunication.this));
        for (int i = 0; i < debugValues.size(); ++i) {
          PyDebugValue resultValue = debugValues.get(i);
          values.get(i).getCallback().ok(resultValue.getValue());
        }
      }
      catch (Exception e) {
        if (values != null) {
          for (PyFrameAccessor.PyAsyncValue vars : values) {
            vars.getCallback().error(new PyDebuggerException(response.toString()));
          }
        }
      }
    }

    @Override
    public boolean IPythonEditor(String path, String line) {
      return execIPythonEditor(path);
    }

    @Override
    public void sendRichOutput(Map<String, String> data) throws TException {
      if (myConsoleView == null) return;
      if (data.isEmpty()) return;
      PyConsoleOutputCustomizer.Companion.getInstance().showRichOutput(myConsoleView, data);
    }
  }

  protected static class CommunicationClosedException extends RuntimeException {
  }
}