Is PyRun_String() broken?

Thomas Wouters thomas at xs4all.net
Tue Jun 5 08:53:28 EDT 2001


On Tue, Jun 05, 2001 at 03:40:55AM -0700, David Gravereaux wrote:

> I tried Py_eval_input, but I can't send it arbitrary code, such as "import
> sys".

That's because import is a statement, not an expression. See below. If you
want to import a module and get the module object, use '__import__()'. 

> If I use Py_file_input, PyRun_String accepts the code, but no return value is
> used.  Just some string that say 'None'.  I'll assume that means "no exception
> generated".

No, it means 'No return value'. There is a big difference between
'statements' and 'expressions', in Python. Statements can't be chained or
nested (unless the statement explicitly allows that, like the assignment
statement) and they do not return a value. You can't "eval" statements, only
expressions.

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list