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

Faheem Mitha faheem at email.unc.edu
Thu Jul 1 05:43:16 CEST 2004


On Tue, 29 Jun 2004 11:51:48 -0400, David Abrahams
<dave at boost-consulting.com> wrote:

> Faheem Mitha <faheem at email.unc.edu> writes:
>
>> On Mon, 28 Jun 2004 15:56:13 -0500, Chad Austin <caustin at gmail.com> wrote:
>>> On Mon, 28 Jun 2004 17:48:59 +0000 (UTC), Faheem Mitha
>>> <faheem at email.unc.edu> wrote:
>>>> 
>>>> On Sun, 27 Jun 2004 23:46:06 -0500, Chad Austin <caustin at gmail.com> wrote:
>>
>>> Nope, sorry...  I had a problem similar to that in that __builtins__
>>> wasn't getting set in my modules, but I was doing some weird stuff. 
>>> You may want to at least check that __builtins__ is set in your
>>> script.
>>
>> How should I do that? I thought that
>>
>> handle<> main_module(borrowed( PyImport_AddModule("__main__") ));
>> handle<> main_namespace(borrowed( PyModule_GetDict(main_module.get())));
>>
>> should do the trick, though I don't really understand what these lines
>> do. 
>
> What specifically are you confused about? Did you check the docs?  

Yes. Lots. I've got multiple windows open in my (tabbed) browser) with
various bits of the tutorial, the reference manual, and various
googled past messages from the mailing list, and I've been looking at
these all weekend. There are various things that are fuzzy, but for
the moment I'd just like to concentrate on getting an example compiled
and working.

> I don't know why you'd do it that way when you can write:

Well, this is direct from the tutorial.

>   object main_module(handle<>(borrowed( PyImport_AddModule("__main__"))));
>   object main_namespace = main_module.attr("__dict__");
>
> instead.

This does not compile with gcc 3.3.4 on Debian. I get the following
errors:

embed.cc:29: error: variable declaration is not allowed here
embed.cc:30: error: request for member `attr' in `main_module', which
is of non-aggregate type `boost::python::api::object ()()'
error: command 'gcc' failed with exit status 1

with the code

*************************************************************************
#include <boost/python.hpp>
using namespace boost::python;

void foo()
{
  Py_Initialize(); 
  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())));    
    Py_Finalize();
}

BOOST_PYTHON_MODULE(embed)
{
  def("foo",foo);
}
*************************************************************************

I thought that perhaps I had the wrong compiler flags, so I tried it
with bjam and the Boost Build system, and I get the same errors.

However, if I replace

object main_module(handle<>(borrowed
	(PyImport_AddModule("__main__"))));

by  

object main_module = object(handle<>(borrowed
	( PyImport_AddModule("__main__"))));

as suggested in a recent post of yours, it compiles. If this is a gcc
bug, as you say, has it been reported? If you can briefly explain to
me what the bug is, I'd be happy to send in a report, unless it has
already been fixed in gcc 3.4. (Can anyone check this?)

However, like the earlier code I was using, it fails to run.

Ie. I get the following errors when I try to load the code:
***************************************************************************
In [2]: embed.foo()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)

/home/faheem/wc/corrmodel/boost/<console>

/home/faheem/wc/corrmodel/boost/<string>

NameError: name 'file' is not defined
*****************************************************************************

It cannot find the built-in `file'. Any suggestions about what is wrong
here, and how to fix it? I am keen to try and get at least some basic
examples of embedding working.

Can anyone reproduce this problem on Linux with gcc?

Unfortunately, I have other problems with the version I compiled using
Boost Build, so I can't currently test that. (Subject for another
message.)

I think that once I have a few examples working on Debian, I will
contribute basic examples along with build instructions to the Debian
package, if they will take it. It may be useful for other people.

>> I wonder if the fact that I am calling the interpreter inside an
>> extension module is causing any problems.

> Certainly not.  Calling the interpreter inside an extension module
is normal.

Good to know. Well, if that is not the problem, I am not sure what it
could be. Please help.

                                                             Faheem.





More information about the Cplusplus-sig mailing list