Opened 9 months ago

Last modified 13 days ago

#20815 new Bug unbuffered text IO error (python3)

Reported by: alex01@… Owned by: nobody
Component: Python 3 Version: master
Severity: Normal Keywords: python3 runfcgi open
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by claudep)

When trying to run fcgi server I get "ValueError: can't have unbuffered text I/O"

My python version and django version:

$ python --version
Python 3.2.3

>>> django.get_version()

Can't start server:
(virtenv02)jsmith@thinkpad01:~/virtenv02/mysite$ python runfcgi host= port=8100
Traceback (most recent call last):
  File "", line 10, in <module>
  File "/home/jsmith/virtenv02/django-trunk/django/core/management/", line 397, in execute_from_command_line
  File "/home/jsmith/virtenv02/django-trunk/django/core/management/", line 390, in execute
  File "/home/jsmith/virtenv02/django-trunk/django/core/management/", line 240, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/jsmith/virtenv02/django-trunk/django/core/management/", line 283, in execute
    output = self.handle(*args, **options)
  File "/home/jsmith/virtenv02/django-trunk/django/core/management/commands/", line 22, in handle
  File "/home/jsmith/virtenv02/django-trunk/django/core/servers/", line 176, in runfastcgi
    become_daemon(our_home_dir=options["workdir"], **daemon_kwargs)
  File "/home/jsmith/virtenv02/django-trunk/django/utils/", line 28, in become_daemon
    so = open(out_log, 'a+', 0)
ValueError: can't have unbuffered text I/O

Server starts successfully and works if I give it daemonize=False option:
(virtenv02)jsmith@thinkpad01:~/virtenv02/mysite$ python runfcgi host= port=8100 daemonize=False

As far as I understand, this issue is specific to python3 and is related to changes in open function introduced in pep-3116


Attachments (0)

Change History (5)

comment:1 follow-up: Changed 9 months ago by claudep

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Unfortunately, this is old untested code. I wonder if the solution would be to simply forget the 0 argument (unbuffered) of the open call. Maybe someone more knowledgeable about buffered/unbuffered output may chime in to enlighten us?

comment:2 follow-up: Changed 9 months ago by aaugustin

Note that FastCGI is deprecated in 1.7.

comment:3 in reply to: ↑ 2 Changed 13 days ago by dk+django@…

Replying to aaugustin:

Note that FastCGI is deprecated in 1.7.

Interesting decision, why so? Cannot spot any rationale in the 1.7 release notes. Pointers?
However, the error is not really fcgi-related, any code using django/utils/ will have the same issue in Py3k.
Log files are text files, and you cannot open them in unbuffered mode.

comment:4 in reply to: ↑ 1 Changed 13 days ago by dk+django@…

Replying to claudep:

Maybe someone more knowledgeable about buffered/unbuffered output may chime in to enlighten us?

Change it to

open( out_log, 'a+', 1)

This buffers up to the next EOL, which makes sense for log files. (Same for err_log)

As per Python3 docs:

buffering is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode), 1 to select line buffering (only usable in text mode)

Looking at open() in Python2, line buffering should also work:

  • If something goes wrong while writing to out_log, you get an exception
  • I haven't yet seen traceback failing in the middle of a line, so it is probably also safe for err_log

Hope this helps.

comment:5 Changed 13 days ago by timo

Ticket for FastCGI deprecation is #20766.

Add Comment

Modify Ticket

Change Properties
<Author field>
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.