ImportError when using Python C API

I have a binary file that can load .soshared objects to extend functionality. These extensions are encoded in C ++, but I want to use some pre-encoded python functions, so I use the Python C API . So far so good.

Calling Python functions works well, but if I import the spidev module in Python , I get the following error:

import spidev
ImportError: /usr/local/lib/python2.7/dist-packages/spidev.so: undefined symbol: _Py_ZeroStruct
Segmentation fault

If I import the standard python modules (sys, os, argparse ...), there is no problem.

What could be the problem?

NB: I know that I could use spidev directly from C ++, but I wanted to use the existing python code as much as possible.

UPDATE:

As @BrianCain and @qarma pointed out, this could be a dependency problem libpython, so I include the outputs ldd:

$ ldd myextension.so 
    /usr/lib/arm-linux-gnueabihf/libcofi_rpi.so (0xb6f89000)
    libpthread.so.0 => /lib/arm-linux-gnueabihf/libpthread.so.0 (0xb6f5f000)
    libdl.so.2 => /lib/arm-linux-gnueabihf/libdl.so.2 (0xb6f54000)
    libutil.so.1 => /lib/arm-linux-gnueabihf/libutil.so.1 (0xb6f49000)
    libm.so.6 => /lib/arm-linux-gnueabihf/libm.so.6 (0xb6ed8000)
    libpython2.7.so.1.0 => /usr/lib/libpython2.7.so.1.0 (0xb6c47000)
    libgcc_s.so.1 => /lib/arm-linux-gnueabihf/libgcc_s.so.1 (0xb6c1f000)
    libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0xb6af0000)
    /lib/ld-linux-armhf.so.3 (0xb6fa2000)
    libz.so.1 => /lib/arm-linux-gnueabihf/libz.so.1 (0xb6ad2000)

$ ldd /usr/local/lib/python2.7/dist-packages/spidev.so
    /usr/lib/arm-linux-gnueabihf/libcofi_rpi.so (0xb6ed3000)
    libpthread.so.0 => /lib/arm-linux-gnueabihf/libpthread.so.0 (0xb6ea9000)
    libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0xb6d7a000)
    /lib/ld-linux-armhf.so.3 (0xb6eed000)

UPDATE2:

The output of the spidev installation.

$ sudo pip install spidev
Downloading/unpacking spidev
  Downloading spidev-2.0.tar.gz
  Running setup.py egg_info for package spidev

Installing collected packages: spidev
  Running setup.py install for spidev
    building 'spidev' extension
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/src/linux/include -I/usr/include/python2.7 -c spidev_module.c -o build/temp.linux-armv6l-2.7/spidev_module.o
    gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-armv6l-2.7/spidev_module.o -o build/lib.linux-armv6l-2.7/spidev.so

Successfully installed spidev
Cleaning up...

$ ldd /usr/local/lib/python2.7/dist-packages/spidev.so
    /usr/lib/arm-linux-gnueabihf/libcofi_rpi.so (0xb6f97000)
    libpthread.so.0 => /lib/arm-linux-gnueabihf/libpthread.so.0 (0xb6f6d000)
    libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0xb6e3e000)
    /lib/ld-linux-armhf.so.3 (0xb6fb1000)

Still independent of libpython...

+4
source share
1 answer

I suspect yours is spidev.sobuilt against the wrong version of Python or poorly created.

Run lddboth in your Python and this lib. If there is a mismatch, hard luck! If not, dig deeper.

Here's how it should work:

(test)[dima@bmg py-spidev]$ python setup.py build
(test)[dima@bmg py-spidev]$ ldd build/lib.linux-x86_64-2.7/spidev.so 
        linux-vdso.so.1 (0x00007fffbadfe000)
        libpython2.7.so.1.0 => /usr/lib/libpython2.7.so.1.0 (0x00007f42c9659000)
        libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f42c943c000)
        libc.so.6 => /usr/lib/libc.so.6 (0x00007f42c9094000)
        libdl.so.2 => /usr/lib/libdl.so.2 (0x00007f42c8e8f000)
        libutil.so.1 => /usr/lib/libutil.so.1 (0x00007f42c8c8c000)
        libm.so.6 => /usr/lib/libm.so.6 (0x00007f42c898b000)
        /usr/lib64/ld-linux-x86-64.so.2 (0x00007f42c9c50000)
(test)[dima@bmg py-spidev]$ python setup.py install
(test)[dima@bmg py-spidev]$ python
>>> import spidev
>>> dir(spidev)
['SpiDev', '__doc__', '__file__', '__name__', '__package__']

Please note that it must be associated with some libpython2.x.so.y.z

Perhaps your target python (if cross compiling) is missing python-config?

0
source

Source: https://habr.com/ru/post/1538591/


All Articles