简体   繁体   English

lxml.etree.iterparse关闭输入文件处理程序?

[英]lxml.etree.iterparse closes input file handler?

filterous is using iterparse to parse a simple XML StringIO object in a unit test . filterous 使用 iterparse单元测试中解析一个简单的XML StringIO对象 However, when trying to access the StringIO object afterwards, Python exits with a " ValueError: I/O operation on closed file " message. 但是,在尝试访问StringIO对象之后,Python将以“ ValueError: I/O operation on closed file ”消息退出。 According to the iterparse documentation , "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 . 根据iterparse文档 ,“从lxml 2.3开始,在错误情况下也会调用.close()方法,”但是我没有得到错误消息或来自iterparse Exception My IO-foo is obviously not up to speed, so does anyone have suggestions? 我的IO-foo显然没有达到速度,所以有人有建议吗?

The command and (hopefully) relevant code: 命令和(希望)相关代码:

$ python2.6 setup.py test

setup.py: setup.py:

from setuptools import setup
from filterous import filterous as package

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

tests/tests.py: 测试/ 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: 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 . PS:测试在2010-07-27都运行良好。

Seems to work fine with StringIO , try using that instead of cStringIO . 似乎可以使用StringIO正常工作,尝试使用它而不是cStringIO No idea why it's getting closed. 不知道为什么它会被关闭。

Docs-fu is the problem. Docs-fu是问题所在。 What you quoted "Starting with lxml 2.3, the .close() method will also be called in the error case," is nothing to do with iterparse. 你引用的内容“从lxml 2.3开始,在错误情况下也会调用.close()方法,”与iterparse无关。 It appears on your linked page before the section on iterparse. 它出现在iterparse部分之前的链接页面上。 It is part of the docs for the target parser interface. 它是目标解析器接口的文档的一部分。 It is referring to the close() method of the target (output!) object, nothing to do with your StringIO. 它指的是target(output!)对象的close()方法,与StringIO无关。 In any case, you also seem to have ignored that little word also . 在任何情况下,你似乎也忽略了这个小小的单词 Before 2.3, lxml closed the target object only if the parse was successful. 在2.3之前,lxml仅在解析成功时才关闭目标对象。 Now it also closes it upon error. 现在它也会在出错时关闭它。

Why do you want to "access" the StringIO object after parsing has finished? 为什么要在解析完成后“访问”StringIO对象?

Update By trying to access the database afterwards, do you mean all those self.xml.getvalue() calls in your tests? 更新通过尝试之后访问数据库,您的意思是在测试中调用所有self.xml.getvalue()吗? [Show the ferschlugginer traceback in your question so we don't need to guess!] If that's causing the problem (it does count as an IO operation), forget getvalue() ... if it were to work, wouldn't it return the (unconventionally named) (invariant) XML? [在您的问题中显示ferschlugginer回溯所以我们不需要猜测!]如果这导致问题(它确实算作IO操作),请忘记getvalue()...如果它可以工作,不会返回(非常规命名)(不变)XML?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM