Jython 和 xml.sax 解析器 - 奇怪的错误

发布于 2024-10-19 01:48:56 字数 1474 浏览 4 评论 0原文

我刚刚开始使用 Python/Jython 和 SAX 解析器 (xml.sax)。我编写了一个简单的内容处理程序作为测试。

from __future__ import with_statement 

from xml.sax import make_parser, handler
from xml.sax.handler import ContentHandler

class CountingHandler(ContentHandler):

    def __init__(self):
        self.counter = 0

    def startElement(self, name, attrs):
        self.counter += 1

def main(argv=sys.argv):
    parser = make_parser()
    h = CountingHandler()
    parser.setContentHandler(h)
    with open(argv[1], "r") as input:
        parser.parse(input)

当我在某些文档(不是全部)上运行此命令时,出现错误:

Traceback (most recent call last):
  File "src/sciencenetworks/xmltools.py", line 93, in <module>
    sys.exit(main())
  File "src/sciencenetworks/xmltools.py", line 88, in main
    parser.parse(input)
  File "/amd.home/home/staudt/workspace/jython/Lib/xml/sax/drivers2/drv_javasax.py", line 141, in parse
    self._parser.parse(JyInputSourceWrapper(source))
  File "/amd.home/home/staudt/workspace/jython/Lib/xml/sax/drivers2/drv_javasax.py", line 90, in resolveEntity
    return JyInputSourceWrapper(self._resolver.resolveEntity(pubId, sysId))
  File "/amd.home/home/staudt/workspace/jython/Lib/xml/sax/drivers2/drv_javasax.py", line 75, in __init__
    if source.getByteStream():
AttributeError: 'unicode' object has no attribute 'getByteStream'

当我查看 drv_javasax.py 的源代码时,似乎输入未被识别为类似文件的对象,这是的。
关于如何解决这个问题有什么想法吗?

I am just getting started with Python/Jython and the SAX parser (xml.sax). I wrote a simple content handler as a test.

from __future__ import with_statement 

from xml.sax import make_parser, handler
from xml.sax.handler import ContentHandler

class CountingHandler(ContentHandler):

    def __init__(self):
        self.counter = 0

    def startElement(self, name, attrs):
        self.counter += 1

def main(argv=sys.argv):
    parser = make_parser()
    h = CountingHandler()
    parser.setContentHandler(h)
    with open(argv[1], "r") as input:
        parser.parse(input)

When I run this on some documents (not all), I get an error:

Traceback (most recent call last):
  File "src/sciencenetworks/xmltools.py", line 93, in <module>
    sys.exit(main())
  File "src/sciencenetworks/xmltools.py", line 88, in main
    parser.parse(input)
  File "/amd.home/home/staudt/workspace/jython/Lib/xml/sax/drivers2/drv_javasax.py", line 141, in parse
    self._parser.parse(JyInputSourceWrapper(source))
  File "/amd.home/home/staudt/workspace/jython/Lib/xml/sax/drivers2/drv_javasax.py", line 90, in resolveEntity
    return JyInputSourceWrapper(self._resolver.resolveEntity(pubId, sysId))
  File "/amd.home/home/staudt/workspace/jython/Lib/xml/sax/drivers2/drv_javasax.py", line 75, in __init__
    if source.getByteStream():
AttributeError: 'unicode' object has no attribute 'getByteStream'

When I look into the source code of drv_javasax.py, it seems like input is not recognized as a file like object, which it is.
Any ideas on how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

俯瞰星空 2024-10-26 01:48:56

我认为这是这个错误:http://bugs.jython.com/issue1488。 Jython 2.5.2-b1 中已修复:http://www.jython.org/latest.html

I think it's this bug: http://bugs.jython.com/issue1488. Fixed in Jython 2.5.2-b1: http://www.jython.org/latest.html

ㄟ。诗瑗 2024-10-26 01:48:56

当您在 with 语句后插入 print type(input) 时,您会看到什么?

当您恢复到旧式“try/finally”代码而不是“with”时,它是否适用于所有文件?

有效的文件和无效的文件有什么不同?

如果将名称 input 更改为不影响内置函数的名称,会发生什么情况?

When you insert print type(input) after your with statement, what do you see?

When you revert to old-style "try/finally" code instead of "with", does it work for all files?

What is different between files that work and files that don't work?

What happens if you change the name input to something that doesn't shadow a built-in function?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文