hibtc / cpymad

Cython binding to MAD-X
http://hibtc.github.io/cpymad/
Other
27 stars 18 forks source link

Can't run madx instance. TypeError #110

Closed JohnMSmith closed 2 years ago

JohnMSmith commented 2 years ago

I have latest version of cpymad on Win10. Python 3.9.12

Attempt to run mad leads to following output.

from cpymad.madx import Madx
madx = Madx()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [31], in <cell line: 2>()
      1 from cpymad.madx import Madx
----> 2 madx = Madx()

File ~\Anaconda3\lib\site-packages\cpymad\madx.py:192, in Madx.__init__(self, libmadx, command_log, stdout, history, prompt, **Popen_args)
    190 if not libmadx.is_started():
    191     with self.reader:
--> 192         libmadx.start()
    193 # init instance variables:
    194 self.history = history

File ~\Anaconda3\lib\site-packages\cpymad\stream.py:57, in AsyncReader.__exit__(self, *exc_info)
     55 output_lines = self.result.get()
     56 if output_lines:
---> 57     self.callback(b''.join(output_lines))

File ~\Anaconda3\lib\site-packages\ipykernel\iostream.py:513, in OutStream.write(self, string)
    503 """Write to current stream after encoding if necessary
    504 
    505 Returns
   (...)
    509 
    510 """
    512 if not isinstance(string, str):
--> 513     raise TypeError(
    514         f"write() argument must be str, not {type(string)}"
    515     )
    517 if self.echo is not None:
    518     try:

TypeError: write() argument must be str, not <class 'bytes'>

How can I solve this?

coldfix commented 2 years ago

Looks like jupyter stdout is set up as text stream for you. Might be a difference between windows and linux. I have not tried it on windows so far. What's your jupyter version?

JohnMSmith commented 2 years ago

What's your jupyter version?

Jupyter Notebook 6.4.8

coldfix commented 2 years ago

It seems jupyter has changed its behaviour. On linux the problem also exists (sys.stdout.write only accepts str), but only on python 3.7 and above. However on linux this doesn't even surface as TypeError because the stream is set up with a fileno that accepts bytes and writes to the terminal, i.e. sys.stdout.write(str) and os.write(sys.stdout.fileno(), bytes) do very different things.

I will release a bugfix shortly.

Thanks for reporting.

coldfix commented 2 years ago

Should be fixed in version v1.10. Can you confirm @JohnMSmith ?

JohnMSmith commented 2 years ago

Yes, it is fixed. Thanks!