Python / C++ embed halfway working - question about constructors

jpw johnwalker3301 at comcast.net
Wed Mar 26 21:33:54 CET 2008


I am running  my C++ / Python App on a MacBook Pro with 2.5 installed

I have had some success in that I can load a module, get the class,
get the reference, but when the python class calls another python
class I don't ever seem to get the object back from the constructor.

I am doing the following in the C++ file:

Py_Initialize()
PyObject* module = PyImport_Import(moduleName);
PyObject* class = PyObject_GetAttrString(module, className);

Py_DECREF(module);
Py_DECREF(class);

// Create the TSAFE object constructor arguments:
// a file, and an int
PyObject* tsafeOutputFile = PyFile_FromString("TsafeOutput", "a");
PyObject* resolveValue = PyInt_FromLong(0);

PyObject* classArgumentTuple = PyTuple_New(2);
PyTuple_SetItem(classArgumentTuple, 0, tsafeOutputFile);
PyTuple_SetItem(classArgumentTuple, 1, resolveValue);

classReference = PyEval_CallObject(class, classArgumentTuple);
Py_MY_INCREF(classReference);
Py_MY_DECREF(class);

PyObject* pythonMethod = PyObject_GetAttrString(classReference,
    "registerFlight");

registerReturn = PyEval_CallObject(pythonMethod, argumentTuple);
Py_MY_DECREF(pythonMethod);
Py_MY_DECREF(argumentTuple);


Essentially the code above should:

     call class constructor
     call registerFlight()
            calls registerFlight_()
                 calls _flightPointer() -> where I never get by the
section of
                                                    code where the
Flight.py Constructor
                                                    get called

Each flight should be added to my class object to build up a
dictionary
of flights.

The python class methods getting called are detailed below and I never
get by the
call to _flightPointer().  I have opened a file and write to it at
each step of
code and I never get all the way through the _flightPointer() method

Is there something I am missing here I cannot print the data out of
the
__init__ method in the Flight.py file?

I did not include all of the code here because I am hoping the issue
is I
have got something wrong and it's not the python code but me.

Ant help will be greatly appreciated.

def registerFlight(self, ID, ACtype="?", IFR=1, RVSM=0, ATCcat="?",
        filedAlt=0, filedSpeed=0):
        "register flight (unitless args for outside client)"

        filedAlt *= FL # FL = Flight Level = 100 ft
        filedSpeed *= kn # kn = knots

        self.registerFlight_(ID, ACtype, IFR, RVSM, ATCcat,
filedAlt,filedSpeed)

def registerFlight_(self, ID, ACtype="?", IFR=1, RVSM=0, ATCcat="?",
        filedAlt=0, filedSpeed=0):
        "register flight (accepts arguments with units)"

        flight = self._flightPointer(ID)

        flight.registerFlight(ACtype, IFR, RVSM, ATCcat, filedAlt,
filedSpeed)

 def _flightPointer(self, ID, track=0, Flight=Flight):
        "manage tracked and untracked flight lists"

        fltPointerFile = open('TSAFE_flightPointerOut', 'a')
        fltPointerFile.write("In the _flightPointer function\n")

        flights = self.flights
        unflights = self.untrackedFlights

        if ID in flights:
            fltPointerFile.write("in first IF\n")
            return flights[ID]

        if track: # new data record is a track update - start tracking
            if ID in unflights: # move flight to tracked status
                flights[ID] = unflights[ID]
                del unflights[ID]
            else: flights[ID] = Flight(ID) # new Flight object
            return flights[ID]

        ## THIS IS THE SECTION OF CODE NOT RETURNING A REFERENCE TO
THE Flight object
        if ID not in unflights:
            fltPointerFile.write(ID)
            fltPointerFile.write("  In if ID not in unflights\n\n")

            unflights[ID] = Flight(ID)

            fltPointerFile.write("BACK FROM CALLING Flight(ID)\n")

            unflightsValue = ("unflights[ID]", unflights[ID])
            unflightsString = str(unflightsValue)
            fltPointerFile.write("Return value is ")
            fltPointerFile.write(unflightsString)
            fltPointerFile.write('\n')

        return unflights[ID]



More information about the Python-list mailing list