[C++-sig] Creating a python class from c++

James Healey healeyjames at yahoo.co.uk
Wed Nov 15 16:30:53 CET 2006


> 
> True, it has been added to boost.python sometime
> 2005,
> but unfortunately didn't make it into the 1.33
> release.
> 
> If you can't wait a current snapshot, you can take a
> verbatim copy of the function:
> 
> // Execute python source code from file filename.
> // global and local are the global and local scopes
> respectively,
> // used during execution.
> object BOOST_PYTHON_DECL exec_file(str filename,
> object global, object local)
> {
>   // should be 'char const *' but older python
> versions don't use 'const' yet.
>   char *f = python::extract<char *>(filename);
>   // Let python open the file to avoid potential
> binary incompatibilities.
>   PyObject *pyfile = PyFile_FromString(f, "r");
>   if (!pyfile) throw
> std::invalid_argument(std::string(f) + " : no such
> file");
>   python::handle<> file(pyfile);
>   PyObject* result =
> PyRun_File(PyFile_AsFile(file.get()),
>                 f,
>                 Py_file_input,
>                 global.ptr(), local.ptr());
>   if (!result) throw_error_already_set();
>   return object(detail::new_reference(result));
> }
> 
> HTH,
> 		Stefan

When I run the PyRun_File command result comes back as
NULL. The only thing im doing before calling exec_file
is Py_Initialize();

Regards.
Jimmy.

Send instant messages to your online friends http://uk.messenger.yahoo.com 



More information about the Cplusplus-sig mailing list