Question

I have the following code:

import sys

def entry_point(argv):
    sys.exit(1)
    return 0

def target(*args):
    return entry_point, None

However, when I run python ./pypy/pypy/translator/goal/translate.py t.py I get the following error:

...
[translation:ERROR]  Exception: unexpected prebuilt constant: <built-in function exit>
[translation:ERROR] Processing block:
[translation:ERROR]  block@9 is a <class 'pypy.objspace.flow.flowcontext.SpamBlock'>
[translation:ERROR]  in (t:3)entry_point
[translation:ERROR]  containing the following operations:
[translation:ERROR]        v0 = simple_call((builtin_function_or_method exit), (1))
[translation:ERROR]  --end--

There was actually more to the error but I thought only this last part was relevant. If you think more of it might be helpful, please comment and I will edit.

In fact, I get another error when I replace sys.exit with something even simpler like sys.stdout.write.

import sys

def entry_point(argv):
    sys.stdout.write('some mesg\n')
    return 0

def target(*args):
    return entry_point, None

gives me:

...
[translation:ERROR]  AnnotatorError: annotation of v0 degenerated to SomeObject()
[translation:ERROR] v0 = getattr((module sys), ('stdout'))
[translation:ERROR]
[translation:ERROR] In <FunctionGraph of (t:3)entry_point at 0x10d03de10>:
[translation:ERROR] Happened at file t.py line 4
[translation:ERROR]
[translation:ERROR] ==>     sys.stdout.write('some mesg\n')
[translation:ERROR]
[translation:ERROR] Previous annotation:
[translation:ERROR]   (none)
[translation:ERROR] Processing block:
[translation:ERROR]  block@3 is a <class 'pypy.objspace.flow.flowcontext.SpamBlock'>
[translation:ERROR]  in (t:3)entry_point
[translation:ERROR]  containing the following operations:
[translation:ERROR]        v0 = getattr((module sys), ('stdout'))
[translation:ERROR]        v1 = getattr(v0, ('write'))
[translation:ERROR]        v2 = simple_call(v1, ('some mesg\n'))
[translation:ERROR]  --end--

Are sys methods simply off limits for RPython? It seems kind of weird to me because exit and stdout are so readily available in C. However, the error messages kind of look like they might be about different things, so I might just be barking down the wrong tree.

Currently I am using this guide to figure out roughly what is allowed and not allowed in RPython. Are there other rather accessible references I could use for more information?

Was it helpful?

Solution

The sys module isn't RPython, you can't use it in an RPython program. To return a status code you must return it directly from the entry_point function.

You also can't use sys.stdout/sys.stdin/sys.stderr, you'll need to read/write using the os.read/os.write functions combined with a file descriptor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top