6

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?

highBandWidth
  • 16,751
  • 20
  • 84
  • 131

2 Answers2

4

So its official, set() is not supported in rpython. Thanks TryPyPy.

highBandWidth
  • 16,751
  • 20
  • 84
  • 131
  • Though I don't understand why it can't be added. If it's just built on top of a dict, one can even write a myset class and use it. Though it will be better to use the same names as python. – highBandWidth Jan 19 '11 at 23:19
0

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.

primo
  • 1,392
  • 16
  • 27