Boost.Python C++ pointer object to python: Attribute does not exist.

Hi,
I'm struggling with the following issue in a boost.python implementation:
I have a list of std::shared_ptr in C++, I am accessing this list from
Python.
- When I access an element in the list, the elements does not have any
attributes apparently. I get an error trying to access an attribute,
hasattr returns false on the attribute, but dir informs me that it exist.
- When I change the C++ implementation from shared_ptr to raw ptr's
everything works as expected.
- I want to use shared_ptrs for what I feel: obvious reasons.
Below is the C++ code and the Python code.
###################
### garage_ext.cpp ###
###################
#include<memory>
#include<vector>
namespace boost { template<class T> T* get_pointer(std::shared_ptr<T>& p){
return p.get(); }}
struct Vehicle{
virtual ~Vehicle(){}
friend bool operator==(const Vehicle& lhs, const Vehicle& rhs) { return
true; }
int testAttribute;
};
struct Boat: public Vehicle {
virtual ~Boat(){}
friend bool operator==(const Boat& lhs, const Boat& rhs) { return true; }
int testBoatAttribute;
};
struct Garage {
friend bool operator==(const Garage& lhs, const Garage& rhs) { return
true; }
std::vector
##################### ### Python test code ### ##################### g = Garage() l = g.boatList() newboat = Boat() l.append(newboat) boatFromList = l[0] print(type(boatFromList)) print(str(hasattr(newboat,"testBoatAttribute"))) dir(boatFromList) # The following line throws the error. print(str(hasattr(boatFromList,"testBoatAttribute"))) I created a cast method for boat. If I cast l[0] to Boat using: boatFromList = Boat.cast(l[0]) print(str(hasattr(boatFromList,"testBoatAttribute"))) works. This is all very strange to me. It feels very buggy. Could someone please help me clarify the reason for this behavious? Regards, Christoff
participants (1)
-
Christoff Kok