How to get source code from PyCodeObject in c python api? - c++

Hi i'm using c python api.
I want to extract function object's source code.
I want pure python code (like def func: ... ) But if it is hard then, i want to
get py byte codes at least.
Here is my c++ code that i get PyCodeObject from PyFunctionObject.
//pObject is PyFunctionObject which is python standard lib's function.
PyFunctionObject* pFunctionObject = (PyFunctionObject*)pObject;
PyCodeObject* codeObject = (PyCodeObject*)pFunctionObject->func_code;
PyObject* strObject = codeObject->co_code; //get code from code object
char * sourceCode = PyString_AsString(strObject); //convert to string
But sourceCode variable(char*) always show only 1 byte.
How should i gonna get this?
There is a lot of ways to do this in python code side, like just use 'dis' or 'inspect' module.
But i want to do this by c python api.
P.S
I guess that the PyCodeObject's co_code member is an byte array.
I used visual studio debug memory view and saw co_code member's adjoined memory byte but it seems like a byte code array(just maybe).

Firstly, PyCodeObject->co_code is the generated byte code, not pure Python source code.
In Python, we could use inspect.getsource to get pure Python source code. And in C, you could also use it via PyObject_CallMethod

Related

how to distribute a lua file with c++

I know how to do stuff with Lua states and what not but what i don't understand is how you would distribute the final program with a seperate lua file because say you have a .exe and a lua file in the same directory how would I make it so that it is all one executable like how Löve 2d uses
copy /b
to append the lua file to the Löve 2d interpreter so it can be distributed.
could someone possibly explain how this works.
many thanks
Blazing
You could embed the lua code directly into your C++ source in a raw string literal like so:
const auto lua_code = R"lua(
...lua code here...
)lua";

converting from Matlab to C++

c = imread('focus.jpg');
I have a 3D array ( image ) in m.file ,I want to use this
array in C++,how can i pass this array to C++ file
thanks in advance
You can call MATLAB Engine directly in C++.
Check out Call MATLAB Functions from C and C++ Applications for more info and examples.
Edit: On the other hand, it seems you don't need to do this, you can simply load this image directly in C++, e.g. using OpenCV.

How can I load and execute python script in C++ application

I want to extend my application, which is written in C++ using python scripts (extensions). I originally wanted to use TCL for that, just like they do in xchat, for example, but later I decided to use python, because it seems to be quite popular for whatever reasons.
However, I am failing to load and execute even very simple python script. I followed http://docs.python.org/2/extending/embedding.html
When I give a filename of script that I want to load as argument to pName, the error I get from PyErr_Print is: ImportError: Import by filename is not supported.
Reading the documentation, I figured I might need to run PyImport_ExecCodeModule, however this C function requires 2 arguments, 1 is char * (probably a name of module), other one is compiled python code, which according to docs I can get by calling python function compile(). Unfortunately it doesn't say how do I call this python function using C api's in my C++ code. Ideally I would imagine to do something like
PyObject *code = PyCompile("print (\"hello :)\"");
but I couldn't find any function like PyCompile, neither any other C-api function that would simply allowed me to execute python internal function (like compile) and grab its output as PyObject.
So, question is: how can I easily load a python script from a file (something.py) and execute it within my application using the embedded python interpretor?

Is it possible to call Matlab from QtCreator?

I am doing image analysis using C++ in the QtCreator environment. In order to build a learning model, I want to use the TreeBagger class from MATLAB, which is really powerful. Can I call MATLAB from QtCreator, give it some parameters, and get back the classification error? Can I do this without using mex files?
From QProcess's Synchronous Process API example:
QProcess gzip;
gzip.start("gzip", QStringList() << "-c");
if (!gzip.waitForStarted())
return false;
gzip.write("Qt rocks!");
gzip.closeWriteChannel();
if (!gzip.waitForFinished())
return false;
QByteArray result = gzip.readAll();
The concept to from this example is the process of being able to execute matlab w/ whatever settings that would be preferable and begin writing a script to it immediately. After the write; you can close the channel, wait for response, then read the results from matlab. Uunfortunately, I'm not experienced w/ it to provide a more direct example, but this is the concept for the most case. Please research the documentation for anything else.
Matlab has an "engine" interface described here to let standalone programs call matlab functions. It has the advantage that you can call engPutVariableand engGetVariable to transfer your data in binary format (I think it works by using shared memory between your process and matlab, but I'm not sure on this), so you don't have to convert your data to ascii and parse the result from ascii.
For c++ you might want to write a wrapper class for RAII or have a look at http://www.codeproject.com/Articles/4216/MATLAB-Engine-API, where this has already been done.

Embed a stateful Python script into C++ program using boost::python

I have a C++ program that keeps generating data. I have a python class that process these data. I want to use this python class to process the data: when each time a data point is generated, I can use this python script to process the data. But this python script must be "stateful", i.e. it should be able to remember what it did before this data point.
One super basic example is, my C++ program just generates numbers, and my python class calculates the cumulative sums of the numbers generated:
Python:
class CumSum:
def addone(x):
self._cumsum += x;
print self._cumsum;
C++
[Somehow construct a CumSum instance, say c]
for (int i=0; i<100000; i++) {
int x = rand() % 1000;
[Call c.addone(x)]
}
I heard boost::python is a good way to handle this. Can anyone sketch out how to do it? I tried to read boost documents but they were too huge for me to digest.
I appreciate your help.
For basic information about how to execute your python script:
http://www.boost.org/doc/libs/1_47_0/libs/python/doc/tutorial/doc/html/python/embedding.html
For details on manipulating python objects in C++
http://www.boost.org/doc/libs/1_47_0/libs/python/doc/tutorial/doc/html/python/object.html
Much of boost-python is concerned with exporting your C++ classes to python but you aren't doing that so you can ignore it.
You may be better off using a simpler wrapper like SCXX
http://davidf.sjsoft.com/mirrors/mcmillan-inc/scxx.html