Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C Extension module
#1
Hello I wrote simple c extension module and assembled it with visual studio everything works
When i try to assemble it with mingw i am getting issue when i try to run my module.

platform Win10
mingw: 2013072200
python 3.11

cpython.c
#include "stdio.h"
#include <Python.h> 

static PyObject *
cpython1_print_info(PyObject *self, PyObject *args)
{
    printf("vasia pupkin 2\n");
    //print_info();

    return Py_BuildValue("");
}

static PyMethodDef cpython1Methods[] = {
    {"print_info",  cpython1_print_info, METH_VARARGS, "call function from static lib which prints something"},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

static struct PyModuleDef cpython1 = 
{
    PyModuleDef_HEAD_INIT,
    "cpython1",
    "cpython1 print_info",
    -1, //global states
    cpython1Methods
};

PyMODINIT_FUNC
PyInit_cpython2(void)
{
    return PyModule_Create(&cpython1);
}
setup.py
from setuptools import setup, Extension

module=Extension("cpython2", 
                 sources=['src/cpython2.c'],
                 include_dirs=['src','lib'],
                 library_dirs=['lib', 'c:/Program Files (x86)/Windows Kits/10/Lib/10.0.22000.0/ucrt/x86/'],
                 libraries=['mylib_lib'],
                 extra_compile_args=["-m32"])

setup(name='cpython2',
      version='1.0.1',
      description='Python API for cpython1.c',
      author='Vasia Pupkin',
      author_email='[email protected]',
      #ext_modules=[Extension('cpython1', ['src/cpython1.c'], include_dirs=['src'])],
      ext_modules=[module],
      py_modules=['cpython2']
)
Whein i try to run module i see this
C:\Appl\test\mingw\cpython2\build\lib.win32-cpython-311>dir

 Directory of C:\Appl\test\mingw\cpython2\build\lib.win32-cpython-311

01/12/2023  04:42 PM    <DIR>          .
01/12/2023  04:42 PM    <DIR>          ..
01/12/2023  04:42 PM            10,752 cpython2.cp311-win32.pyd
               1 File(s)         10,752 bytes
               2 Dir(s)  69,593,444,352 bytes free

C:\Appl\test\mingw\cpython2\build\lib.win32-cpython-311>python
Python 3.11.1 (tags/v3.11.1:a7a450f, Dec  6 2022, 19:43:28) [MSC v.1934 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cpython2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: DLL load failed while importing cpython2: A dynamic link library (DLL) initialization routine failed.
>>>
if run "python -vv" then output will look like this
>>> import cpython2
# trying C:\Appl\test\mingw\cpython2\build\lib.win32-cpython-311\cpython2.cp311-win32.pyd
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 676, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 573, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 1233, in create_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
ImportError: DLL load failed while importing cpython2: A dynamic link library (DLL) initialization routine failed.
>>>
Reply
#2
I don't know if you can use mingw. Python for windows is compiled using Visual Studio. Compatibility issues are mentioned here

https://matthew-brett.github.io/pydagogu...ython.html
https://github.com/microsoft/vscode/issues/100764
https://support.enthought.com/hc/en-us/a...ght-Python
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python C Extension Module loading issue on Cygwin mesibo 0 667 Sep-22-2023, 05:41 AM
Last Post: mesibo
  module requests, .text extension Truman 1 2,766 Jul-19-2018, 11:47 PM
Last Post: gontajones

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020