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

时间:2018-03-20 15:43:34

标签: python-3.x brython

我有一个针对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 皮埃尔声称没有纯粹的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"
                    )

根据我的理解,它应该首先从xml.parsers import expat 导入,但显然它在尝试不存在的pyexpat版本时没有。

所以,问题是,是否有其他人偶然发现同样的问题和/或是否有人有解决方案?

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

从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文件。所以我链接到我的Apache httpd webroot中的那个目录。但那座建筑并没有解决追溯问题。

作为旁注,对于我自己的旧OpenSource屁,我觉得这个源代码树非常陌生,这个项目是在 Mouse camp (Microsoft Windows)中完成的,甚至是我设法的一个罕见的Makefile查找,不适用于GNU Make,因为空白的冲突使用。更不用说会有预期内容的常规INSTALL,README,setup,Makefile等文件。我真的在阅读消息来源并猜测这一切是如何起作用的。但我想这只能说明Python确实是一种跨平台的语言。

作为一个&#34;开源项目&#34;,有趣的是它的讨论并不适合所有人: 您加入Google group brython的申请已被拒绝

2 个答案:

答案 0 :(得分:0)

好吧,深入挖掘,似乎 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。

答案 1 :(得分:0)

该问题的答案仍然相关。 Expat是一个无法使用的C库。您需要使用window.DOMParser

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

$ pip install brython

然后

$ python3 -m brython install

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

请注意,这些都在&#34;不存在&#34; README.md档案。