bottlepy / bottle

bottle.py is a fast and simple micro-framework for python web-applications.
http://bottlepy.org/
MIT License
8.37k stars 1.46k forks source link

Attribute Error if sys.stdout/sys.stderr is None #1244

Closed Killah-Chicken closed 3 years ago

Killah-Chicken commented 3 years ago

If sys.stdout is None or sys.stderr is None, importing bottle causes an AttributeError. It is caused by this piece of code (bottle.py line 118), because attribute write of NoneType could not be found:

# Workaround for the "print is a keyword/function" Python 2/3 dilemma
# and a fallback for mod_wsgi (resticts stdout/err attribute access)
try:
    _stdout, _stderr = sys.stdout.write, sys.stderr.write
except IOError:
    _stdout = lambda x: sys.stdout.write(x)
    _stderr = lambda x: sys.stderr.write(x)

I was wondering that sys.stdout could be set to None, but found: stackoverflow: stdout = None

I could fix it with setting and "empty" lambda to _stdout and _stderr

try:
    _stdout, _stderr = sys.stdout.write, sys.stderr.write
except IOError:
    _stdout = lambda x: sys.stdout.write(x)
    _stderr = lambda x: sys.stderr.write(x)
except AttributeError:
    _stdout = _stderr = lambda x: None
oz123 commented 3 years ago

I think one should consider using from __future__ import print_function. At this point bottle only supports Python2.7 so it's safe.

https://docs.python.org/2/library/__future__.html

I believe the code here should work for you.

@Killah-Chicken if you use python3 give this a shot.

defnull commented 3 years ago

Fixed in 5e2efb6a