Installing/compiling pylzma (lzma python binding)

 ̄綄美尐妖づ 提交于 2019-12-06 05:31:57

I have installed pylzma to try this out myself. For python 2.6 it builds fine with mingw, but msvc compiler chokes for python2.4-2.7. Let's see errors first:

src/pylzma/pylzma.c(102) : error C2275: 'CSha256' : illegal use of this type as an expression c:\users\xavier.lapointe\appdata\local\temp\easy_install-2mfkqu\pylzma-0.4.3\src\pylzma../7zip/C/Sha256.h(18) : see declaration of 'CSha256'

Code in question is:

} else {
    Py_BEGIN_ALLOW_THREADS
    Sha256_Init(&sha);
    CSha256 sha;
    long round;
    int i;
    long rounds = (long) 1 << cycles;
    unsigned char temp[8] = { 0,0,0,0,0,0,0,0 };
    for (round = 0; round < rounds; round++) {

which is clearly invalid C since it's not allowed to declare variables after code. When you renamed .c to .cpp you avoided this error since C++ allows for this (and mingw supports this for C apparently). But switching to C++ led to name mangling and linking errors. To fix those you could place extern "C" { ... } around all the code.

It's better to fix src/pylzma/pylzma.c htough and the fix is trivial - move Py_BEGIN_ALLOW_THREADS and Sha256_Init(&sha); after variable declarations:

} else {
    CSha256 sha;
    long round;
    int i;
    long rounds = (long) 1 << cycles;
    unsigned char temp[8] = { 0,0,0,0,0,0,0,0 };
    Py_BEGIN_ALLOW_THREADS
    Sha256_Init(&sha);
    for (round = 0; round < rounds; round++) {

Now pylzma compiles fine but fails to run manifest tool after linking:

Creating library build\temp.win-amd64-2.6\Release\src/pylzma\pylzma.lib and o bject build\temp.win-amd64-2.6\Release\src/pylzma\pylzma.exp C:\Program Files\Microsoft SDKs\Windows\v7.0\bin\x64\mt.exe -nologo -manifest bu ild\temp.win-amd64-2.6\Release\src/pylzma\pylzma.pyd.manifest -outputresource:bu ild\lib.win-amd64-2.6\pylzma.pyd;2

build\temp.win-amd64-2.6\Release\src/pylzma\pylzma.pyd.manifest : general error c1010070: Failed to load and parse the manifest. The system cannot find the file specified. error: command 'mt.exe' failed with exit status 31

Looking in build directory reveals that there is no pylzma.pyd.manifest there though it's seen from output that link.exe has /MANIFEST:... switch. Quick googling for "link didn't create manifest" finds http://bugs.python.org/issue4431 where it's explained that when using /MT switch manifest doesn't get created and the solution is to add /MANIFEST to linker flags. Alright, let's edit pylzma's setup.py to add that linker flag for MSVC compiler:

    if isinstance(self.compiler, MSVCCompiler):
        # set flags only available when using MSVC
        ext.extra_link_args.append('/MANIFEST') # force linker to create manifest
        if COMPILE_DEBUG:
            ext.extra_compile_args.append('/Zi')
            ext.extra_compile_args.append('/MTd')
            ext.extra_link_args.append('/DEBUG')
        else:
            ext.extra_compile_args.append('/MT')

Voila, pylzma builds with MSVC fine now. I have tested building with python 2.4-2.7 32-bit and 2.6-2.7 64-bit:

08.02.2011  10:08            71 844 pylzma-0.4.3dev-py2.4-win32.egg
08.02.2011  10:09            71 480 pylzma-0.4.3dev-py2.5-win32.egg
08.02.2011  10:07            79 358 pylzma-0.4.3dev-py2.6-win-amd64.egg
08.02.2011  10:09            75 637 pylzma-0.4.3dev-py2.6-win32.egg
08.02.2011  10:08            79 259 pylzma-0.4.3dev-py2.7-win-amd64.egg
08.02.2011  10:09            75 540 pylzma-0.4.3dev-py2.7-win32.egg
               6 File(s)        453 118 bytes

As of general approach to any errors - it probably requires some knowledge and experience to understand what's behind them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!