ipython plugin failure when running commands returning a value
The problem is when writing the command output to the stdout StringIO, see:
In [1]: text = acc.queryText()
In [1]: text.characterCount
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/usr/lib/python3/dist-packages/pyatspi/__init__.py in <module>
----> 1 text.characterCount
/usr/lib/python3/dist-packages/IPython/core/displayhook.py in __call__(self, result)
259 if result is not None and not self.quiet():
260 self.start_displayhook()
--> 261 self.write_output_prompt()
262 format_dict, md_dict = self.compute_format_data(result)
263 self.update_user_ns(result)
/usr/lib/python3/dist-packages/IPython/terminal/prompts.py in write_output_prompt(self)
91
92 if self.shell.pt_app:
---> 93 print_formatted_text(PygmentsTokens(tokens),
94 style=self.shell.pt_app.app.style, end='',
95 )
/usr/lib/python3/dist-packages/prompt_toolkit/shortcuts/utils.py in print_formatted_text(sep, end, file, flush, style, output, color_depth, style_transformation, include_default_pygments_style, *values)
135
136 # Print output.
--> 137 renderer_print_formatted_text(
138 output,
139 fragments,
/usr/lib/python3/dist-packages/prompt_toolkit/renderer.py in print_formatted_text(output, formatted_text, style, style_transformation, color_depth)
793 # Reset again.
794 output.reset_attributes()
--> 795 output.flush()
/usr/lib/python3/dist-packages/prompt_toolkit/output/vt100.py in flush(self)
682 else:
683 out = cast(IO[bytes], self.stdout)
--> 684 out.write(data.encode(self.stdout.encoding or "utf-8", "replace"))
685 else:
686 self.stdout.write(data)
TypeError: string argument expected, got 'bytes'
In [2]:
prompt_toolkit vt100 writes binary data by default, but we are using a StringIO, which is also used by other apps using ipython, so I don't really understand why this is failing now. I managed to fix this by using TextIOWrapper instead of StringIO, but that revealed another issue. prompt_toolkit is adding a special sequence to enable autowrap (\x1b[?7h) that is not handled correctly because gtk_text_buffer_insert_with_tags() fails due to the tag not being found in the table. I fixed it just by checking first if the tag is known to use gtk_text_buffer_insert otherwise. I know this is not happening to other people using the same version of ipython, so I have no idea why this is only failing for me. I'm attaching the patch I used here because I'm not even sure it's right, but if this is a problem for other people I can create an MR.