[C++-sig] CXX : getting a C++ object

Benjamin Golinvaux Benjamin.Golinvaux at euresys.com
Sun Aug 29 00:40:22 CEST 2004


I have found the solution to my problem. 
The base class of the user-defined c++ 
python extension type provides
a static bool check( PyObject* ) method
that checks whether the object is of its
kind.

// somewhere :
class MyExtensionType : public Py::PythonExtension<MyExtensionType>
{
...
}

//somewhere else
Object someFunctionExposedToPython(const Tuple &a)
{
  Object o = a[0]; 
  if(MyExtensionType::check(o.ptr())) {
    MyExtensionType* cppObj = static_cast<MyExtensionType*>(cppObj);
    // do something with cppObj
  }
  else {
    // it's not a MyExtensionType*, do something else
  }
}

(of course this isn't needed for MyExtensionType methods
where "this" is automatically set correctly)
 
this is exactly what i needed. hope this can
help someone else (although it seems everyone
here uses boost :)

Benjamin


-----Message d'origine-----
De : c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org] De la part de Benjamin Golinvaux
Envoyé : Saturday, August 28, 2004 11:48 PM
À : Development of Python/C++ integration
Objet : [C++-sig] CXX : getting a C++ object

Hello

I would like to wrap a small C++ library using CXX.

I would like to know how I can extract the C++ instance from a PyObject passed to a function. Can I assume, after a correct type check, that I can safely downcast the PyObject ? (or using dynamic_cast ?)

If I can't (or don't want to) use dynamic_cast , what could then be the best way to test that a PyObject is actually from a wrapped C++ "python type" class ?  (sorry I'm a beginner in the Python C API)

Thanks for your help

Benjamin Golinvaux 

--------------------------------------------------------
The information contained in this message or any of its attachments may be privileged and confidential and intended for the exclusive use of the addressee. If you are not the addressee any disclosure, reproduction, distribution or other dissemination or use of this communication is strictly prohibited. If you have received this transmission by error please notify the sender immediately and then delete this email. EURESYS shall not be liable for any loss of or damage to revenues, profits, goodwill, data, information systems or other special, incidental, indirect, consequential or punitive damages of any kind arising in connection with the use of information contained in this mail or its attachments. Email transmission cannot be guaranteed to be secure or error free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore is in no way liable for any errors or omissions in the content of this message, which may arise as a result of email transmission. If verification is required, please request a hard copy. Please note that neither EURESYS, nor the sender accepts any responsibility for viruses and it is your responsibility to scan attachments (if any).


--------------------------------------------------------
_______________________________________________
C++-sig mailing list
C++-sig at python.org
http://mail.python.org/mailman/listinfo/c++-sig 

--------------------------------------------------------
The information contained in this message or any of its attachments may be privileged and confidential and intended for the exclusive use of the addressee. If you are not the addressee any disclosure, reproduction, distribution or other dissemination or use of this communication is strictly prohibited. If you have received this transmission by error please notify the sender immediately and then delete this email. EURESYS shall not be liable for any loss of or damage to revenues, profits, goodwill, data, information systems or other special, incidental, indirect, consequential or punitive damages of any kind arising in connection with the use of information contained in this mail or its attachments. Email transmission cannot be guaranteed to be secure or error free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore is in no way liable for any errors or omissions in the content of this message, which may arise as a result of email transmission. If verification is required, please request a hard copy. Please note that neither EURESYS, nor the sender accepts any responsibility for viruses and it is your responsibility to scan attachments (if any).


--------------------------------------------------------



More information about the Cplusplus-sig mailing list