[C++-sig] sharing code between different python extensions

2014-10-23 Thread Wintersberger, Eugen
Hi there
  I have a little problem with two Python extensions (C++ with
boost::python) where share code. An example of what I am trying to do is
attached to this mail so I will refer to the code in the tarball to
explain the problem. 

I have to Python extensions ('a' and 'b') each of them exporting two
functions. What I want is to use a function exported by 'a' in the code
of 'b'. Though the package compiles I get an unresolved symbol error
when trying to load 'b'. 

The reason is quite clear. 'b' cannot resolve the symbol as it is
provided by 'a' to which it is not linked. Is it somehow possible with
distutils to link the two extensions to that 'b' can see the symbols
provided by 'a'?

best regards
  Eugen Wintersberger 


boost_python_extensions.tar.gz
Description: application/compressed-tar


signature.asc
Description: This is a digitally signed message part
___
Cplusplus-sig mailing list
[email protected]
https://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] sharing code between different python extensions

2014-10-23 Thread Nat Goodspeed
On Oct 22, 2014, at 8:08 AM, "Wintersberger, Eugen" 
 wrote:

>  I have a little problem with two Python extensions (C++ with
> boost::python) where share code. 
> 
> I have to Python extensions ('a' and 'b') each of them exporting two
> functions. What I want is to use a function exported by 'a' in the code
> of 'b'. Though the package compiles I get an unresolved symbol error
> when trying to load 'b'. 

If I were faced with this situation either in pure Python, or pure C++, I would 
probably break out module 'c' with functions consumed by both 'a' and 'b'.

Could this work in your situation?
> 
___
Cplusplus-sig mailing list
[email protected]
https://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] sharing code between different python extensions

2014-10-23 Thread Stefan Seefeld
On 22/10/14 08:08 AM, Wintersberger, Eugen wrote:
> Hi there
>   I have a little problem with two Python extensions (C++ with
> boost::python) where share code. An example of what I am trying to do is
> attached to this mail so I will refer to the code in the tarball to
> explain the problem. 
>
> I have to Python extensions ('a' and 'b') each of them exporting two
> functions. What I want is to use a function exported by 'a' in the code
> of 'b'. Though the package compiles I get an unresolved symbol error
> when trying to load 'b'. 
>
> The reason is quite clear. 'b' cannot resolve the symbol as it is
> provided by 'a' to which it is not linked. Is it somehow possible with
> distutils to link the two extensions to that 'b' can see the symbols
> provided by 'a'?

Python extension modules may not depend on each other in that way.
(Arguably that is a Good Thing, as it avoids possible ABI compatibility
issues.)
What I suggest you do is either refactor the code such that your
extensions 'a' and 'b' both link to a shared library 'c' which provides
the symbols used by both. Alternatively you could try to reduce the
dependency to only exist at the Python interface level, such that using
'b' requires 'a' being loaded (for example to enable type converters
defined in 'a' but used in 'b'), but without any direct ABI dependencies
between 'a' and 'b'.

 Stefan



-- 

  ...ich hab' noch einen Koffer in Berlin...

___
Cplusplus-sig mailing list
[email protected]
https://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] sharing code between different python extensions

2014-10-23 Thread Wintersberger, Eugen
Hi

On Thu, 2014-10-23 at 08:11 -0400, Nat Goodspeed wrote:
> On Oct 22, 2014, at 8:08 AM, "Wintersberger, Eugen" 
>  wrote:
> 
> >  I have a little problem with two Python extensions (C++ with
> > boost::python) where share code. 
> > 
> > I have to Python extensions ('a' and 'b') each of them exporting two
> > functions. What I want is to use a function exported by 'a' in the code
> > of 'b'. Though the package compiles I get an unresolved symbol error
> > when trying to load 'b'. 
> 
> If I were faced with this situation either in pure Python, or pure C++, I 
> would probably break out module 'c' with functions consumed by both 'a' and 
> 'b'.
> Could this work in your situation?

This approach would not really help me. I guess what I really need is
something like the numpy approach: a way to export the API of an
extension to other extensions. But I have no idea how to do this ;)?

Are there any reference you can suggest which I should read if I want to
follow this approach?

regards
  Eugen

> > 
> ___
> Cplusplus-sig mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/cplusplus-sig



signature.asc
Description: This is a digitally signed message part
___
Cplusplus-sig mailing list
[email protected]
https://mail.python.org/mailman/listinfo/cplusplus-sig

[C++-sig] Exposing simple polymorphism with boost python

2014-10-23 Thread Erik Türke
Hi Python-Experts :-)


i am starting to get really frustrated trying to expose a simple C++
polymorphism to python with boost::python.
I do have the following structure in C++:

struct Base {
int typeID;
};

struct Derived : public Base {
int derivedProperty;
}

//and some more from base derived types

Base *returnSomethingDerivedFromBase(...) {
Derived *ret = new Derived;
ret->derivedProperty = 1234;
return ret;
}

BOOST_PYTHON_MODULE(foo)
{
class_("Base")
.add_property("baseProperty", &Base::baseProperty);

class_ >("Derived")
.add_property("derivedProperty", &Derived::derivedProperty);

def("returnSomethingDerivedFromBase",
returnSomethingDerivedFromBase);
}


And in Python i just want to have the following:

object = returnSomethingFromDerived() #object is of type Base
if object.typeID = 1:
#here i want to cast to Derived and access "derivedProperty"
#but this is not working :-( :
object.__class__ = Derived

Is there a way to accomplish this at all? Or isn´t this possible as it
would be in C++ ?

Thanks a lot for your help!!
___
Cplusplus-sig mailing list
[email protected]
https://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] sharing code between different python extensions

2014-10-23 Thread Wintersberger, Eugen
Hi Stefan
  
> Python extension modules may not depend on each other in that way.
> (Arguably that is a Good Thing, as it avoids possible ABI compatibility
> issues.)
> What I suggest you do is either refactor the code such that your
> extensions 'a' and 'b' both link to a shared library 'c' which provides
> the symbols used by both. 

If the real problem would be as in the example I would definitely go
with this approach (I even tried to do so). However, the real problem is
more complex. The functions in 'a' call Python API and numpy API
functions. Thus, when distutils builds the code it does this according
to a particular Python version. So building a single shared library
would not help too much. 

> Alternatively you could try to reduce the
> dependency to only exist at the Python interface level, such that using
> 'b' requires 'a' being loaded (for example to enable type converters
> defined in 'a' but used in 'b'), but without any direct ABI dependencies
> between 'a' and 'b'.

This is interesting as it is much closer to my real problem. Extension
'a' provides some converters from numpy objects to my own C++ types and
back. And these guys I would like to use in 'b' (along with some other
numpy utility functions). 

regards
  Eugen



signature.asc
Description: This is a digitally signed message part
___
Cplusplus-sig mailing list
[email protected]
https://mail.python.org/mailman/listinfo/cplusplus-sig