CXX extension types question

Tom NoSpam at NoSpam.com
Sat Apr 28 18:05:39 EDT 2001


"Greg Landrum" <gReGlAnDrUm at earthlink.net> wrote in message
news:9cf7sr$vue$1 at slb4.atl.mindspring.net...
> I'm working on wrapping a series of C++ classes using CXX.  I'm using a
CVS
> pull of CXX (because I wanted the updated examples + the VisualC project
> files).  In case it makes any difference, I'm using Python2.0 and working
> under Win2K.
>
> Things were going swimmingly (CXX is really quite convenient), but I have
> run up against a little wall and was hoping someone could tell me how to
> climb it.
>
> I've got a single extension module which defines a number of extension
types
> (PyAtom, PyMol, PyBond, etc.), the problem arises when I want to call
> methods of one extension type using arguments of another type.
> For example, PyBond has a method FindOtherAtom which takes a PyAtom as an
> argument.  I've got:
>
> Py::Object PyBond::FindOtherAtom(const Py::Tuple& args){
>   Py::Object o(args[0]);
>   // other crap
> }
>
> The problem I'm having is converting o (the Py::Object) into a PyAtom (or
> using some other method of getting the PyAtom from the args).  I've tried
a
> number of ways of doing this, but they've pretty much all failed.

In args[0] you have a PyObject*.
If the PyObject is actually one of your PyAtoms, you can cast it as such.
(PyAtom inherits - I'm assuming - from cxx::PythonExtension<> which inherits
from PyObject.)
Unfortunately, we can't do a safe 'dynamic_cast' because the inheritance of
PyObject is non-virtual, so we'll do it the C way:
    assert( thePyObject->ob_type == PyAtom::type_object() );    // or throw.
    PyAtom* pPyAtom = static_cast< PyAtom* >( thePyObject );

I think this is right, but no guarantees.  Tell me if I'm wrong.

Tom.

> Does anyone have any advice on how to handle this?  A pointer to a more
> appropriate destination for the question would also be welcomed.
>
> Thanks,
> -greg
>
>
>
>
>





More information about the Python-list mailing list