classification
Title: Embedded python crashed on 4th run, if "ctypes" is used
Type: crash Stage: patch review
Components: ctypes Versions: Python 3.1, Python 3.2, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: theller Nosy List: mkp, nik.lutz@gmail.com, ocean-city, robince, tanaga, theller, warlock
Priority: normal Keywords: patch

Created on 2009-09-09 08:31 by warlock, last changed 2010-09-12 16:20 by ocean-city. This issue is now closed.

Files
File name Uploaded Description Edit
fix_ctypes_crash.patch ocean-city, 2009-09-20 02:19
Messages (7)
msg92444 - (view) Author: Ilya (warlock) Date: 2009-09-09 08:31
When embedding python from C, and importing "ctypes" module in embedded 
script, it always crashes on Py_Finalize() on 4th cycle.
Tested with both PyRun_SimpleString(...) and PyRun_String(...).
Platform: Windows XP
IDE's: LabWindows/CVI 8.5 and Code::Blocks/gcc

Code:
--------------------------
#include <stdio.h>
#include <python.h>

int main()
{
    int i;
    for (i=0; i<10; i++)
    {
        printf("--- %d ---\n", i);
        Py_Initialize();
        PyRun_SimpleString("import ctypes");
        Py_Finalize();
    }
    return 0;
}
--------------------------------
Output:
--------------------------------
--- 0 ---
--- 1 ---
--- 2 ---
--- 3 ---

Process returned -1073741819 (0xC0000005)   execution time : 3.109 s
Press any key to continue.
--------------------------------
msg92449 - (view) Author: Ilya (warlock) Date: 2009-09-09 12:49
Tested
   obj=PyImport_ImportModule("ctypes");
   Py_DECREF(obj);
instead of PyRun_SimpleString(...) - same result
msg92555 - (view) Author: (mkp) Date: 2009-09-12 22:12
on python2.5 / mac os x 10.5.7:

$ cat main.c
#include <stdio.h>
#include <python.h>

int main()
{
    int i;
    for (i=0; i<10; i++)
    {
        printf("--- %d ---\n", i);
        Py_Initialize();
        PyRun_SimpleString("import ctypes");
        Py_Finalize();
    }
    return 0;
}

$ gcc -I/usr/include/python2.5/ -L/usr/lib/python2.5/ -lpython main.c
$ ./a.out 
--- 0 ---
--- 1 ---
--- 2 ---
--- 3 ---
Assertion failed: (type->tp_flags & Py_TPFLAGS_HEAPTYPE), function 
type_dealloc, file Objects/typeobject.c, line 2148.
Abort trap
$ uname -mprsv
Darwin 9.7.0 Darwin Kernel Version 9.7.0: Tue Mar 31 22:52:17 PDT 2009; 
root:xnu-1228.12.14~1/RELEASE_I386 i386 i386
msg92880 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2009-09-20 02:19
I hope attached patch will fix this issue.
msg102369 - (view) Author: nik (nik.lutz@gmail.com) Date: 2010-04-05 11:44
I can confirm that the patch fix this issue. I adapted the patch (variable names changed):

5523a5524
>         Py_INCREF(&Struct_Type);
5529a5531
>         Py_INCREF(&Union_Type);
5535a5538
>         Py_INCREF(&Pointer_Type);
5541a5545
>         Py_INCREF(&Array_Type);
5547a5552
>         Py_INCREF(&Simple_Type);
5553a5559
>         Py_INCREF(&CFuncPtr_Type); 

Platform: Linux (kubuntu 9.10 x64)
Python: 2.6.4-0ubuntu3
msg113297 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2010-08-08 18:29
Fixed in rev 83841 (py3k), rev 83842 (release31-maint), and rev 83843 (release27-maint).

Thanks for the patch.
msg116211 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-09-12 16:20
I found another refcount bug in Python3.x.
Fixed in r84741(py3k), r84742(release31-maint).
History
Date User Action Args
2010-09-12 16:20:38ocean-citysetmessages: + msg116211
2010-08-08 18:29:31thellersetstatus: open -> closed
resolution: fixed
messages: + msg113297
2010-08-04 22:22:38terry.reedysetstage: patch review
versions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.6, Python 2.5
2010-07-19 10:42:11ocean-citysetnosy: + tanaga
2010-04-05 11:44:33nik.lutz@gmail.comsetnosy: + nik.lutz@gmail.com
messages: + msg102369
2009-11-13 14:24:19robincesetnosy: + robince
2009-09-20 02:19:51ocean-citysetfiles: + fix_ctypes_crash.patch

nosy: + ocean-city
messages: + msg92880

keywords: + patch
2009-09-12 22:12:00mkpsetnosy: + mkp
messages: + msg92555
2009-09-09 12:49:57warlocksetmessages: + msg92449
2009-09-09 08:31:14warlockcreate