pdbpp: TypeError: write() argument must be str, not bytes

Originally reported by: Aurélien Campéas (Bitbucket: auc, GitHub: auc)


#!python

  File "C:\Anaconda2\envs\py3k\lib\site-packages\_pytest\pdb.py", line 109, in post_mortem
    p.interaction(None, t)
  File "C:\Anaconda2\envs\py3k\lib\site-packages\pdb.py", line 244, in interaction
    self.print_stack_entry(self.stack[self.curindex])
  File "C:\Anaconda2\envs\py3k\lib\site-packages\pdb.py", line 798, in print_stack_entry
    print('[%d] >' % frame_index, file=self.stdout, end=' ')
  File "C:\Anaconda2\envs\py3k\lib\codecs.py", line 377, in write
    self.stream.write(data)
TypeError: write() argument must be str, not bytes

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 4
  • Comments: 20

Commits related to this issue

Most upvoted comments

I met the same error. After searching for a while, I follow the reference and work around by modifying codecs.py: self.stream.write(data) to self.stream.write(data.decode(‘utf-8’))

ref: https://stackoverflow.com/questions/21689365/python-3-typeerror-must-be-str-not-bytes-with-sys-stdout-write

Warning: I am okay with this modification. No guarantee for all cases using codes.py

I’m also having that problem on Python 3.5.2 using pdbpp 0.9.2 but it’s seems even more radical since it’s happening once I try to start pdb just after I imported it:


Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul  5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pdb
>>> pdb.set_trace()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Anaconda3\lib\bdb.py", line 52, in trace_dispatch
    return self.dispatch_return(frame, arg)
  File "C:\Program Files\Anaconda3\lib\bdb.py", line 93, in dispatch_return
    self.user_return(frame, arg)
  File "C:\Program Files\Anaconda3\lib\pdb.py", line 290, in user_return
    self.message('--Return--')
  File "C:\Program Files\Anaconda3\lib\pdb.py", line 447, in message
    print(msg, file=self.stdout)
  File "C:\Program Files\Anaconda3\lib\codecs.py", line 377, in write
    self.stream.write(data)
TypeError: write() argument must be str, not bytes

Yes, this is almost true.

Here is how to reproduce this error:

  1. Create test file with the test raising any exception, like this
def test_error():
    raise ValueError
  1. Run py.test with locale set to some non-Unicode (this is the key change). Like this: LANG=C py.test mytest.py
  2. Observe result:
...
E       ValueError

mytest.py:2: ValueError
======================================================================================= 1 failed in 0.23 seconds ========================================================================================
Traceback (most recent call last):
...
  File "/tmp/tmp.ou5kdfw1Va/lib/python3.6/site-packages/pdb.py", line 809, in print_stack_entry
    print('[%d] >' % frame_index, file=self.stdout, end=' ')
  File "/usr/lib/python3.6/codecs.py", line 377, in write
    self.stream.write(data)
TypeError: write() argument must be str, not bytes

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
...
  File "/tmp/tmp.ou5kdfw1Va/lib/python3.6/site-packages/_pytest/debugging.py", line 123, in post_mortem
    p.interaction(None, t)
  File "/tmp/tmp.ou5kdfw1Va/lib/python3.6/site-packages/pdb.py", line 252, in interaction
    self.print_stack_entry(self.stack[self.curindex])
  File "/tmp/tmp.ou5kdfw1Va/lib/python3.6/site-packages/pdb.py", line 809, in print_stack_entry
    print('[%d] >' % frame_index, file=self.stdout, end=' ')
  File "/usr/lib/python3.6/codecs.py", line 377, in write
    self.stream.write(data)
TypeError: write() argument must be str, not bytes

I think this issue may not be caused by pdbpp (pdbpp is a great debugging tool ! thank you). It is probably caused by the python 3.5 as discussed in the reference above.

The failure case for me: I follow the “Docker image” part to build the docker image of the pytorch (https://github.com/pytorch/pytorch). And then I launch the docker. Running the commands below:

pip install pdbpp python import pdb pdb.set_trace()

And the error is shown below, which is not exactly the same as the original post. I then modify the codes.py to work around the problem.

Traceback (most recent call last): File “<stdin>”, line 1, in <module> File “/opt/conda/envs/pytorch-py35/lib/python3.5/bdb.py”, line 52, in trace_dispatch return self.dispatch_return(frame, arg) File “/opt/conda/envs/pytorch-py35/lib/python3.5/bdb.py”, line 93, in dispatch_return self.user_return(frame, arg) File “/opt/conda/envs/pytorch-py35/lib/python3.5/pdb.py”, line 290, in user_return self.message(‘–Return–’) File “/opt/conda/envs/pytorch-py35/lib/python3.5/pdb.py”, line 447, in message print(msg, file=self.stdout) File “/opt/conda/envs/pytorch-py35/lib/python3.5/codecs.py”, line 377, in write self.stream.write(data) TypeError: write() argument must be str, not bytes