1

I'm trying to debug a module, but whenever I try to print something, python 3.10 stops with the following error:

File "/usr/lib/python3.10/codecs.py", line 378, in write
self.stream.write(data)
TypeError: write() argument must be str, not bytes

This error comes whatever I am trying to print, e.g. print("abc") or print(type(1)). It also occurs when calling breakpoint(), because breakpoint prints its messages to stdout, which does not work for some reason.

When calling print in a separate python shell, it works, but not when it is called from the module which is loaded in a script.

Any ideas what might case this behaviour?

Have tried to actually print the data type with print(type(var)) and print(isinstance(var,bytes)), but this results in the same error.

Andi AA
  • 13
  • 3
  • 2
    Have you tried to convert bytes to string using appropriate encoding 'utf-8' before printing it ?? – swordfish Jun 22 '23 at 10:45
  • @swordfish The problem seems to be that the OP actually tried to enforce an encoding. See my answer below. – cdalitz Jun 22 '23 at 12:15

2 Answers2

1
data = b'abc'

string_data = data.decode('utf-8')

stream.write(string_data)
swordfish
  • 959
  • 16
  • 39
0

This problem typically occurs when you have tried to set the default encoding of stdout like it was done in python 2.x:

>>> import sys; import codecs
>>> sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
>>> print("bla")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/codecs.py", line 377, in write
   self.stream.write(data)
TypeError: write() argument must be str, not bytes
>>> print(type("bla"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/codecs.py", line 377, in write
  self.stream.write(data)
TypeError: write() argument must be str, not bytes

Although there are other ways to achieve this in Python 3.x, this might lead to unintended side-effects when printing binary data that does not represent strings. It might thus be safest not to use a forced decoding method, but to explicitly call decode when it is actually needed.

cdalitz
  • 1,019
  • 1
  • 6
  • 7