2012-02-11 14 views
7

ho il seguente codice:RPython metodi sys non funzionano

import sys 

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

def target(*args): 
    return entry_point, None 

Tuttavia, quando corro python ./pypy/pypy/translator/goal/translate.py t.py ottengo il seguente errore:

... 
[translation:ERROR] Exception: unexpected prebuilt constant: <built-in function exit> 
[translation:ERROR] Processing block: 
[translation:ERROR] [email protected] 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-- 

C'era in realtà più per l'errore, ma Pensavo che solo quest'ultima parte fosse pertinente. Se pensi che potrebbe esserti utile, ti preghiamo di commentare e io modificherò.

In effetti, ottengo un altro errore quando sostituisco sys.exit con qualcosa di ancora più semplice come sys.stdout.write.

import sys 

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

def target(*args): 
    return entry_point, None 

mi da:

... 
[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] [email protected] 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-- 

sono metodi sys semplicemente off limits per RPython? Sembra strano per me perché exit e stdout sono così prontamente disponibili in C. Tuttavia, i messaggi di errore sembrano simili a cose diverse, quindi potrei semplicemente abbaiare dall'albero sbagliato.

Attualmente sto usando la guida this per capire approssimativamente cosa è permesso e non permesso in RPython. Ci sono altri riferimenti piuttosto accessibili che potrei usare per maggiori informazioni?

+2

Mi spiace non posso aiutare ulteriormente, ma permettimi di far notare che l'ultimo esempio è sbagliato che dovresti sostituire: 'sys.std.write' di' sys.stdout.write', non c'è 'sys.std 'in python. – mouad

+0

@mouad Grazie per avermelo fatto notare. Ho provato di nuovo e pubblicato il messaggio di errore aggiornato. – math4tots

risposta

9

Il modulo sys non è RPython, non è possibile utilizzarlo in un programma RPython. Per restituire un codice di stato, è necessario restituirlo direttamente dalla funzione entry_point.

Non è inoltre possibile utilizzare sys.stdout/sys.stdin/sys.stderr, è necessario leggere/scrivere utilizzando le funzioni os.read/os.write combinate con un descrittore di file.

+0

Se sys non è disponibile, come posso ottenere i descrittori di file stdin/stdout in modo che possa passarli su os.read? Sono gli unici modi per ottenere stdin/stdout tramite raw_input/print? – math4tots

+0

stdin = 0, stdout = 1, stderr = 2. Puoi semplicemente chiamare os.write/os.read con questi argomenti interi. –

Problemi correlati