如何在要编译为 pypy 的 rpython 的代码中初始化 set() ?
我想使用 pypy 的 rpython 翻译器编译一些 python 代码。一个非常简单的玩具示例,不执行任何操作:
def main(argv):
a = []
b = set(a)
print b
return 0
def target(driver,args):
return main,None
如果我将其编译为:
python2.6 ~/Downloads/pypy-1.4.1-src/pypy/translator/goal/translate.py --output trypy trypy.py
它不会编译,而只是因错误而停止,如下所示:
[translation:ERROR] AttributeError': 'FrozenDesc' object has no attribute 'rowkey'
[translation:ERROR] .. v1 = simple_call((type set), v0)
[translation:ERROR] .. '(trypy:3)main'
[translation:ERROR] Processing block:
[translation:ERROR] block@0 is a <class 'pypy.objspace.flow.flowcontext.SpamBlock'>
[translation:ERROR] in (trypy:3)main
[translation:ERROR] containing the following operations:
[translation:ERROR] v0 = newlist()
[translation:ERROR] v1 = simple_call((type set), v0)
[translation:ERROR] v2 = str(v1)
[translation:ERROR] v3 = simple_call((function rpython_print_item), v2)
[translation:ERROR] v4 = simple_call((function rpython_print_newline))
[translation:ERROR] --end--
如果我取出 set() 函数,它就会起作用。 rpython 中如何使用集合?
I want to compile some python code using pypy's rpython translator. A very simple toy example that doesn't do anything :
def main(argv):
a = []
b = set(a)
print b
return 0
def target(driver,args):
return main,None
If I compile it as:
python2.6 ~/Downloads/pypy-1.4.1-src/pypy/translator/goal/translate.py --output trypy trypy.py
It doesn't compile, rather just halts with errors something like this:
[translation:ERROR] AttributeError': 'FrozenDesc' object has no attribute 'rowkey'
[translation:ERROR] .. v1 = simple_call((type set), v0)
[translation:ERROR] .. '(trypy:3)main'
[translation:ERROR] Processing block:
[translation:ERROR] block@0 is a <class 'pypy.objspace.flow.flowcontext.SpamBlock'>
[translation:ERROR] in (trypy:3)main
[translation:ERROR] containing the following operations:
[translation:ERROR] v0 = newlist()
[translation:ERROR] v1 = simple_call((type set), v0)
[translation:ERROR] v2 = str(v1)
[translation:ERROR] v3 = simple_call((function rpython_print_item), v2)
[translation:ERROR] v4 = simple_call((function rpython_print_newline))
[translation:ERROR] --end--
If I take out the set() function it works. How do you use sets in rpython?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以官方说,rpython 中不支持 set() 。谢谢尝试PyPy。
So its official, set() is not supported in rpython. Thanks TryPyPy.
虽然 RPython 无法识别set
,但它能够导入Sets
模块。我似乎说得太早了。
sets
模块使用三个参数的getattr
调用,RPython 不支持可选的第三个参数。可以通过以下方法修复此问题:
lib-python\2.7\
下,将sets.py
复制到项目目录,并将副本重命名为rsets.py
。getattr
的五个实例。删除最后一个参数(默认返回值),在每种情况下该参数均为None
。from rsets import Set as set
添加到您的 RPython 代码中。在这五个实例中的每一个中,如果元素不可散列,它将返回
AttributeError
而不是TypeError
,但否则将按预期工作。While RPython does not recognizeset
it is capable of importing theSets
module.I seem to have spoken a bit too soon. The
sets
module uses three parametergetattr
calls, RPython does not support the optional third paramemter.This can be fixed by:
lib-python\2.7\
, copysets.py
to your project directory, and rename the copyrsets.py
.getattr
in the file. Remove the last parameter (the default return value), which is in each caseNone
.from rsets import Set as set
to your RPython code.In each of the five instances, should the element not be hashable, it will return an
AttributeError
rather than aTypeError
, but will otherwise work as expected.