[C++-sig] Embedding...

Beau Sapach beau.sapach at ualberta.ca
Wed Oct 25 19:22:05 CEST 2006


Hello everyone,

Thanks Stefan for your help! I know I'm posting a lot to this list, but I
must admit I'm having a hard time wrapping my head around boost.python.

If I want to make an instance of an object in C++ available to a python
interpreter within the same .exe I must first expose the class correct?
Using the same method described in the tutorial?

Once that's done could I expose a global function (again within the same
exe) that would return a pointer to an instance of the previously exposed
class?  Or is there an easier way of simply passing this to python?

For example if I have a window called a workspace that the user interacts
with, could I expose the workspace class and have a global function called
GetWorkspace() that returns a pointer to the C++ instance of workspace, thus
allowing the user to manipulate the window in python?

Beau

-----Original Message-----
From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org] On
Behalf Of Stefan Seefeld
Sent: Monday, October 23, 2006 1:35 PM
To: Development of Python/C++ integration
Subject: Re: [C++-sig] Embedding...

Beau Sapach wrote:
> Hello Everyone,
> 
> 
> I'm looking to embed Python in an application I'm working on in C++.  I've
> been trying out Boost.Python and it seems pretty straight forward but
> doesn't have (that I've found yet anyway) any complete examples to follow
> for embedding Python.  What I want to be able to do is give users access
to
> objects created in C++ land inside a Python window within my .exe.

Have you looked at the code from CVS ? I added some support for embedding
sometime in 2005 (after the 1.33 release branch was created), containing
'exec', 'exec_string', and 'import' functions:

http://cci.lbl.gov/~rwgk/shortcuts/boost//libs/python/doc/v2/exec.html

(The docs at boost.org don't include these bits yet as they only document
the last release.)

> I'm assuming I'll have to create instances of my classes in C++, for
example
> when my application is starting up. Then I would have to create a Python
> interpreter and have it call Python functions to give it references to my
> previously created C++ objects. Lastly, show the Python window to the
user.

Right. One way is to have preexisting (C++) objects be bound to python
objects that get exposed to user scripts that are run from within the main
(C++) application by means of an embedded python interpreter.

> Please correct me if I'm wrong in how I'm imagining this... The problem
I'm
> having right now is that Boost.Python seems very good for creating DLLs to
> give Python access to classes and functions, but how do I make
> classes/functions that are compiled as part of my exe available to python
> WITHIN the same exe???

See above. The bits to actually provide the missing link are straight
forward,
so you may put it into your own code, if you can't use the latest
not-quite-released-yet
version of boost:

// Execute python source code from file filename.
// global and local are the global and local scopes respectively,
// used during execution.
object 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));
}

Good luck,
		Stefan

-- 

      ...ich hab' noch einen Koffer in Berlin...
_______________________________________________
C++-sig mailing list
C++-sig at python.org
http://mail.python.org/mailman/listinfo/c++-sig





More information about the Cplusplus-sig mailing list