lxml.etree.iterparse closes input file handler?
Question: filterous is
[using](https://github.com/l0b0/filterous/blob/cc172cc54bd068a5f1de231e3567f4fe0bb5d2d1/filterous/filterous.py#L262)
`iterparse` to parse a simple [XML `StringIO`
object](https://github.com/l0b0/filterous/blob/cc172cc54bd068a5f1de231e3567f4fe0bb5d2d1/tests/tests.py#L56)
in a [unit
test](https://github.com/l0b0/filterous/blob/cc172cc54bd068a5f1de231e3567f4fe0bb5d2d1/tests/tests.py#L139).
However, when trying to access the `StringIO` object afterwards, Python exits
with a "`ValueError: I/O operation on closed file`" message. According to the
[`iterparse` documentation](http://lxml.de/parsing.html#iterparse-and-
iterwalk), "Starting with lxml 2.3, the .close() method will also be called in
the error case," but I get no error message or `Exception` from `iterparse`.
My IO-foo is obviously not up to speed, so does anyone have suggestions?

The command and (hopefully) relevant 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: The tests all ran fine on
[2010-07-27](http://pypi.python.org/pypi?%3aaction=display&name=Filterous&version=0.8.2).


Answer: Seems to work fine with `StringIO`, try using that instead of `cStringIO`. No
idea why it's getting closed.

