Python interactor disconnecting

I’ve noticed that sometimes lately the python interactor will stop processing new commands. This is something new in the past few weeks. It seems to happen when there’s a background exception. @lassoan the other day on the developer hangout it sounded like you’d seen this before too, but we didn’t have time to talk. Do you know why it’s happening?

I don’t remember ever experiencing this.

I confirm I experienced similar problem. I will report an issue with exact step.

Thanks @jcfr - I’ve had it happen a couple times. In case it helps, today I was able to track it down to an exception being thrown in an imported library. Nothing showed up on the Slicer console, but when I ran the same code in an external python process the exception showed up.

I was also able to make the python interactor stop processing events correctly in Slicer nightly. Was there ever a bug/fix issued regarding the previous cases of the interactor not processing?

Typed into interactor: a= “Ì”
Ì = windows alt code 0204

Slicer 4.10.2

Python console user input: a="?"
Python console user input: a
'\xec'
Python console user input: print("Hello")
Hello

Slicer 4.11 nightly

Python console user input: a="?"
Python console user input: a
Python console user input: print("Hello")
Python console user input: slicer.app.settingsDialog().show()
  File "<console>", line 1
    a
    ^
SyntaxError: multiple statements found while compiling a single statement

This special character problem is caused by an incorrect Latin1 encoding of the user input in this line:

Changing buffer.toLatin1().data() to buffer.toUtf8().data() fixes the issue.

This change would need to be tested with Python2 and other toLatin1 calls would need to be reviewed and potentially changed to toUtf8 in the CTK Python console.

I won’t be able to investigate this any further. @jamesobutler would you have time to look into this?

@lassoan I can confirm the change to buffer.toUtf8().data() does fix the issue for the Slicer nightly:

>>> a="Ì"
>>> a
'Ì'
>>> print(a)
Ì

The same change for Slicer 4.10 which uses Python2 unfortunately results in a different output compared to what it currently shows. So this code change doesn’t seem to be appropriate for both python 2 and 3:
Unmodified Slicer 4.10:

>>> a="Ì"
>>> a
'\xcc'
>>> print(a)
Ì

Modified Slicer 4.10:

>>> a="Ì"
>>> a
'\xc3\x8c'
>>> print(a)
Ì

@jamesobutler Thanks for testing.

I didn’t get to report an issue. That said, based on the recent updates discussed here, the following PR will be integrated to CTK and Slicer will be updated accordingly: BUG: Update ctkPythonConsole to support auto-completion including unicode characters by jcfr · Pull Request #872 · commontk/CTK · GitHub

Corresponding changes have been integrated in r28353