[Pythonmac-SIG] python plugins in Cocoa app.

ronaldoussoren ronaldoussoren at mac.com
Fri Jul 2 15:25:49 CEST 2010



On 02 Jul, 2010,at 03:14 PM, Georg Seifert <georg.seifert at gmx.de> wrote:

Hi,

I have problems getting plugins made in python (made with py2app) to play with my app.

My app needs to do two things: load plugins (bundles, mostly written in ObjC but also in python) and run scripts from within the app (uses PyRun_SimpleString). If I have a plugin loaded (it works) but it crashes on running the scripts.

I make the plugin the the -A option to keep them small (16MB per plugin is too much)

	python setup.py py2app -A

If I then run this it crashes on "PyRun_SimpleString":
    Py_Initialize();
    PyRun_SimpleString("print \"Test\"\n");
    Py_Finalize();

In my app, I week link to python through
	
	OTHER_LDFLAGS = -weak_framework Python
(This was needed to support both Leopard and Snow Leopard.)

Can anyone shed some light on my problem?

Best Regards
Georg Seifert

_______________________________________________
Pythonmac-SIG maillist - Pythonmac-SIG at python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG

First of all: don't use Py_Initialize and Py_Finalize around the calls to to PyRun_SimpleString, the call to Py_Finalize will clean up resources that are used by the Python plugins.    It is better to call Py_Initialize during program start and Py_Finalize during shutdown (to ensure that python finalizers are called).

That is not what's causing your problem though. The C code in plugin bundles created using py2app ensures that Python is initialized and then runs the main module of the bundle. It then ensures that the Python GIL is released, which is needed to ensure that callbacks to Python code can happen on any thread.

You will have to ensure that your code acquires the GIL before calling Python APIs. The easiest way to do that is:

    PyGILState_STATE gilState = PyGILState_Ensure();
    PyRun_SimpleString("print \"Test\"\n");
    PyGILState_Release(gilState);

Ronald

P.S. As background: the GIL is the Global Interpreter Lock and is a lock that ensures that at most 1 C thread at a time is actively executing Python code.   Threads running Python code will periodicly release and reacquire the GIL to ensure that all such threads can make progress.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythonmac-sig/attachments/20100702/680a45bb/attachment-0001.html>


More information about the Pythonmac-SIG mailing list