Does lxml.etree.iterparse close an input file handler?

filterous using iterparse parsing simple XML StringIOobject in unit test . However, when trying to access the object StringIO, Python exits with the message " ValueError: I/O operation on closed file". According to the iterparsedocumentation , “Starting with lxml 2.3, in case of an error, the .close () method will also be called,” but I do not receive an error message or Exceptionfrom iterparse. My IO-foo is obviously not up to speed, so does anyone have any suggestions?

The command and (hopefully) the appropriate code:

$ python2.6 setup.py test

setup.py:

from setuptools import setup
from filterous import filterous as package

setup(
    ...
    test_suite = 'tests.tests',

Tests / tests.py:

from cStringIO import StringIO
import unittest

from filterous import filterous

XML = '''<posts tag="" total="3" ...'''

class TestSearch(unittest.TestCase):
    def setUp(self):
        self.xml = StringIO(XML)
        self.result = StringIO()
    ...
    def test_empty_tag_not(self):
        """Empty tag; should get N results."""
        filterous.search(
            self.xml,
            self.result,
            {'ntag': [u'']},
            ['href'],
            False)
        self.assertEqual(
            len(self.result.getvalue().splitlines()),
            self.xml.getvalue().count('<post '))

filterous / filterous.py:

from lxml import etree
...
def search(file_pointer, out, terms, includes, human_readable = True):
    ...
    context = etree.iterparse(file_pointer, tag='posts')

Traceback:

ERROR: Empty tag; should get N results.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/victor/dev/filterous/tests/tests.py", line 149, in test_empty_tag_not
    self.xml.getvalue().count('<post '))
ValueError: I/O operation on closed file

PS: All tests passed normally on 2010-07-27 .

+5
2

, StringIO, cStringIO. , .

+1

Docs-fu - . , " lxml 2.3, .close() ", iterparse. iterparse. . close() target (output!), StringIO. , , , . 2.3, lxml , . .

" " StringIO ?

. self.xml.getvalue() ? [ ferschlugginer , !] ( -), getvalue()... , ? ( ) () XML?

0

All Articles