[C++-sig] Re: compile errors with embedding example from tutorial

Chad Austin caustin at gmail.com
Fri Jul 2 17:43:29 CEST 2004


Faheem:  I think you're confused about one point.  All extension
modules "embed the interpreter".  Since Python has loaded them, they
all have access to the Python/C API and can do what they wish with it.
 Your real problem is that you don't seem to have the 'file' builtin. 
You should put more work into trying to decompose and solve that
problem.

Now that I think about it, perhaps your extension module doesn't have
direct access to the builtins, while __main__ does.  Try changing your
hard-coded script to:

          "from __builtin__ import *\n"
          "hello = file('hello.txt', 'w')\n"
          "hello.write('Hello world!')\n"
          "hello.close()"

Chad

On Fri, 2 Jul 2004 15:27:08 +0000 (UTC), Faheem Mitha
<faheem at email.unc.edu> wrote:
> 
> On Fri, 02 Jul 2004 14:57:27 +0800, Joel de Guzman
> <joel at boost-consulting.com> wrote:
> 
> > Faheem Mitha wrote:
> >
> >> However, I still cannot get a Python extension module with the
> >> interpreter imbedded to work, however. I still get the same error as
> >> before.
> >
> > Hi Faheem,
> >
> > The code in the tutorial can be found in the test/embedding.cpp/py
> > You can find a function called test_tutorial() there:
> >
> > void
> > test_tutorial()
> > {
> >      using namespace boost::python;
> >
> >      object main_module(
> >          handle<>(borrowed(PyImport_AddModule("__main__"))));
> >
> >      object main_namespace = main_module.attr("__dict__");
> >
> >      handle<>(PyRun_String(
> >
> >          "hello = file('hello.txt', 'w')\n"
> >          "hello.write('Hello world!')\n"
> >          "hello.close()"
> >
> >        , Py_file_input
> >        , main_namespace.ptr()
> >        , main_namespace.ptr())
> >      );
> >}
> >
> > AFAIK, the code is passing the regression tests. Perhaps you can use
> > the test example as a staring point?
> 
> Thanks. However, I am not sure which version of the test/embedding.cpp
> you are referring to. The version at
> http://www.boost.org/libs/python/test/embedding.cpp which I assume is
> the current CVS, is the same as the version in 1.31, and neither of
> them have the code you refer to.
> 
> In any case, does the code you refer to define an extension module
> which embeds the interpreter and can be called from Python? Because
> that is the case I am interested in. I have been unable to find any
> examples out there along these lines, despite some effort.
> 
> The current version of test/embedding.cpp that I have seen is a C++
> executable, and the extension module defined there is called within
> the interpreter that is embedded in this executable. This is not the
> same as what I am trying to do.
> 
>                                                           Faheem.
> 
> 
> 
> 
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
> 


-- 
Chad Austin
http://aegisknight.org/




More information about the Cplusplus-sig mailing list