in Education by
I have a function in c++ that receives a initialised class as a PyObject. The python class is: class Expression: def __init__(self, obj): self.obj = obj def get_source(self): #Check if the object whose source is being obtained is a function. if inspect.isfunction(self.obj): source = inspect.getsourcelines(self.obj)[0][1:] ls = len(source[0]) - len(source[0].lstrip()) source = [line[ls:] for line in source] #get rid of comments from the source source = [item for item in source if item.lstrip()[0] != '#'] source = ''.join(source) return source else: raise Exception("Expression object is not a function.") The c++ receives this: Expression(somefunctogetsource) From c++ how do I call the get_source method of the expression object? So far I've read the python c-api docs and tried things like this: PyObject* baseClass = (PyObject*)expression->ob_type; PyObject* func = PyObject_GetAttrString(baseClass, "get_source"); PyObject* result = PyObject_CallFunctionObjArgs(func, expression, NULL); And convert the result to a string, but this doesn't work. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Simpler than you're making it. You don't need to retrieve anything from the base class directly. Just do: PyObject* result = PyObject_CallMethod(expression, "get_source", NULL); if (result == NULL) { // Exception occurred, return your own failure status here } // result is a PyObject* (in this case, it should be a PyUnicode_Object) PyObject_CallMethod takes an object to call a method of, a C-style string for the method name, and a format string + varargs for the arguments. When no arguments are needed, the format string can be NULL. The resulting PyObject* isn't super useful to C++ code (it has runtime determined 1, 2 or 4 byte characters, depending on the ordinals involved, so straight memory copying from it into std::string or std::wstring won't work), but PyUnicode_AsUTF8AndSize can be used to get a UTF-8 encoded version and length, which can be used to efficiently construct a std::string with equivalent data. If performance counts, you may want to explicitly make a PyObject* representing "get_source" during module load, e.g. with a global like: PyObject *get_source_name; which is initialized in the module's PyMODINIT_FUNC with: get_source_name = PyUnicode_InternFromString("get_source"); Once you have that, you can use the more efficient PyObject_CallMethodObjArgs with: PyObject* result = PyObject_CallMethodObjArgs(expression, get_source_name, NULL); The savings there are largely in avoiding constructing a Python level str from a C char* over and over, and by using PyUnicode_InternFromString to construct the string, you're using the interned string, making the lookup more efficient (since the name of get_source is itself automatically interned when def-ed in the interpreter, no actual memory comparison of the contents takes place; it realizes the two strings are both interned, and just checks if they point to the same memory or not).

Related questions

0 votes
    I have a function in c++ that receives a initialised class as a PyObject. The python class is: ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    Background: I'm trying to look through assets in an old game's archive files. They're 8-bit. I ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I'm trying to embed python into a C application. For the moment, I am trying to get the following ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    Within a python script can I call an external command? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Has anyone worked on calling a C# module from C module. I tried searching on internet but didn't ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Write an if statement to find the smallest of three given integers using min() method of the Math class. Plz solve this quickly.. Select the correct answer from above options...
asked Dec 20, 2021 in Education by JackTerrance
0 votes
    Which is more effective while calling the C++ functions? a) call by object b) call by pointer c) call by value d) call by reference...
asked Oct 19, 2022 in Education by JackTerrance
0 votes
    I am working on a homework assignment in which we are creating a class to be used in a program to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Which of these method is used to tell the calling thread to give up a monitor and go to sleep until ... division Multithreading of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these method is used to tell the calling thread to give up a monitor and go to sleep ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    What should be the execution order, if a class has a method, static block, instance block, and ... stackoverflow.com 🔗Source: Java Interview Questions and Answers...
asked Dec 19, 2020 in Technology by Editorial Staff
0 votes
    ________ can be used to establish risk and stability estimations on an item of code, such as a class or method ... code (2)Defect density (3)Risk density (4)Cyclomatic complextiy...
asked May 15, 2021 in General by JackTerrance
0 votes
    Im calling a locally hosted wcf service from silverlight and I get the exception below. Iv created a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 22, 2022 in Education by JackTerrance
0 votes
    Im calling a locally hosted wcf service from silverlight and I get the exception below. Iv created a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    First I'll explain my senario: I have a page in which users login or register. The login control ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 10, 2022 in Education by JackTerrance
...