[PYTHON C++-SIG] Typecasts to/from PyObject?

Johann Hibschman johann at physics.berkeley.edu
Fri Feb 21 22:57:47 CET 1997


Hi all,

I have a style question.  If I define a class around the usual PyObject 
pointer, like

   class CPyObject {
      PyObject *obj;
   public:
      CPyObject( PyObject *o ) : obj(o) {}
   }

would it be a good idea to also define C++->CPyObject data conversions, 
like adding,

      CPyObject( double x ) { obj = PyBuildValue("d", d); }

or CPyObject->C++ conversions, like

      operator double() {
         double x;
         PyArg_Parse( obj, "d", &x );   // add appropriate error handling...
         return x;
      }

or would it just provide a source of confusing bugs?

I'm looking at this in the context of creating a PyDict class.  If I 
define the type conversions, I could write:

   PyList plist;
   plist["spam"] = 3.0;
   x = y + plist["spam"];

while otherwise I would have to write something like

   plist[ toPyObject("spam") ] = toPyObject(3.0);
   x= y + PyObjectToDouble( plist[ toPyObject("spam") ] );

Conversions are dangerous, in that they might do unexpected things, but 
they would make the syntax a lot nicer.

As an example of the bad effects, if a program had a reference to a 
floating-point python object, like

   CPyObject pi( PyBuildValue("d", 3.14159 ) );

then this code would give an error of some kind (I believe):

   double x, y=3.14;
   int i, j;
   x = 2.0 + pi;   // fine, converts CPyObject to double
   x = 2 + pi;     // Would this try to convert CPyObject to int or double?
   i = 2 + pi;     // Would the attempted CPyObject to int conversion work?
   i = 2 + y;      // This is fine, but loses information...

Opinions?

- Johann

P.S.  Yes, in a real version, these conversions would probably be 
implemented through traits-style templates, but I haven't quite thought 
my way all the way through that yet.

Johann Hibschman
johann at physics.berkeley.edu   


_______________
C++-SIG - SIG for Development of a C++ Binding to Python

send messages to: c++-sig at python.org
administrivia to: c++-sig-request at python.org
_______________



More information about the Cplusplus-sig mailing list