2

Web アプリをテストするために、/dev/random からいくつかのランダムな文字を Web フロントエンドに貼り付けています。この行はエラーをスローします:

print repr(comment)
import html5lib
print html5lib.parse(comment, treebuilder="lxml")

'a\xef\xbf\xbd\xef\xbf\xbd\xc9\xb6E\xef\xbf\xbd\xef\xbf\xbd`\xef\xbf\xbd]\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd2 \x14\xef\xbf\xbd\xc7\xbe\xef\xbf\xbdy\xcb\x9c\xef\xbf\xbdi1O\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdZ\xef\xbf\xbd.\xef\xbf\xbd\x17^C'

Unhandled Error
    Traceback (most recent call last):
      File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py", line 893, in _inlineCallbacks
        result = g.send(result)
      File "/home/work/random/social/social/item.py", line 389, in _new
        convId, conv = yield plugin.create(request)
      File "/home/work/random/social/social/logging.py", line 47, in wrapper
        ret = func(*args, **kwargs)
      File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py", line 1014, in unwindGenerator
        return _inlineCallbacks(None, f(*args, **kwargs), Deferred())
    --- <exception caught here> ---
      File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py", line 893, in _inlineCallbacks
        result = g.send(result)
      File "/home/work/random/social/twisted/plugins/status.py", line 63, in create
        print html5lib.parse(comment, treebuilder="lxml")
      File "/usr/local/lib/python2.6/dist-packages/html5lib-0.90-py2.6.egg/html5lib/html5parser.py", line 38, in parse
        return p.parse(doc, encoding=encoding)
      File "/usr/local/lib/python2.6/dist-packages/html5lib-0.90-py2.6.egg/html5lib/html5parser.py", line 211, in parse
        parseMeta=parseMeta, useChardet=useChardet)
      File "/usr/local/lib/python2.6/dist-packages/html5lib-0.90-py2.6.egg/html5lib/html5parser.py", line 111, in _parse
        self.mainLoop()
      File "/usr/local/lib/python2.6/dist-packages/html5lib-0.90-py2.6.egg/html5lib/html5parser.py", line 174, in mainLoop
        self.phase.processCharacters(token)
      File "/usr/local/lib/python2.6/dist-packages/html5lib-0.90-py2.6.egg/html5lib/html5parser.py", line 572, in processCharacters
        self.parser.phase.processCharacters(token)
      File "/usr/local/lib/python2.6/dist-packages/html5lib-0.90-py2.6.egg/html5lib/html5parser.py", line 611, in processCharacters
        self.parser.phase.processCharacters(token)
      File "/usr/local/lib/python2.6/dist-packages/html5lib-0.90-py2.6.egg/html5lib/html5parser.py", line 652, in processCharacters
        self.parser.phase.processCharacters(token)
      File "/usr/local/lib/python2.6/dist-packages/html5lib-0.90-py2.6.egg/html5lib/html5parser.py", line 711, in processCharacters
        self.parser.phase.processCharacters(token)
      File "/usr/local/lib/python2.6/dist-packages/html5lib-0.90-py2.6.egg/html5lib/html5parser.py", line 804, in processCharacters
        self.parser.phase.processCharacters(token)
      File "/usr/local/lib/python2.6/dist-packages/html5lib-0.90-py2.6.egg/html5lib/html5parser.py", line 948, in processCharacters
        self.tree.insertText(token["data"])
      File "/usr/local/lib/python2.6/dist-packages/html5lib-0.90-py2.6.egg/html5lib/treebuilders/_base.py", line 288, in insertText
        parent.insertText(data)
      File "/usr/local/lib/python2.6/dist-packages/html5lib-0.90-py2.6.egg/html5lib/treebuilders/etree_lxml.py", line 225, in insertText
        builder.Element.insertText(self, data, insertBefore)
      File "/usr/local/lib/python2.6/dist-packages/html5lib-0.90-py2.6.egg/html5lib/treebuilders/etree.py", line 114, in insertText
        self._element.text += data
      File "lxml.etree.pyx", line 821, in lxml.etree._Element.text.__set__ (src/lxml/lxml.etree.c:33308)

      File "apihelpers.pxi", line 646, in lxml.etree._setNodeText (src/lxml/lxml.etree.c:15287)

      File "apihelpers.pxi", line 1295, in lxml.etree._utf8 (src/lxml/lxml.etree.c:20212)

    exceptions.ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes

ユーザーが入力した文字列をコミットする前に、次のことを行っています。

comment.decode('utf-8').encode('utf-8', "replace")

しかし、これはこの場合には役に立たないようです。

-- アビ

4

1 に答える 1

4

問題は、XMLのテキストに特定の文字を含めることができないことです。主に32未満のバイト値を持つ文字を制御します。XML1.0の推奨事項では、Charを次のように定義しています。

Char :: =#x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]

/ dev / randomは、これと一致しないバイトを提供できます(制御文字や一部のマルチバイト文字など)。

したがって、エンコーディングを試す前に、これらのバイトを除外する必要があります。

于 2011-08-20T07:55:25.650 に答える