How to solve Python-C-API error "This is an issue with the package mentioned above, not pip."?
Asked Answered
W

1

2

I'm trying to implement an algorithm in the form of the C programming language into my system that runs using the python programming language. I'm trying to implement the Python C API with the intention that my algorithm will run in a python environment. As a result it produces an error which I have been trying to fix for several days but still can't find it. Here is the result of the error I got:

$ pip install .
Processing c:\users\angga danar\documents\skripsi\project\gimli
  Preparing metadata (setup.py) ... done
Installing collected packages: gimlihash
  DEPRECATION: gimlihash is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559
  Running setup.py install for gimlihash ... error
  error: subprocess-exited-with-error
  
  × Running setup.py install for gimlihash did not run successfully.
  │ exit code: 1
  ╰─> [17 lines of output]
      running install
      C:\python3.10\lib\site-packages\setuptools\command\install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
        warnings.warn(
      running build
      running build_ext
      building 'gimli' extension
      creating build
      creating build\temp.win-amd64-cpython-310
      creating build\temp.win-amd64-cpython-310\Release
      creating build\temp.win-amd64-cpython-310\Release\Hash
      "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -IC:\python3.10\include -IC:\python3.10\Include "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\shared" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\cppwinrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" /TcHash/hashing.c /Fobuild\temp.win-amd64-cpython-310\Release\Hash/hashing.obj
      hashing.c
      creating C:\Users\Angga Danar\Documents\skripsi\Project\Gimli\build\lib.win-amd64-cpython-310
      "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\bin\HostX86\x64\link.exe" /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:\python3.10\libs /LIBPATH:C:\python3.10 /LIBPATH:C:\python3.10\PCbuild\amd64 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\ATLMFC\lib\x64" "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\lib\um\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.22621.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\\lib\10.0.22621.0\\um\x64" /EXPORT:PyInit_gimli build\temp.win-amd64-cpython-310\Release\Hash/hashing.obj /OUT:build\lib.win-amd64-cpython-310\gimli.cp310-win_amd64.pyd /IMPLIB:build\temp.win-amd64-cpython-310\Release\Hash\gimli.cp310-win_amd64.lib
      LINK : error LNK2001: unresolved external symbol PyInit_gimli
      build\temp.win-amd64-cpython-310\Release\Hash\gimli.cp310-win_amd64.lib : fatal error LNK1120: 1 unresolved externals
      error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.34.31933\\bin\\HostX86\\x64\\link.exe' failed with exit code 1120
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> gimlihash

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.

Here is the code for my hashing.c file:

char print_tex(char* string, char* hex_string) {
    uint8_t output[32];
    size_t l = strlen(string);
    Gimli_hash(string, strlen(string), output, 32);
    int i;
    for (i = 0;i < 32;++i)
        fprintf( stderr, "%02x",output[i]);
        hex_string[i] = (char)output[i];
    return 0;
}

static PyObject* hash(PyObject *self, PyObject *args) {
    char *str;
    char final[32];
    if (!PyArg_ParseTuple(args, "s", &str)){
        return NULL;
    }
    print_tex(str, final);
    return PyUnicode_FromString(final);
}

static PyMethodDef hashMethods[] = {
    {"gimli", hash, METH_VARARGS, "Gimli Algorithm Hash"},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef hashModule = {
    PyModuleDef_HEAD_INIT,
    "hashModule", 
    "hash module",
    -1,
    hashMethods
};

PyMODINIT_FUNC PyInit_hash(void) {
    return PyModule_Create(&hashModule);
}

Basically I implemented a hashing algorithm where the input is in the form of a string and the resulting output is also in the form of the result string of the algorithm. I tried to install the file using the following setup.py file using the pip install . command

from setuptools import setup, Extension

setup(name = "gimlihash",
        version = "0.1",
        ext_modules = [Extension("gimli", ["Hash/hashing.c"])])

Hopefully I can create a local library for my project and I can call the library. Please help because I'm still a beginner.

Wayfaring answered 1/2, 2023 at 6:33 Comment(1)
I assume that PyMODINIT_FUNC PyInit_gimLi(void) { ... } instead of PyInit_hash might work as well.Disrespectful
G
2

According to [Python.Docs]: Extending Python with C or C++ - The Module’s Method Table and Initialization Function (emphasis is mine):

This structure, in turn, must be passed to the interpreter in the module’s initialization function. The initialization function must be named PyInit_name(), where name is the name of the module, and should be the only non-static item defined in the module file:

So, there's a discrepancy between:

  • Module name: gimli (setup.py)

  • Initialization function name: PyInit_hash (hashing.c)

In order for things to be OK, the 2 must match, so either:

  • Use hash everywhere (Extension("hash", ...)

  • Use gimli everywhere (PyMODINIT_FUNC PyInit_gimli(..., as @RetiredNinja suggested in the comment)

Since we're talking about an extension module (which most likely will be part of a package), there's a general convention (not necessarily followed by everyone) that their names start with an UnderScore (_). Therefore, I suggest to name your module _hash. The required changes would be:

  1. setup.py:

    • Module name

      # ...
      ext_modules = [Extension("_hash", ["Hash/hashing.c"])])
      
  2. hashing.c:

    • Function name:

      PyMODINIT_FUNC PyInit__hash(void) {  // @TODO - cfati: Notice the double UnderScore: __
      // ...
      
    • Module name (optional):

      // ...
      PyModuleDef_HEAD_INIT,
      "_hash",  // @TODO - cfati: This is what `_hash.__name__` will return
      "hash module",
      // ...
      

Might also want to check:

Gwen answered 1/2, 2023 at 8:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.