Filling python list in C++ class

Hello,

I use the ROOT feature of exporting C++ classes into python a lot (generating cint dict + compile libs).
So far I have been only “using C++ class in python” and not the other way around (i.e. operate on PyObject in my C++ function).

Though not an expert I know how to write C/C++ API for python.
I have a problem, however, to compile my source code with:

#include "Python.h"

As to searching the past history of questions, I saw this post:
[url]Dictionary generation including PyROOT C++ Objects
which does work until I use some of API function such as

double res=1.0;
PyObject* pres = PyFloat_FromDouble(res);

which needs Python.h (as far as I know how to do in pure python… sorry for lacking knowledge…).

Is there an example for doing this?
For instance, is there an example C++ source code to fill & return python list or tuple?

Thank you for your time and help.

Kazu

Here’s my test code.
sample.h

#ifndef PYTEST_SAMPLE_H
#define PYTEST_SAMPLE_H
#include <iostream>
/*
// Commented out since including Python.h
struct _object;
typedef _object PyObject;
*/
#include "Python.h"     
class sample{
public:
  sample(){}
  virtual ~sample(){};
  PyObject* get() const
  {
    double res=1.0;
    return PyFloat_FromDouble(res);
  }
};
#endif

LinkDef.h

#ifdef __CINT__
#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;
#pragma link C++ class sample+;
#endif

Hi,

what is the goal? Unless you want to call the Python C-API from a CINT macro or the CINT interpreter, you should put all such calls in a .cxx, not in the .h seen by rootcint.

Cheers,
Wim

Dear Wim,

Sorry for being unclear.
My goal is to operate or return PyObject.
For instance, as I described one particular goal as a wished example, I would like to write a simple C++ function that fills python list with integers and return (and call it from python).
But really any example to get started would be great.

My code snipset has no use case, but just to demonstrate a compiler error once I call #include “Python.h”.
I put function implementation in the header thinking that would be easier for others to read.
As described I included Python.h for using one of API function PyFloat_FromDouble.

Thank you for your time and reply.

Kazu

Ah… so hiding from CINT does the job… thank you very much…

and sorry for taking time to understand your suggestion!

Kazu

Hi,

yes. I mean, if you really insist on implementing everything in the header, then that is still possible. For example:#ifndef PYTEST_SAMPLE_H #define PYTEST_SAMPLE_H #include <iostream> #ifdef __CINT__ struct _object; typedef _object PyObject; #else #include "Python.h" #endif class sample{ public: sample(){} virtual ~sample(){}; PyObject* get() const #ifdef __CINT__ ; #else { double res=1.0; return PyFloat_FromDouble(res); } #endif }; #endifBut it makes life needlessly complicated compared to having a .cxx to go with it.

Cheers,
Wim

Yep. I originally had the function implementation in the source but I did not know hiding #include was the key (actually I didn’t realize it was cint causing a problem).

Thank you very, very much. And sorry for a really boring question (apparent to me now)…

Kazu