[C++-sig] How to get output from python embedded in VC++

Jian thecolors at 126.com
Sun Jul 23 03:15:23 EDT 2017



Hi Stefan,


I still got a assertion failed at Py_Initialize(). Below is the message.


Program: C:\Python35\python35_d.dll
File: ..\Objects\object.c
Line: 84
Expression: (op->_ob_prev == NULL) == (op->_ob_next == NULL)


I also pasted mo code below. This function is invoked by a button click function. No other codes around it.


int runBoostPython()
{
    Py_Initialize();
    try {
        //boost::python::object main_module =
        //    boost::python::import("__main__");
        //boost::python::object main_namespace =
        //    main_module.attr("__dict__");
        boost::python::dict main_namespace;


        boost::python::object ignored = exec_file("test1.py",
            main_namespace, main_namespace);


        std::wstring output = boost::python::extract<std::wstring>(main_namespace["return_value"]);


    }
    catch (boost::python::error_already_set) {
        PyErr_Print();
    }
    return 0;
}


Please help check it. 


Thanks & Best Regards,


John.
At 2017-07-23 01:01:29, "Stefan Seefeld" <stefan at seefeld.name> wrote:
>On 21.07.2017 23:17, Jian wrote:
>> Hi Imre,
>>
>> I tried to use boost.python as you advised. Build boost.python with 
>>
>> But I got a error in the import.hpp as below:
>> ------------------------import.hpp start
>> ----------------------------------------
>> namespace boost 
>> { 
>> namespace python 
>> {
>>
>> object BOOST_PYTHON_DECL import(str name)
>> {
>>   // should be 'char const *' but older python versions don't use
>> 'const' yet.
>>   char *n = python::extract<char *>(name);
>> *  python::handle<> module(PyImport_ImportModule(n));   <---here is
>> where the error raised. n is "__main__"*
>>   return python::object(module);
>> }
>>
>> }  // namespace boost::python
>> }  // namespace boost
>> ------------------------import.hpp
>> end----------------------------------------
>>
>> I copied your code and just replaced 'script.py' with the name of my
>> own file. I found the error raised when the code importing module
>> "__main__". 
>>
>> That's my first time to use boost.python. Could you help about this issue?
>
>This probably means you haven't initialized Python correctly. A good
>starting point might be this example:
>https://github.com/boostorg/python/blob/develop/example/quickstart/embedding.cpp
>
>(I'm the Boost.Python maintainer. While I'd be happy to help you using
>Boost.Python, I think this is mostly orthogonal to your original
>question about how to capture output generated by you embedded Python.)
>
>
>
>Best,
>        Stefan
>
>
>
>
>-- 
>
>      ...ich hab' noch einen Koffer in Berlin...
>
>_______________________________________________
>Cplusplus-sig mailing list
>Cplusplus-sig at python.org
>https://mail.python.org/mailman/listinfo/cplusplus-sig
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20170723/19a91b89/attachment-0001.html>


More information about the Cplusplus-sig mailing list