cpython

Why 'Segmentation fault' occurs in my cpython module code? (PyObject_Print)


I am trying to print PyObject and 'Segmentation fault' occured. I want to use PyObject_Print. How can I solve the problem?

int main(){
    Py_Initialize();
    ///////////////////////////////////////////
    PyObject *tmp1 = PyLong_FromLong((1<<29)*3);
    PyObject *tmp2 = PyLong_FromLong((1<<20)*41+1264);
    PyObject *u;
    u = PyNumber_Multiply(tmp1,tmp2);
    PyObject_Print(u,stdout,Py_PRINT_RAW); // Segmentation fault here

    const char* uu = PyUnicode_AsUTF8(PyObject_Repr(u));
    printf("%s\n", uu); // it works
    Py_Finalize();
    return 0;
}

I am debugging with vscode.

My tasks.json is

       {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\msys64\\mingw64\\bin\\gcc.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-I",
                "C:/Users/.../AppData/Local/Programs/Python/Python37/include",
                "-L",
                "C:/Users/.../AppData/Local/Programs/Python/Python37/libs",
                "-lpython37",
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }

I redownloaded whole python3.7 and it did not help I tried python3.11 cpython in anaconda, and it still 'Segmentation fault' occured.

Edit

I found python3.10.10 in mingw64. I changed my local python version to 3.10.10. However, still I have an error.

message: Thread 1 received signal SIGSEGV, Segmentation fault. ntdll!TpWorkOnBehalfClearTicket () from C:\WINDOWS\SYSTEM32\ntdll.dll

I guess the detail error was different from python3.7


Solution

  • Python is built on Win using (native) VStudio, so this configuration (MinGW) is unsupported. Some references:

    I tried reproducing the behavior in several environments.

    main00.c:

    #include <stdio.h>
    #include <Python.h>
    
    
    int main()
    {
        Py_Initialize();
    
        PyObject *tmp1 = PyLong_FromLong((1 << 29) * 3);
        PyObject *tmp2 = PyLong_FromLong((1 << 20) * 41 + 1264);
        PyObject *prod = PyNumber_Multiply(tmp1, tmp2);
        const char *prods = PyUnicode_AsUTF8(PyObject_Repr(prod));
    
        printf("printf: %s\n", prods);
        printf("Before PyObject_Print\n");
    #if defined FILE_NAME
        FILE *fo = fopen(FILE_NAME, "w");
    #else
        FILE *fo = stdout;
    #endif
        PyObject_Print(prod, fo, Py_PRINT_RAW);
    #if defined FILE_NAME
        fclose(fo);
    #endif
        printf("\nAfter PyObject_Print\n");
    
        Py_Finalize();
        printf("\nDone.\n\n");
        return 0;
    }
    

    I used Python 3.10 (where possible).

    Outputs:

    1. MinGW:

      [cfati@CFATI-5510-0:e:\Work\Dev\StackExchange\StackOverflow\q076148413]> sopr.bat
      ### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
      
      [prompt]> "f:\Install\pc064\MinGW\MinGW-W64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw-w64.bat"
      
      [prompt]> echo off
      Microsoft Windows [Version 10.0.19045.2913]
      (c) Microsoft Corporation. All rights reserved.
      
      [prompt]> md mgwgcc
      
      [prompt]> cd mgwgcc
      
      [prompt]>
      [prompt]> gcc -g ..\main00.c -Ic:\Install\pc064\Python\Python\03.10\include -Lc:\Install\pc064\Python\Python\03.10\libs -lpython310 -o pc064_py0310.exe
      
      [prompt]>
      [prompt]> pc064_py0310.exe
      
      [prompt]>
      [prompt]> set PATH=%PATH%;c:\Install\pc064\Python\Python\03.10
      
      [prompt]> pc064_py0310.exe
      printf: 69244880085319680
      Before PyObject_Print
      :: ------- @TODO - cfati: Crashed here -------
      
    2. VStudio:

      [cfati@CFATI-5510-0:e:\Work\Dev\StackExchange\StackOverflow\q076148413]> sopr.bat
      ### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
      
      [prompt]> "c:\Install\pc032\Microsoft\VisualStudioCommunity\2019\VC\Auxiliary\Build\vcvarsall.bat" x64 > nul
      
      [prompt]> md vs
      
      [prompt]> cd vs
      
      [prompt]> cl -nologo ..\main00.c -Ic:\Install\pc064\Python\Python\03.10\include  -link -NOLOGO -LIBPATH:c:\Install\pc064\Python\Python\03.10\libs python310.lib -OUT:pc064_py0310.exe
      main00.c
      
      [prompt]>
      [prompt]> set PATH=%PATH%;c:\Install\pc064\Python\Python\03.10
      
      [prompt]> pc064_py0310.exe
      printf: 69244880085319680
      Before PyObject_Print
      69244880085319680
      After PyObject_Print
      
      Done.
      
    3. WSL:

      (py_pc064_03.10_test0) [cfati@cfati-5510-0:/mnt/e/Work/Dev/StackExchange/StackOverflow/q076148413]> ~/sopr.sh
      ### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
      
      [064bit prompt]> uname -a
      Linux cfati-5510-0 5.15.90.1-microsoft-standard-WSL2 #1 SMP Fri Jan 27 02:56:13 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
      [064bit prompt]> md wslgcc
      [064bit prompt]> cd wslgcc
      [064bit prompt]>
      [064bit prompt]> gcc -g ../main00.c -I/usr/include/python3.10 -L/usr/lib/python3.10 -lpython3.10 -o pc064_py0310
      [064bit prompt]>
      [064bit prompt]> ./pc064_py0310
      printf: 69244880085319680
      Before PyObject_Print
      69244880085319680
      After PyObject_Print
      
      Done.
      
    4. MSYS2:

      [cfati@cfati-5510-0:/e/Work/Dev/StackExchange/StackOverflow/q076148413]> ~/sopr.sh
      ### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
      
      [064bit prompt]> uname -a
      MSYS_NT-10.0-19045 cfati-5510-0 3.4.6.x86_64 2023-04-01 11:43 UTC x86_64 Msys
      [064bit prompt]> md msysgcc
      [064bit prompt]> cd msysgcc
      [064bit prompt]>
      [064bit prompt]> gcc -g ../main00.c -I/usr/include/python3.11 -L/usr/lib/python3.11 -lpython3.11 -o pc064_py0311
      [064bit prompt]>
      [064bit prompt]> ./pc064_py0311.exe
      printf: 69244880085319680
      Before PyObject_Print
      69244880085319680
      After PyObject_Print
      
      Done.
      
      [064bit prompt]>
      [064bit prompt]> gcc -g ../main00.c -I/c/Install/pc064/Python/Python/03.11/include -L/c/Install/pc064/Python/Python/03.11/libs -lpython3.11 -o pc064_py0311win  In file included from /c/Install/pc064/Python/Python/03.11/include/Python.h:38,
                       from ../main00.c:2:
      /c/Install/pc064/Python/Python/03.11/include/pyport.h:601:2: error: #error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
        601 | #error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
      
    5. Cygwin:

      [cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackExchange/StackOverflow/q076148413]> ~/sopr.sh
      ### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
      
      [064bit prompt]> uname -a
      CYGWIN_NT-10.0-19045 cfati-5510-0 3.4.6-1.x86_64 2023-02-14 13:23 UTC x86_64 Cygwin
      [064bit prompt]> md cyggcc
      [064bit prompt]> cd cyggcc
      [064bit prompt]>
      [064bit prompt]> gcc -g ../main00.c -I/usr/include/python3.9 -L/usr/lib/python3.9 -lpython3.9 -o pc064_py0309
      [064bit prompt]>
      [064bit prompt]> ./pc064_py0309
      printf: 69244880085319680
      Before PyObject_Print
      69244880085319680
      After PyObject_Print
      
      Done.
      
      [064bit prompt]>
      [064bit prompt]> gcc -g ../main00.c -I/cygdrive/c/Install/pc064/Python/Python/03.09/include -L/cygdrive/c/Install/pc064/Python/Python/03.09/libs -lpython3.9 -o pc064_py0309win
      In file included from /cygdrive/c/Install/pc064/Python/Python/03.09/include/Python.h:50,
                       from ../main00.c:2:
      /cygdrive/c/Install/pc064/Python/Python/03.09/include/pyport.h:741:2: error: #error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
        741 | #error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
            |  ^~~~~
      # ----- @TODO - cfati: Truncated some lines ----
      

    As seen, "regular" configurations work. In the last 2 examples, trying to use the native Python doesn't even compile.
    I didn't attempt any debugging, and I'm not going to. You should stick with officially supported configurations, pick whichever suits you best. I'd go with the official one (VStudio), here are some examples: