Boost logo

Boost :

From: Thomas Malik (Thomas_Malik%LGBANK_at_[hidden])
Date: 2001-09-05 03:12:03


Hi,
I am new to this group,
i am about to write yet another C++ API interface generator for python
(there are
still too few out there!)
However, i found i needed an additional conversion to/from unsigned
character
strings.
Could these be added to boost::python (i am using boost 1.23.0) ?

in conversions.hpp:
PyObject* to_python(const unsigned char* s);
const unsigned char* from_python(PyObject*, boost::python::type<const
unsigned char*>);

inline PyObject* to_python(const unsigned char* s)
{
  if (s) {
    return PyString_FromString(reinterpret_cast<const char *>(s));
  } else {
    return boost::python::detail::none();
  }
}

in conversions.cpp:

const unsigned char* from_python(PyObject* p, boost::python::type<const
unsigned char*>)
{
  if (p == Py_None) {
    return (const unsigned char*)0;
  } else {
    const char* s = PyString_AsString(p);
    if (!s)
        throw boost::python::argument_error();
    return reinterpret_cast<const unsigned char*>(s);
  }
}

also, i found it more useful to pass a null pointer to a function expecting
a const char*, if
specifying None as an argument (see above):

const char* from_python(PyObject* p, boost::python::type<const char*>)
{
  if (p == Py_None) {
    return (const char*)0;
  } else {
    const char* s = PyString_AsString(p);
    if (!s)
        throw boost::python::argument_error();
    return s;
  }
}

could the part in conversions.cpp be replaced by this ?

Also, i don't have much of an idea how to wrap pointers to C++ class
objects in a way such that
i don't have to wrap the whole class in boost python. This is especially
needed for wrapping pointers
to opaque structs, like the XWindows Display data type. Currently i am
using this:

struct _XDisplay {
};

typedef struct _XDisplay Display;

PyObject* to_python(struct _XDisplay* p)
{
  return PyCObject_FromVoidPtr(p,NULL);
}

struct _XDisplay* from_python(PyObject* p, python::type<struct
_XDisplay*const&>)
{
  return reinterpret_cast<struct _XDisplay*>(PyCObject_AsVoidPtr(p));
}

which doesn't look like an exactly clean solution to me. any ideas ?
Lastly, in the API i am trying to wrap, const and non-const pointers to a
wrapped
class are returned as method arguments all the time.
The boost documentation (libs/python/doc/pointers.html) proposes:

BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE // this is a gcc 2.95.2 bug
workaround
  PyObject* to_python(Foo* p)
  {
      return
boost::python::python_extension_class_converters<Foo>::ptr_to_python(p);
  }

  PyObject* to_python(const Foo* p)
  {
      return to_python(const_cast<Foo*>(p));
  }
BOOST_PYTHON_END_CONVERSION_NAMESPACE

i didn't see any ptr_to_python method, i assume smart_ptr_to_python was
meant.
However, i didn't find documentation and didn't understand everything on
this.
Could anyone clarify this to me ?

In the library i am trying to wrap (Trolltech's Qt)
trees of QObject (the class i have wrapped) are built. The problem is, in
some cases,
these objects are instantiated both by the API or by me (from python) and
destroyed both
by me or by the API. I made a subclass of QObject (as proposed for
overriding virtual
methods in python), this also works so far.
Both Qt-created objects and python-created objects may be returned
by Qt methods. Of course i need to somehow keep track of these objects.
My question is, how is an object created and returned by the C++ API
wrapped into a python
object ? And how can i make sure that a particular C++ object returned by
the API is identified
by a unique Python object (such that i can compare them by the python 'is'
operator) ?
Is there an 'object registry' or similar ?

Thanks in advance.

--
Thomas Malik
Landesbank Baden-Wuerttemberg (Stuttgart, Germany)
Abt. 2340 Tel. (+49 711) 124-7049
Thomas_Malik%lgbank_at_[hidden]
______________________________________________________________________
--------------------------------------------------------------------------------------------
Bitte beachten Sie, dass der Inhalt dieser E-Mail einschließlich eventuell
angehängter Dokumente vertraulich ist. Falls Sie nicht der angegebene
Empfänger sind oder falls diese E-Mail irrtümlich an Sie adressiert wurde,
dürfen Sie die E-Mail und eventuell angehängte Dokumente weder öffnen,
lesen, kopieren, verbreiten noch ihren Inhalt in irgendeiner Weise nutzen.
Bitte verständigen Sie den Absender sofort und löschen Sie die E-Mail
sodann.
Die Sicherheit von Übermittlungen per E-Mail kann nicht garantiert werden.
Per E-Mail übermittelte Informationen können abgefangen oder geändert
werden, verloren gehen oder zerstört werden, verspätet oder unvollständig
ankommen, oder Viren enthalten. Der Absender übernimmt daher keine Gewähr
für Irrtümer oder Auslassungen jeder Art im Inhalt sowie sonstige Risiken,
die auf die Übermittlung per E-Mail zurückzuführen sind. Falls Sie eine
Bestätigung wünschen, fordern Sie bitte den Inhalt der E-Mail als Hardcopy
an.
This e-mail and any attached files are confidential. If you are not the
named addressee or if this transmission has been addressed to you in error,
any disclosure, reproduction, copying, distrubtion, or other dissemination
or use of this communication is prohibited. If you have received this
transmission in error please notify the sender immediately and then delete
this e-mail.
E-mail transmission cannot be guaranteed to be secure or free from error as
information could be intercepted, corrupted, lost, destroyed, arrive late
or incomplete, or contain viruses. The sender therefore does not accept
liability for any errors or omissions in the contents of this message or
any other of such risks which arise as a result of e-mail transmission. If
verification is required, please request a hard copy version.
---------------------------------------------------------------------------------------------

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk