erocarrera / pefile

pefile is a Python module to read and work with PE (Portable Executable) files
MIT License
1.83k stars 516 forks source link

_abc_impl is set to a wrong type in cython #387

Closed selfDepolyed closed 7 months ago

selfDepolyed commented 7 months ago

First of all, unfortunately, I am not able to provide a fully reproducible code due to the size and structure of code.

I have the cython code below:

modulename.pyx:

` cdef char *fileThere(): exe = None try: exe = pefile.PE(b'path/file.exe') # also tried filePath as str print("After ", exe) except (pefile.PEFormatError, Exception) as e: print("Failed to load file", e) return "error"

cdef public int mainfunc(char *cmd): fileThere()

`

This is later called in a C as below:

`

int main(){ PyObject pmodule; wchar_t program;

program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
    fprintf(stderr, "Fatal error: cannot decode argv[0], got %d arguments\n", argc);
    exit(1);
}

if (PyImport_AppendInittab("modulename", PyInit_modulename) == -1) {
    fprintf(stderr, "Error: could not extend in-built modules table\n");
    exit(1);
}
Py_SetProgramName(program);

Py_Initialize();

pmodule = PyImport_ImportModule("modulename");
if (!pmodule) {
    PyErr_Print();
    fprintf(stderr, "Error: could not import module 'embedded'\n");
    return -1;
}

PyObject* sys = PyImport_ImportModule("sys");
PyObject* path = PyObject_GetAttrString(sys, "path");
PyList_Append(path, PyUnicode_FromString("."));
PyList_Append(path, PyUnicode_FromString("C:/Users/username/AppData/Local/Programs/Python/python39/"));

/* 1st: Import the module */
PyObject* ModuleString = PyUnicode_FromString((char*)"modulename");
if (!ModuleString) {
    PyErr_Print();
    printf("Error formating python script\n");
}

PyObject* Module = PyImport_Import(ModuleString);
if (!Module) {
    PyErr_Print();
    printf("Error importing python script\n");
}

mainfunc("start");

Py_Finalize();
return 0;

}`

Pefile.Pe() throws the following exception: "_abc_impl is set to a wrong type".

I removed the try and catch surround the Pefile.Pe() and got the exception below: ` Traceback (most recent call last): File "modulename.pyx", line 1237, in modulename.fileThere exe = pefile.PE("./0_5555.exe") File "C:\Users\username\AppData\Local\Programs\Python\Python312\Lib\site-packages\pefile.py", line 1754, in init self.parse(name, data, fast_load) File "C:\Users\username\AppData\Local\Programs\Python\Python312\Lib\site-packages\pefile.py", line 1823, in parse for byte, byte_count in Counter(bytearray(self.data)).items(): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\username\AppData\Local\Programs\Python\Python312\Lib\collections__init.py", line 607, in init self.update(iterable, **kwds) File "C:\Users\username\AppData\Local\Programs\Python\Python312\Lib\collections\init.py", line 689, in update if isinstance(iterable, _collections_abc.Mapping): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 119, in instancecheck__ TypeError: _abc_impl is set to a wrong type

`

After doing some research, I thought this most probably be a compatibility issue or a big. Can anyone assist?

selfDepolyed commented 7 months ago

The issues seems to be that when you create a seprarte GIL from C using "Py_NewInterpreterFromConfig"

selfDepolyed commented 7 months ago

Issue resides on collections package after further investigation