Python lxml.etree.iterparse是否关闭输入文件处理程序?
filterous是Python lxml.etree.iterparse是否关闭输入文件处理程序?,python,stringio,iterparse,Python,Stringio,Iterparse,filterous是iterparse来解析。但是,当尝试随后访问StringIO对象时,Python退出时会显示一条“ValueError:I/O operation on closed file”消息。根据“从lxml2.3开始,在错误情况下也将调用.close()方法”,但我从iterparse中没有收到错误消息或异常。我的IO-foo显然没有跟上进度,所以有人有什么建议吗 命令和(希望)相关代码: $ python2.6 setup.py test setup.py: from set
iterparse
来解析。但是,当尝试随后访问StringIO
对象时,Python退出时会显示一条“ValueError:I/O operation on closed file
”消息。根据“从lxml2.3开始,在错误情况下也将调用.close()方法”,但我从iterparse
中没有收到错误消息或异常。我的IO-foo显然没有跟上进度,所以有人有什么建议吗
命令和(希望)相关代码:
$ 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 '))
回溯:
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
错误:空标记;应该得到N个结果。
----------------------------------------------------------------------
回溯(最近一次呼叫最后一次):
文件“/home/victor/dev/filterus/tests/tests.py”,第149行,在test\u empty\u tag\u not中
self.xml.getvalue()目标(输出!)对象的方法,与您的StringIO无关。在任何情况下,您似乎也忽略了这个小词。在2.3之前,lxml仅在解析成功时才关闭目标对象。现在,它还会在出现错误时关闭目标对象
为什么要在解析完成后“访问”StringIO对象
更新通过事后尝试访问数据库,您的意思是测试中所有的self.xml.getvalue()调用吗?[在您的问题中显示ferschlugginer回溯,这样我们就不需要猜测了!]如果这导致了问题(它确实算作IO操作),那么忘记getvalue()…如果它能工作,它会不会返回(非常规命名)(不变)XML?似乎可以与StringIO
配合使用,尝试使用它而不是cStringIO
。不知道为什么会关闭它
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