Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Embedded Python in C++
#8
I was using the sample code as base and trying to return a string from the func.

mainly it seem to fail on:
str = PyUnicode_AsEncodedString(result, "utf-8", "~I~");
the returned str object is NULL

int main() {
        const char* spamclass = R""""(
class Spam:
    def func(self):
        if __name__ == '__main__':
            print('called in __main__')
            return "Completed"
        else:
            print('called out of __main__')
            return "Skipped"
    )"""";
        PyObject* mainmod, * Spam, * spam, * meth, * result, * repr, *str;
        const char* s;
        Py_Initialize();
        PyRun_SimpleString("from time import time,ctime\n"
            "print('Today is', ctime(time()))\n");
        PyRun_SimpleString(
            spamclass
        );
        mainmod = PyImport_ImportModule("__main__");
        Spam = PyObject_GetAttrString(mainmod, "Spam");
        spam = PyObject_CallObject(Spam, NULL);
        meth = PyObject_GetAttrString(spam, "func");
        result = PyObject_CallObject(meth, NULL);
        if (!PyUnicode_CheckExact(result)) {
            repr = PyObject_Repr(result);
            str = PyUnicode_AsEncodedString(repr, "utf-8", "~I~");
            s = PyBytes_AS_STRING(str);
            cout << "response from python: " << s << endl;
        }
        else {
            str = PyUnicode_AsEncodedString(result, "utf-8", "~I~");
            s = PyBytes_AS_STRING(str);
            cout << "response from python: " << s << endl;
        }
        if (Py_FinalizeEx() < 0) {
            exit(120);
        }
        return 0;
}
Reply


Messages In This Thread
Embedded Python in C++ - by Xeno - Jul-30-2022, 05:12 AM
RE: Embedded Python in C++ - by Gribouillis - Jul-30-2022, 07:25 AM
RE: Embedded Python in C++ - by Xeno - Jul-30-2022, 02:29 PM
RE: Embedded Python in C++ - by Gribouillis - Jul-30-2022, 04:01 PM
RE: Embedded Python in C++ - by Xeno - Jul-31-2022, 12:17 AM
RE: Embedded Python in C++ - by Xeno - Jul-31-2022, 07:26 AM
RE: Embedded Python in C++ - by Gribouillis - Jul-31-2022, 08:15 AM
RE: Embedded Python in C++ - by Xeno - Jul-31-2022, 07:13 PM
RE: Embedded Python in C++ - by Gribouillis - Jul-31-2022, 08:56 PM
RE: Embedded Python in C++ - by Xeno - Jul-31-2022, 09:21 PM
RE: Embedded Python in C++ - by Xeno - Jul-31-2022, 09:27 PM
RE: Embedded Python in C++ - by Xeno - Jul-31-2022, 09:33 PM
RE: Embedded Python in C++ - by Gribouillis - Jul-31-2022, 09:49 PM
RE: Embedded Python in C++ - by Gribouillis - Jul-31-2022, 09:51 PM
RE: Embedded Python in C++ - by Xeno - Jul-31-2022, 10:47 PM
RE: Embedded Python in C++ - by Gribouillis - Aug-01-2022, 07:35 AM
RE: Embedded Python in C++ - by Xeno - Aug-02-2022, 04:31 AM
RE: Embedded Python in C++ - by Gribouillis - Aug-02-2022, 08:08 AM
RE: Embedded Python in C++ - by Xeno - Aug-03-2022, 01:55 AM
RE: Embedded Python in C++ - by Gribouillis - Aug-03-2022, 07:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to run detectron2, as python embedded code in C++, on GPU? hassaniqbal931 3 2,673 Nov-02-2023, 04:45 PM
Last Post: blabling2
  Adding libraries to embedded Python as a ZIP The_Oman 0 2,290 May-05-2023, 04:05 PM
Last Post: The_Oman
Bug Embedded Python Memory Leaks Alexei 1 1,872 Sep-16-2022, 01:15 PM
Last Post: Alexei
  Embedded python fails to compile on Raspberry Pi tryfon 2 4,853 Dec-22-2020, 02:06 PM
Last Post: tryfon
  Can Embedded Python run any shared library on Android ? sprotz 0 3,250 Nov-08-2020, 12:21 PM
Last Post: sprotz
  memory leak on embedded python in c++ asdf3721 3 4,694 Jul-16-2020, 06:33 AM
Last Post: Gribouillis
  Embedded Python PyObject_CallObject function JRHeisey 1 3,410 Nov-27-2019, 01:50 AM
Last Post: casevh
  Trying to implement Python into embedded OS thesurya7 2 3,297 Apr-02-2019, 06:38 PM
Last Post: ebolisa
  Multiple calls to Python interpreter embedded in C++ application yield segmentation f mmoelle1 0 3,462 Mar-21-2019, 08:54 PM
Last Post: mmoelle1
  python code (embedded in Redshift) to return result of the query Mel 0 2,945 Aug-24-2018, 06:12 PM
Last Post: Mel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020