Problem with uuid package when embedding a python interpreter

Discussion in 'Python' started by Jérôme Fuselier, Jun 30, 2009.

  1. Hello,
    I've tried to import a script in an embedded python intrepreter but
    this script fails when it imports the uuid module. I have a
    segmentation fault in Py_Finalize().

    Here is a simple program which imitate my problem.

    main.c :

    #include "Python.h"

    void test() {
    Py_Initialize();
    PyImport_Import(PyString_FromString("uuid"));
    Py_Finalize();
    }

    main(int argc, char **argv)
    {
    for (i=0 ; i < 10; i++)
    test();
    }

    For my application, I have to call Py_initialize and Py_Finalize
    several times so factorizing them in the main function is not an easy
    solution for me.

    The core which is produced gives me this error :
    Program terminated with signal 11, Segmentation fault.
    #0 0x00190ef6 in type_dealloc (type=0x291320) at Objects/typeobject.c:
    2609
    2609 _PyObject_GC_UNTRACK(type);

    Thanks for your help
    Jerome
    Jérôme Fuselier, Jun 30, 2009
    #1
    1. Advertising

  2. En Tue, 30 Jun 2009 13:05:42 -0300, Jérôme Fuselier
    <> escribió:

    > I've tried to import a script in an embedded python intrepreter but
    > this script fails when it imports the uuid module. I have a
    > segmentation fault in Py_Finalize().
    >
    > #include "Python.h"
    >
    > void test() {
    > Py_Initialize();
    > PyImport_Import(PyString_FromString("uuid"));
    > Py_Finalize();
    > }
    >
    > main(int argc, char **argv)
    > {
    > for (i=0 ; i < 10; i++)
    > test();
    > }
    >
    > For my application, I have to call Py_initialize and Py_Finalize
    > several times so factorizing them in the main function is not an easy
    > solution for me.


    Are you sure you can't do that? Not even using Py_IsInitialized? Try to
    avoid repeatedly calling Py_Initialize - won't work.

    Python 2.x does not have a way to "un-initialize" an extension module
    (that's a big flaw in Python design). Modules that contain global state
    are likely to crash the interpreter when used by the second time. (Python
    3 attempts to fix that)

    --
    Gabriel Genellina
    Gabriel Genellina, Jun 30, 2009
    #2
    1. Advertising

  3. On Jun 30, 7:02 pm, "Gabriel Genellina" <>
    wrote:
    > En Tue, 30 Jun 2009 13:05:42 -0300, Jérôme Fuselier
    > <> escribió:
    >
    >
    >
    > >   I've tried to import a script in an embedded python intrepreter but
    > > this script fails when it imports the uuid module. I have a
    > > segmentation fault in Py_Finalize().

    >
    > > #include "Python.h"

    >
    > > void test() {
    > >     Py_Initialize();
    > >     PyImport_Import(PyString_FromString("uuid"));
    > >     Py_Finalize();
    > > }

    >
    > > main(int argc, char **argv)
    > > {
    > >     for (i=0 ; i < 10; i++)
    > >         test();
    > > }

    >
    > > For my application, I have to call Py_initialize and Py_Finalize
    > > several times so factorizing them in the main function is not an easy
    > > solution for me.

    >
    > Are you sure you can't do that? Not even using Py_IsInitialized? Try to
    > avoid repeatedly calling Py_Initialize - won't work.
    >
    > Python 2.x does not have a way to "un-initialize" an extension module
    > (that's a big flaw in Python design). Modules that contain global state
    > are likely to crash the interpreter when used by the second time. (Python
    > 3 attempts to fix that)
    >
    > --
    > Gabriel Genellina


    Hi Gabriel,
    Thanks for your response. I can modify my code to call
    Py_IsInitialized which I didn't know before and this works well. The
    problem is that I did not own the process which calls Py_Initialize
    and Py_Finalize. I'm able to call Py_Initialize correctly but I can't
    call Py_Finalize so this is not perfect.

    At least I have a temporary solution which is not too bad.
    Thanks,
    Jerome
    Jérôme Fuselier, Jul 1, 2009
    #3
  4. Jérôme Fuselier

    Aahz Guest

    In article <>,
    =?ISO-8859-1?Q?J=E9r=F4me_Fuselier?= <> wrote:
    >
    >I've tried to import a script in an embedded python intrepreter but
    >this script fails when it imports the uuid module. I have a
    >segmentation fault in Py_Finalize().


    You may want to also ask on the capi-sig mailing list.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "as long as we like the same operating system, things are cool." --piranha
    Aahz, Jul 1, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rolf Magnus
    Replies:
    13
    Views:
    757
    Dieter Maurer
    Jan 17, 2005
  2. Jp Calderone
    Replies:
    7
    Views:
    336
    Craig Ringer
    Jan 7, 2005
  3. Jp Calderone
    Replies:
    3
    Views:
    277
    Peter Maas
    Jan 7, 2005
  4. Yogesh Sharma
    Replies:
    1
    Views:
    303
    Yogesh Sharma
    Jan 13, 2005
  5. Replies:
    3
    Views:
    748
    Ziga Seilnacht
    Jan 3, 2007
Loading...

Share This Page