[C++-sig] Redirecting stdout for B.P extensions?
Mike Rovner
mrovner at propel.com
Thu Sep 15 01:24:16 CEST 2005
Hubert Holin wrote:
> Paris (U.E.), le 14/09/2005
> However, were I to embed Python, I assume I could swap out
> cout's file descriptor and both the Python scripts and subsequent C++
> code would be none the wiser, but if instead I extend Python (as is
> the case in my current project), I either have to find a way to
> access the Python executable's cout file descriptor (how would I do
> that? by using a C extension?), or have to make every script and
> extension aware of an additional parameter (be it a pipe or whatever,
> to redirect to) and code explicitly for that (i.e. it would not be
> transparent at al).
>
> Is that correct?
It will not be transparent in either case as you depend on Python notion
of sys.stdout.
You can access the Python executable's cout file descriptor as (untested!):
{
using namespace boost::python;
object sys(PyImport_ImportModule("sys"));
object sys_stdout = sys.attr("stdout");
if( PyFile_Check(sys_stdout.ptr()) )
{
FILE* cout_file = PyFile_AsFile(sys_stdout.ptr());
// ...
}
else
{
throw PythonStdoutNotAfile();
}
}
More information about the Cplusplus-sig
mailing list