Possible bug: OutputWrapper: TypeError: write() argument must be str, not bytes

54 views
Skip to first unread message

emi

unread,
Dec 29, 2017, 7:42:22 AM12/29/17
to Django users
Hello all!
I'm new to the list and mostly new to Django.

I am adding django-encrypted-model-fields to my Django project and one of the steps to install it is to generate a key using a ./manage.py command:
$ ./manage.py generate_encryption_key

When executing it, an error is raised:
Traceback (most recent call last):
  File "./manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/encrypted_model_fields/management/commands/generate_encryption_key.py", line 15, in handle
    self.stdout.write(key, ending=b'\n')
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 106, in write
    self._out.write(style_func(msg))
TypeError: write() argument must be str, not bytes


I have found a workaround for this, decoding output before writing to stdout, on django/core/management/base.py, line 106:

$ diff django/core/management/base.py-original django/core/management/base.py
106c106
<         self._out.write(style_func(msg))
---
>         self._out.write(style_func(msg).decode(self._out.encoding))

Do I have to do a pull request to Django or try to debug django-encrypted-model-fields and talk to their developers?

Thanks in advance!
em and best regardsi

pradam

unread,
Dec 29, 2017, 7:50:39 AM12/29/17
to django...@googlegroups.com
hi,

i think your trying to write bytes instead of str. please have look at below example it might help you:
name = b'i_m byte bro'
print(name.decode('utf-8'))  // just decode the byte to str

o/p: i_m byte bro



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b7372388-ea45-4ec7-b62e-84d0540a365a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matemática A3K

unread,
Dec 29, 2017, 10:04:57 AM12/29/17
to django...@googlegroups.com
I don't think you should patch the core management for only a third-party app, instead you should modify the third-party to match what django expects (i.e. convert to string at generate_encryption_key.py). I think you may contact their developers for including it on the package
 

Thanks in advance!
em and best regardsi

--

Jani Tiainen

unread,
Dec 29, 2017, 10:10:47 AM12/29/17
to django...@googlegroups.com
Hi,

As pointed out problem is not Django but that third party package that tries to output data as bytes but Django expects to have string instead.

First make sure that 3rd party app works with Django version you're using. If not you need to contact that 3rd party app developers to fix the actual issue there.

--
Reply all
Reply to author
Forward
0 new messages