Brython-3.4.0:使用 etree.ElementTree 触发 pyexpat 导入和回溯

Brython-3.4.0: Using etree.ElementTree triggers pyexpat import and tracebacks

我有一个用于 Brython 相关测试的小测试页面,最近在那里添加了 xml.etree.Elementree 模块,但由于某些原因它不起作用。

我有下面的代码(实际上还有更多的东西,但我删除了不相关的部分):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="/js/jquery-ui-1.12.1/jquery-ui.min.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/js/jquery-ui-1.12.1/jquery-ui.structure.min.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/layout.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/visual.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/js/jquery-ui-1.12.1/jquery-ui.theme.min.css" media="screen" />
<script rel="script" type="text/javascript" src="/js/jquery-3.1.1.min.js"></script>
<script rel="script" type="text/javascript" src="/js/jquery-ui-1.12.1/jquery-ui.min.js"></script>
<script rel="script" type="text/javascript" src="/js/brython/brython.js"></script>
<script rel="script" type="text/javascript" src="/js/brython/brython_stdlib.js"></script>
</head>

<body onload='brython()'>
    <script type="text/python" charset="utf-8">
    import sys
    from browser import alert, document as docu
    from browser import ajax
    from xml.etree import ElementTree as ET

    def ajaxReceive(req):
        alert("Input value: " + docu["numinput"].value)
        alert('Ajax response: \n %s' % req.text )
        if req.status == 200 or req.status == 0:
            d = docu['messagebox']
            d.clear()

            r = ET.fromstring(req.text)
            #n = r.findall('./person/name')
            #a = r.findall('./person/age')
            #d.text = 'Dude %s is %s old.' % (n,a)
        else:
            docu['messagebox'] <= 'error: ' + req.text

    def ajaxSend():
        req = ajax.ajax()
        url = '/bryt/'
        x = 1
        y = 2
        z = docu['numinput']

        req.open('POST', url, True)
        req.bind('complete', ajaxReceive)
        req.set_header('content-type', 'application/x-www-form-urlencoded' )
        req.send( {
            'action': 'calc',
            'x': x, 
            'y': y,
            'z': z.value
        })

    docu['ajaxbutton'].bind('click', ajaxSend )

    d = docu['messagebox']
    d.clear()
    d.text = 'ready'
    </script>

    <div id='messagebox' style='width: 20%; border: 1px solid gray; padding: 2em;' >
    </div>
    <br/>

    <input id="numinput" / >
    <br/>
    <br/>
    <button id='ajaxbutton' >Ajax run</button>
</body>
</html>

并且在服务器端,它只将 3 添加到给定的数字。问题是收到 XML 格式的 Ajax 响应。它以明确的方式出现 XML,但即使通过调用 .fromstring() 函数构建一个 etree 根元素,它的回溯如下:

Error 500 means that Python module pyexpat was not found at url http://example.com/bryt/pyexpat.py
brython.js:7171:1
Error 500 means that Python module pyexpat was not found at url http://example.com/bryt/pyexpat/__init__.py
brython.js:7171:1
Error 500 means that Python module pyexpat was not found at url http://example.com/js/brython/Lib/site-packages/pyexpat.py
brython.js:7171:1
Error 500 means that Python module pyexpat was not found at url http://example.com/js/brython/Lib/site-packages/pyexpat/__init__.py
brython.js:7171:1
Error for module xml.parsers.expat
brython.js:7242:21


Error: 
Stack trace:
_b_.ImportError.$factory@http://example.com/js/brython/brython.js line 6466 > eval:49:371
import_hooks@http://example.com/js/brython/brython.js:11605:7
$B.$__import__@http://example.com/js/brython/brython.js:7430:33
$B.$import@http://example.com/js/brython/brython.js:7460:43
$module<@http://example.com/js/brython/brython.js line 7242 > eval:14:9
@http://example.com/js/brython/brython.js line 7242 > eval:1:16
run_py@http://example.com/js/brython/brython.js:7242:1
exec_module@http://example.com/js/brython/brython.js:7276:1
cl_method@http://example.com/js/brython/brython.js:4729:43
import_hooks@http://example.com/js/brython/brython.js:11629:5
$B.$__import__@http://example.com/js/brython/brython.js:7430:33
$B.$import@http://example.com/js/brython/brython.js:7473:5
__init__205@http://example.com/js/brython/brython.js line 7242 > eval:5653:25
type.__call__@http://example.com/js/brython/brython.js:4674:20
factory@http://example.com/js/brython/brython.js:4741:47
XML194@http://example.com/js/brython/brython.js line 7242 > eval:5190:41
ajaxReceive3@http://example.com/js/brython/brython.js line 4294 > eval:176:32
@http://example.com/js/brython/brython.js line 7188 > eval:69:24
ajax.$factory/xmlhttp.onreadystatechange@http://example.com/js/brython/brython.js line 7188 > eval:161:13
brython.js:7243:1
16:21:17.002 args 
Array [ "No module named pyexpat" ]

过去也有过类似的事情brython issue 613 Pierre 指出 没有纯 python pyexpat 这样的东西(2017 年 7 月)。但是 Brython standard distribution 列出了 Lib/xml/etree 和 expat.py - 这是否意味着它仍然不可用?

Brython Lib/xml/etreeElementTree.py 第 1511 行及以后以:

class XMLParser:

    def __init__(self, html=0, target=None, encoding=None):
        try:
            from xml.parsers import expat
        except ImportError:
            try:
                import pyexpat as expat
            except ImportError:
                raise ImportError(
                    "No module named expat; use SimpleXMLTreeBuilder instead"
                    )

根据我的理解,它应该首先 import from xml.parsers import expat 成功,但显然当它尝试使用不存在的 pyexpat 版本时它不会成​​功。

那么,问题是,有没有其他人遇到同样的问题 and/or 有没有人对此有解决方案?


一些额外的(第二天)观察:

克隆签出标签从git构建存储库并不像您期望的那样真正工作(没有双关语意)。

% git clone https://github.com/brython-dev/brython.git brython.git
% cd brython.git/scripts
brython.git/scripts% python3 ./make_dist.py 
/usr/bin
Traceback (most recent call last):
  File "./make_dist.py", line 207, in <module>
    run()
  File "./make_dist.py", line 88, in run
    import make_stdlib_list
  File "brython.git/scripts/make_stdlib_list.py", line 53, in <module>
    with open(os.path.join(static_doc_folder,lang,'stdlib.html'), 'w', encoding="utf-8") as out:
FileNotFoundError: [Errno 2] No such file or directory: 'brython.git/www/static_doc/en/stdlib.html'
brython.git% 

这是由于缺少目录造成的:

brython.git/scripts% mkdir -p  ../www/static_doc/{en,es,fr}

brython.git/scripts% python3 ./make_dist.py

最后的构建行是:

adding xml.etree
adding xml.etree.cElementTree
adding xml.parsers
adding xml.parsers.expat
adding xml.sax

所以也许这些都包括在内。

一旦创建了目标,它们(显然,不太确定)会出现在 brython.git/setup/data 目录中,为实时网站发布 zip 文件和裸 .js 文件.所以我 link 进入我的 Apache httpd webroot 中的那个目录。但是那个楼并没有解决回溯问题

附带说明一下,对于像我这样的老 OpenSource 放屁,我觉得这个源代码树很陌生,这个项目是在 Mouse camp(Microsoft Windows ) 甚至我设法找到的一个罕见的 Makefile 也不能与 GNU Make 一起使用,因为空格的使用存在冲突。更不用说会有常规的 INSTALL、README、setup、Makefile 等具有预期内容的文件。我真的是在阅读资料并猜测这一切应该如何运作。但我想这只能说明 Python 确实是一种跨平台语言。

作为 "Open Source project",有趣的是它的讨论并不适合所有人: 您申请加入 Google group brython 已被拒绝

好吧,深入挖掘,Lib/xml/parses/expat.py 似乎包含:

"""Interface to the Expat non-validating XML parser."""
import sys

from pyexpat import *

# provide pyexpat submodules as xml.parsers.expat submodules
sys.modules['xml.parsers.expat.model'] = model
sys.modules['xml.parsers.expat.errors'] = errors

我试图评论 pyexpat 部分并重建 pkgs,回溯现在不同了。所以没有 expat,没有 pyexpat,也没有 ElementTree。那时 Ajax 中没有 XML 回复。

Issue 的答案仍然相关。 Expat 是不能使用的 C-library。你需要将就 window.DOMParser.

使用 brython 的首选方法不是通过克隆 repo,而是使用 pip:

$ pip install brython

然后

$ python3 -m brython install

在您开发应用程序的目录中。这将复制所有必要的 javascript 文件并创建一个示例应用程序,您可以从中开始。

请注意,这在 "nonexistent" README.md 文件中都有描述。