Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RPython sys methods don't work

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?

like image 789
math4tots Avatar asked Feb 11 '12 09:02

math4tots


People also ask

How does Sys work in Python?

The sys module in Python provides various functions and variables that are used to manipulate different parts of the Python runtime environment. It allows operating on the interpreter as it provides access to the variables and functions that interact strongly with the interpreter.

How do I pass SYS argv in Python?

How do I use SYS argv in Python script? To use, sys. argv in a Python script, we need to impo r t the sys module into the script. Like we import all modules, "import sys" does the job.

Is it good to use Sys exit in Python?

Unlike quit() and exit(), sys. exit() is considered good to be used in production code for the sys module is always available. The optional argument arg can be an integer giving the exit or another type of object. If it is an integer, zero is considered “successful termination”.

Why can't Python find my modules?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.


1 Answers

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.

like image 93
Alex Gaynor Avatar answered Oct 28 '22 17:10

Alex Gaynor