[C++-sig] Traceback error: Pointer

Arunie arunie at gmail.com
Tue May 31 18:23:40 CEST 2005


Hey,
I keep running into this error and I can't figure out what it is that's 
going wrong. I wrote a code for C++, where it basically registers an 
interface. It's a test for now, so it goes out and gets the interface, 
registers it, and then unregisters it. Then I wrote a python file that does 
the same thing, and it uses SWIG to talk to C++ files. All this is done 
compiling with the Makefile. But when I try to run the python file, I keep 
getting this error that drives me nuts.

I did not include the files generated by the Makefile commands, as they 
shouldn't be edited (I could be wrong).
The original files are as follows : 

==========================================
//
// Example.cpp
//

#include <iostream>

#include "Example.h"

ExampleClass::ExampleClass() {
std::cout << __PRETTY_FUNCTION__ << "\n";
this->init();
}

ExampleClass::~ExampleClass() {
std::cout << __PRETTY_FUNCTION__ << "\n";
}

void ExampleClass::registerInterface(ExampleInterface* newInterface) {
std::cout << __PRETTY_FUNCTION__ << "\n";
this->interface = newInterface;
}

void ExampleClass::callInterface() {
std::cout << __PRETTY_FUNCTION__ << "\n";
if (this->interface) {
this->interface->doMe();
}
else {
std::cout << "no callback currently registered\n";
}
}

void ExampleClass::unregisterInterface() {
std::cout << __PRETTY_FUNCTION__ << "\n";
this->init();
}

void ExampleClass::init() {
std::cout << __PRETTY_FUNCTION__ << "\n";
this->interface = 0;
}
==============================================================


===========================
//
// Example.h
//

#ifndef _EXAMPLE_H_
#define _EXAMPLE_H_

class ExampleInterface {

public:
virtual void doMe() = 0;

};

class ExampleClass {

public:
ExampleClass();
~ExampleClass();
void registerInterface(ExampleInterface* newInterface);
void callInterface();
void unregisterInterface();

private:
void init();

private:
ExampleInterface* interface;

};

#endif
==================================================


========================================================
//
// Example.i
//

//%module("directors=1") Example
%module Example

%{
#include "Example.h"
%}



//%feature("director") ExampleInterface;

class ExampleInterface {
public:
virtual void doMe() = 0;
};

class ExampleClass {
public:
ExampleClass();
~ExampleClass();
void registerInterface(ExampleInterface* newInterface);
void callInterface();
void unregisterInterface();
};
================================================================


=============================================================
//
// Makefile
//

NAME = Example

ALL = _${NAME}.so test${NAME}

SRCS = ${NAME}.cpp test${NAME}.cpp
OBJS = ${SRCS:.cpp=.o}

CPP = g++
SRC_CFLAGS = -Wall
SWIG_CFLAGS = -fpic -I/usr/include/python2.3
LDFLAGS = -shared

all: ${ALL}

clean:
rm -f ${ALL} ${NAME}_wrap.cpp ${NAME}.py ${NAME}.pyc *.o *.doc *~

test: ${ALL}
@echo "Running test${NAME}..."
./test${NAME}
@echo
@echo "Running test${NAME}.py..."
./test${NAME}.py

_${NAME}.so: ${NAME}.o ${NAME}_wrap.o
${CPP} ${LDFLAGS} -o $@ $^

${OBJS}: %.o: %.cpp
${CPP} ${SRC_CFLAGS} -c -o $@ $<

${NAME}_wrap.o: ${NAME}_wrap.cpp
${CPP} ${SWIG_CFLAGS} -c -o $@ $<

${NAME}_wrap.cpp: ${NAME}.i
swig -python -c++ -o $@ $<

test${NAME}: %: %.o ${OBJS}
${CPP} -o $@ $^
============================================================


================================================================
//
// testExample.cpp
//

#include <iostream>

#include "Example.h"

class TestInterface : public ExampleInterface {
public:
void doMe();
};

void TestInterface::doMe() {
std::cout << __PRETTY_FUNCTION__ << "\n";
};

int main(int argc, char** argv) {

ExampleClass myObject;
TestInterface myInterface;

myObject.registerInterface(&myInterface);
myObject.callInterface();
myObject.unregisterInterface();

}
===============================================================


AND OFCOURSE, MY PROBLEM:
============================================================
#!/usr/bin/python

import Example

class TestInterface(Example.ExampleInterface):
def __init__(self):
print "TestInterface::__init__()"
def doMe(self):
print "TestInterface::doMe()"

myObject = Example.ExampleClass()
myInterface = TestInterface()

myObject.registerInterface(myInterface)
myObject.callInterface()
myObject.unregisterInterface()
=================================================================

I run Red Hat Enterprise Linux 3.
Python - 2.3.4-14
SWIG - 1.3.21-6

Now I run the command: make all and then the command: make test

This is the error I get:
-----------------------------------------------------------------------------
Running testExample...
./testExample
ExampleClass::ExampleClass()
void ExampleClass::init()
void ExampleClass::registerInterface(ExampleInterface*)
void ExampleClass::callInterface()
virtual void TestInterface::doMe()
void ExampleClass::unregisterInterface()
void ExampleClass::init()
ExampleClass::~ExampleClass()

Running testExample.py...
./testExample.py
make: ./testExample.py: Command not found
make: *** [test] Error 127
-----------------------------------------------------------------------------------

And so I figured since ./testExample.py isn't going properly, I run the 
command: python testExample.py
Results are this:
-------------------------------------------------------------------------------
ExampleClass::ExampleClass()
void ExampleClass::init()
TestInterface::__init__()
Traceback (most recent call last):
File "testExample.py", line 14, in ?
myObject.registerInterface(myInterface)
File "/root/Originals/Example.py", line 68, in registerInterface
def registerInterface(*args): return 
_Example.ExampleClass_registerInterface(*args)
TypeError: Expected a pointer
ExampleClass::~ExampleClass()
----------------------------------------------------------------------------------

I checked the file Example.py, but found nothing. I can't figure out where 
I'm going wrong.
Any help would be appreciated.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20050531/5d751726/attachment.htm>


More information about the Cplusplus-sig mailing list