Skip to content

Commit

Permalink
Ensure Python 3 doesn't choke on bad byte sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtmckee committed Jan 1, 2010
1 parent f36120a commit b2cdb73
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions listparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ def parse(parse_obj, agent=None, etag=None, modified=None, inject=False):
fileobj = Injector(fileobj)
try:
parser.parse(fileobj)
except (SAXParseException, MalformedByteSequenceException), err:
except (SAXParseException, MalformedByteSequenceException,
UnicodeDecodeError), err:
# Jython propagates exceptions past the ErrorHandler
# Python 3 chokes if a file not opened in binary mode
# contains non-Unicode byte sequences
handler.harvest.bozo = 1
handler.harvest.bozo_exception = err
fileobj.close()
Expand Down Expand Up @@ -423,7 +426,7 @@ def _mkfile(obj, agent, etag, modified):
return StringIO.StringIO(obj), SuperDict()
# Try dealing with it as a file
try:
return open(obj), SuperDict()
return open(obj, 'rb'), SuperDict()
except IOError, err:
return None, SuperDict({'bozo': 1, 'bozo_exception': err})
# It's a URL
Expand Down
4 changes: 4 additions & 0 deletions lptest.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ def testBogusFilename(self):
f = 'totally made up and bogus /\:'
result = listparser.parse(f)
self.assert_(result.bozo == 1)
def testImage(self):
f = os.path.abspath(os.path.join('tests', '1x1.gif'))
result = listparser.parse(f)
self.assert_(result.bozo == 1)
def worker(self, evals, testfile, etag, modified):
result = listparser.parse('http://localhost:8091/tests/' + testfile,
etag=etag, modified=modified)
Expand Down
Binary file added tests/1x1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b2cdb73

Please sign in to comment.