Re: A problem in embedding Python in a plug-in

2007-01-07 Thread Gabriel Genellina
On 6 ene, 15:29, Koichi [EMAIL PROTECTED] wrote:

 Hi, I'm now making a plug-in for a CG software. I embed
 Python in a plugin and it works. The problem is that it
 conflicts with other plugins that also embeds Python because it
 runs in the same thread. I don't know when Py_Initialize() and

Given all those constraints, I think the only safe option is to run
Python in another process.
You write a very simple plugin (NOT in Python, maybe C code) that
spawns another process (the actual Python code) and forwards all
requests to that other process, using some form of IPC.

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


A problem in embedding Python in a plug-in

2007-01-06 Thread Koichi
Hi, I'm now making a plug-in for a CG software. I embed
Python in a plugin and it works. The problem is that it
conflicts with other plugins that also embeds Python because it
runs in the same thread. I don't know when Py_Initialize() and
Py_Finalize() are executed because I don't know when user loads/unloads
a plugin that embeds Python. Is there any right way to embed
Python in a plug-in under the following conditions? If not, what
options do I have to give up any of them?

- My plug-in has no way to know when the user loads/unloads it.
- My plug-in has no way to know when the user loads/unloads somebody
  else's plug-in that embeds Python.
- Somebody else's plug-in might have used any Python/C API including
  GIL related stuff, new_interpreter, PyGILState_*
- Every plug-in initially runs in the same thread but some plug-in
  can start a new thread.
- Preferably my plug-in has its own __main__
- My plug-in doesn't have to be thread safe but shouldn't disturb
  any other plug-in (so should be thread safe anyway)

Typical problem is:
- When somebody else loads or unloads another plugin and calls
  Py_IsInitialized() or Py_Finalize(), my plugin fails
- When some other plugin uses PyGILState*(), both plugin fails
  because my plug-in uses new interpreter
(I'm new to Python/C API so I may be misunderstanding one ore more)

Any suggestion is appreciated.

Koichi


-- 
http://mail.python.org/mailman/listinfo/python-list


the problem of embedding python

2005-03-24 Thread
hi,python-chinese!

environment: FreeBSD4.11, gcc2.953.4, python2.2ports.


source code
#include Python.h

int main()
{
Py_Initialize();
PyRun_SimpleString(from time import time, ctime\n
   print 'Today is', ctime( time() )\n);
Py_Finalize();
return 0;
}







document
5.6 Linking Requirements 
While the configure script shipped with the Python sources will correctly build 
Python to export the symbols needed by dynamically linked extensions, this is 
not automatically inherited by applications which embed the Python library 
statically, at least on Unix. This is an issue when the application is linked 
to the static runtime library (libpython.a) and needs to load dynamic 
extensions (implemented as .so files). 

The problem is that some entry points are defined by the Python runtime solely 
for extension modules to use. If the embedding application does not use any of 
these entry points, some linkers will not include those entries in the symbol 
table of the finished executable. Some additional options are needed to inform 
the linker not to remove these symbols. 

Determining the right options to use for any given platform can be quite 
difficult, but fortunately the Python configuration already has those values. 
To retrieve them from an installed Python interpreter, start an interactive 
interpreter and have a short session like this: 


 import distutils.sysconfig
 distutils.sysconfig.get_config_var('LINKFORSHARED')
'-Xlinker -export-dynamic'

The contents of the string presented will be the options that should be used. 
If the string is empty, there's no need to add any additional options. The 
LINKFORSHARED definition corresponds to the variable of the same name in 
Python's top-level Makefile. 






result when compile
[EMAIL PROTECTED] gcc34 test_python.cpp -o test_python 
-L/usr/local/lib/python2.2/config -lpython2.2 -I/usr/local/include/python2.2
 
/usr/local/lib/python2.2/config/libpython2.2.a(posixmodule.o): In function 
`posix_tmpnam':
posixmodule.o(.text+0x297a): warning: tmpnam() possibly used unsafely; consider 
using mkstemp()
/usr/local/lib/python2.2/config/libpython2.2.a(posixmodule.o): In function 
`posix_tempnam':
posixmodule.o(.text+0x28b6): warning: tempnam() possibly used unsafely; 
consider using mkstemp()
/var/tmp//ccw3igF6.o(.eh_frame+0x11): undefined reference to 
`__gxx_personality_v0'
/usr/local/lib/python2.2/config/libpython2.2.a(floatobject.o): In function 
`float_rem':
floatobject.o(.text+0xf69): undefined reference to `fmod'
/usr/local/lib/python2.2/config/libpython2.2.a(floatobject.o): In function 
`float_divmod':
floatobject.o(.text+0x): undefined reference to `fmod'
floatobject.o(.text+0x11a9): undefined reference to `floor'
/usr/local/lib/python2.2/config/libpython2.2.a(floatobject.o): In function 
`float_pow':
floatobject.o(.text+0x13e2): undefined reference to `fmod'
floatobject.o(.text+0x14b5): undefined reference to `floor'
floatobject.o(.text+0x1541): undefined reference to `pow'
/usr/local/lib/python2.2/config/libpython2.2.a(bltinmodule.o): In function 
`builtin_round':
bltinmodule.o(.text+0x1e1d): undefined reference to `floor'
bltinmodule.o(.text+0x1e37): undefined reference to `ceil'
/usr/local/lib/python2.2/config/libpython2.2.a(thread.o): In function 
`PyThread_start_new_thread':
thread.o(.text+0x46): undefined reference to `pthread_attr_init'
thread.o(.text+0x57): undefined reference to `pthread_attr_setstacksize'
thread.o(.text+0x8b): undefined reference to `pthread_create'
thread.o(.text+0xa9): undefined reference to `pthread_attr_destroy'
thread.o(.text+0xbb): undefined reference to `pthread_detach'
/usr/local/lib/python2.2/config/libpython2.2.a(thread.o): In function 
`PyThread_get_thread_ident':
thread.o(.text+0xe5): undefined reference to `pthread_self'
/usr/local/lib/python2.2/config/libpython2.2.a(thread.o): In function 
`PyThread_allocate_lock':
thread.o(.text+0x198): undefined reference to `pthread_mutex_init'
thread.o(.text+0x1c2): undefined reference to `pthread_cond_init'
/usr/local/lib/python2.2/config/libpython2.2.a(thread.o): In function 
`PyThread_free_lock':
thread.o(.text+0x20e): undefined reference to `pthread_mutex_destroy'
thread.o(.text+0x231): undefined reference to `pthread_cond_destroy'
/usr/local/lib/python2.2/config/libpython2.2.a(thread.o): In function 
`PyThread_acquire_lock':
thread.o(.text+0x272): undefined reference to `pthread_mutex_lock'
thread.o(.text+0x2b6): undefined reference to `pthread_cond_wait'
thread.o(.text+0x2ec): undefined reference to `pthread_mutex_unlock'
/usr/local/lib/python2.2/config/libpython2.2.a(thread.o): In function 
`PyThread_release_lock':
thread.o(.text+0x32e): undefined reference to `pthread_mutex_lock'
thread.o(.text+0x354): undefined reference to `pthread_mutex_unlock'
thread.o(.text+0x377): undefined reference to `pthread_cond_signal'
/usr/local/lib/python2.2/config/libpython2.2.a(thread.o): In function 
`PyThread_allocate_sema':