PyRun_File

nick_knight at my-deja.com nick_knight at my-deja.com
Wed Aug 9 02:45:50 EDT 2000


Hello,

I am embedding Python into a server which means I require full logging
from python if and when it runs any programs.

I am running the following small test: -
OS Win 95

#include "Python.h"



#include "stdafx.h"
#include <stdio.h>

int main(int argc, char* argv[])
{
    PyObject *pDict;

    //char *filename = "e:\\downloads\\python\\testbed\\test.py";
    Py_Initialize();    //Required

    pDict = PyDict_New();    //Create our instance

    /*
    Don't need to use this bit now - we will
    use for python itself for all of that.
    FILE * OurFile;

    OurFile = fopen(filename,"rb");
    */

    //Not sure what this bit does - sets up the python envioroment
variables I think ??
    PyDict_SetItemString(pDict, "__builtins__", PyEval_GetBuiltins() );
    PyRun_String("import sys", Py_file_input, pDict, pDict);



    PyRun_String("sys.stdout = open('c:/temp.txt','a')",
                                                  Py_file_input, pDict,
pDict);

    PyRun_String("sys.stderr = open('c:/temp1.txt','a')",
                                                  Py_file_input, pDict,
pDict);

    PyRun_String("print 'Hello'", Py_file_input, pDict, pDict);


    PyRun_String("sys.path.insert(0,'e:/downloads/python/Python-
1.5.2/Demo/ibrowse')"
                                                , Py_file_input, pDict,
pDict);

    char *filename = "e:/Downloads/python/testbed/test.py";
    FILE * OurFile = fopen(filename,"r");
    PyRun_File (OurFile, filename, Py_file_input, pDict, pDict);

    PyRun_String("print 'This is absolute !'", Py_file_input, pDict,
pDict);

	return 0;
}


Now if test.py contains no errors the program runs fine - evrything
gets printed to temp.txt. Including output from test.py. But as soon as
there is an error in test.py i.e.

print rubbish
instead of
print "rubbish"

then no error gets printed to stderr (temp1.txt), and all printing to
stdout (temp.txt) stops.

This is true of when not redirecting stdio.

At the very least is it possible to get python to print to stderr ???

Thanks


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list