Re: [C++-sig] Passing C++ instance to embedded py

2008-12-14 Thread David Abrahams

on Thu Nov 20 2008, fileoffset  wrote:

> To best explain my problem, here is some code:
>
> struct A
> {
>   A()
>   {
>   mTest = 1;
>
>   std::cout << "Test: " << mTest;
>
>   Py_Initialize();
>   object main_module = import("__main__");
>   object main_namespace = main_module.attr("__dict__");
>
>   main_namespace["A_Instance"] = this;
>
>   object result = exec_file(str("test.py"), main_namespace,
> main_namespace);
>
>   std::cout << "Test: " << mTest;
>   }
>
>   int mTest;
> }
>
> in main.cpp or similar:
>
> BOOST_PYTHON_MODULE(MyModule)
> {
> class_("A", init<>())
> .def_readwrite("Test", &A::mTest)
> ;
> }
>
> test.py:
>
> from MyModule import *
>
> A_Instance.Test = 5
>
>
> Essentially I want to be able to access and modify the properties of an 
> instance of a
> C++ class
> at runtime.
>
> When I try code similar to above, python excepts, as I believe it
> attempts to copy-construct the A class instead of just pass the
> reference (this) so that i can modify it.

Correct.

>
> Is what I want possible? If not what would be a better way to do it?

  main_module.attr("A_Instance") = ptr(this);
  object d = main_module.attr("__dict__");
  object result = exec_file(str("test.py"), d, d);
  main_module.attr("A_Instance") = object(); // don't leave a dangling reference

(http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/callbacks.html#argument_handling)

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] preventing boost::python::throw_error_already_set

2008-12-14 Thread Leonard Ritter
On Thu, Dec 11, 2008 at 5:32 PM, Stefan Seefeld wrote:

> Leonard Ritter wrote:
>
>> hi everyone,
>>
>> i'm having a problem where a boost::python::throw_error_already_set is
>> being thrown and causes a segfault (linux). unfortunately the error happens
>> from within python, when reaching the end of an iteration through a vector
>> array, which was exported using the vector indexing suite.
>>
>> is there any possibility to catch the previous exception?
>>
>
> That's hard to tell without more context: Who is doing the iteration, for
> example ?


python code.

i don't have the feeling that the issue is really related to your library.
some machines do not cause that exception. the problem is that this is very
hard to track for me. this is not meant to sound rough, but i would prefer
if you would not try to find the problem for me, but give me instructions
how i can deal with such problems myself.

this is not the only situation where i get throw_error_already_set
exceptions, and i have no idea how to track them down. is such stuff
unusual? did no one else get these?

thank you.
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig