开发者

How to initialize a set() in code to be compiled as pypy's rpython?

开发者 https://www.devze.com 2023-02-05 20:22 出处:网络
I want to compile some python code using pypy\'s rpython translator. A very simple toy example that doesn\'t do anything :

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:

  1. In the pypy install directory, under lib-python\2.7\, copy sets.py to your project directory, and rename the copy rsets.py.
  2. Search for the five instances of getattr in the file. Remove the last parameter (the default return value), which is in each case None.
  3. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号