Differences between revisions 2 and 3
Revision 2 as of 2002-12-12 18:52:19
Size: 1537
Editor: MikeRovner
Comment: Instance dictionary creation example
Revision 3 as of 2003-01-10 02:48:08
Size: 2093
Editor: MikeRovner
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
== Intro ==

Python provides a 'Python/C API' that allows you to create C/C++
extension modules and to embed the Python interpreter in your C/C++
programs. Pointers to Py``Objects are the Python/C API's way to use Python
objects from the C/C++ side.

BoostPython uses advanced C++ techniques to provide a much easier
interface to the Python/C API. One manifestation of this is in
python::object which is basically a (very) convenient, high-level
wrapper around Py``Object*. A lower level wrapping around Py``Object* is
python::handle.

Intro

Python provides a 'Python/C API' that allows you to create C/C++ extension modules and to embed the Python interpreter in your C/C++ programs. Pointers to PyObjects are the Python/C API's way to use Python objects from the C/C++ side.

BoostPython uses advanced C++ techniques to provide a much easier interface to the Python/C API. One manifestation of this is in python::object which is basically a (very) convenient, high-level wrapper around PyObject*. A lower level wrapping around PyObject* is python::handle.

Memory consumption

In general, a wrapped C++ object with a corresponding Python object is the size of:

  • a new-style class (derived from 'object' in Python) instance plus
  • the extra size required to allow variable-length data in the instance, plus
  • the size of the C++ object, plus
  • the size of a vtable pointer, plus

  • a pointer to the C++ object's instanceholder, plus
  • zero or more bytes of padding required to ensure that the instanceholder is properly aligned.

You can see this in boost/python/object/instance.hpp. Most Python objects are represented by instance<value_holder<T> >, for some C++ class T.

All the code for implementing C++ object wrappers is in libs/python/src/object/class.cpp.

Instance dictionaries are created only "on demand", the first time the instance's dict attribute is accessed (see instance_get_dict).

    >>> a = A()  # some extension class A, no instance dict
    >>> a.x      # Attribute lookup fails, still no instance dict
    Traceback ...

    >>> a.y = 1  # y is a C++ data member, still no instance dict
    >>> a.x = 1  # creates an instance dict
    >>> z = A()
    >>> z.__dict__  # also creates an instance dict

If your C++ data structure contains pointers or smart pointers, you can arrange for Python objects to be created which only embed those pointers (instance<pointer_holder<Ptr> >). These Python objects will be in existence only as long as your Python code holds a reference to them.

boost.python/InternalDataStructures (last edited 2008-11-15 13:59:46 by localhost)

Unable to edit the page? See the FrontPage for instructions.