Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BadStatusLine is hell to debug #51676

Closed
djc opened this issue Dec 3, 2009 · 8 comments
Closed

BadStatusLine is hell to debug #51676

djc opened this issue Dec 3, 2009 · 8 comments
Labels
stdlib Python modules in the Lib dir

Comments

@djc
Copy link
Member

djc commented Dec 3, 2009

BPO 7427
Nosy @djc, @merwok, @vadmium

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2010-02-24.04:50:42.730>
created_at = <Date 2009-12-03.15:52:43.024>
labels = ['library']
title = 'BadStatusLine is hell to debug'
updated_at = <Date 2017-03-12.21:48:19.280>
user = 'https://github.com/djc'

bugs.python.org fields:

activity = <Date 2017-03-12.21:48:19.280>
actor = 'martin.panter'
assignee = 'none'
closed = True
closed_date = <Date 2010-02-24.04:50:42.730>
closer = 'djc'
components = ['Library (Lib)']
creation = <Date 2009-12-03.15:52:43.024>
creator = 'djc'
dependencies = []
files = []
hgrepos = []
issue_num = 7427
keywords = []
message_count = 8.0
messages = ['95934', '95935', '95969', '100003', '100004', '171324', '231405', '289511']
nosy_count = 5.0
nosy_names = ['djc', 'eric.araujo', 'jakemcguire', 'martin.panter', 'aswan']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue7427'
versions = ['Python 2.7']

@djc
Copy link
Member Author

djc commented Dec 3, 2009

For whatever reason, BadStatusLine tracebacks often don't show the line
passed into them. Given the errr, heavy architecture of httplib, this
makes it pretty bad to debug. It's not clear to me why this is:

Traceback (most recent call last):
  File "/home/djc/src/couchdb-python/couchdb/tests/client.py", line 138,
in test_attachment_crud_with_files
    doc = self.db['foo']
  File "/home/djc/src/couchdb-python/couchdb/client.py", line 293, in
__getitem__
    _, _, data = self.resource.get(id)
  File "/home/djc/src/couchdb-python/couchdb/http.py", line 333, in get
    return self._request('GET', path, headers=headers, **params)
  File "/home/djc/src/couchdb-python/couchdb/http.py", line 350, in _request
    credentials=self.credentials)
  File "/home/djc/src/couchdb-python/couchdb/http.py", line 179, in request
    resp = _try_request()
  File "/home/djc/src/couchdb-python/couchdb/http.py", line 167, in
_try_request
    return conn.getresponse()
  File "/usr/lib/python2.6/httplib.py", line 950, in getresponse
  File "/usr/lib/python2.6/httplib.py", line 390, in begin
  File "/usr/lib/python2.6/httplib.py", line 354, in _read_status
BadStatusLine

However, some interactive testing shows that this should work:

djc@enrai couchdb-python $ python
Python 2.6.2 (r262:71600, Oct  5 2009, 12:18:48)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class CrapShoot(Exception):
...     def __init__(self, a):
...             self.args = a,
...
>>> raise CrapShoot('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
__main__.CrapShoot: a
>>> class ParentExc(Exception):
...     pass
...
>>> class CrapShoot(ParentExc):
...     def __init__(self, a):
...             self.args = a,
...
>>> raise CrapShoot('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
__main__.CrapShoot: a
>>>

Definition of BadStatusLine:

class BadStatusLine(HTTPException):
    def __init__(self, line):
        self.args = line,
        self.line = line

class HTTPException(Exception):
    # Subclasses that define an __init__ must call Exception.__init__
    # or define self.args.  Otherwise, str() will fail.
    pass

The note here seems like a cautionary but insufficient tale...

@djc djc added the stdlib Python modules in the Lib dir label Dec 3, 2009
@djc
Copy link
Member Author

djc commented Dec 3, 2009

Also, it might be useful here if it showed repr(line) instead of just
line, but that'd just be icing on the cake.

@jakemcguire
Copy link
Mannequin

jakemcguire mannequin commented Dec 4, 2009

I think what's happening is that your connection is being closed due to
inactivity, so the status line that comes back is empty. Printing
repr(line) would probably make the emptiness clear, but maybe the httplib
code should put in a more specific message in this case...

@djc
Copy link
Member Author

djc commented Feb 24, 2010

Fixed up the repr in r48417.

@djc djc closed this as completed Feb 24, 2010
@merwok
Copy link
Member

merwok commented Feb 24, 2010

Actually r78417.

@aswan
Copy link
Mannequin

aswan mannequin commented Sep 25, 2012

I just got tripped up by this change, I wanted to catch the specific case of an http server closing a connection and assumed that the following would work:

try:
resp = conn.getresponse()
except httplib.BadStatusLine, e:
if len(e.line) == 0:
# server closed...
else:
raise

That doesn't work since e.line holds the representation of the empty string instead of just holding the empty string. I think the fragment above would be a much better way to write this test, the current alterntative of:
if e.line == "''":
is hopelessly obscure.

Seems like the original fix should have been to add __repr__ to BadStatusLine rather than changing its contents. Can this be revisited?

@vadmium
Copy link
Member

vadmium commented Nov 20, 2014

As far as I can tell, the “line” attribute isn’t documented anyway. But bpo-8450 is opened about improving the exception when the connection is closed.

@vadmium
Copy link
Member

vadmium commented Mar 12, 2017

This change was only made in 2.7a4, not 2.6

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir
Projects
None yet
Development

No branches or pull requests

3 participants