From 59f9cbd2ad14ebd05d5864033ff0c7ef7a8f0daa Mon Sep 17 00:00:00 2001 From: peterjc Date: Fri, 28 Oct 2011 10:51:46 +0100 Subject: [PATCH] Avoid segmentation fault from xml.parsers.expat http://bugs.python.org/issue4877 --- Bio/Entrez/Parser.py | 4 ++++ Tests/test_Entrez.py | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/Bio/Entrez/Parser.py b/Bio/Entrez/Parser.py index 7327366893d..eaa507fed02 100644 --- a/Bio/Entrez/Parser.py +++ b/Bio/Entrez/Parser.py @@ -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: diff --git a/Tests/test_Entrez.py b/Tests/test_Entrez.py index 49b211d2502..fbd8f600fe7 100644 --- a/Tests/test_Entrez.py +++ b/Tests/test_Entrez.py @@ -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