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开发者_JS百科 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?
So its official, set() is not supported in rpython. Thanks TryPyPy.
While RPython does not recognize set
it is capable of importing the Sets
module.
I seem to have spoken a bit too soon. The sets
module uses three parameter getattr
calls, RPython does not support the optional third paramemter.
This can be fixed by:
- In the pypy install directory, under
lib-python\2.7\
, copysets.py
to your project directory, and rename the copyrsets.py
. - Search for the five instances of
getattr
in the file. Remove the last parameter (the default return value), which is in each caseNone
. - Prepend
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 a TypeError
, but will otherwise work as expected.
精彩评论