filterous用于 在单元测试iterparse
中解析简单的XMLStringIO
对象。但是,当之后尝试访问该对象时,Python 会以“ ”消息退出。根据文档,“从 lxml 2.3 开始,在错误情况下也将调用 .close() 方法”,但我没有收到错误消息或来自. 我的 IO-foo 显然跟不上速度,所以有人有建议吗?StringIO
ValueError: I/O operation on closed file
iterparse
Exception
iterparse
命令和(希望)相关代码:
$ python2.6 setup.py test
设置.py:
from setuptools import setup
from filterous import filterous as package
setup(
...
test_suite = '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 '))
过滤/过滤.py:
from lxml import etree
...
def search(file_pointer, out, terms, includes, human_readable = True):
...
context = etree.iterparse(file_pointer, tag='posts')
追溯:
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:测试在2010-07-27运行良好。