Embedding Python and adding C functions to the interpreter - c++

I'm currently writing an applications that embedds the python interpreter. The idea is to have the program call user specified scripts on certain events in the program. I managed this part but now I want the scripts to be able to call functions in my program.
Here's my code so far:
#include "python.h"
static PyObject* myTest(PyObject* self,PyObject *args)
{
return Py_BuildValue("s","123456789");
}
static PyMethodDef myMethods[] = {{"myTest",myTest},{NULL,NULL}};
int main()
{
Py_Initialize();
Py_InitModule("PROGRAM",myMethods);
PyRun_SimpleString("print PROGRAM.myTest()");
Py_Finalize();
}
Thanks!

You need to bind that function to some module, see http://docs.python.org/extending/embedding.html#extending-embedded-python
Edit:
Basicly your code should work. Whats not working?

Related

Embed Ruby in C and exntend

I have a requirement where I need to embed Ruby code in C++ (I could achieve this) and extend some functionality. When I try to call (require) my extension module, the program fails.
I have pasted a jist of the code below:
main.cpp
#include <ruby.h>
static VALUE Sum(VALUE rb_self, VALUE rb_param1, VALUE rb_param2)
{
double val1 = NUM2DBL(rb_param1);
double val2 = NUM2DBL(rb_param2);
return rb_float_new(val1+val2);
}
void Init_myMod()
{
myMod = rb_define_module("myMod");
rb_define_module_function(myMod, "Sum", Sum, 2);
}
int main()
{
... Init ruby method...
rb_require("myRubyfile");
Init_myMod(); // This is where I need to push my ruby extension.
rb_funcall(rb_mKernel, rb_intern("helloworld"), 0 , NULL);
....
}
myRubyfile.rb
require "myMod"
def helloworld()
puts "Hello world"
#puts "Sum of.. " + myMod.sum(4, 5)
end
The problem is if I dont use require "myMod" the code works and prints "Hello World", but if I use require.., the code dumps.
This is my requirement where I need to embed the ruby script in C, and reuse some of the C methods in ruby (extension).
Can someone help me with this?
You're getting a segfault because the Ruby VM segfaults when there is an uncaught exception. You need to use something like rb_rescue or rb_protect to wrap the code which could raise an exception.
require "myMod" is raising an exception because require is only used to load files. You don't need to require anything because you're creating the module directly in the VM's memory. So... why the error? Well, if you were to write your module in Ruby, you'd get a syntax error:
module myMod
...
end
# SyntaxError: class/module name must be CONSTANT
But when you use rb_define_module, rather than raise an exception, Ruby does a tricky thing: it creates the module but doesn't expose it to the Ruby code. In order to expose the module to Ruby, just make the name valid:
// .cpp
rb_define_module("MyMod");
and
# .rb
puts "Sum of.. " + MyMod.Sum(4, 5).to_s
Something like this should work. Make a bat script to your ruby script file and change system to the appropriate system call. Not sure if system() works in Windows. I have run many bash scripts this way so I would assume you can do the same thing in Windows.
#include <stdio.h>
#include <stdlib.h>
int main()
{
puts("Starting now:");
system("/path_to_script/script_name");
return 0;
}

Create a Python3 module at runtime while initialize an embedded Python

We have a DLL that implements a custom programming language. What I want to do is adding support for the python language keeping the same code for "API function".
I have succefully embedded python in this DLL, now I'm approaching the problem to expose all the old function as a python module.
Now this DLL doesn't expose the API function as interface function but it's installed (as function pointer) to the language engine. In this way it's impossible to create a new python module (a new DLL). But I need to keep the compatibility with the old method...
It's possible to create (and install) at runtime a module defined in the same DLL where the Python is located?
I think something like calling the PyInit_xxxx method after PyInitialize();
This has gotten substantially more complicated in Python 3 (vs. how it was in Python 2), but I've gotten it working for my code, so I hope this works for you as well.
// Python 3's init function must return the module's PyObject* made
// with PyModule_Create()
PyObject* initspam();
const char* spam_module_name;
int main(int argc, char **argv)
{
Py_Initialize();
PyImport_AddModule(spam_module_name);
PyObject* module = initspam();
PyObject* sys_modules = PyImport_GetModuleDict();
PyDict_SetItemString(sys_modules, spam_module_name, module);
Py_DECREF(module)
...
}
I found an example of this in the python 3 source code:
Python-3.4.2\Python\pythonrun.c : import_init()
That has much better error checking and such than my example above.
I've solved using a code like this before Py_Initialize();
/* Add a built-in module, before Py_Initialize */
PyImport_AppendInittab("xxx", PyInit_xxx);
The easiest way to handle this is to statically initialize your statically-linked modules by directly calling initspam() after the call to Py_Initialize() or PyMac_Initialize():
int main(int argc, char **argv)
{
/* Pass argv[0] to the Python interpreter */
Py_SetProgramName(argv[0]);
/* Initialize the Python interpreter. Required. */
Py_Initialize();
/* Add a static module */
initspam();
An example may be found in the file Demo/embed/demo.c in the Python source distribution.

Calling python eval from c++

I'm basically attempting to do the same as the following question but i'm getting run time errors at the call to PyObject_Print complaining about an error in "toupper() at 0x776e0226"
Python C API: Using PyEval_EvalCode
My code is:
int callExecFunction(const char* evalStr)
{
PyCodeObject* code = (PyCodeObject*)Py_CompileString(evalStr, "pyscript", Py_eval_input);
PyObject* global_dict = PyModule_GetDict(pModule);
PyObject* local_dict = PyDict_New();
PyObject* obj = PyEval_EvalCode(code, global_dict, local_dict);
PyObject* result = PyObject_Str(obj);
PyObject_Print(result, stdout, 0);
}
evalStr is "setCurrentFileDir()" and pModule was initialized earlier from a script without error and was working as this code: http://docs.python.org/2/extending/embedding.html#pure-embedding.
And inside the loaded module there is the function:
def setCurrentFileDir():
print "setCurrentFileDir"
return "5"
What have I missed/done wrong in the eval function call. Note that I cannot call the function setCurrentFileDir "directly" through the python API, I must use eval.
Actually this works fine. However I had a .pyd file in the directory that python must have built (and eclipse was hiding from me) from an older run of the python script that didn't have the function defined, causing the problem.
Here is where I got the idea that the problem maybe a .pyd file.
python NameError: name '<anything>' is not defined (but it is!)

How to reinitialise an embedded Python interpreter?

I'm working on embedding Python in our test suite application. The purpose is to use Python to run several tests scripts to collect data and make a report of tests. Multiple test scripts for one test run can create global variables and functions that can be used in the next script.
The application also provides extension modules that are imported in the embedded interpreter, and are used to exchange some data with the application.
But the user can also make multiple test runs. I don't want to share those globals, imports and the exchanged data between multiple test runs. I have to be sure I restart in a genuine state to control the test environment and get the same results.
How should I reinitialise the interpreter?
I used Py_Initialize() and Py_Finalize(), but get an exception on the second run when initialising a second time the extension modules I provide to the interpreter.
And the documentation warns against using it more than once.
Using sub-interpreters seems to have the same caveats with extension modules initialization.
I suspect that I'm doing something wrong with the initialisation of my extension modules, but I fear that the same problem happens with 3rd party extension modules.
Maybe it's possible to get it to work by launching the interpreter in it's own process, so as to be sure that all the memory is released.
By the way, I'm using boost-python for it, that also warns AGAINST using Py_Finalize!
Any suggestion?
Thanks
Here is another way I found to achieve what I want, start with a clean slate in the interpreter.
I can control the global and local namespaces I use to execute the code:
// get the dictionary from the main module
// Get pointer to main module of python script
object main_module = import("__main__");
// Get dictionary of main module (contains all variables and stuff)
object main_namespace = main_module.attr("__dict__");
// define the dictionaries to use in the interpreter
dict global_namespace;
dict local_namespace;
// add the builtins
global_namespace["__builtins__"] = main_namespace["__builtins__"];
I can then use use the namespaces for execution of code contained in pyCode:
exec( pyCode, global_namespace, lobaca_namespace );
I can clean the namespaces when I want to run a new instance of my test, by cleaning the dictionaries:
// empty the interpreters namespaces
global_namespace.clear();
local_namespace.clear();
// Copy builtins to new global namespace
global_namespace["__builtins__"] = main_namespace["__builtins__"];
Depending at what level I want the execution, I can use global = local
How about using code.IteractiveInterpreter?
Something like this should do it:
#include <boost/python.hpp>
#include <string>
#include <stdexcept>
using namespace boost::python;
std::string GetPythonError()
{
PyObject *ptype = NULL, *pvalue = NULL, *ptraceback = NULL;
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
std::string message("");
if(pvalue && PyString_Check(pvalue)) {
message = PyString_AsString(pvalue);
}
return message;
}
// Must be called after Py_Initialize()
void RunInterpreter(std::string codeToRun)
{
object pymodule = object(handle<>(borrowed(PyImport_AddModule("__main__"))));
object pynamespace = pymodule.attr("__dict__");
try {
// Initialize the embedded interpreter
object result = exec( "import code\n"
"__myInterpreter = code.InteractiveConsole() \n",
pynamespace);
// Run the code
str pyCode(codeToRun.c_str());
pynamespace["__myCommand"] = pyCode;
result = eval("__myInterpreter.push(__myCommand)", pynamespace);
} catch(error_already_set) {
throw std::runtime_error(GetPythonError().c_str());
}
}
I'd write another shell script executing the sequence of test scripts with new instances of python each time. Or write it in python like
# run your tests in the process first
# now run the user scripts, each in new process to have virgin env
for script in userScript:
subprocess.call(['python',script])

pass callback from python to c++ using boost::python

I want to pass callback from my python code to c++
I want my code look something like this:
In C++ :
typedef void (*MyCallback_t) (CallbackInfo);
class MyClass
{...
void setcallback(MyCallback_t cb);
...
}
And to use it in python :
import mylib
def myCallback(mylib_CallbackInfo):
...
t = mylib.MyClass()
t.setcallback(myCallback)
I saw some topics near my problem but couldn't solve it
For example here :
Realtime processing and callbacks with Python and C++ there is advice to use boost::python and warning about GLI but no examples.
And here
How to call a python function from a foreign language thread (C++) there is no full description with python code part and with "BOOST_PYTHON_MODULE" part
I also found link to use py_boost_function.hpp for example in Boost python howto but it didn't compile and actualy I couldn't understand how to use it.
Ok, I'm still trying to figure this out too, but here's whats working for me so far:
#this is the variable that will hold a reference to the python function
PyObject *py_callback;
#the following function will invoked from python to populate the call back reference
PyObject *set_py_callback(PyObject *callable)
{
py_callback = callable; /* Remember new callback */
return Py_None;
}
...
#Initialize and acquire the global interpreter lock
PyEval_InitThreads();
#Ensure that the current thread is ready to call the Python C API
PyGILState_STATE state = PyGILState_Ensure();
#invoke the python function
boost::python::call<void>(py_callback);
#release the global interpreter lock so other threads can resume execution
PyGILState_Release(state);
The python function is invoked from C++, and executes as expected.
These test files from the boost::python source code repository contains great examples about how to pass callbacks from Python into C++:
https://github.com/boostorg/python/blob/develop/test/callbacks.cpp
https://github.com/boostorg/python/blob/develop/test/callbacks.py