Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing svg2tikz in Pyzo causes exception, works fine in the terminal #911

Closed
ysalmon opened this issue Oct 5, 2023 · 2 comments
Closed

Comments

@ysalmon
Copy link
Contributor

ysalmon commented Oct 5, 2023

Hi, I stumbled upon a failure with svg2tikz master in Pyzo : xyz2tex/svg2tikz#144

Setting up en venv with the bare minimum to have svg2tikz code from their master branch, and creating a shell in Pyzo to use that venv, causes

>>> import svg2tikz
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/tmp/v2/lib/python3.10/site-packages/svg2tikz/__init__.py", line 1, in <module>
    from .tikz_export import convert_file, convert_svg
  File "/tmp/v2/lib/python3.10/site-packages/svg2tikz/tikz_export.py", line 383, in <module>
    class TikZPathExporter(inkex.Effect, inkex.EffectExtension):
  File "/tmp/v2/lib/python3.10/site-packages/svg2tikz/tikz_export.py", line 1340, in TikZPathExporter
    def run(self, args=None, output=sys.stdout.buffer):
AttributeError: 'FileWrapper' object has no attribute 'buffer'

Opening a terminal and doing

. /tmp/v2/bin/activate
python
import svg2tikz

gives no error.

However, their latest release, which of course predates their master, does not exhibit this problem in pyzo.

I suspect a strange interaction between the two…

@bdieterm
Copy link
Collaborator

bdieterm commented Oct 6, 2023

Hi @ysalmon!
The error means that sys.stdout has not buffer attribute.
This is because Pyzo replaces sys.stdout with a yoton FileWrapper object (see pyzo/yoton/channels/channels_file.py and pyzo/pyzokernel/start.py) in order to communicate between the Python kernel and the Pyzo main application.
Implementing a buffer substitute in the yoton FileWrapper class would be possible but complicated because communication between the kernel and Pyzo is supposed to be via utf-8 strings and not random raw bytes. And the next problem would be to properly display that random bytes (that might not be mapped to utf-8 characters) in the shell in Pyzo via the QPlainTextEdit widget.

As far as I have seen, sys.stdout.buffer as a default parameter value was introduced around two weeks after the current release -- see xyz2tex/svg2tikz#126

I only had a quick look at the code. This stdout buffer seems to be intended to be used when running svg2tikz with command line arguments in a terminal and piping the result from stdout to another tool.

I guess, since you are importing svg2tikz in a shell within Pyzo, that you are not using the command line arguments with that module. In this case you could do a simple workaround. Just set the stdout buffer to None before importing the module:

sys.stdout.buffer = None
import svg2tikz

If you do not want to modify (existing) source codes, then you could also add sys.stdout.buffer = None to the "startupScript" textbox in the Pyzo shell configuration dialog.

@ldevillez
Copy link

ldevillez commented Oct 6, 2023

Fixed in svg2tikz 👍

@ysalmon ysalmon closed this as completed Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants