Bindings for python 2.6

 

Hello,

i have a minimal example that will crash metatrader:

#import "python26.dll"

void Py_Initialize();

void Py_Finalize();

int PyRun_SimpleString(string code);

#import

int init(){

Py_Initialize();

PyRun_SimpleString("x=42");

Py_Finalize();

return(0);

}

From what i have read elsewhere using string in mql4 will pass a char* pointer to the function and thats exactly what this particular function wants. writing a dll with freepascal that takes the exact same argument (PChar) and passes it unmodifed to PyRun_SimpleString() will work.

Can anybody enlighten me why the above code will provoke a crash, although the argument passed to it is indeed a pointer to the string, exactly as required by the function? And why it will *not* crash when i define the *exact* same function in my own dll and just pass on the unmodified argument to the original function in python26.dll?

TIA,

Bernd

 

bindings for python 2.6

because i was not able to call the python interpreter directly i have written a wrapper with even a little abstraction:

Link: py26 (dll into libraries, mqh into include)

You can now execute arbitrary Python code from mql, import modules, run threads, file and socket I/O, interface with databases, run network servers, interface with other lanuages like for example R via rpy, everything you can think of.

 
newdigital:
Yes, sorry.

I moved your posts to separated thread.

I will forgive you ;-) [you can safely delete the posting with the many colors, i was a bit upset because i didnt understand what was going on, i have since then calmed down completely]

I can even answer my question from the first posting: It is not because of the char* pointer, it is because of calling conventions: python26.dll uses __cdecl and metatrader expects __stdcall, the crash happens because the functions in python26.dll will not pop the stack when returning. Therefore the unavoidable need for a wrapper dll which will provide a __stdcall interface to metatrader and call python with __cdecl

I have already identified a new problem: all experts running at the same time will share the same __main__ namespace, so i will have to rethink the abstraction so that it allows to easily specify the namespace when accessing variables and not force you to operate only in __main__

 

I have made some progress: I was able to solve a little problem with threads. Now the interpreter runs in its own thread what effectively means you are now able to have multithreaded expert advisors, while metatrader does something else all python threads started (using the thread module) inside a PyExecute() call keep running in the background even after this function has returned.

All currrent files can be found here: Metatrader Python Integration (7bit)

Attached is a screenshot of what i was able to do with it, shortly after i got it working

Reason: