# Python interactor disconnecting

**URL:** https://discourse.slicer.org/t/python-interactor-disconnecting/7166
**Category:** Development
**Tags:** python
**Created:** [June 13, 2019, 9:19pm UTC](https://discourse.slicer.org/t/python-interactor-disconnecting/7166 "2019-06-13T21:19:43Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [June 13, 2019, 9:19pm UTC](https://discourse.slicer.org/t/python-interactor-disconnecting/7166/1 "2019-06-13T21:19:43Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [June 13, 2019, 9:48pm UTC](https://discourse.slicer.org/t/python-interactor-disconnecting/7166/2 "2019-06-13T21:48:47Z")

</div>

I don’t remember ever experiencing this.

---

<div class="post-metadata">

### Author: ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)
#### Post date: [June 13, 2019, 11:22pm UTC](https://discourse.slicer.org/t/python-interactor-disconnecting/7166/3 "2019-06-13T23:22:06Z")

</div>

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

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [June 13, 2019, 11:50pm UTC](https://discourse.slicer.org/t/python-interactor-disconnecting/7166/4 "2019-06-13T23:50:37Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)
#### Post date: [June 28, 2019, 4:41pm UTC](https://discourse.slicer.org/t/python-interactor-disconnecting/7166/5 "2019-06-28T16:41:42Z")

</div>

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**

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

```

**Slicer 4.11 nightly**

```auto
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

```

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [June 30, 2019, 2:52am UTC](https://discourse.slicer.org/t/python-interactor-disconnecting/7166/6 "2019-06-30T02:52:53Z")

</div>

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

> <https://github.com/commontk/CTK/blob/a18ece7db314a6f638db747ca8d05cc6f5c41bb3/Libs/Scripting/Python/Widgets/ctkPythonConsole.cpp#L506>

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?

---

<div class="post-metadata">

### Author: ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)
#### Post date: [June 30, 2019, 8:38pm UTC](https://discourse.slicer.org/t/python-interactor-disconnecting/7166/7 "2019-06-30T20:38:24Z")

</div>

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

```auto
>>> 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:**

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

```

**Modified Slicer 4.10:**

```auto
>>> a="Ì"
>>> a
'\xc3\x8c'
>>> print(a)
Ã

```

---

<div class="post-metadata">

### Author: ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)
#### Post date: [July 1, 2019, 5:28pm UTC](https://discourse.slicer.org/t/python-interactor-disconnecting/7166/8 "2019-07-01T17:28:07Z")

</div>

@jamesobutler Thanks for testing.

> [@jamesobutler](#):
>
> ever a bug/fix issued regarding the previous cases of the interactor not processing?

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](https://github.com/commontk/CTK/pull/872)

---

<div class="post-metadata">

### Author: ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)
#### Post date: [July 1, 2019, 5:41pm UTC](https://discourse.slicer.org/t/python-interactor-disconnecting/7166/9 "2019-07-01T17:41:46Z")

</div>

Corresponding changes have been integrated in [r28353](http://viewvc.slicer.org/viewvc.cgi/Slicer4?view=revision&revision=28353)
