tuple creation in C extensions

Dan Schmidt dfan at harmonixmusic.com
Mon Jun 12 20:30:09 EDT 2000


Peter Schneider-Kamp <petersc at stud.ntnu.no> writes:

| I have to create a tuple (of two integer values) in a
| C extension. At the moment I use the following (rather
| ugly in my eyes) construction:
| 
| t = PyTuple_New(2);
| PyTuple_SET_ITEM(t,0,PyInt_FromLong(a));
| PyTuple_SET_ITEM(t,1,PyInt_FromLong(b));
| PyErr_SetObject(PyExc_IndexError,t);
| 
| where
| PyObject* t;
| long a,b;
| 
| Is there "A Better Way(TM)"?

I just started using CXX and wholeheartedly recommend it, if you're
coding in C++.  This example basically becomes:

  Tuple t(2);
  t[0] = Int(a);
  t[1] = Int(b);

It deals with the reference counts automatically too.

-- 
                 Dan Schmidt | http://www.dfan.org
Honest Bob CD now available! | http://www.dfan.org/honestbob/cd.html



More information about the Python-list mailing list