Skip to content

Commit

Permalink
Avoid segmentation fault from xml.parsers.expat http://bugs.python.or…
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjc committed Oct 28, 2011
1 parent 0bdb1f2 commit 59f9cbd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Bio/Entrez/Parser.py
Expand Up @@ -161,6 +161,10 @@ def __init__(self, validate):

def read(self, handle):
"""Set up the parser and let it parse the XML results"""
if handle.closed:
#Should avoid a possible Segmentation Fault, see:
#http://bugs.python.org/issue4877
raise IOError("Can't parse a closed handle")
try:
self.parser.ParseFile(handle)
except expat.ExpatError, e:
Expand Down
9 changes: 9 additions & 0 deletions Tests/test_Entrez.py
Expand Up @@ -20,6 +20,15 @@

from Bio import Entrez

class GeneralTests(unittest.TestCase):
'''General tests for Bio.Entrez'''
def test_closed_handle(self):
'''Test parsing closed handle fails gracefully
'''
handle = open('Entrez/einfo1.xml', "rb")
handle.close()
self.assertRaises(IOError, Entrez.read, handle)


class EInfoTest(unittest.TestCase):
'''Tests for parsing XML output returned by EInfo
Expand Down

0 comments on commit 59f9cbd

Please sign in to comment.